feat(platform-winit): apply lifecycle state updates

This commit is contained in:
2026-06-25 07:49:19 +04:00
parent 95391a05c6
commit ad21704bcc
@@ -265,6 +265,41 @@ impl WinitWindow {
pub const fn default_render_request() -> RenderRequest { pub const fn default_render_request() -> RenderRequest {
RenderRequest::conservative() RenderRequest::conservative()
} }
/// Applies one platform event to the cached window descriptor state.
pub fn apply_event(&mut self, event: &PlatformEvent) {
match event {
PlatformEvent::Resize { width, height } => {
self.width = *width;
self.height = *height;
}
PlatformEvent::DpiChanged { scale } => {
self.scale = *scale;
}
PlatformEvent::FocusChanged { focused } => {
self.focused = *focused;
}
PlatformEvent::Minimized { minimized } => {
self.minimized = *minimized;
}
PlatformEvent::Occluded { occluded } => {
self.occluded = *occluded;
}
PlatformEvent::Suspended
| PlatformEvent::Resumed
| PlatformEvent::QuitRequested
| PlatformEvent::KeyboardInput { .. }
| PlatformEvent::MouseInput { .. }
| PlatformEvent::CursorMoved { .. } => {}
}
}
/// Applies a sequence of platform events to the cached window descriptor state.
pub fn apply_events<'a>(&mut self, events: impl IntoIterator<Item = &'a PlatformEvent>) {
for event in events {
self.apply_event(event);
}
}
} }
impl WindowPort for WinitWindow { impl WindowPort for WinitWindow {
@@ -464,6 +499,35 @@ mod tests {
assert!(events.contains(&PlatformEvent::Minimized { minimized: false })); assert!(events.contains(&PlatformEvent::Minimized { minimized: false }));
Ok(()) Ok(())
} }
#[test]
fn window_descriptor_applies_lifecycle_and_resize_events() {
let mut window = WinitWindow::synthetic(640, 360);
let events = [
PlatformEvent::Resize {
width: 1280,
height: 720,
},
PlatformEvent::DpiChanged { scale: 2.0 },
PlatformEvent::FocusChanged { focused: false },
PlatformEvent::Minimized { minimized: true },
PlatformEvent::Occluded { occluded: true },
];
window.apply_events(events.iter());
assert_eq!(
window.drawable_size(),
PhysicalSize {
width: 1280,
height: 720,
}
);
assert_eq!(window.dpi_scale(), 2.0);
assert!(!window.has_focus());
assert!(window.is_minimized());
assert!(window.is_occluded());
}
} }
// SAFETY: no unsafe usage in this crate. // SAFETY: no unsafe usage in this crate.