mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-30 18:27:00 +01:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
25a6fd365b
commit
dd5c833799
4 files changed
+107
-12
No files matched your search
@@ -198,6 +198,9 @@ pub(crate) struct Board {
|
||||
modal_roots: HashMap<ViewportId, Id>,
|
||||
/// The root of the page region the window's shell shows, per viewport: where keys with nothing focused also go.
|
||||
page_roots: HashMap<ViewportId, Id>,
|
||||
/// The window layer of each viewport whose body takes the keys pressed there with nothing focused
|
||||
/// (`note_unfocused_keys_layer`).
|
||||
key_layers: HashMap<ViewportId, Id>,
|
||||
/// The states published this pass, and the pass before, by an instance inside the top modal layer.
|
||||
modal_published: HashSet<StateKey>,
|
||||
modal_published_last: HashSet<StateKey>,
|
||||
@@ -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<Declaration> {
|
||||
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<Id> {
|
||||
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
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<SaveKey>,
|
||||
/// 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<Label>) -> Self {
|
||||
ViewportWindow { title: title.into(), modal: None, overlay: None, remember: None }
|
||||
ViewportWindow { title: title.into(), modal: None, overlay: None, remember: None, takes_unfocused_keys: false }
|
||||
}
|
||||
|
||||
/// Changes the layer the window falls back to, which starts as the resizable dialog layer: the smallest and
|
||||
@@ -75,7 +81,7 @@ impl ViewportWindow {
|
||||
|
||||
/// Shows the window for this pass with `body` as its content.
|
||||
pub fn show(self, ui: &mut Ui, mut body: impl FnMut(&mut Ui, &mut BodyCx<'_>)) -> ViewportWindowOutput {
|
||||
let ViewportWindow { title, modal, overlay, remember } = self;
|
||||
let ViewportWindow { title, modal, overlay, remember, takes_unfocused_keys } = self;
|
||||
let mut dialog = Dialog::new(title).resizable(true);
|
||||
if let Some(change) = overlay {
|
||||
dialog = dialog.overlay(change);
|
||||
@@ -89,7 +95,12 @@ impl ViewportWindow {
|
||||
dialog = dialog.remember(key);
|
||||
}
|
||||
let mut open = true;
|
||||
let shown = dialog.opened(Bind::bound(&mut open)).show(ui, |ui, cx| body(ui, cx));
|
||||
let shown = dialog.opened(Bind::bound(&mut open)).show(ui, |ui, cx| {
|
||||
if takes_unfocused_keys {
|
||||
crate::runtime::commands::note_unfocused_keys_layer(ui.ctx());
|
||||
}
|
||||
body(ui, cx);
|
||||
});
|
||||
ViewportWindowOutput { response: shown.output.response, close_requested: shown.output.closed }
|
||||
}
|
||||
}
|
||||
@@ -239,3 +239,35 @@ fn a_re_registered_instance_goes_last_in_the_pick() {
|
||||
frames.idle(2, &mut show);
|
||||
assert_eq!(taker.get(), "b", "a registered again after b and c");
|
||||
}
|
||||
|
||||
/// 11.4: outside the modal layers, a `ModalRule::Declared` edit command goes to the innermost instance around the
|
||||
/// focus that declares it, as a slide list's pane takes Select All; with the focus outside that instance, it does not.
|
||||
#[trial]
|
||||
fn a_declared_edit_reaches_the_instance_around_the_focus() {
|
||||
let inside = Cell::new(None);
|
||||
let outside = Cell::new(None);
|
||||
let taken = Cell::new(0);
|
||||
let mut show = |ui: &mut Ui| {
|
||||
super::kit::Pane::new().id_salt("pane").show(ui, |ui, cx| {
|
||||
cx.publish(EditCommands::SelectAll, CommandState::new());
|
||||
while let Some((command, fired)) = cx.take_command::<EditCommands>() {
|
||||
if command == EditCommands::SelectAll && fired.target.is_some() {
|
||||
taken.set(taken.get() + 1);
|
||||
}
|
||||
}
|
||||
inside.set(Some(Tap::new().id_salt("in").show(ui).id));
|
||||
});
|
||||
outside.set(Some(Tap::new().id_salt("out").show(ui).id));
|
||||
};
|
||||
let mut frames = Frames::new();
|
||||
frames.idle(3, &mut show);
|
||||
let select_all = || vec![key_press(Key::A, Modifiers::COMMAND), key_release(Key::A, Modifiers::COMMAND)];
|
||||
focus_on(&mut frames, inside.get().unwrap(), &mut show);
|
||||
frames.frame(select_all(), &mut show);
|
||||
frames.idle(2, &mut show);
|
||||
assert_eq!(taken.get(), 1, "with the focus in the pane, the pane takes Select All");
|
||||
focus_on(&mut frames, outside.get().unwrap(), &mut show);
|
||||
frames.frame(select_all(), &mut show);
|
||||
frames.idle(2, &mut show);
|
||||
assert_eq!(taken.get(), 1, "with the focus outside it, the pane is not its target");
|
||||
}
|
||||
Reference in new issue
Block a user