1
0
Fork 0

Split off AppRunner from WindowHandle on Windows and Linux

This commit is contained in:
Joakim Frostegård 2020-11-29 17:33:14 +01:00
parent c1d04b978e
commit 35a03aff17
2 changed files with 58 additions and 9 deletions

View file

@ -207,11 +207,25 @@ pub struct Window {
hwnd: HWND, hwnd: HWND,
} }
pub struct WindowHandle { pub struct WindowHandle<H: WindowHandler> {
// FIXME: replace this with channel sender
phantom_data: std::marker::PhantomData<H::Message>,
}
impl <H: WindowHandler>WindowHandle<H> {
pub fn try_send_message(
&mut self,
message: H::Message
) -> Result<(), H::Message> {
Err(message)
}
}
pub struct AppRunner {
hwnd: HWND, hwnd: HWND,
} }
impl WindowHandle { impl AppRunner {
pub fn app_run_blocking(self) { pub fn app_run_blocking(self) {
unsafe { unsafe {
let mut msg: MSG = std::mem::zeroed(); let mut msg: MSG = std::mem::zeroed();
@ -234,7 +248,7 @@ impl Window {
pub fn open<H, B>( pub fn open<H, B>(
options: WindowOpenOptions, options: WindowOpenOptions,
build: B build: B
) -> crate::WindowHandle ) -> (crate::WindowHandle<H>, Option<crate::AppRunner>)
where H: WindowHandler, where H: WindowHandler,
B: FnOnce(&mut crate::Window) -> H, B: FnOnce(&mut crate::Window) -> H,
B: Send + 'static B: Send + 'static
@ -311,7 +325,17 @@ impl Window {
SetWindowLongPtrA(hwnd, GWLP_USERDATA, Box::into_raw(window_state) as *const _ as _); SetWindowLongPtrA(hwnd, GWLP_USERDATA, Box::into_raw(window_state) as *const _ as _);
SetTimer(hwnd, 4242, 13, None); 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)
} }
} }
} }

View file

@ -30,12 +30,25 @@ pub struct Window {
new_physical_size: Option<PhySize> new_physical_size: Option<PhySize>
} }
// FIXME: move to outer crate context pub struct WindowHandle<H: WindowHandler> {
pub struct WindowHandle { // FIXME: replace this with channel sender
phantom_data: std::marker::PhantomData<H::Message>,
}
impl <H: WindowHandler>WindowHandle<H> {
pub fn try_send_message(
&mut self,
message: H::Message
) -> Result<(), H::Message> {
Err(message)
}
}
pub struct AppRunner {
thread: std::thread::JoinHandle<()>, thread: std::thread::JoinHandle<()>,
} }
impl WindowHandle { impl AppRunner {
pub fn app_run_blocking(self) { pub fn app_run_blocking(self) {
let _ = self.thread.join(); let _ = self.thread.join();
} }
@ -47,11 +60,13 @@ impl Window {
pub fn open<H, B>( pub fn open<H, B>(
options: WindowOpenOptions, options: WindowOpenOptions,
build: B build: B
) -> crate::WindowHandle ) -> (crate::WindowHandle<H>, Option<crate::AppRunner>)
where H: WindowHandler, where H: WindowHandler,
B: FnOnce(&mut crate::Window) -> H, B: FnOnce(&mut crate::Window) -> H,
B: Send + 'static B: Send + 'static
{ {
let is_not_parented = matches!(options.parent, Parent::None);
let (tx, rx) = mpsc::sync_channel::<WindowOpenResult>(1); let (tx, rx) = mpsc::sync_channel::<WindowOpenResult>(1);
let thread = thread::spawn(move || { let thread = thread::spawn(move || {
@ -63,7 +78,17 @@ impl Window {
// FIXME: placeholder types for returning errors in the future // FIXME: placeholder types for returning errors in the future
let _ = rx.recv(); 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<H, B>(options: WindowOpenOptions, build: B, fn window_thread<H, B>(options: WindowOpenOptions, build: B,