PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Selection tools: arrow keys nudge the selection outline (#1428) (#1502)
David Mann2aa1eeadccad
diff --git a/crates/ui-egui/src/marquee_tests.rs b/crates/ui-egui/src/marquee_tests.rsindex 068ce5aed..b3dc81c4a 100644--- a/crates/ui-egui/src/marquee_tests.rs+++ b/crates/ui-egui/src/marquee_tests.rs@@ -291,6 +291,64 @@ fn mouse_drag_inside_the_selection_moves_it() {assert_eq!(selection(&h), Rect::new(120, 90, 220, 170), "moved by (20, 10)");}+/// #1428: hovering inside the ants with a marquee shows the move cursor (a press there drags the+/// outline); outside it is the marquee's own cursor.+#[test]+fn hovering_inside_the_selection_shows_the_move_cursor() {+ for tool in [Tool::RectMarquee, Tool::EllipseMarquee] {+ let mut h = harness(tool);+ press_at(&mut h, 100.0, 80.0, Modifiers::NONE);+ release_at(&mut h, 200.0, 160.0, Modifiers::NONE);+ move_to(&mut h, 150.0, 120.0);+ assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::Move, "{tool:?} inside");+ move_to(&mut h, 300.0, 250.0);+ assert_ne!(h.output().platform_output.cursor_icon, egui::CursorIcon::Move, "{tool:?} outside");+ }+}++/// #1428: with a marquee, the arrow keys nudge the selection outline 1 px, ⇧ 10 px (Photoshop);+/// each press is one undoable step.+#[test]+fn arrow_keys_nudge_the_selection_outline() {+ use egui::Key;+ for tool in [Tool::RectMarquee, Tool::EllipseMarquee] {+ let mut h = harness(tool);+ press_at(&mut h, 100.0, 80.0, Modifiers::NONE);+ release_at(&mut h, 200.0, 160.0, Modifiers::NONE);+ let drawn = selection(&h);+ assert!(!drawn.is_empty(), "{tool:?} drew");+ let shifted = |dx: i32, dy: i32| Rect::new(drawn.x0 + dx, drawn.y0 + dy, drawn.x1 + dx, drawn.y1 + dy);+ let steps = h.state().session.active().unwrap().history.past_len();+ h.key_press(Key::ArrowRight);+ h.run_steps(1);+ assert_eq!(selection(&h), shifted(1, 0), "{tool:?} right 1 px");+ h.key_press(Key::ArrowUp);+ h.run_steps(1);+ assert_eq!(selection(&h), shifted(1, -1), "{tool:?} up 1 px");+ h.key_press_modifiers(Modifiers::SHIFT, Key::ArrowDown);+ h.run_steps(1);+ assert_eq!(selection(&h), shifted(1, 9), "{tool:?} shift-down 10 px");+ h.key_press_modifiers(Modifiers::SHIFT, Key::ArrowLeft);+ h.run_steps(1);+ assert_eq!(selection(&h), shifted(-9, 9), "{tool:?} shift-left 10 px");+ assert_eq!(h.state().session.active().unwrap().history.past_len(), steps + 4, "one step per press");+ h.state_mut().session.undo();+ assert_eq!(selection(&h), shifted(1, 9), "{tool:?} undo takes back one nudge");+ }+}++/// Arrow keys with a marquee but no selection change nothing and report no error.+#[test]+fn arrow_keys_without_a_selection_do_nothing() {+ let mut h = harness(Tool::RectMarquee);+ let steps = h.state().session.active().unwrap().history.past_len();+ h.key_press(egui::Key::ArrowRight);+ h.run_steps(1);+ assert_eq!(selection(&h), Rect::EMPTY);+ assert_eq!(h.state().session.active().unwrap().history.past_len(), steps);+ assert!(!h.state().ui.status_error);+}+/// ⌘⌥-drag copies the selected pixels instead of cutting them: the original stays.#[test]fn cmd_alt_drag_floats_a_copy() {diff --git a/crates/ui-egui/src/move_mods.rs b/crates/ui-egui/src/move_mods.rsindex 572ea03a8..18badd58b 100644--- a/crates/ui-egui/src/move_mods.rs+++ b/crates/ui-egui/src/move_mods.rs@@ -170,12 +170,13 @@ pub fn abandon(app: &mut PhotocraftApp) {/// ⌥ duplicates the layers first. Returns true when a key was used.pub fn arrow_keys(app: &mut PhotocraftApp, ctx: &egui::Context) -> bool {if app.ui.tool != Tool::Move && app.ui.transform.is_none() {- return false;+ return selection_arrow_keys(app, ctx);}use egui::{Key, Modifiers};let keys = [(Key::ArrowLeft, -1.0, 0.0), (Key::ArrowRight, 1.0, 0.0), (Key::ArrowUp, 0.0, -1.0), (Key::ArrowDown, 0.0, 1.0)];for (key, ux, uy) in keys {- for mods in [Modifiers::NONE, Modifiers::SHIFT, Modifiers::ALT, Modifiers::SHIFT | Modifiers::ALT] {+ // Most specific first: `consume_key` ignores extra ⇧ / ⌥ (`matches_logically`).+ for mods in [Modifiers::SHIFT | Modifiers::ALT, Modifiers::SHIFT, Modifiers::ALT, Modifiers::NONE] {if ctx.input_mut(|i| i.consume_key(mods, key)) {let k = if mods.shift { 10.0 } else { 1.0 };nudge(app, ux * k, uy * k, mods.alt);@@ -212,6 +213,38 @@ pub fn nudge(app: &mut PhotocraftApp, dx: f64, dy: f64, duplicate_first: bool) {}}+/// Arrow keys with a selection tool (#1428), as in Photoshop: nudge the selection outline 1 px+/// (⇧ 10 px) through `select.transformSelection`, one history step per press; the pixels stay+/// put. A floating piece (⌘-dragged pixels) moves instead, like a plain drag on it. Not while a+/// drag, a polygon or a Magnetic Lasso border is in progress. Returns true when a key was used.+fn selection_arrow_keys(app: &mut PhotocraftApp, ctx: &egui::Context) -> bool {+ if !crate::tool_feedback::is_selection_tool(app.ui.tool) || app.drag.is_some() || !app.ui.polygon.is_empty() || app.ui.magnetic.active() {+ return false;+ }+ let floating = app.session.active().is_some_and(|st| photocraft_engine::float_cmds::floating(st).is_some());+ if !floating && !app.session.is_enabled("select.transformSelection") {+ return false;+ }+ use egui::{Key, Modifiers};+ let keys = [(Key::ArrowLeft, -1.0, 0.0), (Key::ArrowRight, 1.0, 0.0), (Key::ArrowUp, 0.0, -1.0), (Key::ArrowDown, 0.0, 1.0)];+ for (key, ux, uy) in keys {+ // ⇧ first: `consume_key` ignores an extra ⇧.+ for mods in [Modifiers::SHIFT, Modifiers::NONE] {+ if ctx.input_mut(|i| i.consume_key(mods, key)) {+ let k = if mods.shift { 10.0 } else { 1.0 };+ let (dx, dy) = (ux * k, uy * k);+ let id = if floating { "select.float" } else { "select.transformSelection" };+ if let Err(e) = app.run(id, json!({"dx": dx, "dy": dy})) {+ app.ui.status = e;+ app.ui.status_error = true;+ }+ return true;+ }+ }+ }+ false+}+#[cfg(test)]mod tests {use super::*;@@ -354,4 +387,53 @@ mod tests {nudge(&mut app, 0.0, 10.0, false);assert_eq!(app.ui.transform.as_ref().unwrap().quad[0], [q0[0][0], q0[0][1] + 10.0]);}++ /// One key press through `arrow_keys`, as egui delivers it.+ fn press(app: &mut PhotocraftApp, key: egui::Key, mods: egui::Modifiers) -> bool {+ let ctx = egui::Context::default();+ ctx.input_mut(|i| {+ i.modifiers = mods;+ i.events.push(egui::Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers: mods });+ });+ arrow_keys(app, &ctx)+ }++ /// Real key events: ⇧ nudges 10 px and ⌥ duplicates first (egui ignores extra ⇧ / ⌥ when+ /// matching, so the plain arrow must not be tried first).+ #[test]+ fn arrow_key_modifiers_reach_the_move_tool() {+ let mut app = app_with_layer();+ let id = app.session.active().unwrap().active_layer.unwrap();+ let n0 = app.session.active().unwrap().doc.layers.len();+ assert!(press(&mut app, egui::Key::ArrowRight, egui::Modifiers::NONE));+ assert_eq!(bounds(&app, id), photocraft_geom::Rect::new(9, 8, 25, 24));+ assert!(press(&mut app, egui::Key::ArrowDown, egui::Modifiers::SHIFT));+ assert_eq!(bounds(&app, id), photocraft_geom::Rect::new(9, 18, 25, 34));+ assert!(press(&mut app, egui::Key::ArrowLeft, egui::Modifiers::ALT));+ let st = app.session.active().unwrap();+ assert_eq!(st.doc.layers.len(), n0 + 1, "⌥ duplicated first");+ assert_eq!(bounds(&app, id), photocraft_geom::Rect::new(9, 18, 25, 34), "the original stays");+ }++ /// #1428: with a selection tool the arrows nudge the selection outline (⇧ 10 px), one step+ /// each; the layer stays put. Without a selection they are left alone.+ #[test]+ fn arrow_keys_nudge_the_selection_with_a_selection_tool() {+ let mut app = app_with_layer();+ let id = app.session.active().unwrap().active_layer.unwrap();+ app.ui.tool = Tool::Lasso;+ assert!(!press(&mut app, egui::Key::ArrowRight, egui::Modifiers::NONE), "no selection: not used");+ app.session.execute("select.rect", json!({"x": 4, "y": 4, "width": 10, "height": 10})).unwrap();+ let sel = |app: &PhotocraftApp| app.session.active().unwrap().doc.selection.as_ref().unwrap().content_bounds();+ let h0 = app.session.active().unwrap().history.past_len();+ assert!(press(&mut app, egui::Key::ArrowRight, egui::Modifiers::NONE));+ assert!(press(&mut app, egui::Key::ArrowDown, egui::Modifiers::SHIFT));+ assert_eq!(sel(&app), photocraft_geom::Rect::new(5, 14, 15, 24));+ assert_eq!(app.session.active().unwrap().history.past_len(), h0 + 2);+ assert_eq!(bounds(&app, id), photocraft_geom::Rect::new(8, 8, 24, 24), "pixels stay put");+ // Not while a polygon is being drawn.+ app.ui.tool = Tool::PolygonLasso;+ app.ui.polygon = vec![[1.0, 1.0]];+ assert!(!press(&mut app, egui::Key::ArrowRight, egui::Modifiers::NONE));+ }}