Skip to content

Commit

Permalink
examples/winit: Implement proper Event::Resumed semantics
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MarijnS95 committed Nov 6, 2023
1 parent c2ec494 commit e3d086e
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions examples/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,29 @@ fn main() {
.unwrap();
}

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

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

match event {
Event::Resumed => {
let context = softbuffer::Context::new(window.clone()).unwrap();
let surface = softbuffer::Surface::new(&context, window.clone()).unwrap();
state = Some((context, surface));
}
Event::Suspended => {
state = None;
}
Event::WindowEvent {
window_id,
event: WindowEvent::RedrawRequested,
} if window_id == window.id() => {
let Some((_, surface)) = state.as_mut() else {
eprintln!("RedrawRequested fired before Resumed or after Suspended");
return;
};
if let (Some(width), Some(height)) = {
let size = window.inner_size();
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
Expand Down

0 comments on commit e3d086e

Please sign in to comment.