use std::time::Instant;
use winit::event_loop::ControlFlow;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(super) struct LoopNeeds {
pub(super) continuous_video: bool,
pub(super) redraw_pending: bool,
pub(super) polling: bool,
pub(super) next_redraw: Option<Instant>,
pub(super) next_timer: Option<Instant>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum LoopWait {
Poll,
WaitUntil(Instant),
Wait,
}
impl LoopWait {
pub(super) fn of(needs: LoopNeeds, now: Instant) -> Self {
if needs.continuous_video {
return Self::Poll;
}
if needs.redraw_pending {
let next_redraw = needs.next_redraw.filter(|redraw| *redraw > now);
return Self::until(next_redraw.into_iter().chain(needs.next_timer).min());
}
if needs.polling {
return Self::Poll;
}
Self::until(needs.next_redraw.into_iter().chain(needs.next_timer).min())
}
fn until(deadline: Option<Instant>) -> Self {
deadline.map_or(Self::Wait, Self::WaitUntil)
}
pub(super) fn control_flow(self) -> ControlFlow {
match self {
Self::Poll => ControlFlow::Poll,
Self::WaitUntil(deadline) => ControlFlow::WaitUntil(deadline),
Self::Wait => ControlFlow::Wait,
}
}
pub(super) fn name(self) -> &'static str {
match self {
Self::Poll => "poll",
Self::WaitUntil(_) => "wait_until",
Self::Wait => "wait",
}
}
}