From bc0a72165cd46d7815775461f2fc1c643c31fdfb Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 20 Nov 2026 17:46:45 -0500 Subject: [PATCH] Route scoped keys through the current modal focus chain --- crates/pages/tests/it/views.rs | 25 ++++++++++++++++++ .../ui-contract/src/runtime/commands/board.rs | 26 ++++++++++++++----- .../ui-contract/src/runtime/commands/mod.rs | 3 +++ 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/crates/pages/tests/it/views.rs b/crates/pages/tests/it/views.rs index 00340a778..5fbbf0951 100644 --- a/crates/pages/tests/it/views.rs +++ b/crates/pages/tests/it/views.rs @@ -602,6 +602,31 @@ fn the_preview_takes_page_keys_when_first_opened() { assert_eq!(*at.borrow(), 2, "Page Down steps the newly opened preview"); } +#[trial] +fn the_preview_in_a_settled_region_takes_page_keys_when_opened() { + let at = Rc::new(RefCell::new(1_usize)); + let kept = Rc::clone(&at); + let pages = DemoPages::new(5); + let mut open = false; + let mut fixture = Fixture::new(move |ui| { + craft_ui::contract::Lazy::new("region", 0.0).settles(true).show(ui, |ui| { + if craft_ui::contract::clicked(&craft_ui::buttons::Button::new("Preview").show(ui)) { + open = true; + } + if open { + PagePreview::new(&pages, &[0, 1, 2, 3, 4], &mut kept.borrow_mut(), PAPER).show(ui); + } + }); + }); + fixture.room(vec2(900.0, 600.0)).settle(); + let button = fixture.by_label("Preview").expect("preview button"); + fixture.click(&button).settle(); + // Focus entered this modal after the command board last recorded its vantage. + craft_ui::runtime::commands::clear_focus_vantage(fixture.ctx()); + fixture.key(Key::PageDown, Modifiers::NONE); + assert_eq!(*at.borrow(), 2, "Page Down steps the preview in a settled region"); +} + #[trial] fn the_preview_steps_with_the_arrow_and_page_keys_from_a_button_of_its_row() { let at = Rc::new(RefCell::new(1_usize)); diff --git a/crates/ui-contract/src/runtime/commands/board.rs b/crates/ui-contract/src/runtime/commands/board.rs index c19125859..2de670832 100644 --- a/crates/ui-contract/src/runtime/commands/board.rs +++ b/crates/ui-contract/src/runtime/commands/board.rs @@ -708,6 +708,18 @@ pub fn focus_within(ctx: &Context, id: Id) -> bool { with(ctx, |board| board.here.focused.iter().any(|(_, chain)| chain.contains(&id))) } +/// Simulates a focus change after the command board's previous begin hook. +#[cfg(feature = "testing")] +#[doc(hidden)] +pub fn clear_focus_vantage(ctx: &Context) { + with(ctx, |board| { + board.here.inside_modal.clear(); + for (_, chain) in &mut board.here.focused { + chain.clear(); + } + }); +} + /// Records that the container `id` is an instance of `scope` in the running pass of its viewport. pub(crate) fn register_instance(ctx: &Context, scope: CommandScope, id: Id) { register_in(ctx, scope, id, overlay::drawing_parent(ctx)); @@ -1188,17 +1200,19 @@ pub(crate) fn binds_scoped(ctx: &Context, chord: &Chord) -> bool { let focused = focus::record(ctx, viewport).focused; let layout = keys::layout_of(ctx); let found = with(ctx, |board| { - let chain: Vec = - board.chains.get(&viewport).filter(|(id, _)| Some(*id) == focused).map(|(_, chain)| chain.ids().collect()).unwrap_or_default(); - let keys = board.key_scopes(&chain)?; + let chain = board.chains.get(&viewport).filter(|(id, _)| Some(*id) == focused).map(|(_, chain)| chain.clone())?; + let ids: Vec = chain.ids().collect(); + let keys = board.key_scopes(&ids)?; let hit = keys.press(&layout, &chord_input(chord), false, None)?; // The lookup also ranks panels that share with the focused one and, with fallthrough, every other panel: only // a command of a scope around the focus counts. let command = std::iter::once(&hit.command).chain(&hit.cycle).find(|command| keys.scopes.contains(&board.scope_of(command)))?.clone(); - let (_, instance) = board.target_of(ctx, &command, &board.here); - Some((command, instance)) + let instance = board.innermost(&chain, board.scope_of(&command)); + Some((command, instance, chain)) }); - found.is_some_and(|(command, instance)| resolve_in(ctx, &command, instance, Reach::Focus, None).0.enabled) + // Key routing runs before the command board refreshes its focus vantage for this pass. Check the actual focused + // chain, so a focus that just entered a modal can reach its own commands without opening the modal to outsiders. + found.is_some_and(|(command, instance, chain)| resolve_in(ctx, &command, instance, Reach::Chain(&chain), None).0.enabled) } /// Where a shortcut pressed with focus in a menu or on a menu bar matches, and where a hit gives the focus back. The diff --git a/crates/ui-contract/src/runtime/commands/mod.rs b/crates/ui-contract/src/runtime/commands/mod.rs index 4bb6e5ce4..6d405b40f 100644 --- a/crates/ui-contract/src/runtime/commands/mod.rs +++ b/crates/ui-contract/src/runtime/commands/mod.rs @@ -15,6 +15,9 @@ pub use registry::CommandDefError; #[doc(hidden)] pub use registry::{SetEntry, sets}; +#[cfg(feature = "testing")] +#[doc(hidden)] +pub use board::clear_focus_vantage; #[doc(hidden)] pub use board::declare_child; #[doc(hidden)]