Skip to content

Commit

Permalink
Move out of rnote-engine & use gdk::Device::name for comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
cyclane committed May 17, 2023
1 parent 38cf0a7 commit 6448e34
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 66 deletions.
41 changes: 6 additions & 35 deletions rnote-engine/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{render, AudioPlayer, WidgetFlags};
use crate::{Camera, Document, PenHolder, StrokeStore};
use anyhow::Context;
use futures::channel::{mpsc, oneshot};
use gtk4::{gdk, gsk};
use gtk4::gsk;
use p2d::bounding_volume::{Aabb, BoundingVolume};
use rnote_compose::helpers::AabbHelpers;
use rnote_compose::penevents::{PenEvent, ShortcutKey};
Expand Down Expand Up @@ -343,10 +343,6 @@ pub struct RnoteEngine {
pub background_tile_image: Option<render::Image>,
#[serde(skip)]
background_rendernodes: Vec<gsk::RenderNode>,
#[serde(skip)]
zooming: bool,
#[serde(skip)]
pub zooming_ended: Option<Instant>,
}

impl Default for RnoteEngine {
Expand All @@ -369,8 +365,6 @@ impl Default for RnoteEngine {
tasks_tx,
background_tile_image: None,
background_rendernodes: Vec::default(),
zooming: false,
zooming_ended: None,
}
}
}
Expand Down Expand Up @@ -419,6 +413,11 @@ impl RnoteEngine {
self.pen_sounds
}

/// whether pen is idle
pub fn pen_idle(&self) -> bool {
self.penholder.current_pen_progress() == PenProgress::Idle
}

