From 9c06506b5f10ded5fba2443b0f13e3fc88f1212f Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sat, 21 Nov 2026 03:25:24 -0600 Subject: [PATCH 1/2] Remove: Undo takes back the last waiting stroke With Remove after each stroke off, Edit > Undo (Cmd/Ctrl+Z or any shortcut for it) now takes back the last waiting stroke, one per press, and leaves the document and its history alone; the tint and the status count follow. With nothing waiting it undoes the last document step as before, and Undo stays enabled while strokes wait. Fixes #12544 --- crates/ui-egui/src/menus/invoke.rs | 4 + crates/ui-egui/src/menus/state.rs | 4 + .../src/tools/remove_pending_undo_tests.rs | 87 +++++++++++++++++++ crates/ui-egui/src/tools/retouch_ui.rs | 28 ++++++ 4 files changed, 123 insertions(+) create mode 100644 crates/ui-egui/src/tools/remove_pending_undo_tests.rs diff --git a/crates/ui-egui/src/menus/invoke.rs b/crates/ui-egui/src/menus/invoke.rs index 9b24124092..0ba9451b93 100644 --- a/crates/ui-egui/src/menus/invoke.rs +++ b/crates/ui-egui/src/menus/invoke.rs @@ -151,6 +151,10 @@ fn invoke_early_commands(app: &mut PhotocraftApp, ctx: &egui::Context, id: &str, // where a Pen path was finished (closed, ↩/Esc, another tool) gives the path back to the Pen // rather than deleting it; Redo finishes it again. Step Backward/Forward step the same way. // An open Free Transform or Crop box steps itself first. + // Waiting Remove strokes are gesture state too: Undo takes back the last one first (#12544). + if id == "edit.undo" && crate::retouch_ui::remove_pending_undo_last(app) { + return Some(Ok(json!({"strokes": app.remove_pending.len()}))); + } if let Some(r) = crate::vector_ui::pen_history_step(app, id) { return Some(r.map(|_| Value::Null)); } diff --git a/crates/ui-egui/src/menus/state.rs b/crates/ui-egui/src/menus/state.rs index b391038165..e055e3257d 100644 --- a/crates/ui-egui/src/menus/state.rs +++ b/crates/ui-egui/src/menus/state.rs @@ -21,6 +21,10 @@ pub fn is_enabled(app: &PhotocraftApp, id: &str) -> bool { if let Some(on) = crate::vector_ui::pen_history_enabled(app, id) { return on; } + // Waiting Remove strokes can be taken back even when the document has no History entries (#12544). + if id == "edit.undo" && app.ui.tool == crate::state::Tool::Remove && !app.remove_pending.is_empty() { + return true; + } if id == "edit.undo" && crate::actions::library::edit_undo_label(app).is_some() { return true; } diff --git a/crates/ui-egui/src/tools/remove_pending_undo_tests.rs b/crates/ui-egui/src/tools/remove_pending_undo_tests.rs new file mode 100644 index 0000000000..0f19671c98 --- /dev/null +++ b/crates/ui-egui/src/tools/remove_pending_undo_tests.rs @@ -0,0 +1,87 @@ +//! #12544: with Remove after each stroke off, Cmd+Z (Ctrl+Z) takes back the last waiting stroke, one per press, and +//! leaves the document and its history alone; with nothing waiting it undoes the last document step as before. + +use egui::{Key, Modifiers}; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::canvas::testing; +use crate::canvas::{ToolEvent, tool_event}; +use crate::state::Tool; + +/// A red layer "Empty" over the Background ("New Layer" is the last document step), with the Remove tool collecting +/// strokes. +fn app() -> Box { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 64, "height": 64})).unwrap(); + app.run("edit.fill", json!({"color": "#dc1e1e"})).unwrap(); + app.run("layer.new.layer", json!({"name": "Empty"})).unwrap(); + let background = app.session.active().unwrap().doc.layers[0].id.0; + app.run("layer.select", json!({"layer": background})).unwrap(); + app.ui.tool = Tool::Remove; + app.ui.tool_options.remove_after_each_stroke = false; + testing::shown(&mut app); + app +} + +fn stroke(app: &mut PhotocraftApp, y: f64) { + tool_event(app, ToolEvent::Down { x: 12.0, y, pressure: 1.0 }, Modifiers::NONE); + tool_event(app, ToolEvent::Move { x: 40.0, y, pressure: 1.0 }, Modifiers::NONE); + tool_event(app, ToolEvent::Up { x: 40.0, y }, Modifiers::NONE); +} + +fn steps(app: &PhotocraftApp) -> usize { + app.session.active().unwrap().history.past_len() +} + +fn layers(app: &PhotocraftApp) -> usize { + app.session.active().unwrap().doc.layers.len() +} + +fn undo(app: &mut PhotocraftApp) { + testing::key(app, Key::Z, Modifiers::COMMAND); +} + +#[test] +fn undo_takes_back_one_waiting_stroke_per_press() { + let mut app = app(); + for y in [12.0, 30.0, 48.0] { + stroke(&mut app, y); + } + assert_eq!(app.remove_pending.len(), 3, "three strokes wait"); + let (before, count, journal) = (steps(&app), layers(&app), app.session.journal.len()); + undo(&mut app); + assert_eq!(app.remove_pending.len(), 2); + assert!(app.ui.status.contains('2'), "the status counts what is left: {}", app.ui.status); + assert!(app.remove_trail.is_some(), "the strokes left are still tinted"); + assert_eq!((steps(&app), layers(&app), app.session.journal.len()), (before, count, journal), "the document is untouched"); + undo(&mut app); + assert_eq!(app.remove_pending.len(), 1); + undo(&mut app); + assert!(app.remove_pending.is_empty(), "the last one taken back leaves nothing waiting: {}", app.remove_pending.len()); + assert!(app.remove_trail.is_none(), "and no tint"); + assert_eq!((steps(&app), layers(&app)), (before, count), "still no history change"); +} + +#[test] +fn with_nothing_waiting_undo_steps_back_the_document() { + let mut app = app(); + let (before, count) = (steps(&app), layers(&app)); + undo(&mut app); + assert_eq!(steps(&app), before - 1, "New Layer is undone"); + assert_eq!(layers(&app), count - 1, "the layer Empty is gone"); +} + +#[test] +fn undo_stays_enabled_while_strokes_wait() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 64, "height": 64})).unwrap(); + app.ui.tool = Tool::Remove; + app.ui.tool_options.remove_after_each_stroke = false; + testing::shown(&mut app); + stroke(&mut app, 20.0); + assert_eq!(app.remove_pending.len(), 1); + assert!(crate::menus::is_enabled(&app, "edit.undo"), "a waiting stroke can be taken back"); + undo(&mut app); + assert!(app.remove_pending.is_empty()); +} diff --git a/crates/ui-egui/src/tools/retouch_ui.rs b/crates/ui-egui/src/tools/retouch_ui.rs index 276cafd6d9..c1e8cd3695 100644 --- a/crates/ui-egui/src/tools/retouch_ui.rs +++ b/crates/ui-egui/src/tools/retouch_ui.rs @@ -371,6 +371,30 @@ pub fn remove_pending_cancel(app: &mut PhotocraftApp) -> bool { true } +/// Undo while strokes wait takes back the last one, as the Pen takes back its last point (#12544): the document and +/// its history stay as they are, and the tint is redrawn from the strokes left. The last one taken back leaves nothing +/// waiting. False when none wait, so Undo goes on to the document. +pub fn remove_pending_undo_last(app: &mut PhotocraftApp) -> bool { + if !remove_pending_live(app) { + return false; + } + if app.remove_pending.len() == 1 { + return remove_pending_cancel(app); + } + app.remove_pending.pop(); + if let Some(st) = app.session.active() { + let mut trail = crate::stroke_trail::Trail::new([st.doc.size.width, st.doc.size.height]); + for entry in &app.remove_pending { + let points: Vec<[f64; 3]> = serde_json::from_value(entry["points"].clone()).unwrap_or_default(); + trail.feed_stroke_reach(&points, || photocraft_engine::retouch_cmds::stroke_brush(&app.session.tools.brush, entry)); + } + app.remove_trail = Some(trail); + } + app.ui.status = crate::i18n::fmt(tl!("Strokes to remove: {n} (Enter applies, Esc cancels)"), &[("n", &app.remove_pending.len().to_string())]); + app.ui.status_error = false; + true +} + /// Whether Remove strokes are still waiting: they are dropped when the tool or the document /// changed under them (checked every frame). pub fn remove_pending_live(app: &mut PhotocraftApp) -> bool { @@ -725,3 +749,7 @@ mod clone_stamp_no_source_alert_tests; #[cfg(test)] #[path = "remove_options_bar_labels_tests.rs"] mod remove_options_bar_labels_tests; + +#[cfg(test)] +#[path = "remove_pending_undo_tests.rs"] +mod remove_pending_undo_tests; From b9294d09123690963919e197c8f049402f6e477b Mon Sep 17 00:00:00 2001 From: David Mann Date: Sun, 29 Nov 2026 00:38:37 +0100 Subject: [PATCH 2/2] Remove undo test: the newest stroke is taken back and loses its tint Co-Authored-By: Claude Opus 5.5 --- .../src/tools/remove_pending_undo_tests.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/tools/remove_pending_undo_tests.rs b/crates/ui-egui/src/tools/remove_pending_undo_tests.rs index 0f19671c98..9e3f3cf31c 100644 --- a/crates/ui-egui/src/tools/remove_pending_undo_tests.rs +++ b/crates/ui-egui/src/tools/remove_pending_undo_tests.rs @@ -38,6 +38,14 @@ fn layers(app: &PhotocraftApp) -> usize { app.session.active().unwrap().doc.layers.len() } +/// Whether the waiting-stroke tint covers the middle of the stroke drawn at `y`. +fn tinted(app: &PhotocraftApp, y: f64) -> bool { + app.remove_trail.as_ref().is_some_and(|t| { + let s = f64::from(t.scale()); + t.coverage((26.0 / s) as usize, (y / s) as usize) > 0 + }) +} + fn undo(app: &mut PhotocraftApp) { testing::key(app, Key::Z, Modifiers::COMMAND); } @@ -45,18 +53,25 @@ fn undo(app: &mut PhotocraftApp) { #[test] fn undo_takes_back_one_waiting_stroke_per_press() { let mut app = app(); + // A small brush keeps the three strokes' tints apart. + app.session.tools.brush.size = 6.0; for y in [12.0, 30.0, 48.0] { stroke(&mut app, y); } assert_eq!(app.remove_pending.len(), 3, "three strokes wait"); + assert!([12.0, 30.0, 48.0].iter().all(|&y| tinted(&app, y)), "all three are tinted"); + let queued = app.remove_pending.clone(); let (before, count, journal) = (steps(&app), layers(&app), app.session.journal.len()); undo(&mut app); - assert_eq!(app.remove_pending.len(), 2); + assert_eq!(app.remove_pending.as_slice(), &queued[..2], "the newest stroke is the one taken back"); + assert!(tinted(&app, 12.0) && tinted(&app, 30.0), "the strokes left keep their tint"); + assert!(!tinted(&app, 48.0), "the stroke taken back loses its tint"); assert!(app.ui.status.contains('2'), "the status counts what is left: {}", app.ui.status); assert!(app.remove_trail.is_some(), "the strokes left are still tinted"); assert_eq!((steps(&app), layers(&app), app.session.journal.len()), (before, count, journal), "the document is untouched"); undo(&mut app); - assert_eq!(app.remove_pending.len(), 1); + assert_eq!(app.remove_pending.as_slice(), &queued[..1]); + assert!(tinted(&app, 12.0) && !tinted(&app, 30.0)); undo(&mut app); assert!(app.remove_pending.is_empty(), "the last one taken back leaves nothing waiting: {}", app.remove_pending.len()); assert!(app.remove_trail.is_none(), "and no tint");