From cd646a9b344b2d2029ffc0293d2c720cd26b2d6c Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 22 Nov 2026 06:41:16 -0800 Subject: [PATCH] Scripts: a ScriptUI edit field keeps a refused text after it loses the focus A text longer than 16 KiB that an edit field gives as it loses the focus now goes back in the field, with the error in the status bar, instead of being dropped when the next frame draws the script's old text. The field keeps it until the person changes it. --- crates/ui-egui/src/dialogs/script_window.rs | 24 ++++++++++++++++++++- crates/ui-egui/src/tests/script_window.rs | 22 +++++++++++++++++++ docs/mcp.md | 3 ++- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/dialogs/script_window.rs b/crates/ui-egui/src/dialogs/script_window.rs index 93a81f417f..158a86ea5b 100644 --- a/crates/ui-egui/src/dialogs/script_window.rs +++ b/crates/ui-egui/src/dialogs/script_window.rs @@ -108,6 +108,9 @@ 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 let Some(e) = keep_refused_texts(&w, d, &mut events) { + app.status(e); + } if events.iter().any(|e| matches!(e, UiEvent::Click(_))) && let Err(e) = commit_drafts(&w, d, &mut events) { @@ -176,6 +179,24 @@ pub(crate) fn commit_drafts(w: &ScriptUiDialog, d: &mut Dialog, events: &mut Vec Ok(()) } +/// Takes out of `events` each text an edit field gave as it lost the focus that the script would +/// refuse, and puts it back in its field, so the person can fix it. → the error of the last one. +pub(crate) fn keep_refused_texts(w: &ScriptUiDialog, d: &mut Dialog, events: &mut Vec) -> Option { + let mut refused = None; + events.retain(|e| { + let UiEvent::Change(i, UiValue::Text(text)) = e else { return true }; + match vectorcraft_engine::script::scriptui::check_event(w, e.clone()) { + Ok(_) => true, + Err(error) => { + d.fields.insert(draft_key(*i), json!(text)); + refused = Some(error); + false + } + } + }); + refused +} + /// The field that holds an edit field's text while it is typed. fn draft_key(i: usize) -> String { format!("text{i}") @@ -341,7 +362,8 @@ fn edit(ui: &mut egui::Ui, d: &mut Dialog, rect: egui::Rect, (i, n, prompt): (us d.fields.remove(&key); return Some(UiEvent::Change(i, UiValue::Text(text))); } - if !r.has_focus() && !r.changed() { + // A text the script refused stays in the field until the person changes it. + if !r.has_focus() && !r.changed() && (text == n.text || n.readonly) { d.fields.remove(&key); } None diff --git a/crates/ui-egui/src/tests/script_window.rs b/crates/ui-egui/src/tests/script_window.rs index 2d68a9e6a6..2e2cd186ca 100644 --- a/crates/ui-egui/src/tests/script_window.rs +++ b/crates/ui-egui/src/tests/script_window.rs @@ -330,6 +330,28 @@ fn a_typed_text_the_script_refuses_keeps_every_field_and_queues_nothing() { assert_eq!(log(&app), json!([1])); } +#[test] +fn a_typed_text_the_script_refuses_stays_in_its_field_after_it_loses_the_focus() { + let mut app = app(); + start(&mut app, "var w = new Window('dialog'); w.add('edittext', undefined, 'a'); w.add('button', undefined, 'OK'); vc.log(w.show());"); + assert!(until_window(&mut app)); + let w = app.session.script_window().unwrap(); + let long = "x".repeat(16 * 1024 + 1); + let d = app.ui.dialog.as_mut().unwrap(); + let mut events = vec![UiEvent::Change(1, UiValue::Text(long.clone())), UiEvent::Change(1, UiValue::Text("fine".into()))]; + assert!(script_window::keep_refused_texts(&w, d, &mut events).is_some(), "the long text is an error"); + assert_eq!(events, vec![UiEvent::Change(1, UiValue::Text("fine".into()))], "a text the script takes is still given"); + assert!(d.fields.get("text1") == Some(&json!(long)), "the refused text goes back in its field"); + frame(&mut app, Vec::new()); + frame(&mut app, Vec::new()); + let d = app.ui.dialog.as_ref().expect("the dialog stays open"); + assert!(d.fields.get("text1") == Some(&json!(long)), "and stays there while the field has no focus"); + assert!(app.session.script_window().is_some(), "the script still waits"); + assert!(give(&mut app, json!({"event": "click", "control": "OK"})).is_err(), "OK is an error until it is fixed"); + app.session.script_request_stop(); + assert!(!until_window(&mut app)); +} + #[test] fn cancel_on_the_quit_question_does_not_show_a_dialog_whose_show_ended() { let mut app = app(); diff --git a/docs/mcp.md b/docs/mcp.md index 11fded84c1..951edebb05 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -2950,7 +2950,8 @@ asks whether to stop the script, and Cancel shows it again with the events it ha 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. When one of those texts is longer than 16 KiB, the script gets no event and every field keeps its text: `ui.dialog.confirm`, `ui.dialog.cancel` and `ui.dialog.set` with `action` -return an error, and Escape shows it in the status bar. A disabled or hidden control, a value out of a list's range, a +return an error, and Escape shows it in the status bar. Such a text stays in its field when the field loses the +focus too, with the error 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.