From 35a03aff171f74329ed0f95e5d7063081375d29a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Sun, 29 Nov 2020 17:33:14 +0100 Subject: [PATCH] Split off AppRunner from WindowHandle on Windows and Linux --- src/win/window.rs | 32 ++++++++++++++++++++++++++++---- src/x11/window.rs | 35 ++++++++++++++++++++++++++++++----- 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/src/win/window.rs b/src/win/window.rs index 8fd2bc2..06ff2a2 100644 --- a/src/win/window.rs +++ b/src/win/window.rs @@ -207,11 +207,25 @@ pub struct Window { hwnd: HWND, } -pub struct WindowHandle { +pub struct WindowHandle { + // FIXME: replace this with channel sender + phantom_data: std::marker::PhantomData, +} + +impl WindowHandle { + pub fn try_send_message( + &mut self, + message: H::Message + ) -> Result<(), H::Message> { + Err(message) + } +} + +pub struct AppRunner { hwnd: HWND, } -impl WindowHandle { +impl AppRunner { pub fn app_run_blocking(self) { unsafe { let mut msg: MSG = std::mem::zeroed(); @@ -234,7 +248,7 @@ impl Window { pub fn open( options: WindowOpenOptions, build: B - ) -> crate::WindowHandle + ) -> (crate::WindowHandle, Option) where H: WindowHandler, B: FnOnce(&mut crate::Window) -> H, B: Send + 'static @@ -311,7 +325,17 @@ impl Window { SetWindowLongPtrA(hwnd, GWLP_USERDATA, Box::into_raw(window_state) as *const _ as _); SetTimer(hwnd, 4242, 13, None); - crate::WindowHandle(WindowHandle { hwnd }) + let window_handle = crate::WindowHandle(WindowHandle { + phantom_data: std::marker::PhantomData, + }); + + let opt_app_runner = if let crate::Parent::None = options.parent { + Some(crate::AppRunner(AppRunner { hwnd })) + } else { + None + }; + + (window_handle, opt_app_runner) } } } diff --git a/src/x11/window.rs b/src/x11/window.rs index caceaa4..049ffc1 100644 --- a/src/x11/window.rs +++ b/src/x11/window.rs @@ -30,12 +30,25 @@ pub struct Window { new_physical_size: Option } -// FIXME: move to outer crate context -pub struct WindowHandle { +pub struct WindowHandle { + // FIXME: replace this with channel sender + phantom_data: std::marker::PhantomData, +} + +impl WindowHandle { + pub fn try_send_message( + &mut self, + message: H::Message + ) -> Result<(), H::Message> { + Err(message) + } +} + +pub struct AppRunner { thread: std::thread::JoinHandle<()>, } -impl WindowHandle { +impl AppRunner { pub fn app_run_blocking(self) { let _ = self.thread.join(); } @@ -47,11 +60,13 @@ impl Window { pub fn open( options: WindowOpenOptions, build: B - ) -> crate::WindowHandle + ) -> (crate::WindowHandle, Option) where H: WindowHandler, B: FnOnce(&mut crate::Window) -> H, B: Send + 'static { + let is_not_parented = matches!(options.parent, Parent::None); + let (tx, rx) = mpsc::sync_channel::(1); let thread = thread::spawn(move || { @@ -63,7 +78,17 @@ impl Window { // FIXME: placeholder types for returning errors in the future let _ = rx.recv(); - crate::WindowHandle(WindowHandle { thread }) + let window_handle = crate::WindowHandle(WindowHandle { + phantom_data: std::marker::PhantomData + }); + + let opt_app_runner = if is_not_parented { + Some(crate::AppRunner(AppRunner { thread })) + } else { + None + }; + + (window_handle, opt_app_runner) } fn window_thread(options: WindowOpenOptions, build: B,