Scripts: a ScriptUI dialog shown in place of another gives its first field the focus

A dialog a script shows right after closing the open one now counts as
newly opened: the control used in the first dialog gives up the keyboard
focus, and the first editable field of the new dialog takes it. ScriptUI
edit fields now take the first-field focus like the app's other dialogs.
This commit is contained in:
dotCooCoo
2026-11-22 11:57:39 -08:00
parent 26e0cef48d
commit 9beb3bb5f9
3 changed files with 76 additions and 6 deletions
+18 -5
View File
@@ -929,7 +929,7 @@ pub fn show(app: &mut VectorcraftApp, ctx: &egui::Context) {
revolve_gizmo::clear(ctx);
app.ui.dialog_file = None;
ctx.data_mut(|m| {
m.remove::<String>(shown_id);
m.remove::<(String, Option<u64>)>(shown_id);
m.remove::<bool>(focus_id);
});
return;
@@ -937,13 +937,26 @@ pub fn show(app: &mut VectorcraftApp, ctx: &egui::Context) {
if !revolve_gizmo::active(&d) {
revolve_gizmo::clear(ctx);
}
let focus_first = ctx.data_mut(|m| {
if m.get_temp::<String>(shown_id).as_deref() != Some(d.kind.as_str()) {
m.insert_temp(shown_id, d.kind.clone());
// A ScriptUI dialog that a script showed in place of the one open is a new dialog too.
let shown_as = (d.kind.clone(), script_window::shown_of(&d));
let (focus_first, replaced) = ctx.data_mut(|m| {
let before = m.get_temp::<(String, Option<u64>)>(shown_id);
let replaced = before.as_ref().is_some_and(|b| b.0 == shown_as.0 && *b != shown_as);
if before.as_ref() != Some(&shown_as) {
m.insert_temp(shown_id, shown_as);
m.insert_temp(focus_id, true);
}
m.get_temp::<bool>(focus_id).unwrap_or(false)
(m.get_temp::<bool>(focus_id).unwrap_or(false), replaced)
});
// It draws in the same window with the same control ids, so the control used in the dialog
// before it gives up the keyboard focus.
if replaced {
ctx.memory_mut(|m| {
if let Some(id) = m.focused() {
m.surrender_focus(id);
}
});
}
// Not in the frame of a click (the one on the menu item that opened it): egui takes the focus
// from a field the click missed, so the field takes it a frame later (#1367).
let clicked = ctx.input(|i| i.pointer.any_click());
+11 -1
View File
@@ -54,6 +54,11 @@ pub(crate) fn is_open_for(d: &Dialog, w: &ScriptUiDialog) -> bool {
d.kind == KIND && d.fields.get(SHOWN_KEY).and_then(Value::as_u64) == Some(w.shown)
}
/// Which `show()` of the run `d` is open for, when it is a ScriptUI dialog.
pub(crate) fn shown_of(d: &Dialog) -> Option<u64> {
if d.kind == KIND { d.fields.get(SHOWN_KEY).and_then(Value::as_u64) } else { None }
}
fn heading(d: &Dialog) -> String {
let title = d.str("title");
if title.is_empty() { tl!("Script Alert").into() } else { vectorcraft_engine::script::dialogs::shown_text(&title, false) }
@@ -357,8 +362,13 @@ pub(crate) fn screen_reader_name(n: &UiNode, prompt: &str) -> String {
fn edit(ui: &mut egui::Ui, d: &mut Dialog, rect: egui::Rect, (i, n, prompt): (usize, &UiNode, &str)) -> Option<UiEvent> {
let key = draft_key(i);
let mut text = d.fields.get(&key).and_then(Value::as_str).map_or_else(|| n.text.clone(), str::to_string);
let id = ui.make_persistent_id("edit");
// The first field that can be typed in takes the keyboard focus as the dialog opens.
if !n.readonly {
widgets::take_dialog_focus(ui, id, &text);
}
let field = if n.multiline { egui::TextEdit::multiline(&mut text) } else { egui::TextEdit::singleline(&mut text) };
let r = ui.put(rect, field.interactive(!n.readonly).desired_width(rect.width()));
let r = ui.put(rect, field.id(id).interactive(!n.readonly).desired_width(rect.width()));
name_for_screen_readers(ui, &r, n, prompt);
if r.changed() {
d.fields.insert(key.clone(), json!(text));
+47
View File
@@ -210,6 +210,53 @@ fn a_second_dialog_replaces_the_first() {
assert_eq!(log(&app), json!([[1, "y"]]));
}
/// Draws one frame of the dialog layer for screen readers. → The role of the control with the
/// keyboard focus.
fn focused_role(app: &mut VectorcraftApp) -> Option<egui::accesskit::Role> {
let ctx = CTX.with(Clone::clone);
ctx.enable_accesskit();
let screen_rect = Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1200.0, 800.0)));
let mut out = ctx.run_ui(egui::RawInput { screen_rect, ..Default::default() }, |ui| dialogs::show(app, ui.ctx()));
out.textures_delta.clear();
let update = out.platform_output.accesskit_update?;
update.nodes.iter().find(|(id, _)| *id == update.focus).map(|(_, n)| n.role())
}
#[test]
fn a_dialog_shown_in_place_of_another_gives_its_first_field_the_focus() {
use egui::accesskit::Role;
let mut app = app();
start(
&mut app,
"var a = new Window('dialog', 'A'); a.add('edittext', undefined, 'x'); a.add('button', undefined, 'OK'); a.show(); \
var b = new Window('dialog', 'B'); b.add('statictext', undefined, 'Name'); b.add('button', undefined, 'Back'); \
b.add('edittext', undefined, 'y'); b.add('button', undefined, 'OK'); vc.log(b.show());",
);
assert!(until_window(&mut app));
frame(&mut app, Vec::new());
frame(&mut app, Vec::new());
let role = focused_role(&mut app);
assert!(matches!(role, Some(Role::TextInput | Role::MultilineTextInput)), "the first dialog's field has the focus, not {role:?}");
let mut on_button = false;
for _ in 0..4 {
frame(&mut app, vec![key(egui::Key::Tab)]);
on_button = focused_role(&mut app) == Some(Role::Button);
if on_button {
break;
}
}
assert!(on_button, "Tab puts the focus on the first dialog's button");
give(&mut app, json!({"event": "click", "control": "OK"})).unwrap();
assert!(until_window(&mut app));
assert_eq!(app.ui.dialog.as_ref().unwrap().str("title"), "B");
frame(&mut app, Vec::new());
frame(&mut app, Vec::new());
let role = focused_role(&mut app);
assert!(matches!(role, Some(Role::TextInput | Role::MultilineTextInput)), "the second dialog's field has the focus, not {role:?}");
app.session.script_request_stop();
assert!(!until_window(&mut app));
}
#[test]
fn a_dialog_function_after_show_opens_its_own_dialog() {
let mut app = app();