From a934a03738efd82a1fc1e28c6c01b414a0051f7e Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 26 Nov 2026 14:30:28 +0100 Subject: fix(engine): don't bombard windowing app msg queue when window is drag resized --- engine/src/windowing.rs | 97 +++++++++++++++++++++++++++++------------- engine/src/windowing/window.rs | 36 ++++++++++++---- 2 files changed, 96 insertions(+), 37 deletions(-) (limited to 'engine') diff --git a/engine/src/windowing.rs b/engine/src/windowing.rs index fee8755..8214515 100644 --- a/engine/src/windowing.rs +++ b/engine/src/windowing.rs @@ -195,17 +195,23 @@ fn handle_app_messages(context: &mut Context, actions: &mut Actions, world: &Wor MessageFromApp::Window(WindowMessage::Created( window_ent_id, winit_window, - window_creation_attrs, + window_settings, )) => { + let window_id = WindowId::from_inner(winit_window.id()); + + tracing::info!( + window_id = ?window_id, + window_title = %window_settings.title, + "Window creation completed" + ); + actions.add_components( window_ent_id, - (Window::new(&winit_window, &window_creation_attrs),), + (Window::new(&winit_window, &window_settings),), ); actions.remove_comps::<(WindowCreationReady,)>(window_ent_id); - let window_id = WindowId::from_inner(winit_window.id()); - windows.insert( window_id, ( @@ -214,15 +220,10 @@ fn handle_app_messages(context: &mut Context, actions: &mut Actions, world: &Wor pending_resize_req: None, entity_id: window_ent_id, flags: WindowFlags::empty(), + settings: window_settings, }, ), ); - - tracing::info!( - window_id = ?window_id, - window_title = %window_creation_attrs.title, - "Window creation completed" - ); } MessageFromApp::Window(WindowMessage::Resized(window_id)) => { let Some((_, window_metadata)) = windows.get_mut(window_id) else { @@ -444,7 +445,8 @@ fn handle_window_changed( continue; }; - window.apply(winit_window); + let is_window_settings_changed = + window.apply_to_settings(&mut window_metadata.settings); if window_metadata.pending_resize_req.is_none() && window.inner_size != window.real_inner_size @@ -468,10 +470,14 @@ fn handle_window_changed( } } - context.try_send_message_to_app(MessageToApp::SetWindowCursorGrabMode( - window.wid(), - window.cursor_grab_mode, - )); + if is_window_settings_changed { + let new_window_settings = window_metadata.settings.clone(); + + context.try_send_message_to_app(MessageToApp::SetWindowSettings( + window.wid(), + new_window_settings, + )); + } } } @@ -692,6 +698,7 @@ struct WindowMetadata pending_resize_req: Option>, entity_id: Uid, flags: WindowFlags, + settings: WindowSettings, } bitflags! { @@ -797,7 +804,7 @@ enum MessageFromApp #[derive(Debug)] enum WindowMessage { - Created(Uid, Arc, WindowCreationAttributes), + Created(Uid, Arc, WindowSettings), Resized(WindowId), CloseRequested(WindowId), ScaleFactorChanged(WindowId, f64), @@ -807,7 +814,7 @@ enum WindowMessage enum MessageToApp { CreateWindow(Uid, WindowCreationAttributes), - SetWindowCursorGrabMode(WindowId, CursorGrabMode), + SetWindowSettings(WindowId, WindowSettings), } #[derive(Debug)] @@ -940,34 +947,40 @@ impl App window_creation_attrs.apply_extra_attrs_to_window(&winit_window); + let window_settings = + WindowSettings::from_window_creation_attrs(window_creation_attrs); + self.windows.insert( WindowId::from_inner(winit_window.id()), - ( - Arc::downgrade(&winit_window), - WindowSettings { - cursor_grab_mode: window_creation_attrs.cursor_grab_mode, - }, - ), + (Arc::downgrade(&winit_window), window_settings.clone()), ); self.send_message(MessageFromApp::Window(WindowMessage::Created( window_ent_id, winit_window, - window_creation_attrs, + window_settings, ))); } - MessageToApp::SetWindowCursorGrabMode(window_id, cursor_grab_mode) => { - let Some((_, window_settings)) = self.windows.get_mut(window_id) + MessageToApp::SetWindowSettings(window_id, new_window_settings) => { + let Some((winit_window, window_settings)) = + self.windows.get_mut(window_id) else { tracing::warn!( window_id=?window_id, - "Cannot set window cursor grab mode. Window not found" + "Cannot set window settings. Window not found" ); continue; }; - window_settings.cursor_grab_mode = cursor_grab_mode; + let Some(winit_window) = winit_window.upgrade() else { + continue; + }; + + new_window_settings + .apply_to_winit_window(&winit_window, window_settings); + + *window_settings = new_window_settings; } } } @@ -1259,12 +1272,38 @@ impl ApplicationHandler for App } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone)] struct WindowSettings { + title: Box, + cursor_visible: bool, cursor_grab_mode: CursorGrabMode, } +impl WindowSettings +{ + fn from_window_creation_attrs(window_creation_attrs: WindowCreationAttributes) + -> Self + { + Self { + title: window_creation_attrs.title.into_owned().into_boxed_str(), + cursor_visible: window_creation_attrs.cursor_visible, + cursor_grab_mode: window_creation_attrs.cursor_grab_mode, + } + } + + fn apply_to_winit_window(&self, winit_window: &WinitWindow, old_settings: &Self) + { + if self.title != old_settings.title { + winit_window.set_title(&self.title); + } + + if self.cursor_visible != old_settings.cursor_visible { + winit_window.set_cursor_visible(self.cursor_visible); + } + } +} + fn iter_array_queue( queue: &ArrayQueue, ) -> impl Iterator + use<'_, Item> diff --git a/engine/src/windowing/window.rs b/engine/src/windowing/window.rs index 6f8ea6c..1f67130 100644 --- a/engine/src/windowing/window.rs +++ b/engine/src/windowing/window.rs @@ -225,17 +225,17 @@ impl Window self.scale_factor } - pub(crate) fn new( + pub(super) fn new( winit_window: &winit::window::Window, - creation_attrs: &CreationAttributes, + window_settings: &super::WindowSettings, ) -> Self { let inner_size = PhysicalSize::from(winit_window.inner_size()); Self { - title: creation_attrs.title.clone().into_owned(), - cursor_visible: creation_attrs.cursor_visible, - cursor_grab_mode: creation_attrs.cursor_grab_mode, + title: window_settings.title.clone().into_string(), + cursor_visible: window_settings.cursor_visible, + cursor_grab_mode: window_settings.cursor_grab_mode, inner_size, wid: Id::from_inner(winit_window.id()), scale_factor: winit_window.scale_factor(), @@ -243,10 +243,30 @@ impl Window } } - pub(crate) fn apply(&self, winit_window: &winit::window::Window) + pub(super) fn apply_to_settings(&self, settings: &mut super::WindowSettings) -> bool { - winit_window.set_title(&self.title); - winit_window.set_cursor_visible(self.cursor_visible); + let mut is_settings_changed = false; + + if self.title != *settings.title { + settings.title = self.title.clone().into_boxed_str(); + is_settings_changed = true; + } + + // For cursor visibility specifically, we especially do not want to set it unless + // it has changed. This is because of glitchiness that occurs on Windows when + // simultaneously the cursor is confined to the window, the window is drag resized + // and the cursor is made invisible. + if self.cursor_visible != settings.cursor_visible { + settings.cursor_visible = self.cursor_visible; + is_settings_changed = true; + } + + if self.cursor_grab_mode != settings.cursor_grab_mode { + settings.cursor_grab_mode = self.cursor_grab_mode; + is_settings_changed = true; + } + + is_settings_changed } pub(crate) fn set_scale_factor(&mut self, scale_factor: f64) -- cgit v1.2.3-18-g5258