From f15b0e87d2821784474eedff764fb70477734ef9 Mon Sep 17 00:00:00 2001 From: aiongg Date: Wed, 25 Nov 2026 14:22:25 +0000 Subject: [PATCH] An Escape nothing in a floated dock window takes is the dock window's Cancel A dock panel's OS window gets its keys alone, so an Escape pressed there with nothing focused became that window's Cancel, which no app code reads: a command started from a floated palette could not be cancelled. The end hook hands a Cancel left in such a window (`WindowSpec::hands_on_cancel`) to the window it was opened from, which reads it as its own in its next pass. A popup or a modal open in the floated window keeps the Escape, and a modal that opened meanwhile drops it. --- crates/ui-contract/src/runtime/frame.rs | 2 + crates/ui-contract/src/runtime/keys.rs | 7 +- .../ui-contract/src/runtime/keys/intents.rs | 24 ++++- crates/ui-contract/src/runtime/keys/route.rs | 11 +++ crates/ui-contract/src/runtime/overlay.rs | 6 ++ crates/ui-contract/src/runtime/windows.rs | 8 ++ .../ui-contract/src/runtime/windows/spec.rs | 11 +++ .../src/runtime/windows/viewport.rs | 2 + crates/ui-panels/src/docking/native.rs | 6 +- crates/ui-patterns/tests/it/docking/native.rs | 95 +++++++++++++++++++ docs/craft-framework.md | 2 +- 11 files changed, 168 insertions(+), 6 deletions(-) diff --git a/crates/ui-contract/src/runtime/frame.rs b/crates/ui-contract/src/runtime/frame.rs index 98672ec6f1..e810c741b8 100644 --- a/crates/ui-contract/src/runtime/frame.rs +++ b/crates/ui-contract/src/runtime/frame.rs @@ -871,6 +871,8 @@ pub(crate) fn end_pass(ui: &mut Ui) { paint_frame_layers(&ctx); } toast::end_pass(&ctx); + // Every taker of the pass has run, and the layers and the focus are the pass's still. + keys::hand_on_cancel(&ctx); overlay::end_pass(&ctx); focus::end(&ctx); let sweeps: Vec = pass_state::with::(&ctx, |state| state.sweeps.values().copied().collect()); diff --git a/crates/ui-contract/src/runtime/keys.rs b/crates/ui-contract/src/runtime/keys.rs index c2762c7415..9e1f035709 100644 --- a/crates/ui-contract/src/runtime/keys.rs +++ b/crates/ui-contract/src/runtime/keys.rs @@ -42,8 +42,8 @@ pub use intents::settle; #[doc(hidden)] pub use intents::take_submit; pub(crate) use intents::{ - Lent, context_part, context_request, context_requests, give_back, lend, request_context, route_whole_reset, scope_escape, take_cancel, - take_every_escape, take_window_cancel, + Lent, context_part, context_request, context_requests, give_back, hand_on_cancel, lend, request_context, route_whole_reset, scope_escape, + take_cancel, take_every_escape, take_window_cancel, }; pub use intents::{with_canvas_window_intents, with_window_intents}; pub(crate) use nodes::{redirect_node, register_expanded, register_node}; @@ -337,6 +337,9 @@ pub(crate) struct Stage { held_over: Vec, /// The modifiers of the last Escape of the pass that became a `Cancel`. escape_mods: Option, + /// The Escapes that windows opened from this one handed on (`intents::hand_on_cancel`), by their modifiers: the next + /// pass's `Cancel`s. + handed_cancels: Vec, /// How many lent keys session members have taken (`lend`), counted over the viewport's life. lent_taken: u64, /// The pane moves that menu, palette or automation fires of `NavCommand` asked for: `true` is forward. diff --git a/crates/ui-contract/src/runtime/keys/intents.rs b/crates/ui-contract/src/runtime/keys/intents.rs index d97420d34b..6b368115a4 100644 --- a/crates/ui-contract/src/runtime/keys/intents.rs +++ b/crates/ui-contract/src/runtime/keys/intents.rs @@ -1,7 +1,7 @@ //! The intents routed to a component and what it leaves untaken: Submit, Cancel, Escape and context requests. use super::route::push; -use super::{Nudge, Source, Stage, current}; +use super::{Nudge, Source, Stage, current, with}; use crate::runtime::focus::{self}; use craft_behavior::focus::{EscapeFacts, EscapeStep, escape_step}; use craft_behavior::{AccessRequest, CancelRule, EditCommand, EditorInput, Intent, MoveMods, NavMove, OwnerClass, PartRef}; @@ -442,6 +442,28 @@ pub(crate) fn take_window_cancel(ctx: &Context) -> bool { with_canvas_window_intents(ctx, false, |window| window.take(Intent::Cancel)) } +/// The end hook's hand-on: an Escape that nothing in the running window took, not even a reader of its intents, is the +/// Cancel of the window it was opened from, where its spec says so (`WindowSpec::hands_on_cancel`). That window reads it +/// in its next pass, which is requested. A popup or a modal open in the running window keeps the Escapes it leaves. +pub(crate) fn hand_on_cancel(ctx: &Context) { + let Some(opener) = crate::runtime::windows::cancel_to(ctx) else { return }; + if crate::runtime::overlay::popup_or_modal_open(ctx) { + return; + } + let mut left = 0; + with_window_intents(ctx, false, |window| { + while window.take(Intent::Cancel) { + left += 1; + } + left > 0 + }); + if left > 0 { + let mods = current(ctx, |stage| stage.escape_mods).unwrap_or(Mods::NONE); + with(ctx, opener, |stage| stage.handed_cancels.extend(std::iter::repeat_n(mods, left))); + crate::runtime::egui::repaint::request_of(ctx, opener); + } +} + /// Writes back what a component left untaken, so a scope's `Submit` and `Cancel` rules see it. #[doc(hidden)] pub fn settle(ctx: &Context, id: Option, intents: &Intents) { diff --git a/crates/ui-contract/src/runtime/keys/route.rs b/crates/ui-contract/src/runtime/keys/route.rs index 9a001b6986..d1767e89a9 100644 --- a/crates/ui-contract/src/runtime/keys/route.rs +++ b/crates/ui-contract/src/runtime/keys/route.rs @@ -209,6 +209,17 @@ pub(crate) fn begin(ctx: &Context, press_captured: bool) -> EscapeRouting { push(&mut routed, focused, Intent::Cancel, false); cancel_mods = Some(escape.mods); } + // The Escapes the windows opened from this one handed on are the window's. A modal that opened meanwhile blocks the + // window they were pressed in, so they reach nothing. + let handed = current(ctx, |stage| std::mem::take(&mut stage.handed_cancels)); + if !handed.is_empty() && overlay::app_modal_viewport(ctx).is_none() { + for mods in handed { + push(&mut routed, None, Intent::Cancel, false); + cancel_mods = Some(mods); + } + // A view that reads what is sent (`Input::Sent`) runs for them as for a key of the window's own. + current(ctx, |stage| stage.sent += 1); + } // A text opener's list keys arrive as claimed chords, withheld from its text part: they go to the list. The chords // fired commands stood for go to the owners of their claims. let mut claimed = fired_claims; diff --git a/crates/ui-contract/src/runtime/overlay.rs b/crates/ui-contract/src/runtime/overlay.rs index 1d127d127e..3de46a52f4 100644 --- a/crates/ui-contract/src/runtime/overlay.rs +++ b/crates/ui-contract/src/runtime/overlay.rs @@ -449,6 +449,12 @@ pub fn any_open(ctx: &Context, filter: OpenFilter) -> bool { }) } +/// Whether a popup or a modal of the running viewport was shown last frame: the layers whose rank takes keys, which the +/// window of a dock is not. +pub(crate) fn popup_or_modal_open(ctx: &Context) -> bool { + with_pass(ctx, |pass| pass.last.iter().any(|entry| !entry.exiting && entry.rank.takes_keys())) +} + /// The layer that receives Cancel: the newest member of the topmost family that takes keys. pub fn top(ctx: &Context) -> Option { crate::runtime::replay::note_input(ctx, crate::runtime::inputs::Input::Overlays); diff --git a/crates/ui-contract/src/runtime/windows.rs b/crates/ui-contract/src/runtime/windows.rs index 4035cd796b..9cc9b43764 100644 --- a/crates/ui-contract/src/runtime/windows.rs +++ b/crates/ui-contract/src/runtime/windows.rs @@ -161,6 +161,9 @@ pub(crate) struct Record { min_size: Option, /// For a popup window ([`show_popup`]): the window its layer was opened from, while the layer shows. opener: Option, + /// The window an Escape nothing in this one takes goes to: the one it was opened from, where its spec says so + /// (`WindowSpec::hands_on_cancel`). + cancel_to: Option, } /// The raise of the window holding an app-modal layer above the palettes the modal lowered. The raise goes out in the @@ -217,6 +220,11 @@ pub(crate) fn os_resizes(ctx: &Context, viewport: ViewportId) -> bool { chrome == WindowChrome::Inset && PerViewport::::with(ctx, viewport, |record| record.resizable != Some(false)) } +/// The window an Escape nothing in the running viewport's window takes goes to (`WindowSpec::hands_on_cancel`). +pub(crate) fn cancel_to(ctx: &Context) -> Option { + PerViewport::::with_existing(ctx, pass::current_viewport(ctx), |record| record.cancel_to).flatten() +} + /// Whether the app runs the move of `viewport`'s window (`Overlay::moved_by_app`). pub(crate) fn app_moves(ctx: &Context, viewport: ViewportId) -> bool { PerViewport::::with(ctx, viewport, |record| record.moved_by_app) diff --git a/crates/ui-contract/src/runtime/windows/spec.rs b/crates/ui-contract/src/runtime/windows/spec.rs index af9e889e60..95115c0597 100644 --- a/crates/ui-contract/src/runtime/windows/spec.rs +++ b/crates/ui-contract/src/runtime/windows/spec.rs @@ -89,6 +89,9 @@ pub struct WindowSpec { pub takes_focus: bool, /// Whether pointer input passes through the window to the window below it. pub passes_pointer: bool, + /// Whether an Escape nothing in the window takes is the Cancel of the window it was opened from (a dock window's): + /// no component, layer, capture or app widget of the window, and no reader of its intents. + pub hands_on_cancel: bool, } impl WindowSpec { @@ -121,6 +124,7 @@ impl WindowSpec { shown_after_first_paint: false, takes_focus: true, passes_pointer: false, + hands_on_cancel: false, } } @@ -184,6 +188,13 @@ impl WindowSpec { self } + /// Sets whether an Escape nothing in the window takes is the Cancel of the window it was opened from. + #[must_use] + pub fn hands_on_cancel(mut self, hands_on_cancel: bool) -> Self { + self.hands_on_cancel = hands_on_cancel; + self + } + /// Sets whether the window opens hidden and appears after its first paint, so a window that shows a capture or /// other content that must be ready does not flash an empty frame. #[must_use] diff --git a/crates/ui-contract/src/runtime/windows/viewport.rs b/crates/ui-contract/src/runtime/windows/viewport.rs index 80a45b4825..c669d95fde 100644 --- a/crates/ui-contract/src/runtime/windows/viewport.rs +++ b/crates/ui-contract/src/runtime/windows/viewport.rs @@ -54,6 +54,7 @@ pub(crate) fn show_viewport(ctx: &Context, open: &ViewportOpen<'_>, mut body: im keep_placed(ctx, viewport, pos, size.filter(|_| open.follows_size)); } let spec = open.spec; + let cancel_to = spec.hands_on_cancel.then(|| crate::runtime::egui::pass::current_viewport(ctx)); let state = shared(ctx, |shared| shared.app); let os = os_of(ctx); let (chrome, captions) = frame_of_spec(ctx, spec); @@ -64,6 +65,7 @@ pub(crate) fn show_viewport(ctx: &Context, open: &ViewportOpen<'_>, mut body: im record.resizable = Some(open.resizable); record.min_size = open.limits.0; record.moved_by_app = open.moved_by_app; + record.cancel_to = cancel_to; record.awaiting_first_paint |= spec.shown_after_first_paint && record.facts.is_none(); if record.title.as_deref() != Some(open.title) { record.title = Some(open.title.to_owned()); diff --git a/crates/ui-panels/src/docking/native.rs b/crates/ui-panels/src/docking/native.rs index caf6d5b1ef..0b54ab05dc 100644 --- a/crates/ui-panels/src/docking/native.rs +++ b/crates/ui-panels/src/docking/native.rs @@ -53,9 +53,11 @@ pub fn pixels_per_point(ctx: &Context, viewport: ViewportId) -> Option { } /// The window a group of panels with `specs` opens: a document window when any panel is a document, else a tool -/// window (a palette level, no taskbar entry). +/// window (a palette level, no taskbar entry). An Escape nothing in it takes is the Cancel of the window the dock is in. pub fn spec_for(specs: &[PanelSpec]) -> WindowSpec { - if specs.iter().any(|spec| spec.window_kind == WindowKind::Document) { WindowSpec::document() } else { WindowSpec::tool() } + let document = specs.iter().any(|spec| spec.window_kind == WindowKind::Document); + let spec = if document { WindowSpec::document() } else { WindowSpec::tool() }; + spec.hands_on_cancel(true) } /// The rect in the main window's points of `desktop`, a rect in desktop points: where a window at `desktop` would sit diff --git a/crates/ui-patterns/tests/it/docking/native.rs b/crates/ui-patterns/tests/it/docking/native.rs index 206c0b1e0d..d15c97f5a1 100644 --- a/crates/ui-patterns/tests/it/docking/native.rs +++ b/crates/ui-patterns/tests/it/docking/native.rs @@ -1606,6 +1606,101 @@ fn enter_and_escape_in_a_native_palettes_field_stay_the_fields() { } } +/// What panel 1's body holds that takes an Escape pressed in its window. +#[derive(Clone, Copy, Debug, PartialEq)] +enum Taker { + Nothing, + /// A field, which a click focuses. + Field, + /// A button whose context menu a secondary click opens. + Menu, + /// App code that reads the window's leftover Escape. + App, +} + +/// Panels of which panel 1 holds `taker`. +struct EscapePalette { + taker: Taker, + opacity: Option, +} + +impl DockPanels for EscapePalette { + fn body(&mut self, panel: &u32, ui: &mut Ui, _: &mut BodyCx<'_>) { + match self.taker { + _ if *panel != 1 => {} + Taker::Nothing => {} + Taker::Field => { + NumberField::new("Opacity", Bind::bound(&mut self.opacity)).show(ui); + } + Taker::Menu => { + let target = Button::new("Target").show(ui); + let _ = Menu::context("Target menu", &target).show(ui, |ui, _| { + MenuItem::new("Row").show(ui); + }); + } + Taker::App => { + keys::with_window_intents(ui.ctx(), false, |window| window.take(Intent::Cancel)); + } + } + } +} + +/// An Escape pressed in a native palette that holds `taker`, with the field focused or the menu open: how many of the +/// main window's Cancels two readers found that the app runs before its dock, one after the other. +fn main_window_cancels(taker: Taker) -> (usize, usize) { + let mut dock = NativeDock::on(Os::Windows, Arc::new(FakeSystem::new())); + let palette = dock.palette.expect("the palette's window opened"); + let mut panels = EscapePalette { taker, opacity: Some(50.0) }; + let mut read = (0, 0); + let mut frame = |child: Vec| { + dock.windows.frame(vec![], child, |ui| { + let cancel = |ui: &Ui| usize::from(keys::with_window_intents(ui.ctx(), false, |window| window.take(Intent::Cancel))); + read.0 += cancel(ui); + read.1 += cancel(ui); + let _ = DockArea::new("Native dock", Bind::bound(&mut dock.workspace), &dock.specs, &mut panels, DockConfig::default()).show(ui); + }) + }; + frame(vec![]); + let out = frame(vec![]); + let at = |name: &str| { + let tree = tree_of_viewport(&out, palette); + let part = tree.keys().into_iter().find(|key| tree.name_of(*key) == Some(name) && tree.role_of(*key) != Some(Role::LABEL)); + part.and_then(|key| tree.bounds_of(key)).expect("the palette shows it").center() + }; + let opened = match taker { + Taker::Field => { + let field = at("Opacity"); + vec![vec![moved(field), press(field)], vec![release(field)]] + } + Taker::Menu => { + let target = at("Target"); + vec![vec![moved(target)], vec![secondary(target, true)], vec![secondary(target, false)]] + } + Taker::Nothing | Taker::App => vec![], + }; + let escape = [vec![key_press(Key::Escape, Modifiers::NONE)], vec![key_release(Key::Escape, Modifiers::NONE)], vec![], vec![]]; + for events in opened.into_iter().chain([vec![], vec![]]).chain(escape) { + frame(events); + } + read +} + +/// An Escape that nothing in a native palette takes is the main window's Cancel, once: the app reads it where it reads +/// the main window's own, and the read takes it. +#[trial] +fn an_escape_nothing_in_a_native_palette_takes_is_the_main_windows_cancel_once() { + assert_eq!(main_window_cancels(Taker::Nothing), (1, 0), "the first read finds the Escape, the second nothing"); +} + +/// An Escape in a native palette that a focused field, an open menu or the app's own code there takes or keeps never +/// reaches the main window. +#[trial] +fn an_escape_taken_in_a_native_palette_never_reaches_the_main_window() { + for taker in [Taker::Field, Taker::Menu, Taker::App] { + assert_eq!(main_window_cancels(taker), (0, 0), "{taker:?}: the Escape stays in the palette's window"); + } +} + /// A palette float made in the app window. fn float() -> Floating { Floating::single(104, vec![1], 0, Rect::from_min_size(Pos2::new(40.0, 40.0), Vec2::new(300.0, 200.0))) diff --git a/docs/craft-framework.md b/docs/craft-framework.md index 5484774f97..7317edd253 100644 --- a/docs/craft-framework.md +++ b/docs/craft-framework.md @@ -169,7 +169,7 @@ An app with its own eframe `App` calls the hooks in `craft::ui::shell::embed`. ` | App modals | A modal the app draws with its own egui code blocks nothing outside egui by itself. While one shows, the app names its window each frame with `FrameInput::app_modal(Some(viewport))`: the app's other windows, panels floated into windows of their own among them, are then blocked as under a framework app-modal layer (their presses, keys, wheel and text are dropped, a press there brings the modal's window forward, and they refuse to close). | | Input pacing | One press per pass: extra presses in one frame wait for the next passes. A press outside an open framework menu closes the menu and goes no further. After an OS resize of the window, the held pointer is released. | -The window's keys are the ones the runtime routes to no component: an Escape or a plain Enter while nothing has the focus, neither a framework stop nor one of the app's own widgets or popups, and the arrows' nudge. The window's plain Enter also stays in egui's input. `with_window_intents` offers them to its closure while nothing has framework focus. The closure takes what it handles (`Intents::take(Intent::Cancel)`; `Intent` and the other types `Intents` reads are in `ui::runtime::keys`) and returns whether it took anything; the rest stays for later readers. A framework scope that ran earlier in the pass takes them first (an open dialog takes the Escape that closes it), so the app reads them last in its UI to see what no component took. App code that read `Key::Escape`, `Key::Tab`, F6 or F10 from egui while none of its own widgets had focus reads these intents and `FrameOutput::chrome_toggles` instead; its own focused widgets and popups keep reading Escape and the plain Enter from egui (Tab only where the widget keeps it or a popup is open), and no window intent comes of them. +The window's keys are the ones the runtime routes to no component: an Escape or a plain Enter while nothing has the focus, neither a framework stop nor one of the app's own widgets or popups, and the arrows' nudge. The window's plain Enter also stays in egui's input. `with_window_intents` offers them to its closure while nothing has framework focus. The closure takes what it handles (`Intents::take(Intent::Cancel)`; `Intent` and the other types `Intents` reads are in `ui::runtime::keys`) and returns whether it took anything; the rest stays for later readers. A framework scope that ran earlier in the pass takes them first (an open dialog takes the Escape that closes it), so the app reads them last in its UI to see what no component took. App code that read `Key::Escape`, `Key::Tab`, F6 or F10 from egui while none of its own widgets had focus reads these intents and `FrameOutput::chrome_toggles` instead; its own focused widgets and popups keep reading Escape and the plain Enter from egui (Tab only where the widget keeps it or a popup is open), and no window intent comes of them. An Escape pressed in the OS window of a floated dock panel that nothing there takes, not even a reader of that window's intents, is the Cancel of the window the dock is in, in its next pass, so the app reads it where it reads its own; a popup or a modal open over the panel keeps it, and any other OS window (an app's own viewport, a dialog or document window) keeps its Escape to itself (`WindowSpec::hands_on_cancel`). Framework dialogs the app keeps as values run on a `DialogHost`: the app calls `run(ui)` once per pass after its own UI and reads results with `take`.