mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-29 01:34:29 +01:00
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.
This commit is contained in:
1 parent
7578da1824
commit
cd646a9b34
3 files changed
+47
-2
No files matched your search
@@ -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<UiEvent>) -> Option<String> {
|
||||
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
|
||||
|
||||
@@ -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();
|
||||
|
||||
+2
-1
@@ -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.
|
||||
|
||||
|
||||
Reference in new issue
Block a user