Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

fix(ui): Escape cancels any drag the pages hold, and its release does nothing

alehel9f483490c7e1
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 91d2c4136..c2c74ac8a 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -473,6 +473,9 @@ pub struct DocView {
pub(crate) signature_drag: crate::signature_drag::SignatureDrag,
/// What the page modules claim for the cursor this frame; `document_area` sets it once.
pub(crate) cursor: crate::page_cursor::PageCursor,
+ /// Escape cancelled the press the pages hold: the page ignores it until the button is up, so
+ /// its release neither clicks nor ends a drag.
+ pub(crate) press_cancelled: bool,
/// A non-edit action requested by the organize toolbar, handled by the app.
pub pending_action: Option<ViewAction>,
/// The grid gap the next inserted files go to (set by a "+" between pages); otherwise they
@@ -726,6 +729,7 @@ impl DocView {
fill_signature_page: None,
signature_drag: Default::default(),
cursor: Default::default(),
+ press_cancelled: false,
}
}
@@ -1737,7 +1741,8 @@ fn draw_page(ui: &mut egui::Ui, view: &mut DocView, frame: &mut PageFrame<'_>, i
}
let slot = PageSlot { i, r, xf };
- let input = page_tool_input(ui, view, frame, &slot);
+ // A cancelled press takes nothing until the button is up.
+ let input = if view.press_cancelled { PageInput { consumed: true, area_tool: false } } else { page_tool_input(ui, view, frame, &slot) };
let painter = ui.painter();
let pcx = comments::PageCx { page: i, xf: &xf, ptr, info, tool, prefs, allowed, hidden: comments_hidden, redact_mark };
let preview_target = (matches!(tool, QuickTool::Select | QuickTool::Fill(_)) && !comments_hidden).then_some(view.comments.selected).flatten();
@@ -1754,6 +1759,20 @@ fn draw_page(ui: &mut egui::Ui, view: &mut DocView, frame: &mut PageFrame<'_>, i
page_overlays(ui, view, frame, &slot, &input);
}
+/// Escape while the pages hold a press abandons the gesture it drives, whichever tool's: nothing is
+/// placed, drawn, moved, resized or opened, and the press is ignored until the button is up. The
+/// frame of the release still belongs to the cancelled press.
+fn cancel_on_escape(ctx: &egui::Context, view: &mut DocView, ptr: &PagePointer) {
+ if view.press_cancelled && !ptr.any_down && ptr.edges.is_empty() {
+ view.press_cancelled = false;
+ }
+ if !view.press_cancelled && ptr.origin.is_some() && ptr.primary_down && ctx.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Escape))
+ {
+ view.cancel_pointer_gestures();
+ view.press_cancelled = true;
+ }
+}
+
fn draw_pages(
ui: &mut egui::Ui,
viewport: Rect,
@@ -1833,6 +1852,7 @@ fn draw_pages(
// The pointer as the pages see it: nothing over them (a dialog from the frame it opens),
// or a press they hold. The page modules read it rather than egui's raw pointer state.
let ptr = PagePointer::read(ui, &resp, modal);
+ cancel_on_escape(ui.ctx(), view, &ptr);
let pointer = ptr.pos;
// A press on a page makes it the current one (as in Acrobat), so a page command with
// no selection (Rotate, Delete) acts on the page the reader pointed at, not on a page
diff --git a/crates/ui-egui/tests/it/pointer.rs b/crates/ui-egui/tests/it/pointer.rs
index 74120834b..442eade9e 100644
--- a/crates/ui-egui/tests/it/pointer.rs
+++ b/crates/ui-egui/tests/it/pointer.rs
@@ -713,7 +713,6 @@ fn touch_drag_moves_a_comment_and_leaves_no_gesture_behind() {
// Ending a gesture.
#[test]
-#[ignore = "step 4 of the page pointer router: press-time capture and cancel"]
fn escape_cancels_a_signature_rectangle() {
let mut h = open(link_pdf(), "150", false);
h.state_mut().quick_tool = QuickTool::SignArea { certify: false };
@@ -732,3 +731,44 @@ fn escape_cancels_a_signature_rectangle() {
assert_eq!(s.quick_tool, QuickTool::SignArea { certify: false }, "Escape abandons the rectangle; the tool stays");
assert!(s.dialog.is_none(), "and no signing dialog opens: {:?}", s.dialog);
}
+
+/// Escape with the button down drops the press: its release places no stamp, and the next click
+/// places one.
+#[test]
+fn escape_mid_press_keeps_its_release_from_placing_a_stamp() {
+ let mut h = open(link_pdf(), "150", false);
+ stamp_tool(&mut h);
+ let p = at(&h, 150.0, 150.0);
+ h.hover_at(p);
+ h.run_steps(1);
+ h.drag_at(p);
+ h.run_steps(1);
+ h.key_press(egui::Key::Escape);
+ h.run_steps(1);
+ h.drop_at(p);
+ h.run_steps(4);
+ assert_eq!(stamps(&h), 0, "the cancelled press places nothing on release");
+ mouse_click(&mut h, p);
+ assert_eq!(stamps(&h), 1, "the next click places a stamp");
+}
+
+/// Escape mid-drag abandons a crop rectangle: the page keeps its boxes.
+#[test]
+fn escape_cancels_a_crop_rectangle() {
+ let mut h = open(text_pdf(), "150", false);
+ h.state_mut().quick_tool = QuickTool::Crop;
+ h.run_steps(4);
+ let (a, b) = (at(&h, 40.0, 360.0), at(&h, 260.0, 60.0));
+ h.hover_at(a);
+ h.run_steps(1);
+ h.drag_at(a);
+ h.run_steps(1);
+ h.hover_at(b);
+ h.run_steps(2);
+ h.key_press(egui::Key::Escape);
+ h.run_steps(1);
+ h.drop_at(b);
+ h.run_steps(4);
+ let s = h.state();
+ assert_eq!(s.session.get(s.views[0].id).unwrap().can_undo(), None, "no page box was set");
+}