diff --git a/src/lib.rs b/src/lib.rs index 67ac9dd..47a95a8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,26 +2,21 @@ use raw_window_handle::RawWindowHandle; #[cfg(target_os = "windows")] mod win; -#[cfg(target_os = "windows")] -pub use win::*; - #[cfg(target_os = "linux")] mod x11; -#[cfg(target_os = "linux")] -pub use crate::x11::*; - #[cfg(target_os = "macos")] mod macos; -#[cfg(target_os = "macos")] -pub use macos::*; mod event; mod keyboard; mod mouse_cursor; +mod window; mod window_info; mod window_open_options; + pub use event::*; pub use mouse_cursor::MouseCursor; +pub use window::*; pub use window_info::*; pub use window_open_options::*; diff --git a/src/macos/window.rs b/src/macos/window.rs index 7194eb8..78295fd 100644 --- a/src/macos/window.rs +++ b/src/macos/window.rs @@ -56,9 +56,12 @@ impl WindowHandle { } impl Window { - pub fn open(options: WindowOpenOptions, build: B) -> WindowHandle + pub fn open( + options: WindowOpenOptions, + build: B + ) -> crate::WindowHandle where H: WindowHandler, - B: FnOnce(&mut Window) -> H, + B: FnOnce(&mut crate::Window) -> H, B: Send + 'static { let _pool = unsafe { NSAutoreleasePool::new(nil) }; @@ -155,7 +158,7 @@ impl Window { }, }; - let window_handler = build(&mut window); + let window_handler = build(&mut crate::Window(&mut window)); let window_state_arc = Arc::new(WindowState { window, @@ -195,7 +198,7 @@ impl Window { ) } - WindowHandle + crate::WindowHandle(WindowHandle) } } @@ -220,7 +223,10 @@ impl WindowState { } pub(super) fn trigger_event(&mut self, event: Event){ - self.window_handler.on_event(&mut self.window, event); + self.window_handler.on_event( + &mut crate::Window(&mut self.window), + event + ); } pub(super) fn trigger_frame(&mut self){ diff --git a/src/win/window.rs b/src/win/window.rs index ae952be..75d84ec 100644 --- a/src/win/window.rs +++ b/src/win/window.rs @@ -73,6 +73,7 @@ unsafe extern "system" fn wnd_proc( if !win_ptr.is_null() { let window_state = &*(win_ptr as *const RefCell>); let mut window = Window { hwnd }; + let mut window = crate::Window(&mut window); match msg { WM_MOUSEMOVE => { @@ -175,9 +176,12 @@ impl WindowHandle { } impl Window { - pub fn open(options: WindowOpenOptions, build: B) -> WindowHandle + pub fn open( + options: WindowOpenOptions, + build: B + ) -> crate::WindowHandle where H: WindowHandler, - B: FnOnce(&mut Window) -> H, + B: FnOnce(&mut crate::Window) -> H, B: Send + 'static { unsafe { @@ -240,9 +244,7 @@ impl Window { ); // todo: manage error ^ - let mut window = Window { hwnd }; - - let handler = build(&mut window); + let handler = build(&mut crate::Window(&mut Window { hwnd })); let window_state = Box::new(RefCell::new(WindowState { window_class, @@ -253,7 +255,7 @@ impl Window { SetWindowLongPtrA(hwnd, GWLP_USERDATA, Box::into_raw(window_state) as *const _ as _); SetTimer(hwnd, 4242, 13, None); - WindowHandle { hwnd } + crate::WindowHandle(WindowHandle { hwnd }) } } } diff --git a/src/window.rs b/src/window.rs new file mode 100644 index 0000000..93f3aa2 --- /dev/null +++ b/src/window.rs @@ -0,0 +1,45 @@ +use raw_window_handle::{HasRawWindowHandle, RawWindowHandle}; + +use crate::WindowHandler; +use crate::window_open_options::WindowOpenOptions; + +#[cfg(target_os = "windows")] +use crate::win as platform; +#[cfg(target_os = "linux")] +use crate::x11 as platform; +#[cfg(target_os = "macos")] +use crate::macos as platform; + + +pub struct WindowHandle(pub(crate) platform::WindowHandle); + + +impl WindowHandle { + pub fn app_run_blocking(self){ + self.0.app_run_blocking(); + } +} + + +pub struct Window<'a>(pub(crate) &'a mut platform::Window); + + +impl <'a>Window<'a> { + pub fn open( + options: WindowOpenOptions, + build: B + ) -> WindowHandle + where H: WindowHandler, + B: FnOnce(&mut Window) -> H, + B: Send + 'static + { + platform::Window::open::(options, build) + } +} + + +unsafe impl <'a>HasRawWindowHandle for Window<'a> { + fn raw_window_handle(&self) -> RawWindowHandle { + self.0.raw_window_handle() + } +} \ No newline at end of file diff --git a/src/x11/window.rs b/src/x11/window.rs index 9d3a5e5..946f240 100644 --- a/src/x11/window.rs +++ b/src/x11/window.rs @@ -44,9 +44,12 @@ impl WindowHandle { type WindowOpenResult = Result<(), ()>; impl Window { - pub fn open(options: WindowOpenOptions, build: B) -> WindowHandle + pub fn open( + options: WindowOpenOptions, + build: B + ) -> crate::WindowHandle where H: WindowHandler, - B: FnOnce(&mut Window) -> H, + B: FnOnce(&mut crate::Window) -> H, B: Send + 'static { let (tx, rx) = mpsc::sync_channel::(1); @@ -60,13 +63,13 @@ impl Window { // FIXME: placeholder types for returning errors in the future let _ = rx.recv(); - WindowHandle { thread } + crate::WindowHandle(WindowHandle { thread }) } fn window_thread(options: WindowOpenOptions, build: B, tx: mpsc::SyncSender) -> WindowOpenResult where H: WindowHandler, - B: FnOnce(&mut Window) -> H, + B: FnOnce(&mut crate::Window) -> H, B: Send + 'static { // Connect to the X server @@ -173,7 +176,7 @@ impl Window { new_physical_size: None, }; - let mut handler = build(&mut window); + let mut handler = build(&mut crate::Window(&mut window)); let _ = tx.send(Ok(())); @@ -222,9 +225,12 @@ impl Window { self.window_info.scale() ); - handler.on_event(self, Event::Window( - WindowEvent::Resized(self.window_info) - )) + let window_info = self.window_info; + + handler.on_event( + &mut crate::Window(self), + Event::Window(WindowEvent::Resized(window_info)) + ) } } @@ -313,7 +319,10 @@ impl Window { .unwrap_or(xcb::NONE); if wm_delete_window == data32[0] { - handler.on_event(self, Event::Window(WindowEvent::WillClose)); + handler.on_event( + &mut crate::Window(self), + Event::Window(WindowEvent::WillClose) + ); // FIXME: handler should decide whether window stays open or not self.event_loop_running = false; @@ -342,7 +351,7 @@ impl Window { let logical_pos = physical_pos.to_logical(&self.window_info); handler.on_event( - self, + &mut crate::Window(self), Event::Mouse(MouseEvent::CursorMoved { position: logical_pos, }), @@ -357,7 +366,7 @@ impl Window { match detail { 4 => { handler.on_event( - self, + &mut crate::Window(self), Event::Mouse(MouseEvent::WheelScrolled(ScrollDelta::Lines { x: 0.0, y: 1.0, @@ -366,7 +375,7 @@ impl Window { } 5 => { handler.on_event( - self, + &mut crate::Window(self), Event::Mouse(MouseEvent::WheelScrolled(ScrollDelta::Lines { x: 0.0, y: -1.0, @@ -375,7 +384,10 @@ impl Window { } detail => { let button_id = mouse_id(detail); - handler.on_event(self, Event::Mouse(MouseEvent::ButtonPressed(button_id))); + handler.on_event( + &mut crate::Window(self), + Event::Mouse(MouseEvent::ButtonPressed(button_id)) + ); } } } @@ -386,7 +398,10 @@ impl Window { if detail != 4 && detail != 5 { let button_id = mouse_id(detail); - handler.on_event(self, Event::Mouse(MouseEvent::ButtonReleased(button_id))); + handler.on_event( + &mut crate::Window(self), + Event::Mouse(MouseEvent::ButtonReleased(button_id)) + ); } } @@ -397,7 +412,7 @@ impl Window { let event = unsafe { xcb::cast_event::(&event) }; handler.on_event( - self, + &mut crate::Window(self), Event::Keyboard(convert_key_press_event(&event)) ); } @@ -406,7 +421,7 @@ impl Window { let event = unsafe { xcb::cast_event::(&event) }; handler.on_event( - self, + &mut crate::Window(self), Event::Keyboard(convert_key_release_event(&event)) ); }