From f8fc12250edc216e43e6668cf48073227fea3be9 Mon Sep 17 00:00:00 2001 From: Doublonmousse <115779707+Doublonmousse@users.noreply.github.com> Date: Tue, 12 Nov 2024 20:07:02 +0100 Subject: [PATCH] mapping between gtk timestamp and instant --- crates/rnote-ui/src/canvas/input.rs | 6 +-- crates/rnote-ui/src/canvas/mod.rs | 60 ++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/crates/rnote-ui/src/canvas/input.rs b/crates/rnote-ui/src/canvas/input.rs index 8775ec852b..85a90e9f0b 100644 --- a/crates/rnote-ui/src/canvas/input.rs +++ b/crates/rnote-ui/src/canvas/input.rs @@ -18,7 +18,7 @@ pub(crate) fn handle_pointer_controller_event( event: &gdk::Event, mut pen_state: PenState, ) -> (glib::Propagation, PenState) { - let now = Instant::now(); + let now = canvas.get_time(event.time()); let mut widget_flags = WidgetFlags::default(); let touch_drawing = canvas.touch_drawing(); let gdk_event_type = event.event_type(); @@ -370,9 +370,7 @@ fn retrieve_pointer_elements( } let entry_delta = Duration::from_millis(event_time.saturating_sub(entry.time()) as u64); - let Some(entry_time) = now.checked_sub(entry_delta) else { - continue; - }; + let entry_time = canvas.get_time(event_time); if let BacklogPolicy::Limit(delta_limit) = backlog_policy { // We go back in time, so `entry_delta` will increase diff --git a/crates/rnote-ui/src/canvas/mod.rs b/crates/rnote-ui/src/canvas/mod.rs index 11bee39b2b..f00243fb54 100644 --- a/crates/rnote-ui/src/canvas/mod.rs +++ b/crates/rnote-ui/src/canvas/mod.rs @@ -28,9 +28,9 @@ use rnote_engine::ext::GraphenePointExt; use rnote_engine::ext::GrapheneRectExt; use rnote_engine::Camera; use rnote_engine::{Engine, WidgetFlags}; -use std::cell::{Cell, Ref, RefCell, RefMut}; +use std::cell::{Cell, OnceCell, Ref, RefCell, RefMut}; use std::path::Path; -use std::time::Duration; +use std::time::{Duration, Instant}; use tracing::{debug, error, warn}; #[derive(Debug, Default)] @@ -53,6 +53,8 @@ struct Connections { } mod imp { + use std::time::Instant; + use super::*; #[derive(Debug)] @@ -87,6 +89,11 @@ mod imp { pub(crate) show_drawing_cursor: Cell, pub(crate) last_export_dir: RefCell>, + + // mapping from gtk timestamp to instant + pub mapping_time: OnceCell, + pub last_gtk_time: RefCell, + pub overflow_duration: RefCell, } impl Default for RnCanvas { @@ -181,6 +188,10 @@ mod imp { show_drawing_cursor: Cell::new(false), last_export_dir: RefCell::new(None), + + mapping_time: OnceCell::new(), + last_gtk_time: RefCell::new(0), + overflow_duration: RefCell::new(Duration::default()), } } } @@ -1479,4 +1490,49 @@ impl RnCanvas { na::point![f64::from(self.width()), f64::from(self.height())], ) } + + pub(crate) fn get_time(&self, time_gtk: u32) -> Instant { + // this is built so that we get the time by the mapping_time + + // time_gtk*1e-3 seconds + // time_gtk cannot be 0, 0 is the absence of timestamp + if self.imp().mapping_time.get().is_none() { + let _ = self.imp().mapping_time.set( + Instant::now() + .checked_sub(Duration::from_millis(time_gtk as u64)) + .unwrap_or(Instant::now()), + ); + self.imp().last_gtk_time.replace(time_gtk); + return Instant::now(); + } else { + let overflow = time_gtk + .checked_sub(*self.imp().last_gtk_time.borrow()) + .is_none() + && (time_gtk != 0); + if overflow { + let _ = self.imp().overflow_duration.replace_with(|&mut old| { + old.checked_add(Duration::from_millis(u32::MAX as u64)) + .unwrap() + }); + } + let time_out = self + .imp() + .mapping_time + .get() + .unwrap() + .checked_add( + self.imp() + .overflow_duration + .borrow() + .checked_add(Duration::from_millis(if time_gtk == 0 { + *self.imp().last_gtk_time.borrow() + } else { + time_gtk + } as u64)) + .unwrap(), + ) + .unwrap(); + self.imp().last_gtk_time.replace(time_gtk); + return time_out; + } + } }