Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mapping between gtk timestamp and instant #1290

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions crates/rnote-ui/src/canvas/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down
60 changes: 58 additions & 2 deletions crates/rnote-ui/src/canvas/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -53,6 +53,8 @@ struct Connections {
}

mod imp {
use std::time::Instant;

use super::*;

#[derive(Debug)]
Expand Down Expand Up @@ -87,6 +89,11 @@ mod imp {
pub(crate) show_drawing_cursor: Cell<bool>,

pub(crate) last_export_dir: RefCell<Option<gio::File>>,

// mapping from gtk timestamp to instant
pub mapping_time: OnceCell<Instant>,
pub last_gtk_time: RefCell<u32>,
pub overflow_duration: RefCell<Duration>,
}

impl Default for RnCanvas {
Expand Down Expand Up @@ -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()),
}
}
}
Expand Down Expand Up @@ -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;
}
}
}