Scripts: stopping from the quit prompt between startup runs drops the queued runs

A startup script refused before it begins, such as with an undo group
open, is named in the Script Console. Stopping the runs from the quit
prompt between two queued runs drops the runs not yet started, and the
status bar says so. While that prompt is open, or before its answer is
taken, the next run does not start.
This commit is contained in:
dotCooCoo committed 2026-11-25 17:33:55 -08:00
1 parent f341cb5b0d
commit 83184e76bd
7 files changed
+155 -10

No files matched your search

+8 -2
View File
@@ -417,7 +417,7 @@ impl Session {
self.startup_loading = Some(vectorcraft_plugins::safe_mode::loading(&item));
let locale = script::dollar::ui_locale(self.ui_language.as_deref().unwrap_or(&self.prefs.interface_language));
let request = script::Request {
name,
name: name.clone(),
name_given: false,
file: Some(path.to_string_lossy().into_owned()),
checked_real: Some(real),
@@ -427,9 +427,15 @@ impl Session {
locale,
answers: vec![],
};
let runs = self.script_runs;
self.script_start(request).map_err(|e| {
self.startup_ended();
e.to_string()
let message = e.to_string();
// A run refused before it began, such as with an undo group open, has no console entry yet.
if self.script_runs == runs {
self.note_unread(&name, &message);
}
message
})
}
+4 -2
View File
@@ -1919,13 +1919,15 @@ impl Session {
}
/// Stops the running script at once and puts everything back, with the report of a stopped run
/// ([`Self::script_report`]). False with none running.
/// ([`Self::script_report`]). Between two runs `script.startup.run` queued, the queued runs are
/// not started, and the report stays the last run's. False with none running or queued.
pub fn script_abandon(&mut self) -> bool {
// The runs queued after it are not started.
let queued = !self.script_queue.is_empty();
self.script_queue.clear();
self.script_queue_scope = None;
self.startup_ended();
let Some(key) = self.script_key.take() else { return false };
let Some(key) = self.script_key.take() else { return queued };
let Some(job) = JOBS.with(|jobs| jobs.borrow_mut().remove(&key.id)) else {
self.script_gone(key, console::Ending::Stopped);
return true;
+33
View File
@@ -345,6 +345,39 @@ fn in_the_app_a_stop_between_two_runs_stops_the_run_that_starts_next() {
}
}
#[test]
fn a_startup_script_refused_before_it_begins_is_in_the_script_console() {
let _f = fresh();
let dir = tmp_dir("refused");
write(&dir, "a.jsx", "vc.log(1);");
let mut s = session(&dir);
s.execute("file.new", &json!({})).unwrap();
s.execute("edit.undoGroup.begin", &json!({})).unwrap();
let r = s.execute("script.startup.run", &json!({"reason": "launch"})).unwrap();
let error = r["failed"][0]["error"].as_str().unwrap().to_string();
assert!(error.contains("undo group"), "{r}");
assert_eq!(started(&s), ["a.jsx"]);
assert_eq!(console(&s, "error"), [format!("a.jsx: {error}")]);
}
#[test]
fn in_the_app_abandoning_between_two_runs_drops_the_queued_runs() {
let _f = fresh();
let dir = tmp_dir("app-abandon-between");
write(&dir, "a.jsx", "vc.log(1);");
write(&dir, "b.jsx", "vc.log(2);");
let mut s = session(&dir);
s.script_frames = true;
let then = json!({"source": "vc.log(3);", "name": "chosen"});
s.execute("script.startup.run", &json!({"reason": "menu", "then": then})).unwrap();
assert_eq!((0..10_000).find_map(|_| s.script_pump()).unwrap().unwrap()["ok"], true, "a.jsx ends");
assert!(s.script_running() && s.script_name().is_none(), "b.jsx waits for the next pump");
assert!(s.script_abandon(), "the queued runs are stopped");
assert!(!s.script_running());
assert!(s.script_pump().is_none());
assert_eq!(started(&s), ["a.jsx"]);
}
#[test]
fn in_the_app_startup_scripts_that_cannot_start_are_in_the_reply() {
let _f = fresh();
+32 -5
View File
@@ -408,6 +408,11 @@ pub(crate) fn with_host<R>(app: &VectorcraftApp, f: impl FnOnce(&mut DialogHost,
Some(app.dialogs()?.with(|host| f(host, &ctx)))
}
/// Whether the question asked on quitting with a script running is open.
pub(crate) fn asking_to_close(app: &VectorcraftApp) -> bool {
with_host(app, |host, _| host.is_open::<CloseQuestion>()).unwrap_or(false)
}
/// Whether a dialog is open or waits to open: a script's dialog opens only over nothing.
pub(crate) fn busy(app: &VectorcraftApp) -> bool {
app.any_dialog_open() || with_host(app, |host, _| !host.is_empty()).unwrap_or(false)
@@ -578,20 +583,42 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) {
}
None => {}
}
answer_close(app, close);
if with_host(app, |host, _| host.get_mut::<ScriptFiles>().is_some_and(|d| std::mem::take(&mut d.choose))).unwrap_or(false) {
choose(app);
}
}
/// Takes the answer to the quit question given between two queued runs, before the next run
/// starts. True when it stopped the runs.
pub(crate) fn answer_close_between_runs(app: &mut VectorcraftApp) -> bool {
let Some(close) = with_host(app, |host, _| host.take::<CloseQuestion>()).flatten() else {
return false;
};
answer_close(app, Some(close));
!app.session.script_running()
}
/// With Stop as the answer to the quit question, stops the run, or between two queued runs, the
/// runs not yet started.
fn answer_close(app: &mut VectorcraftApp, close: Option<DialogResult<<CloseQuestion as StackDialog>::Result>>) {
let startup = app.session.startup_script_running();
// Between two queued runs: none runs, and the report is the last run's.
let between = app.session.script_running() && app.session.script_name().is_none();
if let Some(DialogResult::Ok(answer)) = close
&& answer.choice == PromptChoice::Proceed
&& app.session.script_abandon()
{
let error = app.session.script_failure_text().unwrap_or_default();
crate::script_runs::ended_with_error(app, startup, error);
if between {
crate::script_runs::stopped_between_runs(app);
} else {
let error = app.session.script_failure_text().unwrap_or_default();
crate::script_runs::ended_with_error(app, startup, error);
}
forget::<ScriptNotice>(app);
forget::<ScriptFiles>(app);
app.sync_views();
}
if with_host(app, |host, _| host.get_mut::<ScriptFiles>().is_some_and(|d| std::mem::take(&mut d.choose))).unwrap_or(false) {
choose(app);
}
}
/// Gives the running script the answer of its dialog. Cancel is Cancel for a prompt, No for a
+19
View File
@@ -18,6 +18,10 @@ pub fn poll(app: &mut VectorcraftApp) {
if ask(app) {
return;
}
// Between two queued runs, the next one waits for the answer to the quit question.
if app.session.script_name().is_none() && (script_run::asking_to_close(app) || script_run::answer_close_between_runs(app)) {
return;
}
let name = app.session.script_name().unwrap_or_default();
let startup = app.session.startup_script_running();
let Some(result) = app.session.script_pump() else {
@@ -213,3 +217,18 @@ pub(crate) fn failed(app: &mut VectorcraftApp, error: String) {
app.status(error);
}
}
/// Shows that the runs `script.startup.run` queued were stopped between two of them, before the
/// next one started. The message shows first, before the round's notes ([`end_round`]).
pub(crate) fn stopped_between_runs(app: &mut VectorcraftApp) {
let message = "The scripts waiting to run were not run";
let mut notes = vec![message.to_string()];
notes.append(&mut app.ui.startup_notes);
if notes.len() == 1 {
// Notes an earlier round left for the same message are not this round's.
app.ui.status_notes = None;
app.status(message);
} else {
show_notes(app, notes);
}
}
+57
View File
@@ -278,6 +278,63 @@ fn a_startup_script_stopped_by_quitting_stays_in_the_status_bar() {
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn quitting_between_a_startup_script_and_the_chosen_script_stops_the_chosen_script() {
let mut app = app();
let dir = folder("startup-quit-between");
// A first startup script that cannot start leaves a note for the round.
std::fs::write(dir.join("0.jsx"), [0xff, 0xfe, 0x00]).unwrap();
std::fs::write(dir.join("a.jsx"), "vc.log(1);").unwrap();
app.session.attach_startup_scripts_dir(dir.clone());
app.session.prefs.show_external_script_warning = false;
app.run("prefs.set", json!({"key": "runStartupScripts", "value": true})).unwrap();
menus::run_ui_command(&mut app, "ui.runScript", &json!({ "source": DRAW, "name": "chosen" })).unwrap().unwrap();
for _ in 0..10_000 {
if app.session.script_name().is_none() {
break;
}
script_runs::poll(&mut app);
}
assert!(app.session.script_running() && app.session.script_name().is_none(), "the chosen script waits for the next frame");
script_run::ask_to_close(&mut app);
// Frames drawn while the question is open do not start the chosen script.
for _ in 0..3 {
script_runs::poll(&mut app);
}
assert!(app.session.script_running() && app.session.script_name().is_none(), "the chosen script waits for the answer");
// A frame takes the answer before it would start the next run, then pumps the dialogs.
let id = open_id(&app, CLOSE_KIND);
assert!(script_run::with_host(&app, |host, ctx| host.confirm_by_id(ctx, id)).unwrap());
script_runs::poll(&mut app);
assert!(!app.session.script_running());
pump(&mut app);
script_runs::poll(&mut app);
let message = "The scripts waiting to run were not run";
assert_eq!(app.ui.status, message);
let notes = app.ui.status_notes.clone().map(|(_, notes)| notes).unwrap_or_default();
assert_eq!(notes.len(), 2, "{notes:?}");
assert_eq!(notes[0], message);
assert!(notes[1].starts_with("0.jsx: "), "{notes:?}");
assert_eq!(objects(&app), 0, "the chosen script did not run");
// A later round stopped the same way, with no notes of its own, does not show the earlier round's.
std::fs::remove_file(dir.join("0.jsx")).unwrap();
menus::run_ui_command(&mut app, "ui.runScript", &json!({ "source": DRAW, "name": "chosen" })).unwrap().unwrap();
for _ in 0..10_000 {
if app.session.script_name().is_none() {
break;
}
script_runs::poll(&mut app);
}
assert!(app.session.script_running() && app.session.script_name().is_none());
script_run::ask_to_close(&mut app);
assert!(confirm(&mut app, CLOSE_KIND));
script_runs::poll(&mut app);
assert_eq!(app.ui.status, message);
assert_eq!(app.ui.status_notes, None);
assert_eq!(objects(&app), 0);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_stop_names_the_files_the_run_left_written() {
let mut app = app();
+2 -1
View File
@@ -3310,7 +3310,8 @@ disabled, truncated, unreadable}`, with `then` holding `script.run`'s reply. In
reply is `{reason, skipped: false, running, queued, failed, disabled, truncated, unreadable}`, with `failed` naming
the runs that could not start before the first one did. Each run then reports as a `script.run` in the app does, the
next one starts at the frame after, and a later run that cannot start, the chosen script included, is named in the
status bar. Stop (`script.stop`) asked for between two runs stops the run that starts next. Under `--automation-read-root` it reads the folder only when the folder lies inside a
status bar. Stop (`script.stop`) asked for between two runs stops the run that starts next. In the app, stopping the runs from
the prompt shown on quitting, between two runs, drops the runs not yet started, and the status bar says so. While that prompt is open, the next run does not start. Under `--automation-read-root` it reads the folder only when the folder lies inside a
read root. With the preference on, `ui.runScript` and `ui.runInstalledScript` still return `script.run`'s reply,
or its error when the chosen script cannot start, and add `startup` holding the `script.startup.run` reply. While a
startup script runs before the chosen script, that reply is `{running: true, queued: true}`, and a chosen script that