diff --git a/window/src/os/windows/connection.rs b/window/src/os/windows/connection.rs index 20eb00df8..cb56011e6 100644 --- a/window/src/os/windows/connection.rs +++ b/window/src/os/windows/connection.rs @@ -1,6 +1,7 @@ //! The connection to the GUI subsystem use super::EventHandle; use super::{HWindow, WindowInner}; +use crate::connection::ConnectionOps; use failure::Fallible; use promise::{BasicExecutor, SpawnFunc}; use std::cell::RefCell; @@ -17,10 +18,6 @@ lazy_static::lazy_static! { static ref SPAWN_QUEUE: Arc = Arc::new(SpawnQueue::new().expect("failed to create SpawnQueue")); } -thread_local! { - static CONN: RefCell>> = RefCell::new(None); -} - struct SpawnQueue { spawned_funcs: Mutex>, event_handle: EventHandle, @@ -58,38 +55,14 @@ pub struct Connection { pub(crate) windows: Mutex>>>, } -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 - } - - fn new() -> Self { - let event_handle = SPAWN_QUEUE.event_handle.0; - Self { - event_handle, - windows: Mutex::new(HashMap::new()), - } - } - - pub fn init() -> Fallible> { - let conn = Rc::new(Self::new()); - CONN.with(|m| *m.borrow_mut() = Some(Rc::clone(&conn))); - Ok(conn) - } - - pub fn terminate_message_loop(&self) { +impl ConnectionOps for Connection { + fn terminate_message_loop(&self) { unsafe { PostQuitMessage(0); } } - pub fn run_message_loop(&self) -> Fallible<()> { + fn run_message_loop(&self) -> Fallible<()> { let mut msg: MSG = unsafe { std::mem::zeroed() }; loop { SPAWN_QUEUE.run(); @@ -114,6 +87,24 @@ impl Connection { } } + fn spawn_task + 'static>(&self, future: F) { + unimplemented!(); + } + + fn wake_task_by_id(slot: usize) { + unimplemented!(); + } +} + +impl Connection { + pub(crate) fn create_new() -> Fallible { + let event_handle = SPAWN_QUEUE.event_handle.0; + Ok(Self { + event_handle, + windows: Mutex::new(HashMap::new()), + }) + } + fn wait_message(&self) { unsafe { MsgWaitForMultipleObjects(1, &self.event_handle, 0, INFINITE, QS_ALLEVENTS); diff --git a/window/src/os/windows/window.rs b/window/src/os/windows/window.rs index 04670550e..6feeddfc1 100644 --- a/window/src/os/windows/window.rs +++ b/window/src/os/windows/window.rs @@ -2,12 +2,14 @@ use super::gdi::*; use super::*; use crate::bitmaps::*; use crate::color::Color; +use crate::connection::ConnectionOps; use crate::{ Dimensions, KeyCode, KeyEvent, Modifiers, MouseButtons, MouseCursor, MouseEvent, MouseEventKind, MousePress, Operator, PaintContext, WindowCallbacks, WindowOps, WindowOpsMut, }; use failure::Fallible; use promise::Future; +use std::any::Any; use std::cell::RefCell; use std::io::Error as IoError; use std::ptr::{null, null_mut}; @@ -248,6 +250,16 @@ impl WindowOps for Window { let title = title.to_owned(); Connection::with_window_inner(self.0, move |inner| inner.set_title(&title)); } + + fn apply(&self, func: F) + where + Self: Sized, + { + Connection::with_window_inner(self.0, move |inner| { + let window = Window(inner.hwnd); + func(inner.callbacks.as_any(), &window); + }); + } } /// Set up bidirectional pointers: