diff --git a/crates/engine/src/cmd/swatchlib.rs b/crates/engine/src/cmd/swatchlib.rs index d6eed98378..9ba2833052 100644 --- a/crates/engine/src/cmd/swatchlib.rs +++ b/crates/engine/src/cmd/swatchlib.rs @@ -789,7 +789,14 @@ fn copy_to_user(s: &mut Session, p: &Value) -> Result { /// 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, &'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 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 { }; 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); diff --git a/crates/engine/src/tests/swatchlib.rs b/crates/engine/src/tests/swatchlib.rs index fb088508c4..9be829fdf5 100644 --- a/crates/engine/src/tests/swatchlib.rs +++ b/crates/engine/src/tests/swatchlib.rs @@ -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.