From 66dc0eab25d0a40bbc2009d5950acff179b587ae Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Tue, 15 Oct 2024 19:06:40 -0400 Subject: [PATCH] feat: SuggestedBounds event --- winit-core/src/event.rs | 7 +++++++ winit-wayland/src/event_loop/mod.rs | 16 ++++++++++++++++ winit-wayland/src/state.rs | 24 +++++++++++++++++++----- 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/winit-core/src/event.rs b/winit-core/src/event.rs index 24a80990..9e0540cf 100644 --- a/winit-core/src/event.rs +++ b/winit-core/src/event.rs @@ -368,6 +368,13 @@ pub enum WindowEvent { surface_size_writer: SurfaceSizeWriter, }, + /// The suggested bounds of the window's surface has changed. + /// + /// Contains the new bounds of the surface + /// + /// - **iOS / Android / Web / Orbital / Windows:** Unsupported. + SuggestedBounds(Option>), + /// The system window theme has changed. /// /// Applications might wish to react to this to change the theme of the content of the window diff --git a/winit-wayland/src/event_loop/mod.rs b/winit-wayland/src/event_loop/mod.rs index 3a6ca698..aa6ea201 100644 --- a/winit-wayland/src/event_loop/mod.rs +++ b/winit-wayland/src/event_loop/mod.rs @@ -379,6 +379,22 @@ impl EventLoop { } } + if compositor_update.suggested_bounds { + let suggested_bounds = self.with_state(|state| { + let windows = state.windows.get_mut(); + let window = windows.get(&window_id).unwrap().lock().unwrap(); + + window + .last_configure + .as_ref() + .and_then(|c| c.suggested_bounds) + .map(|b| dpi::PhysicalSize::new(b.0, b.1)) + .clone() + }); + let event = WindowEvent::SuggestedBounds(suggested_bounds); + app.window_event(&self.active_event_loop, window_id, event); + } + // NOTE: Rescale changed the physical size which winit operates in, thus we should // resize. if compositor_update.resized || compositor_update.scale_changed { diff --git a/winit-wayland/src/state.rs b/winit-wayland/src/state.rs index d6090599..68d911d3 100644 --- a/winit-wayland/src/state.rs +++ b/winit-wayland/src/state.rs @@ -301,15 +301,20 @@ impl WindowHandler for WinitState { self.window_compositor_updates.len() - 1 }; - // Populate the configure to the window. - self.window_compositor_updates[pos].resized |= self + let mut winit_window = self .windows .get_mut() .get_mut(&window_id) .expect("got configure for dead window.") .lock() - .unwrap() - .configure(configure, &self.shm, &self.subcompositor_state); + .unwrap(); + // Populate the configure to the window. + + self.window_compositor_updates[pos].suggested_bounds |= configure.suggested_bounds + != winit_window.last_configure.as_ref().and_then(|last| last.suggested_bounds); + + self.window_compositor_updates[pos].resized |= + winit_window.configure(configure, &self.shm, &self.subcompositor_state); // NOTE: configure demands wl_surface::commit, however winit doesn't commit on behalf of the // users, since it can break a lot of things, thus it'll ask users to redraw instead. @@ -437,11 +442,20 @@ pub struct WindowCompositorUpdate { /// Close the window. pub close_window: bool, + + /// New suggested bounds. + pub suggested_bounds: bool, } impl WindowCompositorUpdate { fn new(window_id: WindowId) -> Self { - Self { window_id, resized: false, scale_changed: false, close_window: false } + Self { + window_id, + resized: false, + scale_changed: false, + close_window: false, + suggested_bounds: false, + } } }