diff --git a/crates/ui-canvas/src/canvas/tool_host.rs b/crates/ui-canvas/src/canvas/tool_host.rs index 83cbdf6db..f957401ff 100644 --- a/crates/ui-canvas/src/canvas/tool_host.rs +++ b/crates/ui-canvas/src/canvas/tool_host.rs @@ -1322,5 +1322,10 @@ impl<'a, 't: 'a> Run<'a, 't> { self.response.took_key = Some(kept); } } + // An Escape the focused canvas's sessions and tool were offered and left is the window's, as it is with + // nothing focused. + if !self.cancelled { + keys::leave_to_window(ctx, canvas, |intent| *intent == Intent::Cancel); + } } } diff --git a/crates/ui-canvas/tests/it/canvas/tool.rs b/crates/ui-canvas/tests/it/canvas/tool.rs index 188fa0933..34dc6ccd1 100644 --- a/crates/ui-canvas/tests/it/canvas/tool.rs +++ b/crates/ui-canvas/tests/it/canvas/tool.rs @@ -866,7 +866,13 @@ fn a_held_space_pans_over_a_canvas_control_and_leaves_it_unchanged() { let corner = fixture.by_label("Crop corner, top left").expect("the crop shows its corners").rect.center(); let before = view.get().expect("the canvas ran").center; let space = craft_keymap::Key::Space; - for input in [ToolInput::hold(space), ToolInput::down(PointerButton::Primary, corner), ToolInput::moved(corner + BY), ToolInput::up(corner + BY), ToolInput::let_go(space)] { + for input in [ + ToolInput::hold(space), + ToolInput::down(PointerButton::Primary, corner), + ToolInput::moved(corner + BY), + ToolInput::up(corner + BY), + ToolInput::let_go(space), + ] { ToolHost::inject(fixture.ctx(), canvas.get(), input); } fixture.run_injected(); @@ -1161,3 +1167,39 @@ fn a_drag_past_the_canvas_edge_scrolls_the_view_and_a_turned_view_scrolls_on_the assert_eq!(tool.borrow().count(Call::Press), 1, "{:?}", tool.borrow().calls); } } + +/// 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>) { + let tool = RefCell::new(tool); + let cancels = Rc::new(Cell::new(0)); + let seen = Rc::clone(&cancels); + let mut fixture = Fixture::new(move |ui| { + let room = ui.available_rect_before_wrap(); + let shown = Canvas::new("Document").document(DOCUMENT).view(CanvasView::new(room, Point::new(600.0, 450.0), 1.0)).show(ui, |_, _| {}); + let _ = ToolHost::new().run(ui, &shown.output, &mut *tool.borrow_mut()); + craft_ui::runtime::keys::with_window_intents(ui.ctx(), false, |window| { + let took = window.take(craft_behavior::Intent::Cancel); + seen.set(seen.get() + u32::from(took)); + took + }); + }); + fixture.settle(); + (fixture, cancels) +} + +/// On a canvas focused by a click, an Escape the tool does not take reaches the window's intents, and one the tool +/// takes does not. +#[trial] +fn a_focused_canvas_leaves_the_cancel_no_tool_took_to_the_window() { + for takes in [false, true] { + let (mut fixture, cancels) = + if takes { window_reading_canvas(Recorder::default()) } else { window_reading_canvas(Probe::new("main", &Log::default())) }; + let canvas = fixture.by_role(Role::CANVAS).remove(0); + fixture.click(&canvas).settle(); + cancels.set(0); + fixture.frame(vec![key_press(Key::Escape, Modifiers::NONE)]).settle(); + let want = if takes { 0 } else { 1 }; + assert_eq!(cancels.get(), want, "the window sees the Escape only when the tool leaves it (tool takes it: {takes})"); + } +} diff --git a/crates/ui-contract/src/runtime/keys.rs b/crates/ui-contract/src/runtime/keys.rs index dcd6bacf9..68931f9f9 100644 --- a/crates/ui-contract/src/runtime/keys.rs +++ b/crates/ui-contract/src/runtime/keys.rs @@ -32,6 +32,8 @@ pub(crate) use input::{input_hook, queue_nav, take_menu_keys, take_nav}; pub use intents::Intents; #[doc(hidden)] pub use intents::intents_of; +#[doc(hidden)] +pub use intents::leave_to_window; #[cfg(feature = "testing")] pub(crate) use intents::lent_taken; #[doc(hidden)] diff --git a/crates/ui-contract/src/runtime/keys/intents.rs b/crates/ui-contract/src/runtime/keys/intents.rs index 704aba64a..7a2df5c0b 100644 --- a/crates/ui-contract/src/runtime/keys/intents.rs +++ b/crates/ui-contract/src/runtime/keys/intents.rs @@ -263,16 +263,18 @@ pub fn intents_of(ctx: &Context, id: Option, rtl: bool) -> Intents { } } -/// Offers the window's unfocused intents to `take` while nothing in the window has framework focus, and writes back -/// what it left untaken. `take` returns whether it took any. `false` when something is focused or nothing was taken. +/// 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. /// /// 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. A component or a -/// scope that runs earlier in the pass takes them first (an open dialog takes the Escape that closes 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. +/// or a plain Enter (`Intent::Submit`) while no framework stop is focused, and the arrows' nudge; with the document +/// canvas focused, the Escape its sessions and its tool left (`leave_to_window`). 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 { - if focus::focused_rec(ctx).is_some() { + if focus::focused_rec(ctx).is_some_and(|rec| !rec.stop.keys.chrome_key) { return false; } let mut window = intents_of(ctx, None, rtl); @@ -283,6 +285,30 @@ pub fn with_window_intents(ctx: &Context, rtl: bool, take: impl FnOnce(&mut Inte taken } +/// Moves the intents `which` picks that the focused document canvas `from` left untaken this pass to the window's, +/// where `with_window_intents` reads them. The canvas's tool host calls it once the canvas's session members and its +/// tool had their chance. Nothing moves while `from` is not the focused stop. +#[doc(hidden)] +pub fn leave_to_window(ctx: &Context, from: Id, which: impl Fn(&Intent) -> bool) { + if crate::runtime::measure::active(ctx) || !focus::focused_rec(ctx).is_some_and(|rec| rec.stop.id == from && rec.stop.keys.chrome_key) { + return; + } + current(ctx, |stage| { + let Some(routed) = stage.routed.get_mut(&Some(from)) else { return }; + let mut left = Vec::new(); + routed.items.retain(|item| { + let moves = which(&item.intent); + if moves { + left.push(item.clone()); + } + !moves + }); + if !left.is_empty() { + stage.routed.entry(None).or_default().items.extend(left); + } + }); +} + /// The keys an arena's session control borrowed this pass (`KeyUse::session`), with the ids they were routed to. #[derive(Debug, Default)] pub(crate) struct Lent {