Scripts: palette events keep their checks after getters and print questions

- paletteClose with another engine's palette id no longer drops that
  palette's waiting events.
- A hidden palette is not described after a run or an add, so a
  throwing getter on it no longer fails the run; script.palettes and
  docs/mcp.md say a hidden palette lists its controls as they were.
- A check box's click rechecks the box after reading its value, so a
  getter that disables it or closes the palette stops the click.
- A list's change rechecks that the list is still enabled and visible
  after its item texts are read, and is dropped when a getter disabled
  or hid it.
- A waiting palette event does not start its run, or ask for frames,
  while the last run's print jobs wait for their answer.
- script.palette.event's help states that a change merges only after
  its palette's last waiting click or close.
This commit is contained in:
dotCooCoo committed 2026-12-02 17:05:59 -08:00
1 parent e1558e7e4a
commit 79ef692ce5
9 files changed
+115 -8

No files matched your search

+2 -2
View File
@@ -68,7 +68,7 @@ pub fn specs() -> Vec<CommandSpec> {
"List Script Palettes",
[],
None,
"{} the ScriptUI palettes that runs in named engines showed and have not closed, shown or hidden, in the order they were first shown, and the events that wait for a run → {palettes: [{id, engine, title, visible, controls: [{index, kind, name, text, enabled, visible, value (check box, radio button, slider, progress bar), items and selection (lists)}]}], waiting: events waiting, limit: 16, waitingLimit: 64}",
"{} the ScriptUI palettes that runs in named engines showed and have not closed, shown or hidden, in the order they were first shown, and the events that wait for a run; a hidden palette lists its controls as they were when it was hidden, until show() shows it again → {palettes: [{id, engine, title, visible, controls: [{index, kind, name, text, enabled, visible, value (check box, radio button, slider, progress bar), items and selection (lists)}]}], waiting: events waiting, limit: 16, waitingLimit: 64}",
always,
palettes
),
@@ -77,7 +77,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Script Palette Event",
[],
None,
"{palette: an id script.palettes lists, event: click|change|close, control?: a control (its index, name or text; click and change), value?: the new value (an edit field's text, a slider's number, or a list's item index or null; change)} give a shown palette a click, a change or a close, as the person does: it runs as a run of its own in the palette's engine, with its report like script.run's, once no run is going, oldest first; a newer click on the same control, change of it or close of the same palette replaces the most recent waiting event of that kind for that control or palette only when it has the same file access roots; otherwise the new event waits at the end of the line (merged: false); a click or close takes its place only if no change of its palette waits after it, otherwise it waits as a new event (merged: false); a list's change ends its run with an error when a script added or removed the list's items, or changed an item's text, by then, even if the list again holds the same texts; dialogs throw in the handler in the app, and with nobody to ask the run ends at one; a hidden palette, a control that is disabled, hidden or cannot take the event, and a 65th waiting event are refused, and with nobody to ask so is an event while an undo group or a drag in a document other than the active one is open → in the app {waiting: true, merged: whether it took the place of a waiting event, waitingEvents}, and the report later in ui.inspect's scriptReport; with nobody to ask, the waiting events run in order and the answer is the report of the run that handled this event (or its error): its own run, or a run of the same engine whose own code took it with update(), which gives it no report or undo step of its own; an event dropped without a run, because an earlier handler disabled, hid or removed its control or hid or closed its palette, ends with an error; an event whose run starts and finds its control, or a group above it, disabled or hidden by a getter read for the palette, or whose `enabled` or `visible` state a getter gives, runs no handler",
"{palette: an id script.palettes lists, event: click|change|close, control?: a control (its index, name or text; click and change), value?: the new value (an edit field's text, a slider's number, or a list's item index or null; change)} give a shown palette a click, a change or a close, as the person does: it runs as a run of its own in the palette's engine, with its report like script.run's, once no run is going, oldest first; a newer click on the same control, change of it or close of the same palette replaces the most recent waiting event of that kind for that control or palette only when it has the same file access roots; otherwise the new event waits at the end of the line (merged: false); a click or close takes its place only if no change of its palette waits after it, and a change only if no click or close of its palette waits after it, otherwise it waits as a new event (merged: false); a list's change ends its run with an error when a script added or removed the list's items, or changed an item's text, by then, even if the list again holds the same texts; dialogs throw in the handler in the app, and with nobody to ask the run ends at one; a hidden palette, a control that is disabled, hidden or cannot take the event, and a 65th waiting event are refused, and with nobody to ask so is an event while an undo group or a drag in a document other than the active one is open → in the app {waiting: true, merged: whether it took the place of a waiting event, waitingEvents}, and the report later in ui.inspect's scriptReport; with nobody to ask, the waiting events run in order and the answer is the report of the run that handled this event (or its error): its own run, or a run of the same engine whose own code took it with update(), which gives it no report or undo step of its own; an event dropped without a run, because an earlier handler disabled, hid or removed its control or hid or closed its palette, ends with an error; an event whose run starts and finds its control, or a group above it, disabled or hidden by a getter read for the palette, or whose `enabled` or `visible` state a getter gives, runs no handler",
always,
palette_event
),
+1 -1
View File
@@ -172,9 +172,9 @@ impl Palettes {
pub(crate) fn close(&mut self, id: u64, engine: &str) -> bool {
let before = self.list.len();
self.list.retain(|p| !(p.id == id && p.engine == engine));
self.waiting.retain(|q| q.palette != id);
let closed = self.list.len() != before;
if closed {
self.waiting.retain(|q| q.palette != id);
self.actions.retain(|(p, _)| *p != id);
}
closed
@@ -580,6 +580,19 @@ fn a_palette_click_during_another_run_runs_after_it() {
assert_eq!(s.script_palette_events_waiting(), 0);
}
#[test]
fn closing_a_palette_id_in_another_engine_keeps_that_palette_s_waiting_events() {
let mut s = one_doc();
run_in_app(&mut s, TOOLS);
run_in_app(&mut s, "#targetengine other\nvar q = new Window('palette', 'Other'); q.add('button', undefined, 'B'); q.show();");
let tools = s.script_palettes().iter().find(|p| p.engine == "tools").expect("the tools palette").id;
assert_eq!(s.script_palette_event(tools, UiEvent::Click(1)), Ok(Waiting::Added));
assert!(!s.script_palettes.close(tools, "other"), "the other engine has no palette with that id");
assert_eq!(s.script_palette_events_waiting(), 1, "the tools palette's click still waits");
assert!(s.script_palette_start().expect("the click starts its run").is_ok());
assert_eq!(pump(&mut s).unwrap().unwrap()["log"], json!(["click 1"]));
}
#[test]
fn double_clicks_while_queued_run_once() {
let mut s = one_doc();
+14 -1
View File
@@ -1406,6 +1406,10 @@ fn click_control(i: &mut Interp, c: ObjId, kind: &str) -> JsResult<bool> {
fn click_control_for(i: &mut Interp, c: ObjId, kind: &str, taker: Taker) -> JsResult<Option<bool>> {
if kind == "checkbox" {
let on = i.get_value(&Value::Object(c), &key("value"))?.truthy();
// A `value` getter may have disabled or hidden the box, or closed its palette.
if taker.is_some() && !takes(i, taker)? {
return Ok(None);
}
i.put(c, key("value"), Value::Bool(!on), true)?;
} else if kind == "radiobutton" {
i.put(c, key("value"), Value::Bool(true), true)?;
@@ -1791,9 +1795,13 @@ fn close_palette(i: &mut Interp, win: ObjId) -> JsResult<()> {
Ok(())
}
/// Gives the host the controls of the palette `win` as they are now, when it shows the palette.
/// Gives the host the controls of the palette `win` as they are now, when it shows the palette. A
/// palette that `hide()` hid is not read: `show()` gives its controls when it shows it again.
fn update_palette(i: &mut Interp, win: ObjId) -> JsResult<()> {
let Value::Number(id) = slot(i, win, PALETTE_ID)? else { return Ok(()) };
if !slot(i, win, SHOWN)?.truthy() {
return Ok(());
}
let (desc, ..) = describe(i, win)?;
let f = host_fn(i, "paletteUpdate")?;
i.rooted(std::slice::from_ref(&desc), |i| i.call(&f, &Value::Undefined, &[desc.clone(), Value::Number(id)]))?;
@@ -2318,6 +2326,11 @@ fn take_event(i: &mut Interp, win: ObjId, nodes: ObjId, ev: ObjId, event: &str,
if !holds(i, win, c)? {
return Err(error(i, "a palette event names a control the palette no longer holds"));
}
// An item's text getter may have disabled or hidden the list, or a
// group above it: the event is then dropped, as for a click.
if !live_up(i, win, c)? {
return Ok(());
}
if revised || !same || (n >= 0.0 && n as usize >= items_of(i, c)?.len()) {
return Err(error(i, "a palette's list changed before the person's choice was taken"));
}
+50
View File
@@ -1198,6 +1198,56 @@ fn a_palette_list_change_does_not_run_on_change_once_an_item_s_text_getter_close
assert_eq!(r.run("[reads, got.join(), String(l.selection)].join()").unwrap(), JsValue::String("2,,null".into()));
}
#[test]
fn a_palette_list_change_is_dropped_once_an_item_s_text_getter_disabled_or_hid_the_list() {
for disable in ["l.enabled = false;", "l.visible = false;"] {
let (mut r, _) = palette_realm(vec![seen_list_change(1.0)]);
// The second read of the first item's text, which compares it with the items the person
// saw, disables the list.
r.run(&format!(
"var p = new Window('palette'); var l = p.add('listbox', undefined, ['a', 'b']); var got = []; \
l.onChange = function () {{ got.push(String(l.selection)); }}; var reads = -1; \
Object.defineProperty(l.items[0], 'text', {{get: function () {{ if (reads >= 0 && ++reads == 2) {{ {disable} }} return 'a'; }}}}); \
p.show();"
))
.unwrap();
let e = r.run("reads = 0; try { Window.__vcPaletteEvent(); 'none'; } catch (e) { e.message; }").unwrap();
assert_eq!(e, JsValue::String("none".into()), "{disable}");
assert_eq!(r.run("[got.join(), String(l.selection)].join()").unwrap(), JsValue::String(",null".into()), "{disable}");
}
}
#[test]
fn a_palette_click_does_not_turn_over_a_check_box_its_value_getter_disabled() {
// Whichever read of `value` disables the box, the click neither sets it nor runs its handler.
for read in 1..=4 {
let (mut r, _) = palette_realm(vec![palette_event(1.0, click(1))]);
r.run(&format!(
"var p = new Window('palette'); var c = p.add('checkbox', undefined, 'On'); var clicks = 0, on = false, reads = -1; \
Object.defineProperty(c, 'value', {{get: function () {{ if (reads >= 0 && ++reads == {read}) {{ c.enabled = false; }} return on; }}, \
set: function (v) {{ on = v; }}}}); c.onClick = function () {{ clicks++; }}; p.show(); reads = 0;"
))
.unwrap();
r.run("Window.__vcPaletteEvent();").unwrap();
let got = r.run(&format!("reads >= {read} ? [clicks, on].join() : 'not read'")).unwrap();
assert!(got == JsValue::String("0,false".into()) || (read > 1 && got == JsValue::String("not read".into())), "read {read}: {got:?}");
}
}
#[test]
fn a_hidden_palette_is_not_described_after_the_run() {
let (mut r, calls) = palette_realm(Vec::new());
r.run(
"var p = new Window('palette', 'T'); p.add('button', undefined, 'Go'); p.show(); p.hide(); \
Object.defineProperty(p, 'text', {get: function () { throw new Error('read'); }});",
)
.unwrap();
let updates = |calls: &Calls| calls.borrow().iter().filter(|(n, _)| n == "paletteUpdate").count();
let before = updates(&calls);
r.refresh_script_ui().expect("a hidden palette's throwing title getter is not read");
assert_eq!(updates(&calls), before, "the host is not given a hidden palette's controls");
}
#[test]
fn a_palette_event_does_not_run_a_handler_whose_getter_closed_the_palette() {
let (mut r, calls) = palette_realm(vec![palette_event(1.0, click(1))]);
+2 -2
View File
@@ -587,7 +587,7 @@ impl VectorcraftApp {
// asks for its frames, and a run that waits for the person goes on at their next input. An
// event that waits for an open undo group asks for no frame; the pass that ends the group
// asks for the next one (`ui_frame`).
if self.session.script_palette_can_start() {
if crate::script_palettes::can_start(self) {
ctx.request_repaint();
}
if self.session.script_running()
@@ -828,7 +828,7 @@ impl VectorcraftApp {
}
menus::native::sync(self, &ctx);
// A palette event that waited for an undo group this pass ended starts its run next frame.
if self.session.script_palette_can_start() {
if crate::script_palettes::can_start(self) {
ctx.request_repaint();
}
// Only a field's commit in the pass of the press is the one it made.
+8 -1
View File
@@ -464,11 +464,18 @@ pub(crate) fn with_palette<R>(app: &VectorcraftApp, id: u64, f: impl FnOnce(&mut
with_host(app, |host, _| host.get_mut_by_id::<PaletteWindow>(palette_id(id)).map(f)).flatten()
}
/// Whether a waiting palette event's run can start now ([`vectorcraft_engine::Session::script_palette_can_start`]):
/// it also waits while the last run's print jobs wait to be approved or canceled, and asks for no
/// frame meanwhile.
pub(crate) fn can_start(app: &VectorcraftApp) -> bool {
app.session.script_palette_can_start() && !crate::script_runs::waiting_for_print(app)
}
/// Starts the run of the oldest waiting palette event when no run is going. → whether one started.
/// A run that could not begin shows its own error: the last report is an earlier run's. The run
/// lists the printers it may print to ([`crate::print::script_printers`]), as `script.run` does.
pub(crate) fn start_next(app: &mut VectorcraftApp) -> bool {
if !app.session.script_palette_can_start() {
if !can_start(app) {
return false;
}
app.session.script_printers = crate::print::script_printers(app);
+23
View File
@@ -256,6 +256,29 @@ fn the_print_question_waits_while_a_text_field_has_the_keyboard() {
assert!(sent.borrow().is_empty());
}
/// A palette's click waits while the last run's print jobs wait for their answer, and runs after it.
#[test]
fn a_palette_click_waits_for_the_answer_to_the_last_runs_print_jobs() {
let (mut app, _, _) = print_app(true);
run(
&mut app,
"#targetengine tools\nvar p = new Window('palette', 'Tools'); var go = p.add('button', undefined, 'Go'); \
go.onClick = function () { vc.log('clicked'); }; p.show();",
);
let id = app.session.script_palettes()[0].id;
run(&mut app, PRINT_TO_LAB);
assert!(app.session.script_prints_waiting() || asking(&app), "the jobs wait for their answer");
app.session.script_palette_event(id, vectorcraft_engine::script::UiEvent::Click(1)).unwrap();
for _ in 0..5 {
script_runs::poll(&mut app);
}
assert!(!app.session.script_running(), "the palette's run does not start while the jobs wait");
assert_eq!(app.session.script_palette_events_waiting(), 1);
cancel(&mut app, PRINT_KIND);
script_runs::poll(&mut app);
assert_eq!(app.session.script_palette_events_waiting(), 0, "the click's run starts once the jobs are answered");
}
/// Cancel, from anyone, drops the jobs and keeps the run's changes.
#[test]
fn cancel_drops_a_scripts_print_jobs_and_keeps_its_changes() {
+2 -1
View File
@@ -4307,7 +4307,8 @@ the same controls, handlers and limits:
`#targetengine`, or with `#targetengine main`, `new Window("palette")` throws an `Error` that says to add a
`#targetengine` line (see [Script engines](#script-engines)).
- `show()` runs `onShow` and returns `undefined` at once. `hide()` hides the palette, and `show()` shows it again.
`close()` runs `onClose` and closes the palette unless `onClose` returns `false`; the close box does the same.
While a palette is hidden, `script.palettes` lists its controls as they were when it was hidden; `show()` gives
the controls as they are then. `close()` runs `onClose` and closes the palette unless `onClose` returns `false`; the close box does the same.
A palette also closes when `script.engine.drop` drops its engine, when its engine ends with an internal error,
and when the session ends. The palette shows its controls as `show()`, `add()`, each event's handlers and each
run of its engine leave them; a run that a limit or `script.stop` ended can leave it showing them as they were.