From 32e5da9d3809e056de9fc50b0ec3da24dcc55076 Mon Sep 17 00:00:00 2001 From: John Nunley Date: Tue, 9 Jan 2024 21:18:46 -0800 Subject: [PATCH 1/3] Add ability to get underlying window handle Adds the `get_ref` and `get_mut` functions, which can be used to get references (mutable or otherwise) to the underlying window handle. cc https://github.com/rust-windowing/raw-window-handle/issues/158#issuecomment-1881376603 Signed-off-by: John Nunley --- src/cg.rs | 16 +++++++++++++-- src/kms.rs | 16 +++++++++++++-- src/lib.rs | 51 ++++++++++++++++++++++++++++++++++++++++++++++ src/orbital.rs | 16 +++++++++++++-- src/wayland/mod.rs | 16 +++++++++++++-- src/web.rs | 18 +++++++++++++--- src/win32.rs | 16 +++++++++++++-- src/x11.rs | 16 +++++++++++++-- 8 files changed, 150 insertions(+), 15 deletions(-) diff --git a/src/cg.rs b/src/cg.rs index 9f6035a..cb34b38 100644 --- a/src/cg.rs +++ b/src/cg.rs @@ -30,7 +30,7 @@ pub struct CGImpl { window: id, color_space: CGColorSpace, size: Option<(NonZeroU32, NonZeroU32)>, - _window_source: W, + window_handle: W, _display: PhantomData, } @@ -62,10 +62,22 @@ impl CGImpl { color_space, size: None, _display: PhantomData, - _window_source: window_src, + window_handle: window_src, }) } + /// Get the inner window handle. + #[inline] + pub fn get_ref(&self) -> &W { + &self.window_handle + } + + /// Get a mutable reference to the inner window handle. + #[inline] + pub fn get_mut(&mut self) -> &mut W { + &mut self.window_handle + } + pub fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> { self.size = Some((width, height)); Ok(()) diff --git a/src/kms.rs b/src/kms.rs index 6e041e6..dd4c5ac 100644 --- a/src/kms.rs +++ b/src/kms.rs @@ -73,7 +73,7 @@ pub(crate) struct KmsImpl { buffer: Option, /// Window handle that we are keeping around. - _window: W, + window_handle: W, } #[derive(Debug)] @@ -200,10 +200,22 @@ impl KmsImpl { connectors, display, buffer: None, - _window: window, + window_handle: window, }) } + /// Get the inner window handle. + #[inline] + pub fn get_ref(&self) -> &W { + &self.window_handle + } + + /// Get a mutable reference to the inner window handle. + #[inline] + pub fn get_mut(&mut self) -> &mut W { + &mut self.window_handle + } + /// Resize the internal buffer to the given size. pub(crate) fn resize( &mut self, diff --git a/src/lib.rs b/src/lib.rs index 6ec267c..e6ec2cb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -86,6 +86,24 @@ macro_rules! make_dispatch { } impl SurfaceDispatch { + fn get_ref(&self) -> &W { + match self { + $( + $(#[$attr])* + Self::$name(inner) => inner.get_ref(), + )* + } + } + + fn get_mut(&mut self) -> &mut W { + match self { + $( + $(#[$attr])* + Self::$name(inner) => inner.get_mut(), + )* + } + } + pub fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> { match self { $( @@ -311,6 +329,16 @@ impl Surface { }) } + /// Get a reference to the underlying window handle. + pub fn get_ref(&self) -> &W { + self.surface_impl.get_ref() + } + + /// Get a mutable reference to the underlying window handle. + pub fn get_mut(&mut self) -> &mut W { + self.surface_impl.get_mut() + } + /// Set the size of the buffer that will be returned by [`Surface::buffer_mut`]. /// /// If the size of the buffer does not match the size of the window, the buffer is drawn @@ -350,6 +378,29 @@ impl Surface { } } +impl AsRef for Surface { + #[inline] + fn as_ref(&self) -> &W { + self.get_ref() + } +} + +impl AsMut for Surface { + #[inline] + fn as_mut(&mut self) -> &mut W { + self.get_mut() + } +} + +impl HasWindowHandle for Surface { + #[inline] + fn window_handle( + &self, + ) -> Result, raw_window_handle::HandleError> { + self.get_ref().window_handle() + } +} + /// A buffer that can be written to by the CPU and presented to the window. /// /// This derefs to a `[u32]`, which depending on the backend may be a mapping into shared memory diff --git a/src/orbital.rs b/src/orbital.rs index 3495b89..92afdc0 100644 --- a/src/orbital.rs +++ b/src/orbital.rs @@ -59,7 +59,7 @@ pub struct OrbitalImpl { width: u32, height: u32, presented: bool, - _window_source: W, + window_handle: W, _display: PhantomData, } @@ -76,11 +76,23 @@ impl OrbitalImpl { width: 0, height: 0, presented: false, - _window_source: window, + window_handle: window, _display: PhantomData, }) } + /// Get the inner window handle. + #[inline] + pub fn get_ref(&self) -> &W { + &self.window_handle + } + + /// Get a mutable reference to the inner window handle. + #[inline] + pub fn get_mut(&mut self) -> &mut W { + &mut self.window_handle + } + pub fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> { let width = width.get(); let height = height.get(); diff --git a/src/wayland/mod.rs b/src/wayland/mod.rs index 8984872..0fc7592 100644 --- a/src/wayland/mod.rs +++ b/src/wayland/mod.rs @@ -83,7 +83,7 @@ pub struct WaylandImpl { /// /// This has to be dropped *after* the `surface` field, because the `surface` field implicitly /// borrows this. - _window: W, + window_handle: W, } impl WaylandImpl { @@ -109,10 +109,22 @@ impl WaylandImpl { surface: Some(surface), buffers: Default::default(), size: None, - _window: window, + window_handle: window, }) } + /// Get the inner window handle. + #[inline] + pub fn get_ref(&self) -> &W { + &self.window_handle + } + + /// Get a mutable reference to the inner window handle. + #[inline] + pub fn get_mut(&mut self) -> &mut W { + &mut self.window_handle + } + pub fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> { self.size = Some( (|| { diff --git a/src/web.rs b/src/web.rs index 7b78fca..7969bee 100644 --- a/src/web.rs +++ b/src/web.rs @@ -57,7 +57,7 @@ pub struct WebImpl { size: Option<(NonZeroU32, NonZeroU32)>, /// The underlying window handle. - _window: W, + window_handle: W, /// The underlying display handle. _display: PhantomData, @@ -114,7 +114,7 @@ impl WebImpl { buffer: Vec::new(), buffer_presented: false, size: None, - _window: window, + window_handle: window, _display: PhantomData, }) } @@ -130,11 +130,23 @@ impl WebImpl { buffer: Vec::new(), buffer_presented: false, size: None, - _window: window, + window_handle: window, _display: PhantomData, }) } + /// Get the inner window handle. + #[inline] + pub fn get_ref(&self) -> &W { + &self.window_handle + } + + /// Get a mutable reference to the inner window handle. + #[inline] + pub fn get_mut(&mut self) -> &mut W { + &mut self.window_handle + } + /// De-duplicates the error handling between `HtmlCanvasElement` and `OffscreenCanvas`. fn resolve_ctx( result: Option>, diff --git a/src/win32.rs b/src/win32.rs index 4263d1d..bfb4622 100644 --- a/src/win32.rs +++ b/src/win32.rs @@ -142,7 +142,7 @@ pub struct Win32Impl { /// The handle for the window. /// /// This should be kept alive in order to keep `window` valid. - _window: W, + handle: W, /// The display handle. /// @@ -184,11 +184,23 @@ impl Win32Impl { dc, window: hwnd, buffer: None, - _window: window, + handle: window, _display: PhantomData, }) } + /// Get the inner window handle. + #[inline] + pub fn get_ref(&self) -> &W { + &self.handle + } + + /// Get a mutable reference to the inner window handle. + #[inline] + pub fn get_mut(&mut self) -> &mut W { + &mut self.handle + } + pub fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> { let (width, height) = (|| { let width = NonZeroI32::new(i32::try_from(width.get()).ok()?)?; diff --git a/src/x11.rs b/src/x11.rs index a5baa82..14a3e16 100644 --- a/src/x11.rs +++ b/src/x11.rs @@ -150,7 +150,7 @@ pub struct X11Impl { size: Option<(NonZeroU16, NonZeroU16)>, /// Keep the window alive. - _window_handle: W, + window_handle: W, } /// The buffer that is being drawn to. @@ -292,10 +292,22 @@ impl X11Impl { buffer, buffer_presented: false, size: None, - _window_handle: window_src, + window_handle: window_src, }) } + /// Get the inner window handle. + #[inline] + pub fn get_ref(&self) -> &W { + &self.window_handle + } + + /// Get a mutable reference to the inner window handle. + #[inline] + pub fn get_mut(&mut self) -> &mut W { + &mut self.window_handle + } + /// Resize the internal buffer to the given width and height. pub(crate) fn resize( &mut self, From baeea1b4215a25bf350c66ec06c5aba4224018c8 Mon Sep 17 00:00:00 2001 From: John Nunley Date: Wed, 10 Jan 2024 18:12:17 -0800 Subject: [PATCH 2/3] get_ref -> window Signed-off-by: John Nunley --- src/cg.rs | 4 ++-- src/kms.rs | 4 ++-- src/lib.rs | 22 +++++++++++----------- src/orbital.rs | 4 ++-- src/wayland/mod.rs | 4 ++-- src/web.rs | 4 ++-- src/win32.rs | 4 ++-- src/x11.rs | 4 ++-- 8 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/cg.rs b/src/cg.rs index cb34b38..7e791e2 100644 --- a/src/cg.rs +++ b/src/cg.rs @@ -68,13 +68,13 @@ impl CGImpl { /// Get the inner window handle. #[inline] - pub fn get_ref(&self) -> &W { + pub fn window(&self) -> &W { &self.window_handle } /// Get a mutable reference to the inner window handle. #[inline] - pub fn get_mut(&mut self) -> &mut W { + pub fn window_mut(&mut self) -> &mut W { &mut self.window_handle } diff --git a/src/kms.rs b/src/kms.rs index dd4c5ac..d04da29 100644 --- a/src/kms.rs +++ b/src/kms.rs @@ -206,13 +206,13 @@ impl KmsImpl { /// Get the inner window handle. #[inline] - pub fn get_ref(&self) -> &W { + pub fn window(&self) -> &W { &self.window_handle } /// Get a mutable reference to the inner window handle. #[inline] - pub fn get_mut(&mut self) -> &mut W { + pub fn window_mut(&mut self) -> &mut W { &mut self.window_handle } diff --git a/src/lib.rs b/src/lib.rs index e6ec2cb..626557f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -86,20 +86,20 @@ macro_rules! make_dispatch { } impl SurfaceDispatch { - fn get_ref(&self) -> &W { + fn window(&self) -> &W { match self { $( $(#[$attr])* - Self::$name(inner) => inner.get_ref(), + Self::$name(inner) => inner.window(), )* } } - fn get_mut(&mut self) -> &mut W { + fn window_mut(&mut self) -> &mut W { match self { $( $(#[$attr])* - Self::$name(inner) => inner.get_mut(), + Self::$name(inner) => inner.window_mut(), )* } } @@ -330,13 +330,13 @@ impl Surface { } /// Get a reference to the underlying window handle. - pub fn get_ref(&self) -> &W { - self.surface_impl.get_ref() + pub fn window(&self) -> &W { + self.surface_impl.window() } /// Get a mutable reference to the underlying window handle. - pub fn get_mut(&mut self) -> &mut W { - self.surface_impl.get_mut() + pub fn window_mut(&mut self) -> &mut W { + self.surface_impl.window_mut() } /// Set the size of the buffer that will be returned by [`Surface::buffer_mut`]. @@ -381,14 +381,14 @@ impl Surface { impl AsRef for Surface { #[inline] fn as_ref(&self) -> &W { - self.get_ref() + self.window() } } impl AsMut for Surface { #[inline] fn as_mut(&mut self) -> &mut W { - self.get_mut() + self.window_mut() } } @@ -397,7 +397,7 @@ impl HasWindowHandle for Surface fn window_handle( &self, ) -> Result, raw_window_handle::HandleError> { - self.get_ref().window_handle() + self.window().window_handle() } } diff --git a/src/orbital.rs b/src/orbital.rs index 92afdc0..cc6f6b8 100644 --- a/src/orbital.rs +++ b/src/orbital.rs @@ -83,13 +83,13 @@ impl OrbitalImpl { /// Get the inner window handle. #[inline] - pub fn get_ref(&self) -> &W { + pub fn window(&self) -> &W { &self.window_handle } /// Get a mutable reference to the inner window handle. #[inline] - pub fn get_mut(&mut self) -> &mut W { + pub fn window_mut(&mut self) -> &mut W { &mut self.window_handle } diff --git a/src/wayland/mod.rs b/src/wayland/mod.rs index 0fc7592..473f4b0 100644 --- a/src/wayland/mod.rs +++ b/src/wayland/mod.rs @@ -115,13 +115,13 @@ impl WaylandImpl { /// Get the inner window handle. #[inline] - pub fn get_ref(&self) -> &W { + pub fn window(&self) -> &W { &self.window_handle } /// Get a mutable reference to the inner window handle. #[inline] - pub fn get_mut(&mut self) -> &mut W { + pub fn window_mut(&mut self) -> &mut W { &mut self.window_handle } diff --git a/src/web.rs b/src/web.rs index 7969bee..ceac7e6 100644 --- a/src/web.rs +++ b/src/web.rs @@ -137,13 +137,13 @@ impl WebImpl { /// Get the inner window handle. #[inline] - pub fn get_ref(&self) -> &W { + pub fn window(&self) -> &W { &self.window_handle } /// Get a mutable reference to the inner window handle. #[inline] - pub fn get_mut(&mut self) -> &mut W { + pub fn window_mut(&mut self) -> &mut W { &mut self.window_handle } diff --git a/src/win32.rs b/src/win32.rs index bfb4622..efb1be7 100644 --- a/src/win32.rs +++ b/src/win32.rs @@ -191,13 +191,13 @@ impl Win32Impl { /// Get the inner window handle. #[inline] - pub fn get_ref(&self) -> &W { + pub fn window(&self) -> &W { &self.handle } /// Get a mutable reference to the inner window handle. #[inline] - pub fn get_mut(&mut self) -> &mut W { + pub fn window_mut(&mut self) -> &mut W { &mut self.handle } diff --git a/src/x11.rs b/src/x11.rs index 14a3e16..508501f 100644 --- a/src/x11.rs +++ b/src/x11.rs @@ -298,13 +298,13 @@ impl X11Impl { /// Get the inner window handle. #[inline] - pub fn get_ref(&self) -> &W { + pub fn window(&self) -> &W { &self.window_handle } /// Get a mutable reference to the inner window handle. #[inline] - pub fn get_mut(&mut self) -> &mut W { + pub fn window_mut(&mut self) -> &mut W { &mut self.window_handle } From 2b59776a7bdcc7bfc2cfa3e415a47a52689af7cf Mon Sep 17 00:00:00 2001 From: John Nunley Date: Wed, 10 Jan 2024 21:21:31 -0800 Subject: [PATCH 3/3] Remove window_mut There are considerations to be made here, so remove it for now. Signed-off-by: John Nunley --- src/cg.rs | 6 ------ src/kms.rs | 6 ------ src/lib.rs | 21 --------------------- src/orbital.rs | 6 ------ src/wayland/mod.rs | 6 ------ src/web.rs | 6 ------ src/win32.rs | 6 ------ src/x11.rs | 6 ------ 8 files changed, 63 deletions(-) diff --git a/src/cg.rs b/src/cg.rs index 7e791e2..ef96fb9 100644 --- a/src/cg.rs +++ b/src/cg.rs @@ -72,12 +72,6 @@ impl CGImpl { &self.window_handle } - /// Get a mutable reference to the inner window handle. - #[inline] - pub fn window_mut(&mut self) -> &mut W { - &mut self.window_handle - } - pub fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> { self.size = Some((width, height)); Ok(()) diff --git a/src/kms.rs b/src/kms.rs index d04da29..2761843 100644 --- a/src/kms.rs +++ b/src/kms.rs @@ -210,12 +210,6 @@ impl KmsImpl { &self.window_handle } - /// Get a mutable reference to the inner window handle. - #[inline] - pub fn window_mut(&mut self) -> &mut W { - &mut self.window_handle - } - /// Resize the internal buffer to the given size. pub(crate) fn resize( &mut self, diff --git a/src/lib.rs b/src/lib.rs index 626557f..14191ca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -95,15 +95,6 @@ macro_rules! make_dispatch { } } - fn window_mut(&mut self) -> &mut W { - match self { - $( - $(#[$attr])* - Self::$name(inner) => inner.window_mut(), - )* - } - } - pub fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> { match self { $( @@ -334,11 +325,6 @@ impl Surface { self.surface_impl.window() } - /// Get a mutable reference to the underlying window handle. - pub fn window_mut(&mut self) -> &mut W { - self.surface_impl.window_mut() - } - /// Set the size of the buffer that will be returned by [`Surface::buffer_mut`]. /// /// If the size of the buffer does not match the size of the window, the buffer is drawn @@ -385,13 +371,6 @@ impl AsRef for Surface { } } -impl AsMut for Surface { - #[inline] - fn as_mut(&mut self) -> &mut W { - self.window_mut() - } -} - impl HasWindowHandle for Surface { #[inline] fn window_handle( diff --git a/src/orbital.rs b/src/orbital.rs index cc6f6b8..53e2e99 100644 --- a/src/orbital.rs +++ b/src/orbital.rs @@ -87,12 +87,6 @@ impl OrbitalImpl { &self.window_handle } - /// Get a mutable reference to the inner window handle. - #[inline] - pub fn window_mut(&mut self) -> &mut W { - &mut self.window_handle - } - pub fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> { let width = width.get(); let height = height.get(); diff --git a/src/wayland/mod.rs b/src/wayland/mod.rs index 473f4b0..d61a5f1 100644 --- a/src/wayland/mod.rs +++ b/src/wayland/mod.rs @@ -119,12 +119,6 @@ impl WaylandImpl { &self.window_handle } - /// Get a mutable reference to the inner window handle. - #[inline] - pub fn window_mut(&mut self) -> &mut W { - &mut self.window_handle - } - pub fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> { self.size = Some( (|| { diff --git a/src/web.rs b/src/web.rs index ceac7e6..595073d 100644 --- a/src/web.rs +++ b/src/web.rs @@ -141,12 +141,6 @@ impl WebImpl { &self.window_handle } - /// Get a mutable reference to the inner window handle. - #[inline] - pub fn window_mut(&mut self) -> &mut W { - &mut self.window_handle - } - /// De-duplicates the error handling between `HtmlCanvasElement` and `OffscreenCanvas`. fn resolve_ctx( result: Option>, diff --git a/src/win32.rs b/src/win32.rs index efb1be7..7055b44 100644 --- a/src/win32.rs +++ b/src/win32.rs @@ -195,12 +195,6 @@ impl Win32Impl { &self.handle } - /// Get a mutable reference to the inner window handle. - #[inline] - pub fn window_mut(&mut self) -> &mut W { - &mut self.handle - } - pub fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> { let (width, height) = (|| { let width = NonZeroI32::new(i32::try_from(width.get()).ok()?)?; diff --git a/src/x11.rs b/src/x11.rs index 508501f..31b2325 100644 --- a/src/x11.rs +++ b/src/x11.rs @@ -302,12 +302,6 @@ impl X11Impl { &self.window_handle } - /// Get a mutable reference to the inner window handle. - #[inline] - pub fn window_mut(&mut self) -> &mut W { - &mut self.window_handle - } - /// Resize the internal buffer to the given width and height. pub(crate) fn resize( &mut self,