GitCorner

Code

ossartcraftphotocraft🌐
856f865cf98d
use super::*;
use crate::canvas::ToolEvent;

fn app_with_layer() -> Box<PhotocraftApp> {
    let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
    app.session.execute("file.new", json!({"width": 64, "height": 64})).unwrap();
    app.sync_views();
    app.session.execute("layer.new.layer", json!({})).unwrap();
    app.session
        .edit("paint", |doc, a| {
            doc.layer_mut(a.unwrap()).unwrap().surface_mut().unwrap().fill_rect(photocraft_geom::Rect::new(8, 8, 24, 24), &[1.0, 0.0, 0.0, 1.0]);
            Ok(())
        })
        .unwrap();
    app.ui.tool = Tool::Move;
    app.ui.tool_options.move_auto_select = false;
    // Snapping (to the document centre with a 1:1 test view) would shift the expected offsets.
    app.ui.extras.snap = false;
    app.ui.view.show.smart_guides = false;
    app
}

fn bounds(app: &PhotocraftApp, id: photocraft_doc::LayerId) -> photocraft_geom::Rect {
    app.session.active().unwrap().doc.layer(id).unwrap().surface().unwrap().content_bounds()
}

fn drag(app: &mut PhotocraftApp, pts: &[[f64; 2]], mods: egui::Modifiers) {
    crate::canvas::tool_event(app, ToolEvent::Down { x: pts[0][0], y: pts[0][1], pressure: 1.0 }, mods);
    for p in &pts[1..pts.len() - 1] {
        crate::canvas::tool_event(app, ToolEvent::Move { x: p[0], y: p[1], pressure: 1.0 }, mods);
    }
    let e = pts[pts.len() - 1];
    crate::canvas::tool_event(app, ToolEvent::Up { x: e[0], y: e[1] }, mods);
}

#[test]
fn shift_drag_moves_along_one_axis_only() {
    let mut app = app_with_layer();
    let id = app.session.active().unwrap().active_layer.unwrap();
    drag(&mut app, &[[16.0, 16.0], [22.0, 18.0], [26.0, 19.0]], egui::Modifiers::SHIFT);
    assert_eq!(bounds(&app, id), photocraft_geom::Rect::new(18, 8, 34, 24));
    // Switching axis mid-drag: the end point decides.
    drag(&mut app, &[[16.0, 16.0], [22.0, 17.0], [18.0, 30.0], [17.0, 36.0]], egui::Modifiers::SHIFT);
    assert_eq!(bounds(&app, id), photocraft_geom::Rect::new(18, 28, 34, 44));
}

#[test]
fn alt_drag_duplicates_and_moves_the_copy_in_one_step() {
    let mut app = app_with_layer();
    let orig = app.session.active().unwrap().active_layer.unwrap();
    let n0 = app.session.active().unwrap().doc.layers.len();
    let h0 = app.session.active().unwrap().history.past_len();
    drag(&mut app, &[[16.0, 16.0], [20.0, 16.0], [26.0, 21.0]], egui::Modifiers::ALT);
    let st = app.session.active().unwrap();
    assert_eq!(st.doc.layers.len(), n0 + 1, "one copy");
    let copy = st.active_layer.unwrap();
    assert_ne!(copy, orig);
    assert_eq!(bounds(&app, orig), photocraft_geom::Rect::new(8, 8, 24, 24), "original untouched");
    assert_eq!(bounds(&app, copy), photocraft_geom::Rect::new(18, 13, 34, 29));
    let st = app.session.active().unwrap();
    assert_eq!(st.history.past_len(), h0 + 1, "one undo step");
    assert_eq!(st.history.undo_label(), Some("Move"));
    assert!(app.session.undo());
    assert_eq!(app.session.active().unwrap().doc.layers.len(), n0);
    assert_eq!(bounds(&app, orig), photocraft_geom::Rect::new(8, 8, 24, 24));
    // ⌥-click without a drag makes no copy.
    drag(&mut app, &[[16.0, 16.0], [16.0, 16.0]], egui::Modifiers::ALT);
    assert_eq!(app.session.active().unwrap().doc.layers.len(), n0);
    // ⇧⌥: a copy moved along one axis.
    drag(&mut app, &[[16.0, 16.0], [30.0, 19.0]], egui::Modifiers::SHIFT | egui::Modifiers::ALT);
    let st = app.session.active().unwrap();
    assert_eq!(st.doc.layers.len(), n0 + 1);
    assert_eq!(bounds(&app, st.active_layer.unwrap()), photocraft_geom::Rect::new(22, 8, 38, 24));
}

