From dd5c833799273ae05c01c70efea4dd67acdf7451 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 28 Nov 2026 00:39:17 +0000 Subject: [PATCH] Command board: declared edits reach the instance around the focus; a window layer can take unfocused keys Outside the modal layers, a ModalRule::Declared app command now goes to the innermost instance around the focus that published its state, as it already did inside a modal layer: a list pane that declares Select All takes it while it holds the focus. Trial: commands::board::a_declared_edit_reaches_the_instance_around_the_focus. ViewportWindow::takes_unfocused_keys (opt-in, off by default) records the window's layer with the board; while that layer is open, a key pressed in its viewport with nothing focused goes to the instances in the window's body instead of the shell and page roots, as a modal layer's root already gets them. Co-Authored-By: Claude Opus 5.5 --- .../ui-contract/src/runtime/commands/board.rs | 68 ++++++++++++++++--- .../ui-contract/src/runtime/commands/mod.rs | 2 + crates/ui-shell/src/shell/viewport_window.rs | 17 ++++- crates/ui-shell/tests/it/commands/board.rs | 32 +++++++++ 4 files changed, 107 insertions(+), 12 deletions(-) diff --git a/crates/ui-contract/src/runtime/commands/board.rs b/crates/ui-contract/src/runtime/commands/board.rs index 8d3474f5cd..c6da3e6005 100644 --- a/crates/ui-contract/src/runtime/commands/board.rs +++ b/crates/ui-contract/src/runtime/commands/board.rs @@ -198,6 +198,9 @@ pub(crate) struct Board { modal_roots: HashMap, /// The root of the page region the window's shell shows, per viewport: where keys with nothing focused also go. page_roots: HashMap, + /// The window layer of each viewport whose body takes the keys pressed there with nothing focused + /// (`note_unfocused_keys_layer`). + key_layers: HashMap, /// The states published this pass, and the pass before, by an instance inside the top modal layer. modal_published: HashSet, modal_published_last: HashSet, @@ -738,7 +741,8 @@ impl Board { /// Who answers for `command` ahead of the app: an instance inside the top modal layer that published its state, /// else the focused component when it takes the command as an edit (inside a modal layer, only when the focus - /// is in it). + /// is in it), else, outside the modal layers, the innermost instance around the focus that published its state + /// (a slide list's Select All with the focus in the list). fn declaring(&self, ctx: &Context, command: &CommandRef, from: &Vantage, reads: &mut Reads) -> Option { reads.focus = true; let dialog_open = from.modal_open || from.dialog_keys; @@ -747,25 +751,32 @@ impl Board { if let Some(id) = published.and_then(|(_, id)| *id).filter(|_| dialog_open) { return Some(Declaration::Instance(id)); } - let edit = self.registry.edit_of(&command.id)?; + let viewport = pass::current_viewport(ctx); + let around = || { + let (_, chain) = from.focused.iter().find(|(at, _)| *at == viewport).filter(|_| !dialog_open)?; + let published = |id: &&Id| [&self.this, &self.last].iter().any(|states| states.contains_key(&(number, Some(**id)))); + chain.iter().rev().find(published).map(|id| Declaration::Instance(*id)) + }; + let Some(edit) = self.registry.edit_of(&command.id) else { return around() }; let keys = match from.edits { Some(edits) => Some((edits, from.inactive.unwrap_or(EditSet::NONE))), None => { reads.edits = true; - focused_edits(ctx, pass::current_viewport(ctx)) + focused_edits(ctx, viewport) } }; let in_reach = !dialog_open || !from.inside_modal.is_empty(); - let (edits, inactive) = keys.filter(|_| in_reach)?; + let Some((edits, inactive)) = keys.filter(|_| in_reach) else { return around() }; let inactive = inactive.contains(edit); // An Undo or Redo the focused part has no step for goes where `fall_through` sends it (the declaring instance // or the app command), so that target answers for it and its state enables the command: a toolbar Undo stays // enabled while the app can undo though the field cannot. Cut, Copy and Delete with nothing selected stay the // part's and disabled. - if inactive && matches!(edit, EditCommand::Undo | EditCommand::Redo) { - return None; + let falls_through = inactive && matches!(edit, EditCommand::Undo | EditCommand::Redo); + if !falls_through && edits.contains(edit) { + return Some(Declaration::Focused { inactive }); } - edits.contains(edit).then_some(Declaration::Focused { inactive }) + around() } /// `command` as seen by `instance`, or by the app for an app-scope command, from `from`, before the gate, and the @@ -1411,6 +1422,25 @@ pub fn note_page_root(ctx: &Context, viewport: ViewportId, root: Id) { }); } +/// Records the layer whose body is running as the window layer of the running viewport that takes the keys pressed +/// there with nothing focused (`ViewportWindow::takes_unfocused_keys`): the instances in its body take them, ahead of +/// the window's shell and page, as long as the layer stays open. +#[doc(hidden)] +pub fn note_unfocused_keys_layer(ctx: &Context) { + if let Some(layer) = overlay::drawing_parent(ctx) { + note_key_layer(ctx, layer); + } +} + +/// `note_unfocused_keys_layer` for the layer `layer`, which a replay of the note keeps. +fn note_key_layer(ctx: &Context, layer: Id) { + crate::runtime::replay::journal(ctx, || layer, |ctx, &layer| note_key_layer(ctx, layer)); + let viewport = pass::current_viewport(ctx); + with(ctx, |board| { + board.key_layers.insert(viewport, layer); + }); +} + /// The root of the top modal layer of `viewport`, if one is open. pub(crate) fn modal_root(ctx: &Context, viewport: ViewportId) -> Option { with(ctx, |board| board.modal_roots.get(&viewport).copied()) @@ -2075,6 +2105,15 @@ pub(crate) fn begin(ctx: &Context) { let returning = focused.filter(|_| !shortcuts.keys.is_empty()).and_then(|focused| Returning::of(ctx, focused)); let shell = frame::shell_root(ctx, viewport); let edits = focused_edits(ctx, viewport); + let key_layer = with(ctx, |board| board.key_layers.get(&viewport).copied()); + let key_layer = match key_layer { + Some(layer) if overlay::is_open(ctx, layer) => Some(layer), + Some(_) => { + with(ctx, |board| board.key_layers.remove(&viewport)); + None + } + None => None, + }; let taken = with(ctx, |board| { if board.held_open != held_open { board.held_open = held_open; @@ -2100,9 +2139,20 @@ pub(crate) fn begin(ctx: &Context) { // Nothing focused outside a modal: the window's shell root takes the commands, so a window command's key comes // before an app command's, and the page region's after it. Nothing holds focus there, so the chain last focused // stays. + // A window layer that takes the unfocused keys takes them in its body's instances instead, as a modal's root does. let chain = if chain.is_empty() && focused.is_none() { - let page = board.page_roots.get(&viewport).copied(); - shell.into_iter().chain(page).collect() + match key_layer { + Some(layer) => board + .instances + .iter() + .filter(|i| i.viewport == viewport && i.layer == Some(layer) && board.live(ctx, i)) + .map(|i| i.id) + .collect(), + None => { + let page = board.page_roots.get(&viewport).copied(); + shell.into_iter().chain(page).collect() + } + } } else { chain }; diff --git a/crates/ui-contract/src/runtime/commands/mod.rs b/crates/ui-contract/src/runtime/commands/mod.rs index 99b330a8de..2f5add30b8 100644 --- a/crates/ui-contract/src/runtime/commands/mod.rs +++ b/crates/ui-contract/src/runtime/commands/mod.rs @@ -27,6 +27,8 @@ pub use board::focus_within; pub use board::keymap; #[doc(hidden)] pub use board::note_page_root; +#[doc(hidden)] +pub use board::note_unfocused_keys_layer; #[cfg(feature = "testing")] pub(crate) use board::observe_fired; pub(crate) use board::publish as publish_for; diff --git a/crates/ui-shell/src/shell/viewport_window.rs b/crates/ui-shell/src/shell/viewport_window.rs index c465c40fe4..6c99f42e0f 100644 --- a/crates/ui-shell/src/shell/viewport_window.rs +++ b/crates/ui-shell/src/shell/viewport_window.rs @@ -40,6 +40,8 @@ impl Outcome for ViewportWindowOutput { /// - **App-modal.** By default the window blocks nothing. [`app_modal`](ViewportWindow::app_modal) makes the app's /// other windows ignore input while it is open, and a press on one raises this window. /// - **Keys.** Escape asks to close an app-modal window, unless a popup in it is open; a tool window ignores it. +/// [`takes_unfocused_keys`](ViewportWindow::takes_unfocused_keys) gives a layer window the keys pressed with +/// nothing focused. #[must_use = "show the window with `show`"] #[derive(Pattern, Props)] pub struct ViewportWindow { @@ -54,12 +56,16 @@ pub struct ViewportWindow { /// Keeps the window's place and size across runs under `key`. #[prop] remember: Option, + /// Where the window is a layer, a key pressed in its viewport with nothing focused goes to the window's body, not + /// to the window under it: for a window that stands in front of the one it covers while it is shown. + #[prop(flag)] + takes_unfocused_keys: bool, } impl ViewportWindow { /// A window titled `title` that blocks nothing. pub fn new(title: impl Into