From e8327cc3d8ff70889ecb8e1c224529db8a5ad0fa Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 16:29:53 -0800 Subject: [PATCH] Create untitled copies from browser template picks --- crates/ui-egui/src/io/open.rs | 24 ++++++++++++++--- crates/ui-egui/src/tests/save.rs | 45 ++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/crates/ui-egui/src/io/open.rs b/crates/ui-egui/src/io/open.rs index 98464ba721..d3712ed83a 100644 --- a/crates/ui-egui/src/io/open.rs +++ b/crates/ui-egui/src/io/open.rs @@ -646,13 +646,25 @@ fn open_file_with(app: &mut VectorcraftApp, path: &str, background: bool, p: &Va } /// File → New from Template…: a template (or any readable file) as a new untitled document. Without -/// a path the open dialog starts in the Templates folder. On the web the browser's file picker -/// opens it (`.vctemplate`, `.ait` and template files open untitled there too). +/// a path the open dialog starts in the Templates folder. The browser supplies the picked file's +/// name and bytes for the same template loader. pub fn new_from_template(app: &mut VectorcraftApp, path: Option) -> Result { let Some(path) = path else { let filters = std::iter::once(("Templates", TEMPLATE_EXTS)).chain(fileio::builtin_open_filters()); let request = open_request("template", fileio::templates_dialog_folder(&app.session.prefs), filters); - return ask_open(app, request, Owner::Free, new_from_path); + return Ok(file_asks::ask(app, request, Owner::Free, |app, picked| match picked { + Picked::Contents(files) => { + let (name, bytes) = files.into_iter().next().ok_or("cancelled")?; + if let Err(error) = open_template(app, &name, &bytes, None) { + report_open_error(app, &name, &error); + } + Ok(Value::Null) + } + picked => { + let path = picked.first_path().ok_or("cancelled")?; + new_from_path(app, path.to_string_lossy().into_owned()) + } + })); }; new_from_path(app, path) } @@ -660,7 +672,11 @@ pub fn new_from_template(app: &mut VectorcraftApp, path: Option) -> Resu /// [`new_from_template`] for the file at `path`. fn new_from_path(app: &mut VectorcraftApp, path: String) -> Result { let bytes = read(app, &path)?; - let r = fileio::open_template(&mut app.session, &path, &bytes, Some(&path)).map_err(|e| e.to_string())?; + open_template(app, &path, &bytes, Some(&path)) +} + +fn open_template(app: &mut VectorcraftApp, name: &str, bytes: &[u8], path: Option<&str>) -> Result { + let r = fileio::open_template(&mut app.session, name, bytes, path).map_err(|e| e.to_string())?; opened(app, &r); Ok(r) } diff --git a/crates/ui-egui/src/tests/save.rs b/crates/ui-egui/src/tests/save.rs index e92f4bbdf9..f11ef23a35 100644 --- a/crates/ui-egui/src/tests/save.rs +++ b/crates/ui-egui/src/tests/save.rs @@ -170,6 +170,51 @@ fn new_from_template_takes_bytes_from_agents() { assert_eq!(app.session.active().unwrap().path, None); } +#[test] +fn new_from_template_opens_browser_bytes_and_desktop_paths_as_untitled_copies() { + let folder = dir("new-from-template-pick"); + let name = "Source Drawing.svg"; + let bytes = br#""#; + let path = folder.join(name); + std::fs::write(&path, bytes).unwrap(); + for browser in [false, true] { + let mut app = app(); + if browser { + app.services.download = Some(Box::new(|_, _| Ok(()))); + } + app.run("file.new", json!({"width": 300, "height": 200})).unwrap(); + rect(&mut app); + let source = app.session.active().unwrap(); + let source_uid = source.uid; + let source_undo = source.history.undo.len(); + let picker = scripted(&mut app); + picks::asks(&mut app, "file.newFromTemplate"); + assert_eq!(picker.requests()[0].purpose, "template"); + if browser { + picker.answer_contents(&[(name, bytes)]); + } else { + picker.answer(&[path.to_str().unwrap()]); + } + poll(&mut app); + + let opened = app.session.active().unwrap(); + assert!(opened.title().starts_with("Untitled-"), "browser={browser}: the template copy was named {:?}", opened.title()); + assert_eq!((app.session.documents().len(), app.views.len()), (2, 2)); + assert_eq!((opened.path.as_deref(), opened.history.undo.len()), (None, 0)); + assert_eq!(opened.doc.layers[0].children().unwrap().len(), 1); + let source = &app.session.documents()[0]; + assert_eq!((source.uid, source.history.undo.len()), (source_uid, source_undo)); + if browser { + picks::asks(&mut app, "file.newFromTemplate"); + picker.answer_contents(&[(name, b"not a drawing")]); + poll(&mut app); + assert_eq!(app.session.documents().len(), 2, "an invalid template opens no document"); + assert!(app.ui.status.contains("Couldn't open Source Drawing.svg"), "{}", app.ui.status); + } + } + let _ = std::fs::remove_dir_all(folder); +} + #[test] fn show_in_folder_needs_a_saved_document_and_a_file_manager() { let shown = Rc::new(RefCell::new(vec![]));