diff --git a/crates/ui-egui/src/dialogs/screen_formats.rs b/crates/ui-egui/src/dialogs/screen_formats.rs index f85cdfe558..2ff5b5bf69 100644 --- a/crates/ui-egui/src/dialogs/screen_formats.rs +++ b/crates/ui-egui/src/dialogs/screen_formats.rs @@ -14,7 +14,7 @@ use std::sync::LazyLock; use craft::behavior::{ColumnWidth, IconId}; use craft::egui::{Response, Ui}; -use craft::host::{PickRequest, Picked}; +use craft::host::PickRequest; use craft::ui::buttons::{Button, IconButton}; use craft::ui::contract::overlay::Trigger; use craft::ui::contract::{Bind, Entry, Props as _, Show as _, ShowContainer as _}; @@ -178,26 +178,16 @@ pub(crate) fn save_preset_row(ui: &mut Ui, fields: &mut Map) -> O } /// Import Preset…: the presets of a picked file are added to the saved ones; `then` gets the names -/// added, or the error (`cancelled` when no file was picked). On the web the picked file arrives -/// with its bytes and imports through `io::open_bytes`, which reports no names. +/// added, or the error (`cancelled` when no file was picked). Browser picks use the file's contents. pub(crate) fn import_presets(app: &mut VectorcraftApp, then: impl FnOnce(&mut VectorcraftApp, Result, String>) + 'static) { let request = PickRequest::open("export-presets").filter("Export presets", fileio::SCREEN_EXPORT_PRESET_EXTS); // `then` reports its own result, so the continuation never fails (a failure would reach the status line twice). let _ = file_asks::ask(app, request, Owner::Free, move |app, picked| { - let result: Result, String> = match picked { - Picked::Contents(files) => match files.first() { - Some((name, bytes)) => io::open_bytes(app, name, bytes, None).map(|_| Vec::new()), - None => Err("cancelled".into()), - }, - other => match other.first_path() { - Some(path) => { - let path = path.to_string_lossy().into_owned(); - io::import_presets(app, "document.exportPresets.import", io::EXPORT_PRESETS, json!({ "path": path })) - .map(|r| r["imported"].as_array().into_iter().flatten().filter_map(Value::as_str).map(str::to_string).collect()) - } - None => Err("cancelled".into()), - }, - }; + let result = file_asks::open_params(&picked).ok_or_else(|| "cancelled".to_owned()).and_then(|mut params| { + params.remove("name"); + io::import_presets(app, "document.exportPresets.import", io::EXPORT_PRESETS, Value::Object(params)) + .map(|r| r["imported"].as_array().into_iter().flatten().filter_map(Value::as_str).map(str::to_string).collect()) + }); then(app, result); Ok(Value::Null) }); @@ -488,7 +478,6 @@ pub(crate) fn rows(ui: &mut Ui, fields: &mut Map, mut menu: Optio } shown.output } - /// The format Format Settings opens on for format rows `rows`: the first row's format when it has /// settings of its own, else PNG. Row spellings (`jpeg`, `tif`) give the canonical id. pub(crate) fn settings_format(rows: &[Value]) -> &'static str { @@ -561,3 +550,39 @@ pub(crate) fn settings(ui: &mut Ui, fields: &mut Map, presets: &[ } shown.output } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn the_browser_preset_picker_routes_by_content_without_opening_drawings() { + let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); + app.services.download = Some(Box::new(|_, _| Ok(()))); + app.run("file.new", json!({"width": 200, "height": 200})).unwrap(); + app.run("document.exportPresets.save", json!({"name": "Fleet", "settings": {"formats": [{"format": "png"}]}})).unwrap(); + let export = app.run("document.exportPresets.export", json!({"names": ["Fleet"]})).unwrap(); + let bytes = export["data"].as_str().unwrap().as_bytes(); + app.run("document.exportPresets.delete", json!({"name": "Fleet"})).unwrap(); + let document = app.session.active().unwrap().uid; + let picker = crate::tests::picks::scripted(&mut app); + let ctx = app.ui_ctx.clone().unwrap(); + let picked = |app: &mut VectorcraftApp, name: &str, bytes: &[u8]| { + let answer = Rc::new(Cell::new(None)); + let report = Rc::clone(&answer); + import_presets(app, move |_, result| report.set(Some(result))); + picker.answer_contents(&[(name, bytes)]); + crate::tests::picks::poll(app, &ctx); + answer.take().unwrap() + }; + + let svg = br#""#; + let error = picked(&mut app, "drawing.svg", svg); + assert_eq!(app.session.documents().len(), 1, "Import Preset must not open a drawing"); + assert!(error.unwrap_err().contains("not an export presets file")); + + assert_eq!(picked(&mut app, "different-extension.svg", bytes), Ok(vec!["Fleet".to_string()])); + assert_eq!(app.ui.status, "Imported export presets: Fleet"); + assert_eq!(app.session.active().unwrap().uid, document); + } +}