From 43cdc39335c1d44c973292fdfb2b6c16383b0870 Mon Sep 17 00:00:00 2001 From: William Light Date: Tue, 20 Oct 2020 21:31:44 +0200 Subject: [PATCH 1/2] x11: closure for building WindowHandler --- examples/open_window.rs | 10 +++------- src/lib.rs | 2 -- src/x11/window.rs | 19 +++++++++++++------ 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/examples/open_window.rs b/examples/open_window.rs index 911a039..fe77455 100644 --- a/examples/open_window.rs +++ b/examples/open_window.rs @@ -1,14 +1,10 @@ use baseview::{Event, Window, WindowHandler}; -struct MyProgram {} +struct OpenWindowExample; -impl WindowHandler for MyProgram { +impl WindowHandler for OpenWindowExample { type Message = (); - fn build(_window: &mut Window) -> Self { - Self {} - } - fn on_frame(&mut self) {} fn on_event(&mut self, _window: &mut Window, event: Event) { @@ -30,6 +26,6 @@ fn main() { parent: baseview::Parent::None, }; - let handle = Window::open::(window_open_options); + let handle = Window::open(window_open_options, |_| OpenWindowExample); handle.app_run_blocking(); } diff --git a/src/lib.rs b/src/lib.rs index fb84d94..4247d26 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,8 +42,6 @@ pub struct WindowOpenOptions { pub trait WindowHandler { type Message; - fn build(window: &mut Window) -> Self; - fn on_frame(&mut self); fn on_event(&mut self, window: &mut Window, event: Event); fn on_message(&mut self, window: &mut Window, message: Self::Message); diff --git a/src/x11/window.rs b/src/x11/window.rs index c7cd911..5e36af6 100644 --- a/src/x11/window.rs +++ b/src/x11/window.rs @@ -41,11 +41,15 @@ impl WindowHandle { type WindowOpenResult = Result<(), ()>; impl Window { - pub fn open(options: WindowOpenOptions) -> WindowHandle { + pub fn open(options: WindowOpenOptions, build: B) -> WindowHandle + where H: WindowHandler, + B: FnOnce(&mut Window) -> H, + B: Send + 'static + { let (tx, rx) = mpsc::sync_channel::(1); let thread = thread::spawn(move || { - if let Err(e) = Self::window_thread::(options, tx.clone()) { + if let Err(e) = Self::window_thread::(options, build, tx.clone()) { let _ = tx.send(Err(e)); } }); @@ -56,9 +60,12 @@ impl Window { WindowHandle { thread } } - fn window_thread( - options: WindowOpenOptions, tx: mpsc::SyncSender, - ) -> WindowOpenResult { + fn window_thread(options: WindowOpenOptions, build: B, + tx: mpsc::SyncSender) -> WindowOpenResult + where H: WindowHandler, + B: FnOnce(&mut Window) -> H, + B: Send + 'static + { // Connect to the X server // FIXME: baseview error type instead of unwrap() let xcb_connection = XcbConnection::new().unwrap(); @@ -164,7 +171,7 @@ impl Window { new_size: None }; - let mut handler = H::build(&mut window); + let mut handler = build(&mut window); let _ = tx.send(Ok(())); From cf96f3cc85ce4fb3ec6d1da28055955b40fcad6a Mon Sep 17 00:00:00 2001 From: William Light Date: Tue, 20 Oct 2020 21:33:31 +0200 Subject: [PATCH 2/2] win/mac: closure for building WindowHandler --- src/macos/window.rs | 8 ++++++-- src/win/window.rs | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/macos/window.rs b/src/macos/window.rs index 6e718ba..e16f5b3 100644 --- a/src/macos/window.rs +++ b/src/macos/window.rs @@ -33,7 +33,11 @@ impl WindowHandle { } impl Window { - pub fn open(options: WindowOpenOptions) -> WindowHandle { + pub fn open(options: WindowOpenOptions, build: B) -> WindowHandle + where H: WindowHandler, + B: FnOnce(&mut Window) -> H, + B: Send + 'static + { unsafe { let _pool = NSAutoreleasePool::new(nil); @@ -59,7 +63,7 @@ impl Window { let mut window = Window { ns_window, ns_view }; - let handler = H::build(&mut window); + let handler = build(&mut window); // FIXME: only do this in the unparented case let current_app = NSRunningApplication::currentApplication(nil); diff --git a/src/win/window.rs b/src/win/window.rs index 6e077a6..aa1b7fb 100644 --- a/src/win/window.rs +++ b/src/win/window.rs @@ -166,7 +166,11 @@ impl WindowHandle { } impl Window { - pub fn open(options: WindowOpenOptions) -> WindowHandle { + pub fn open(options: WindowOpenOptions, build: B) -> WindowHandle + where H: WindowHandler, + B: FnOnce(&mut Window) -> H, + B: Send + 'static + { unsafe { let title = (options.title.to_owned() + "\0").as_ptr() as *const i8; @@ -222,7 +226,7 @@ impl Window { let mut window = Window { hwnd }; - let handler = H::build(&mut window); + let handler = build(&mut window); let window_state = Rc::new(RefCell::new(WindowState { window_class,