/// Allow Non-Linear History: the copy's step moves the later states in front of it, and
/// folding the copy and the move into one step must keep them.
#[test]
fn alt_drag_keeps_the_later_states_in_non_linear_history() {
    let mut app = app_with_layer();
    app.session.execute("history.options", json!({"allowNonLinearHistory": true})).unwrap();
    let id = app.session.active().unwrap().active_layer.unwrap();
    app.session.execute("layer.setProps", json!({"layer": id.0, "opacity": 0.5})).unwrap();
    assert!(app.session.undo());
    let mut expected = app.session.active().unwrap().history.entries();
    expected.extend(app.session.active().unwrap().history.redo_labels().map(str::to_string));
    expected.push("Move".into());
    drag(&mut app, &[[16.0, 16.0], [20.0, 16.0], [26.0, 21.0]], egui::Modifiers::ALT);
    assert_eq!(app.session.active().unwrap().history.entries(), expected, "the later state stays");
}

#[test]
fn alt_drag_duplicates_every_selected_layer() {
    let mut app = app_with_layer();
    let a = app.session.active().unwrap().active_layer.unwrap();
    app.session.execute("layer.new.layer", json!({})).unwrap();
    let b = app.session.active().unwrap().active_layer.unwrap();
    app.session.execute("layer.select", json!({"layer": a.0, "mode": "add"})).unwrap();
    let n0 = app.session.active().unwrap().doc.layers.len();
    let h0 = app.session.active().unwrap().history.past_len();
    drag(&mut app, &[[16.0, 16.0], [26.0, 16.0]], egui::Modifiers::ALT);
    let st = app.session.active().unwrap();
    assert_eq!(st.doc.layers.len(), n0 + 2);
    assert_eq!(st.history.past_len(), h0 + 1);
    assert_eq!(bounds(&app, a), photocraft_geom::Rect::new(8, 8, 24, 24));
    assert!(st.doc.layer(b).is_some());
}

#[test]
fn nudge_moves_layers_and_duplicates_first() {
    let mut app = app_with_layer();
    let id = app.session.active().unwrap().active_layer.unwrap();
    nudge(&mut app, 10.0, 0.0, false);
    nudge(&mut app, 0.0, -1.0, false);
    assert_eq!(bounds(&app, id), photocraft_geom::Rect::new(18, 7, 34, 23));
    let n0 = app.session.active().unwrap().doc.layers.len();
    nudge(&mut app, 1.0, 0.0, true);
    assert_eq!(app.session.active().unwrap().doc.layers.len(), n0 + 1);
    assert_eq!(bounds(&app, id), photocraft_geom::Rect::new(18, 7, 34, 23));
}

/// One arrow press through `arrow_key`, as the dispatcher hands it over.
fn press(app: &mut PhotocraftApp, key: egui::Key, mods: egui::Modifiers) -> bool {
    press_at(app, key, mods, 0.0)
}

/// [`press`] at time `time` (seconds).
fn press_at(app: &mut PhotocraftApp, key: egui::Key, mods: egui::Modifiers, time: f64) -> bool {
    press_with(app, key, mods, time, false)
}

