Reconfigure window Surface on any resize event

This commit is contained in:
Héctor Ramón Jiménez 2025-10-29 18:28:18 +01:00
parent 9d4ef528d8
commit f53bee4225
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
3 changed files with 21 additions and 31 deletions

View file

@ -697,7 +697,7 @@ async fn run_instance<P>(
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<P>(
}
// 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<P>(
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,

View file

@ -58,6 +58,7 @@ where
) -> &mut Window<P, C> {
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<u32>,
pub surface_version: u64,
pub renderer: P::Renderer,
pub redraw_at: Option<Instant>,
preedit: Option<Preedit<P::Renderer>>,
@ -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) {

View file

@ -17,6 +17,7 @@ where
title: String,
scale_factor: f32,
viewport: Viewport,
surface_version: u64,
cursor_position: Option<winit::dpi::PhysicalPosition<f64>>,
modifiers: winit::keyboard::ModifiersState,
theme: Option<P::Theme>,
@ -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<u32> {
self.viewport.physical_size()
}
/// Returns the logical [`Size`] of the [`Viewport`] of the [`State`].
pub fn logical_size(&self) -> Size<f32> {
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<P>,
@ -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<P>,