From f6aef4fc8af2afa360c7a842caa856b561cf4831 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 00:18:53 -0800 Subject: [PATCH] Scripts: a queued list choice is dropped when a handler replaced the item, and the new items stay kept while counted A list counts how many times a script replaced its items, so a choice queued against an item a handler removed and added again with the same text is not replayed against the new item. The items array stays kept from collection while the count's slot reserves room. --- crates/engine/src/script/scriptui.rs | 4 ++++ crates/js/src/scriptui.rs | 20 ++++++++++++++++---- crates/js/src/scriptui_tests.rs | 13 +++++++++++++ crates/ui-egui/src/dialogs/script_window.rs | 9 +++++---- crates/ui-egui/src/tests/script_window.rs | 20 ++++++++++++++++++++ docs/target-app-parity.md | 2 +- 6 files changed, 59 insertions(+), 9 deletions(-) diff --git a/crates/engine/src/script/scriptui.rs b/crates/engine/src/script/scriptui.rs index b578bdb0b7..4226196239 100644 --- a/crates/engine/src/script/scriptui.rs +++ b/crates/engine/src/script/scriptui.rs @@ -139,6 +139,9 @@ pub struct UiNode { pub min: f64, pub max: f64, pub items: Vec, + /// How many times the script replaced a list's items (removed or added some), which tells a + /// list from one that holds the same texts again. + pub items_revision: u64, pub selection: Option, pub multiline: bool, pub readonly: bool, @@ -511,6 +514,7 @@ pub fn parse(d: &Data) -> Result { min, max, items, + items_revision: number(field(f, "itemsRevision")).map_or(0, |n| n as u64), selection, multiline: flag("multiline"), readonly: flag("readonly"), diff --git a/crates/js/src/scriptui.rs b/crates/js/src/scriptui.rs index 7ad0e41c20..d1a79939c7 100644 --- a/crates/js/src/scriptui.rs +++ b/crates/js/src/scriptui.rs @@ -101,6 +101,9 @@ const COUNT: &str = "__vcCount"; const ITEM_COUNT: &str = "__vcItemCount"; const CLOSING: &str = "__vcClosing"; const ITEMS: &str = "__vcItems"; +/// How many times a list's items have been replaced, which tells a list from one whose items were +/// removed and added again with the same texts. +const ITEMS_REV: &str = "__vcItemsRev"; const SELECTION: &str = "__vcSelection"; const LIST: &str = "__vcList"; const VALUE: &str = "__vcValue"; @@ -220,6 +223,14 @@ fn set_slot(i: &mut Interp, o: ObjId, name: &str, value: Value) -> JsResult<()> i.define(o, key(name), Prop::Data { value, flags: 0 }) } +/// Gives the list `o` the array `kept` as its items and counts the replacement ([`ITEMS_REV`]). +fn set_items(i: &mut Interp, o: ObjId, kept: ObjId) -> JsResult<()> { + let rev = number_slot(i, o, ITEMS_REV)? + 1.0; + // The array stays kept from collection while the revision's slot reserves room. + i.rooted(&[Value::Object(kept)], |i| set_slot(i, o, ITEMS_REV, Value::Number(rev)))?; + set_slot(i, o, ITEMS, Value::Object(kept)) +} + /// Sets the property `name` of `o` that a script can change. fn set_open(i: &mut Interp, o: ObjId, name: &str, value: Value) -> JsResult<()> { i.rooted(std::slice::from_ref(&value), |i| i.reserve_heap(PROP_BYTES))?; @@ -672,7 +683,7 @@ fn add(i: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult } "dropdownlist" | "listbox" => { let kept = list(i, &[], false)?; - set_slot(i, c, ITEMS, Value::Object(kept))?; + set_items(i, c, kept)?; set_slot(i, c, SELECTION, Value::Number(-1.0))?; let items = list(i, &[], true)?; set_open(i, c, "items", Value::Object(items))?; @@ -778,7 +789,7 @@ fn new_items(i: &mut Interp, o: ObjId, texts: &[Value]) -> JsResult { } let kept = list(i, &items, false)?; i.temp_roots.push(Value::Object(kept)); - set_slot(i, o, ITEMS, Value::Object(kept))?; + set_items(i, o, kept)?; let copy = list(i, &items, true)?; set_open(i, o, "items", Value::Object(copy))?; Ok(items.len()) @@ -875,7 +886,7 @@ fn remove_all(i: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult JsResult JsResult> { texts.push(Data::String(text_of(i, &t, "a list item's text")?)); } d.push(("items".into(), Data::Array(texts))); + d.push(("itemsRevision".into(), Data::Number(number_slot(i, o, ITEMS_REV)?))); // The item texts' getters ran above, so the selection is read after them. selected = number_slot(i, o, SELECTION)?; let sel = selection_index(i, o)?.filter(|n| *n < items.len()); diff --git a/crates/js/src/scriptui_tests.rs b/crates/js/src/scriptui_tests.rs index 98bab84b8b..e739efe5b0 100644 --- a/crates/js/src/scriptui_tests.rs +++ b/crates/js/src/scriptui_tests.rs @@ -891,6 +891,19 @@ fn a_close_a_getter_asks_for_while_the_dialog_is_read_again_ends_show() { assert_eq!(seen.borrow().ends, vec![(7.0, true)]); } +#[test] +fn a_list_replaced_with_the_same_texts_is_described_with_a_new_revision() { + let (mut r, seen, _) = realm(vec![click(2), close()]); + let source = "var w = new Window('dialog'); var l = w.add('listbox', undefined, ['a', 'b']); var go = w.add('button', undefined, 'Go'); \ + go.onClick = function () { l.remove(1); l.add('item', 'b'); }; w.show()"; + r.run(source).unwrap(); + let seen = seen.borrow(); + let revision = |at: usize| field(&nodes(&seen.descriptions[at])[1], "itemsRevision").clone(); + // `open` and the first `wait` see the dialog as the person does; the second `wait` sees it after the handler. + assert_eq!(field(&nodes(&seen.descriptions[2])[1], "items"), field(&nodes(&seen.descriptions[1])[1], "items"), "the same texts"); + assert_ne!(revision(1), revision(2), "a replacement item is not the one the person saw"); +} + #[test] fn a_selection_a_later_getter_changes_is_described_as_it_is() { let (mut r, seen, _) = realm(vec![click(2)]); diff --git a/crates/ui-egui/src/dialogs/script_window.rs b/crates/ui-egui/src/dialogs/script_window.rs index 6ae7961174..b4963ae08f 100644 --- a/crates/ui-egui/src/dialogs/script_window.rs +++ b/crates/ui-egui/src/dialogs/script_window.rs @@ -65,8 +65,9 @@ fn list_rows(height: f32) -> u16 { #[derive(Clone, Debug, PartialEq)] pub(crate) enum Queued { Click(u64), - /// A change, and for a list's, the list's items as the person saw them. - Change(u64, UiValue, Option>), + /// A change, and for a list's, the list's items as the person saw them and how many times the + /// script had replaced them. + Change(u64, UiValue, Option<(Vec, u64)>), Close, /// Escape or `ui.dialog.cancel` while a handler ran: the click on the cancel button of the /// dialog the run waits at next, or its close box. @@ -251,7 +252,7 @@ fn queued(w: &ScriptUiDialog, event: &UiEvent) -> Option { Some(match event { UiEvent::Click(i) => Queued::Click(id(*i)?), UiEvent::Change(i, v) => { - let items = matches!(v, UiValue::Index(_)).then(|| w.nodes.get(*i).map(|n| n.items.clone())).flatten(); + let items = matches!(v, UiValue::Index(_)).then(|| w.nodes.get(*i).map(|n| (n.items.clone(), n.items_revision))).flatten(); Queued::Change(id(*i)?, v.clone(), items) } UiEvent::Close => Queued::Close, @@ -266,7 +267,7 @@ fn resolved(w: &ScriptUiDialog, q: Queued) -> Option { Queued::Click(id) => Some(UiEvent::Click(index_of(w, id)?)), Queued::Change(id, v, items) => { let i = index_of(w, id)?; - if items.is_some_and(|items| items != w.nodes[i].items) { + if items.is_some_and(|(items, revision)| items != w.nodes[i].items || revision != w.nodes[i].items_revision) { return None; } Some(UiEvent::Change(i, v)) diff --git a/crates/ui-egui/src/tests/script_window.rs b/crates/ui-egui/src/tests/script_window.rs index 627822f738..e79e3d16eb 100644 --- a/crates/ui-egui/src/tests/script_window.rs +++ b/crates/ui-egui/src/tests/script_window.rs @@ -632,6 +632,26 @@ fn a_queued_list_change_is_dropped_after_a_handler_removes_an_item() { assert_eq!(log(&app), json!([[0, true]])); } +#[test] +fn a_queued_list_change_is_dropped_after_a_handler_replaces_the_item_with_one_of_the_same_text() { + let mut app = app(); + start( + &mut app, + "var w = new Window('dialog'); var rm = w.add('button', undefined, 'Replace'); var l = w.add('listbox', undefined, ['a', 'b', 'c']); \ + rm.onClick = function () { l.remove(2); l.add('item', 'c'); }; vc.log([w.show(), l.selection === null]);", + ); + assert!(until_window(&mut app)); + give(&mut app, json!({"event": "click", "control": "Replace"})).unwrap(); + with_window(&app, |d| d.queue(&UiEvent::Change(2, UiValue::Index(Some(2))))).unwrap(); + assert!(until_window(&mut app), "the script runs the handler and waits again"); + step(&mut app, Vec::new()); + assert!(queued(&app).is_empty(), "the change chose the item that was replaced, so it is dropped"); + assert_eq!(app.session.script_window().unwrap().nodes[2].selection, None); + give(&mut app, json!({"event": "close"})).unwrap(); + assert!(!until_window(&mut app)); + assert_eq!(log(&app), json!([[0, true]])); +} + #[test] fn cancel_on_the_quit_question_does_not_show_a_dialog_whose_show_ended() { let mut app = app(); diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index 8ad72fc70b..71de04cbae 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1083,7 +1083,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur - Custom arrowheads under `--automation-read-root`: `stroke.arrowheads.list` gives `file` as `null` for an arrowheads file outside the read roots and names it by its file name in `error`, and `stroke.arrowheads.edit` and `stroke.arrowheads.reload` refuse such a file before anything is made, opened, brought to the front or read; inside the roots `edit` opens the file under the client's roots. `crates/engine/src/tests/arrowheads.rs` (`an_agent_outside_the_arrowheads_files_folder_sees_its_file_name_alone_and_cannot_reload_it`, `an_agent_edits_the_arrowheads_file_only_inside_its_read_roots`). - Plug-in, recovery and script report replies under `--automation-read-root`: `plugin.list`, `plugin.info` and `plugin.safeMode` name the plug-in folder, failed files, plug-in sources and disabled plug-ins' files outside the read roots by file name or `null`, `file.recovery.*` leave out a document's original file and the recovery folder outside them, and `ui.inspect`'s `scriptReport` names files a run wrote outside them by file name. `crates/engine/src/tests/plugins.rs` (`plugin_list_leaves_out_paths_outside_the_read_root`), `crates/engine/src/tests/recovery.rs` (`recovery_replies_leave_out_paths_outside_the_read_root`), `crates/ui-egui/src/tests/automation.rs` (`ui_inspect_script_report_leaves_out_files_outside_the_read_root`). - Scripts: `alert`, `confirm` and `prompt` open dialogs during a run, each with a Stop button that ends the run and puts every document back. In the app the run waits at the call for the person's answer; a run over MCP or the command line takes its answers in order from `script.run`'s `answers`, and the report lists each dialog with its answer. `crates/engine/src/tests/script_dialogs.rs`, `crates/ui-egui/src/tests/script_dialogs.rs`. - - Scripts: ScriptUI's `new Window("dialog")` builds a modal dialog of groups, panels, static text, edit fields, buttons, check boxes, radio buttons, drop-down lists, list boxes, sliders and progress bars (https://extendscript.docsforadobe.dev/user-interface-tools/window-object/). `show()` waits for the person, runs the controls' `onClick`, `onChanging` and `onChange` handlers and the dialog's `onShow` and `onClose` callbacks, and returns the result of `close(result)`, or 1 for the default button and 2 for the cancel button when the button has no `onClick` or click listener. Controls also take `notify`, size objects a script can set one side of, lists' `find` and `remove`, and `addEventListener` for clicks and changes, which the dialog also takes for `show`; `Window.alert`, `Window.confirm` and `Window.prompt` open the script dialogs; a run over MCP or the command line takes the clicks and changes from a `window` answer. `new Window` and `add` also build a window or a control and the controls inside it from a resource specification (https://extendscript.docsforadobe.dev/user-interface-tools/resource-specifications/). Palettes and other control types throw that they are not available yet. `crates/js/src/scriptui_tests.rs`, `crates/engine/src/tests/script_scriptui.rs`, `crates/ui-egui/src/tests/script_window.rs`. + - Scripts: ScriptUI's `new Window("dialog")` builds a modal dialog of groups, panels, static text, edit fields, buttons, check boxes, radio buttons, drop-down lists, list boxes, sliders and progress bars (https://extendscript.docsforadobe.dev/user-interface-tools/window-object/). `show()` waits for the person and runs the controls' `onClick`, `onChanging` and `onChange` handlers. The window's own callbacks are `onShow`, which runs when `show()` starts, and `onClose`, which runs before the dialog closes and keeps it open when it returns false. `show()` returns the result of `close(result)`, or 1 for the default button and 2 for the cancel button when the button has no `onClick` or click listener and no handler closed the dialog with another result. Controls also take `notify`, size objects a script can set one side of, lists' `find` and `remove`, and `addEventListener` for clicks and changes, which the dialog also takes for `show`; `Window.alert`, `Window.confirm` and `Window.prompt` open the script dialogs; a run over MCP or the command line takes the clicks and changes from a `window` answer. `new Window` and `add` also build a window or a control and the controls inside it from a resource specification (https://extendscript.docsforadobe.dev/user-interface-tools/resource-specifications/). Palettes and other control types throw that they are not available yet. `crates/js/src/scriptui_tests.rs`, `crates/engine/src/tests/script_scriptui.rs`, `crates/ui-egui/src/tests/script_window.rs`. - Parameter keys: the engine refuses a key that a command's params description doesn't name before the command runs, in every build and for every caller, including the app's own calls, the commands a command runs and the steps of `command.batch` and `action.batch`. An action recorded by an older version still plays from the Actions panel and the Batch dialog (`Session::replay`). `crates/engine/tests/it/param_keys.rs`. - Parameter key lookups: the engine reads the params description of each built-in command, with the descriptions its `…` entries name, once per process, and answers the key check and the `ids` and `id` questions of each later command from that reading. A plug-in command's description, and a description whose `…` entries name a plug-in command, are still read each time. The accepted keys, the answers and the error messages are the same. In a release build on a one-object document, `object.move` with `ids` and `dx` takes 48 µs of thread CPU time per command instead of 61 µs, and `select.set` with `ids` 2.3 µs instead of 3.2 µs (medians of 7 alternating runs of 20,000 commands in one process, measured while other builds were running; `tests::params_cache::bench_params_keys_once`). The time is saved per command, in scripts, batches and MCP or plug-in call loops, and does not grow with the size of the document. `crates/engine/src/tests/params_cache.rs` compares every built-in command's answers with those of a fresh parse. - Effect parameter types: `effect.apply`, `appearance.addEffect` and `effect.setParams` refuse an effect parameter of another type than the effect reads, for keys only the params text names too (the earlier 3D `perspective`, `lightAzimuth` and `lightElevation`, Scribble's `variation`, Outline Stroke's `width`, Gaussian Blur's `linear`), a Crop Marks `style` other than `roman` or `japanese`, and an installed plug-in effect's parameter of another type than its manifest gives it, in a `command.batch` step too, with the document and undo history unchanged; a replayed action still plays. `effect.setParams` checks the parameters against a Warp's new style, and refuses a `visible` that is not true or false, in a replayed action too. `crates/engine/src/tests/params.rs` (`an_effect_parameter_without_a_default_is_held_to_its_type`, `effect_commands_refuse_an_undefaulted_parameter_of_the_wrong_type`), `crates/engine/src/tests/plugins.rs` (`live_effect_plugins_reshape_editable_art_and_expand`), `crates/engine/src/cmd/effectcmd/tests.rs` (`set_params_refuses_a_visible_that_is_not_true_or_false`, `set_params_checks_the_effect_it_leaves`).