1
0
Fork 0

Unify Window struct across backends (#162)

The public Window struct holds a mutable reference to `platform::Window`, which is a pattern that doesn't make sense for all backends. On Windows, the `platform::Window` struct itself just holds another (immutable) reference, and on macOS the `platform::Window` struct has to be wrapped in a `RefCell` so that a mutable reference to it can be formed.

Change the public `Window` struct to hold `platform::Window` directly, and change `platform::Window` in the macOS and X11 backends so that it simply holds a reference to another `WindowInner` struct similarly to the Windows backend. This allows us to remove the platform conditional in the declaration of the top-level `Window` struct. It also allows us to remove the `RefCell` wrapping `platform::Window` in the macOS backend and replace it with `Cell`s wrapping its individual fields.
This commit is contained in:
Micah Johnston 2023-12-17 02:21:31 -06:00 committed by GitHub
parent 937ef965b4
commit fdb43eac11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 110 additions and 103 deletions

View file

@ -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 /// Only set if we created the parent window, i.e. we are running in
/// parentless mode /// parentless mode
ns_app: Option<id>, ns_app: Cell<Option<id>>,
/// Only set if we created the parent window, i.e. we are running in /// Only set if we created the parent window, i.e. we are running in
/// parentless mode /// parentless mode
ns_window: Option<id>, ns_window: Cell<Option<id>>,
/// Our subclassed NSView /// Our subclassed NSView
ns_view: id, ns_view: id,
close_requested: bool, close_requested: Cell<bool>,
#[cfg(feature = "opengl")] #[cfg(feature = "opengl")]
gl_context: Option<GlContext>, gl_context: Option<GlContext>,
} }
impl Window { pub struct Window<'a> {
inner: &'a WindowInner,
}
impl<'a> Window<'a> {
pub fn open_parented<P, H, B>(parent: &P, options: WindowOpenOptions, build: B) -> WindowHandle pub fn open_parented<P, H, B>(parent: &P, options: WindowOpenOptions, build: B) -> WindowHandle
where where
P: HasRawWindowHandle, P: HasRawWindowHandle,
@ -135,11 +139,11 @@ impl Window {
let ns_view = unsafe { create_view(&options) }; let ns_view = unsafe { create_view(&options) };
let window = Window { let window_inner = WindowInner {
ns_app: None, ns_app: Cell::new(None),
ns_window: None, ns_window: Cell::new(None),
ns_view, ns_view,
close_requested: false, close_requested: Cell::new(false),
#[cfg(feature = "opengl")] #[cfg(feature = "opengl")]
gl_context: options gl_context: options
@ -147,7 +151,7 @@ impl Window {
.map(|gl_config| Self::create_gl_context(None, ns_view, gl_config)), .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 { unsafe {
let _: id = msg_send![handle.ns_view as *mut Object, addSubview: ns_view]; 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 ns_view = unsafe { create_view(&options) };
let window = Window { let window_inner = WindowInner {
ns_app: Some(app), ns_app: Cell::new(Some(app)),
ns_window: Some(ns_window), ns_window: Cell::new(Some(ns_window)),
ns_view, ns_view,
close_requested: false, close_requested: Cell::new(false),
#[cfg(feature = "opengl")] #[cfg(feature = "opengl")]
gl_context: options gl_context: options
@ -223,7 +227,7 @@ impl Window {
.map(|gl_config| Self::create_gl_context(Some(ns_window), ns_view, gl_config)), .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 { unsafe {
ns_window.setContentView_(ns_view); ns_window.setContentView_(ns_view);
@ -236,24 +240,26 @@ impl Window {
} }
fn init<H, B>( fn init<H, B>(
parented: bool, mut window: Window, window_info: WindowInfo, build: B, parented: bool, window_inner: WindowInner, window_info: WindowInfo, build: B,
) -> WindowHandle ) -> WindowHandle
where where
H: WindowHandler + 'static, H: WindowHandler + 'static,
B: FnOnce(&mut crate::Window) -> H, B: FnOnce(&mut crate::Window) -> H,
B: Send + 'static, 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, window_handle) = ParentHandle::new(window.raw_window_handle());
let parent_handle = if parented { Some(parent_handle) } else { None }; 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 { let window_state_ptr = Box::into_raw(Box::new(WindowState {
window: RefCell::new(window), window_inner,
window_handler: RefCell::new(window_handler), window_handler: RefCell::new(window_handler),
keyboard_state: KeyboardState::new(), keyboard_state: KeyboardState::new(),
frame_timer: Cell::new(None), frame_timer: Cell::new(None),
@ -272,7 +278,7 @@ impl Window {
} }
pub fn close(&mut self) { pub fn close(&mut self) {
self.close_requested = true; self.inner.close_requested.set(true);
} }
pub fn resize(&mut self, size: Size) { pub fn resize(&mut self, size: Size) {
@ -280,19 +286,19 @@ impl Window {
// the size is in fractional pixels. // the size is in fractional pixels.
let size = NSSize::new(size.width.round(), size.height.round()); 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 { 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. // When using OpenGL the `NSOpenGLView` needs to be resized separately? Why? Because macOS.
#[cfg(feature = "opengl")] #[cfg(feature = "opengl")]
if let Some(gl_context) = &self.gl_context { if let Some(gl_context) = &self.inner.gl_context {
gl_context.resize(size); gl_context.resize(size);
} }
// If this is a standalone window then we'll also need to resize the window itself // 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) }; unsafe { NSWindow::setContentSize_(ns_window, size) };
} }
} }
@ -303,7 +309,7 @@ impl Window {
#[cfg(feature = "opengl")] #[cfg(feature = "opengl")]
pub fn gl_context(&self) -> Option<&GlContext> { pub fn gl_context(&self) -> Option<&GlContext> {
self.gl_context.as_ref() self.inner.gl_context.as_ref()
} }
#[cfg(feature = "opengl")] #[cfg(feature = "opengl")]
@ -318,7 +324,7 @@ impl Window {
} }
pub(super) struct WindowState { pub(super) struct WindowState {
window: RefCell<Window>, window_inner: WindowInner,
window_handler: RefCell<Box<dyn WindowHandler>>, window_handler: RefCell<Box<dyn WindowHandler>>,
keyboard_state: KeyboardState, keyboard_state: KeyboardState,
frame_timer: Cell<Option<CFRunLoopTimer>>, frame_timer: Cell<Option<CFRunLoopTimer>>,
@ -337,13 +343,13 @@ impl WindowState {
} }
pub(super) fn trigger_event(&self, event: Event) -> EventStatus { pub(super) fn trigger_event(&self, event: Event) -> EventStatus {
let mut window = self.window.borrow_mut(); let mut window = crate::Window::new(Window { inner: &self.window_inner });
self.window_handler.borrow_mut().on_event(&mut crate::Window::new(&mut window), event) self.window_handler.borrow_mut().on_event(&mut window, event)
} }
pub(super) fn trigger_frame(&self) { pub(super) fn trigger_frame(&self) {
let mut window = self.window.borrow_mut(); let mut window = crate::Window::new(Window { inner: &self.window_inner });
self.window_handler.borrow_mut().on_frame(&mut crate::Window::new(&mut window)); self.window_handler.borrow_mut().on_frame(&mut window);
let mut do_close = false; let mut do_close = false;
@ -359,14 +365,14 @@ impl WindowState {
*/ */
// Check if the user requested the window to close // Check if the user requested the window to close
if window.close_requested { if self.window_inner.close_requested.get() {
do_close = true; do_close = true;
window.close_requested = false; self.window_inner.close_requested.set(false);
} }
if do_close { if do_close {
unsafe { 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 { if let Some(ns_window) = ns_window {
ns_window.close(); ns_window.close();
} else { } else {
@ -430,26 +436,26 @@ impl WindowState {
window_state.trigger_event(Event::Window(WindowEvent::WillClose)); window_state.trigger_event(Event::Window(WindowEvent::WillClose));
// If in non-parented mode, we want to also quit the app altogether // 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 { if let Some(app) = app {
app.stop_(app); app.stop_(app);
} }
} }
} }
unsafe impl HasRawWindowHandle for Window { unsafe impl<'a> HasRawWindowHandle for Window<'a> {
fn raw_window_handle(&self) -> RawWindowHandle { 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(); let mut handle = AppKitWindowHandle::empty();
handle.ns_window = ns_window; 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) RawWindowHandle::AppKit(handle)
} }
} }
unsafe impl HasRawDisplayHandle for Window { unsafe impl<'a> HasRawDisplayHandle for Window<'a> {
fn raw_display_handle(&self) -> RawDisplayHandle { fn raw_display_handle(&self) -> RawDisplayHandle {
RawDisplayHandle::AppKit(AppKitDisplayHandle::empty()) RawDisplayHandle::AppKit(AppKitDisplayHandle::empty())
} }

View file

@ -90,8 +90,7 @@ impl DropTarget {
}; };
unsafe { unsafe {
let mut window = window_state.create_window(); let mut window = crate::Window::new(window_state.create_window());
let mut window = crate::Window::new(&mut window);
let event = Event::Mouse(event); let event = Event::Mouse(event);
let event_status = let event_status =

View file

@ -168,8 +168,7 @@ unsafe fn wnd_proc_inner(
) -> Option<LRESULT> { ) -> Option<LRESULT> {
match msg { match msg {
WM_MOUSEMOVE => { WM_MOUSEMOVE => {
let mut window = window_state.create_window(); let mut window = crate::Window::new(window_state.create_window());
let mut window = crate::Window::new(&mut window);
let x = (lparam & 0xFFFF) as i16 as i32; let x = (lparam & 0xFFFF) as i16 as i32;
let y = ((lparam >> 16) & 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) Some(0)
} }
WM_MOUSEWHEEL | WM_MOUSEHWHEEL => { WM_MOUSEWHEEL | WM_MOUSEHWHEEL => {
let mut window = window_state.create_window(); let mut window = crate::Window::new(window_state.create_window());
let mut window = crate::Window::new(&mut window);
let value = (wparam >> 16) as i16; let value = (wparam >> 16) as i16;
let value = value as i32; 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_LBUTTONDOWN | WM_LBUTTONUP | WM_MBUTTONDOWN | WM_MBUTTONUP | WM_RBUTTONDOWN
| WM_RBUTTONUP | WM_XBUTTONDOWN | WM_XBUTTONUP => { | WM_RBUTTONUP | WM_XBUTTONDOWN | WM_XBUTTONUP => {
let mut window = window_state.create_window(); let mut window = crate::Window::new(window_state.create_window());
let mut window = crate::Window::new(&mut window);
let mut mouse_button_counter = window_state.mouse_button_counter.get(); let mut mouse_button_counter = window_state.mouse_button_counter.get();
@ -278,8 +275,7 @@ unsafe fn wnd_proc_inner(
None None
} }
WM_TIMER => { WM_TIMER => {
let mut window = window_state.create_window(); let mut window = crate::Window::new(window_state.create_window());
let mut window = crate::Window::new(&mut window);
if wparam == WIN_FRAME_TIMER { if wparam == WIN_FRAME_TIMER {
window_state.handler.borrow_mut().as_mut().unwrap().on_frame(&mut window); window_state.handler.borrow_mut().as_mut().unwrap().on_frame(&mut window);
@ -290,8 +286,7 @@ unsafe fn wnd_proc_inner(
WM_CLOSE => { WM_CLOSE => {
// Make sure to release the borrow before the DefWindowProc call // Make sure to release the borrow before the DefWindowProc call
{ {
let mut window = window_state.create_window(); let mut window = crate::Window::new(window_state.create_window());
let mut window = crate::Window::new(&mut window);
window_state window_state
.handler .handler
@ -307,8 +302,7 @@ unsafe fn wnd_proc_inner(
} }
WM_CHAR | WM_SYSCHAR | WM_KEYDOWN | WM_SYSKEYDOWN | WM_KEYUP | WM_SYSKEYUP WM_CHAR | WM_SYSCHAR | WM_KEYDOWN | WM_SYSKEYDOWN | WM_KEYUP | WM_SYSKEYUP
| WM_INPUTLANGCHANGE => { | WM_INPUTLANGCHANGE => {
let mut window = window_state.create_window(); let mut window = crate::Window::new(window_state.create_window());
let mut window = crate::Window::new(&mut window);
let opt_event = let opt_event =
window_state.keyboard_state.borrow_mut().process_message(hwnd, msg, wparam, lparam); window_state.keyboard_state.borrow_mut().process_message(hwnd, msg, wparam, lparam);
@ -329,8 +323,7 @@ unsafe fn wnd_proc_inner(
} }
} }
WM_SIZE => { WM_SIZE => {
let mut window = window_state.create_window(); let mut window = crate::Window::new(window_state.create_window());
let mut window = crate::Window::new(&mut window);
let width = (lparam & 0xFFFF) as u16 as u32; let width = (lparam & 0xFFFF) as u16 as u32;
let height = ((lparam >> 16) & 0xFFFF) as u16 as u32; let height = ((lparam >> 16) & 0xFFFF) as u16 as u32;
@ -676,8 +669,7 @@ impl Window<'_> {
}); });
let handler = { let handler = {
let mut window = window_state.create_window(); let mut window = crate::Window::new(window_state.create_window());
let mut window = crate::Window::new(&mut window);
build(&mut window) build(&mut window)
}; };

View file

@ -50,10 +50,7 @@ pub trait WindowHandler {
} }
pub struct Window<'a> { pub struct Window<'a> {
#[cfg(target_os = "windows")] window: platform::Window<'a>,
window: &'a mut platform::Window<'a>,
#[cfg(not(target_os = "windows"))]
window: &'a mut platform::Window,
// so that Window is !Send on all platforms // so that Window is !Send on all platforms
phantom: PhantomData<*mut ()>, phantom: PhantomData<*mut ()>,
@ -61,12 +58,12 @@ pub struct Window<'a> {
impl<'a> Window<'a> { impl<'a> Window<'a> {
#[cfg(target_os = "windows")] #[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 } Window { window, phantom: PhantomData }
} }
#[cfg(not(target_os = "windows"))] #[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 } Window { window, phantom: PhantomData }
} }

View file

@ -87,7 +87,7 @@ impl Drop for ParentHandle {
} }
} }
pub struct Window { struct WindowInner {
xcb_connection: XcbConnection, xcb_connection: XcbConnection,
window_id: u32, window_id: u32,
window_info: WindowInfo, window_info: WindowInfo,
@ -105,6 +105,10 @@ pub struct Window {
gl_context: Option<GlContext>, gl_context: Option<GlContext>,
} }
pub struct Window<'a> {
inner: &'a mut WindowInner,
}
// Hack to allow sending a RawWindowHandle between threads. Do not make public // Hack to allow sending a RawWindowHandle between threads. Do not make public
struct SendableRwh(RawWindowHandle); struct SendableRwh(RawWindowHandle);
@ -112,7 +116,7 @@ unsafe impl Send for SendableRwh {}
type WindowOpenResult = Result<SendableRwh, ()>; type WindowOpenResult = Result<SendableRwh, ()>;
impl Window { impl<'a> Window<'a> {
pub fn open_parented<P, H, B>(parent: &P, options: WindowOpenOptions, build: B) -> WindowHandle pub fn open_parented<P, H, B>(parent: &P, options: WindowOpenOptions, build: B) -> WindowHandle
where where
P: HasRawWindowHandle, P: HasRawWindowHandle,
@ -311,7 +315,7 @@ impl Window {
GlContext::new(context) GlContext::new(context)
}); });
let mut window = Self { let mut inner = WindowInner {
xcb_connection, xcb_connection,
window_id, window_id,
window_info, window_info,
@ -329,57 +333,56 @@ impl Window {
gl_context, 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 // Send an initial window resized event so the user is alerted of
// the correct dpi scaling. // the correct dpi scaling.
handler.on_event( handler.on_event(&mut window, Event::Window(WindowEvent::Resized(window_info)));
&mut crate::Window::new(&mut window),
Event::Window(WindowEvent::Resized(window_info)),
);
let _ = tx.send(Ok(SendableRwh(window.raw_window_handle()))); 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) { pub fn set_mouse_cursor(&mut self, mouse_cursor: MouseCursor) {
if self.mouse_cursor == mouse_cursor { if self.inner.mouse_cursor == mouse_cursor {
return; 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 { if xid != 0 {
xcb::change_window_attributes( xcb::change_window_attributes(
&self.xcb_connection.conn, &self.inner.xcb_connection.conn,
self.window_id, self.inner.window_id,
&[(xcb::CW_CURSOR, xid)], &[(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) { pub fn close(&mut self) {
self.close_requested = true; self.inner.close_requested = true;
} }
pub fn resize(&mut self, size: Size) { 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); let new_window_info = WindowInfo::from_logical_size(size, scaling);
xcb::configure_window( xcb::configure_window(
&self.xcb_connection.conn, &self.inner.xcb_connection.conn,
self.window_id, self.inner.window_id,
&[ &[
(xcb::CONFIG_WINDOW_WIDTH as u16, new_window_info.physical_size().width), (xcb::CONFIG_WINDOW_WIDTH as u16, new_window_info.physical_size().width),
(xcb::CONFIG_WINDOW_HEIGHT as u16, new_window_info.physical_size().height), (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` // This will trigger a `ConfigureNotify` event which will in turn change `self.window_info`
// and notify the window handler about it // and notify the window handler about it
@ -387,7 +390,7 @@ impl Window {
#[cfg(feature = "opengl")] #[cfg(feature = "opengl")]
pub fn gl_context(&self) -> Option<&crate::gl::GlContext> { 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<xcb_screen_t>, depth: u8) -> Option<u32> { fn find_visual_for_depth(screen: &StructPtr<xcb_screen_t>, depth: u8) -> Option<u32> {
@ -405,7 +408,9 @@ impl Window {
None None
} }
}
impl WindowInner {
#[inline] #[inline]
fn drain_xcb_events(&mut self, handler: &mut dyn WindowHandler) { 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 // 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; let window_info = self.window_info;
handler.on_event( handler.on_event(
&mut crate::Window::new(self), &mut crate::Window::new(Window { inner: self }),
Event::Window(WindowEvent::Resized(window_info)), Event::Window(WindowEvent::Resized(window_info)),
); );
} }
@ -453,7 +458,7 @@ impl Window {
// if it's already time to draw a new frame. // if it's already time to draw a new frame.
let next_frame = last_frame + self.frame_interval; let next_frame = last_frame + self.frame_interval;
if Instant::now() >= next_frame { 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); 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) { 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 // FIXME: handler should decide whether window stays open or not
self.event_loop_running = false; self.event_loop_running = false;
} }
fn handle_must_close(&mut self, handler: &mut dyn WindowHandler) { 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; self.event_loop_running = false;
} }
@ -578,7 +589,7 @@ impl Window {
let logical_pos = physical_pos.to_logical(&self.window_info); let logical_pos = physical_pos.to_logical(&self.window_info);
handler.on_event( handler.on_event(
&mut crate::Window::new(self), &mut crate::Window::new(Window { inner: self }),
Event::Mouse(MouseEvent::CursorMoved { Event::Mouse(MouseEvent::CursorMoved {
position: logical_pos, position: logical_pos,
modifiers: key_mods(event.state()), modifiers: key_mods(event.state()),
@ -589,7 +600,7 @@ impl Window {
xcb::ENTER_NOTIFY => { xcb::ENTER_NOTIFY => {
handler.on_event( handler.on_event(
&mut crate::Window::new(self), &mut crate::Window::new(Window { inner: self }),
Event::Mouse(MouseEvent::CursorEntered), Event::Mouse(MouseEvent::CursorEntered),
); );
// since no `MOTION_NOTIFY` event is generated when `ENTER_NOTIFY` is generated, // 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 physical_pos = PhyPoint::new(event.event_x() as i32, event.event_y() as i32);
let logical_pos = physical_pos.to_logical(&self.window_info); let logical_pos = physical_pos.to_logical(&self.window_info);
handler.on_event( handler.on_event(
&mut crate::Window::new(self), &mut crate::Window::new(Window { inner: self }),
Event::Mouse(MouseEvent::CursorMoved { Event::Mouse(MouseEvent::CursorMoved {
position: logical_pos, position: logical_pos,
modifiers: key_mods(event.state()), modifiers: key_mods(event.state()),
@ -607,8 +618,10 @@ impl Window {
} }
xcb::LEAVE_NOTIFY => { xcb::LEAVE_NOTIFY => {
handler handler.on_event(
.on_event(&mut crate::Window::new(self), Event::Mouse(MouseEvent::CursorLeft)); &mut crate::Window::new(Window { inner: self }),
Event::Mouse(MouseEvent::CursorLeft),
);
} }
xcb::BUTTON_PRESS => { xcb::BUTTON_PRESS => {
@ -618,7 +631,7 @@ impl Window {
match detail { match detail {
4..=7 => { 4..=7 => {
handler.on_event( handler.on_event(
&mut crate::Window::new(self), &mut crate::Window::new(Window { inner: self }),
Event::Mouse(MouseEvent::WheelScrolled { Event::Mouse(MouseEvent::WheelScrolled {
delta: match detail { delta: match detail {
4 => ScrollDelta::Lines { x: 0.0, y: 1.0 }, 4 => ScrollDelta::Lines { x: 0.0, y: 1.0 },
@ -634,7 +647,7 @@ impl Window {
detail => { detail => {
let button_id = mouse_id(detail); let button_id = mouse_id(detail);
handler.on_event( handler.on_event(
&mut crate::Window::new(self), &mut crate::Window::new(Window { inner: self }),
Event::Mouse(MouseEvent::ButtonPressed { Event::Mouse(MouseEvent::ButtonPressed {
button: button_id, button: button_id,
modifiers: key_mods(event.state()), modifiers: key_mods(event.state()),
@ -651,7 +664,7 @@ impl Window {
if !(4..=7).contains(&detail) { if !(4..=7).contains(&detail) {
let button_id = mouse_id(detail); let button_id = mouse_id(detail);
handler.on_event( handler.on_event(
&mut crate::Window::new(self), &mut crate::Window::new(Window { inner: self }),
Event::Mouse(MouseEvent::ButtonReleased { Event::Mouse(MouseEvent::ButtonReleased {
button: button_id, button: button_id,
modifiers: key_mods(event.state()), modifiers: key_mods(event.state()),
@ -667,7 +680,7 @@ impl Window {
let event = unsafe { xcb::cast_event::<xcb::KeyPressEvent>(&event) }; let event = unsafe { xcb::cast_event::<xcb::KeyPressEvent>(&event) };
handler.on_event( handler.on_event(
&mut crate::Window::new(self), &mut crate::Window::new(Window { inner: self }),
Event::Keyboard(convert_key_press_event(event)), Event::Keyboard(convert_key_press_event(event)),
); );
} }
@ -676,7 +689,7 @@ impl Window {
let event = unsafe { xcb::cast_event::<xcb::KeyReleaseEvent>(&event) }; let event = unsafe { xcb::cast_event::<xcb::KeyReleaseEvent>(&event) };
handler.on_event( handler.on_event(
&mut crate::Window::new(self), &mut crate::Window::new(Window { inner: self }),
Event::Keyboard(convert_key_release_event(event)), 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 { fn raw_window_handle(&self) -> RawWindowHandle {
let mut handle = XlibWindowHandle::empty(); let mut handle = XlibWindowHandle::empty();
handle.window = self.window_id.into(); handle.window = self.inner.window_id.into();
handle.visual_id = self.visual_id.into(); handle.visual_id = self.inner.visual_id.into();
RawWindowHandle::Xlib(handle) RawWindowHandle::Xlib(handle)
} }
} }
unsafe impl HasRawDisplayHandle for Window { unsafe impl<'a> HasRawDisplayHandle for Window<'a> {
fn raw_display_handle(&self) -> RawDisplayHandle { 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(); let mut handle = XlibDisplayHandle::empty();
handle.display = display as *mut c_void; handle.display = display as *mut c_void;