Skip to content

Commit

Permalink
Fixed window lazy loop when requesting frames
Browse files Browse the repository at this point in the history
  • Loading branch information
Nazariglez committed Jan 30, 2024
1 parent bb73b43 commit 36d6c3e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
37 changes: 27 additions & 10 deletions crates/notan_winit/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,6 @@ impl BackendSystem for WinitBackend {

// Await for the next event to run the loop again
let is_lazy = b.window.as_ref().map_or(false, |w| w.lazy);
if is_lazy {
*control_flow = ControlFlow::Wait;
}

match event {
WEvent::WindowEvent { ref event, .. } => {
Expand Down Expand Up @@ -259,6 +256,11 @@ impl BackendSystem for WinitBackend {
}
}
WEvent::RedrawRequested(_) => {
request_redraw = false;
if let Some(w) = &mut b.window {
w.frame_requested = false;
}

match cb(&mut app, &mut state) {
Ok(FrameState::End) => {
backend(&mut app.backend)
Expand All @@ -277,7 +279,11 @@ impl BackendSystem for WinitBackend {
}
}
WEvent::RedrawEventsCleared => {
request_redraw = false;
if let Some(w) = &mut b.window {
if w.frame_requested {
request_redraw = true;
}
}
}
WEvent::DeviceEvent { ref event, .. } => {
if let Some(evt) = mouse::process_device_events(event) {
Expand All @@ -287,13 +293,24 @@ impl BackendSystem for WinitBackend {
_ => {}
}

let b = backend(&mut app.backend);

// Close the loop if the user want to exit
let exit_requested = b.exit_requested;
if exit_requested {
*control_flow = ControlFlow::Exit;
}
*control_flow = {
let b = backend(&mut app.backend);
let exit_requested = b.exit_requested;
if exit_requested {
// Close the loop if the user want to exit
ControlFlow::Exit
} else if request_redraw {
// If something needs to be drawn keep polling events
ControlFlow::Poll
} else if is_lazy {
// If is in lazy mode and nothing needs to be drawn just wait
ControlFlow::Wait
} else {
// by default keep polling events
ControlFlow::Poll
}
};
});
}))
}
Expand Down
3 changes: 3 additions & 0 deletions crates/notan_winit/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct WinitWindowBackend {
mouse_passthrough: bool,
title: String,
use_touch_as_mouse: bool,
pub(crate) frame_requested: bool
}

impl WindowBackend for WinitWindowBackend {
Expand Down Expand Up @@ -73,6 +74,7 @@ impl WindowBackend for WinitWindowBackend {
fn request_frame(&mut self) {
if self.lazy {
self.window().request_redraw();
self.frame_requested = true;
}
}

Expand Down Expand Up @@ -363,6 +365,7 @@ impl WinitWindowBackend {
mouse_passthrough,
title,
use_touch_as_mouse: false,
frame_requested: false,
})
}

Expand Down

0 comments on commit 36d6c3e

Please sign in to comment.