diff --git a/src/macos/window.rs b/src/macos/window.rs index 666d863..abef415 100644 --- a/src/macos/window.rs +++ b/src/macos/window.rs @@ -95,22 +95,26 @@ impl Drop for ParentHandle { } } -pub struct Window { +struct WindowInner { /// Only set if we created the parent window, i.e. we are running in /// parentless mode - ns_app: Option, + ns_app: Cell>, /// Only set if we created the parent window, i.e. we are running in /// parentless mode - ns_window: Option, + ns_window: Cell>, /// Our subclassed NSView ns_view: id, - close_requested: bool, + close_requested: Cell, #[cfg(feature = "opengl")] gl_context: Option, } -impl Window { +pub struct Window<'a> { + inner: &'a WindowInner, +} + +impl<'a> Window<'a> { pub fn open_parented(parent: &P, options: WindowOpenOptions, build: B) -> WindowHandle where P: HasRawWindowHandle, @@ -135,11 +139,11 @@ impl Window { let ns_view = unsafe { create_view(&options) }; - let window = Window { - ns_app: None, - ns_window: None, + let window_inner = WindowInner { + ns_app: Cell::new(None), + ns_window: Cell::new(None), ns_view, - close_requested: false, + close_requested: Cell::new(false), #[cfg(feature = "opengl")] gl_context: options @@ -147,7 +151,7 @@ impl Window { .map(|gl_config| Self::create_gl_context(None, ns_view, gl_config)), }; - let window_handle = Self::init(true, window, window_info, build); + let window_handle = Self::init(true, window_inner, window_info, build); unsafe { let _: id = msg_send![handle.ns_view as *mut Object, addSubview: ns_view]; @@ -211,11 +215,11 @@ impl Window { let ns_view = unsafe { create_view(&options) }; - let window = Window { - ns_app: Some(app), - ns_window: Some(ns_window), + let window_inner = WindowInner { + ns_app: Cell::new(Some(app)), + ns_window: Cell::new(Some(ns_window)), ns_view, - close_requested: false, + close_requested: Cell::new(false), #[cfg(feature = "opengl")] gl_context: options @@ -223,7 +227,7 @@ impl Window { .map(|gl_config| Self::create_gl_context(Some(ns_window), ns_view, gl_config)), }; - let _ = Self::init(false, window, window_info, build); + let _ = Self::init(false, window_inner, window_info, build); unsafe { ns_window.setContentView_(ns_view); @@ -236,24 +240,26 @@ impl Window { } fn init( - parented: bool, mut window: Window, window_info: WindowInfo, build: B, + parented: bool, window_inner: WindowInner, window_info: WindowInfo, build: B, ) -> WindowHandle where H: WindowHandler + 'static, B: FnOnce(&mut crate::Window) -> H, B: Send + 'static, { - let window_handler = Box::new(build(&mut crate::Window::new(&mut window))); + let mut window = crate::Window::new(Window { inner: &window_inner }); + let window_handler = Box::new(build(&mut window)); let (parent_handle, window_handle) = ParentHandle::new(window.raw_window_handle()); let parent_handle = if parented { Some(parent_handle) } else { None }; - let retain_count_after_build: usize = unsafe { msg_send![window.ns_view, retainCount] }; + let retain_count_after_build: usize = + unsafe { msg_send![window_inner.ns_view, retainCount] }; - let ns_view = window.ns_view; + let ns_view = window_inner.ns_view; let window_state_ptr = Box::into_raw(Box::new(WindowState { - window: RefCell::new(window), + window_inner, window_handler: RefCell::new(window_handler), keyboard_state: KeyboardState::new(), frame_timer: Cell::new(None), @@ -272,7 +278,7 @@ impl Window { } pub fn close(&mut self) { - self.close_requested = true; + self.inner.close_requested.set(true); } pub fn resize(&mut self, size: Size) { @@ -280,19 +286,19 @@ impl Window { // the size is in fractional pixels. let size = NSSize::new(size.width.round(), size.height.round()); - unsafe { NSView::setFrameSize(self.ns_view, size) }; + unsafe { NSView::setFrameSize(self.inner.ns_view, size) }; unsafe { - let _: () = msg_send![self.ns_view, setNeedsDisplay: YES]; + let _: () = msg_send![self.inner.ns_view, setNeedsDisplay: YES]; } // When using OpenGL the `NSOpenGLView` needs to be resized separately? Why? Because macOS. #[cfg(feature = "opengl")] - if let Some(gl_context) = &self.gl_context { + if let Some(gl_context) = &self.inner.gl_context { gl_context.resize(size); } // If this is a standalone window then we'll also need to resize the window itself - if let Some(ns_window) = self.ns_window { + if let Some(ns_window) = self.inner.ns_window.get() { unsafe { NSWindow::setContentSize_(ns_window, size) }; } } @@ -303,7 +309,7 @@ impl Window { #[cfg(feature = "opengl")] pub fn gl_context(&self) -> Option<&GlContext> { - self.gl_context.as_ref() + self.inner.gl_context.as_ref() } #[cfg(feature = "opengl")] @@ -318,7 +324,7 @@ impl Window { } pub(super) struct WindowState { - window: RefCell, + window_inner: WindowInner, window_handler: RefCell>, keyboard_state: KeyboardState, frame_timer: Cell>, @@ -337,13 +343,13 @@ impl WindowState { } pub(super) fn trigger_event(&self, event: Event) -> EventStatus { - let mut window = self.window.borrow_mut(); - self.window_handler.borrow_mut().on_event(&mut crate::Window::new(&mut window), event) + let mut window = crate::Window::new(Window { inner: &self.window_inner }); + self.window_handler.borrow_mut().on_event(&mut window, event) } pub(super) fn trigger_frame(&self) { - let mut window = self.window.borrow_mut(); - self.window_handler.borrow_mut().on_frame(&mut crate::Window::new(&mut window)); + let mut window = crate::Window::new(Window { inner: &self.window_inner }); + self.window_handler.borrow_mut().on_frame(&mut window); let mut do_close = false; @@ -359,14 +365,14 @@ impl WindowState { */ // Check if the user requested the window to close - if window.close_requested { + if self.window_inner.close_requested.get() { do_close = true; - window.close_requested = false; + self.window_inner.close_requested.set(false); } if do_close { unsafe { - let ns_window = self.window.borrow_mut().ns_window.take(); + let ns_window = self.window_inner.ns_window.take(); if let Some(ns_window) = ns_window { ns_window.close(); } else { @@ -430,26 +436,26 @@ impl WindowState { window_state.trigger_event(Event::Window(WindowEvent::WillClose)); // If in non-parented mode, we want to also quit the app altogether - let app = window_state.window.borrow_mut().ns_app.take(); + let app = window_state.window_inner.ns_app.take(); if let Some(app) = app { app.stop_(app); } } } -unsafe impl HasRawWindowHandle for Window { +unsafe impl<'a> HasRawWindowHandle for Window<'a> { fn raw_window_handle(&self) -> RawWindowHandle { - let ns_window = self.ns_window.unwrap_or(ptr::null_mut()) as *mut c_void; + let ns_window = self.inner.ns_window.get().unwrap_or(ptr::null_mut()) as *mut c_void; let mut handle = AppKitWindowHandle::empty(); handle.ns_window = ns_window; - handle.ns_view = self.ns_view as *mut c_void; + handle.ns_view = self.inner.ns_view as *mut c_void; RawWindowHandle::AppKit(handle) } } -unsafe impl HasRawDisplayHandle for Window { +unsafe impl<'a> HasRawDisplayHandle for Window<'a> { fn raw_display_handle(&self) -> RawDisplayHandle { RawDisplayHandle::AppKit(AppKitDisplayHandle::empty()) } diff --git a/src/win/drop_target.rs b/src/win/drop_target.rs index a2b3b6f..fd1c55b 100644 --- a/src/win/drop_target.rs +++ b/src/win/drop_target.rs @@ -90,8 +90,7 @@ impl DropTarget { }; unsafe { - let mut window = window_state.create_window(); - let mut window = crate::Window::new(&mut window); + let mut window = crate::Window::new(window_state.create_window()); let event = Event::Mouse(event); let event_status = diff --git a/src/win/window.rs b/src/win/window.rs index 0515a14..637821c 100644 --- a/src/win/window.rs +++ b/src/win/window.rs @@ -168,8 +168,7 @@ unsafe fn wnd_proc_inner( ) -> Option { match msg { WM_MOUSEMOVE => { - let mut window = window_state.create_window(); - let mut window = crate::Window::new(&mut window); + let mut window = crate::Window::new(window_state.create_window()); let x = (lparam & 0xFFFF) as i16 as i32; let y = ((lparam >> 16) & 0xFFFF) as i16 as i32; @@ -189,8 +188,7 @@ unsafe fn wnd_proc_inner( Some(0) } WM_MOUSEWHEEL | WM_MOUSEHWHEEL => { - let mut window = window_state.create_window(); - let mut window = crate::Window::new(&mut window); + let mut window = crate::Window::new(window_state.create_window()); let value = (wparam >> 16) as i16; let value = value as i32; @@ -214,8 +212,7 @@ unsafe fn wnd_proc_inner( } WM_LBUTTONDOWN | WM_LBUTTONUP | WM_MBUTTONDOWN | WM_MBUTTONUP | WM_RBUTTONDOWN | WM_RBUTTONUP | WM_XBUTTONDOWN | WM_XBUTTONUP => { - let mut window = window_state.create_window(); - let mut window = crate::Window::new(&mut window); + let mut window = crate::Window::new(window_state.create_window()); let mut mouse_button_counter = window_state.mouse_button_counter.get(); @@ -278,8 +275,7 @@ unsafe fn wnd_proc_inner( None } WM_TIMER => { - let mut window = window_state.create_window(); - let mut window = crate::Window::new(&mut window); + let mut window = crate::Window::new(window_state.create_window()); if wparam == WIN_FRAME_TIMER { window_state.handler.borrow_mut().as_mut().unwrap().on_frame(&mut window); @@ -290,8 +286,7 @@ unsafe fn wnd_proc_inner( WM_CLOSE => { // Make sure to release the borrow before the DefWindowProc call { - let mut window = window_state.create_window(); - let mut window = crate::Window::new(&mut window); + let mut window = crate::Window::new(window_state.create_window()); window_state .handler @@ -307,8 +302,7 @@ unsafe fn wnd_proc_inner( } WM_CHAR | WM_SYSCHAR | WM_KEYDOWN | WM_SYSKEYDOWN | WM_KEYUP | WM_SYSKEYUP | WM_INPUTLANGCHANGE => { - let mut window = window_state.create_window(); - let mut window = crate::Window::new(&mut window); + let mut window = crate::Window::new(window_state.create_window()); let opt_event = window_state.keyboard_state.borrow_mut().process_message(hwnd, msg, wparam, lparam); @@ -329,8 +323,7 @@ unsafe fn wnd_proc_inner( } } WM_SIZE => { - let mut window = window_state.create_window(); - let mut window = crate::Window::new(&mut window); + let mut window = crate::Window::new(window_state.create_window()); let width = (lparam & 0xFFFF) as u16 as u32; let height = ((lparam >> 16) & 0xFFFF) as u16 as u32; @@ -676,8 +669,7 @@ impl Window<'_> { }); let handler = { - let mut window = window_state.create_window(); - let mut window = crate::Window::new(&mut window); + let mut window = crate::Window::new(window_state.create_window()); build(&mut window) }; diff --git a/src/window.rs b/src/window.rs index e0372f8..63e3f49 100644 --- a/src/window.rs +++ b/src/window.rs @@ -50,10 +50,7 @@ pub trait WindowHandler { } pub struct Window<'a> { - #[cfg(target_os = "windows")] - window: &'a mut platform::Window<'a>, - #[cfg(not(target_os = "windows"))] - window: &'a mut platform::Window, + window: platform::Window<'a>, // so that Window is !Send on all platforms phantom: PhantomData<*mut ()>, @@ -61,12 +58,12 @@ pub struct Window<'a> { impl<'a> Window<'a> { #[cfg(target_os = "windows")] - pub(crate) fn new(window: &'a mut platform::Window<'a>) -> Window<'a> { + pub(crate) fn new(window: platform::Window<'a>) -> Window<'a> { Window { window, phantom: PhantomData } } #[cfg(not(target_os = "windows"))] - pub(crate) fn new(window: &mut platform::Window) -> Window { + pub(crate) fn new(window: platform::Window) -> Window { Window { window, phantom: PhantomData } } diff --git a/src/x11/window.rs b/src/x11/window.rs index f29ccb9..90ec281 100644 --- a/src/x11/window.rs +++ b/src/x11/window.rs @@ -87,7 +87,7 @@ impl Drop for ParentHandle { } } -pub struct Window { +struct WindowInner { xcb_connection: XcbConnection, window_id: u32, window_info: WindowInfo, @@ -105,6 +105,10 @@ pub struct Window { gl_context: Option, } +pub struct Window<'a> { + inner: &'a mut WindowInner, +} + // Hack to allow sending a RawWindowHandle between threads. Do not make public struct SendableRwh(RawWindowHandle); @@ -112,7 +116,7 @@ unsafe impl Send for SendableRwh {} type WindowOpenResult = Result; -impl Window { +impl<'a> Window<'a> { pub fn open_parented(parent: &P, options: WindowOpenOptions, build: B) -> WindowHandle where P: HasRawWindowHandle, @@ -311,7 +315,7 @@ impl Window { GlContext::new(context) }); - let mut window = Self { + let mut inner = WindowInner { xcb_connection, window_id, window_info, @@ -329,57 +333,56 @@ impl Window { gl_context, }; - let mut handler = build(&mut crate::Window::new(&mut window)); + let mut window = crate::Window::new(Window { inner: &mut inner }); + + let mut handler = build(&mut window); // Send an initial window resized event so the user is alerted of // the correct dpi scaling. - handler.on_event( - &mut crate::Window::new(&mut window), - Event::Window(WindowEvent::Resized(window_info)), - ); + handler.on_event(&mut window, Event::Window(WindowEvent::Resized(window_info))); let _ = tx.send(Ok(SendableRwh(window.raw_window_handle()))); - window.run_event_loop(&mut handler); + inner.run_event_loop(&mut handler); } pub fn set_mouse_cursor(&mut self, mouse_cursor: MouseCursor) { - if self.mouse_cursor == mouse_cursor { + if self.inner.mouse_cursor == mouse_cursor { return; } - let xid = self.xcb_connection.get_cursor_xid(mouse_cursor); + let xid = self.inner.xcb_connection.get_cursor_xid(mouse_cursor); if xid != 0 { xcb::change_window_attributes( - &self.xcb_connection.conn, - self.window_id, + &self.inner.xcb_connection.conn, + self.inner.window_id, &[(xcb::CW_CURSOR, xid)], ); - self.xcb_connection.conn.flush(); + self.inner.xcb_connection.conn.flush(); } - self.mouse_cursor = mouse_cursor; + self.inner.mouse_cursor = mouse_cursor; } pub fn close(&mut self) { - self.close_requested = true; + self.inner.close_requested = true; } pub fn resize(&mut self, size: Size) { - let scaling = self.window_info.scale(); + let scaling = self.inner.window_info.scale(); let new_window_info = WindowInfo::from_logical_size(size, scaling); xcb::configure_window( - &self.xcb_connection.conn, - self.window_id, + &self.inner.xcb_connection.conn, + self.inner.window_id, &[ (xcb::CONFIG_WINDOW_WIDTH as u16, new_window_info.physical_size().width), (xcb::CONFIG_WINDOW_HEIGHT as u16, new_window_info.physical_size().height), ], ); - self.xcb_connection.conn.flush(); + self.inner.xcb_connection.conn.flush(); // This will trigger a `ConfigureNotify` event which will in turn change `self.window_info` // and notify the window handler about it @@ -387,7 +390,7 @@ impl Window { #[cfg(feature = "opengl")] pub fn gl_context(&self) -> Option<&crate::gl::GlContext> { - self.gl_context.as_ref() + self.inner.gl_context.as_ref() } fn find_visual_for_depth(screen: &StructPtr, depth: u8) -> Option { @@ -405,7 +408,9 @@ impl Window { None } +} +impl WindowInner { #[inline] fn drain_xcb_events(&mut self, handler: &mut dyn WindowHandler) { // the X server has a tendency to send spurious/extraneous configure notify events when a @@ -423,7 +428,7 @@ impl Window { let window_info = self.window_info; handler.on_event( - &mut crate::Window::new(self), + &mut crate::Window::new(Window { inner: self }), Event::Window(WindowEvent::Resized(window_info)), ); } @@ -453,7 +458,7 @@ impl Window { // if it's already time to draw a new frame. let next_frame = last_frame + self.frame_interval; if Instant::now() >= next_frame { - handler.on_frame(&mut crate::Window::new(self)); + handler.on_frame(&mut crate::Window::new(Window { inner: self })); last_frame = Instant::max(next_frame, Instant::now() - self.frame_interval); } @@ -499,14 +504,20 @@ impl Window { } fn handle_close_requested(&mut self, handler: &mut dyn WindowHandler) { - handler.on_event(&mut crate::Window::new(self), Event::Window(WindowEvent::WillClose)); + handler.on_event( + &mut crate::Window::new(Window { inner: self }), + Event::Window(WindowEvent::WillClose), + ); // FIXME: handler should decide whether window stays open or not self.event_loop_running = false; } fn handle_must_close(&mut self, handler: &mut dyn WindowHandler) { - handler.on_event(&mut crate::Window::new(self), Event::Window(WindowEvent::WillClose)); + handler.on_event( + &mut crate::Window::new(Window { inner: self }), + Event::Window(WindowEvent::WillClose), + ); self.event_loop_running = false; } @@ -578,7 +589,7 @@ impl Window { let logical_pos = physical_pos.to_logical(&self.window_info); handler.on_event( - &mut crate::Window::new(self), + &mut crate::Window::new(Window { inner: self }), Event::Mouse(MouseEvent::CursorMoved { position: logical_pos, modifiers: key_mods(event.state()), @@ -589,7 +600,7 @@ impl Window { xcb::ENTER_NOTIFY => { handler.on_event( - &mut crate::Window::new(self), + &mut crate::Window::new(Window { inner: self }), Event::Mouse(MouseEvent::CursorEntered), ); // since no `MOTION_NOTIFY` event is generated when `ENTER_NOTIFY` is generated, @@ -598,7 +609,7 @@ impl Window { let physical_pos = PhyPoint::new(event.event_x() as i32, event.event_y() as i32); let logical_pos = physical_pos.to_logical(&self.window_info); handler.on_event( - &mut crate::Window::new(self), + &mut crate::Window::new(Window { inner: self }), Event::Mouse(MouseEvent::CursorMoved { position: logical_pos, modifiers: key_mods(event.state()), @@ -607,8 +618,10 @@ impl Window { } xcb::LEAVE_NOTIFY => { - handler - .on_event(&mut crate::Window::new(self), Event::Mouse(MouseEvent::CursorLeft)); + handler.on_event( + &mut crate::Window::new(Window { inner: self }), + Event::Mouse(MouseEvent::CursorLeft), + ); } xcb::BUTTON_PRESS => { @@ -618,7 +631,7 @@ impl Window { match detail { 4..=7 => { handler.on_event( - &mut crate::Window::new(self), + &mut crate::Window::new(Window { inner: self }), Event::Mouse(MouseEvent::WheelScrolled { delta: match detail { 4 => ScrollDelta::Lines { x: 0.0, y: 1.0 }, @@ -634,7 +647,7 @@ impl Window { detail => { let button_id = mouse_id(detail); handler.on_event( - &mut crate::Window::new(self), + &mut crate::Window::new(Window { inner: self }), Event::Mouse(MouseEvent::ButtonPressed { button: button_id, modifiers: key_mods(event.state()), @@ -651,7 +664,7 @@ impl Window { if !(4..=7).contains(&detail) { let button_id = mouse_id(detail); handler.on_event( - &mut crate::Window::new(self), + &mut crate::Window::new(Window { inner: self }), Event::Mouse(MouseEvent::ButtonReleased { button: button_id, modifiers: key_mods(event.state()), @@ -667,7 +680,7 @@ impl Window { let event = unsafe { xcb::cast_event::(&event) }; handler.on_event( - &mut crate::Window::new(self), + &mut crate::Window::new(Window { inner: self }), Event::Keyboard(convert_key_press_event(event)), ); } @@ -676,7 +689,7 @@ impl Window { let event = unsafe { xcb::cast_event::(&event) }; handler.on_event( - &mut crate::Window::new(self), + &mut crate::Window::new(Window { inner: self }), Event::Keyboard(convert_key_release_event(event)), ); } @@ -686,20 +699,20 @@ impl Window { } } -unsafe impl HasRawWindowHandle for Window { +unsafe impl<'a> HasRawWindowHandle for Window<'a> { fn raw_window_handle(&self) -> RawWindowHandle { let mut handle = XlibWindowHandle::empty(); - handle.window = self.window_id.into(); - handle.visual_id = self.visual_id.into(); + handle.window = self.inner.window_id.into(); + handle.visual_id = self.inner.visual_id.into(); RawWindowHandle::Xlib(handle) } } -unsafe impl HasRawDisplayHandle for Window { +unsafe impl<'a> HasRawDisplayHandle for Window<'a> { fn raw_display_handle(&self) -> RawDisplayHandle { - let display = self.xcb_connection.conn.get_raw_dpy(); + let display = self.inner.xcb_connection.conn.get_raw_dpy(); let mut handle = XlibDisplayHandle::empty(); handle.display = display as *mut c_void;