From 832064c012337e2d866a0d9477a7467f13baa570 Mon Sep 17 00:00:00 2001 From: John Nunley Date: Fri, 12 Jan 2024 08:40:02 -0800 Subject: [PATCH] feat: 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 | 10 ++++++++-- src/kms.rs | 10 ++++++++-- src/lib.rs | 30 ++++++++++++++++++++++++++++++ src/orbital.rs | 10 ++++++++-- src/wayland/mod.rs | 10 ++++++++-- src/web.rs | 12 +++++++++--- src/win32.rs | 10 ++++++++-- src/x11.rs | 10 ++++++++-- 8 files changed, 87 insertions(+), 15 deletions(-) diff --git a/src/cg.rs b/src/cg.rs index 9f6035a5..ef96fb9a 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,16 @@ impl CGImpl { color_space, size: None, _display: PhantomData, - _window_source: window_src, + window_handle: window_src, }) } + /// Get the inner window handle. + #[inline] + pub fn window(&self) -> &W { + &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 6e041e61..2761843d 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,16 @@ impl KmsImpl { connectors, display, buffer: None, - _window: window, + window_handle: window, }) } + /// Get the inner window handle. + #[inline] + pub fn window(&self) -> &W { + &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 6ec267ca..14191ca5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -86,6 +86,15 @@ macro_rules! make_dispatch { } impl SurfaceDispatch { + fn window(&self) -> &W { + match self { + $( + $(#[$attr])* + Self::$name(inner) => inner.window(), + )* + } + } + pub fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> { match self { $( @@ -311,6 +320,11 @@ impl Surface { }) } + /// Get a reference to the underlying window handle. + pub fn window(&self) -> &W { + self.surface_impl.window() + } + /// 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 +364,22 @@ impl Surface { } } +impl AsRef for Surface { + #[inline] + fn as_ref(&self) -> &W { + self.window() + } +} + +impl HasWindowHandle for Surface { + #[inline] + fn window_handle( + &self, + ) -> Result, raw_window_handle::HandleError> { + self.window().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 3495b893..53e2e99a 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,17 @@ impl OrbitalImpl { width: 0, height: 0, presented: false, - _window_source: window, + window_handle: window, _display: PhantomData, }) } + /// Get the inner window handle. + #[inline] + pub fn window(&self) -> &W { + &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 89848727..d61a5f16 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,16 @@ impl WaylandImpl { surface: Some(surface), buffers: Default::default(), size: None, - _window: window, + window_handle: window, }) } + /// Get the inner window handle. + #[inline] + pub fn window(&self) -> &W { + &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 7b78fca4..595073d4 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,17 @@ 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 window(&self) -> &W { + &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 4263d1d7..7055b443 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,17 @@ impl Win32Impl { dc, window: hwnd, buffer: None, - _window: window, + handle: window, _display: PhantomData, }) } + /// Get the inner window handle. + #[inline] + pub fn window(&self) -> &W { + &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 a5baa82a..31b2325a 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,16 @@ impl X11Impl { buffer, buffer_presented: false, size: None, - _window_handle: window_src, + window_handle: window_src, }) } + /// Get the inner window handle. + #[inline] + pub fn window(&self) -> &W { + &self.window_handle + } + /// Resize the internal buffer to the given width and height. pub(crate) fn resize( &mut self,