From 826248f56a6216fe1e08ea6b6d241be632d50cd2 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 16:15:31 -0800 Subject: [PATCH] Load and replace Action sets from browser-picked contents --- crates/ui-egui/src/panels/actions/files.rs | 29 ++++++++---- .../ui-egui/src/panels/actions/tests_files.rs | 46 +++++++++++++++++++ 2 files changed, 67 insertions(+), 8 deletions(-) diff --git a/crates/ui-egui/src/panels/actions/files.rs b/crates/ui-egui/src/panels/actions/files.rs index 9672ee1c64..dd6981c763 100644 --- a/crates/ui-egui/src/panels/actions/files.rs +++ b/crates/ui-egui/src/panels/actions/files.rs @@ -20,7 +20,7 @@ //! `actionSet.read`), a Set Selection step as a Select Object step. use craft::automation::actions::{Action, ActionColor, ActionStep, ActionsFile}; -use craft::host::{PickKind, PickRequest}; // PLAN: assumed path of the host's picker types in the app's `craft` facade +use craft::host::{PickKind, PickRequest, Picked}; // PLAN: assumed path of the host's picker types in the app's `craft` facade use serde::{Deserialize, Serialize}; use serde_json::{Value, json}; @@ -577,15 +577,20 @@ fn read_path(app: &mut VectorcraftApp, path: &str) -> Result { (None, Some(read)) => read(path)?, (None, None) => return Err("no file reader".into()), }; + read_bytes(app, path, &bytes) +} + +/// A desktop or browser file's contents, with its name used only as the unnamed set's fallback. +fn read_bytes(app: &mut VectorcraftApp, name: &str, bytes: &[u8]) -> Result { if bytes.len() > MAX_SET_FILE { return Err("the action set file is larger than 8 MiB".into()); } - if is_aia(&bytes) { - return read_aia(app, json!({ "name": path, "dataBase64": vectorcraft_format::base64_encode(&bytes) })); + if is_aia(bytes) { + return read_aia(app, json!({ "name": name, "dataBase64": vectorcraft_format::base64_encode(bytes) })); } - let stem = std::path::Path::new(path).file_stem().map(|s| s.to_string_lossy().into_owned()).unwrap_or_default(); - let text = String::from_utf8(bytes).map_err(|_| "not an action set file")?; - let (name, actions) = read_set(&text, if stem.is_empty() { "Actions" } else { &stem })?; + let stem = std::path::Path::new(name).file_stem().map(|s| s.to_string_lossy().into_owned()).unwrap_or_default(); + let text = std::str::from_utf8(bytes).map_err(|_| "not an action set file")?; + let (name, actions) = read_set(text, if stem.is_empty() { "Actions" } else { &stem })?; Ok(FileRead { name, actions, aia: None }) } @@ -613,8 +618,16 @@ fn read_file( let exts = [&[ACTIONS_EXT], vectorcraft_engine::cmd::actionset::EXTS].concat(); let request = PickRequest::open("actions").filter("Action sets", &exts); Ok(file_asks::ask(app, request, Owner::Free, move |app, picked| { - let path = picked.first_path().ok_or("cancelled")?; - let read = read_path(app, &path.to_string_lossy())?; + let read = match picked { + Picked::Contents(files) => { + let (name, bytes) = files.into_iter().next().ok_or("cancelled")?; + read_bytes(app, &name, &bytes)? + } + other => { + let path = other.first_path().ok_or("cancelled")?; + read_path(app, &path.to_string_lossy())? + } + }; then(app, read) })) } diff --git a/crates/ui-egui/src/panels/actions/tests_files.rs b/crates/ui-egui/src/panels/actions/tests_files.rs index 79bad2c42e..a88cbc36fe 100644 --- a/crates/ui-egui/src/panels/actions/tests_files.rs +++ b/crates/ui-egui/src/panels/actions/tests_files.rs @@ -7,6 +7,52 @@ use super::tests_support::*; use super::*; use crate::dialogs::action_batch::BatchDialog; +#[test] +fn browser_picks_load_and_replace_action_sets_without_a_file_reader() { + let native = json!({ + "format": "vcactions", "version": 1, "name": "Browser", + "actions": [{"name": "Group", "steps": [{"command": "object.group", "params": {}}]}] + }) + .to_string(); + let mut aia = aia_text("", &[("Group", &[("group", true)])]).into_bytes(); + // AIA's text encoding is not required to be UTF-8. + aia.extend(b"/note (\xFF)\n"); + for command in [LOAD_SET, REPLACE_SET] { + for (name, bytes) in [("Browser.vcactions", native.as_bytes()), ("Browser.aia", aia.as_slice())] { + let mut app = app_with_sets(); + app.run("file.new", json!({"width": 200, "height": 100})).unwrap(); + let picker = crate::tests::picks::scripted(&mut app); + app.services.read = None; + app.services.read_head = None; + let before = saved_sets(&app); + let undo = undo_steps(&app); + app.run(command, json!({})).unwrap(); + picker.cancel(); + crate::file_asks::poll(&mut app); + assert_eq!(saved_sets(&app), before, "cancel keeps the sets"); + for invalid in [vec![0xff], vec![b' '; files::MAX_SET_FILE + 1]] { + app.run(command, json!({})).unwrap(); + picker.answer_contents(&[(name, &invalid)]); + crate::file_asks::poll(&mut app); + assert_eq!(saved_sets(&app), before, "invalid picked bytes keep the sets"); + assert!(!app.ui.status.is_empty(), "a rejected file reports why"); + } + app.run(command, json!({})).unwrap(); + picker.answer_contents(&[(name, bytes)]); + crate::file_asks::poll(&mut app); + let names = set_names(&app); + assert_eq!(names.last().map(String::as_str), Some("Browser")); + if command == REPLACE_SET { + assert_eq!(names, ["Browser"]); + } else { + assert!(names.len() > 1, "Load keeps existing sets"); + } + assert_eq!(set_actions(&app, names.len() - 1)[0].steps, [ActionStep::new("object.group", json!({}))]); + assert_eq!(undo_steps(&app), undo, "loading actions does not edit the drawing"); + } + } +} + #[test] fn defaults_are_valid_commands() { for a in default_sets().actions {