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 <dev@notgull.net>
This commit is contained in:
parent
03c6f8dca1
commit
832064c012
8 changed files with 87 additions and 15 deletions
10
src/cg.rs
10
src/cg.rs
|
|
@ -30,7 +30,7 @@ pub struct CGImpl<D, W> {
|
||||||
window: id,
|
window: id,
|
||||||
color_space: CGColorSpace,
|
color_space: CGColorSpace,
|
||||||
size: Option<(NonZeroU32, NonZeroU32)>,
|
size: Option<(NonZeroU32, NonZeroU32)>,
|
||||||
_window_source: W,
|
window_handle: W,
|
||||||
_display: PhantomData<D>,
|
_display: PhantomData<D>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -62,10 +62,16 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> CGImpl<D, W> {
|
||||||
color_space,
|
color_space,
|
||||||
size: None,
|
size: None,
|
||||||
_display: PhantomData,
|
_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> {
|
pub fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> {
|
||||||
self.size = Some((width, height));
|
self.size = Some((width, height));
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
||||||
10
src/kms.rs
10
src/kms.rs
|
|
@ -73,7 +73,7 @@ pub(crate) struct KmsImpl<D: ?Sized, W: ?Sized> {
|
||||||
buffer: Option<Buffers>,
|
buffer: Option<Buffers>,
|
||||||
|
|
||||||
/// Window handle that we are keeping around.
|
/// Window handle that we are keeping around.
|
||||||
_window: W,
|
window_handle: W,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|
@ -200,10 +200,16 @@ impl<D: ?Sized, W: HasWindowHandle> KmsImpl<D, W> {
|
||||||
connectors,
|
connectors,
|
||||||
display,
|
display,
|
||||||
buffer: None,
|
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.
|
/// Resize the internal buffer to the given size.
|
||||||
pub(crate) fn resize(
|
pub(crate) fn resize(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
|
|
||||||
30
src/lib.rs
30
src/lib.rs
|
|
@ -86,6 +86,15 @@ macro_rules! make_dispatch {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceDispatch<D, W> {
|
impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceDispatch<D, W> {
|
||||||
|
fn window(&self) -> &W {
|
||||||
|
match self {
|
||||||
|
$(
|
||||||
|
$(#[$attr])*
|
||||||
|
Self::$name(inner) => inner.window(),
|
||||||
|
)*
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> {
|
pub fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> {
|
||||||
match self {
|
match self {
|
||||||
$(
|
$(
|
||||||
|
|
@ -311,6 +320,11 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> Surface<D, W> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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`].
|
/// 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
|
/// If the size of the buffer does not match the size of the window, the buffer is drawn
|
||||||
|
|
@ -350,6 +364,22 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> Surface<D, W> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<D: HasDisplayHandle, W: HasWindowHandle> AsRef<W> for Surface<D, W> {
|
||||||
|
#[inline]
|
||||||
|
fn as_ref(&self) -> &W {
|
||||||
|
self.window()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<D: HasDisplayHandle, W: HasWindowHandle> HasWindowHandle for Surface<D, W> {
|
||||||
|
#[inline]
|
||||||
|
fn window_handle(
|
||||||
|
&self,
|
||||||
|
) -> Result<raw_window_handle::WindowHandle<'_>, raw_window_handle::HandleError> {
|
||||||
|
self.window().window_handle()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// A buffer that can be written to by the CPU and presented to the window.
|
/// 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
|
/// This derefs to a `[u32]`, which depending on the backend may be a mapping into shared memory
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,7 @@ pub struct OrbitalImpl<D, W> {
|
||||||
width: u32,
|
width: u32,
|
||||||
height: u32,
|
height: u32,
|
||||||
presented: bool,
|
presented: bool,
|
||||||
_window_source: W,
|
window_handle: W,
|
||||||
_display: PhantomData<D>,
|
_display: PhantomData<D>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -76,11 +76,17 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> OrbitalImpl<D, W> {
|
||||||
width: 0,
|
width: 0,
|
||||||
height: 0,
|
height: 0,
|
||||||
presented: false,
|
presented: false,
|
||||||
_window_source: window,
|
window_handle: window,
|
||||||
_display: PhantomData,
|
_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> {
|
pub fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> {
|
||||||
let width = width.get();
|
let width = width.get();
|
||||||
let height = height.get();
|
let height = height.get();
|
||||||
|
|
|
||||||
|
|
@ -83,7 +83,7 @@ pub struct WaylandImpl<D: ?Sized, W: ?Sized> {
|
||||||
///
|
///
|
||||||
/// This has to be dropped *after* the `surface` field, because the `surface` field implicitly
|
/// This has to be dropped *after* the `surface` field, because the `surface` field implicitly
|
||||||
/// borrows this.
|
/// borrows this.
|
||||||
_window: W,
|
window_handle: W,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> WaylandImpl<D, W> {
|
impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> WaylandImpl<D, W> {
|
||||||
|
|
@ -109,10 +109,16 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> WaylandImpl<D, W> {
|
||||||
surface: Some(surface),
|
surface: Some(surface),
|
||||||
buffers: Default::default(),
|
buffers: Default::default(),
|
||||||
size: None,
|
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> {
|
pub fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> {
|
||||||
self.size = Some(
|
self.size = Some(
|
||||||
(|| {
|
(|| {
|
||||||
|
|
|
||||||
12
src/web.rs
12
src/web.rs
|
|
@ -57,7 +57,7 @@ pub struct WebImpl<D, W> {
|
||||||
size: Option<(NonZeroU32, NonZeroU32)>,
|
size: Option<(NonZeroU32, NonZeroU32)>,
|
||||||
|
|
||||||
/// The underlying window handle.
|
/// The underlying window handle.
|
||||||
_window: W,
|
window_handle: W,
|
||||||
|
|
||||||
/// The underlying display handle.
|
/// The underlying display handle.
|
||||||
_display: PhantomData<D>,
|
_display: PhantomData<D>,
|
||||||
|
|
@ -114,7 +114,7 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> WebImpl<D, W> {
|
||||||
buffer: Vec::new(),
|
buffer: Vec::new(),
|
||||||
buffer_presented: false,
|
buffer_presented: false,
|
||||||
size: None,
|
size: None,
|
||||||
_window: window,
|
window_handle: window,
|
||||||
_display: PhantomData,
|
_display: PhantomData,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -130,11 +130,17 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> WebImpl<D, W> {
|
||||||
buffer: Vec::new(),
|
buffer: Vec::new(),
|
||||||
buffer_presented: false,
|
buffer_presented: false,
|
||||||
size: None,
|
size: None,
|
||||||
_window: window,
|
window_handle: window,
|
||||||
_display: PhantomData,
|
_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`.
|
/// De-duplicates the error handling between `HtmlCanvasElement` and `OffscreenCanvas`.
|
||||||
fn resolve_ctx<T: JsCast>(
|
fn resolve_ctx<T: JsCast>(
|
||||||
result: Option<Option<Object>>,
|
result: Option<Option<Object>>,
|
||||||
|
|
|
||||||
10
src/win32.rs
10
src/win32.rs
|
|
@ -142,7 +142,7 @@ pub struct Win32Impl<D: ?Sized, W> {
|
||||||
/// The handle for the window.
|
/// The handle for the window.
|
||||||
///
|
///
|
||||||
/// This should be kept alive in order to keep `window` valid.
|
/// This should be kept alive in order to keep `window` valid.
|
||||||
_window: W,
|
handle: W,
|
||||||
|
|
||||||
/// The display handle.
|
/// The display handle.
|
||||||
///
|
///
|
||||||
|
|
@ -184,11 +184,17 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> Win32Impl<D, W> {
|
||||||
dc,
|
dc,
|
||||||
window: hwnd,
|
window: hwnd,
|
||||||
buffer: None,
|
buffer: None,
|
||||||
_window: window,
|
handle: window,
|
||||||
_display: PhantomData,
|
_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> {
|
pub fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> {
|
||||||
let (width, height) = (|| {
|
let (width, height) = (|| {
|
||||||
let width = NonZeroI32::new(i32::try_from(width.get()).ok()?)?;
|
let width = NonZeroI32::new(i32::try_from(width.get()).ok()?)?;
|
||||||
|
|
|
||||||
10
src/x11.rs
10
src/x11.rs
|
|
@ -150,7 +150,7 @@ pub struct X11Impl<D: ?Sized, W: ?Sized> {
|
||||||
size: Option<(NonZeroU16, NonZeroU16)>,
|
size: Option<(NonZeroU16, NonZeroU16)>,
|
||||||
|
|
||||||
/// Keep the window alive.
|
/// Keep the window alive.
|
||||||
_window_handle: W,
|
window_handle: W,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The buffer that is being drawn to.
|
/// The buffer that is being drawn to.
|
||||||
|
|
@ -292,10 +292,16 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> X11Impl<D, W> {
|
||||||
buffer,
|
buffer,
|
||||||
buffer_presented: false,
|
buffer_presented: false,
|
||||||
size: None,
|
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.
|
/// Resize the internal buffer to the given width and height.
|
||||||
pub(crate) fn resize(
|
pub(crate) fn resize(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue