mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 16:59:43 +01:00
Load browser-selected files in library panels
This commit is contained in:
1 parent
5c7bbeecaa
commit
49d67e781f
6 files changed
+83
-27
No files matched your search
@@ -944,7 +944,7 @@ pub(crate) fn load_library(app: &mut VectorcraftApp, params: Value) -> Result<Va
|
||||
/// dialog.
|
||||
pub(crate) fn other_library(app: &mut VectorcraftApp, path: Option<String>) -> Result<Value, String> {
|
||||
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.
|
||||
|
||||
@@ -1041,7 +1041,7 @@ pub(crate) fn load_library(app: &mut VectorcraftApp, params: Value) -> Result<Va
|
||||
/// Other Library…: load the library (or document) at `path`, else one picked in an open dialog.
|
||||
pub(crate) fn other_library(app: &mut VectorcraftApp, path: Option<String>) -> Result<Value, String> {
|
||||
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.
|
||||
|
||||
@@ -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<Value, Strin
|
||||
}
|
||||
let path = p.get("path").and_then(Value::as_str).map(str::to_string);
|
||||
let panel = given.to_string();
|
||||
pick_library_file(app, path, library_pick_filters(k.kind), move |app, path| located(app, &panel, &path))
|
||||
pick_library_file(app, path, library_pick_filters(k.kind), move |app, params| located(app, &panel, params))
|
||||
}
|
||||
|
||||
/// Locate Library… answered with the file at `path` for missing library panel `given`.
|
||||
fn located(app: &mut VectorcraftApp, given: &str, path: &str) -> Result<Value, String> {
|
||||
/// Locate Library… answered with the chosen file for missing library panel `given`.
|
||||
fn located(app: &mut VectorcraftApp, given: &str, params: Value) -> Result<Value, String> {
|
||||
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<String>,
|
||||
filters: Vec<(&'static str, &'static [&'static str])>,
|
||||
then: impl FnOnce(&mut VectorcraftApp, String) -> Result<Value, String> + 'static,
|
||||
then: impl FnOnce(&mut VectorcraftApp, Value) -> Result<Value, String> + 'static,
|
||||
) -> Result<Value, String> {
|
||||
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),
|
||||
}))
|
||||
}
|
||||
|
||||
|
||||
@@ -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<K: LibraryKind>(filename: &str, bytes: &[u8], entry: &str) -> Option<String> {
|
||||
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::<SwatchLibraries>("Browser Palette.gpl", palette, "Browser Red"),
|
||||
browser_library::<BrushLibraries>("Browser Studio.vectorcraft", &document, "Studio Brush"),
|
||||
browser_library::<SymbolLibraries>("Browser Studio.vectorcraft", &document, "Studio Symbol"),
|
||||
browser_library::<GraphicStyleLibraries>("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()));
|
||||
|
||||
@@ -200,12 +200,9 @@ pub(crate) fn load_library(app: &mut VectorcraftApp, params: Value) -> Result<Va
|
||||
open_library(app, &json!({ "library": r["library"] }))
|
||||
}
|
||||
|
||||
/// Other Library…: load the library (or document) at `path`, else one picked in an open dialog
|
||||
/// (on the web the picked file arrives later and opens through [`crate::io::open_bytes`]).
|
||||
/// Other Library…: load the library (or document) at `path`, else one picked in an open dialog.
|
||||
pub(crate) fn other_library(app: &mut VectorcraftApp, path: Option<String>) -> Result<Value, String> {
|
||||
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.
|
||||
|
||||
@@ -1037,9 +1037,7 @@ pub(crate) fn load_library(app: &mut VectorcraftApp, params: Value) -> Result<Va
|
||||
/// Other Library…: load the symbols of the document at `path`, else of one picked in an open
|
||||
/// dialog.
|
||||
pub(crate) fn other_library(app: &mut VectorcraftApp, path: Option<String>) -> Result<Value, String> {
|
||||
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.
|
||||
|
||||
Reference in new issue
Block a user