Swatches: a user swatch whose name starts with [ can be deleted and edited

The panel's Delete Swatch and Swatch Options, in the footer and the panel
menu, treated every selected name starting with "[" as reserved, so a
user swatch such as [Mine] or a color group such as [Team] could not be
deleted or edited there, and New Color Group and Save Swatch Library left
them out. The checks now use Swatch::is_reserved, as the engine's
swatch.delete and swatch.edit and the item context menu already do, so
only None and [Registration] stay protected. Selected names the document
no longer holds are left out instead of reaching the command as errors.
This commit is contained in:
dotCooCoo
2026-11-17 09:22:26 -08:00
parent 36b5a5920c
commit ea782ca87e
4 changed files with 35 additions and 11 deletions
+12 -9
View File
@@ -2,16 +2,19 @@ use super::dnd::{group_of, selection};
use super::libraries::SwatchLibraries;
use super::{Kind, LibraryKind, Ui, Value, VectorcraftApp, View, active_paint, json, library_panel, menu_item, pstate, set_pstate, widgets};
/// The selected swatches and groups that can be deleted (all but None and Registration).
pub(super) fn deletable(sel: &[String]) -> Vec<String> {
sel.iter().filter(|n| !n.starts_with('[')).cloned().collect()
/// The selected swatches and groups that can be deleted: every color group in the document and
/// every swatch that `Swatch::is_reserved` does not reserve (None and Registration). Names the
/// document does not hold are left out.
pub(super) fn deletable(app: &VectorcraftApp, sel: &[String]) -> Vec<String> {
let Some(d) = app.session.active().map(|st| &st.doc) else { return vec![] };
sel.iter().filter(|n| d.swatch(n).map_or_else(|| d.swatch_groups.iter().any(|g| g.name == **n), |w| !w.is_reserved())).cloned().collect()
}
/// The selected swatch when exactly one swatch with an editor is selected (not None, Registration
/// or a group).
fn editable(app: &VectorcraftApp, sel: &[String]) -> Option<String> {
pub(super) fn editable(app: &VectorcraftApp, sel: &[String]) -> Option<String> {
match sel {
[n] if !n.starts_with('[') && app.session.active().is_some_and(|st| st.doc.swatch(n).is_some()) => Some(n.clone()),
[n] if app.session.active().and_then(|st| st.doc.swatch(n)).is_some_and(|w| !w.is_reserved()) => Some(n.clone()),
_ => None,
}
}
@@ -77,7 +80,7 @@ pub(super) fn bottom(app: &mut VectorcraftApp, ui: &mut Ui, sel: &[String]) {
}
ui.add_space((ui.available_width() - 3.0 * 28.0).max(0.0));
if widgets::icon_button(ui, "dc-folder", tl!("New Color Group"), false, 24.0).clicked() {
app.run("ui.newColorGroup", json!({"swatches": deletable(sel)})).ok();
app.run("ui.newColorGroup", json!({"swatches": deletable(app, sel)})).ok();
}
// Alt-click skips the dialog; Ctrl/Cmd-click makes a spot color, and Ctrl/Cmd+Shift-click
// makes a global process color.
@@ -85,7 +88,7 @@ pub(super) fn bottom(app: &mut VectorcraftApp, ui: &mut Ui, sel: &[String]) {
new_swatch(app, sel, ui.input(|i| i.modifiers));
}
// Alt-click deletes without asking.
let del = deletable(sel);
let del = deletable(app, sel);
if widgets::icon_button_enabled(ui, "trash-2", tl!("Delete Swatch"), false, !del.is_empty(), 24.0).clicked() {
delete(app, del, ui.input(|i| i.modifiers.alt));
}
@@ -123,7 +126,7 @@ pub(super) fn new_swatch(app: &mut VectorcraftApp, sel: &[String], m: egui::Modi
pub fn menu(app: &mut VectorcraftApp, ui: &mut Ui) {
let view = app.ui.swatches_view;
let selected = selection(app, ui);
let del = deletable(&selected);
let del = deletable(app, &selected);
let opts = editable(app, &selected);
if menu_item(ui, tl!("New Swatch…"), true, false) {
new_swatch(app, &selected, egui::Modifiers::NONE);
@@ -198,6 +201,6 @@ pub fn menu(app: &mut VectorcraftApp, ui: &mut Ui) {
ui.separator();
ui.menu_button(tl!("Open Swatch Library"), |ui| library_panel::library_menu::<SwatchLibraries>(app, ui));
if menu_item(ui, tl!("Save Swatch Library…"), true, false) {
app.run("ui.saveSwatchLibrary", json!({ "names": deletable(&selected) })).ok();
app.run("ui.saveSwatchLibrary", json!({ "names": deletable(app, &selected) })).ok();
}
}
@@ -142,7 +142,7 @@ impl LibraryKind for SwatchLibraries {
app.status(e);
}
if menu_item(ui, tl!("Save Swatch Library…"), app.session.active().is_some(), false) {
let names = deletable(&selection(app, ui));
let names = deletable(app, &selection(app, ui));
app.run("ui.saveSwatchLibrary", json!({ "names": names })).ok();
}
}
+21 -1
View File
@@ -281,7 +281,27 @@ fn delete_asks_then_removes_the_selection_and_unlinks_art() {
assert!(app.session.doc().unwrap().doc.swatch("Orange").is_some());
delete(&mut app, vec!["Orange".into()], true);
assert!(app.ui.dialog.is_none() && app.session.doc().unwrap().doc.swatch("Orange").is_none());
assert_eq!(deletable(&["[None]".into(), REGISTRATION.into(), "Grays".into()]), ["Grays"]);
assert_eq!(deletable(&app, &["[None]".into(), REGISTRATION.into(), "Grays".into()]), ["Grays"]);
}
/// Only None and Registration are reserved: a user swatch or color group whose name starts with
/// `[` can be deleted, the swatch can be edited, and names the document does not hold are left out.
#[test]
fn bracketed_user_swatches_and_groups_can_be_deleted_and_edited() {
let mut app = app();
app.run("swatch.new", json!({"name": "[Mine]", "color": "#336699"})).unwrap();
app.run("swatch.newGroup", json!({"name": "[Team]", "colors": ["#ff0000"]})).unwrap();
let names = |v: &[&str]| v.iter().map(|n| n.to_string()).collect::<Vec<String>>();
assert_eq!(deletable(&app, &names(&["[None]", REGISTRATION, "[Mine]", "[Team]", "Gone"])), ["[Mine]", "[Team]"]);
assert_eq!(actions::editable(&app, &names(&["[Mine]"])).as_deref(), Some("[Mine]"));
for name in ["[None]", REGISTRATION, "[Team]", "Gone"] {
assert_eq!(actions::editable(&app, &names(&[name])), None, "{name}");
}
let doomed = deletable(&app, &names(&["[Mine]"]));
delete(&mut app, doomed, false);
assert_eq!(app.ui.dialog.as_ref().map(|d| d.str("message")).as_deref(), Some("Delete “[Mine]”?"));
crate::dialogs::confirm(&mut app).unwrap();
assert!(app.session.doc().unwrap().doc.swatch("[Mine]").is_none());
}
/// The prompt for several swatches and groups gives the form the language's plural rule picks for the count.