From b7a1d9733688eb46db1214b04ac34234b9a68604 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 17:14:10 -0800 Subject: [PATCH] Require an open drawing when adding items from a Library panel --- crates/ui-egui/src/panels/library_panel.rs | 30 +++++++++++++----- .../ui-egui/src/panels/library_panel/tests.rs | 31 +++++++++++++++++++ 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/crates/ui-egui/src/panels/library_panel.rs b/crates/ui-egui/src/panels/library_panel.rs index f0eb4a72e2..ee20b56fed 100644 --- a/crates/ui-egui/src/panels/library_panel.rs +++ b/crates/ui-egui/src/panels/library_panel.rs @@ -180,7 +180,6 @@ pub(crate) struct Kind { find: fn(&VectorcraftApp, &str) -> Found, path: fn(&VectorcraftApp, &str) -> Option, neighbour: fn(&VectorcraftApp, &str, isize) -> Option, - add: fn(&mut VectorcraftApp, &str, Vec), add_label: &'static str, body: fn(&mut VectorcraftApp, &mut Ui, &str), } @@ -195,7 +194,6 @@ fn kind_of() -> Kind { find: K::find, path: K::path, neighbour: neighbour::, - add: K::add, add_label: K::ADD, body: body::, } @@ -213,6 +211,21 @@ fn kind(kind: &str) -> Option { .find(|k| k.kind == kind) } +/// The document command each library's Add action runs. +fn add_command(kind: &str) -> Option<&'static str> { + match kind { + "swatches" => Some("swatch.library.add"), + "brushes" => Some("brush.addFromLibrary"), + "symbols" => Some("symbol.addFromLibrary"), + "graphicStyles" => Some("graphicStyle.addFromLibrary"), + _ => None, + } +} + +fn can_add(app: &VectorcraftApp, kind: &str) -> bool { + add_command(kind).is_some_and(|command| crate::menus::enabled(app, command)) +} + /// The id of the panel of library `id` of `kind` (`library:swatches:pastels`). pub fn panel_id(kind: &str, id: &str) -> String { format!("{PREFIX}{kind}:{id}") @@ -666,7 +679,7 @@ fn body(app: &mut VectorcraftApp, ui: &mut Ui, id: &str) { let title = library_name(id, &name).to_string(); // The footer stays under the browser, which scrolls in the room left above it. let footer_out = RefCell::new(None); - let any = !selection.items.is_empty(); + let any = !selection.items.is_empty() && can_add(app, K::KIND); let mut shown = Panel::new(title.clone()) .collection_body() .footer(|ui, _| *footer_out.borrow_mut() = Some(footer_bar::(any).show_footer(ui))) @@ -707,8 +720,8 @@ fn body(app: &mut VectorcraftApp, ui: &mut Ui, id: &str) { } } -/// The panel's footer: the libraries menu, previous / next library, and Add to … , on while `any` item or folder is -/// selected. +/// The panel's footer: the libraries menu, previous / next library, and Add to … , on while items or folders are +/// selected and the document can receive them. fn footer_bar(any: bool) -> FooterBar { let icon = |name: &'static str| IconSpec::svg(IconId::new(name)); FooterBar::actions([ @@ -786,7 +799,7 @@ fn menu_entries(app: &VectorcraftApp, panel: &str) -> Vec { params["panel"] = json!(panel); MenuEntry::Row { command: CommandRef::with_params(command, params), label, enabled, checked } }; - let has_selection = !selected(app, panel).items.is_empty(); + let has_selection = !selected(app, panel).items.is_empty() && can_add(app, k.kind); let mut out = vec![row("ui.libraryPanel.add", json!({}), k.add_label, has_selection, None), MenuEntry::Separator]; out.extend(View::ALL.iter().map(|&(v, label)| row("ui.libraryPanel", json!({ "view": v }), label, true, Some(v == view)))); out.extend([ @@ -835,7 +848,7 @@ pub(crate) fn menu_states(app: &VectorcraftApp, panel: &str, states: &mut Comman } /// Runs a library panel command that the engine does not know: `ui.libraryPanel.add {panel}` adds the -/// panel's selection to the document. +/// panel's selection to the document. A refused addition returns its command's error. pub(crate) fn run(app: &mut VectorcraftApp, id: &str, params: &Value) -> Result { if id != "ui.libraryPanel.add" { return Err(format!("`{id}` isn't a library panel command")); @@ -843,7 +856,8 @@ pub(crate) fn run(app: &mut VectorcraftApp, id: &str, params: &Value) -> Result< let given = params.get("panel").and_then(Value::as_str).unwrap_or_default(); let (k, lib) = parse(given).ok_or_else(|| format!("`{given}` isn't a library panel (see window.swatchLibrary)"))?; let names: Vec = selected(app, given).items.into_iter().map(|key| key.name).collect(); - (k.add)(app, lib, names); + let command = add_command(k.kind).ok_or("this library cannot add items to a drawing")?; + app.run(command, json!({ "library": lib, "names": names }))?; Ok(Value::Null) } diff --git a/crates/ui-egui/src/panels/library_panel/tests.rs b/crates/ui-egui/src/panels/library_panel/tests.rs index 68fc0cf0de..da4cb07bec 100644 --- a/crates/ui-egui/src/panels/library_panel/tests.rs +++ b/crates/ui-egui/src/panels/library_panel/tests.rs @@ -182,6 +182,37 @@ fn modifier_clicks_select_and_add_to_swatches_adds_the_selection() { assert!(selected_names(&app, "library:swatches:brights").is_empty()); } +#[test] +fn adding_selected_library_items_requires_an_open_drawing() { + let mut app = app(); + let ctx = context(); + app.run("window.swatchLibrary", json!({"library": "earth-tones"})).unwrap(); + let panel = "library:swatches:earth-tones"; + click(&mut app, &ctx, "Clay 3", Modifiers::COMMAND, 0.0); + assert_eq!(selected_names(&app, panel), ["Clay 3"]); + let draw = |app: &mut VectorcraftApp, ui: &mut Ui| show(app, ui, panel); + let command = CommandRef::with_params("ui.libraryPanel.add", json!({"panel": panel})); + let menu_enabled = |app: &VectorcraftApp| { + let mut states = CommandStates::new(); + menu_states(app, panel, &mut states); + states.get(&command).is_some_and(CommandState::is_enabled) + }; + assert!(control(&mut app, &ctx, &draw, "Add to Swatches").0); + assert!(menu_enabled(&app)); + + app.session.execute("file.close", &json!({"index": 0})).unwrap(); + assert!(!control(&mut app, &ctx, &draw, "Add to Swatches").0); + assert!(!menu_enabled(&app)); + assert_eq!(selected_names(&app, panel), ["Clay 3"], "the library remains browsable"); + assert!(app.run("ui.libraryPanel.add", json!({"panel": panel})).is_err()); + + app.run("file.new", json!({"width": 100, "height": 100})).unwrap(); + assert!(control(&mut app, &ctx, &draw, "Add to Swatches").0); + assert!(menu_enabled(&app)); + app.run("ui.libraryPanel.add", json!({"panel": panel})).unwrap(); + assert!(app.session.active().unwrap().doc.swatch("Clay 3").is_some()); +} + const SWATCH_IDS: [&str; 9] = [ "web-safe-216", "grays-neutrals",