diff --git a/window/src/connection.rs b/window/src/connection.rs new file mode 100644 index 000000000..8f022edd3 --- /dev/null +++ b/window/src/connection.rs @@ -0,0 +1,30 @@ +use crate::Connection; +use failure::Fallible; +use std::cell::RefCell; +use std::rc::Rc; + +thread_local! { + static CONN: RefCell>> = RefCell::new(None); +} + +pub trait ConnectionOps { + fn get() -> Option> { + let mut res = None; + CONN.with(|m| { + if let Some(mux) = &*m.borrow() { + res = Some(Rc::clone(mux)); + } + }); + res + } + + fn init() -> Fallible> { + let conn = Rc::new(Connection::create_new()?); + CONN.with(|m| *m.borrow_mut() = Some(Rc::clone(&conn))); + Ok(conn) + } + + fn terminate_message_loop(&self); + fn run_message_loop(&self) -> Fallible<()>; + fn spawn_task + 'static>(&self, future: F); +} diff --git a/window/src/lib.rs b/window/src/lib.rs index eab4cb108..19d9a8c79 100644 --- a/window/src/lib.rs +++ b/window/src/lib.rs @@ -1,13 +1,14 @@ use std::any::Any; pub mod bitmaps; pub mod color; +pub mod connection; pub mod input; pub mod os; mod tasks; pub use bitmaps::BitmapImage; pub use color::Color; - +pub use connection::*; pub use input::*; pub use os::*; diff --git a/window/src/os/macos/connection.rs b/window/src/os/macos/connection.rs index 0d7ce857f..b0b9e8c09 100644 --- a/window/src/os/macos/connection.rs +++ b/window/src/os/macos/connection.rs @@ -1,4 +1,5 @@ use super::window::WindowInner; +use crate::connection::ConnectionOps; use crate::tasks::{Task, Tasks}; use cocoa::appkit::{NSApp, NSApplication, NSApplicationActivationPolicyRegular}; use cocoa::base::{id, nil}; @@ -24,9 +25,6 @@ struct SpawnQueue { spawned_funcs: Mutex>, } -thread_local! { - static CONN: RefCell>> = RefCell::new(None); -} lazy_static::lazy_static! { static ref SPAWN_QUEUE: Arc = Arc::new(SpawnQueue::new().expect("failed to create SpawnQueue")); } @@ -82,17 +80,7 @@ impl SpawnQueue { } impl Connection { - pub fn get() -> Option> { - let mut res = None; - CONN.with(|m| { - if let Some(mux) = &*m.borrow() { - res = Some(Rc::clone(mux)); - } - }); - res - } - - pub fn init() -> Fallible> { + pub(crate) fn create_new() -> Fallible { // Ensure that the SPAWN_QUEUE is created; it will have nothing // to run right now. SPAWN_QUEUE.run(); @@ -100,35 +88,16 @@ impl Connection { unsafe { let ns_app = NSApp(); ns_app.setActivationPolicy_(NSApplicationActivationPolicyRegular); - let conn = Rc::new(Self { + let conn = Self { ns_app, windows: RefCell::new(HashMap::new()), tasks: Default::default(), next_window_id: AtomicUsize::new(1), - }); - CONN.with(|m| *m.borrow_mut() = Some(Rc::clone(&conn))); + }; Ok(conn) } } - pub fn terminate_message_loop(&self) { - unsafe { - let () = msg_send![NSApp(), stop: nil]; - } - } - - pub fn run_message_loop(&self) -> Fallible<()> { - unsafe { - self.ns_app.run(); - } - self.windows.borrow_mut().clear(); - Ok(()) - } - - pub fn executor() -> impl BasicExecutor { - SpawnQueueExecutor {} - } - pub(crate) fn next_window_id(&self) -> usize { self.next_window_id .fetch_add(1, ::std::sync::atomic::Ordering::Relaxed) @@ -150,11 +119,6 @@ impl Connection { })); } - pub fn spawn_task + 'static>(&self, future: F) { - let id = self.tasks.add_task(Task(Box::pin(future))); - Self::wake_task_by_id(id); - } - pub(crate) fn wake_task_by_id(slot: usize) { SpawnQueueExecutor {}.execute(Box::new(move || { let conn = Connection::get().unwrap(); @@ -163,6 +127,27 @@ impl Connection { } } +impl ConnectionOps for Connection { + fn terminate_message_loop(&self) { + unsafe { + let () = msg_send![NSApp(), stop: nil]; + } + } + + fn run_message_loop(&self) -> Fallible<()> { + unsafe { + self.ns_app.run(); + } + self.windows.borrow_mut().clear(); + Ok(()) + } + + fn spawn_task + 'static>(&self, future: F) { + let id = self.tasks.add_task(Task(Box::pin(future))); + Self::wake_task_by_id(id); + } +} + struct SpawnQueueExecutor; impl BasicExecutor for SpawnQueueExecutor { fn execute(&self, f: SpawnFunc) { diff --git a/window/src/os/macos/window.rs b/window/src/os/macos/window.rs index 66e37d621..d323d136d 100644 --- a/window/src/os/macos/window.rs +++ b/window/src/os/macos/window.rs @@ -1,5 +1,6 @@ use super::{nsstring, nsstring_to_str}; use crate::bitmaps::Image; +use crate::connection::ConnectionOps; use crate::os::macos::bitmap::BitmapRef; use crate::{ BitmapImage, Color, Connection, Dimensions, KeyCode, KeyEvent, Modifiers, MouseButtons,