winit-win32: prevent inner size reported as (0,0) when minimized

This commit is contained in:
Diggory Hardy 2025-11-01 14:39:57 +00:00 committed by GitHub
parent 9d9d21cfdb
commit a9c189a423
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 37 additions and 10 deletions

View file

@ -1301,7 +1301,6 @@ unsafe fn public_window_callback_inner(
use winit_core::event::WindowEvent::SurfaceResized;
let w = util::loword(lparam as u32) as u32;
let h = util::hiword(lparam as u32) as u32;
let physical_size = PhysicalSize::new(w, h);
{
@ -1313,7 +1312,14 @@ unsafe fn public_window_callback_inner(
w.set_window_flags_in_place(|f| f.set(WindowFlags::MAXIMIZED, maximized));
}
}
userdata.send_window_event(window, SurfaceResized(physical_size));
let mut state = userdata.window_state_lock();
if (w, h) != (0, 0) && physical_size != state.surface_size {
// WM_SIZE is received with size (0, 0) when a window is minimized; ignore.
state.surface_size = physical_size;
drop(state);
userdata.send_window_event(window, SurfaceResized(physical_size));
}
result = ProcResult::Value(0);
},

View file

@ -509,14 +509,7 @@ impl CoreWindow for Window {
}
fn surface_size(&self) -> PhysicalSize<u32> {
let mut rect: RECT = unsafe { mem::zeroed() };
if unsafe { GetClientRect(self.hwnd(), &mut rect) } == false.into() {
panic!(
"Unexpected GetClientRect failure: please report this error to \
rust-windowing/winit"
)
}
PhysicalSize::new((rect.right - rect.left) as u32, (rect.bottom - rect.top) as u32)
self.window_state_lock().surface_size
}
fn outer_size(&self) -> PhysicalSize<u32> {

View file

@ -33,6 +33,9 @@ pub(crate) struct WindowState {
pub min_size: Option<Size>,
pub max_size: Option<Size>,
/// The last known size of the window surface
pub surface_size: PhysicalSize<u32>,
pub surface_resize_increments: Option<Size>,
pub window_icon: Option<Icon>,
@ -166,6 +169,8 @@ impl WindowState {
min_size: attributes.min_surface_size,
max_size: attributes.max_surface_size,
surface_size: PhysicalSize::default(),
surface_resize_increments: attributes.surface_resize_increments,
window_icon: attributes.window_icon.clone(),

View file

@ -203,6 +203,15 @@ impl Application {
Action::DumpMonitors => self.dump_monitors(_event_loop),
Action::Message => {
info!("User wake up");
for (id, window) in self.windows.iter() {
if window.emit_surface_size {
let size = window.window.surface_size();
info!(
"Window {id:?} has physical surface size {}x{}",
size.width, size.height
);
}
}
},
_ => unreachable!("Tried to execute invalid action without `WindowId`"),
}
@ -315,6 +324,9 @@ impl Application {
window.continuous_redraw = !window.continuous_redraw;
window.window.request_redraw();
},
Action::EmitSurfaceSize => {
window.toggle_emit_surface_size();
},
}
}
@ -615,6 +627,8 @@ struct WindowState {
start_time: Instant,
/// Redraw continuously
continuous_redraw: bool,
/// Periodically emit the surface size
emit_surface_size: bool,
/// Cursor position over the window.
cursor_position: Option<PhysicalPosition<f64>>,
/// Window modifiers state.
@ -666,6 +680,7 @@ impl WindowState {
theme,
animated_fill_color: false,
continuous_redraw: false,
emit_surface_size: false,
#[cfg(not(android_platform))]
start_time: Instant::now(),
cursor_position: Default::default(),
@ -738,6 +753,10 @@ impl WindowState {
self.window.set_fullscreen(fullscreen);
}
fn toggle_emit_surface_size(&mut self) {
self.emit_surface_size = !self.emit_surface_size;
}
/// Cycle through the grab modes ignoring errors.
fn cycle_cursor_grab(&mut self) {
self.cursor_grab = match self.cursor_grab {
@ -1031,6 +1050,7 @@ enum Action {
Message,
ToggleAnimatedFillColor,
ToggleContinuousRedraw,
EmitSurfaceSize,
}
impl Action {
@ -1076,6 +1096,7 @@ impl Action {
Action::Message => "Prints a message through a user wake up",
Action::ToggleAnimatedFillColor => "Toggle animated fill color",
Action::ToggleContinuousRedraw => "Toggle continuous redraw",
Action::EmitSurfaceSize => "Periodically print the surface size",
}
}
}
@ -1294,6 +1315,7 @@ const KEY_BINDINGS: &[Binding<&'static str>] = &[
Binding::new("T", ModifiersState::META, Action::CreateNewTab),
#[cfg(macos_platform)]
Binding::new("O", ModifiersState::CONTROL, Action::CycleOptionAsAlt),
Binding::new("S", ModifiersState::ALT, Action::EmitSurfaceSize),
Binding::new("S", ModifiersState::CONTROL, Action::Message),
];

View file

@ -268,6 +268,7 @@ changelog entry.
- On Windows, `Window::theme` will return the correct theme after setting it through `Window::set_theme`.
- On Windows, `Window::set_theme` will change the title bar color immediately now.
- On Windows 11, prevent incorrect shifting when dragging window onto a monitor with different DPI.
- On Windows, avoid returning `SurfaceResized` with size zero when an application is minimized. Let `Window::surface_size` return the pre-minimization window size even while minimized.
- On Web, device events are emitted regardless of cursor type.
- On Wayland, `axis_value120` scroll events now generate `MouseScrollDelta::LineDelta`
- On X11, mouse scroll button events no longer cause duplicated `WindowEvent::MouseWheel` events.