From cbbfa814d4b3a54c758ebe80dc7d1cabd8feba14 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 27 Nov 2026 20:54:05 +0000 Subject: [PATCH] keys: a focused canvas's untaken Enter reaches the window's intents, as its Escape does Co-Authored-By: Claude Opus 5.5 --- crates/ui-canvas/tests/it/canvas/tool.rs | 26 ++++++++++++--- .../ui-contract/src/runtime/keys/intents.rs | 33 ++++++++++++------- 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/crates/ui-canvas/tests/it/canvas/tool.rs b/crates/ui-canvas/tests/it/canvas/tool.rs index 786a0011d9..3432eacf4b 100644 --- a/crates/ui-canvas/tests/it/canvas/tool.rs +++ b/crates/ui-canvas/tests/it/canvas/tool.rs @@ -1923,8 +1923,8 @@ fn a_drag_held_in_the_band_inside_the_canvas_edge_scrolls_the_view() { } /// A canvas whose app reads the window's intents after the tool host, last in its UI: returns the fixture and how many -/// Cancels the window took. -fn window_reading_canvas(tool: impl CanvasTool + 'static) -> (Fixture, Rc>) { +/// of `intent` the window took. +fn window_reading_canvas(tool: impl CanvasTool + 'static, intent: craft_behavior::Intent) -> (Fixture, Rc>) { let tool = RefCell::new(tool); let cancels = Rc::new(Cell::new(0)); let seen = Rc::clone(&cancels); @@ -1936,7 +1936,7 @@ fn window_reading_canvas(tool: impl CanvasTool + 'static) -> (Fixture, Rc (Fixture, Rc>>); diff --git a/crates/ui-contract/src/runtime/keys/intents.rs b/crates/ui-contract/src/runtime/keys/intents.rs index d3ca4f9f9f..3accce4d51 100644 --- a/crates/ui-contract/src/runtime/keys/intents.rs +++ b/crates/ui-contract/src/runtime/keys/intents.rs @@ -317,7 +317,8 @@ pub fn intents_of(ctx: &Context, id: Option, rtl: bool) -> Intents { /// /// 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 a framework -/// stop focused, its unused Escape, unless it is a text field. A component or a +/// stop focused, its unused Escape, unless it is a text field, and, with a canvas or a control in it focused, its unused +/// Enter. 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. @@ -339,19 +340,27 @@ fn offer_window_intents(ctx: &Context, rtl: bool, take: impl FnOnce(&mut Intents // palette; the document's editor and a canvas control lend it as any other control does. let focused = focus::focused_rec(ctx) .filter(|rec| rec.stop.keys.owner != OwnerClass::Text || rec.stop.keys.is_document_editor() || focus::is_canvas_member(ctx, rec)) - .map(|rec| rec.stop.id); + .map(|rec| (rec.stop.id, rec.stop.keys.chrome_key || focus::is_canvas_member(ctx, &rec))); let mut window = intents_of(ctx, None, rtl); - // The focused stop's leftover Escape stays in its slot until taken. - let lent_cancel = - focused.filter(|id| !window.items.iter().any(|(item, _)| *item == Intent::Cancel) && holds(ctx, Some(*id), &Intent::Cancel)).map(|id| { - window.items.push((Intent::Cancel, false)); - (id, window.items.len() - 1) - }); + // The focused stop's leftover Escape, and a focused canvas's leftover Enter, stay in its slot until taken: a canvas + // routes Enter to its tool and leaves the rest to the scopes around it, as it does Escape. + let mut lent = Vec::new(); + if let Some((id, canvas)) = focused { + for intent in [Intent::Cancel, Intent::Submit] { + if (intent == Intent::Cancel || canvas) && !window.items.iter().any(|(item, _)| *item == intent) && holds(ctx, Some(id), &intent) { + window.items.push((intent.clone(), false)); + lent.push((intent, window.items.len() - 1)); + } + } + } let taken = take(&mut window); - if let Some((id, at)) = lent_cancel { - let (_, cancel_taken) = window.items.remove(at); - if cancel_taken && !crate::runtime::measure::active(ctx) { - current(ctx, |stage| take_from(stage, Some(id), &Intent::Cancel)); + for (intent, at) in lent.into_iter().rev() { + let (_, lent_taken) = window.items.remove(at); + if lent_taken + && !crate::runtime::measure::active(ctx) + && let Some((id, _)) = focused + { + current(ctx, |stage| take_from(stage, Some(id), &intent)); } } if taken {