Skip to content

Commit

Permalink
Stop polling in event loop on RedrawRequest::NextFrame
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Dec 20, 2023
1 parent e772e5a commit 50a7852
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 13 deletions.
6 changes: 1 addition & 5 deletions examples/loading_spinners/src/circular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,6 @@ where
shell: &mut Shell<'_, Message>,
_viewport: &Rectangle,
) -> event::Status {
const FRAME_RATE: u64 = 60;

let state = tree.state.downcast_mut::<State>();

if let Event::Window(_, window::Event::RedrawRequested(now)) = event {
Expand All @@ -287,9 +285,7 @@ where
);

state.cache.clear();
shell.request_redraw(RedrawRequest::At(
now + Duration::from_millis(1000 / FRAME_RATE),
));
shell.request_redraw(RedrawRequest::NextFrame);
}

event::Status::Ignored
Expand Down
6 changes: 1 addition & 5 deletions examples/loading_spinners/src/linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,16 +196,12 @@ where
shell: &mut Shell<'_, Message>,
_viewport: &Rectangle,
) -> event::Status {
const FRAME_RATE: u64 = 60;

let state = tree.state.downcast_mut::<State>();

if let Event::Window(_, window::Event::RedrawRequested(now)) = event {
*state = state.timed_transition(self.cycle_duration, now);

shell.request_redraw(RedrawRequest::At(
now + Duration::from_millis(1000 / FRAME_RATE),
));
shell.request_redraw(RedrawRequest::NextFrame);
}

event::Status::Ignored
Expand Down
9 changes: 6 additions & 3 deletions winit/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ async fn run_instance<A, E, C>(
match event {
event::Event::NewEvents(
event::StartCause::Init
| event::StartCause::Poll
| event::StartCause::ResumeTimeReached { .. },
) if !redraw_pending => {
window.request_redraw();
Expand Down Expand Up @@ -387,7 +386,11 @@ async fn run_instance<A, E, C>(
user_interface::State::Updated {
redraw_request: Some(redraw_request),
} => match redraw_request {
window::RedrawRequest::NextFrame => ControlFlow::Poll,
window::RedrawRequest::NextFrame => {
window.request_redraw();

ControlFlow::Wait
}
window::RedrawRequest::At(at) => {
ControlFlow::WaitUntil(at)
}
Expand Down Expand Up @@ -469,7 +472,7 @@ async fn run_instance<A, E, C>(
_ => {}
}

if !redraw_pending && events.is_empty() && messages.is_empty() {
if events.is_empty() && messages.is_empty() {
continue;
}

Expand Down

0 comments on commit 50a7852

Please sign in to comment.