/// [`press_at`] with a selection outline open or not.
fn press_with(app: &mut PhotocraftApp, key: egui::Key, mods: egui::Modifiers, time: f64, outline_open: bool) -> bool {
    let direction = match key {
        egui::Key::ArrowLeft => egui::vec2(-1.0, 0.0),
        egui::Key::ArrowRight => egui::vec2(1.0, 0.0),
        egui::Key::ArrowUp => egui::vec2(0.0, -1.0),
        egui::Key::ArrowDown => egui::vec2(0.0, 1.0),
        other => panic!("not an arrow: {other:?}"),
    };
    let mods = craft::keymap::Mods::from_egui(&mods, craft::keymap::Platform::current());
    arrow_key(app, direction, mods, time, outline_open)
}

/// Photoshop's ← / → with a painting tool turn the brush tip 1°, ⇧ 15°; → is clockwise. Other
/// tools keep their arrows (the Hand has none, the marquee nudges the selection).
#[test]
fn arrows_turn_the_brush_tip_for_painting_tools_only() {
    let mut app = app_with_layer();
    for tool in [Tool::Brush, Tool::Eraser, Tool::CloneStamp] {
        app.ui.tool = tool;
        app.run("tools.setBrush", json!({"brush": {"angle": 0}})).unwrap();
        assert!(press(&mut app, egui::Key::ArrowLeft, egui::Modifiers::NONE), "{tool:?}");
        assert_eq!(app.session.tools.brush.angle, 1.0, "{tool:?}: ← anticlockwise");
        assert!(press(&mut app, egui::Key::ArrowRight, egui::Modifiers::SHIFT));
        assert_eq!(app.session.tools.brush.angle, -14.0, "{tool:?}: ⇧→ 15° clockwise");
    }
    app.run("tools.setBrush", json!({"brush": {"angle": 0}})).unwrap();
    // ⌥-arrows are not the brush's (they kern type, and ⌘ moves).
    assert!(!press(&mut app, egui::Key::ArrowLeft, egui::Modifiers::ALT));
    app.ui.tool = Tool::Hand;
    assert!(!press(&mut app, egui::Key::ArrowLeft, egui::Modifiers::NONE));
    assert_eq!(app.session.tools.brush.angle, 0.0);
}

/// 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");
}

/// ⌘ as a Mac sends it, and Ctrl as Windows and Linux send it (`command` is Ctrl there).
const MAC_CMD: egui::Modifiers = egui::Modifiers { alt: false, ctrl: false, shift: false, mac_cmd: true, command: true };
const CTRL: egui::Modifiers = egui::Modifiers { alt: false, ctrl: true, shift: false, mac_cmd: false, command: true };

