use winit::{event::WindowEvent, window::Window};
use super::frame_pacing::{FramePacing, PresentedFrame};
use crate::{app::Tree, time::Instant};
#[derive(Default)]
pub(super) struct FrameSource {
pacing: FramePacing,
pending: bool,
#[cfg(target_os = "ios")]
link: Option<super::ios::FrameLink>,
#[cfg(target_os = "ios")]
idle_since: Option<Instant>,
}
#[cfg(target_os = "ios")]
const IDLE_REFRESHES: std::time::Duration = std::time::Duration::from_millis(500);
impl FrameSource {
#[cfg(target_os = "ios")]
pub(super) fn attach(&mut self) {
self.link = Some(super::ios::FrameLink::new());
}
pub(super) fn handle_event(&mut self, event: &WindowEvent) {
self.pacing.handle_event(event);
}
pub(super) fn request(&mut self, window: &Window) {
if self.pending {
return;
}
self.pending = true;
#[cfg(target_os = "ios")]
if let Some(link) = &mut self.link {
link.set_running(true);
return;
}
window.request_redraw();
}
pub(super) fn request_timeline_frame(&mut self, tree: &mut Tree, window: Option<&Window>, now: Instant) {
#[cfg(target_os = "ios")]
if self.link.is_some() {
self.pacing.request_frame(tree, || std::time::Duration::ZERO, now);
return;
}
self
.pacing
.request_frame(tree, || super::frame_pacing::refresh_interval(window), now);
}
pub(super) fn take_due(&mut self, wanted: bool) -> bool {
#[cfg(target_os = "ios")]
if let Some(link) = &mut self.link {
let wanted = self.pending || wanted;
if !wanted {
let now = Instant::now();
if now.duration_since(*self.idle_since.get_or_insert(now)) >= IDLE_REFRESHES {
link.set_running(false);
self.idle_since = None;
}
return false;
}
self.idle_since = None;
return link.take_due();
}
let _ = wanted;
false
}
#[cfg(feature = "perf_profile")]
pub(super) fn refresh_interval(&mut self, window: Option<&Window>, now: Instant) -> std::time::Duration {
self
.pacing
.refresh_interval(|| super::frame_pacing::refresh_interval(window), now)
}
pub(super) fn pending(&self) -> bool {
self.pending
}
pub(super) fn settle(&mut self) {
self.pending = false;
}
pub(super) fn presented(&mut self, frame: PresentedFrame) {
self.pacing.presented(frame);
}
}