From 49d67e781f777ca26b85a4d8b8b08f3df390a9fb Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 16:13:21 -0800 Subject: [PATCH] Load browser-selected files in library panels --- crates/ui-egui/src/panels/brushes.rs | 2 +- crates/ui-egui/src/panels/graphic_styles.rs | 2 +- crates/ui-egui/src/panels/library_panel.rs | 30 ++++----- .../ui-egui/src/panels/library_panel/tests.rs | 65 +++++++++++++++++++ .../ui-egui/src/panels/swatches/libraries.rs | 7 +- crates/ui-egui/src/panels/symbols.rs | 4 +- 6 files changed, 83 insertions(+), 27 deletions(-) diff --git a/crates/ui-egui/src/panels/brushes.rs b/crates/ui-egui/src/panels/brushes.rs index 3311617585..6f670ae2fd 100644 --- a/crates/ui-egui/src/panels/brushes.rs +++ b/crates/ui-egui/src/panels/brushes.rs @@ -944,7 +944,7 @@ pub(crate) fn load_library(app: &mut VectorcraftApp, params: Value) -> Result) -> Result { let filters = library_panel::library_pick_filters(BrushLibraries::KIND); - library_panel::pick_library_file(app, path, filters, |app, path| load_library(app, json!({ "path": path }))) + library_panel::pick_library_file(app, path, filters, load_library) } /// The id prefix of the Window → Brush Libraries → User Defined slots. diff --git a/crates/ui-egui/src/panels/graphic_styles.rs b/crates/ui-egui/src/panels/graphic_styles.rs index 1d089378d3..cf42d21f00 100644 --- a/crates/ui-egui/src/panels/graphic_styles.rs +++ b/crates/ui-egui/src/panels/graphic_styles.rs @@ -1041,7 +1041,7 @@ pub(crate) fn load_library(app: &mut VectorcraftApp, params: Value) -> Result) -> Result { let filters = library_panel::library_pick_filters(GraphicStyleLibraries::KIND); - library_panel::pick_library_file(app, path, filters, |app, path| load_library(app, json!({ "path": path }))) + library_panel::pick_library_file(app, path, filters, load_library) } /// The id prefix of the Window → Graphic Style Libraries → User Defined slots. diff --git a/crates/ui-egui/src/panels/library_panel.rs b/crates/ui-egui/src/panels/library_panel.rs index f0eb4a72e2..4ed813fb6a 100644 --- a/crates/ui-egui/src/panels/library_panel.rs +++ b/crates/ui-egui/src/panels/library_panel.rs @@ -20,7 +20,7 @@ use std::cell::RefCell; use std::collections::HashMap; use craft::behavior::{IconId, Selection, SelectionModel}; -use craft::host::{PickRequest, Picked}; +use craft::host::PickRequest; use craft::theme::variant::Tone; use craft::ui::contract::{Bind, MemoKey, MemoProps, SelectMode, Show as _, ShowContainer as _, Trigger}; use craft::ui::menu::{Menu, MenuItem, PanelRow}; @@ -113,7 +113,7 @@ pub(crate) trait LibraryKind { const OPEN: &'static str; /// The engine command listing the libraries, for error messages. const LIST: &'static str; - /// The engine command loading a library from a file (`{path}` → `{library}`). + /// The engine command loading a library by path or by file name and contents (`{library}`). const LOAD: &'static str; /// The libraries menu's tooltip ("Swatch Libraries Menu"). const MENU: &'static str; @@ -502,13 +502,13 @@ pub(crate) fn locate(app: &mut VectorcraftApp, p: &Value) -> Result Result { +/// Locate Library… answered with the chosen file for missing library panel `given`. +fn located(app: &mut VectorcraftApp, given: &str, params: Value) -> Result { let (k, id) = parse(given).ok_or_else(|| format!("`{given}` isn't a library panel (see window.swatchLibrary)"))?; - let r = app.run(k.load, json!({ "path": path }))?; + let r = app.run(k.load, params)?; let next = r["library"].as_str().unwrap_or_default().to_string(); let to = panel_id(k.kind, &next); if to != given && is_open(&app.ui, &to) { @@ -543,25 +543,21 @@ pub(crate) fn library_pick_filters(kind: &str) -> Vec<(&'static str, &'static [& } } -/// Other Library… and Locate Library…: `then` loads the library file at `path`, else one picked in -/// an open dialog. The pick is asked of the app (`file_asks`) and answers on a later pass, which -/// runs `then` with the picked file; until then this returns what `file_asks::ask` returns. +/// Other Library… and Locate Library…: `then` gets the load command parameters for the file at +/// `path`, or for the picked file. A browser pick supplies the name and contents. pub(crate) fn pick_library_file( app: &mut VectorcraftApp, path: Option, filters: Vec<(&'static str, &'static [&'static str])>, - then: impl FnOnce(&mut VectorcraftApp, String) -> Result + 'static, + then: impl FnOnce(&mut VectorcraftApp, Value) -> Result + 'static, ) -> Result { if let Some(path) = path { - return then(app, path); + return then(app, json!({ "path": path })); } let request = filters.into_iter().fold(PickRequest::open("library"), |r, (name, exts)| r.filter(name, exts)); - Ok(file_asks::ask(app, request, Owner::Free, move |app, picked| match picked { - Picked::Paths(paths) => match paths.into_iter().next() { - Some(path) => then(app, path.to_string_lossy().into_owned()), - None => Ok(Value::Null), - }, - _ => Ok(Value::Null), + Ok(file_asks::ask(app, request, Owner::Free, move |app, picked| match file_asks::open_params(&picked) { + Some(params) => then(app, Value::Object(params)), + None => 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 1844bb5f5f..c950725965 100644 --- a/crates/ui-egui/src/panels/library_panel/tests.rs +++ b/crates/ui-egui/src/panels/library_panel/tests.rs @@ -247,6 +247,45 @@ fn saving_to_a_file_and_opening_a_palette_file() { assert!(!builtin_library(&open.id) && builtin_library(vectorcraft_engine::cmd::swatchlib::DOCUMENT_SWATCHES)); } +fn browser_library(filename: &str, bytes: &[u8], entry: &str) -> Option { + let mut app = app(); + app.services.download = Some(Box::new(|_, _| Ok(()))); + let picker = scripted(&mut app); + let undo = undo_len(&app); + crate::tests::picks::asks(&mut app, &format!("{}.other", K::OPEN)); + picker.answer_contents(&[(filename, bytes)]); + let ctx = app.ui_ctx.clone().unwrap(); + crate::tests::picks::poll(&mut app, &ctx); + assert_eq!((app.session.documents().len(), undo_len(&app)), (1, undo), "loading a library does not change the drawing"); + let open = last_open(&app.ui)?; + assert_eq!(open.kind, K::KIND); + assert!(is_open(&app.ui, &open.panel())); + let (name, library) = K::get(&app, &open.id)?; + assert!(K::rows(&library, &open.id).iter().any(|row| row.name == entry && row.item.is_some()), "{entry} is in the {name} library"); + Some(name) +} + +#[test] +fn other_library_opens_browser_pick_contents_for_each_panel_kind() { + use crate::panels::{brushes::BrushLibraries, graphic_styles::GraphicStyleLibraries, symbols::SymbolLibraries}; + + let mut source = app(); + source.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 40, "height": 40})).unwrap(); + source.run("graphicStyle.new", json!({"name": "Studio Style"})).unwrap(); + source.run("symbol.new", json!({"name": "Studio Symbol"})).unwrap(); + source.run("brush.new", json!({"type": "bristle", "name": "Studio Brush"})).unwrap(); + let encoded = source.run("document.serialize", json!({})).unwrap(); + let document = vectorcraft_format::base64_decode(encoded["dataBase64"].as_str().unwrap()).unwrap(); + let palette = b"GIMP Palette\nName: Browser Palette\n#\n255 0 0\tBrowser Red\n"; + let loaded = [ + browser_library::("Browser Palette.gpl", palette, "Browser Red"), + browser_library::("Browser Studio.vectorcraft", &document, "Studio Brush"), + browser_library::("Browser Studio.vectorcraft", &document, "Studio Symbol"), + browser_library::("Browser Studio.vectorcraft", &document, "Studio Style"), + ]; + assert_eq!(loaded.map(|name| name.unwrap_or_default()), ["Browser Palette", "Browser Studio", "Browser Studio", "Browser Studio"]); +} + #[test] fn the_save_dialog_writes_swatch_exchange_files() { let written = std::rc::Rc::new(std::cell::RefCell::new(vec![])); @@ -488,6 +527,32 @@ fn a_persistent_panel_whose_file_is_gone_is_listed_as_missing_until_located() { let _ = std::fs::remove_dir_all(dir); } +#[test] +fn locate_library_loads_browser_contents_into_the_missing_panel() { + let path = std::env::temp_dir().join(format!("vc-browser-locate-{}.gpl", std::process::id())); + let palette = b"GIMP Palette\nName: Replacement\n#\n255 0 0\tNew Red\n"; + std::fs::write(&path, palette).unwrap(); + let mut app = app(); + let missing = panel_of(app.run("window.swatchLibrary.other", json!({"path": path})).unwrap()); + app.run("ui.libraryPanel", json!({"panel": missing, "persistent": true, "view": "smallList"})).unwrap(); + std::fs::remove_file(&path).unwrap(); + + let mut app = restarted(&app); + tidy(&mut app); + app.services.download = Some(Box::new(|_, _| Ok(()))); + let picker = scripted(&mut app); + assert_eq!(app.run("ui.libraryPanel.locate", json!({"panel": missing})), Ok(json!({"pending": "fileDialog"}))); + picker.answer_contents(&[("Replacement.gpl", palette)]); + let ctx = app.ui_ctx.clone().unwrap(); + crate::tests::picks::poll(&mut app, &ctx); + + assert!(!is_open(&app.ui, &missing), "the missing panel has been recovered"); + let panel = last_open(&app.ui).unwrap(); + assert!(is_open(&app.ui, &panel.panel())); + assert_eq!((panel.persistent, panel.view), (true, Some(View::SmallList))); + assert!(SwatchLibraries::get(&app, &panel.id).is_some_and(|(_, lib)| lib.swatch("New Red").is_some())); +} + #[test] fn locating_a_library_open_in_its_own_panel_brings_that_panel_forward() { let dir = std::env::temp_dir().join(format!("vc-locate-open-library-{}", std::process::id())); diff --git a/crates/ui-egui/src/panels/swatches/libraries.rs b/crates/ui-egui/src/panels/swatches/libraries.rs index e255b6757d..4f16980b9a 100644 --- a/crates/ui-egui/src/panels/swatches/libraries.rs +++ b/crates/ui-egui/src/panels/swatches/libraries.rs @@ -200,12 +200,9 @@ pub(crate) fn load_library(app: &mut VectorcraftApp, params: Value) -> Result) -> Result { - library_panel::pick_library_file(app, path, library_panel::library_pick_filters(SwatchLibraries::KIND), |app, path| { - load_library(app, json!({ "path": path })) - }) + library_panel::pick_library_file(app, path, library_panel::library_pick_filters(SwatchLibraries::KIND), load_library) } /// The id prefix of the Window → Swatch Libraries → User Defined slots. diff --git a/crates/ui-egui/src/panels/symbols.rs b/crates/ui-egui/src/panels/symbols.rs index 7143c2d88e..321a0a7c6d 100644 --- a/crates/ui-egui/src/panels/symbols.rs +++ b/crates/ui-egui/src/panels/symbols.rs @@ -1037,9 +1037,7 @@ pub(crate) fn load_library(app: &mut VectorcraftApp, params: Value) -> Result) -> Result { - library_panel::pick_library_file(app, path, library_panel::library_pick_filters(SymbolLibraries::KIND), |app, path| { - load_library(app, json!({ "path": path })) - }) + library_panel::pick_library_file(app, path, library_panel::library_pick_filters(SymbolLibraries::KIND), load_library) } /// The id prefix of the Window → Symbol Libraries → User Defined slots.