Merge pull request #8456 from dotCooCoo/feat/swatches-color-group-options

Swatches: Color Group Options renames the selected color group
This commit is contained in:
BFlat
2026-11-19 14:58:45 +07:00
committed by GitHub
46 changed files with 662 additions and 8 deletions
@@ -600,6 +600,7 @@ pub(super) const ALLOWED: &[&str] = &[
"swatch.move",
"swatch.new",
"swatch.newGroup",
"swatch.renameGroup",
"swatch.resetDefaults",
"swatch.setSpot",
"swatch.sortByKind",
+41 -1
View File
@@ -9,7 +9,9 @@ use vectorcraft_doc::swatches::{LinkFn, PaintFn, color_name, map_node_paints, ma
use vectorcraft_doc::{Document, NodeId};
use super::paint::paint_from;
use super::{CommandSpec, Result, Session, bad, bool_or, check_numbers, check_switches, cmd, color_value, has_doc, index, ok, str_param, switch_or};
use super::{
CommandSpec, Result, Session, bad, bool_or, check_numbers, check_switches, cmd, color_value, has_doc, index, ok, str_param, switch_or, text_param,
};
use crate::EngineError;
pub fn specs() -> Vec<CommandSpec> {
@@ -106,6 +108,15 @@ pub fn specs() -> Vec<CommandSpec> {
has_doc,
swatch_ungroup
),
cmd!(
"swatch.renameGroup",
"Color Group Options",
["Window", "Swatches"],
None,
"{group: color group, name: its new name (trimmed)} rename a color group, as one undo step; its swatches, their colors and the art linked to them stay as they are. Swatches and color groups share one set of names, so a name another color group or a swatch has is an error, as is an empty name; the group's own name records no undo step. A drag still in progress becomes an undo step of its own first → {name}",
has_doc,
swatch_rename_group
),
cmd!(
"swatch.sortByKind",
"Sort by Kind",
@@ -1200,6 +1211,35 @@ fn swatch_ungroup(s: &mut Session, p: &Value) -> Result<Value> {
Ok(json!({ "swatches": moved }))
}
fn swatch_rename_group(s: &mut Session, p: &Value) -> Result<Value> {
const C: &str = "swatch.renameGroup";
let group = text_param(p, "group", C)?.ok_or_else(|| bad(C, "missing `group`"))?.to_string();
let name = text_param(p, "name", C)?.ok_or_else(|| bad(C, "missing `name`"))?.trim().to_string();
if name.is_empty() {
return Err(bad(C, "`name` is empty"));
}
let d = &s.doc()?.doc;
if !d.swatch_groups.iter().any(|g| g.name == group) {
return Err(bad(C, format!("no colour group `{group}`")));
}
if name == group {
return Ok(json!({ "name": name }));
}
if d.swatch_name_taken(&name) {
return Err(bad(C, format!("a swatch or color group named `{name}` already exists")));
}
// A drag still in progress is a step of its own, so canceling or undoing it leaves the rename.
if !s.previewing && s.batch_stash.is_none() && s.in_interaction() {
super::settle_interaction(s)?;
}
s.edit("Color Group Options", |d, _| {
let g = d.swatch_groups.iter_mut().find(|g| g.name == group).ok_or_else(|| bad(C, format!("no colour group `{group}`")))?;
g.name.clone_from(&name);
Ok(())
})?;
Ok(json!({ "name": name }))
}
fn swatch_sort_by_kind(s: &mut Session, _: &Value) -> Result<Value> {
let rank = |w: &Swatch| match &w.paint {
Paint::None => 0,
+66
View File
@@ -569,6 +569,72 @@ fn ungroup_and_sort_by_kind_are_one_undo_step_each() {
assert_eq!(n[n.iter().position(|x| x == "Orange").unwrap() + 1], "Ocean", "the spot colour comes after the process ones, then the gradients");
}
/// Color Group Options renames a color group alone, as one undo step: its swatches, their colors
/// and the art linked to them stay as they are. A name another group or a swatch has, an empty
/// name and a parameter that is missing or not a string are errors that change nothing, and the
/// group's own name records no undo step.
#[test]
fn rename_group_renames_only_the_group_in_one_undo_step() {
let mut s = session();
run(&mut s, "swatch.edit", json!({"name": "Bright Red", "global": true}));
let a = rect_filled(&mut s, json!({"swatch": "Bright Red"}));
let linked = fill(&s, a);
let members = names(&s, Some("Brights"));
let before = doc(&s).clone();
let undo = undo_len(&s);
assert_eq!(run(&mut s, "swatch.renameGroup", json!({"group": "Brights", "name": " Vivid Colors "}))["name"], "Vivid Colors");
assert!(doc(&s).swatch_groups.iter().all(|g| g.name != "Brights"));
assert_eq!(names(&s, Some("Vivid Colors")), members);
assert_eq!(fill(&s, a), linked, "linked art keeps its swatch and color");
assert_eq!(undo_len(&s), undo + 1);
run(&mut s, "edit.undo", json!({}));
assert_eq!(*doc(&s), before);
run(&mut s, "edit.redo", json!({}));
assert_eq!(names(&s, Some("Vivid Colors")), members);
let renamed = doc(&s).clone();
// The group's own name, trimmed, changes nothing.
assert_eq!(run(&mut s, "swatch.renameGroup", json!({"group": "Vivid Colors", "name": " Vivid Colors"}))["name"], "Vivid Colors");
assert_eq!(undo_len(&s), undo + 1);
let refused = [
json!({"group": "Vivid Colors", "name": "Grays"}),
json!({"group": "Vivid Colors", "name": "White"}),
json!({"group": "Vivid Colors", "name": "[None]"}),
json!({"group": "Vivid Colors", "name": "[Registration]"}),
json!({"group": "Vivid Colors", "name": ""}),
json!({"group": "Vivid Colors", "name": " "}),
json!({"group": "Vivid Colors"}),
json!({"group": "Vivid Colors", "name": null}),
json!({"group": "Vivid Colors", "name": 5}),
json!({"name": "Bold"}),
json!({"group": 5, "name": "Bold"}),
json!({"group": "Nope", "name": "Bold"}),
json!({"group": "White", "name": "Bold"}),
];
for p in refused {
assert!(s.execute("swatch.renameGroup", &p).is_err(), "{p}");
assert_eq!(*doc(&s), renamed, "{p}");
assert_eq!(undo_len(&s), undo + 1, "{p}");
}
}
/// A color group renamed while a drag is still in progress keeps its new name when the drag is
/// canceled: the drag becomes a step of its own first, and the rename is the next step.
#[test]
fn rename_group_during_a_drag_is_a_step_of_its_own() {
let mut s = session();
rect_filled(&mut s, json!({"swatch": "Bright Red"}));
let undo = undo_len(&s);
s.begin_interaction("Move").unwrap();
s.preview("object.move", &json!({"dx": 7, "dy": 0})).unwrap();
run(&mut s, "swatch.renameGroup", json!({"group": "Brights", "name": "Vivid"}));
s.cancel_interaction().ok();
assert!(doc(&s).swatch_groups.iter().any(|g| g.name == "Vivid"), "canceling the drag keeps the rename");
assert_eq!(undo_len(&s), undo + 2, "the drag and the rename are two steps");
run(&mut s, "edit.undo", json!({}));
assert!(doc(&s).swatch_groups.iter().any(|g| g.name == "Brights"), "Undo takes back the rename alone");
assert_eq!(undo_len(&s), undo + 1);
}
#[test]
fn a_cmyk_document_starts_with_cmyk_swatches_and_stores_new_colours_as_cmyk() {
use vectorcraft_color::Color;