From 3a246807758e434a432cc11d5a19c9f9a4e25fe7 Mon Sep 17 00:00:00 2001 From: Doublonmousse <115779707+Doublonmousse@users.noreply.github.com> Date: Thu, 11 Jul 2024 23:30:20 +0200 Subject: [PATCH] detect multiple touch inputs --- crates/rnote-ui/src/canvas/input.rs | 33 +++++++++++++++++++++++++---- crates/rnote-ui/src/canvas/mod.rs | 4 +++- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/crates/rnote-ui/src/canvas/input.rs b/crates/rnote-ui/src/canvas/input.rs index a0c96c9060..a14377057f 100644 --- a/crates/rnote-ui/src/canvas/input.rs +++ b/crates/rnote-ui/src/canvas/input.rs @@ -1,5 +1,6 @@ // Imports use super::RnCanvas; +use adw::glib::subclass::types::ObjectSubclassIsExt; use gtk4::{gdk, glib, graphene, prelude::*, Native}; use rnote_compose::penevent::{KeyboardKey, ModifierKey, PenEvent, PenState, ShortcutKey}; use rnote_compose::penpath::Element; @@ -28,7 +29,7 @@ pub(crate) fn handle_pointer_controller_event( //std::thread::sleep(std::time::Duration::from_millis(100)); //super::input::debug_gdk_event(event); - if reject_pointer_input(event, touch_drawing) { + if reject_pointer_input(event, touch_drawing, canvas) { return (glib::Propagation::Proceed, pen_state); } @@ -294,10 +295,34 @@ fn debug_gdk_event(event: &gdk::Event) { } /// Returns true if input should be rejected -fn reject_pointer_input(event: &gdk::Event, touch_drawing: bool) -> bool { +fn reject_pointer_input(event: &gdk::Event, touch_drawing: bool, canvas: &RnCanvas) -> bool { if touch_drawing { - if event.device().unwrap().num_touches() > 1 { - return true; + let sequence = event.event_sequence().as_ptr() as usize; + let event_type = event.event_type(); + let status_tid = canvas.imp().touch_id.get(); + + match event_type { + gdk::EventType::TouchBegin => { + if status_tid.is_none() { + canvas.imp().touch_id.set(Some(sequence)); + return false; + } else { + return true; + } + } + gdk::EventType::TouchUpdate => { + return Some(sequence) != status_tid; + // reject if the sequence is not the same as the one in store + } + gdk::EventType::TouchEnd | gdk::EventType::TouchCancel => { + if Some(sequence) == status_tid { + canvas.imp().touch_id.set(None); + return false; + } else { + return false; + } + } + _ => return false, } } else { let event_type = event.event_type(); diff --git a/crates/rnote-ui/src/canvas/mod.rs b/crates/rnote-ui/src/canvas/mod.rs index eb929a57d1..e2aad93529 100644 --- a/crates/rnote-ui/src/canvas/mod.rs +++ b/crates/rnote-ui/src/canvas/mod.rs @@ -15,7 +15,7 @@ use gettextrs::gettext; use gtk4::{ gdk, gio, glib, glib::clone, graphene, prelude::*, subclass::prelude::*, Adjustment, DropTarget, EventControllerKey, EventControllerLegacy, IMMulticontext, PropagationPhase, - Scrollable, ScrollablePolicy, Widget, + Scrollable, ScrollablePolicy, Widget }; use notify_debouncer_full::notify::{self, Watcher}; use once_cell::sync::Lazy; @@ -80,6 +80,7 @@ mod imp { pub(crate) unsaved_changes: Cell, pub(crate) empty: Cell, pub(crate) touch_drawing: Cell, + pub(crate) touch_id: Cell>, // we need to extract the memory value ... pub(crate) show_drawing_cursor: Cell, pub(crate) last_export_dir: RefCell>, @@ -174,6 +175,7 @@ mod imp { unsaved_changes: Cell::new(false), empty: Cell::new(true), touch_drawing: Cell::new(false), + touch_id: Cell::new(None), show_drawing_cursor: Cell::new(false), last_export_dir: RefCell::new(None),