Skip to content

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

aiongg67b61c5b8be8
diff --git a/crates/engine/src/cmd/style.rs b/crates/engine/src/cmd/style.rs
index 52f0d9467..a69952664 100644
--- a/crates/engine/src/cmd/style.rs
+++ b/crates/engine/src/cmd/style.rs
@@ -144,7 +144,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
),
@@ -807,7 +807,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() }
}
@@ -1997,8 +2000,14 @@ 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.rs
index 28c8e9e27..b07048608 100644
--- a/crates/ui-egui/src/panels/swatches.rs
+++ b/crates/ui-egui/src/panels/swatches.rs
@@ -70,13 +70,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| {
crate::menus::menu_button(ui, crate::rtl::widget(ui, crate::i18n::tr(&app.ui.language, "Ink Manager")), |ui| {