diff --git a/crates/ui-egui/src/panels/actions/playback.rs b/crates/ui-egui/src/panels/actions/playback.rs index fccf301699..bff0bd1d4d 100644 --- a/crates/ui-egui/src/panels/actions/playback.rs +++ b/crates/ui-egui/src/panels/actions/playback.rs @@ -39,14 +39,20 @@ fn playback_steps(action: &Action) -> Vec { .collect() } -/// For command `id` `script.run`: gives its `params` the panel's actions as `actions`, which the -/// script's `app.doScript` plays, unless the caller gave them: every action of every set, in panel -/// order, with its steps as playback runs them ([`playback_steps`]) and each step's place in the -/// action. An action that `actions` cannot hold (a step that is not a command, a name that is too -/// long, or one past the limits of [`vectorcraft_engine::script::actions`]) is left out. +/// For command `id` `script.run`, or the script of `script.startup.run`'s `then`: gives its +/// `params` the panel's actions as `actions`, which the script's `app.doScript` plays, unless the +/// caller gave them: every action of every set, in panel order, with its steps as playback runs +/// them ([`playback_steps`]) and each step's place in the action. An action that `actions` cannot +/// hold (a step that is not a command, a name that is too long, or one past the limits of +/// [`vectorcraft_engine::script::actions`]) is left out. pub(crate) fn add_script_actions(app: &VectorcraftApp, id: &str, params: &mut Value) { use vectorcraft_engine::script::actions::{MAX_ACTIONS, MAX_ACTIONS_BYTES, MAX_ALL_STEPS, Playable, parse}; - let Some(p) = params.as_object_mut().filter(|_| id == "script.run") else { return }; + let target = match id { + "script.run" => Some(params), + "script.startup.run" => params.get_mut("then"), + _ => None, + }; + let Some(p) = target.and_then(Value::as_object_mut) else { return }; if p.contains_key("actions") { return; } diff --git a/crates/ui-egui/src/panels/actions/tests_playback.rs b/crates/ui-egui/src/panels/actions/tests_playback.rs index 2b9a8225b5..f566eb22fc 100644 --- a/crates/ui-egui/src/panels/actions/tests_playback.rs +++ b/crates/ui-egui/src/panels/actions/tests_playback.rs @@ -674,6 +674,19 @@ fn a_script_plays_the_panels_actions() { assert_eq!(objects(&app), 2); } +/// The script that `script.startup.run` runs after the startup scripts (its `then`) plays the +/// panel's actions too, as File › Scripts runs a script with Run Startup Scripts on. +#[test] +fn the_script_after_the_startup_scripts_plays_the_panels_actions() { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.run("file.new", json!({"width": 200, "height": 200})).unwrap(); + push_set(&mut app, "Mine", vec![action("Draw", &[("shape.rectangle", json!({"x": 0, "y": 10, "width": 10, "height": 10}))])]); + app.session.script_frames = false; + let then = json!({"source": "try { app.doScript('Draw', 'Mine'); vc.log('ok'); } catch (e) { vc.log(e.message); }", "name": "t.js"}); + let r = app.run("script.startup.run", json!({"reason": "menu", "then": then})).unwrap(); + assert_eq!(r["then"]["log"], json!(["ok"]), "{r}"); +} + /// The steps a script's `app.doScript` plays act on the view's active artboard, as the panel's /// playback does: Align to Artboard in a recorded batch, and Fit to Selected Art. #[test]