/// #2474: ⌘ is the temporary Move tool, so ⌘-arrows nudge the layer from the Brush (⌘⇧ 10 px,
/// ⌘⌥ duplicates first); the plain arrows still do nothing there.
#[test]
fn cmd_arrows_nudge_the_layer_from_the_brush() {
    let mut app = app_with_layer();
    app.ui.tool = Tool::Brush;
    let id = app.session.active().unwrap().active_layer.unwrap();
    let n0 = app.session.active().unwrap().doc.layers.len();
    // A plain arrow turns the Brush's tip (below), never the layer.
    assert!(press(&mut app, egui::Key::ArrowRight, egui::Modifiers::NONE));
    assert_eq!(bounds(&app, id), photocraft_geom::Rect::new(8, 8, 24, 24));
    assert_eq!(app.session.tools.brush.angle, -1.0);
    app.run("tools.setBrush", json!({"brush": {"angle": 0}})).unwrap();
    for cmd in [CTRL, MAC_CMD] {
        assert!(press(&mut app, egui::Key::ArrowRight, cmd));
    }
    assert_eq!(bounds(&app, id), photocraft_geom::Rect::new(10, 8, 26, 24), "1 px per press");
    assert!(press(&mut app, egui::Key::ArrowDown, CTRL | egui::Modifiers::SHIFT));
    assert_eq!(bounds(&app, id), photocraft_geom::Rect::new(10, 18, 26, 34), "⌘⇧: 10 px");
    assert!(press(&mut app, egui::Key::ArrowLeft, CTRL | 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(10, 18, 26, 34), "the original stays");
    assert_eq!(bounds(&app, st.active_layer.unwrap()), photocraft_geom::Rect::new(9, 18, 25, 34), "the copy moved");
    assert_eq!(app.ui.tool, Tool::Brush);
    // The Move tool itself takes ⌘-arrows as plain ones.
    app.ui.tool = Tool::Move;
    assert!(press(&mut app, egui::Key::ArrowUp, CTRL));
    let copy = app.session.active().unwrap().active_layer.unwrap();
    assert_eq!(bounds(&app, copy), photocraft_geom::Rect::new(9, 17, 25, 33));
}

/// #2474: tools where ⌘ means something else in Photoshop (Pen and path tools, Hand, Zoom,
/// Crop, Type) keep ⌘-arrows away from the layer, as does an active Free Transform.
#[test]
fn cmd_arrows_leave_the_layer_alone_where_cmd_is_not_the_move_tool() {
    let mut app = app_with_layer();
    let id = app.session.active().unwrap().active_layer.unwrap();
    for tool in [Tool::Pen, Tool::PathSelection, Tool::DirectSelection, Tool::Rectangle, Tool::Hand, Tool::Zoom, Tool::Crop, Tool::Type] {
        app.ui.tool = tool;
        assert!(!press(&mut app, egui::Key::ArrowRight, CTRL), "{tool:?}");
        assert!(!press(&mut app, egui::Key::ArrowRight, MAC_CMD | egui::Modifiers::SHIFT), "{tool:?}");
    }
    assert_eq!(bounds(&app, id), photocraft_geom::Rect::new(8, 8, 24, 24));
    app.ui.tool = Tool::Brush;
    let ctx = app.ctx().clone();
    crate::transform_tool::begin(&mut app, &ctx).unwrap();
    // PLAN: `session.quad()` is the framework box's reading of the open Free Transform (tools-w27).
    let q0 = app.ui.transform.as_ref().unwrap().session.quad();
    assert!(!press(&mut app, egui::Key::ArrowRight, CTRL));
    assert_eq!(app.ui.transform.as_ref().unwrap().session.quad(), q0);
}

/// #2474: with a selection tool ⌘-arrows move the selected pixels (the temporary Move tool), the
/// plain arrows the outline.
#[test]
fn cmd_arrows_move_the_selected_pixels_with_a_selection_tool() {
    let mut app = app_with_layer();
    app.ui.tool = Tool::RectMarquee;
    app.session.execute("select.rect", json!({"x": 8, "y": 8, "width": 8, "height": 8})).unwrap();
    assert!(press(&mut app, egui::Key::ArrowRight, CTRL));
    let offset = |app: &PhotocraftApp| photocraft_engine::float_cmds::floating(app.session.active().unwrap()).map(|f| f.offset);
    assert_eq!(offset(&app), Some((1, 0)), "the selected pixels float, 1 px to the right");
    assert!(press(&mut app, egui::Key::ArrowDown, CTRL | egui::Modifiers::SHIFT));
    assert_eq!(offset(&app), Some((1, 10)), "⌘⇧: 10 px");
    assert_eq!(app.ui.tool, Tool::RectMarquee);
    // Not while a polygon is being drawn.
    app.ui.tool = Tool::PolygonLasso;
    assert!(!press_with(&mut app, egui::Key::ArrowRight, CTRL, 0.0, true));
}

/// #1428: with a selection tool the arrows nudge the selection outline (⇧ 10 px); the layer
/// stays put. Without a selection they are left alone. One step each with Bundle Arrow-Key
/// Nudges off (#2259).
#[test]
fn arrow_keys_nudge_the_selection_with_a_selection_tool() {
    let mut app = app_with_layer();
    app.session.edit_prefs(|p| p.tools.bundle_nudges = false);
    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;
    assert!(!press_with(&mut app, egui::Key::ArrowRight, egui::Modifiers::NONE, 0.0, true));
}
fn history(app: &PhotocraftApp) -> usize {
    app.session.active().unwrap().history.past_len()
}

fn left_edge(app: &PhotocraftApp, id: photocraft_doc::LayerId) -> i32 {
    bounds(app, id).x0
}

/// #2259: tapping or holding an arrow key is one History state, so one Undo takes the whole
/// run back (Photoshop folds a run of nudges into one "Move").
#[test]
fn a_run_of_arrow_nudges_is_one_history_step() {
    let mut app = app_with_layer();
    let id = app.session.active().unwrap().active_layer.unwrap();
    let h0 = history(&app);
    // Twelve taps, 0.2 s apart, then ⇧ for ten more px: still the same run.
    for i in 0..12 {
        assert!(press_at(&mut app, egui::Key::ArrowRight, egui::Modifiers::NONE, 5.0 + 0.2 * f64::from(i)));
    }
    assert!(press_at(&mut app, egui::Key::ArrowDown, egui::Modifiers::SHIFT, 7.6));
    assert_eq!(bounds(&app, id), photocraft_geom::Rect::new(20, 18, 36, 34));
    assert_eq!(history(&app), h0 + 1, "thirteen presses, one state");
    assert_eq!(app.session.active().unwrap().history.undo_label(), Some("Nudge"));
    assert!(app.session.undo());
    assert_eq!(bounds(&app, id), photocraft_geom::Rect::new(8, 8, 24, 24), "one Undo takes back the run");
    assert!(app.session.redo());
    assert_eq!(bounds(&app, id), photocraft_geom::Rect::new(20, 18, 36, 34), "and one Redo does it again");
}

/// #9511: zoomed out, where each press moves several document pixels, a run of nudges is still
/// one History state.
#[test]
fn a_run_of_arrow_nudges_is_one_history_step_zoomed_out() {
    let mut app = app_with_layer();
    let id = app.session.active().unwrap().active_layer.unwrap();
    app.ui.views[0].zoom = 0.5;
    let h0 = history(&app);
    // Twelve taps of 2 px each at 50 %, then ⇧ for ten more px: still the same run.
    for i in 0..12 {
        assert!(press_at(&mut app, egui::Key::ArrowRight, egui::Modifiers::NONE, 5.0 + 0.2 * f64::from(i)));
    }
    assert!(press_at(&mut app, egui::Key::ArrowDown, egui::Modifiers::SHIFT, 7.6));
    assert_eq!(bounds(&app, id), photocraft_geom::Rect::new(32, 18, 48, 34));
    assert_eq!(history(&app), h0 + 1, "thirteen presses, one state");
    assert_eq!(app.session.active().unwrap().history.undo_label(), Some("Nudge"));
    assert!(app.session.undo());
    assert_eq!(bounds(&app, id), photocraft_geom::Rect::new(8, 8, 24, 24), "one Undo takes back the run");
    assert!(app.session.redo());
    assert_eq!(bounds(&app, id), photocraft_geom::Rect::new(32, 18, 48, 34), "and one Redo does it again");
}

/// A pause longer than Nudge Bundle Pause starts a new run; a longer pause setting keeps it.
#[test]
fn a_pause_ends_the_run() {
    let mut app = app_with_layer();
    let id = app.session.active().unwrap().active_layer.unwrap();
    let h0 = history(&app);
    press_at(&mut app, egui::Key::ArrowRight, egui::Modifiers::NONE, 1.0);
    press_at(&mut app, egui::Key::ArrowRight, egui::Modifiers::NONE, 1.9);
    assert_eq!(history(&app), h0 + 1, "0.9 s: the same run");
    press_at(&mut app, egui::Key::ArrowRight, egui::Modifiers::NONE, 3.0);
    assert_eq!(history(&app), h0 + 2, "1.1 s of quiet: a new run");
    app.session.edit_prefs(|p| p.tools.nudge_bundle_pause_ms = 5000);
    press_at(&mut app, egui::Key::ArrowRight, egui::Modifiers::NONE, 7.0);
    assert_eq!(history(&app), h0 + 2, "4 s with a 5 s pause: the same run");
    assert_eq!(left_edge(&app, id), 12);
    assert!(app.session.undo());
    assert_eq!(left_edge(&app, id), 10, "Undo takes back the second run only");
}

/// With Bundle Arrow-Key Nudges off every press is a state, as before (#2259).
#[test]
fn every_press_is_a_step_with_bundling_off() {
    let mut app = app_with_layer();
    let id = app.session.active().unwrap().active_layer.unwrap();
    app.session.edit_prefs(|p| p.tools.bundle_nudges = false);
    let h0 = history(&app);
    for _ in 0..5 {
        assert!(press(&mut app, egui::Key::ArrowRight, egui::Modifiers::NONE));
    }
    assert_eq!(history(&app), h0 + 5);
    assert!(app.session.undo());
    assert_eq!(left_edge(&app, id), 12, "one pixel back");
    // Turning it back on bundles the next run, which doesn't swallow the earlier steps.
    app.session.edit_prefs(|p| p.tools.bundle_nudges = true);
    for _ in 0..3 {
        press(&mut app, egui::Key::ArrowRight, egui::Modifiers::NONE);
    }
    assert_eq!(history(&app), h0 + 5);
    assert_eq!(left_edge(&app, id), 15);
}

/// Any other change to the document ends the run: another edit, Undo, a different layer
/// selected, a save.
#[test]
fn other_changes_end_the_run() {
    let mut app = app_with_layer();
    let a = app.session.active().unwrap().active_layer.unwrap();
    let tap = |app: &mut PhotocraftApp| assert!(press(app, egui::Key::ArrowRight, egui::Modifiers::NONE));
    let h0 = history(&app);
    tap(&mut app);
    tap(&mut app);
    assert_eq!(history(&app), h0 + 1);
    // An edit of another kind between two nudges.
    app.session.execute("layer.setProps", json!({"opacity": 0.5})).unwrap();
    tap(&mut app);
    tap(&mut app);
    assert_eq!(history(&app), h0 + 3, "nudges, the edit, nudges");
    // Undo ends it too: the next nudge is a state of its own, not folded into the one before.
    assert!(app.session.undo());
    tap(&mut app);
    assert_eq!(history(&app), h0 + 3);
    assert!(app.session.undo());
    assert_eq!(left_edge(&app, a), 10, "the new run was undone alone");
    // Saving keeps the saved state a step of its own.
    tap(&mut app);
    tap(&mut app);
    let before = history(&app);
    let st = app.session.active_mut().unwrap();
    st.saved_revision = st.revision;
    tap(&mut app);
    assert_eq!(history(&app), before + 1, "a save between two nudges ends the run");
    // Selecting another layer: the same keys now move that one, in a run of its own.
    let b = {
        app.session.execute("layer.new.layer", json!({})).unwrap();
        app.session.active().unwrap().active_layer.unwrap()
    };
    assert_ne!(a, b);
    let before = history(&app);
    app.session
        .edit("paint", |doc, active| {
            doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap().fill_rect(photocraft_geom::Rect::new(0, 0, 4, 4), &[0.0, 1.0, 0.0, 1.0]);
            Ok(())
        })
        .unwrap();
    tap(&mut app);
    app.session.select_layer(a).unwrap();
    tap(&mut app);
    assert_eq!(history(&app), before + 3, "paint, a nudge of the new layer, a nudge of the first");
}

/// ⌥ copies, so ⌥-nudges are never folded into a run (the copy is part of each step).
#[test]
fn alt_nudges_do_not_join_a_run() {
    let mut app = app_with_layer();
    let h0 = history(&app);
    press(&mut app, egui::Key::ArrowRight, egui::Modifiers::NONE);
    press(&mut app, egui::Key::ArrowRight, egui::Modifiers::ALT);
    press(&mut app, egui::Key::ArrowRight, egui::Modifiers::NONE);
    assert_eq!(history(&app), h0 + 3);
    assert_eq!(app.session.active().unwrap().history.undo_label(), Some("Nudge"), "the run after the copy is a state of its own");
}

/// #2259: nudges of the selection outline bundle the same way.
#[test]
fn selection_outline_nudges_bundle_into_one_step() {
    let mut app = app_with_layer();
    app.ui.tool = Tool::Lasso;
    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 = history(&app);
    for i in 0..5 {
        assert!(press_at(&mut app, egui::Key::ArrowRight, egui::Modifiers::NONE, 2.0 + 0.1 * f64::from(i)));
    }
    assert!(press_at(&mut app, egui::Key::ArrowDown, egui::Modifiers::SHIFT, 2.6));
    assert_eq!(sel(&app), photocraft_geom::Rect::new(9, 14, 19, 24));
    assert_eq!(history(&app), h0 + 1);
    assert!(app.session.undo());
    assert_eq!(sel(&app), photocraft_geom::Rect::new(4, 4, 14, 14), "Undo takes the outline back where it was drawn");
    // The layer nudge and the outline nudge are different runs even back to back.
    app.session.execute("select.deselect", json!({})).unwrap();
    app.ui.tool = Tool::Move;
    let h1 = history(&app);
    press_at(&mut app, egui::Key::ArrowRight, egui::Modifiers::NONE, 3.0);
    assert_eq!(history(&app), h1 + 1);
}

/// Arrow keys while editing text must not nudge layers, leaving the keys to the type tool (#5355).
#[test]
fn arrow_keys_ignored_during_text_edit() {
    let mut app = app_with_layer();
    let id = app.session.active().unwrap().active_layer.unwrap();
    let b0 = bounds(&app, id);
    app.ui.text_edit = Some(crate::state::TextEdit { layer: id.0, document: None, caret: 0, anchor: 0, session: "test".into(), created: false, mask: false });
    assert!(!press(&mut app, egui::Key::ArrowRight, egui::Modifiers::NONE));
    assert!(!press(&mut app, egui::Key::ArrowDown, egui::Modifiers::SHIFT));
    assert!(!press(&mut app, egui::Key::ArrowLeft, egui::Modifiers::ALT));
    assert_eq!(bounds(&app, id), b0, "arrow keys do not nudge while editing text");
}

/// #9511: as in Photoshop, a plain arrow nudge is one screen pixel, so zoomed out it moves the
/// layer several document pixels (floor of 1 / zoom: 4 at 25 %, 2 at 50 %, 1 at 66.7 %, 1 at
/// 100 % and above, up to 1600 %); ⇧ is 10 document pixels at any zoom.
#[test]
fn a_nudge_is_one_screen_pixel() {
    let lowest = craft::behavior::canvas::ZoomLadder::min(craft::behavior::kurbo::Size::new(12_000.0, 12_000.0));
    // Below 0.1 % a large document still nudges one screen pixel: 6000 document pixels at a
    // 12,000 × 12,000 px document's lowest zoom, and the ladder's f32 rungs (0.1 %, 0.2 %) give
    // whole steps (1000, not 999). 0.3333334 is just above a third, off the ladder (pinch, Zoom
    // tool drag): 2.9999994 screen pixels' worth floors to 2, while the ladder's own third gives 3.
    let rows: [(f64, i32); 12] = [
        (0.25, 4),
        (0.5, 2),
        (2.0 / 3.0, 1),
        (1.0, 1),
        (2.0, 1),
        (16.0, 1),
        (0.002, 500),
        (0.001, 1000),
        (0.0005, 2000),
        (lowest, 6000),
        (0.333_333_4, 2),
        (1.0 / 3.0, 3),
    ];
    for (zoom, step) in rows {
        let mut app = app_with_layer();
        let id = app.session.active().unwrap().active_layer.unwrap();
        app.ui.views[0].zoom = zoom as _;
        assert!(press(&mut app, egui::Key::ArrowRight, egui::Modifiers::NONE));
        assert_eq!(left_edge(&app, id), 8 + step, "→ at {zoom}");
        assert!(press(&mut app, egui::Key::ArrowLeft, egui::Modifiers::SHIFT));
        assert_eq!(left_edge(&app, id), 8 + step - 10, "⇧← at {zoom}");
    }
}