DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Integration: #763 New Swatch with nothing selected makes a colour swatch in the engine; the panel sends the Fill/Stroke target
aionggaad9db158f7b
diff --git a/crates/engine/src/cmd/style.rs b/crates/engine/src/cmd/style.rsindex 66a20f942..81ccd63bf 100644--- a/crates/engine/src/cmd/style.rs+++ b/crates/engine/src/cmd/style.rs@@ -179,7 +179,7 @@ pub fn specs() -> Vec<CommandSpec> {"New Swatch",[],None,- "{swatch?: name, target?: fill|stroke} → {name} — the Swatches panel's New Swatch: `swatch` (default: the selection's fill or stroke) as a new swatch. An unnamed colour or gradient becomes a named one (a gradient: \"New Gradient Swatch\"), still applied; a named swatch is duplicated (\"… copy\")",+ "{swatch?: name, target?: fill|stroke} → {name} — the Swatches panel's New Swatch: `swatch` (default: the selection's fill or stroke) as a new swatch. An unnamed colour or gradient becomes a named one (a gradient: \"New Gradient Swatch\"), still applied; a named swatch is duplicated (\"… copy\"); with nothing selected and no `swatch`, a new colour swatch",has_doc,new_swatch),@@ -960,7 +960,10 @@ fn new_swatch(s: &mut Session, p: &Value) -> Result<Value> {let name = match str_param(p, "swatch") {Some(n) => n.to_string(),None => {- let id = st.selection.items.first().copied().ok_or_else(|| bad(ID, "select an object or give `swatch`"))?;+ let Some(id) = st.selection.items.first().copied() else {+ // Nothing selected: a new colour swatch.+ return create_swatch(s, &json!({"color": {"c": 0, "m": 50, "y": 100, "k": 0}}));+ };let it = st.doc.item(id).ok_or_else(|| bad(ID, "no such item"))?;if str_param(p, "target") == Some("stroke") { it.stroke.swatch.clone() } else { it.fill.swatch.clone() }}@@ -2833,7 +2836,13 @@ mod gradient_swatch_tests {assert_eq!(s.execute("swatch.new", &json!({})).unwrap()["name"], "New Gradient Swatch copy");assert_eq!(s.execute("swatch.new", &json!({"swatch": "C=100 M=0 Y=0 K=0"})).unwrap()["name"], "C=100 M=0 Y=0 K=0 copy");assert!(s.execute("swatch.new", &json!({"swatch": "[None]"})).is_err());+ // The stroke in front: the stroke's swatch.+ s.execute("object.stroke", &json!({"ids": [id], "swatch": "C=100 M=0 Y=0 K=0"})).unwrap();+ assert_eq!(s.execute("swatch.new", &json!({"target": "stroke"})).unwrap()["name"], "C=100 M=0 Y=0 K=0 copy 2");+ // Nothing selected, no swatch given: a new colour swatch.s.execute("edit.deselectAll", &json!({})).unwrap();- assert!(s.execute("swatch.new", &json!({})).is_err(), "nothing selected, no swatch given");+ let n = s.execute("swatch.new", &json!({})).unwrap()["name"].as_str().unwrap().to_string();+ let w = s.doc().unwrap().doc.swatch(&n).unwrap();+ assert!(w.named && !w.hidden && matches!(w.value, SwatchValue::Color { .. }), "{w:?}");}}diff --git a/crates/ui-egui/src/panels/swatches.rs b/crates/ui-egui/src/panels/swatches.rsindex 37b843575..0b12c6c1c 100644--- a/crates/ui-egui/src/panels/swatches.rs+++ b/crates/ui-egui/src/panels/swatches.rs@@ -79,13 +79,11 @@ pub fn show(app: &mut DesignApp, ui: &mut egui::Ui) {egui::RichText::new(crate::i18n::tr(&app.ui.language, "Right-click: stroke, groups")).size(10.5).color(t.text_disabled),));ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {- // As InDesign's: the selection's colour or gradient becomes a swatch (a gradient made in- // the Gradient panel: "New Gradient Swatch"), a named one is duplicated; with nothing+ // The selection's fill or stroke (whichever is in front) becomes a swatch; with nothing// selected, a new colour.- if icons::button(ui, "plus", 20.0, false, crate::i18n::tr(&app.ui.language, "New Swatch")).clicked()- && app.run("swatch.new", json!({})).is_err()- {- let _ = app.run("swatch.create", json!({"color": {"c": 0, "m": 50, "y": 100, "k": 0}}));+ if icons::button(ui, "plus", 20.0, false, crate::i18n::tr(&app.ui.language, "New Swatch")).clicked() {+ let target = if app.ui.stroke_front { "stroke" } else { "fill" };+ let _ = app.run("swatch.new", json!({"target": target}));}crate::menus::menu_button(ui, "☰", |ui| {let current = fill_cur.as_deref().filter(|n| doc.swatch(n).is_some_and(|w| !w.hidden));