mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 03:17:43 +00:00
Route scoped keys through the current modal focus chain
This commit is contained in:
@@ -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));
|
||||
|
||||
@@ -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<Id> =
|
||||
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<Id> = 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
|
||||
|
||||
@@ -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)]
|
||||
|
||||
Reference in New Issue
Block a user