From 0e96f75911754559867b2eb7b403c3460c5931ae Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 30 Nov 2026 06:28:49 -0800 Subject: [PATCH] Undo: Escape in a panel select keeps edits other callers made while it was open A select or slider on the Scope path opens an undo scope, and Escape took the whole scope back with `end_undo_scope(scope, true)`. A command that automation, a script or a plug-in ran while the list was open joins that scope, so Escape also took back that command's edit. The Cancel now notes each command another caller runs between the gesture's own runs. It takes the scope back and runs those commands again, in order, each as its own undo step, and puts back the paint defaults, the active proxy and the remembered colors as those callers left them. The scope keeps its step with every edit in it when a change cannot run again: a change that journaled nothing, journal entries that were changed or removed, a command run in another document, several commands between two of the gesture's runs, or a command other than a paint or stroke command. A command that left the document and its selection as they were keeps its effect and does not run again. Every `EditPath::Scope` select and slider goes through `apply_event`, so the polygon Corner list and the other Scope controls all get the change. --- crates/ui-egui/src/edits.rs | 412 ++++++++++++++++++- crates/ui-egui/src/lib.rs | 4 +- crates/ui-egui/src/panels/transform/tests.rs | 39 ++ 3 files changed, 443 insertions(+), 12 deletions(-) diff --git a/crates/ui-egui/src/edits.rs b/crates/ui-egui/src/edits.rs index 9eb89f448e..ac5fc8e80a 100644 --- a/crates/ui-egui/src/edits.rs +++ b/crates/ui-egui/src/edits.rs @@ -1,11 +1,109 @@ //! Applying the edits a framework component reports to the document, one undo step per gesture. +use std::sync::Arc; + use craft::behavior::{CancelReason, EditEvent, GestureId}; use craft::ui::contract::EditResponse; +use vectorcraft_doc::{Document, Selection}; +use vectorcraft_engine::UndoScope; +use vectorcraft_engine::journal::JournalEntry; use crate::VectorcraftApp; use crate::panels::Live; +/// The undo scope of an [`EditPath::Scope`] gesture in progress, with the commands that other +/// callers (automation, a script, a plug-in) ran in it. +pub(crate) struct OpenScope { + scope: UndoScope, + /// The scope's document ([`vectorcraft_engine::DocState::uid`]). + uid: Option, + /// The journal's length when the scope began. + start: usize, + /// The journal's length and [`vectorcraft_engine::Journal::rewrites`] after the gesture's last + /// run, or when the scope began. + journaled: (usize, u64), + /// The document and its selection as the gesture's last run, or the scope's start, left them. + seen: Option<(Arc, Selection)>, + /// The commands other callers ran in the scope that changed its document or selection, in order. + others: Vec, + /// Whether a change landed in the scope that a cancel cannot run again. That is the case for: + /// - a change that journaled nothing; + /// - journal entries changed, removed or already let go; + /// - a command run while another document was active; + /// - several commands between two of the gesture's runs that changed the document; + /// - a command that [`replayable_entry`] does not list. + unplayable: bool, +} + +/// The journal's length and [`vectorcraft_engine::Journal::rewrites`]. +fn journal_mark(app: &VectorcraftApp) -> (usize, u64) { + (app.session.journal.len(), app.session.journal.rewrites()) +} + +/// The document with [`vectorcraft_engine::DocState::uid`] `uid` and its selection, as they are now. +fn doc_state(app: &VectorcraftApp, uid: Option) -> Option<(Arc, Selection)> { + let st = app.session.documents().iter().find(|st| Some(st.uid) == uid)?; + Some((st.doc.clone(), st.selection.clone())) +} + +/// Whether a cancel may run journal entry `command` again: a paint or stroke command, whose +/// effects besides the document are the paint state that [`cancel_scope`] puts back after the +/// replays. A scope with any other command run in it keeps every edit. +fn replayable_entry((command, _): &JournalEntry) -> bool { + matches!( + command.as_str(), + "paint.setFill" + | "paint.setStroke" + | "paint.swap" + | "paint.default" + | "paint.toggleActive" + | "stroke.set" + | "stroke.setAdvanced" + | "stroke.weightUp" + | "stroke.weightDown" + ) +} + +/// Notes in the scope of gesture `id` the command another caller ran since the gesture's last run +/// when it changed the scope's document or selection, and any change that cannot run again +/// ([`OpenScope::unplayable`]). A command that left the document and its selection as they were +/// keeps its effect through a cancel and is not run again; when it is not one [`replayable_entry`] +/// lists and an earlier command is to run again, the scope keeps every edit. +fn note_other_edits(app: &mut VectorcraftApp, id: &GestureId) { + let Some((uid, (len, rewrites))) = app.edit_scopes.get(id).map(|open| (open.uid, open.journaled)) else { return }; + let (now_len, now_rewrites) = journal_mark(app); + let entries = app.session.journal.since(len).to_vec(); + let elsewhere = app.session.active().map(|st| st.uid) != uid; + let now = doc_state(app, uid); + let Some(open) = app.edit_scopes.get_mut(id) else { return }; + let changed = match (&open.seen, &now) { + (Some((a, sa)), Some((b, sb))) => !(Arc::ptr_eq(a, b) || **a == **b) || sa != sb, + (None, None) => false, + _ => true, + }; + let rewritten = now_rewrites != rewrites || now_len < len || entries.len() != now_len - len; + let ran = entries.len(); + let unlisted = entries.iter().any(|entry| !replayable_entry(entry)); + if rewritten || (unlisted && (changed || !open.others.is_empty())) || (changed && (ran != 1 || elsewhere)) { + open.unplayable = true; + } else if changed { + open.others.extend(entries); + } + open.journaled = (now_len, now_rewrites); + open.seen = now; +} + +/// Records in the scope of gesture `id` what the journal, the document and its selection are now. +fn note_own_edit(app: &mut VectorcraftApp, id: &GestureId) { + let Some(uid) = app.edit_scopes.get(id).map(|open| open.uid) else { return }; + let mark = journal_mark(app); + let now = doc_state(app, uid); + if let Some(open) = app.edit_scopes.get_mut(id) { + open.journaled = mark; + open.seen = now; + } +} + /// How a gesture reaches the document. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) enum EditPath { @@ -41,22 +139,32 @@ pub(crate) fn apply_event( match event { EditEvent::Begin { id, .. } => { if scoped && let Some(scope) = app.session.begin_undo_scope() { - app.edit_scopes.insert(*id, scope); + let uid = app.session.active().map(|st| st.uid); + let journaled = journal_mark(app); + let seen = doc_state(app, uid); + app.edit_scopes.insert(*id, OpenScope { scope, uid, start: journaled.0, journaled, seen, others: vec![], unplayable: false }); + } + } + EditEvent::Update { id, value, .. } => { + if scoped { + note_other_edits(app, id); + apply(app, value, Live::Released); + note_own_edit(app, id); + } else { + apply(app, value, Live::Dragging); } } - EditEvent::Update { value, .. } => apply(app, value, if scoped { Live::Released } else { Live::Dragging }), EditEvent::Commit { id, after, .. } => { apply(app, after, Live::Released); - if scoped && let Some(scope) = app.edit_scopes.remove(id) { - app.session.end_undo_scope(scope, false); + if scoped && let Some(open) = app.edit_scopes.remove(id) { + app.session.end_undo_scope(open.scope, false); } } EditEvent::Cancel { id, reason, .. } => { if scoped { - if let Some(scope) = app.edit_scopes.remove(id) { - // A document that moved under the gesture keeps the gesture's edits: a revert - // would take back the change too. - app.session.end_undo_scope(scope, !reason.document_moved()); + note_other_edits(app, id); + if let Some(open) = app.edit_scopes.remove(id) { + cancel_scope(app, open, *reason); } } else { end_preview(app, *reason); @@ -67,13 +175,53 @@ pub(crate) fn apply_event( } } +/// Ends the scope `open` of a gesture that did not commit: the scope's undo step is taken back. +/// The commands other callers ran in the scope then run again, in order, each as its own undo +/// step. The step stays, with every edit in it, when the document moved under the gesture +/// (`reason`), when another change landed in the scope that cannot run again +/// ([`OpenScope::unplayable`]), or when another document is active. Replays leave the session's +/// paint defaults, active proxy, and remembered colors as they were immediately before the cancel. +fn cancel_scope(app: &mut VectorcraftApp, open: OpenScope, reason: CancelReason) { + let elsewhere = app.session.active().map(|st| st.uid) != open.uid; + if reason.document_moved() || open.unplayable || (elsewhere && !open.others.is_empty()) { + app.session.end_undo_scope(open.scope, false); + return; + } + // The session state that [`replayable_entry`]'s commands change besides the document, as the + // other callers left it: the replays leave it as it is now. + let retained = (!open.others.is_empty()).then(|| { + let s = &app.session; + (s.paint.clone(), s.fill_active, s.last_solid, s.last_gradient.clone(), s.recent_colors.clone()) + }); + app.session.end_undo_scope(open.scope, true); + // Entries the cancel left in the journal (commands in other documents) ran outside the scope's + // step and stay as they are. + let mut kept = app.session.journal.since(open.start).to_vec().into_iter().peekable(); + let mut failed = None; + for (command, params) in open.others { + if kept.next_if(|entry| entry.0 == command && entry.1 == params).is_some() { + continue; + } + if let Err(e) = app.replay(&command, params) { + failed.get_or_insert(e); + } + } + if let Some((paint, fill_active, last_solid, last_gradient, recent_colors)) = retained { + let s = &mut app.session; + (s.paint, s.fill_active, s.last_solid, s.last_gradient, s.recent_colors) = (paint, fill_active, last_solid, last_gradient, recent_colors); + } + if let Some(e) = failed { + app.status(e); + } +} + /// Ends the undo scopes of the gestures whose component went away while they ran /// (`FrameOutput::removed_gestures`), whose `Cancel` never comes: what they applied stays, as one /// step, as a removed preview keeps what it showed. pub(crate) fn end_removed(app: &mut VectorcraftApp, removed: &[GestureId]) { for id in removed { - if let Some(scope) = app.edit_scopes.remove(id) { - app.session.end_undo_scope(scope, false); + if let Some(open) = app.edit_scopes.remove(id) { + app.session.end_undo_scope(open.scope, false); } } } @@ -186,4 +334,248 @@ mod tests { }); assert_eq!(phases.take(), [Live::Released]); } + + /// Object `id`'s stroke and fill, in the document with [`vectorcraft_engine::DocState::uid`] `uid`. + fn stroke_and_fill(app: &VectorcraftApp, uid: u64, id: u64) -> (String, String) { + let st = app.session.documents().iter().find(|st| st.uid == uid).unwrap(); + let n = st.doc.node(vectorcraft_doc::NodeId(id)).unwrap(); + (format!("{:?}", n.appearance.stroke()), format!("{:?}", n.appearance.fill())) + } + + /// Feeds `events` of a stroke gesture on the Scope path. Each value sets the weight and a dash + /// of that length, in two commands. + fn feed_stroke(app: &mut VectorcraftApp, events: &[EditEvent]) { + for event in events { + apply_event(app, event, &EditPath::Scope, &mut |app, v, _| { + app.run("stroke.set", json!({"weight": v})).unwrap(); + app.run("stroke.set", json!({"dash": [v, v]})).unwrap(); + }); + } + } + + /// A rectangle's app, its document and the rectangle. + fn rectangle_app() -> (VectorcraftApp, u64, u64) { + let mut a = app(); + let id = a.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap()["id"].as_u64().unwrap(); + let uid = a.session.active().unwrap().uid; + (a, uid, id) + } + + /// An Escape that ends gesture 1. + fn escape() -> EditEvent { + cancel(1, CancelReason::Escape) + } + + /// A canceled scope gesture takes back its own edits and keeps a paint command another caller + /// ran meanwhile, as that command's own undo step, with the paint defaults it left. + #[test] + fn a_canceled_scope_keeps_an_edit_made_by_another_command_meanwhile() { + let red = json!({"color": "#ff0000"}); + + let (mut a, uid, id) = rectangle_app(); + let (stroke, fill) = stroke_and_fill(&a, uid, id); + let base = steps(&a); + feed_stroke(&mut a, &[begin(1), update(1, 4.0)]); + a.run("paint.setFill", red.clone()).unwrap(); + let filled = stroke_and_fill(&a, uid, id).1; + assert_ne!(filled, fill); + feed_stroke(&mut a, &[update(1, 6.0), escape()]); + assert_eq!(stroke_and_fill(&a, uid, id), (stroke.clone(), filled.clone()), "the stroke goes back and the fill stays"); + assert_eq!(steps(&a), base + 1, "the fill is one step"); + a.run("edit.undo", json!({})).unwrap(); + assert_eq!(stroke_and_fill(&a, uid, id), (stroke.clone(), fill.clone()), "Undo takes back the fill"); + + // A swap changes both the object's paints and the defaults, each once. + let (mut a, uid, id) = rectangle_app(); + assert!(a.session.prefs.new_art_basic); + let n = a.session.active().unwrap().doc.node(vectorcraft_doc::NodeId(id)).unwrap(); + let swapped = (n.appearance.fill_paint(), n.appearance.stroke_paint()); + let base = steps(&a); + feed_stroke(&mut a, &[begin(1), update(1, 4.0)]); + let defaults = a.session.paint.clone(); + a.run("paint.swap", json!({})).unwrap(); + let swapped_defaults = a.session.paint.clone(); + assert_eq!(swapped_defaults.fill, defaults.stroke); + assert_eq!(swapped_defaults.stroke, defaults.fill); + feed_stroke(&mut a, &[escape()]); + assert_eq!(a.session.paint, swapped_defaults, "the defaults keep the other caller's swap"); + let n = a.session.active().unwrap().doc.node(vectorcraft_doc::NodeId(id)).unwrap(); + assert_eq!((n.appearance.stroke_paint(), n.appearance.fill_paint()), swapped, "the object's paints are swapped once"); + assert_eq!(steps(&a), base + 1, "the swap is one step"); + a.run("edit.undo", json!({})).unwrap(); + assert_eq!(stroke_and_fill(&a, uid, id), (stroke.clone(), fill.clone()), "Undo takes back the object's swap"); + + // A later default-only color stays ahead of the replayed fill in Recent Colors. + let (mut a, uid, id) = rectangle_app(); + feed_stroke(&mut a, &[begin(1), update(1, 4.0)]); + a.run("paint.setFill", red.clone()).unwrap(); + feed_stroke(&mut a, &[update(1, 6.0)]); + let filled = stroke_and_fill(&a, uid, id).1; + a.run("paint.setStroke", json!({"color": "#0000ff", "defaultsOnly": true})).unwrap(); + let defaults = a.session.paint.clone(); + let recent = a.run("paint.recent", json!({})).unwrap(); + let fill_active = a.session.fill_active; + feed_stroke(&mut a, &[escape()]); + assert_eq!(a.session.paint, defaults, "the later defaults stay"); + assert_eq!(a.session.fill_active, fill_active, "the later proxy focus stays"); + assert_eq!(a.run("paint.recent", json!({})).unwrap(), recent, "the last paints and recent color order stay"); + assert_eq!(stroke_and_fill(&a, uid, id), (stroke, filled), "the stroke goes back and the fill stays"); + } + + /// A canceled scope gesture keeps its step with every edit in it when another caller ran a + /// command that cannot run again, or changed the selection without a command. + #[test] + fn a_canceled_scope_keeps_its_step_after_an_edit_that_cannot_run_again() { + // Cut also copies the gesture's stroke to the clipboard, so the scope keeps every edit. + let (mut a, uid, id) = rectangle_app(); + let base = steps(&a); + feed_stroke(&mut a, &[begin(1), update(1, 4.0)]); + a.run("edit.cut", json!({})).unwrap(); + let copied = format!("{:?}", a.session.clipboard.nodes); + feed_stroke(&mut a, &[escape()]); + assert!(a.session.documents().iter().find(|st| st.uid == uid).unwrap().doc.node(vectorcraft_doc::NodeId(id)).is_none()); + assert_eq!(format!("{:?}", a.session.clipboard.nodes), copied, "Cut keeps the clipboard it left"); + assert_eq!(steps(&a), base + 1, "Cut keeps the scope's step"); + + // A change that journaled nothing: the step keeps every edit. + let (mut a, uid, id) = rectangle_app(); + let base = steps(&a); + feed_stroke(&mut a, &[begin(1), update(1, 4.0)]); + let stroked = stroke_and_fill(&a, uid, id).0; + a.session.active_mut().unwrap().selection = Default::default(); + feed_stroke(&mut a, &[escape()]); + assert_eq!(stroke_and_fill(&a, uid, id).0, stroked, "the stroke stays"); + assert_eq!(steps(&a), base + 1); + } + + /// A command another caller ran in another document stays as it is through a canceled scope + /// gesture. A cancel with another document active keeps the scope's step with every edit in it. + #[test] + fn a_canceled_scope_with_another_document() { + // A command that opened another document leaves the scope's document as it was: the stroke + // goes back and the new document stays open. + let (mut a, uid, id) = rectangle_app(); + let stroke = stroke_and_fill(&a, uid, id).0; + feed_stroke(&mut a, &[begin(1), update(1, 4.0)]); + a.run("file.new", json!({"width": 50, "height": 50})).unwrap(); + feed_stroke(&mut a, &[escape()]); + assert_eq!(a.session.documents().len(), 2, "the new document stays open"); + assert_eq!(stroke_and_fill(&a, uid, id).0, stroke, "the stroke goes back"); + + // A command run in the scope's document, then another document active at the cancel: the + // step keeps every edit. + assert!(a.session.set_active(0)); + feed_stroke(&mut a, &[begin(1), update(1, 6.0)]); + a.run("paint.setFill", json!({"color": "#ff0000"})).unwrap(); + feed_stroke(&mut a, &[update(1, 7.0)]); + let edited = stroke_and_fill(&a, uid, id); + assert!(a.session.set_active(1)); + feed_stroke(&mut a, &[escape()]); + assert_eq!(stroke_and_fill(&a, uid, id), edited, "the stroke and the fill stay"); + } + + /// A command another caller ran that left the document as it was keeps its effect through a + /// canceled scope gesture and does not run again. + #[test] + fn a_canceled_scope_runs_a_command_that_left_the_document_as_it_was_once() { + let (mut a, uid, id) = rectangle_app(); + let (stroke, fill) = stroke_and_fill(&a, uid, id); + let base = steps(&a); + let fill_active = a.session.fill_active; + feed_stroke(&mut a, &[begin(1), update(1, 4.0)]); + a.run("paint.toggleActive", json!({})).unwrap(); + feed_stroke(&mut a, &[escape()]); + assert_eq!(a.session.fill_active, !fill_active, "the toggle stays"); + assert_eq!(stroke_and_fill(&a, uid, id), (stroke.clone(), fill), "the stroke goes back"); + assert_eq!(steps(&a), base); + + // A later toggle keeps its effect when an earlier fill would otherwise run again. + let (mut a, uid, id) = rectangle_app(); + feed_stroke(&mut a, &[begin(1), update(1, 4.0)]); + a.run("paint.setFill", json!({"color": "#ff0000"})).unwrap(); + feed_stroke(&mut a, &[update(1, 6.0)]); + let filled = stroke_and_fill(&a, uid, id).1; + let fill_active = a.session.fill_active; + a.run("paint.toggleActive", json!({})).unwrap(); + feed_stroke(&mut a, &[escape()]); + assert_eq!(a.session.fill_active, !fill_active, "the later toggle stays"); + assert_eq!(stroke_and_fill(&a, uid, id), (stroke, filled), "the stroke goes back and the fill stays"); + } + + /// A batch another caller ran keeps the canceled scope gesture's step with every edit in it, and + /// none of its commands runs again. + #[test] + fn a_canceled_scope_keeps_its_step_after_a_batch() { + let red = json!({"color": "#ff0000"}); + // A batch can change the document and state outside its undo step: every edit stays. + let (mut a, uid, id) = rectangle_app(); + let fill = stroke_and_fill(&a, uid, id).1; + let base = steps(&a); + let fill_active = a.session.fill_active; + feed_stroke(&mut a, &[begin(1), update(1, 4.0)]); + a.run( + "command.batch", + json!({"commands": [ + {"command": "paint.toggleActive", "params": {}}, + {"command": "paint.setFill", "params": {"color": "#ff0000", "focus": false}} + ]}), + ) + .unwrap(); + let edited = stroke_and_fill(&a, uid, id); + assert_ne!(edited.1, fill); + feed_stroke(&mut a, &[escape()]); + assert_eq!(a.session.fill_active, !fill_active, "the batch's toggle stays"); + assert_eq!(stroke_and_fill(&a, uid, id), edited, "the stroke and fill stay"); + assert_eq!(steps(&a), base + 1, "the scope keeps its step"); + + // A batch that edits two documents and returns to the first runs only once. + let (mut a, uid, id) = rectangle_app(); + a.run("file.new", json!({"width": 50, "height": 50})).unwrap(); + let other = a.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 5, "height": 5})).unwrap()["id"].as_u64().unwrap(); + let position = + |a: &VectorcraftApp| a.session.documents()[1].doc.node(vectorcraft_doc::NodeId(other)).unwrap().geometric_bounds().unwrap().center(); + let before = position(&a); + assert!(a.session.set_active(0)); + let base = steps(&a); + feed_stroke(&mut a, &[begin(1), update(1, 4.0)]); + a.run( + "command.batch", + json!({"commands": [ + {"command": "paint.setFill", "params": red.clone()}, + {"command": "document.activate", "params": {"index": 1}}, + {"command": "object.move", "params": {"dx": 7, "dy": 0}}, + {"command": "document.activate", "params": {"index": 0}} + ]}), + ) + .unwrap(); + let moved = position(&a); + assert_eq!(moved.x, before.x + 7.0); + let edited = stroke_and_fill(&a, uid, id); + let journaled = a.session.journal.len(); + feed_stroke(&mut a, &[escape()]); + assert_eq!(position(&a), moved, "the other document's object moves once"); + assert_eq!(stroke_and_fill(&a, uid, id), edited, "the scope keeps every edit"); + assert_eq!(steps(&a), base + 1); + assert_eq!(a.session.journal.len(), journaled, "the batch stays in the journal once"); + } + + /// A canceled scope gesture with no other command run in it is taken back with no step. After + /// another caller removed journal entries (a canceled undo group), the gesture goes on and its + /// Escape runs nothing again. + #[test] + fn a_canceled_scope_alone_or_after_removed_entries() { + let (mut a, uid, id) = rectangle_app(); + let original = stroke_and_fill(&a, uid, id); + let base = steps(&a); + feed_stroke(&mut a, &[begin(1), update(1, 4.0), escape()]); + assert_eq!(stroke_and_fill(&a, uid, id), original, "Escape takes the stroke back"); + assert_eq!(steps(&a), base, "and records no step"); + + let (mut a, _, _) = rectangle_app(); + feed_stroke(&mut a, &[begin(1), update(1, 4.0)]); + a.run("edit.undoGroup.end", json!({"cancel": true})).unwrap(); + let journaled = a.session.journal.len(); + feed_stroke(&mut a, &[update(1, 5.0), escape()]); + assert_eq!(a.session.journal.len(), journaled + 2, "the journal holds the gesture's two commands"); + } } diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 3cd948fce0..69e7ffa920 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -115,7 +115,7 @@ use craft::host::Host; use craft::ui::runtime::overlay::{self, OpenFilter}; use vectorcraft_engine::cmd::fileio; use vectorcraft_engine::file_access::{self, AutomationRoots}; -use vectorcraft_engine::{Session, UndoScope, ViewInfo}; +use vectorcraft_engine::{Session, ViewInfo}; pub use settings::{AppSettings, from_app, parse_legacy}; pub use state::{UiState, View}; @@ -277,7 +277,7 @@ pub struct VectorcraftApp { pub(crate) document_setup_source: Option<(u64, u64)>, /// The undo step each value gesture in progress edits under: a scrub or a slider drag is one /// step however many values it sets. - pub(crate) edit_scopes: HashMap, + pub(crate) edit_scopes: HashMap, /// The parameter dialog of a command, while one is open. pub(crate) command_dialog: dialogs::command::CommandDialog, /// The open dialogs of the Effect menu. diff --git a/crates/ui-egui/src/panels/transform/tests.rs b/crates/ui-egui/src/panels/transform/tests.rs index a5f37234e1..fbab65a461 100644 --- a/crates/ui-egui/src/panels/transform/tests.rs +++ b/crates/ui-egui/src/panels/transform/tests.rs @@ -1141,3 +1141,42 @@ fn polygon_sides_slider_drag_is_one_undo_step() { assert_eq!(polygon_sides(&p.app()), 6); assert_eq!(undo_depth(&p.app()), depth, "a canceled drag records no undo step"); } + +/// The selected live polygon's corner kind and fill. +fn polygon_corner_and_fill(app: &VectorcraftApp) -> (Option, String) { + let n = first_selected(app).unwrap(); + (crate::panels::corner_style(app, &n).1, format!("{:?}", n.appearance.fill())) +} + +/// Escape in the polygon's open Corner list puts back the kind a highlighted row previewed and records no undo +/// step. A fill set through automation while the list is open stays after Escape, as its own undo step, which Undo +/// then takes back. +#[test] +fn escape_in_the_corner_list_keeps_an_edit_made_by_automation_meanwhile() { + use craft::automation::{ControlRequest, Reply}; + use craft::ui::contract::Role; + use egui::{Key, Modifiers}; + let mut p = Panel::new(polygon_app(6), show); + let (kind, fill) = polygon_corner_and_fill(&p.app()); + let depth = undo_depth(&p.app()); + + p.click_role("Corner", Role::COMBO_BOX); + p.key(Key::ArrowDown, Modifiers::NONE); + assert_ne!(polygon_corner_and_fill(&p.app()).0, kind, "the highlighted row previews its kind"); + p.key(Key::Escape, Modifiers::NONE); + assert_eq!(polygon_corner_and_fill(&p.app()), (kind, fill.clone()), "Escape puts the kind back"); + assert_eq!(undo_depth(&p.app()), depth, "Escape records no undo step"); + + p.click_role("Corner", Role::COMBO_BOX); + p.key(Key::ArrowDown, Modifiers::NONE); + let (req, _) = ControlRequest::new("engine.execute", json!({"command": "paint.setFill", "params": {"color": "#ff0000"}})); + let Reply::Done(reply) = crate::control::handle(&mut p.app(), p.ctx(), &req) else { panic!("expected an immediate reply") }; + assert_eq!(reply["ok"], true, "{reply}"); + let red = polygon_corner_and_fill(&p.app()).1; + assert_ne!(red, fill, "automation sets the fill"); + p.key(Key::Escape, Modifiers::NONE); + assert_eq!(polygon_corner_and_fill(&p.app()), (kind, red), "Escape puts the kind back and keeps the fill"); + assert_eq!(undo_depth(&p.app()), depth + 1, "the fill is one undo step"); + p.app().run("edit.undo", json!({})).unwrap(); + assert_eq!(polygon_corner_and_fill(&p.app()), (kind, fill), "Undo takes back the fill"); +}