From 9eaacbbaf9708e8bedefd878cc296e5695759eb3 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 22 Nov 2026 05:37:55 -0800 Subject: [PATCH] Scripts: a refused typed text keeps the ScriptUI dialog's fields, and quitting forgets an ended show() - OK, Cancel, Escape, ui.dialog.confirm and ui.dialog.cancel now check every text still typed in the dialog's edit fields before giving the script any event. When one is longer than 16 KiB, every field keeps its text, nothing is queued, and the click or cancel returns an error (Escape and the dialog's own buttons show it in the status bar). A disabled or hidden field keeps its typed text and is not sent. - The quit question forgets the ScriptUI dialog it was asked over once the run has gone on to a different show(), so Cancel does not show the old dialog again. --- crates/ui-egui/src/dialogs.rs | 6 +- crates/ui-egui/src/dialogs/script_run.rs | 19 +++--- crates/ui-egui/src/dialogs/script_window.rs | 50 +++++++++++----- crates/ui-egui/src/tests/script_window.rs | 66 ++++++++++++++++++++- docs/mcp.md | 4 +- 5 files changed, 118 insertions(+), 27 deletions(-) diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index eba418f360..cc98a59374 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -813,8 +813,10 @@ pub fn cancel(app: &mut VectorcraftApp) { if d.kind == script_run::PICKER_KIND { script_run::picker_cancel(app, &d); } - if d.kind == script_window::KIND { - script_window::cancel(app, &mut d); + if d.kind == script_window::KIND + && let Err(e) = script_window::cancel(app, &mut d) + { + app.status(e); } missing_links::cancelled(app, &d); // Cancel discards Recolor Artwork's own history of color edits with them. diff --git a/crates/ui-egui/src/dialogs/script_run.rs b/crates/ui-egui/src/dialogs/script_run.rs index 8a43c064a4..9b65d0b96a 100644 --- a/crates/ui-egui/src/dialogs/script_run.rs +++ b/crates/ui-egui/src/dialogs/script_run.rs @@ -210,25 +210,26 @@ pub(crate) fn ask_to_close(app: &mut VectorcraftApp) { /// Closes the question quitting asks once the run it asks about has ended, and forgets the /// script's dialog it was asked over once the run no longer waits at that dialog (or, for a -/// ScriptUI dialog, once its `show()` has ended), so Cancel does not show it again. +/// ScriptUI dialog, once its `show()` has ended, also when the run has gone on to another +/// `show()`), so Cancel does not show it again. pub(crate) fn settle_close(app: &mut VectorcraftApp) { if !app.ui.dialog.as_ref().is_some_and(|d| d.kind == CLOSE_KIND) { return; } if !app.session.script_running() { app.ui.dialog = None; - } else if app.session.script_dialog().is_none() - && app.session.script_picker().is_none() - && app.session.script_window().is_none() - && !over_running_show(app) - && let Some(d) = app.ui.dialog.as_mut() - { + return; + } + let over_window = app.ui.dialog.as_ref().and_then(super::parent).is_some_and(|p| p.kind == super::script_window::KIND); + let keep = if over_window { over_running_show(app) } else { app.session.script_dialog().is_some() || app.session.script_picker().is_some() }; + if !keep && let Some(d) = app.ui.dialog.as_mut() { d.fields.remove(super::PARENT); } } -/// Was the question asked over the ScriptUI dialog of the `show()` the run is still in (one of its -/// handlers runs)? Cancel then shows that dialog again, with the events it has not given yet. +/// Was the question asked over the ScriptUI dialog of the `show()` the run is still in (it waits +/// there, or one of its handlers runs)? Cancel then shows that dialog again, with the events it +/// has not given yet. fn over_running_show(app: &VectorcraftApp) -> bool { let Some(w) = app.session.script_window_showing() else { return false }; app.ui.dialog.as_ref().and_then(super::parent).is_some_and(|p| super::script_window::is_open_for(&p, &w)) diff --git a/crates/ui-egui/src/dialogs/script_window.rs b/crates/ui-egui/src/dialogs/script_window.rs index 2a1b31bf1d..7bebdbf174 100644 --- a/crates/ui-egui/src/dialogs/script_window.rs +++ b/crates/ui-egui/src/dialogs/script_window.rs @@ -108,8 +108,12 @@ fn body(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> DialogAc if events.is_empty() && waiting && !typing && crate::shortcuts::pressed_anew(ui.ctx(), egui::Key::Enter) { events.extend(w.default_element.map(UiEvent::Click)); } - if events.iter().any(|e| matches!(e, UiEvent::Click(_))) { - commit_drafts(&w, d, &mut events); + if events.iter().any(|e| matches!(e, UiEvent::Click(_))) + && let Err(e) = commit_drafts(&w, d, &mut events) + { + // A refused text stays in its field, and the click waits for the person to fix it. + events.retain(|e| !matches!(e, UiEvent::Click(_))); + app.status(e); } let mut queue: Vec = match d.fields.remove(QUEUE_KEY) { Some(Value::Array(queue)) => queue, @@ -137,23 +141,39 @@ fn body(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> DialogAc const QUEUE_KEY: &str = "queued"; /// Before a click or a close, gives the script the text typed in each edit field that has not left -/// the field yet, so the handler of a button that closes the dialog, or `onClose`, reads it. -pub(crate) fn commit_drafts(w: &ScriptUiDialog, d: &mut Dialog, events: &mut Vec) { +/// the field yet, so the handler of a button that closes the dialog, or `onClose`, reads it. A +/// disabled or hidden field keeps its text for later. Each text is checked as the script would +/// check it first; when one is refused, every field keeps its text, `events` stays as it was and +/// the error is returned. +pub(crate) fn commit_drafts(w: &ScriptUiDialog, d: &mut Dialog, events: &mut Vec) -> Result<(), String> { + let mut drafts = Vec::new(); let mut changes = Vec::new(); for (i, n) in w.nodes.iter().enumerate() { - if n.kind != UiKind::EditText || n.readonly || events.iter().any(|e| matches!(e, UiEvent::Change(c, _) if *c == i)) { + if n.kind != UiKind::EditText + || n.readonly + || !n.enabled + || !n.visible + || events.iter().any(|e| matches!(e, UiEvent::Change(c, _) if *c == i)) + { continue; } - if let Some(Value::String(text)) = d.fields.remove(&draft_key(i)) - && text != n.text - { - changes.push(UiEvent::Change(i, UiValue::Text(text))); + let key = draft_key(i); + if let Some(Value::String(text)) = d.fields.get(&key) { + if *text != n.text { + let change = UiEvent::Change(i, UiValue::Text(text.clone())); + changes.push(vectorcraft_engine::script::scriptui::check_event(w, change)?); + } + drafts.push(key); } } + for key in drafts { + d.fields.remove(&key); + } let closing = |e: &UiEvent| matches!(e, UiEvent::Click(_) | UiEvent::Close); events.sort_by_key(closing); let at = events.iter().position(closing).unwrap_or(events.len()); events.splice(at..at, changes); + Ok(()) } /// The field that holds an edit field's text while it is typed. @@ -377,7 +397,7 @@ fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result { if let Some(open) = open && matches!(events[0], UiEvent::Click(_) | UiEvent::Close) { - commit_drafts(&w, open, &mut events); + commit_drafts(&w, open, &mut events)?; } let mut events = events.into_iter(); if let Some(first) = events.next() { @@ -402,12 +422,13 @@ fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result { /// Escape or `ui.dialog.cancel`: a click on the cancel button, or the close box when the dialog /// has none or it cannot be clicked, after the text still typed in its fields. Events the script -/// has not had yet go after the ones before them. -pub(super) fn cancel(app: &mut VectorcraftApp, d: &mut Dialog) { - let Some(w) = app.session.script_window() else { return }; +/// has not had yet go after the ones before them. A typed text the script would refuse is an +/// error, and the dialog is left as it was. +pub(crate) fn cancel(app: &mut VectorcraftApp, d: &mut Dialog) -> Result<(), String> { + let Some(w) = app.session.script_window() else { return Ok(()) }; let click = w.cancel_element.map(UiEvent::Click).filter(|e| vectorcraft_engine::script::scriptui::check_event(&w, e.clone()).is_ok()); let mut events = vec![click.unwrap_or(UiEvent::Close)]; - commit_drafts(&w, d, &mut events); + commit_drafts(&w, d, &mut events)?; let mut queue: Vec = match d.fields.remove(QUEUE_KEY) { Some(Value::Array(queue)) => queue, _ => Vec::new(), @@ -422,4 +443,5 @@ pub(super) fn cancel(app: &mut VectorcraftApp, d: &mut Dialog) { if !queue.is_empty() { d.fields.insert(QUEUE_KEY.into(), Value::Array(queue)); } + Ok(()) } diff --git a/crates/ui-egui/src/tests/script_window.rs b/crates/ui-egui/src/tests/script_window.rs index 8b8b68a41b..49ef7abd18 100644 --- a/crates/ui-egui/src/tests/script_window.rs +++ b/crates/ui-egui/src/tests/script_window.rs @@ -125,7 +125,7 @@ fn text_still_in_a_field_reaches_the_script_before_a_click() { let d = app.ui.dialog.as_mut().unwrap(); d.fields.insert("text1".into(), json!("Ann")); let mut events = vec![UiEvent::Click(3)]; - script_window::commit_drafts(&w, d, &mut events); + script_window::commit_drafts(&w, d, &mut events).unwrap(); assert_eq!(events, vec![UiEvent::Change(1, UiValue::Text("Ann".into())), UiEvent::Click(3)]); assert!(!d.fields.contains_key("text1"), "the text is given once"); d.fields.insert("queued".into(), json!([{"event": "change", "control": 1, "value": "Ann"}, {"event": "click", "control": 3, "value": null}])); @@ -287,6 +287,70 @@ fn a_click_from_ui_dialog_confirm_gives_the_typed_text_first() { assert_eq!(log(&app), json!([[1, "typed"]])); } +#[test] +fn a_typed_text_the_script_refuses_keeps_every_field_and_queues_nothing() { + let mut app = app(); + start( + &mut app, + "var w = new Window('dialog'); w.add('edittext', undefined, 'a'); w.add('edittext', undefined, 'b'); \ + w.add('button', undefined, 'OK'); w.add('button', undefined, 'Cancel'); vc.log(w.show());", + ); + assert!(until_window(&mut app)); + let long = "x".repeat(16 * 1024 + 1); + let d = app.ui.dialog.as_mut().unwrap(); + d.fields.insert("text1".into(), json!(long)); + d.fields.insert("text2".into(), json!("fine")); + let kept = |app: &VectorcraftApp| { + let d = app.ui.dialog.as_ref().expect("the dialog stays open"); + assert_eq!(d.kind, KIND); + assert_eq!(d.fields.get("text1"), Some(&json!(long)), "the refused text stays in its field"); + assert_eq!(d.fields.get("text2"), Some(&json!("fine")), "and so does the other one"); + assert!(!d.fields.contains_key("queued"), "nothing is queued"); + }; + assert!(give(&mut app, json!({"event": "click", "control": "OK"})).is_err(), "OK is an error"); + kept(&app); + let mut d = app.ui.dialog.clone().unwrap(); + assert!(script_window::cancel(&mut app, &mut d).is_err(), "Cancel is an error"); + dialogs::cancel(&mut app); + kept(&app); + assert!(app.session.script_window().is_some(), "the script still waits"); + app.ui.dialog.as_mut().unwrap().fields.remove("text1"); + give(&mut app, json!({"event": "click", "control": "OK"})).unwrap(); + assert!(until_window(&mut app), "the script takes the other text and waits again"); + assert_eq!(app.session.script_window().unwrap().nodes[2].text, "fine"); + frame(&mut app, Vec::new()); + assert!(!until_window(&mut app), "the queued click on OK closes the dialog"); + assert_eq!(log(&app), json!([1])); +} + +#[test] +fn cancel_on_the_quit_question_does_not_show_a_dialog_whose_show_ended() { + let mut app = app(); + start( + &mut app, + "var a = new Window('dialog', 'A'); a.add('button', undefined, 'OK'); a.show(); \ + var b = new Window('dialog', 'B'); b.add('button', undefined, 'OK'); vc.log(b.show());", + ); + assert!(until_window(&mut app)); + let first = app.session.script_window().unwrap().shown; + dialogs::script_run::ask_to_close(&mut app); + assert_eq!(app.ui.dialog.as_ref().map(|d| d.kind.as_str()), Some(CLOSE_KIND)); + app.session.script_window_answer(UiEvent::Click(1)).unwrap(); + for _ in 0..10_000 { + if app.session.script_window().is_some_and(|w| w.shown != first) { + break; + } + script_runs::poll(&mut app); + } + assert_eq!(app.session.script_window().map(|w| w.title().to_string()), Some("B".to_string()), "the run waits at the second show()"); + dialogs::script_run::settle_close(&mut app); + let d = app.ui.dialog.as_ref().unwrap(); + assert_eq!(d.kind, CLOSE_KIND); + assert!(!d.fields.contains_key(dialogs::PARENT), "the first dialog is forgotten"); + app.session.script_request_stop(); + assert!(!until_window(&mut app)); +} + #[test] fn ui_dialog_confirm_takes_a_whole_float_as_a_list_index_and_refuses_a_fraction() { let mut app = app(); diff --git a/docs/mcp.md b/docs/mcp.md index 546f865d5f..6c14c23906 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -2948,7 +2948,9 @@ cancel button, or closes the dialog when it has none. Quitting while it waits, o asks whether to stop the script, and Cancel shows it again with the events it has not given the script yet. `ui.dialog.set` with `event` (`click`, `change` or `close`), `control` (a control's index in the dialog, its name or its text) and `value`, then `ui.dialog.confirm`, gives the script that event; a click or a close first gives the -script the text still typed in the dialog's fields. A disabled or hidden control, a value out of a list's range, a +script the text still typed in the dialog's fields. When one of those texts is longer than 16 KiB, the script gets +no event and every field keeps its text: `ui.dialog.confirm` returns an error, and Escape and `ui.dialog.cancel` show +it in the status bar. A disabled or hidden control, a value out of a list's range, a list index that is not a whole number and a click on a control that takes none are errors, and the dialog stays open.