Skip to content

Commit 2608734

Browse files
committed
desktop: Clean up the ControlFlow usage
1 parent b93cd2c commit 2608734

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

desktop/src/app.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use ruffle_render::backend::ViewportDimensions;
1313
use std::cell::RefCell;
1414
use std::rc::Rc;
1515
use std::sync::Arc;
16-
use std::time::{Duration, Instant};
16+
use std::time::Instant;
1717
use url::Url;
1818
use winit::application::ApplicationHandler;
1919
use winit::dpi::{LogicalSize, PhysicalPosition, PhysicalSize, Size};
@@ -563,18 +563,23 @@ impl ApplicationHandler<RuffleEvent> for App {
563563
self.next_frame_time = None;
564564
}
565565
self.check_redraw();
566-
// After polling events, sleep the event loop until the next event or the next frame.
567-
event_loop.set_control_flow(if matches!(self.loaded, LoadingState::Loaded) {
568-
if let Some(next_frame_time) = self.next_frame_time {
569-
ControlFlow::WaitUntil(next_frame_time)
570-
} else {
571-
// prevent 100% cpu use
572-
// TODO: use set_request_repaint_callback to correctly get egui repaint requests.
573-
ControlFlow::WaitUntil(Instant::now() + Duration::from_millis(10))
574-
}
575-
} else {
576-
ControlFlow::Wait
577-
});
566+
}
567+
}
568+
569+
// The event loop is finished; let's find out how long we need to wait for
570+
// (but don't change something that's already requesting a sooner update, or we'll delay it)
571+
if let Some(next_frame_time) = self.next_frame_time {
572+
match event_loop.control_flow() {
573+
// A "Wait" has no time limit, set ours
574+
ControlFlow::Wait => {
575+
event_loop.set_control_flow(ControlFlow::WaitUntil(next_frame_time))
576+
}
577+
// If the existing "WaitUntil" is later than ours, update it
578+
ControlFlow::WaitUntil(next) if next > next_frame_time => {
579+
event_loop.set_control_flow(ControlFlow::WaitUntil(next_frame_time));
580+
}
581+
// It's sooner than ours, don't delay it
582+
_ => {}
578583
}
579584
}
580585
}

0 commit comments

Comments
 (0)