Move tool: History names its steps Move, Nudge and Duplicate as the reference does (#11383)

An Alt+drag copy of a layer is now one step named "Move", arrow-key nudges of the selected layers are named "Nudge" (one per press, or one per bundled run), and an Alt+drag copy of selected pixels is named "Duplicate". Alt+arrow keeps "Duplicate + Move". The Bundle Arrow-Key Nudges preference and its default are unchanged.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Trevor BrownandClaude Fable 5.1 committed 2026-11-05 05:59:44 -05:00
1 parent 98bcbfab90
commit cd203aa531
8 files changed
+191 -21

No files matched your search

+8 -4
View File
@@ -243,6 +243,8 @@ pub struct Floating {
/// The piece has been moved at least once, even if it is back where it was cut (nudged away
/// and back): its selection is the moved pixels' then, as each of Photoshop's nudges leaves it.
pub moved: bool,
/// History step name when dropped; `None` is "Move". The Move tool names a ⌥-drag copy "Duplicate".
pub step_name: Option<String>,
}
/// The floating piece of `st`, if it is still valid (the document hasn't changed under it).
@@ -289,12 +291,13 @@ pub fn locked_for_float(doc: &Document, l: &photocraft_doc::Layer) -> bool {
fn float(s: &mut Session, p: &Value) -> Result<Value> {
let (dx, dy) = (int_param(p, "dx"), int_param(p, "dy"));
let copy = p.get("copy").and_then(Value::as_bool).unwrap_or(false);
let step_name = p.get("stepName").and_then(Value::as_str).map(str::to_string);
let background = s.tools.background;
let st = s.active_mut().ok_or(EngineError::NoDocument)?;
if floating(st).is_none() {
let layer = st.active_layer.ok_or_else(|| EngineError::Other("no active layer".into()))?;
let parts = CutParts::new(&st.doc, layer, copy, background, crate::channel_cmds::target_of(p) == crate::channel_cmds::Target::Mask)?;
st.floating = Some(Floating { layer, offset: (0, 0), revision: st.revision, parts: Arc::new(parts), moved: false });
st.floating = Some(Floating { layer, offset: (0, 0), revision: st.revision, parts: Arc::new(parts), moved: false, step_name });
}
let f = st.floating.as_mut().ok_or(EngineError::NoDocument)?;
f.offset = (f.offset.0.saturating_add(dx), f.offset.1.saturating_add(dy));
@@ -311,20 +314,21 @@ pub fn drop_floating(s: &mut Session) -> Result<Value> {
return Ok(json!({"dropped": false}));
};
st.floating = None;
let name = f.step_name.clone().unwrap_or_else(|| "Move".to_string());
let (dx, dy) = f.offset;
if (dx, dy) == (0, 0) {
// Back where it was cut: the pixels are as they were. A piece that never moved (a click)
// changes nothing; one nudged away and back keeps the moved pixels' selection (#7538).
if let Some(outline) = f.parts.outline().filter(|_| f.moved) {
let outline = outline.clone();
s.edit("Move", |doc, _| {
s.edit(&name, |doc, _| {
doc.selection = Some(outline);
Ok(())
})?;
}
return Ok(json!({"dropped": true, "offset": [0, 0]}));
}
s.edit("Move", |doc, _| {
s.edit(&name, |doc, _| {
f.parts.place(doc, dx, dy)?;
let sel = f.parts.outline().or(doc.selection.as_ref());
doc.selection = sel.map(|sel| photocraft_algo::resample::translate_surface(sel, dx, dy));
@@ -374,7 +378,7 @@ pub fn specs() -> Vec<CommandSpec> {
label: "Float Selection",
menu: &[],
shortcut: None,
params: r##"{"dx":px=0,"dy":px=0,"copy":bool=false,"target":"pixels"|"mask"="pixels" (mask: float the selected part of the layer's mask, the hole takes the background colour)} → {layer, offset} (cuts the selected pixels of the active layer into a floating piece the first time, or with copy leaves them and floats a duplicate; on the Background the hole takes the background colour; then moves it by whole pixels; dropped by select.drop or any other command, put back by edit.undo)"##,
params: r##"{"dx":px=0,"dy":px=0,"copy":bool=false,"stepName":string="Move" (History name of the step that drops the piece),"target":"pixels"|"mask"="pixels" (mask: float the selected part of the layer's mask, the hole takes the background colour)} → {layer, offset} (cuts the selected pixels of the active layer into a floating piece the first time, or with copy leaves them and floats a duplicate; on the Background the hole takes the background colour; then moves it by whole pixels; dropped by select.drop or any other command, put back by edit.undo)"##,
enabled: can_float,
journal: true,
run: float,
+11 -1
View File
@@ -36,9 +36,19 @@ pub(crate) fn moves_selected_pixels_with(app: &PhotocraftApp, tool: Tool) -> boo
/// time, or with `copy` (⌥) floats a duplicate and leaves them; `copy` on a piece already floating
/// drops it where it is and floats a copy of it. False when it failed (reported in the status bar).
pub(crate) fn float_selected(app: &mut PhotocraftApp, copy: bool, dx: f64, dy: f64) -> bool {
float_selected_as(app, copy, dx, dy, None)
}
/// The start of a Move-tool drag of the selected pixels. An ⌥-drag copy is one "Duplicate" step,
/// as in Photoshop (#11383); a plain drag is "Move".
pub(crate) fn float_selected_for_drag(app: &mut PhotocraftApp, copy: bool) -> bool {
float_selected_as(app, copy, 0.0, 0.0, copy.then(|| tl!("Duplicate")))
}
fn float_selected_as(app: &mut PhotocraftApp, copy: bool, dx: f64, dy: f64, step_name: Option<&str>) -> bool {
let floating = app.session.active().is_some_and(|st| photocraft_engine::float_cmds::floating(st).is_some());
let dropped = if copy && floating { app.run("select.drop", json!({})) } else { Ok(json!(null)) };
match dropped.and_then(|_| app.run("select.float", json!({"dx": dx, "dy": dy, "copy": copy}))) {
match dropped.and_then(|_| app.run("select.float", json!({"dx": dx, "dy": dy, "copy": copy, "stepName": step_name}))) {
Ok(_) => {
crate::snap_ui::follow_cut(app);
true
+1 -1
View File
@@ -272,7 +272,7 @@ fn handle_move_pointer(app: &mut PhotocraftApp, ev: ToolEvent, tool: Tool, mods:
// With a selection: cut the selected pixels (⌥ copies them) and drag them as a floating
// piece, from anywhere, as a marquee ⌘-drag does.
if crate::move_ui::moves_selected_pixels_with(app, tool) {
if crate::move_ui::float_selected(app, mods.alt, 0.0, 0.0) {
if crate::move_ui::float_selected_for_drag(app, mods.alt) {
let mut d = Drag::new(tool, [x, y], vec![[x, y, pressure as f64]], mods, false);
d.sel_move = Some(true);
app.drag = Some(d);
+3
View File
@@ -816,6 +816,9 @@ mod save_identity_tests;
#[cfg(test)]
mod move_auto_select_tests;
#[cfg(test)]
mod move_history_names_tests;
#[cfg(test)]
mod new_group_button_tests;
@@ -0,0 +1,145 @@
//! #11383: the Move tool's History steps have the reference application's names: a drag and an
//! Alt+drag copy of a layer are "Move", arrow-key nudges are "Nudge" (one per press, or one per
//! run with Bundle Arrow-Key Nudges on), an Alt+drag of selected pixels is "Duplicate". An
//! Alt+arrow nudge keeps "Duplicate + Move" (not measured).
use photocraft_geom::Rect;
use serde_json::json;
use crate::PhotocraftApp;
use crate::canvas::ToolEvent;
use crate::state::Tool;
/// A 64x64 document with one painted layer (8..24)^2, selected, and the Move tool active.
fn app() -> 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(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) -> Rect {
app.session.active().unwrap().doc.layer(id).unwrap().surface().unwrap().content_bounds()
}
fn drag(app: &mut PhotocraftApp, from: [f64; 2], by: [f64; 2], mods: egui::Modifiers) {
crate::canvas::tool_event(app, ToolEvent::Down { x: from[0], y: from[1], pressure: 1.0 }, mods);
let mid = [from[0] + by[0] / 2.0, from[1] + by[1] / 2.0];
crate::canvas::tool_event(app, ToolEvent::Move { x: mid[0], y: mid[1], pressure: 1.0 }, mods);
let end = [from[0] + by[0], from[1] + by[1]];
crate::canvas::tool_event(app, ToolEvent::Move { x: end[0], y: end[1], pressure: 1.0 }, mods);
crate::canvas::tool_event(app, ToolEvent::Up { x: end[0], y: end[1] }, mods);
}
fn press(app: &mut PhotocraftApp, key: egui::Key, mods: egui::Modifiers, time: f64) {
let ctx = egui::Context::default();
ctx.input_mut(|i| {
i.time = time;
i.modifiers = mods;
i.events.push(egui::Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers: mods });
});
assert!(crate::move_mods::arrow_keys(app, &ctx));
}
fn history(app: &PhotocraftApp) -> usize {
app.session.active().unwrap().history.past_len()
}
fn newest(app: &PhotocraftApp) -> Option<String> {
app.session.active().unwrap().history.entries().pop()
}
/// The five presses of the reference measurement: ->, Shift+->, down, Shift+down, <-.
fn five_presses(app: &mut PhotocraftApp) {
let keys = [
(egui::Key::ArrowRight, egui::Modifiers::NONE),
(egui::Key::ArrowRight, egui::Modifiers::SHIFT),
(egui::Key::ArrowDown, egui::Modifiers::NONE),
(egui::Key::ArrowDown, egui::Modifiers::SHIFT),
(egui::Key::ArrowLeft, egui::Modifiers::NONE),
];
for (i, (key, mods)) in keys.into_iter().enumerate() {
press(app, key, mods, 1.0 + 0.1 * i as f64);
}
}
#[test]
fn a_drag_of_a_layer_is_one_move_step() {
let mut app = app();
let id = app.session.active().unwrap().active_layer.unwrap();
let h0 = history(&app);
drag(&mut app, [16.0, 16.0], [10.0, 4.0], egui::Modifiers::NONE);
assert_eq!(bounds(&app, id), Rect::new(18, 12, 34, 28));
assert_eq!(history(&app), h0 + 1);
assert_eq!(newest(&app).as_deref(), Some("Move"));
}
#[test]
fn an_alt_drag_copy_of_a_layer_is_one_move_step() {
let mut app = app();
let orig = app.session.active().unwrap().active_layer.unwrap();
let h0 = history(&app);
drag(&mut app, [16.0, 16.0], [20.0, 10.0], egui::Modifiers::ALT);
let copy = app.session.active().unwrap().active_layer.unwrap();
assert_ne!(copy, orig, "the copy is selected");
assert_eq!(bounds(&app, copy), Rect::new(28, 18, 44, 34), "at the offset");
assert_eq!(bounds(&app, orig), Rect::new(8, 8, 24, 24), "the original stays");
assert_eq!(history(&app), h0 + 1, "the copy and the move are one step");
assert_eq!(newest(&app).as_deref(), Some("Move"));
}
#[test]
fn five_arrow_presses_with_bundling_off_are_five_nudge_steps() {
let mut app = app();
app.session.edit_prefs(|p| p.tools.bundle_nudges = false);
let id = app.session.active().unwrap().active_layer.unwrap();
let h0 = history(&app);
five_presses(&mut app);
assert_eq!(bounds(&app, id), Rect::new(8 + 1 + 10 - 1, 8 + 1 + 10, 24 + 10, 24 + 11));
assert_eq!(history(&app), h0 + 5);
let entries = app.session.active().unwrap().history.entries();
assert_eq!(&entries[entries.len() - 5..], ["Nudge"; 5]);
}
#[test]
fn five_arrow_presses_with_bundling_on_are_one_nudge_step() {
let mut app = app();
app.session.edit_prefs(|p| p.tools.bundle_nudges = true);
let h0 = history(&app);
five_presses(&mut app);
assert_eq!(history(&app), h0 + 1);
assert_eq!(newest(&app).as_deref(), Some("Nudge"));
}
#[test]
fn an_alt_drag_of_selected_pixels_is_one_duplicate_step() {
let mut app = app();
app.session.execute("select.rect", json!({"x": 8, "y": 8, "width": 16, "height": 16})).unwrap();
let h0 = history(&app);
drag(&mut app, [16.0, 16.0], [20.0, 10.0], egui::Modifiers::ALT);
// The copied piece floats until something drops it.
app.session.execute("select.drop", json!({})).unwrap();
assert_eq!(history(&app), h0 + 1, "the copy and its move are one step");
assert_eq!(newest(&app).as_deref(), Some("Duplicate"));
}
#[test]
fn an_alt_arrow_nudge_keeps_duplicate_plus_move() {
let mut app = app();
let h0 = history(&app);
press(&mut app, egui::Key::ArrowRight, egui::Modifiers::ALT, 1.0);
assert_eq!(history(&app), h0 + 1);
assert_eq!(newest(&app).as_deref(), Some("Duplicate + Move"));
}
+2 -2
View File
@@ -48,8 +48,8 @@ fn tapping_an_arrow_key_ten_times_is_one_history_step() {
assert_eq!(bounds(&h).x0, start.x0 + 10);
assert_eq!(steps(&h), n0 + 1, "ten taps, one state");
let entries = h.state().session.active().unwrap().history.entries();
assert_eq!(entries.last().map(String::as_str), Some("Move"));
assert_eq!(entries.iter().filter(|e| *e == "Move").count(), 1);
assert_eq!(entries.last().map(String::as_str), Some("Nudge"));
assert_eq!(entries.iter().filter(|e| *e == "Nudge").count(), 1);
h.state_mut().session.undo();
assert_eq!(bounds(&h), start, "one Undo takes all ten pixels back");
}
+17 -9
View File
@@ -6,7 +6,8 @@
//! eight directions ([`constrain`]).
//! - ⌥ held when the drag starts duplicates the selected layer(s) (groups included) once the
//! pointer actually moves, and the drag then moves the copies. Duplicate and move land as one
//! history step ("Duplicate + Move"). ⇧⌥ combines both.
//! history step ("Move", as in Photoshop; an ⌥-arrow nudge keeps "Duplicate + Move"). ⇧⌥
//! combines both.
//! - Arrow keys nudge the selected layers 1 px (⇧: 10 px); ⌥ duplicates first. While Free
//! Transform is active they nudge the box instead. A run of nudges is one History state
//! (`nudge_bundle`, #2259).
@@ -34,7 +35,7 @@ const HYSTERESIS_DEG: f64 = 8.0;
/// ⌥-drag duplicates only once the pointer has moved this far (screen points), so an ⌥-click
/// without a drag doesn't make a copy (Photoshop).
const DUPLICATE_THRESHOLD: f64 = 2.0;
/// History label of an ⌥-drag / ⌥-nudge.
/// History label of an ⌥-nudge. An ⌥-drag of a layer is a plain "Move", as in Photoshop (#11383).
pub const DUPLICATE_MOVE_LABEL: &str = "Duplicate + Move";
/// Locks the drag offset `d` to the nearest of `n` evenly spaced directions (0° = right). `prev`
@@ -105,8 +106,8 @@ fn duplicate(app: &mut PhotocraftApp) -> Option<usize> {
}
}
/// Merges every history step after `from` into one "Duplicate + Move" step.
fn fold_history(app: &mut PhotocraftApp, from: usize) {
/// Merges every history step after `from` into one step called `label`.
fn fold_history(app: &mut PhotocraftApp, from: usize, label: &str) {
let Some(st) = app.session.active_mut() else { return };
while st.history.past_len() > from + 1 {
if !st.history.purge_last() {
@@ -114,7 +115,7 @@ fn fold_history(app: &mut PhotocraftApp, from: usize) {
}
}
if st.history.past_len() == from + 1 {
st.history.set_current_label(DUPLICATE_MOVE_LABEL);
st.history.set_current_label(label);
}
}
@@ -185,7 +186,7 @@ pub(crate) fn duplicated(app: &PhotocraftApp) -> bool {
pub fn finish(app: &mut PhotocraftApp) {
let st = std::mem::take(&mut app.move_mods);
if let Some(from) = st.dup_from {
fold_history(app, from);
fold_history(app, from, tl!("Move"));
}
}
@@ -276,11 +277,18 @@ pub fn nudge(app: &mut PhotocraftApp, dx: f64, dy: f64, duplicate_first: bool) {
} else {
crate::nudge_bundle::run(app, "layers", "layer.translate", json!({"dx": dx, "dy": dy}))
};
if let Err(e) = moved {
app.ui.status = e;
match moved {
// The reference names an arrow-key move "Nudge"; a bundled run is one step with that name.
Ok(_) if from.is_none() => {
if let Some(st) = app.session.active_mut() {
st.history.set_current_label(tl!("Nudge"));
}
}
Ok(_) => {}
Err(e) => app.ui.status = e,
}
if let Some(from) = from {
fold_history(app, from);
fold_history(app, from, DUPLICATE_MOVE_LABEL);
}
}
+4 -4
View File
@@ -90,7 +90,7 @@ fn alt_drag_duplicates_and_moves_the_copy_in_one_step() {
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(DUPLICATE_MOVE_LABEL));
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));
@@ -115,7 +115,7 @@ fn alt_drag_keeps_the_later_states_in_non_linear_history() {
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(DUPLICATE_MOVE_LABEL.into());
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");
}
@@ -328,7 +328,7 @@ fn a_run_of_arrow_nudges_is_one_history_step() {
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("Move"));
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());
@@ -434,7 +434,7 @@ fn alt_nudges_do_not_join_a_run() {
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("Move"), "the run after the copy is a state of its own");
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.