diff --git a/examples/open_window.rs b/examples/open_window.rs index 7a04268..7809817 100755 --- a/examples/open_window.rs +++ b/examples/open_window.rs @@ -1,14 +1,10 @@ use baseview::{Event, Window, WindowHandler, WindowSize, WindowScalePolicy}; -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) { @@ -35,6 +31,6 @@ fn main() { parent: baseview::Parent::None, }; - let (handle, _window_info) = Window::open::(window_open_options).unwrap(); + let handle = Window::open(window_open_options, |_| OpenWindowExample); handle.app_run_blocking(); } diff --git a/src/lib.rs b/src/lib.rs index 2b9b126..c2f855b 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,8 +40,6 @@ type WindowOpenResult = Result; 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/macos/window.rs b/src/macos/window.rs index 6e718ba..e16f5b3 100755 --- 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 a2bf4f6..c74e7d4 100755 --- a/src/win/window.rs +++ b/src/win/window.rs @@ -173,7 +173,11 @@ impl WindowHandle { } impl Window { - pub fn open(options: WindowOpenOptions) -> Result<(WindowHandle, WindowInfo), ()> { + 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; @@ -239,7 +243,7 @@ impl Window { let mut window = Window { hwnd }; - let handler = H::build(&mut window); + let handler = build(&mut window); let window_state = Box::new(RefCell::new(WindowState { window_class, diff --git a/src/x11/window.rs b/src/x11/window.rs index f7df37a..a6eeb21 100755 --- a/src/x11/window.rs +++ b/src/x11/window.rs @@ -40,11 +40,15 @@ impl WindowHandle { } impl Window { - pub fn open(options: WindowOpenOptions) -> Result<(WindowHandle, WindowInfo), ()> { + 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)); } }); @@ -55,9 +59,12 @@ impl Window { Ok((WindowHandle { thread }, window_info)) } - 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(); @@ -168,7 +175,7 @@ impl Window { new_physical_size: None, }; - let mut handler = H::build(&mut window); + let mut handler = build(&mut window); let _ = tx.send(Ok(window_info));