Skip to main content

cranpose_app_shell/
pointer_event_time.rs

1use cranpose_foundation::PointerEvent;
2use cranpose_render_common::Renderer;
3use web_time::Instant;
4
5use crate::{AppShell, ShellApp};
6
7#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8/// Platform and animation-clock timestamps for one pointer sample.
9pub struct PointerEventTime {
10    /// Timestamp supplied by the platform, in its millisecond clock domain.
11    pub platform_time_ms: Option<i64>,
12    /// Timestamp in the animation frame-clock domain.
13    pub animation_time_nanos: u64,
14    /// The instant at which the shell received the sample.
15    pub received_at: Instant,
16}
17
18impl PointerEventTime {
19    pub(crate) fn stamp(self, event: PointerEvent) -> PointerEvent {
20        event
21            .with_time_ms(self.platform_time_ms)
22            .with_animation_time_nanos(self.animation_time_nanos)
23            .with_received_at(self.received_at)
24    }
25}
26
27impl ShellApp {
28    pub(crate) fn realtime_pointer_event_time(
29        &self,
30        platform_time_ms: Option<i64>,
31    ) -> PointerEventTime {
32        let received_at = Instant::now();
33        PointerEventTime {
34            platform_time_ms,
35            animation_time_nanos: self
36                .frame_time_nanos_at(received_at)
37                .max(self.last_frame_time_nanos),
38            received_at,
39        }
40    }
41}
42
43impl<R> AppShell<R>
44where
45    R: Renderer,
46    R::Error: std::fmt::Debug,
47{
48    /// Timestamp a live input sample against the current animation clock.
49    pub fn realtime_pointer_event_time(&self, platform_time_ms: Option<i64>) -> PointerEventTime {
50        self.app.realtime_pointer_event_time(platform_time_ms)
51    }
52
53    /// Timestamp deterministic input at the most recently processed frame. The sample counts as
54    /// received at the time of the call.
55    pub fn exact_pointer_event_time(&self, platform_time_ms: Option<i64>) -> PointerEventTime {
56        PointerEventTime {
57            platform_time_ms,
58            animation_time_nanos: self.app.last_frame_time_nanos,
59            received_at: Instant::now(),
60        }
61    }
62}