ferroui_base/rendering/i_render_timer.rs
1use std::sync::Arc;
2use std::time::Duration;
3
4/// The callback of an [`IRenderTimer`]. The argument is the timestamp of the
5/// tick on the timer's monotonic clock.
6pub type RenderTimerTick = Arc<dyn Fn(Duration) + Send + Sync>;
7
8/// Defines the interface implemented by an application render timer.
9pub trait IRenderTimer: Send + Sync {
10 /// Gets the callback to be invoked when the timer ticks.
11 fn tick(&self) -> Option<RenderTimerTick>;
12
13 /// Sets the callback to be invoked when the timer ticks.
14 ///
15 /// This can be called from any thread, but it's guaranteed that it's not
16 /// called concurrently (i. e. the render loop always does it under a
17 /// lock). Setting the value to `None` suggests the timer to stop ticking,
18 /// however the timer is allowed to produce ticks on the previously set
19 /// value as long as it stops doing so.
20 ///
21 /// The callback can be invoked on any thread. Implementations must not
22 /// invoke it synchronously from this method: the render loop calls it
23 /// while holding a lock that the callback takes.
24 fn set_tick(&self, value: Option<RenderTimerTick>);
25
26 /// Indicates if the timer ticks on a non-UI thread.
27 fn runs_in_background(&self) -> bool;
28
29 /// Asks for a tick out of turn: as soon as the thread that ticks can
30 /// produce one, without waiting for the next period of the timer (the
31 /// next frame of a display). Not from upstream.
32 ///
33 /// Called by a thread that is about to block until a frame has been
34 /// rendered. A timer whose next tick may be far away, or may depend on
35 /// the caller returning to its event loop, invokes the callback it was
36 /// given once on the thread it ticks on; it must not invoke it on the
37 /// calling thread, and does nothing while no callback is set. The
38 /// default does nothing: the caller then waits for the next tick.
39 fn request_tick_out_of_turn(&self) {}
40}