use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::{Duration, Instant};
use crate::component::ComponentId;
use crate::event::{EventArgs, PointerEvent};
use crate::timing::{self, TimerPrecision};
use crate::ui::Ui;
pub const LONG_PRESS_TIME: f64 = 0.5;
pub(crate) struct Timeout {
pub(crate) id: u64,
pub(crate) due: f64,
pub(crate) job: Box<dyn FnOnce(&mut Ui)>,
}
pub(crate) struct TimerEntry {
pub(crate) id: ComponentId,
pub(crate) interval: f64,
pub(crate) next: f64,
pub(crate) precision: TimerPrecision,
pub(crate) thread: Option<(Arc<AtomicBool>, std::thread::Thread)>,
}
impl TimerEntry {
fn start(ui: &Ui, id: ComponentId, interval: f64, precision: TimerPrecision, background: bool) -> Self {
if precision != TimerPrecision::Normal {
timing::fine_clock(true);
}
let thread = background.then(|| {
let stop = Arc::new(AtomicBool::new(false));
let poster = ui.poster();
let flag = stop.clone();
let period = Duration::from_secs_f64(interval);
let spawned = std::thread::Builder::new().name("aylis-timer".into()).spawn(move || {
let mut next = Instant::now() + period;
loop {
if !timing::park_until(next, precision, &flag) {
break;
}
let now = Instant::now();
let late = now.saturating_duration_since(next).as_secs_f64();
if Arc::strong_count(&poster.0) == 1 {
break;
}
let f = flag.clone();
poster.post(move |ui| {
if !f.load(Ordering::Relaxed) {
ui.fire_timer(id, late);
}
});
next += period;
if next <= now {
next = now + period;
}
}
});
match spawned {
Ok(handle) => (stop, handle.thread().clone()),
Err(_) => (stop, std::thread::current()),
}
});
let next = if thread.is_some() { f64::INFINITY } else { ui.time + interval };
Self { id, interval, next, precision, thread }
}
pub(crate) fn stop(self) {
if self.precision != TimerPrecision::Normal {
timing::fine_clock(false);
}
if let Some((flag, thread)) = self.thread {
flag.store(true, Ordering::Relaxed);
thread.unpark();
}
}
}
impl Ui {
pub fn sync_timer(&mut self, id: ComponentId) {
let wanted = self.component(id).and_then(|c| {
let interval = c.timer_interval().filter(|i| *i > 0.0)?;
Some((interval, c.timer_precision(), c.timer_background()))
});
let position = self.timers.iter().position(|t| t.id == id);
match (position, wanted) {
(Some(i), Some((interval, precision, background))) => {
let t = &self.timers[i];
if (t.interval - interval).abs() > f64::EPSILON || t.precision != precision || t.thread.is_some() != background {
self.timers.remove(i).stop();
let entry = TimerEntry::start(self, id, interval, precision, background);
self.timers.push(entry);
}
}
(Some(i), None) => self.timers.remove(i).stop(),
(None, Some((interval, precision, background))) => {
let entry = TimerEntry::start(self, id, interval, precision, background);
self.timers.push(entry);
}
(None, None) => {}
}
}
pub fn restart_timer(&mut self, id: ComponentId) {
if let Some(i) = self.timers.iter().position(|t| t.id == id) {
self.timers.remove(i).stop();
}
self.sync_timer(id);
}
fn fire_timer(&mut self, id: ComponentId, late: f64) {
let _profile = crate::profile::scope(crate::profile::Lane::Timer, || self.path_of(id));
let _ = self.with_component(id, |c, ctx| {
c.timer_late(late);
c.timer(ctx);
});
self.sync_timer(id);
}
pub fn exact_timer_due(&self) -> Option<f64> {
self.timers.iter().filter(|t| t.precision == TimerPrecision::Exact && t.thread.is_none()).map(|t| t.next).reduce(f64::min)
}
pub fn set_timeout(&mut self, ms: u64, job: impl FnOnce(&mut Ui) + 'static) -> u64 {
self.next_timeout += 1;
let id = self.next_timeout;
self.timeouts.push(Timeout { id, due: self.time + ms as f64 / 1000.0, job: Box::new(job) });
self.request_repaint();
id
}
pub fn clear_timeout(&mut self, id: u64) {
self.timeouts.retain(|t| t.id != id);
}
pub fn run_timers(&mut self) -> bool {
if self.design_mode {
return false;
}
let now = self.time;
let mut due_now: Vec<(f64, u64)> = self.timeouts.iter().filter(|t| t.due <= now).map(|t| (t.due, t.id)).collect();
due_now.sort_by(|a, b| a.0.total_cmp(&b.0));
let ran_timeout = !due_now.is_empty();
for (_, id) in due_now {
if let Some(i) = self.timeouts.iter().position(|t| t.id == id) {
let t = self.timeouts.remove(i);
(t.job)(self);
}
}
if ran_timeout {
self.process_events();
}
let due: Vec<(ComponentId, f64)> = self.timers.iter().filter(|t| t.next <= now).map(|t| (t.id, now - t.next)).collect();
for t in self.timers.iter_mut().filter(|t| t.next <= now) {
t.next = if now - t.next > t.interval { now + t.interval } else { t.next + t.interval };
}
for &(id, late) in &due {
self.fire_timer(id, late);
}
let pressed = self.check_long_press();
let hint = self.check_hints();
ran_timeout || !due.is_empty() || pressed || hint
}
fn check_hints(&mut self) -> bool {
let now = self.time;
let delay = self.hint_delay();
let mut any = false;
for state in self.input.values_mut() {
if let Some(h) = state.hint.as_mut().filter(|h| !h.shown && now >= h.since + delay) {
h.shown = true;
any = true;
}
}
if any {
self.request_repaint();
}
any
}
pub(crate) fn timers_wake(&self) -> Option<f64> {
let timer = self.timers.iter().map(|t| t.next).filter(|n| n.is_finite()).chain(self.timeouts.iter().map(|t| t.due)).reduce(f64::min);
let delay = self.hint_delay();
let press = self
.input
.values()
.flat_map(|s| [s.long_press_due(), s.hint_due(delay)])
.flatten()
.reduce(f64::min);
match (timer, press) {
(Some(a), Some(b)) => Some(a.min(b)),
(a, b) => a.or(b),
}
}
fn check_long_press(&mut self) -> bool {
let now = self.time;
let roots: Vec<ComponentId> = self.input.keys().copied().collect();
let mut any = false;
for root in roots {
let Some((target, position, touch)) = self.input.get_mut(&root).and_then(|s| s.take_long_press(now)) else {
continue;
};
if !touch || !self.contains(target) {
continue;
}
any = true;
let local = self.local_point(target, position);
let event = PointerEvent { position: local, kind: crate::event::PointerKind::Touch, ..Default::default() };
self.fire(target, "on_long_press", &EventArgs::Pointer(event));
if self.contains(target) && self.open_context_menu(target, position, true) {
self.mark_press_menu(root, target);
}
self.request_repaint();
}
any
}
}