Skip to content

Commit e3d086e

Browse files
committed
examples/winit: Implement proper Event::Resumed semantics
On Android the backing buffer (`NativeWindow`) disappears when the application is not focussed and/or the screen is locked. Winit handles this by requiring apps to create their `raw_window_handle()` consumers _after_ `Event::Resumed` and to clean it up _before_ returning from `Event::Suspended`. For consistency Winit also sends `Resumed` on all other platforms during init.
1 parent c2ec494 commit e3d086e

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

examples/winit.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,29 @@ fn main() {
2222
.unwrap();
2323
}
2424

25-
let context = softbuffer::Context::new(window.clone()).unwrap();
26-
let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap();
25+
let mut state = None;
2726

2827
event_loop
2928
.run(move |event, elwt| {
3029
elwt.set_control_flow(ControlFlow::Wait);
3130

3231
match event {
32+
Event::Resumed => {
33+
let context = softbuffer::Context::new(window.clone()).unwrap();
34+
let surface = softbuffer::Surface::new(&context, window.clone()).unwrap();
35+
state = Some((context, surface));
36+
}
37+
Event::Suspended => {
38+
state = None;
39+
}
3340
Event::WindowEvent {
3441
window_id,
3542
event: WindowEvent::RedrawRequested,
3643
} if window_id == window.id() => {
44+
let Some((_, surface)) = state.as_mut() else {
45+
eprintln!("RedrawRequested fired before Resumed or after Suspended");
46+
return;
47+
};
3748
if let (Some(width), Some(height)) = {
3849
let size = window.inner_size();
3950
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))

0 commit comments

Comments
 (0)