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.
This commit is contained in:
dotCooCoo committed 2026-11-30 10:04:04 -08:00
1 parent 0d0c49cebb
commit fca5dc67ea
9 files changed
+307 -24

No files matched your search

+3
View File
@@ -25,6 +25,8 @@ pub(super) fn batch(s: &mut Session, p: &Value) -> Result<Value> {
// 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<Value> {
results
}
Err(e) => {
s.swatch_renames = swatch_renames;
roll_back(s, start, &stash);
s.notify_text_tool("batch.rollBack");
s.active = active;
+7
View File
@@ -977,6 +977,13 @@ fn swatch_edit(s: &mut Session, p: &Value) -> Result<Value> {
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}))
}
+6
View File
@@ -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<DocState>,
active: Option<usize>,
@@ -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<u64>,
/// 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<String>,
+1
View File
@@ -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,
+6
View File
@@ -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<T>(&mut self, label: &str, f: impl FnOnce(&mut Document, &mut Selection) -> Result<T>) -> Result<T> {
+58
View File
@@ -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();