Scripts: a palette event is dropped once a getter or a handler closed the palette

A palette event checks again, after the palette's description is read and
after each handler is read or run, that the host still shows the palette
under the id the event names. A getter or an earlier handler that closed
the palette now stops the event, so no click or change reaches a closed
palette, and a list change whose palette closed is dropped rather than
refused.
This commit is contained in:
dotCooCoo committed 2026-11-24 09:17:14 -08:00
1 parent f05ec9a090
commit 8cc34c71a0
3 files changed
+133 -6

No files matched your search

+51 -5
View File
@@ -794,6 +794,10 @@ fn palette_event(i: &mut Interp, _: Value, _: &[Value], _: bool) -> JsResult<Val
close_palette(i, win)?;
} else {
let (_, nodes, ..) = describe(i, win)?;
// A getter read in the description may have closed the palette.
if !still_takes(i, win, ev, None)? {
return Ok(Value::Undefined);
}
i.temp_roots.push(Value::Object(nodes));
let held = list_values(i, nodes)?;
// The host gives the control's place, which is found again in the palette as it is now:
@@ -820,6 +824,34 @@ fn palette_event(i: &mut Interp, _: Value, _: &[Value], _: bool) -> JsResult<Val
r
}
/// Whether the window `win` still takes the event `ev`: a dialog (`keep` is `Some`) always does,
/// and a palette only while the host shows it under the id the event names. A getter or a handler
/// run for the event may have closed the palette.
fn still_takes(i: &mut Interp, win: ObjId, ev: ObjId, keep: Option<&[Value]>) -> JsResult<bool> {
if keep.is_some() {
return Ok(true);
}
let id = slot(i, ev, "palette")?;
Ok(matches!((slot(i, win, PALETTE_ID)?, id), (Value::Number(a), Value::Number(b)) if a == b))
}
/// Calls control `c`'s handler `name` for the event `ev` when it has one and `win` still takes the
/// event once the handler has been read: a getter for `name` may have closed the palette.
/// → `None` when `win` no longer takes the event, otherwise `handler`'s answer.
fn event_handler(i: &mut Interp, win: ObjId, c: ObjId, ev: ObjId, keep: Option<&[Value]>, name: &str) -> JsResult<Option<Option<Value>>> {
if !still_takes(i, win, ev, keep)? {
return Ok(None);
}
let f = i.get_value(&Value::Object(c), &key(name))?;
if !still_takes(i, win, ev, keep)? {
return Ok(None);
}
if !i.is_callable(&f) {
return Ok(Some(None));
}
i.call(&f, &Value::Object(c), &[]).map(|v| Some(Some(v)))
}
/// The control at `place` in `win` as it is now: the index of each control among its container's
/// controls, from the window down, as the host counts places. `None` when the palette has no
/// control there.
@@ -975,7 +1007,8 @@ fn answered(i: &mut Interp, keep: &[Value], answer: Value) -> JsResult<Value> {
/// A click or a change the host gave in `ev` for control `id` of `nodes` (a description's list of
/// elements): the control takes it and its handler runs. A dialog's button without `onClick` asks
/// to close it with 1 when it is the default element and 2 when it is the cancel element, which
/// `keep` (`show()`'s kept values) names; a palette has no `keep`.
/// `keep` (`show()`'s kept values) names; a palette has no `keep`. No handler of a palette runs
/// once a getter, a setter or an earlier handler of the event closed the palette.
fn take_event(i: &mut Interp, win: ObjId, nodes: ObjId, ev: ObjId, event: &str, keep: Option<&[Value]>) -> JsResult<()> {
let id = match slot(i, ev, "id")? {
Value::Number(n) if n >= 0.0 && n.fract() == 0.0 => n as usize,
@@ -1003,7 +1036,10 @@ fn take_event(i: &mut Interp, win: ObjId, nodes: ObjId, ev: ObjId, event: &str,
}
i.put(c, key("value"), Value::Bool(true), true)?;
}
if handler(i, c, "onClick")?.is_none()
let Some(ran) = event_handler(i, win, c, ev, keep, "onClick")? else {
return Ok(());
};
if ran.is_none()
&& kind == "button"
&& let Some(keep) = keep
{
@@ -1019,11 +1055,15 @@ fn take_event(i: &mut Interp, win: ObjId, nodes: ObjId, ev: ObjId, event: &str,
match kind.as_str() {
"edittext" => {
i.put(c, key("text"), value, true)?;
handler(i, c, "onChanging")?;
if event_handler(i, win, c, ev, keep, "onChanging")?.is_none() {
return Ok(());
}
}
"slider" => {
i.put(c, key("value"), value, true)?;
handler(i, c, "onChanging")?;
if event_handler(i, win, c, ev, keep, "onChanging")?.is_none() {
return Ok(());
}
}
_ => {
let n = match value {
@@ -1050,6 +1090,11 @@ fn take_event(i: &mut Interp, win: ObjId, nodes: ObjId, ev: ObjId, event: &str,
Value::Number(r) => r != number_slot(i, c, REVISION)?,
_ => false,
};
// An item's text getter may have closed the palette, and its `onClose`
// may have changed the list: the event is then dropped, not refused.
if !still_takes(i, win, ev, keep)? {
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"));
}
@@ -1058,7 +1103,8 @@ fn take_event(i: &mut Interp, win: ObjId, nodes: ObjId, ev: ObjId, event: &str,
set_slot(i, c, SELECTION, Value::Number(n))?;
}
}
handler(i, c, "onChange")?;
// `onChanging` may have closed the palette.
event_handler(i, win, c, ev, keep, "onChange")?;
}
_ => return Err(Abrupt::Internal(format!("the host gave show the event {event}"))),
}
+62
View File
@@ -549,6 +549,68 @@ fn a_palette_event_finds_its_control_by_its_place() {
assert_eq!(r.run("clicks").unwrap(), JsValue::Number(1.0));
}
#[test]
fn a_palette_event_does_not_run_a_handler_once_a_getter_closed_the_palette() {
let (mut r, calls) = palette_realm(vec![palette_event(1.0, click(2))]);
r.run(
"var p = new Window('palette'); var label = p.add('statictext', undefined, 'idle'); var b = p.add('button', undefined, 'Go'); \
var clicks = 0; b.onClick = function () { clicks++; }; var armed = false; \
Object.defineProperty(label, 'text', {get: function () { if (armed) { armed = false; p.close(); } return 'idle'; }}); p.show();",
)
.unwrap();
// The event reads the label's text for the palette's description, which closes the palette.
r.run("armed = true; Window.__vcPaletteEvent();").unwrap();
assert_eq!(r.run("clicks").unwrap(), JsValue::Number(0.0), "the click is not given to a closed palette");
assert!(calls.borrow().contains(&("paletteClose".into(), 1.0)));
}
#[test]
fn a_palette_list_change_does_not_run_on_change_once_an_item_s_text_getter_closed_the_palette() {
let (mut r, _) = palette_realm(vec![seen_list_change(1.0)]);
// The event reads the first item's text twice: for the palette's description, then to compare
// it with the items the person saw. The second read closes the palette.
r.run(
"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) { p.close(); } 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()));
assert_eq!(r.run("[reads, got.join(), String(l.selection)].join()").unwrap(), JsValue::String("2,,null".into()));
}
#[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))]);
r.run(
"var p = new Window('palette'); var b = p.add('button', undefined, 'Go'); var clicks = 0; var reads = 0; \
Object.defineProperty(b, 'onClick', {get: function () { reads++; p.close(); return function () { clicks++; }; }}); p.show();",
)
.unwrap();
r.run("Window.__vcPaletteEvent();").unwrap();
assert_eq!(r.run("[reads, clicks].join()").unwrap(), JsValue::String("1,0".into()), "the handler read from a closed palette does not run");
assert!(calls.borrow().contains(&("paletteClose".into(), 1.0)));
}
#[test]
fn a_palette_list_change_is_dropped_once_an_item_s_text_getter_closed_the_palette_and_on_close_changed_the_list() {
let (mut r, _) = palette_realm(vec![seen_list_change(1.0)]);
// The second read of the first item's text closes the palette, and its `onClose` adds an item.
r.run(
"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; \
p.onClose = function () { l.add('item', 'c'); }; \
Object.defineProperty(l.items[0], 'text', {get: function () { if (reads >= 0 && ++reads == 2) { p.close(); } 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()), "the event for a closed palette is dropped without an error");
assert_eq!(r.run("[reads >= 2, l.items.length, got.join(), String(l.selection)].join()").unwrap(), JsValue::String("true,3,,null".into()));
}
#[test]
fn a_palette_s_visible_follows_hide_and_show() {
let (mut r, _) = palette_realm(Vec::new());
+20 -1
View File
@@ -551,7 +551,7 @@ fn a_queued_list_choice_is_not_given_after_a_handler_changed_the_list() {
// A click on Swap, then a choice in the list as it was before Swap's handler ran, both given
// in one frame.
let swap = json!({"event": "click", "control": 2, "value": null});
let choice = json!({"event": "change", "control": 1, "value": 1, "list": {"items": ["a", "b"], "revision": 2.0}});
let choice = json!({"event": "change", "control": 1, "value": 1, "place": [0], "list": {"items": ["a", "b"], "revision": 2.0}});
app.ui.dialog.as_mut().unwrap().fields.insert("queued".into(), json!([swap, choice]));
frame(&mut app, Vec::new());
assert!(until_window(&mut app), "the script takes the click and waits again");
@@ -564,6 +564,25 @@ fn a_queued_list_choice_is_not_given_after_a_handler_changed_the_list() {
assert_eq!(log(&app), json!([[1, "null"]]), "the choice for the old items was not given");
}
#[test]
fn a_queued_list_choice_is_given_while_its_list_is_unchanged() {
let mut app = app();
start(
&mut app,
"var w = new Window('dialog'); var l = w.add('listbox', undefined, ['a', 'b']); w.add('button', undefined, 'OK'); \
vc.log([w.show(), String(l.selection)]);",
);
assert!(until_window(&mut app));
let choice = json!({"event": "change", "control": 1, "value": 1, "place": [0], "list": {"items": ["a", "b"], "revision": 2.0}});
app.ui.dialog.as_mut().unwrap().fields.insert("queued".into(), json!([choice]));
frame(&mut app, Vec::new());
assert!(!app.ui.dialog.as_ref().unwrap().fields.contains_key("queued"), "the choice left the line");
assert!(until_window(&mut app), "the script takes the choice and waits again");
give(&mut app, json!({"event": "click", "control": "OK"})).unwrap();
assert!(!until_window(&mut app));
assert_eq!(log(&app), json!([[1, "b"]]), "the choice for the same items was given");
}
#[test]
fn continue_on_the_growth_question_shows_the_dialog_again_with_its_queued_click_and_typed_text() {
let mut app = app();