From 5473a2217bb1ac0e50ad591f586eaea12fde14b4 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 25 Nov 2026 15:44:37 -0800 Subject: [PATCH] Scripts: a ScriptUI dialog's OK and Cancel found by name or text skip buttons that cannot be clicked Without defaultElement or cancelElement, the dialog's default and cancel buttons are the first enabled and visible buttons named or titled OK and Cancel, so Enter and Escape click a button the dialog takes a click on. --- crates/js/src/scriptui.rs | 6 ++++-- crates/js/src/scriptui_tests.rs | 9 +++++++++ docs/mcp.md | 3 ++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/crates/js/src/scriptui.rs b/crates/js/src/scriptui.rs index 6f45d87c7c..52440bcdce 100644 --- a/crates/js/src/scriptui.rs +++ b/crates/js/src/scriptui.rs @@ -1611,9 +1611,11 @@ fn describe_rooted(i: &mut Interp, win: ObjId) -> JsResult> { ("name".into(), Data::String(name.clone())), ("text".into(), Data::String(text.clone())), ]; - names.push((kind.clone(), name, text)); let enabled = get(i, o, "enabled")?.truthy(); let visible = get(i, o, "visible")?.truthy(); + // Enter and Escape click a button found by its name or text only when it can be clicked. + let clickable = kind == "button" && enabled && visible; + names.push((clickable, name, text)); d.push(("enabled".into(), Data::Bool(enabled))); d.push(("visible".into(), Data::Bool(visible))); // A value a getter gives is kept from collection while its own properties are read. @@ -1694,7 +1696,7 @@ fn describe_rooted(i: &mut Interp, win: ObjId) -> JsResult> { if !matches!(given, Value::Undefined | Value::Null) { return Ok(-1.0); } - let button = |by_name: bool| names.iter().position(|(k, n, t)| k == "button" && (if by_name { n } else { t }).to_lowercase() == word); + let button = |by_name: bool| names.iter().position(|(c, n, t)| *c && (if by_name { n } else { t }).to_lowercase() == word); Ok(button(true).or_else(|| button(false)).map_or(-1.0, |n| n as f64)) }; let def = target(i, "defaultElement", "ok")?; diff --git a/crates/js/src/scriptui_tests.rs b/crates/js/src/scriptui_tests.rs index eb80e515ab..78b491b021 100644 --- a/crates/js/src/scriptui_tests.rs +++ b/crates/js/src/scriptui_tests.rs @@ -403,6 +403,15 @@ fn a_close_that_on_close_asks_for_is_part_of_the_close_it_refuses() { assert_eq!(r.run(source).unwrap(), JsValue::String("1,2".into()), "the refused close waits for the click on OK"); } +#[test] +fn a_hidden_or_disabled_ok_button_is_not_the_default_found_by_its_text() { + let (mut r, seen, _) = realm(vec![close()]); + let source = "var w = new Window('dialog'); w.add('button', undefined, 'OK').enabled = false; w.add('button', undefined, 'OK').visible = false; \ + w.add('button', undefined, 'OK'); w.show()"; + assert_eq!(r.run(source).unwrap(), JsValue::Number(0.0)); + assert_eq!(field(&seen.borrow().descriptions[0], "defaultElement"), &Data::Number(3.0), "the OK button that can be clicked is the default"); +} + #[test] fn an_edit_field_that_hides_its_text_is_refused_when_shown() { let (mut r, seen, _) = realm(Vec::new()); diff --git a/docs/mcp.md b/docs/mcp.md index 33049dfe14..10be342154 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -3052,7 +3052,8 @@ close box. The default or cancel button closes the dialog only when it has no `o its own and no `capture` handler on a container stopped the click; `click` handlers on the containers that do not capture do not keep it from closing (VectorCraft's own rule). `onClose` runs before the dialog closes, and returning `false` keeps it open. The default button is `defaultElement`, else a button named or titled OK, and the -cancel button is `cancelElement`, else a button named or titled Cancel, in any case. A `show()` inside a handler, +cancel button is `cancelElement`, else a button named or titled Cancel, in any case; a button found by its name or +title counts only while it is enabled and visible. A `show()` inside a handler, or of a dialog that is showing, throws an `Error`, and so does one inside a callback of a built-in function. In the app, `alert`, `confirm`, `prompt` and the file dialogs called from a handler also throw an `Error`; in a run with nobody to ask, `alert`, `confirm` and `prompt` in a handler take their answers from `answers`. A shown dialog can be shown again after it closes.