From fca5dc67eae17f87f9fa25cab36c7d6e02f026bd Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 30 Nov 2026 10:04:04 -0800 Subject: [PATCH] Gradient: the stop popover selects only the stop's own swatch and keeps it through renames The Swatches view no longer lets the palette select a swatch by color. When no swatch is the stop's own, nothing is selected, so a spot or global swatch of an unlinked stop's color is not shown as picked and a click on it links the stop. swatch.edit records each rename with the uid of its document. A preview records nothing, a failed command.batch puts the record back as it was, and the session keeps the newest 64 renames. VectorcraftApp::run and the popover take the record, and the popover's remembered pick follows a rename of its swatch in its own document, inside a batch too. The popover tests check the cells the view draws selected through the panel harness. --- crates/engine/src/cmd/fileio/batch.rs | 3 + crates/engine/src/cmd/swatch.rs | 7 + crates/engine/src/lib.rs | 6 + crates/engine/src/session/documents.rs | 1 + crates/engine/src/session/edit.rs | 6 + crates/engine/src/tests/swatches.rs | 58 +++++ crates/ui-egui/src/app/commands.rs | 3 + crates/ui-egui/src/dialogs/gradient_stop.rs | 241 ++++++++++++++++++-- crates/ui-egui/src/tests/panel_harness.rs | 6 + 9 files changed, 307 insertions(+), 24 deletions(-) diff --git a/crates/engine/src/cmd/fileio/batch.rs b/crates/engine/src/cmd/fileio/batch.rs index 93670cefd9..68e03b3228 100644 --- a/crates/engine/src/cmd/fileio/batch.rs +++ b/crates/engine/src/cmd/fileio/batch.rs @@ -25,6 +25,8 @@ pub(super) fn batch(s: &mut Session, p: &Value) -> Result { // file, format and saved state share their data with the open ones until a step changes them. let start = s.docs.clone(); let active = s.active; + // A rollback puts back the swatch renames recorded before the batch, including any its steps pushed out of the record. + let swatch_renames = s.swatch_renames.clone(); s.batch_stash = Some(vec![]); let mut grouped = Vec::with_capacity(cmds.len()); // The Type tool keeps what the steps' Undo and Redo change in it, to put back on a rollback. @@ -38,6 +40,7 @@ pub(super) fn batch(s: &mut Session, p: &Value) -> Result { results } Err(e) => { + s.swatch_renames = swatch_renames; roll_back(s, start, &stash); s.notify_text_tool("batch.rollBack"); s.active = active; diff --git a/crates/engine/src/cmd/swatch.rs b/crates/engine/src/cmd/swatch.rs index 9916ba569c..4e8b25f3f4 100644 --- a/crates/engine/src/cmd/swatch.rs +++ b/crates/engine/src/cmd/swatch.rs @@ -977,6 +977,13 @@ fn swatch_edit(s: &mut Session, p: &Value) -> Result { Ok(relinked) })?; relink.defaults(s)?; + if to != name && !s.previewing { + let uid = s.doc()?.uid; + if s.swatch_renames.len() == crate::MAX_SWATCH_RENAMES { + s.swatch_renames.remove(0); + } + s.swatch_renames.push((uid, name, to.clone())); + } Ok(json!({"name": to, "relinked": relinked})) } diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index fb2115190c..5a78b39871 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -52,6 +52,9 @@ pub use vectorcraft_tools as tools; pub use vectorcraft_doc::MAX_COORD; const _: () = assert!(vectorcraft_doc::MAX_DEPTH == 32, "TOO_DEEP (and its catalog rows) names the cap"); +/// The maximum number of newest swatch renames a session keeps until they are taken. +pub(crate) const MAX_SWATCH_RENAMES: usize = 64; + pub struct Session { docs: Vec, active: Option, @@ -303,6 +306,9 @@ pub struct Session { /// appearance: the documents ([`DocState::uid`]) whose newest history step takes the colors the /// command finally left. pub(crate) batch_paint_docs: Vec, + /// Committed swatch renames as (document uid, old name, new name), in execution order until taken by the frontend. + /// The uid is [`DocState::uid`]; the record is never saved in documents or history. + pub(crate) swatch_renames: Vec<(u64, String, String)>, /// The composite fonts (lowercase names) that the open documents defined when the font /// database was last brought in step with them ([`cmd::compositefonts::sync`]). pub(crate) composite_names: Vec, diff --git a/crates/engine/src/session/documents.rs b/crates/engine/src/session/documents.rs index 8a9eea3b4a..b6461d8e1d 100644 --- a/crates/engine/src/session/documents.rs +++ b/crates/engine/src/session/documents.rs @@ -136,6 +136,7 @@ impl Session { batch_stash: None, batch_steps: None, batch_paint_docs: Vec::new(), + swatch_renames: Vec::new(), composite_names: Vec::new(), composite_names_registered: HashSet::new(), reading: false, diff --git a/crates/engine/src/session/edit.rs b/crates/engine/src/session/edit.rs index d5d62e607c..6fd96e6ffc 100644 --- a/crates/engine/src/session/edit.rs +++ b/crates/engine/src/session/edit.rs @@ -5,6 +5,12 @@ use super::{ use crate::doc_state::{kept_from, objects_only}; impl Session { + /// Takes committed swatch renames as (document uid, old name, new name) in execution order and leaves the record empty. + /// The uid identifies the originating document through [`crate::DocState::uid`]. + pub fn take_swatch_renames(&mut self) -> Vec<(u64, String, String)> { + std::mem::take(&mut self.swatch_renames) + } + /// Run `f` on a mutable copy of the active document. Outside an interaction this records one /// undo step labelled `label`; inside one it just mutates (the interaction commits later). pub fn edit(&mut self, label: &str, f: impl FnOnce(&mut Document, &mut Selection) -> Result) -> Result { diff --git a/crates/engine/src/tests/swatches.rs b/crates/engine/src/tests/swatches.rs index 20144d4411..ab379c2f6c 100644 --- a/crates/engine/src/tests/swatches.rs +++ b/crates/engine/src/tests/swatches.rs @@ -989,6 +989,64 @@ fn linked(hex: &str, name: &str) -> Paint { Paint::Solid { color: Color::from_hex(hex).unwrap(), swatch: Some(name.into()), tint: 1.0 } } +#[test] +fn swatch_edits_record_only_renames_and_taking_empties_the_record() { + let mut s = session(); + let uid = s.active().unwrap().uid; + run(&mut s, "swatch.new", json!({"name": "Teal", "color": "#008080"})); + run(&mut s, "swatch.edit", json!({"name": "Teal", "color": "#009090"})); + assert!(s.take_swatch_renames().is_empty()); + run(&mut s, "swatch.edit", json!({"name": "Teal", "newName": "Renamed teal"})); + assert_eq!(s.take_swatch_renames(), vec![(uid, "Teal".to_owned(), "Renamed teal".to_owned())]); + assert!(s.take_swatch_renames().is_empty()); +} + +#[test] +fn failed_batches_restore_pending_swatch_renames_after_eviction() { + let mut s = session(); + let uid = s.active().unwrap().uid; + run(&mut s, "swatch.new", json!({"name": "Teal", "color": "#008080"})); + run(&mut s, "swatch.edit", json!({"name": "Teal", "newName": "Ink 0 letter"})); + let mut commands = Vec::new(); + for i in 0..=MAX_SWATCH_RENAMES { + commands.push(json!({"command": "swatch.edit", "params": {"name": format!("Ink {i} letter"), "newName": format!("Ink {} letter", i + 1)}})); + } + commands.push(json!({"command": "command.batch", "params": {"commands": []}})); + assert!(s.execute("command.batch", &json!({"commands": commands})).is_err()); + assert!(doc(&s).swatch("Ink 0 letter").is_some()); + assert_eq!(s.take_swatch_renames(), vec![(uid, "Teal".to_owned(), "Ink 0 letter".to_owned())]); + commands.pop(); + run(&mut s, "command.batch", json!({"commands": commands})); + let renames = s.take_swatch_renames(); + let expected: Vec<_> = (1..=MAX_SWATCH_RENAMES).map(|i| (uid, format!("Ink {i} letter"), format!("Ink {} letter", i + 1))).collect(); + assert_eq!(renames, expected, "only the newest renames remain, in order"); +} + +#[test] +fn swatch_rename_previews_leave_no_record_and_confirmation_records_once() { + let mut s = session(); + let uid = s.active().unwrap().uid; + run(&mut s, "swatch.new", json!({"name": "Teal", "color": "#008080"})); + s.begin_interaction("Swatch Options").unwrap(); + for name in ["Preview teal", "Replacement teal"] { + s.preview("swatch.edit", &json!({"name": "Teal", "newName": name})).unwrap(); + assert!(doc(&s).swatch(name).is_some()); + assert!(s.take_swatch_renames().is_empty()); + } + s.cancel_interaction().unwrap(); + assert!(doc(&s).swatch("Teal").is_some()); + assert!(s.take_swatch_renames().is_empty()); + + // Swatch Options cancels its preview and runs the confirmed edit again. + s.begin_interaction("Swatch Options").unwrap(); + s.preview("swatch.edit", &json!({"name": "Teal", "newName": "Renamed teal"})).unwrap(); + assert!(s.take_swatch_renames().is_empty()); + s.cancel_interaction().unwrap(); + run(&mut s, "swatch.edit", json!({"name": "Teal", "newName": "Renamed teal"})); + assert_eq!(s.take_swatch_renames(), vec![(uid, "Teal".to_owned(), "Renamed teal".to_owned())]); + assert!(s.take_swatch_renames().is_empty()); +} + #[test] fn editing_a_global_swatch_recolours_linked_art_in_one_step() { let mut s = session(); diff --git a/crates/ui-egui/src/app/commands.rs b/crates/ui-egui/src/app/commands.rs index 1452288749..4d50e9ac41 100644 --- a/crates/ui-egui/src/app/commands.rs +++ b/crates/ui-egui/src/app/commands.rs @@ -97,6 +97,9 @@ impl VectorcraftApp { crate::plugin_runs::show_notice(self, id, r); } placing::settle_swatch_paste(self); + for (uid, from, to) in self.session.take_swatch_renames() { + dialogs::gradient_stop::follow_swatch_rename(self, uid, &from, &to); + } result } diff --git a/crates/ui-egui/src/dialogs/gradient_stop.rs b/crates/ui-egui/src/dialogs/gradient_stop.rs index a06be20bf4..17ae6eaa36 100644 --- a/crates/ui-egui/src/dialogs/gradient_stop.rs +++ b/crates/ui-egui/src/dialogs/gradient_stop.rs @@ -103,6 +103,22 @@ impl TargetKey { } } +/// The swatch last picked in the Swatches view and the stop or point it is picked for. +#[derive(Clone)] +struct PickedSwatch { + target: TargetKey, + name: String, +} + +impl PickedSwatch { + /// The active document's swatch `name`, picked for `target`. + fn of(app: &VectorcraftApp, target: &Target, name: &str) -> Option { + let key = TargetKey::of(app, target)?; + app.session.active()?.doc.swatch(name)?; + Some(Self { target: key, name: name.to_owned() }) + } +} + /// A Color mode command from the popover's menu that has not been seen to finish. struct OwnMode { /// The document's history step when it ran. @@ -264,9 +280,9 @@ fn recolor(mut stops: Vec, i: usize, color: Color, link: Option<(S /// comes from: a linked colour's tint swatch for that tint, or its global swatch at 100%; an unlinked colour's process /// swatch of that very colour. Where several entries qualify, `picked`, the swatch last picked in the popover for this /// target, is the one. -fn swatch_colors(app: &VectorcraftApp, target: &Target, picked: Option<&(TargetKey, String)>) -> (Vec<(String, Color)>, Option) { +fn swatch_colors(app: &VectorcraftApp, target: &Target, picked: Option<&PickedSwatch>) -> (Vec<(String, Color)>, Option) { let Some(st) = app.session.active() else { return (vec![], None) }; - let picked = picked.filter(|(key, _)| key.matches(app, target)).map(|(_, name)| name.as_str()); + let picked = picked.filter(|p| p.target.matches(app, target)); let (link, color) = match target { Target::Stop(paint, i) => { let stop = &paint.gradient.stops[*i]; @@ -277,6 +293,7 @@ fn swatch_colors(app: &VectorcraftApp, target: &Target, picked: Option<&(TargetK let mut colors = Vec::new(); let mut selected = None; for swatch in st.doc.swatches_iter() { + let is_picked = picked.is_some_and(|p| p.name == swatch.name); let Some(own) = swatch.paint.color() else { continue }; // Picking this swatch would give the target the colour and link it has now. let source = match (link, swatch.tint_of()) { @@ -284,7 +301,7 @@ fn swatch_colors(app: &VectorcraftApp, target: &Target, picked: Option<&(TargetK (Some((base, tint)), None) => swatch.global && swatch.name == base && same_tint(tint, 1.0), (None, _) => !swatch.global && matches!(swatch.paint, Paint::Solid { color: c, swatch: None, .. } if c == color), }; - if source && (selected.is_none() || picked == Some(swatch.name.as_str())) { + if source && (selected.is_none() || is_picked) { selected = Some(colors.len()); } colors.push((swatch.name.clone(), own)); @@ -377,7 +394,7 @@ pub(crate) struct GradientStopPopover { panel: ColorPanelState, feed: Option, /// The swatch last picked in the Swatches view and the stop or point it was picked for. - picked_swatch: Option<(TargetKey, String)>, + picked_swatch: Option, /// The fields set from outside, applied at confirm. set: PopoverFields, filed: Filed, @@ -552,7 +569,10 @@ fn swatches(ui: &mut Ui, feed: &Feed, filed: &mut Filed) { Notice::new(Tone::Info, tl!("The document has no colour swatches.").to_owned()).show(ui); return; } - let shown = SwatchPalette::new("Swatches", Bind::given(feed.current), &feed.tiles).selected_swatch(feed.selected_swatch).show(ui); + // Only the entry the target's colour comes from is selected, and none when no entry is: a swatch that only draws + // the same colour, such as a spot ink an unlinked stop has the colour of, is not. + let shown = + SwatchPalette::new("Swatches", Bind::given(feed.current), &feed.tiles).selected_swatch(feed.selected_swatch).select_by_color(false).show(ui); // A click on a swatch other than the one shown as the target's, also one of the colour the target already has. A // click on the one shown changes nothing. if let Some(swatch) = shown.picked_swatch.filter(|i| Some(*i) != feed.selected_swatch).and_then(|i| feed.swatches.get(i)) { @@ -643,6 +663,23 @@ fn refresh(app: &VectorcraftApp, ctx: &egui::Context, id: DialogId, target: &Tar }); } +/// After the swatch `from` of the document `uid` was renamed `to`, the open popover's remembered pick takes the new name +/// when it is that swatch, picked for the selected stop or point. +pub(crate) fn follow_swatch_rename(app: &VectorcraftApp, uid: u64, from: &str, to: &str) { + let Some(target) = selected(app) else { return }; + let Some(dialogs) = app.dialogs() else { return }; + dialogs.with(|host| { + if let Some(popover) = host.get_mut_by_id::(dialog_id(KIND)) + && let Some(picked) = &mut popover.picked_swatch + && picked.target.document.0 == uid + && picked.target.matches(app, &target) + && picked.name == from + { + picked.name = to.to_owned(); + } + }); +} + /// Opens the popover on the selected stop (a freeform gradient's selected point) of the active gradient, the stop /// at `index` when it is not the selected one. Nothing opens without a window or a stop to edit. pub(crate) fn open_at(app: &mut VectorcraftApp, index: usize, anchor: Anchor) { @@ -670,6 +707,9 @@ pub(crate) fn open_at(app: &mut VectorcraftApp, index: usize, anchor: Anchor) { /// Once per frame: applies what the user did in the popover and its confirmed answer, closes it when its stop or /// point goes away, and pushes the target's values for the next frame. One line of `dialogs::host::pump`. pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &egui::Context) { + for (uid, from, to) in app.session.take_swatch_renames() { + follow_swatch_rename(app, uid, &from, &to); + } let Some(dialogs) = app.dialogs() else { return }; let id = dialog_id(KIND); match dialogs.with(|host| host.take_by_id::(id)) { @@ -735,7 +775,7 @@ pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &egui::Context) { { pick_swatch(app, &target, &name, color); // Swatches of one colour draw alike: the Swatches view shows the one picked, not the first of them. - let picked = selected(app).and_then(|target| TargetKey::of(app, &target)).map(|key| (key, name)); + let picked = selected(app).and_then(|target| PickedSwatch::of(app, &target, &name)); dialogs.with(|host| { if let Some(popover) = host.get_mut_by_id::(id) { popover.picked_swatch = picked; @@ -918,20 +958,29 @@ mod tests { let target = selected(&app).unwrap(); pick_swatch(&mut app, &target, "First green ink", Color::from_hex("#00ff00").unwrap()); let mut panel = swatches_view(app); - assert_eq!(shown_swatch(&panel.app()).as_deref(), Some("First green ink")); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some("First green ink")); panel.click("Second green ink"); assert_eq!(stops(&panel.app())[1].swatch.as_deref(), Some("Second green ink")); - assert_eq!(shown_swatch(&panel.app()).as_deref(), Some("Second green ink"), "the view shows the swatch picked"); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some("Second green ink"), "the view shows the swatch picked"); panel.app().run("swatch.edit", json!({"name": "Second green ink", "color": "#123456"})).unwrap(); assert_eq!(stops(&panel.app())[1].color.to_hex(), "#123456", "the stop follows the swatch it was given"); } - /// The swatch the open popover's Swatches view shows as the target's. - fn shown_swatch(app: &VectorcraftApp) -> Option { - app.dialogs().unwrap().with(|host| { - let feed = host.get_by_id::(dialog_id(KIND))?.feed.as_ref()?; - feed.swatches.get(feed.selected_swatch?).map(|(name, _)| name.clone()) - }) + /// The swatch the open popover's Swatches view shows as the target's, after two fresh frames: the first one's pump + /// feeds the view an edit made outside it, and the second draws that feed. The view draws that swatch's cell + /// selected and no other, and no cell selected when it shows none. + fn shown_swatch(panel: &mut crate::tests::panel_harness::Panel) -> Option { + panel.frame(); + panel.frame(); + let (names, shown) = panel.app().dialogs().unwrap().with(|host| { + let feed = host.get_by_id::(dialog_id(KIND)).and_then(|popover| popover.feed.as_ref()).expect("the view's feed"); + let shown = feed.selected_swatch.and_then(|i| feed.swatches.get(i)).map(|(name, _)| name.clone()); + (feed.swatches.iter().map(|(name, _)| name.clone()).collect::>(), shown) + }); + let drawn: Vec = + names.into_iter().filter(|name| panel.selected_role(name, craft::ui::contract::Role::LIST_BOX_OPTION) == Some(true)).collect(); + assert_eq!(drawn, shown.iter().cloned().collect::>(), "the cells the view draws selected"); + shown } #[test] @@ -941,16 +990,149 @@ mod tests { app.run("swatch.new", json!({"name": name, "color": "#008080"})).unwrap(); } let mut panel = swatches_view(app); - assert_eq!(shown_swatch(&panel.app()), None, "the stop's green is no swatch's"); + assert_eq!(shown_swatch(&mut panel), None, "the stop's green is no swatch's"); panel.click("Second teal"); assert_eq!(stops(&panel.app())[1].color.to_hex(), "#008080"); - assert_eq!(shown_swatch(&panel.app()).as_deref(), Some("Second teal")); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some("Second teal")); let undo = panel.app().session.active().unwrap().history.undo.len(); panel.click("First teal"); - assert_eq!(shown_swatch(&panel.app()).as_deref(), Some("First teal")); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some("First teal")); assert_eq!(panel.app().session.active().unwrap().history.undo.len(), undo, "the same colour again is no edit"); } + #[test] + fn the_swatches_view_keeps_the_picked_process_swatch_through_a_rename() { + let mut app = app(); + for name in ["First teal", "Second teal"] { + app.run("swatch.new", json!({"name": name, "color": "#008080"})).unwrap(); + } + let mut panel = swatches_view(app); + panel.click("Second teal"); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some("Second teal")); + panel.app().run("swatch.edit", json!({"name": "Second teal", "newName": "Renamed teal"})).unwrap(); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some("Renamed teal"), "the renamed swatch stays the one shown"); + panel.app().run("edit.undo", json!({})).unwrap(); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some("First teal"), "Undo of the rename falls back to the first qualifying swatch"); + panel.click("Second teal"); + panel.app().run("swatch.edit", json!({"name": "First teal", "newName": "Other teal"})).unwrap(); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some("Second teal"), "a rename of the other swatch moves nothing"); + let first = panel.app().session.active().unwrap().doc.swatches_iter().position(|s| s.name == "Other teal").unwrap(); + panel.app().run("swatch.move", json!({"name": "Second teal", "to": first})).unwrap(); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some("Second teal"), "the picked swatch moved before the other stays the one shown"); + } + + #[test] + fn the_swatches_view_keeps_the_picked_swatch_through_a_rename_in_a_batch() { + let mut app = app(); + for name in ["First teal", "Second teal"] { + app.run("swatch.new", json!({"name": name, "color": "#008080"})).unwrap(); + } + let mut panel = swatches_view(app); + panel.click("Second teal"); + panel + .app() + .run("command.batch", json!({"commands": [{"command": "swatch.edit", "params": {"name": "Second teal", "newName": "Renamed teal"}}]})) + .unwrap(); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some("Renamed teal")); + } + + #[test] + fn the_swatches_view_keeps_the_pick_when_a_batch_renames_a_swatch_in_another_document() { + let mut app = app(); + for name in ["First teal", "Second teal"] { + app.run("swatch.new", json!({"name": name, "color": "#008080"})).unwrap(); + } + app.run("file.new", json!({"width": 100, "height": 100})).unwrap(); + for name in ["First teal", "Second teal"] { + app.run("swatch.new", json!({"name": name, "color": "#008080"})).unwrap(); + } + app.run("document.activate", json!({"index": 0})).unwrap(); + let mut panel = swatches_view(app); + panel.click("Second teal"); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some("Second teal")); + panel + .app() + .run( + "command.batch", + json!({"commands": [ + {"command": "document.activate", "params": {"index": 1}}, + {"command": "swatch.edit", "params": {"name": "Second teal", "newName": "Renamed teal"}}, + {"command": "document.activate", "params": {"index": 0}} + ]}), + ) + .unwrap(); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some("Second teal")); + assert!(panel.app().session.documents()[1].doc.swatch("Renamed teal").is_some()); + } + + #[test] + fn the_swatches_view_follows_renames_that_bypass_app_run() { + let mut app = app(); + for name in ["First teal", "Second teal"] { + app.run("swatch.new", json!({"name": name, "color": "#008080"})).unwrap(); + } + let mut panel = swatches_view(app); + panel.click("Second teal"); + panel.app().session.execute("swatch.edit", &json!({"name": "Second teal", "newName": "Renamed teal"})).unwrap(); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some("Renamed teal")); + } + + #[test] + fn the_swatches_view_does_not_give_a_deleted_picked_swatch_to_its_neighbor() { + let mut app = app(); + for name in ["First teal", "Second teal", "Third teal"] { + app.run("swatch.new", json!({"name": name, "color": "#008080"})).unwrap(); + } + let mut panel = swatches_view(app); + panel.click("Second teal"); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some("Second teal")); + panel.app().run("swatch.delete", json!({"name": "Second teal"})).unwrap(); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some("First teal"), "the swatch after the deleted one is not taken for it"); + panel.app().run("edit.undo", json!({})).unwrap(); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some("Second teal"), "Undo of the deletion shows the picked swatch again"); + } + + #[test] + fn the_swatches_view_does_not_give_a_deleted_picked_swatch_to_a_replacement() { + let mut app = app(); + for name in ["First teal", "Second teal"] { + app.run("swatch.new", json!({"name": name, "color": "#008080"})).unwrap(); + } + let mut panel = swatches_view(app); + panel.click("Second teal"); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some("Second teal")); + panel.app().run("swatch.delete", json!({"name": "Second teal"})).unwrap(); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some("First teal")); + panel.app().run("swatch.new", json!({"name": "New teal", "color": "#008080"})).unwrap(); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some("First teal"), "a new swatch in the deleted one's place is not taken for it"); + panel.app().run("edit.undo", json!({})).unwrap(); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some("First teal")); + panel.app().run("edit.undo", json!({})).unwrap(); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some("Second teal"), "Undo of the deletion shows the picked swatch again"); + } + + #[test] + fn the_swatches_view_does_not_follow_a_replacement_in_a_batch() { + let mut app = app(); + for name in ["First teal", "Second teal"] { + app.run("swatch.new", json!({"name": name, "color": "#008080"})).unwrap(); + } + let mut panel = swatches_view(app); + panel.click("Second teal"); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some("Second teal")); + panel + .app() + .run( + "command.batch", + json!({"commands": [ + {"command": "swatch.delete", "params": {"name": "Second teal"}}, + {"command": "swatch.new", "params": {"name": "New teal", "color": "#008080"}} + ]}), + ) + .unwrap(); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some("First teal"), "the replacement does not inherit the pick"); + } + #[test] fn the_swatches_view_shows_the_tint_swatch_picked_among_two_of_one_tint() { let mut app = app(); @@ -962,12 +1144,12 @@ mod tests { panel.click(&tints[1]); let stop = stops(&panel.app())[1].clone(); assert_eq!((stop.swatch.as_deref(), (stop.tint * 100.0).round()), (Some("Green ink"), 40.0)); - assert_eq!(shown_swatch(&panel.app()).as_deref(), Some(tints[1].as_str())); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some(tints[1].as_str())); panel.click(&tints[0]); - assert_eq!(shown_swatch(&panel.app()).as_deref(), Some(tints[0].as_str())); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some(tints[0].as_str())); panel.click("Green ink"); assert_eq!(stops(&panel.app())[1].tint, 1.0); - assert_eq!(shown_swatch(&panel.app()).as_deref(), Some("Green ink")); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some("Green ink")); } #[test] @@ -975,17 +1157,28 @@ mod tests { let mut app = app(); app.run("swatch.new", json!({"name": "Green ink", "color": "#00ff00", "spot": true})).unwrap(); let mut panel = swatches_view(app); - assert_eq!(shown_swatch(&panel.app()), None, "the stop's green is not linked to the ink"); + assert_eq!(shown_swatch(&mut panel), None, "the stop's green is not linked to the ink"); panel.click("Green ink"); assert_eq!(stops(&panel.app())[1].swatch.as_deref(), Some("Green ink")); - assert_eq!(shown_swatch(&panel.app()).as_deref(), Some("Green ink")); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some("Green ink")); + + // Two spot inks of the stop's green: the unlinked stop shows neither, and a pick links the stop to that one. + let mut app = self::app(); + for name in ["First green ink", "Second green ink"] { + app.run("swatch.new", json!({"name": name, "color": "#00ff00", "spot": true})).unwrap(); + } + let mut panel = swatches_view(app); + assert_eq!(shown_swatch(&mut panel), None, "the stop's green is linked to neither ink"); + panel.click("Second green ink"); + assert_eq!(stops(&panel.app())[1].swatch.as_deref(), Some("Second green ink")); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some("Second green ink")); let mut app = self::app(); app.run("swatch.new", json!({"name": "Green ink", "color": "#00ff00", "spot": true})).unwrap(); crate::panels::gradient::recolor_selected(&mut app, Color::from_hex("#00ff00").unwrap(), &json!({"swatch": "Green ink", "tint": 40})); assert_eq!((stops(&app)[1].tint * 100.0).round(), 40.0); let mut panel = swatches_view(app); - assert_eq!(shown_swatch(&panel.app()), None, "a 40% tint is not the ink at 100%"); + assert_eq!(shown_swatch(&mut panel), None, "a 40% tint is not the ink at 100%"); panel.click("Green ink"); let stop = stops(&panel.app())[1].clone(); assert_eq!((stop.swatch.as_deref(), stop.tint), (Some("Green ink"), 1.0)); @@ -1005,7 +1198,7 @@ mod tests { app.run("gradient.selectStop", json!({"index": 1})).unwrap(); let undo = app.session.active().unwrap().history.undo.len(); let mut panel = swatches_view(app); - assert_eq!(shown_swatch(&panel.app()).as_deref(), Some("Green ink")); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some("Green ink")); panel.click("Green ink"); let Paint::Gradient(paint) = active_paint(&panel.app()) else { panic!("expected a gradient") }; assert_eq!(paint.swatch.as_deref(), Some("Linked gradient")); diff --git a/crates/ui-egui/src/tests/panel_harness.rs b/crates/ui-egui/src/tests/panel_harness.rs index 0c7d427a68..a180c583b4 100644 --- a/crates/ui-egui/src/tests/panel_harness.rs +++ b/crates/ui-egui/src/tests/panel_harness.rs @@ -464,6 +464,12 @@ impl Panel { self.fixture.tree().toggled_of(self.look(name).node) } + /// The selected state of the control of `role` named `name`, where nodes of other roles share the name (a list + /// option beside its tooltip). + pub(crate) fn selected_role(&self, name: &str, role: craft::ui::contract::Role) -> Option { + self.fixture.tree().selected_of(self.look_role(name, role).node) + } + /// The kind of popup the control named `name` reports that it opens. pub(crate) fn popup_kind(&self, name: &str) -> Option { self.fixture.tree().has_popup_of(self.look(name).node)