diff --git a/crates/ui-contract/src/runtime/focus.rs b/crates/ui-contract/src/runtime/focus.rs index 7bc26a8ef..b8bc8345f 100644 --- a/crates/ui-contract/src/runtime/focus.rs +++ b/crates/ui-contract/src/runtime/focus.rs @@ -220,6 +220,17 @@ pub(crate) fn canvas_keys(ctx: &Context, viewport: ViewportId) -> Option with_viewport(ctx, viewport, |state| state.stops.iter().find(|rec| rec.stop.keys.chrome_key).map(|rec| rec.stop.keys)) } +/// Whether `rec`, the focused stop, is a canvas control's: a non-text member of the document canvas's arena, as the +/// end hook recorded the focus (`FocusedRecord::root`). Its leftover Escape is the window's, as the canvas's own is. +pub(crate) fn is_canvas_member(ctx: &Context, rec: &StopRec) -> bool { + with(ctx, |state| { + state.record.focused == Some(rec.stop.id) + && state.record.root.is_some_and(|root| { + state.stops.iter().chain(&state.last_stops).any(|canvas| canvas.stop.keys.chrome_key && canvas.stop.root == root) + }) + }) +} + /// Whether a Tab in `viewport` has nothing to leave and nowhere to go: egui held no focus at the end of its last pass /// and it registered no focus stop there. Read by the input hook, before the begin hook rotates the registrations. pub(crate) fn tab_from_nothing(ctx: &Context, viewport: ViewportId) -> bool { diff --git a/crates/ui-contract/src/runtime/keys.rs b/crates/ui-contract/src/runtime/keys.rs index 78fd30062..19e8deca2 100644 --- a/crates/ui-contract/src/runtime/keys.rs +++ b/crates/ui-contract/src/runtime/keys.rs @@ -37,11 +37,11 @@ pub(crate) use intents::lent_taken; #[doc(hidden)] pub use intents::settle; #[doc(hidden)] -pub use intents::take_cancel; -#[doc(hidden)] pub use intents::take_submit; pub use intents::with_window_intents; -pub(crate) use intents::{Lent, context_part, context_request, give_back, lend, request_context, route_whole_reset, scope_escape, take_window_cancel}; +pub(crate) use intents::{ + Lent, context_part, context_request, give_back, lend, request_context, route_whole_reset, scope_escape, take_cancel, take_window_cancel, +}; pub(crate) use nodes::{redirect_node, register_expanded, register_node}; pub use route::focus_holds; pub(crate) use route::{activated_member, begin, layout_of, owner_at, pasted_text, queue_edit, take_shortcuts}; diff --git a/crates/ui-contract/src/runtime/keys/intents.rs b/crates/ui-contract/src/runtime/keys/intents.rs index 278cb1668..66f271c39 100644 --- a/crates/ui-contract/src/runtime/keys/intents.rs +++ b/crates/ui-contract/src/runtime/keys/intents.rs @@ -264,23 +264,26 @@ pub fn intents_of(ctx: &Context, id: Option, rtl: bool) -> Intents { } /// Offers the window's intents to `take` while nothing in the window has framework focus or the focused stop is the -/// window's document canvas (`KeyUse::chrome_key`), and writes back what it left untaken. `take` returns whether it -/// took any. `false` when another stop is focused or nothing was taken. +/// window's document canvas (`KeyUse::chrome_key`) or one of its canvas controls (`focus::is_canvas_member`), and +/// writes back what it left untaken. `take` returns whether it took any. `false` when another stop is focused or +/// nothing was taken. /// /// The window's intents are the keys the runtime routes to no component this pass: an Escape (`Intent::Cancel`) /// or a plain Enter (`Intent::Submit`) while no framework stop is focused, and the arrows' nudge; with the document -/// canvas focused, the Escape the canvas, its sessions, its tool and every scope around it left. A component or a +/// canvas or a canvas control focused, the Escape that stop, the canvas's sessions, its tool and every scope around +/// it left. A component or a /// scope that runs earlier in the pass takes them first (an open dialog takes the Escape that closes it, a canvas /// session the one that ends it), so an app that hosts the runtime reads them last in its UI to see what no /// component took. `rtl` reads horizontal moves in the window's direction. pub fn with_window_intents(ctx: &Context, rtl: bool, take: impl FnOnce(&mut Intents) -> bool) -> bool { let canvas = match focus::focused_rec(ctx) { - Some(rec) if !rec.stop.keys.chrome_key => return false, - Some(rec) => Some(rec.stop.id), + Some(rec) if rec.stop.keys.chrome_key || focus::is_canvas_member(ctx, &rec) => Some(rec.stop.id), + Some(_) => return false, None => None, }; let mut window = intents_of(ctx, None, rtl); - // The focused canvas's leftover Escape is offered as the window's; it stays in the canvas's slot until taken. + // The focused canvas's or canvas control's leftover Escape is offered as the window's; it stays in that stop's + // slot until taken. let lent_cancel = canvas .filter(|canvas| !window.items.iter().any(|(item, _)| *item == Intent::Cancel) && holds(ctx, Some(*canvas), &Intent::Cancel)) .map(|canvas| { @@ -437,11 +440,8 @@ pub fn take_submit(ctx: &Context) -> bool { take_untaken(ctx, &Intent::Submit) } -/// Takes an untaken `Cancel` (Escape) for the focused stop, without deciding what the scope does with it. An app -/// that opens a modal layer in the pass an Escape arrives in takes it with this, so the focused component (a frame -/// part of a canvas, say) does not act on it beneath the layer. -#[doc(hidden)] -pub fn take_cancel(ctx: &Context) -> bool { +/// Takes an untaken `Cancel` (Escape) for the focused stop, without deciding what the scope does with it. +pub(crate) fn take_cancel(ctx: &Context) -> bool { take_untaken(ctx, &Intent::Cancel) }