Skip to content

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

M5: Alt+Left / Alt+Right go back through the view history with a comment selected (#3632)

Adrian5d6f36f06029
diff --git a/crates/ui-egui/src/tools/comments/input.rs b/crates/ui-egui/src/tools/comments/input.rs
index af78a1e46..2c4f4f059 100644
--- a/crates/ui-egui/src/tools/comments/input.rs
+++ b/crates/ui-egui/src/tools/comments/input.rs
@@ -637,19 +637,28 @@ pub(crate) fn nudge_keys(ctx: &egui::Context, view: &mut DocView, info: &DocInfo
if !movable {
return;
}
- use egui::{Key, Modifiers};
+ use egui::Key;
let mut step = egui::Vec2::ZERO;
ctx.input_mut(|i| {
- for (key, direction) in
- [(Key::ArrowLeft, vec2(-1.0, 0.0)), (Key::ArrowRight, vec2(1.0, 0.0)), (Key::ArrowUp, vec2(0.0, -1.0)), (Key::ArrowDown, vec2(0.0, 1.0))]
- {
- // The modified patterns first: a plain one also matches a Shift press.
- if i.consume_key(Modifiers::COMMAND, key) || i.consume_key(Modifiers::SHIFT, key) {
- step += direction * 10.0;
- } else if i.consume_key(Modifiers::NONE, key) {
- step += direction;
+ // Only an arrow pressed alone or with ⌘/Ctrl or Shift, judged by each press's own
+ // modifiers. Alt+← / Alt+→ go back and forward through the view history in
+ // `canvas::shortcuts` (#3632), and egui's key patterns would let an extra Alt through.
+ // A Ctrl that isn't the command key (macOS) is left alone, as the patterns left it.
+ i.events.retain(|e| {
+ let egui::Event::Key { key, pressed: true, modifiers, .. } = e else { return true };
+ if modifiers.alt || (modifiers.ctrl && !modifiers.command) {
+ return true;
}
- }
+ let direction = match key {
+ Key::ArrowLeft => vec2(-1.0, 0.0),
+ Key::ArrowRight => vec2(1.0, 0.0),
+ Key::ArrowUp => vec2(0.0, -1.0),
+ Key::ArrowDown => vec2(0.0, 1.0),
+ _ => return true,
+ };
+ step += direction * if modifiers.command || modifiers.shift { 10.0 } else { 1.0 };
+ false
+ });
});
if step == egui::Vec2::ZERO {
return;
diff --git a/crates/ui-egui/tests/it/comments.rs b/crates/ui-egui/tests/it/comments.rs
index 3a3169576..3351013c2 100644
--- a/crates/ui-egui/tests/it/comments.rs
+++ b/crates/ui-egui/tests/it/comments.rs
@@ -1004,6 +1004,38 @@ fn arrow_keys_nudge_the_selected_comment() {
assert_eq!(h.state().views[0].current, 1, "and the view goes to the next page");
}
+/// #3632: with a comment selected, Alt+← / Alt+→ still go back and forward through the view
+/// history instead of nudging it (egui's key patterns let an extra Alt through).
+#[test]
+fn alt_arrows_navigate_instead_of_nudging_the_selected_comment() {
+ use egui::{Key, Modifiers};
+ let mut h = harness(|app| app.set_option("quick", "square").unwrap());
+ drag_pt(&mut h, (40.0, 100.0), (140.0, 40.0));
+ h.state_mut().set_option("quick", "select").unwrap();
+ click_pt(&mut h, (90.0, 70.0));
+ assert_eq!(h.state().views[0].comments.selected, Some((0, 0)));
+ let before = comments(&h)[0].rect;
+ // A followed link's jump to page 2 and back, as `go_to_dest` makes it.
+ h.state_mut().views[0].go_to_page(1);
+ h.run_steps(2);
+ h.state_mut().views[0].go_to_page(0);
+ h.run_steps(2);
+ assert_eq!(h.state().views[0].comments.selected, Some((0, 0)), "still selected, so a nudge could take the keys");
+ h.key_press_modifiers(Modifiers::ALT, Key::ArrowLeft);
+ h.run_steps(3);
+ assert_eq!(h.state().views[0].current, 1, "Alt+← went back");
+ h.key_press_modifiers(Modifiers::ALT, Key::ArrowRight);
+ h.run_steps(3);
+ assert_eq!(h.state().views[0].current, 0, "Alt+→ went forward");
+ assert_eq!(comments(&h)[0].rect, before, "the comment didn't move");
+ let id = h.state().views[0].id;
+ assert_ne!(h.state().session.get(id).unwrap().can_undo(), Some("Move comment"), "nothing was nudged");
+ // A plain arrow still nudges it.
+ h.key_press(Key::ArrowRight);
+ h.run_steps(3);
+ assert!(comments(&h)[0].rect[0] > before[0], "→ nudged it");
+}
+
#[test]
fn a_shape_just_drawn_resizes_from_its_handles_without_the_select_tool() {
// #2338: the new shape showed its handles, but only the Select tool answered them.