From f53bee4225e7c5673cc6c48f61957180081cf08b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Wed, 29 Oct 2025 18:28:18 +0100 Subject: [PATCH] Reconfigure window `Surface` on any resize event --- winit/src/lib.rs | 17 ++++++++--------- winit/src/window.rs | 11 +++++------ winit/src/window/state.rs | 24 ++++++++---------------- 3 files changed, 21 insertions(+), 31 deletions(-) diff --git a/winit/src/lib.rs b/winit/src/lib.rs index 764fc1c3..3871fd85 100644 --- a/winit/src/lib.rs +++ b/winit/src/lib.rs @@ -697,7 +697,7 @@ async fn run_instance

( id, core::Event::Window(window::Event::Opened { position: window.position(), - size: window.size(), + size: window.logical_size(), }), )); @@ -796,7 +796,9 @@ async fn run_instance

( } // Window was resized between redraws - if window.surface_size != physical_size { + if window.surface_version + != window.state.surface_version() + { let ui = user_interfaces .remove(&id) .expect("Remove user interface"); @@ -814,7 +816,8 @@ async fn run_instance

( physical_size.height, ); - window.surface_size = physical_size; + window.surface_version = + window.state.surface_version(); } let redraw_event = core::Event::Window( @@ -1483,11 +1486,7 @@ fn run_action<'a, P, C>( } window::Action::GetSize(id, channel) => { if let Some(window) = window_manager.get_mut(id) { - let size = window - .raw - .inner_size() - .to_logical(f64::from(window.state.scale_factor())); - + let size = window.logical_size(); let _ = channel.send(Size::new(size.width, size.height)); } } @@ -1761,7 +1760,7 @@ fn run_action<'a, P, C>( }; let cache = ui.into_cache(); - let size = window.size(); + let size = window.logical_size(); let _ = interfaces.insert( id, diff --git a/winit/src/window.rs b/winit/src/window.rs index 5d66913f..101556f8 100644 --- a/winit/src/window.rs +++ b/winit/src/window.rs @@ -58,6 +58,7 @@ where ) -> &mut Window { let state = State::new(program, id, &window, system_theme); let surface_size = state.physical_size(); + let surface_version = state.surface_version(); let surface = compositor.create_surface( window.clone(), surface_size.width, @@ -74,7 +75,7 @@ where state, exit_on_close_request, surface, - surface_size, + surface_version, renderer, mouse_interaction: mouse::Interaction::None, redraw_at: None, @@ -166,7 +167,7 @@ where pub exit_on_close_request: bool, pub mouse_interaction: mouse::Interaction, pub surface: C::Surface, - pub surface_size: Size, + pub surface_version: u64, pub renderer: P::Renderer, pub redraw_at: Option, preedit: Option>, @@ -190,10 +191,8 @@ where }) } - pub fn size(&self) -> Size { - let size = self.raw.inner_size().to_logical(self.raw.scale_factor()); - - Size::new(size.width, size.height) + pub fn logical_size(&self) -> Size { + self.state.logical_size() } pub fn request_redraw(&mut self, redraw_request: RedrawRequest) { diff --git a/winit/src/window/state.rs b/winit/src/window/state.rs index 042e37be..8db36697 100644 --- a/winit/src/window/state.rs +++ b/winit/src/window/state.rs @@ -17,6 +17,7 @@ where title: String, scale_factor: f32, viewport: Viewport, + surface_version: u64, cursor_position: Option>, modifiers: winit::keyboard::ModifiersState, theme: Option, @@ -72,6 +73,7 @@ where title, scale_factor, viewport, + surface_version: 0, cursor_position: None, modifiers: winit::keyboard::ModifiersState::default(), theme, @@ -81,27 +83,26 @@ where } } - /// Returns the current [`Viewport`] of the [`State`]. pub fn viewport(&self) -> &Viewport { &self.viewport } - /// Returns the physical [`Size`] of the [`Viewport`] of the [`State`]. + pub fn surface_version(&self) -> u64 { + self.surface_version + } + pub fn physical_size(&self) -> Size { self.viewport.physical_size() } - /// Returns the logical [`Size`] of the [`Viewport`] of the [`State`]. pub fn logical_size(&self) -> Size { self.viewport.logical_size() } - /// Returns the current scale factor of the [`Viewport`] of the [`State`]. pub fn scale_factor(&self) -> f32 { self.viewport.scale_factor() } - /// Returns the current cursor position of the [`State`]. pub fn cursor(&self) -> mouse::Cursor { self.cursor_position .map(|cursor_position| { @@ -114,32 +115,26 @@ where .unwrap_or(mouse::Cursor::Unavailable) } - /// Returns the current keyboard modifiers of the [`State`]. pub fn modifiers(&self) -> winit::keyboard::ModifiersState { self.modifiers } - /// Returns the current theme of the [`State`]. pub fn theme(&self) -> &P::Theme { self.theme.as_ref().unwrap_or(&self.default_theme) } - /// Returns the current [`theme::Mode`] of the [`State`]. pub fn theme_mode(&self) -> theme::Mode { self.theme_mode } - /// Returns the current background [`Color`] of the [`State`]. pub fn background_color(&self) -> Color { self.style.background_color } - /// Returns the current text [`Color`] of the [`State`]. pub fn text_color(&self) -> Color { self.style.text_color } - /// Processes the provided window event and updates the [`State`] accordingly. pub fn update( &mut self, program: &program::Instance

, @@ -154,6 +149,7 @@ where size, window.scale_factor() as f32 * self.scale_factor, ); + self.surface_version += 1; } WindowEvent::ScaleFactorChanged { scale_factor: new_scale_factor, @@ -165,6 +161,7 @@ where size, *new_scale_factor as f32 * self.scale_factor, ); + self.surface_version += 1; } WindowEvent::CursorMoved { position, .. } | WindowEvent::Touch(Touch { @@ -192,11 +189,6 @@ where } } - /// Synchronizes the [`State`] with its [`Program`] and its respective - /// window. - /// - /// Normally, a [`Program`] should be synchronized with its [`State`] - /// and window after calling [`Program::update`]. pub fn synchronize( &mut self, program: &program::Instance

,