/// Enables/disables the pen sounds.
/// If pen sound should be enabled, the pkg data dir must be provided.
pub fn set_pen_sounds(&mut self, pen_sounds: bool, pkg_data_dir: Option<PathBuf>) {
Expand Down Expand Up @@ -633,43 +632,15 @@ impl RnoteEngine {
(widget_flags, quit)
}

/// Handle zooming gesture
pub fn handle_zooming(
&mut self,
zooming: bool,
device: Option<gdk::Device>,
now: Instant,
) -> Option<WidgetFlags> {
self.zooming = zooming;
if !zooming {
self.zooming_ended = Some(now);
None
} else if zooming && self.penholder.current_pen_progress() != PenProgress::Idle {
if self.penholder.pen_device == device {
Some(self.undo(now))
} else {
Some(self.reinstall_pen_current_style())
}
} else {
None
}
}

/// Handle a pen event.
pub fn handle_pen_event(
&mut self,
event: PenEvent,
device: Option<gdk::Device>,
pen_mode: Option<PenMode>,
now: Instant,
) -> WidgetFlags {
// Prevents end of zooming gesture from drawing a line
if self.zooming {
return WidgetFlags::default();
}
self.penholder.handle_pen_event(
event,
device,
pen_mode,
now,
&mut EngineViewMut {
Expand Down
11 changes: 0 additions & 11 deletions rnote-engine/src/pens/penholder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::engine::{EngineView, EngineViewMut};
use crate::pens::shortcuts::ShortcutAction;
use crate::widgetflags::WidgetFlags;
use crate::DrawOnDocBehaviour;
use gtk4::gdk;
use p2d::bounding_volume::Aabb;
use piet::RenderContext;
use rnote_compose::penevents::{PenEvent, ShortcutKey};
Expand Down Expand Up @@ -41,8 +40,6 @@ pub struct PenHolder {
#[serde(skip)]
pen_progress: PenProgress,
#[serde(skip)]
pub pen_device: Option<gdk::Device>,
#[serde(skip)]
toggle_pen_style: Option<PenStyle>,
#[serde(skip)]
prev_shortcut_key: Option<ShortcutKey>,
Expand All @@ -57,7 +54,6 @@ impl Default for PenHolder {

current_pen: Pen::default(),
pen_progress: PenProgress::Idle,
pen_device: None,
toggle_pen_style: None,
prev_shortcut_key: None,
}
Expand Down Expand Up @@ -222,19 +218,12 @@ impl PenHolder {
pub fn handle_pen_event(
&mut self,
event: PenEvent,
device: Option<gdk::Device>,
pen_mode: Option<PenMode>,
now: Instant,
engine_view: &mut EngineViewMut,
) -> WidgetFlags {
let mut widget_flags = WidgetFlags::default();

if self.pen_progress != PenProgress::InProgress {
self.pen_device = device;
} else if self.pen_device != device {
return widget_flags;
}

if let Some(pen_mode) = pen_mode {
widget_flags.merge(self.change_pen_mode(pen_mode, engine_view));
}
Expand Down
24 changes: 14 additions & 10 deletions rnote-ui/src/canvas/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,14 @@ pub(crate) fn handle_pointer_controller_event(
//super::input::debug_gdk_event(event);

let zooming_ended_elapsed = canvas
.engine()
.borrow()
.zooming_ended
.zooming_ended()
.and_then(|zooming_ended| Some(now - zooming_ended));
if reject_pointer_input(event, touch_drawing, zooming_ended_elapsed) {
if canvas.zooming() || reject_pointer_input(event, state, canvas.pen_device_eq(event.device()), touch_drawing, zooming_ended_elapsed) {
return (Inhibit(false), state);
}

if zooming_ended_elapsed.is_some() && gdk_event_type == gdk::EventType::TouchBegin {
canvas.engine().borrow_mut().zooming_ended = None;
canvas.set_zooming_ended(None)
}

let mut handle_pen_event = false;
Expand Down Expand Up @@ -191,7 +189,6 @@ pub(crate) fn handle_pointer_controller_event(
element,
modifier_keys: modifier_keys.clone(),
},
event.device(),
pen_mode,
event_time,
));
Expand All @@ -204,7 +201,6 @@ pub(crate) fn handle_pointer_controller_event(
element,
modifier_keys: modifier_keys.clone(),
},
event.device(),
pen_mode,
event_time,
));
Expand All @@ -218,7 +214,6 @@ pub(crate) fn handle_pointer_controller_event(
element,
modifier_keys: modifier_keys.clone(),
},
event.device(),
pen_mode,
event_time,
));
Expand All @@ -227,6 +222,12 @@ pub(crate) fn handle_pointer_controller_event(
}
}

if matches!(state, PenState::Down) {
canvas.set_pen_device(event.device());
} else {
canvas.set_pen_device(None);
}

canvas.emit_handle_widget_flags(widget_flags);
(Inhibit(inhibit), state)
}
Expand All @@ -249,7 +250,6 @@ pub(crate) fn handle_key_controller_key_pressed(
modifier_keys,
},
None,
None,
now,
);
canvas.emit_handle_widget_flags(widget_flags);
Expand All @@ -264,7 +264,6 @@ pub(crate) fn handle_imcontext_text_commit(canvas: &RnCanvas, text: &str) {
text: text.to_string(),
},
None,
None,
now,
);
canvas.emit_handle_widget_flags(widget_flags);
Expand All @@ -289,9 +288,14 @@ fn debug_gdk_event(event: &gdk::Event) {
/// Returns true if input should be rejected
fn reject_pointer_input(
event: &gdk::Event,
state: PenState,
device_matches: bool,
touch_drawing: bool,
zooming_ended_elapsed: Option<Duration>,
) -> bool {
if matches!(state, PenState::Down) && !device_matches {
return true;
}
if touch_drawing {
if event.device().unwrap().num_touches() > 1 {
return true;
Expand Down
46 changes: 45 additions & 1 deletion rnote-ui/src/canvas/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use rnote_engine::Document;
use rnote_engine::{RnoteEngine, WidgetFlags};
use std::cell::{Cell, RefCell};
use std::rc::Rc;
use std::time::Duration;
use std::time::{Duration, Instant};

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, glib::Boxed)]
#[boxed_type(name = "WidgetFlagsBoxed")]
Expand Down Expand Up @@ -69,6 +69,9 @@ mod imp {
pub(crate) key_controller_im_context: IMMulticontext,
pub(crate) drop_target: DropTarget,
pub(crate) drawing_cursor_enabled: Cell<bool>,
pub(crate) pen_device: RefCell<Option<gdk::Device>>,
pub(crate) zooming: Cell<bool>,
pub(crate) zooming_ended: Cell<Option<Instant>>,

pub(crate) engine: Rc<RefCell<RnoteEngine>>,

Expand Down Expand Up @@ -147,6 +150,9 @@ mod imp {
key_controller_im_context,
drop_target,
drawing_cursor_enabled: Cell::new(false),
pen_device: RefCell::new(None),
zooming: Cell::new(false),
zooming_ended: Cell::new(None),

engine: Rc::new(RefCell::new(engine)),

Expand Down Expand Up @@ -598,6 +604,43 @@ impl RnCanvas {
}
}

#[allow(unused)]
pub(crate) fn pen_device_eq(&self, other: Option<gdk::Device>) -> bool {
self.imp().pen_device.borrow().as_ref().and_then(|d| Some(d.name())) == other.and_then(|d| Some(d.name()))
}

#[allow(unused)]
pub(crate) fn set_pen_device(&self, pen_device: Option<gdk::Device>) {
self.imp().pen_device.replace(pen_device);
}

#[allow(unused)]
pub(crate) fn zooming(&self) -> bool {
self.imp().zooming.get()
}

#[allow(unused)]
pub(crate) fn set_zooming(&self, zooming: bool, device: Option<gdk::Device>, now: Instant) {
self.imp().zooming.set(zooming);
if !zooming {
self.set_zooming_ended(Some(now));
} else if zooming && !self.engine().borrow().pen_idle() {
if self.pen_device_eq(device) {
self.emit_handle_widget_flags(self.engine().borrow_mut().undo(now));
}
}
}

#[allow(unused)]
pub(crate) fn zooming_ended(&self) -> Option<Instant> {
self.imp().zooming_ended.get()
}

#[allow(unused)]
pub(crate) fn set_zooming_ended(&self, zooming_ended: Option<Instant>) {
self.imp().zooming_ended.set(zooming_ended);
}

#[allow(unused)]
fn emit_zoom_changed(&self) {
self.emit_by_name::<()>("zoom-changed", &[]);
Expand Down Expand Up @@ -1275,3 +1318,4 @@ impl RnCanvas {
self.queue_draw();
}
}

12 changes: 3 additions & 9 deletions rnote-ui/src/canvaswrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,7 @@ mod imp {
new_zoom.set(current_zoom);
prev_scale.set(1.0);

if let Some(widget_flags) = canvaswrapper.canvas().engine().borrow_mut().handle_zooming(true, gesture.device(), Instant::now()) {
canvaswrapper.canvas().emit_handle_widget_flags(widget_flags);
}
canvaswrapper.canvas().set_zooming(true, gesture.device(), Instant::now());

bbcenter_begin.set(gesture.bounding_box_center().map(|coords| na::vector![coords.0, coords.1]));
offset_begin.set(canvaswrapper.canvas().engine().borrow().camera.offset);
Expand Down Expand Up @@ -393,19 +391,15 @@ mod imp {
self.canvas_zoom_gesture.connect_end(
clone!(@weak obj as canvaswrapper => move |gesture, _event_sequence| {
gesture.set_state(EventSequenceState::Denied);
if let Some(widget_flags) = canvaswrapper.canvas().engine().borrow_mut().handle_zooming(false, gesture.device(), Instant::now()) {
canvaswrapper.canvas().emit_handle_widget_flags(widget_flags);
}
canvaswrapper.canvas().set_zooming(false, gesture.device(), Instant::now());
canvaswrapper.canvas().update_engine_rendering();
}),
);

self.canvas_zoom_gesture.connect_cancel(
clone!(@weak obj as canvaswrapper => move |gesture, _event_sequence| {
gesture.set_state(EventSequenceState::Denied);
if let Some(widget_flags) = canvaswrapper.canvas().engine().borrow_mut().handle_zooming(false, gesture.device(), Instant::now()) {
canvaswrapper.canvas().emit_handle_widget_flags(widget_flags);
}
canvaswrapper.canvas().set_zooming(false, gesture.device(), Instant::now());
canvaswrapper.canvas().update_engine_rendering();
}),
);
Expand Down

0 comments on commit 6448e34

Please sign in to comment.