Swatches: Add to Swatches checks a picked color group before skipping repeated names

A library color group named together with one of its own members, as in
names ["Sunset", "Brand"], is now refused when it holds a gradient or
pattern swatch. The group was checked after a member named first had
been taken on its own, so the group came in without it.
This commit is contained in:
dotCooCoo committed 2026-11-15 18:07:28 -08:00
1 parent e9a6864c96
commit 32ede687c6
2 files changed
+17 -7

No files matched your search

+12 -5
View File
@@ -789,7 +789,14 @@ fn copy_to_user(s: &mut Session, p: &Value) -> Result<Value> {
/// The library swatches `names` stand for, each once, with the colour group it goes into (`None`:
/// ungrouped): a group's name brings the group, a swatch's name the swatch alone; no names, all.
/// A picked group that holds a gradient or pattern swatch is refused.
fn picked<'a>(lib: &'a SwatchLibrary, names: &[String], cmd: &str) -> Result<Vec<(Option<&'a str>, &'a Swatch)>> {
// Color groups hold solid colors only, as swatch.new, swatch.newGroup and swatch.move keep them.
// Each picked group is checked whole, so a member also named on its own is not skipped.
let solid = |g: &SwatchGroup| match g.swatches.iter().find(|w| w.paint.color().is_none()) {
Some(w) => Err(bad(cmd, format!("`{}`: colour groups hold solid colours only", w.name))),
None => Ok(()),
};
let mut out: Vec<(Option<&str>, &Swatch)> = vec![];
let mut seen: HashSet<&str> = HashSet::new();
let mut push = |g: Option<&'a str>, w: &'a Swatch| {
@@ -798,12 +805,16 @@ fn picked<'a>(lib: &'a SwatchLibrary, names: &[String], cmd: &str) -> Result<Vec
}
};
if names.is_empty() {
lib.groups.iter().try_for_each(solid)?;
lib.swatches.iter().for_each(|w| push(None, w));
lib.groups.iter().for_each(|g| g.swatches.iter().for_each(|w| push(Some(&g.name), w)));
}
for n in names {
match (lib.group(n), lib.swatch(n)) {
(Some(g), _) => g.swatches.iter().for_each(|w| push(Some(&g.name), w)),
(Some(g), _) => {
solid(g)?;
g.swatches.iter().for_each(|w| push(Some(&g.name), w));
}
(None, Some(w)) => push(None, w),
(None, None) => return Err(bad(cmd, format!("no swatch or colour group `{n}` in `{}`", lib.name))),
}
@@ -839,10 +850,6 @@ fn add(s: &mut Session, p: &Value) -> Result<Value> {
};
let focus = switch_or(p, "focus", true, C)?;
let picked = picked(&lib, &names_param(p, C)?, C)?;
// Color groups hold solid colors only, as swatch.new, swatch.newGroup and swatch.move keep them.
if let Some((_, w)) = picked.iter().find(|(g, w)| g.is_some() && w.paint.color().is_none()) {
return Err(bad(C, format!("`{}`: colour groups hold solid colours only", w.name)));
}
// Swatches the document already has (same name and paint; for a pattern swatch, a swatch of the
// same pattern) are reused, not added again.
let patterns = patternlib::library_doc(&info.id);
+5 -2
View File
@@ -178,11 +178,14 @@ fn add_refuses_a_library_color_group_holding_a_gradient() {
let (_, loaded) = cmd::swatchlib::library(&s, &id).unwrap();
assert!(matches!(loaded.groups[0].swatches[1].paint, Paint::Gradient(_)), "the reader keeps the gradient in the group");
run(&mut s, "swatch.delete", json!({"names": ["Brand", "Sunset"]}));
let (before, undo) = (doc(&s).swatch_groups.clone(), undo_len(&s));
for p in [json!({"library": id}), json!({"library": id, "names": ["Brand"]})] {
let (before, swatches, undo) = (doc(&s).swatch_groups.clone(), doc(&s).swatches.len(), undo_len(&s));
// A group is refused whole even when its gradient was also named on its own before it.
let cases = [json!({"library": id}), json!({"library": id, "names": ["Brand"]}), json!({"library": id, "names": ["Sunset", "Brand"]})];
for p in cases {
let e = s.execute("swatch.library.add", &p).unwrap_err().to_string();
assert!(e.contains("`Sunset`: colour groups hold solid colours only"), "{p}: {e}");
assert_eq!(doc(&s).swatch_groups, before, "{p}");
assert_eq!(doc(&s).swatches.len(), swatches, "{p}");
assert_eq!(undo_len(&s), undo, "{p}");
}
// The gradient on its own comes in ungrouped.