diff --git a/crates/ui-egui/src/script_palettes.rs b/crates/ui-egui/src/script_palettes.rs index e35d277600..b07deea333 100644 --- a/crates/ui-egui/src/script_palettes.rs +++ b/crates/ui-egui/src/script_palettes.rs @@ -34,6 +34,10 @@ pub(crate) fn palette_id(id: u64) -> DialogId { dialog_id((KIND, id)) } +/// One batch of given events: its pass's scope, its own scope, the events, and the typed texts as +/// they were in its pass, each with its typing scope. +type Given = (ScopeHandle, ScopeHandle, Vec, BTreeMap); + /// A shown palette on the window's dialog host. pub(crate) struct PaletteWindow { /// The palette as the session has it, from the last frame. @@ -44,8 +48,9 @@ pub(crate) struct PaletteWindow { draft_scopes: BTreeMap, /// The events the person gave since the app last took them, each with its control by its id, /// and a list's change with the items the person saw, so a run that changes the palette before - /// the app takes them leaves each with its own control. - given: Vec<(ScopeHandle, ScopeHandle, Vec)>, + /// the app takes them leaves each with its own control. Each batch also keeps the typed texts as + /// they were in its pass, so a click or close gives only the texts typed before it. + given: Vec, /// The text each edit field gave the script, by the field's id, while its change waits: the /// field shows it until no change of that field waits, even while the palette's field holds the /// same text, since a run before the change may set the field to another one. @@ -187,7 +192,9 @@ impl PaletteWindow { _ => batches.push((scope, vec![q])), } } - self.given.extend(batches.into_iter().map(|(scope, events)| (here.clone(), scope, events))); + let seen: BTreeMap = + self.drafts.iter().filter_map(|(id, text)| self.draft_scopes.get(id).map(|s| (*id, (text.clone(), s.clone())))).collect(); + self.given.extend(batches.into_iter().map(|(scope, events)| (here.clone(), scope, events, seen.clone()))); } } @@ -292,9 +299,39 @@ pub fn show(app: &mut VectorcraftApp, ctx: &egui::Context) { // Texts whose change went in line, typed ones a click or close gave included: the field // shows each until its change no longer waits. let mut accepted = BTreeMap::new(); - for (pass_scope, scope, given) in given { + for (pass_scope, scope, given, seen) in given { let mut events: Vec = given.into_iter().filter_map(|q| script_window::resolved(&p.dialog, q)).collect(); - for (event_scope, event) in drafted(app, &p.dialog, &mut drafts, &mut events, &scope, &pass_scope, &draft_scopes) { + // Only the texts typed by this batch's pass are given with its click or close; a text + // typed in a later pass stays in its field. A text this frame already gave is not given + // again. + let seen: BTreeMap = + seen.into_iter().filter(|(field, (text, _))| accepted.get(field) != Some(text)).collect(); + let mut at_pass: BTreeMap = seen.iter().map(|(field, (text, _))| (*field, text.clone())).collect(); + // Each text keeps the scope it had when it was typed, even when a later pass gave it. + let at_scopes: BTreeMap = seen.into_iter().map(|(field, (_, s))| (field, s)).collect(); + script_window::keep_fields(&p.dialog, &mut at_pass); + // A field's change carrying a text this frame already gave is not given a second time. + events.retain(|e| match e { + UiEvent::Change(i, UiValue::Text(text)) => p.dialog.nodes.get(*i).is_none_or(|n| accepted.get(&n.id) != Some(text)), + _ => true, + }); + let before = at_pass.clone(); + let (batch, refused) = drafted(app, &p.dialog, &mut at_pass, &mut events, &scope, &pass_scope, &at_scopes); + // A refused text keeps every later click and close out of line too. + change_refused |= refused; + // A text the click or close took leaves its field unless a later one was typed there. + for (field, text) in &before { + if !at_pass.contains_key(field) && drafts.get(field) == Some(text) { + drafts.remove(field); + } + } + // A text refused in this batch goes back in its field unless a later one was typed there. + for (field, text) in at_pass { + if before.get(&field) != Some(&text) { + drafts.entry(field).or_insert(text); + } + } + for (event_scope, event) in batch { event_scope.run(|| give(app, ctx, p, &mut drafts, vec![event], &mut change_refused, &mut accepted)); } // A text that went back in its field without a scope of its own takes this one. @@ -317,7 +354,8 @@ pub fn show(app: &mut VectorcraftApp, ctx: &egui::Context) { } /// Commits drafts in dialog order, each under its typing and committing scopes. Every draft is -/// validated before any is removed. A refused draft keeps the click or close out of line. +/// validated before any is removed. A refused draft keeps the click or close out of line. → the +/// events with their scopes, and whether a text was refused. fn drafted( app: &mut VectorcraftApp, w: &ScriptUiDialog, @@ -326,9 +364,11 @@ fn drafted( scope: &ScopeHandle, pass_scope: &ScopeHandle, draft_scopes: &BTreeMap, -) -> Vec<(ScopeHandle, UiEvent)> { +) -> (Vec<(ScopeHandle, UiEvent)>, bool) { + let mut refused = false; if let Some(e) = script_window::keep_refused(w, drafts, events) { app.status(e); + refused = true; } let closing = |e: &UiEvent| matches!(e, UiEvent::Click(_) | UiEvent::Close); let mut changes = Vec::new(); @@ -343,6 +383,7 @@ fn drafted( changes.clear(); events.retain(|e| !closing(e)); app.status(e); + refused = true; } } } @@ -364,7 +405,7 @@ fn drafted( let mut given: Vec<_> = events.drain(..at).map(|e| (scope.clone(), e)).collect(); given.extend(changes); given.extend(events.drain(..).map(|e| (closing_scope.clone(), e))); - given + (given, refused) } /// Puts the events the person gave the palette `p` in line, or shows why one was refused. A diff --git a/crates/ui-egui/src/tests/script_palettes.rs b/crates/ui-egui/src/tests/script_palettes.rs index 4b72675b2e..ef816ce39e 100644 --- a/crates/ui-egui/src/tests/script_palettes.rs +++ b/crates/ui-egui/src/tests/script_palettes.rs @@ -455,6 +455,36 @@ fn buffered_passes_keep_a_click_or_close_before_a_later_text_commit() { } } +#[test] +fn a_buffered_click_or_close_does_not_give_a_text_typed_after_it() { + for closing in [UiEvent::Click(2), UiEvent::Close] { + let mut app = app(); + let id = shown( + &mut app, + "#targetengine later\nvar p = new Window('palette'); var t = p.add('edittext', undefined, 'a'); \ + var go = p.add('button', undefined, 'Go'); t.onChange = function () { vc.log('changed ' + t.text); }; \ + go.onClick = function () { vc.log('clicked ' + t.text); }; \ + p.onClose = function () { vc.log('closed ' + t.text); return false; }; p.show();", + ); + let first = if matches!(closing, UiEvent::Close) { "closed" } else { "clicked" }; + with_palette(&app, id, |w| { + w.type_text(1, "early"); + w.pass(&[], vec![closing]); + w.type_text(1, "later"); + }) + .unwrap(); + follow(&mut app); + assert_eq!(app.session.script_palette_events_waiting(), 2); + for expected in ["changed early".to_string(), format!("{first} early")] { + script_runs::poll(&mut app); + run_to_end(&mut app); + assert_eq!(app.session.script_report().unwrap()["log"], json!([expected])); + } + assert_eq!(app.session.script_palette_events_waiting(), 0); + assert_eq!(with_palette(&app, id, |w| w.typed(1).map(str::to_string)).unwrap().as_deref(), Some("later")); + } +} + #[test] fn a_click_or_close_commits_drafts_in_dialog_order_across_scopes() { use vectorcraft_engine::file_access::{AutomationRoots, confine, unconfined};