From 48d19d281053524a4c794f69db88688d770ddbf4 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Tue, 24 Nov 2026 12:08:24 +0700 Subject: [PATCH 1/6] ui-contract: a command fired from a button runs as a command; only a menu's or the palette's fire stands for the key a component claims --- crates/ui-canvas/tests/it/canvas/tool_keys.rs | 17 ++++++++++++++++- .../ui-contract/src/runtime/commands/board.rs | 9 +++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/crates/ui-canvas/tests/it/canvas/tool_keys.rs b/crates/ui-canvas/tests/it/canvas/tool_keys.rs index aab42724d..41eedb0ef 100644 --- a/crates/ui-canvas/tests/it/canvas/tool_keys.rs +++ b/crates/ui-canvas/tests/it/canvas/tool_keys.rs @@ -20,7 +20,7 @@ use craft_ui::dialog::Dialog; use craft_ui::display::Text; use craft_ui::popover::Popover; use craft_ui::primitives::Draw; -use craft_ui::runtime::commands::{self, EditCommands, FireSource}; +use craft_ui::runtime::commands::{self, CommandSet as _, EditCommands, FireSource}; use craft_ui::runtime::frame::take_output; use craft_ui::testing::access::Declared; use craft_ui::testing::focus::{AccessActionKind, key_release}; @@ -169,6 +169,21 @@ fn a_fired_command_whose_key_the_tool_claims_reaches_it_with_nothing_focused() { assert!(take_output(canvas.fixture().ctx()).fired.is_empty(), "no app command ran"); } +/// A command a button fires runs as the button's command, though the tool claims its key: only a menu's or the +/// palette's stands for the key (a toolbar's tool button finishes a gesture the tool holds the keyboard for). +#[trial] +fn a_pointer_fired_command_whose_key_the_tool_claims_runs_as_a_command() { + let mut canvas = ToolFixture::new("Document", Outline::new(true)); + canvas.fixture().settle(); + assert_eq!(canvas.fixture().focused(), None, "nothing is focused"); + take_output(canvas.fixture().ctx()); + assert!(commands::fire(canvas.fixture().ctx(), EditCommands::Delete, FireSource::Pointer), "Delete runs"); + canvas.fixture().settle(); + assert_eq!(canvas.tool().seen, [], "the tool got no Delete"); + let fired = take_output(canvas.fixture().ctx()).fired; + assert!(fired.iter().any(|fired| fired.command.id == EditCommands::Delete.id()), "the app's Delete ran: {fired:?}"); +} + /// A tool that takes every key the host offers it, and the lone Alt tap while `taps`. #[derive(Default)] struct Taker { diff --git a/crates/ui-contract/src/runtime/commands/board.rs b/crates/ui-contract/src/runtime/commands/board.rs index 7af58b6f8..c06b9a868 100644 --- a/crates/ui-contract/src/runtime/commands/board.rs +++ b/crates/ui-contract/src/runtime/commands/board.rs @@ -1731,10 +1731,11 @@ fn queue(ctx: &Context, fired: Fired) { observed.push(fired.command.clone()); } }); - // A command whose key the focused component claims reaches it as that key, as the key's press does; an edit command - // goes to the focused component that lists it; otherwise it goes to the instance that declared it or, with none, - // stays an app command. - if fired.phase != FirePhase::Release { + // A command run from a menu or the palette whose key the focused component claims reaches it as that key, as the + // key's press does (a button's is the button's command, even while a canvas gesture claims every key); an edit + // command goes to the focused component that lists it; otherwise it goes to the instance that declared it or, with + // none, stays an app command. + if fired.phase != FirePhase::Release && matches!(fired.source, FireSource::Menu | FireSource::Palette) { let chords: Vec = bindings(ctx, &fired.command).into_iter().filter(|binding| !binding.trigger.is_double()).map(|binding| binding.trigger.chord).collect(); if keys::fire_claimed(ctx, &chords) { From 51eaf9fd256bd55a966c6369721b5225dbde9fbd Mon Sep 17 00:00:00 2001 From: bflatastic Date: Tue, 24 Nov 2026 12:19:01 +0700 Subject: [PATCH 2/6] ui-contract: a page key the keymap binds to a command that runs now runs it ahead of the document canvas's page scroll, focused or not; an unbound one still pages --- crates/ui-canvas/tests/it/canvas/navigate.rs | 36 ++++++++++++++++++++ crates/ui-contract/src/runtime/keys/route.rs | 20 +++++++++-- crates/ui-contract/src/testing/key_trials.rs | 25 +++++++++----- 3 files changed, 69 insertions(+), 12 deletions(-) diff --git a/crates/ui-canvas/tests/it/canvas/navigate.rs b/crates/ui-canvas/tests/it/canvas/navigate.rs index 87694e16f..bcb8801f4 100644 --- a/crates/ui-canvas/tests/it/canvas/navigate.rs +++ b/crates/ui-canvas/tests/it/canvas/navigate.rs @@ -152,6 +152,42 @@ fn cmd_or_ctrl_with_page_down_scrolls_a_focused_canvas_sideways_on_every_os() { } } +const NEXT_ARTBOARD: &str = "canvasTrial.nextArtboard"; + +/// A page key the app binds to a command runs it ahead of the canvas's page scroll, with nothing focused and with the +/// canvas focused (Page Down goes to the next artboard); an unbound page key still scrolls the view. +#[trial] +fn a_bound_page_key_runs_its_command_and_an_unbound_one_pages_the_canvas() { + let view = Rc::new(Cell::new(CanvasView::new(egui::Rect::ZERO, Point::new(2000.0, 2000.0), 1.0))); + let shown = Rc::clone(&view); + let mut fixture = Fixture::new(move |ui| { + let mut given = shown.get(); + given.rect = ui.available_rect_before_wrap(); + let canvas = Canvas::new("Document").document(Size::new(4000.0, 4000.0)).view(given).show(ui, |_, _| {}); + shown.set(canvas.output.view.unwrap_or(given)); + }); + let binding = Binding::parse("PageDown").expect("the trial's chord parses"); + let catalog = + AppCatalog::new(vec![CommandDef::new(NEXT_ARTBOARD, "Next Artboard", Scope::app()).with_default(binding)], 1).expect("the catalog is valid"); + fixture.commands(CommandInput::new().catalog(catalog)); + fixture.settle(); + let page = |fixture: &mut Fixture, key: Key| { + frame::take_output(fixture.ctx()); + let before = view.get().center; + fixture.key(key, Modifiers::NONE).settle(); + let fired: Vec = frame::take_output(fixture.ctx()).fired.into_iter().map(|fired| fired.command.id).collect(); + (fired, view.get().center != before) + }; + assert_eq!(fixture.focused(), None, "nothing is focused"); + assert_eq!(page(&mut fixture, Key::PageDown), (vec![NEXT_ARTBOARD.to_owned()], false), "unfocused: the bound Page Down runs its command"); + assert_eq!(page(&mut fixture, Key::PageUp), (Vec::new(), true), "unfocused: the unbound Page Up scrolls"); + let canvas = fixture.by_role(Role::CANVAS)[0]; + fixture.click(&canvas); + assert!(fixture.focused().is_some(), "the click focuses the canvas"); + assert_eq!(page(&mut fixture, Key::PageDown), (vec![NEXT_ARTBOARD.to_owned()], false), "focused: the bound Page Down runs its command"); + assert_eq!(page(&mut fixture, Key::PageUp), (Vec::new(), true), "focused: the unbound Page Up scrolls"); +} + #[trial] fn cmd_or_ctrl_with_page_down_on_a_focused_canvas_control_scrolls_the_canvas_sideways() { let view = Rc::new(Cell::new(CanvasView::new(egui::Rect::ZERO, Point::new(2000.0, 2000.0), 1.0))); diff --git a/crates/ui-contract/src/runtime/keys/route.rs b/crates/ui-contract/src/runtime/keys/route.rs index f88b186d9..752a8cca3 100644 --- a/crates/ui-contract/src/runtime/keys/route.rs +++ b/crates/ui-contract/src/runtime/keys/route.rs @@ -140,7 +140,8 @@ pub(crate) fn begin(ctx: &Context, press_captured: bool) -> EscapeRouting { // session control on a canvas), unless a modal layer is open. The window's document canvas is its key target // (`focus::host_canvas`): Page Up and Down, Home and End page it as they do while it has the focus. What the // canvas takes is its alone, as while it has the focus: the app's shortcuts do not run with it too. It takes the - // arrows with the modifiers it acts on (`KeyUse::nudge_mods`); the others are the app's. + // arrows with the modifiers it acts on (`KeyUse::nudge_mods`); the others are the app's, as is a page key bound to + // a command that runs now (`page_key_runs`). let declared = |mods: Mods| canvas.as_ref().is_none_or(|canvas| canvas.stop.keys.nudge_mods.contains(mods)); for (press, taken) in batch.presses.iter().zip(&mut consumed).filter(|(press, _)| !press.canvas) { if let Some(id) = hover_delete @@ -161,6 +162,7 @@ pub(crate) fn begin(ctx: &Context, press_captured: bool) -> EscapeRouting { } else if let Some(canvas) = &canvas && let Some(Intent::Move(mv @ (NavMove::PageUp | NavMove::PageDown | NavMove::Start | NavMove::End), mods)) = keys::intent(os, press.key, press.mods, canvas.stop.role) + && !page_key_runs(ctx, canvas, press) { push(&mut routed, Some(canvas.stop.id), Intent::Move(mv, mods), false); *taken = Taken::Canvas; @@ -277,6 +279,18 @@ fn remap_mods(mods: &[(usize, Mods)], kept: &[bool]) -> Vec<(usize, Mods)> { mods.iter().map(|(at, held)| (kept.get(..*at).unwrap_or(kept).iter().filter(|stays| **stays).count(), *held)).collect() } +/// Whether `press`, a page key (Page Up or Down, Home, End) of the window's document canvas `canvas`, focused or the +/// unfocused window's key target, runs the command the window's keymap binds it to (`commands::runs_now`): that app +/// shortcut goes ahead of the canvas's page scroll (Q370), as the keymap ran it before the canvas paged. A canvas's +/// editor part keeps its page keys as its input. +fn page_key_runs(ctx: &Context, canvas: &StopRec, press: &Press) -> bool { + let keys = &canvas.stop.keys; + keys.chrome_key + && !keys.is_document_editor() + && matches!(press.key, Key::PageUp | Key::PageDown | Key::Home | Key::End) + && commands::runs_now(ctx, crate::runtime::egui::pass::current_viewport(ctx), &Chord { mods: press.mods, key: press.key }) +} + pub(super) fn push(routed: &mut HashMap, Routed>, target: Option, intent: Intent, horizontal: bool) { routed.entry(target).or_default().items.push(Item { intent, horizontal, source: None }); } @@ -687,8 +701,8 @@ fn press_route(ctx: &Context, os: Os, rec: &StopRec, press: &Press, driven: Opti let leaves_row = !caret && is_vertical(mv) && focus::is_row_child(ctx, stop); let kept = caret || (stop.part.is_none() && stop.keys.keep.keeps_arrow(mv)); // A stop that does not act on its moves leaves a move key a scoped command around it binds to that - // command, ahead of its roving group and the scroll owner. - if !leaves_row && !kept && !stop.role.moves && commands::binds_scoped(ctx, &chord) { + // command, ahead of its roving group and the scroll owner; the document canvas leaves a bound page key. + if (!leaves_row && !kept && !stop.role.moves && commands::binds_scoped(ctx, &chord)) || page_key_runs(ctx, rec, press) { PressRoute::Free } else { PressRoute::Move(mv, mods) diff --git a/crates/ui-contract/src/testing/key_trials.rs b/crates/ui-contract/src/testing/key_trials.rs index acc91136a..ff1e4e6cf 100644 --- a/crates/ui-contract/src/testing/key_trials.rs +++ b/crates/ui-contract/src/testing/key_trials.rs @@ -231,9 +231,10 @@ fn a_recorder_reads_a_press_as_one_chord_and_no_command_runs() { assert_eq!(press_k("button").0, Vec::new(), "no other role reads chords"); } -/// With nothing focused, the window's document canvas takes Page Down and the arrows' nudge, as while it has the focus; -/// the press is its alone, so no app shortcut runs with it as well (a page view would turn twice). Without a canvas the -/// press is the app's. +/// With nothing focused, the window's document canvas takes the arrows' nudge and an unbound Page Up, as while it has +/// the focus; the press is its alone, so no app shortcut runs with it as well (a page view would turn twice). A page key +/// the app binds to a command that runs is the app's (Page Down to the next artboard). Without a canvas every press is +/// the app's. #[trial] fn an_unfocused_press_the_window_canvas_takes_reaches_no_app_shortcut() { let paged = Rc::new(Cell::new(0)); @@ -245,8 +246,10 @@ fn an_unfocused_press_the_window_canvas_takes_reaches_no_app_shortcut() { stop(ui, "canvas", KeyUse::CANVAS); } let mut intents = intents_of(ui.ctx(), Some(id("canvas")), false); - if intents.take(Intent::Move(craft_behavior::NavMove::PageDown, craft_behavior::MoveMods::PLAIN)) { - sink.set(sink.get() + 1); + for page in [craft_behavior::NavMove::PageDown, craft_behavior::NavMove::PageUp] { + if intents.take(Intent::Move(page, craft_behavior::MoveMods::PLAIN)) { + sink.set(sink.get() + 1); + } } }; let mut bound = Overrides::default(); @@ -256,17 +259,21 @@ fn an_unfocused_press_the_window_canvas_takes_reaches_no_app_shortcut() { frames.commands(CommandInput::new().overrides(Arc::new(bound))); frames.frame(Vec::new(), &mut show); take_output(&frames.ctx); - let fired = |frames: &mut Frames, show: &mut dyn FnMut(&mut Ui)| { - frames.frame(vec![key_press(Key::PageDown, Modifiers::NONE), key_press(Key::ArrowRight, Modifiers::NONE)], &mut *show); + let fired = |frames: &mut Frames, show: &mut dyn FnMut(&mut Ui), key: Key| { + frames.frame(vec![key_press(key, Modifiers::NONE), key_release(key, Modifiers::NONE)], &mut *show); frames.frame(Vec::new(), &mut *show); take_output(&frames.ctx).fired.len() }; - assert_eq!(fired(&mut frames, &mut show), 0, "neither press reaches the app's commands"); + assert_eq!(fired(&mut frames, &mut show, Key::ArrowRight), 0, "the arrow is the canvas's nudge"); + assert_eq!(fired(&mut frames, &mut show, Key::PageUp), 0, "the unbound Page Up is the canvas's"); assert_eq!(paged.get(), 1, "the canvas pages"); + assert_eq!(fired(&mut frames, &mut show, Key::PageDown), 1, "the bound Page Down is the app's"); + assert_eq!(paged.get(), 1, "the canvas does not page with it"); canvas.set(false); frames.idle(2, &mut show); take_output(&frames.ctx); - assert_eq!(fired(&mut frames, &mut show), 2, "without a canvas both presses are the app's"); + let presses = [Key::PageDown, Key::ArrowRight].map(|key| fired(&mut frames, &mut show, key)); + assert_eq!(presses, [1, 1], "without a canvas both presses are the app's"); } /// A recorder holds every press: the app reads none from egui's input, and `focus_holds` answers so before the press. From aa973d98fa85c578a933eb8e3c54a64a71649283 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Tue, 24 Nov 2026 12:23:14 +0700 Subject: [PATCH 3/6] ui-contract: a press the focused component holds leaves egui's input again unless the app's own widget reads it; the merge b138b533f passed whether anything had focus for whether the focus was the app's widget --- crates/ui-contract/src/runtime/keys/route.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/ui-contract/src/runtime/keys/route.rs b/crates/ui-contract/src/runtime/keys/route.rs index 752a8cca3..07f405ab1 100644 --- a/crates/ui-contract/src/runtime/keys/route.rs +++ b/crates/ui-contract/src/runtime/keys/route.rs @@ -187,7 +187,9 @@ pub(crate) fn begin(ctx: &Context, press_captured: bool) -> EscapeRouting { } let over: Vec = batch.presses.iter().map(held_press).collect(); let (shortcuts, held) = owned_shortcuts(ctx, target.as_ref(), &batch, &consumed, &taken_texts, &over); - withdraw_taken(ctx, target.is_some(), &batch, &held, &withdrawn_texts); + // The app's own widget, focused or the window's canvas, reads its keys from egui. + let host = target.as_ref().or(canvas.as_ref()).is_some_and(|rec| rec.stop.host); + withdraw_taken(ctx, host, &batch, &held, &withdrawn_texts); let pasted = batch.paste.clone().zip(focused).map(|(content, id)| (id, content)); // PLAN: a repeat Tab of a held-over key is not gated; `take_nav` reads `batch.tabs` before this hook runs (owner: focus). // One Escape press cancels once: its repeats cancel nothing more, in the layer it closed or in what that uncovered. From d3b03c1b44186d0bcf7a69221919d71a7994098f Mon Sep 17 00:00:00 2001 From: bflatastic Date: Tue, 24 Nov 2026 13:30:24 +0700 Subject: [PATCH 4/6] ui-contract: a command a key, a native menu pick or the palette runs no longer closes a layer whose Dismiss::commands is off; a dock panel's flyout turns it off, so the command no longer closes and reopens it and a focused list in it keeps the focus --- .../ui-contract/src/contract/overlay/host.rs | 2 + .../ui-contract/src/contract/overlay/props.rs | 13 ++++- crates/ui-contract/src/runtime/overlay.rs | 12 +++- crates/ui-panels/src/docking/flyout.rs | 9 ++- crates/ui-panels/src/docking/iconic/trials.rs | 56 ++++++++++++++++++- 5 files changed, 84 insertions(+), 8 deletions(-) diff --git a/crates/ui-contract/src/contract/overlay/host.rs b/crates/ui-contract/src/contract/overlay/host.rs index d16b5617b..25ebaa196 100644 --- a/crates/ui-contract/src/contract/overlay/host.rs +++ b/crates/ui-contract/src/contract/overlay/host.rs @@ -253,6 +253,7 @@ pub(crate) fn host(ctx: &Context, layer: &Layer<'_>, body: &mut dyn FnMut(&mut U entry.region = layer.region; entry.scoped = scope.is_some(); entry.absorbs = overlay.dismiss.outside == Outside::Absorb; + entry.closes_on_commands = overlay.dismiss.commands; overlay::record(ctx, entry); let dismissed = if layer.exiting { None } else { dismissal(ctx, layer, shown.rect, anchor, scope, &mut memory) }; // The layer closes after its body ran: the run shows it open, so a record of the run is not replayed. @@ -330,6 +331,7 @@ fn host_in_popup(ctx: &Context, layer: &Layer<'_>, popup: PopupRoom, body: &mut entry.exiting = layer.exiting; entry.scoped = scope.is_some(); entry.absorbs = overlay.dismiss.outside == Outside::Absorb; + entry.closes_on_commands = overlay.dismiss.commands; entry.window = true; entry.popup = true; overlay::record(ctx, entry); diff --git a/crates/ui-contract/src/contract/overlay/props.rs b/crates/ui-contract/src/contract/overlay/props.rs index 2d9d8dfe6..a01d00a92 100644 --- a/crates/ui-contract/src/contract/overlay/props.rs +++ b/crates/ui-contract/src/contract/overlay/props.rs @@ -41,14 +41,16 @@ pub struct Dismiss { pub on_activate: bool, /// The anchor is no longer shown. pub anchor_gone: bool, + /// A command a key, a native menu pick or the palette ran, from outside the layer and the layers opened from it. + pub commands: bool, /// Seconds after opening. pub timeout: Option, } impl Dismiss { - /// An outside press closes; nothing else does. + /// An outside press or a command closes; nothing else does. pub const fn new() -> Self { - Dismiss { outside: Outside::Close, enter: false, focus_left: false, on_activate: false, anchor_gone: true, timeout: None } + Dismiss { outside: Outside::Close, enter: false, focus_left: false, on_activate: false, anchor_gone: true, commands: true, timeout: None } } /// Sets what an outside press does. @@ -75,6 +77,13 @@ impl Dismiss { self } + /// Closes when a key, a native menu pick or the palette runs a command from outside it; off for a layer its host + /// opens and closes, as a dock panel's flyout. + pub const fn commands(mut self, on: bool) -> Self { + self.commands = on; + self + } + /// Closes after `seconds`. pub const fn timeout(mut self, seconds: Option) -> Self { self.timeout = seconds; diff --git a/crates/ui-contract/src/runtime/overlay.rs b/crates/ui-contract/src/runtime/overlay.rs index f8b607866..5046e20d5 100644 --- a/crates/ui-contract/src/runtime/overlay.rs +++ b/crates/ui-contract/src/runtime/overlay.rs @@ -168,6 +168,8 @@ pub struct OpenEntry { pub(crate) scoped: bool, /// A press outside its family closes it and goes no further. pub(crate) absorbs: bool, + /// A command run from outside it closes it (`Dismiss::commands`). + pub(crate) closes_on_commands: bool, /// The entry is an OS window of the integration: it takes its place in the stack's order and facts, and no egui /// layer is shown for it. pub(crate) window: bool, @@ -225,6 +227,7 @@ impl OpenEntry { cover: None, scoped: false, absorbs: false, + closes_on_commands: true, window: false, popup: false, region: None, @@ -365,7 +368,8 @@ pub fn request_close(ctx: &Context, id: Id, why: CloseReason) { push_close(ctx, id, why, false); } -/// Queues a close of every popup layer of the running viewport shown last frame: its menus, dropdowns and lists. The +/// Queues a close of every popup layer of the running viewport shown last frame that a command closes +/// (`Dismiss::commands`): its menus, dropdowns and lists, not a dock panel's flyout. The /// runtime's command dispatcher calls it when a key runs a command; an app whose own dispatcher runs a shortcut from the /// keys the runtime leaves in egui's input calls it when the shortcut runs. Either way the command closes the open /// popups as a menu pick does; modal layers, windows and toasts stay. @@ -387,7 +391,11 @@ pub(crate) fn close_popups_outside(ctx: &Context, inside: &[Id]) { at = pass.last.iter().find(|entry| entry.id == id).and_then(|entry| entry.parent); } } - pass.last.iter().filter(|entry| entry.rank == Rank::Popup && !entry.exiting && !kept.contains(&entry.id)).map(|entry| entry.id).collect() + pass.last + .iter() + .filter(|entry| entry.rank == Rank::Popup && entry.closes_on_commands && !entry.exiting && !kept.contains(&entry.id)) + .map(|entry| entry.id) + .collect() }); for id in popups { push_close(ctx, id, CloseReason::Requested, false); diff --git a/crates/ui-panels/src/docking/flyout.rs b/crates/ui-panels/src/docking/flyout.rs index 8b62c56a1..645360fbc 100644 --- a/crates/ui-panels/src/docking/flyout.rs +++ b/crates/ui-panels/src/docking/flyout.rs @@ -90,13 +90,18 @@ impl Flyout { let bounded = self.within.is_finite(); let least = if bounded { self.least.min(self.within.size().max(Vec2::ZERO)) } else { self.least }; // A float place as well as the caller's position: the placement pins no edge, so the edge set alone decides - // the grips. + // the grips. The host's binding opens and closes the flyout, so a command leaves it open. let overlay = Overlay::popover() .place(Place::Float(FloatPlace::at(top_left))) .placed_at(top_left) .size_limits(SizeLimits::at_least(least)) .resize_edges(free.union(ResizeEdges::TOP).union(ResizeEdges::BOTTOM)) - .dismiss(Dismiss::new().outside(if self.auto_collapse { Outside::Close } else { Outside::Keep }).focus_left(self.auto_collapse)) + .dismiss( + Dismiss::new() + .outside(if self.auto_collapse { Outside::Close } else { Outside::Keep }) + .focus_left(self.auto_collapse) + .commands(false), + ) .body_scroll(false); if bounded { overlay.bounds(self.within) } else { overlay } } diff --git a/crates/ui-panels/src/docking/iconic/trials.rs b/crates/ui-panels/src/docking/iconic/trials.rs index 65ad0bdbf..1b1f02a33 100644 --- a/crates/ui-panels/src/docking/iconic/trials.rs +++ b/crates/ui-panels/src/docking/iconic/trials.rs @@ -1,15 +1,20 @@ //! Trials of `IconicColumn`: the letter of a panel without an icon, the names a wide column shows, the grips of its -//! groups, and the resizes of its flyouts. +//! groups, the resizes of its flyouts, and a flyout that a command leaves open. #![allow(clippy::panic, clippy::expect_used, reason = "trials assert by panicking")] use super::GRIP; +use crate::buttons::Button; +use crate::contract::overlay::{self, OpenFilter}; use crate::contract::{Bind, Show as _, ShowContainer as _}; use crate::docking::group::GRIP_ABOVE; use crate::docking::{ DockHeights, DockSizes, DockSpot, DockTargets, DragUnit, FlyoutResize, GroupAt, GroupDrag, GroupGrip, IconicColumn, PanelKey, PanelSpec, }; +use crate::runtime::commands::{AppCatalog, Binding, CommandDef, CommandInput, Scope}; use crate::runtime::drag; use crate::runtime::env::Direction; +use crate::runtime::focus::{FocusTarget, request}; +use crate::runtime::frame::take_output; use crate::sections::Panel; use crate::style::StyleCx; use crate::testing::memory::global; @@ -18,7 +23,7 @@ use crate::testing::{Fixture, trial}; use craft_behavior::DockSide; use craft_behavior::dock::{Action, Column, DockEdge, Group, Layout, Node}; use craft_behavior::overlay::Side; -use egui::{Pos2, Rect, Vec2}; +use egui::{Key, Modifiers, Pos2, Rect, UiBuilder, Vec2}; use std::cell::{Cell, RefCell}; use std::rc::Rc; @@ -238,6 +243,53 @@ fn a_press_outside_closes_the_flyout_only_with_auto_collapse() { } } +/// The app command the trials bind to a key. +const NUDGE: &str = "flyoutTrial.nudge"; + +/// A key that runs an app command, with a stop in the open flyout focused, leaves the flyout open and live in every +/// pass and the stop focused, with Auto-Collapse Iconic Panels off or on: the host's binding opens and closes a flyout, +/// and a command closes only the transient popups (menus, dropdowns, lists). +#[trial] +fn a_key_command_leaves_the_open_flyout_and_the_focus_in_it() { + for auto_collapse in [false, true] { + let (open, stop, faded) = (Rc::new(Cell::new(None)), Rc::new(Cell::new(None)), Rc::new(Cell::new(false))); + let (seen, shown, fade) = (Rc::clone(&open), Rc::clone(&stop), Rc::clone(&faded)); + let place = Rect::from_min_size(Pos2::ZERO, Vec2::new(RAIL, SCREEN.y)); + let mut fixture = Fixture::new(move |ui| { + let column = Column::new(vec![Group::new(vec![0, 1], 0)]); + let mut now = seen.get(); + IconicColumn::new("Panels", global("iconic column", "commands"), &column, Bind::bound(&mut now), &spec) + .within(screen()) + .auto_collapse(auto_collapse) + .show_flyouts_at(ui, place, |_, at, ui, _| { + let GroupAt::Show(rect) = at else { return None }; + shown.set(Some(ui.scope_builder(UiBuilder::new().max_rect(rect), |ui| Button::new("Snap").show(ui).id).inner)); + None + }); + // Last pass the open flyout was not among the live layers that take keys: it played its exit. + fade.set(fade.get() || (now.is_some() && !overlay::any_open(ui.ctx(), OpenFilter::Keys))); + seen.set(now); + }); + let binding = Binding::parse("F9").expect("the trial's chord parses"); + let catalog = AppCatalog::new(vec![CommandDef::new(NUDGE, NUDGE, Scope::app()).with_default(binding)], 1).expect("the catalog is taken"); + fixture.commands(CommandInput::new().catalog(catalog)).room(SCREEN).settle(); + let icon = fixture.by_label("Swatches").expect("the Swatches icon").rect.center(); + fixture.click_at(icon).settle(); + let button = stop.get().expect("the flyout shows its stop"); + request(fixture.ctx(), FocusTarget::Id(button)); + fixture.settle(); + assert_eq!(fixture.focused_stop().map(|(id, _)| id), Some(button), "auto-collapse {auto_collapse}: the flyout's stop has the focus"); + faded.set(false); + take_output(fixture.ctx()); + fixture.key(Key::F9, Modifiers::NONE).settle(); + let fired = take_output(fixture.ctx()).fired; + assert!(fired.iter().any(|fired| fired.command.id == NUDGE), "auto-collapse {auto_collapse}: the key runs its command"); + assert_eq!(open.get(), Some(1), "auto-collapse {auto_collapse}: the flyout stays open"); + assert!(!faded.get(), "auto-collapse {auto_collapse}: and never plays its exit"); + assert_eq!(fixture.focused_stop().map(|(id, _)| id), Some(button), "auto-collapse {auto_collapse}: and its stop keeps the focus"); + } +} + /// The top edge and the free top corner grow the flyout upward on either window edge: the bottom and the column-side /// edge stay, the column-side edge has no grip, and the new top is kept as an offset from the icons, so it survives /// another group's flyout and reopening. From 49d439d63da5b0521370ce42960c7bb8acbbb075 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Tue, 24 Nov 2026 13:33:04 +0700 Subject: [PATCH 5/6] ui-widgets: a tile grid without card_columns asks for one card in a measuring run again (ebf925452, lost in the merge b138b533f), so a dialog pane sized from it holds its rows and the row under them: presses on the tiles, the button and the field beside it land on them --- .../tests/it/form_dialogs/picker.rs | 67 +++++++++++++++++++ crates/ui-widgets/src/tile_grid/grid.rs | 3 - 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/crates/ui-patterns/tests/it/form_dialogs/picker.rs b/crates/ui-patterns/tests/it/form_dialogs/picker.rs index 2036ecdd2..b16ce939a 100644 --- a/crates/ui-patterns/tests/it/form_dialogs/picker.rs +++ b/crates/ui-patterns/tests/it/form_dialogs/picker.rs @@ -130,3 +130,70 @@ fn a_picker_dialog_settles_after_opening() { assert!(passes[2..].iter().all(|&n| n == 1), "{name}: every frame from the third runs one pass: {passes:?}; {warned:?}"); } } + +/// A form dialog with two panes side by side, as Export for Screens lays them out: a heading, a grid of boards and a +/// row of a button and a text field that takes the focus as the dialog opens, and options beside. Counts the button's +/// clicks and the grid's selection events. +fn panes_with_a_field(clicks: &Rc>, picks: &Rc>) -> Fixture { + use craft_ui::buttons::Button; + use craft_ui::contract::{CollectionEvent, Props as _}; + use craft_ui::list::ToggleState; + use craft_ui::runtime::focus::FocusRequest; + use craft_ui::text::TextField; + let opening = Rc::new(Cell::new(true)); + let (clicks, picks, opens) = (clicks.clone(), picks.clone(), opening.clone()); + let mut fixture = Fixture::new(move |ui| { + let mut value = String::new(); + FormDialog::new("Export", Bind::bound(&mut value)).show(ui, |ui, _, _| { + let keys: Vec = (0..3).collect(); + let source = keyed(&keys, |key: &u64| *key); + Row::new().show(ui, |ui, _| { + Stack::new().show(ui, |ui, _| { + Text::new("Artboards").show(ui); + let out = TileGrid::new("Boards").checks(true).max_rows(3).show_items(ui, &source, &mut |key, _| { + Tile::new(format!("Board {key}")).check(ToggleState::Off).thumb(ThumbAspect::SQUARE, |_, _| {}) + }); + picks.set(picks.get() + out.response.events.iter().filter(|event| matches!(event, CollectionEvent::Selection { .. })).count()); + Row::new().show(ui, |ui, _| { + clicks.set(clicks.get() + Button::new("Clear Selection").show(ui).clicks); + let field = TextField::new("Prefix", Bind::given(String::new())); + if opens.get() { field.request_focus(FocusRequest::Take) } else { field }.show(ui); + }); + }); + Stack::new().show(ui, |ui, _| { + Text::new("Options").show(ui); + }); + }); + true + }); + }); + fixture.room(egui::vec2(1024.0, 768.0)); + fixture.settle(); + opening.set(false); + assert!(fixture.focused_stop().is_some(), "the field takes the focus"); + fixture +} + +/// A press on a button in a row beside a focused text field, under a grid, is the button's: the pane's room holds the +/// grid's rows and what follows them. (The field keeps the focus, as a dialog's field does when one of its buttons is +/// pressed.) +#[trial] +fn a_press_on_a_button_beside_a_field_is_the_buttons() { + let (clicks, picks) = (Rc::new(Cell::new(0)), Rc::new(Cell::new(0))); + let mut fixture = panes_with_a_field(&clicks, &picks); + let button = fixture.by_label("Clear Selection").expect("the button shows"); + fixture.click(&button).settle(); + assert_eq!(clicks.get(), 1, "the press on the button clicks it"); +} + +/// A click on a tile of a grid over a focused text field picks the tile, and takes the focus off the field. +#[trial] +fn a_tile_click_beside_a_focused_field_is_reported() { + let (clicks, picks) = (Rc::new(Cell::new(0)), Rc::new(Cell::new(0))); + let mut fixture = panes_with_a_field(&clicks, &picks); + let field = fixture.focused_stop(); + let tile = fixture.by_label("Board 2").expect("the tile shows"); + fixture.click(&tile).settle(); + assert!(picks.get() > 0, "the grid reports the click on its tile as a selection"); + assert_ne!(fixture.focused_stop(), field, "and the field loses the focus"); +} diff --git a/crates/ui-widgets/src/tile_grid/grid.rs b/crates/ui-widgets/src/tile_grid/grid.rs index 36961bf62..07d0c57aa 100644 --- a/crates/ui-widgets/src/tile_grid/grid.rs +++ b/crates/ui-widgets/src/tile_grid/grid.rs @@ -1042,9 +1042,6 @@ impl Collection for TileGrid<'_, K /// The last layout again while the width, the rows stamp and everything the arrangement reads are the same. fn ideal_width(&self, item: Option, _rows: usize, cx: &StyleCx<'_>) -> f32 { - if self.card_columns == 0 { - return 0.0; - } let style = GridSizes::of(cx, self.card, self.card_text, self.items.row_height, self.regular_card_padding, self.action_place); let aspect = if self.aspect.is_finite() && self.aspect > 0.0 { self.aspect } else { 1.0 }; let around = style.around.get(self.caption, self.detail_line); From bbef5516c0ab0cc5b84b8b64e4907baf095b90e4 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Tue, 24 Nov 2026 13:48:36 +0700 Subject: [PATCH 6/6] ui-widgets: the selectable text trial names its canvas with Id::unique, as egui deprecates Id::new --- crates/ui-widgets/tests/it/text/selectable.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ui-widgets/tests/it/text/selectable.rs b/crates/ui-widgets/tests/it/text/selectable.rs index c471d839b..ecc76542b 100644 --- a/crates/ui-widgets/tests/it/text/selectable.rs +++ b/crates/ui-widgets/tests/it/text/selectable.rs @@ -153,7 +153,7 @@ fn metadata_value_copies_without_selecting_the_row() { fn tab_toggles_the_panels_from_read_only_text_a_press_or_drag_focused() { let text = Rc::new(Cell::new(Id::NULL)); let seen = text.clone(); - let canvas = Id::new("document canvas"); + let canvas = Id::unique("document canvas"); let mut fixture = Fixture::new(move |ui| { Button::new("Save").show(ui); seen.set(SelectableText::new("Path", "/projects/poster.indd").show(ui).id);