Merge #16094 into the merge queue batch

Remove: Undo takes back the last waiting stroke
This commit is contained in:
David Mann committed 2026-11-29 04:41:25 +01:00
commit fb4b7696bc
4 files changed
+138

No files matched your search

+4
View File
@@ -154,6 +154,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));
}
+4
View File
@@ -24,6 +24,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;
}
@@ -0,0 +1,102 @@
//! #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<PhotocraftApp> {
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()
}
/// 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);
}
#[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.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.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");
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());
}
+28
View File
@@ -441,6 +441,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(&mut app.texture_serials, [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 {
@@ -808,6 +832,10 @@ mod clone_stamp_no_source_alert_tests;
#[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;
#[cfg(test)]
#[path = "../smudge_bar_angle_tests.rs"]
mod smudge_bar_angle_tests;