From c1cb2df8ebad4b2bbdcfa53acd6f300c8df66c1f Mon Sep 17 00:00:00 2001 From: aiongg Date: Tue, 17 Nov 2026 03:05:28 +0000 Subject: [PATCH] Canvas Space under a dialog: Space over the canvas a dialog keeps open fires the canvas's momentary command and types nothing while a field in the dialog has focus (no hold, no typing rule for the canvas's press) --- crates/ui-canvas/tests/it/canvas/tool.rs | 42 +++++++++++++++++++ .../ui-contract/src/runtime/commands/board.rs | 2 +- crates/ui-contract/src/runtime/keys.rs | 6 ++- crates/ui-contract/src/runtime/keys/input.rs | 23 ++++++---- crates/ui-contract/src/runtime/keys/route.rs | 3 +- 5 files changed, 63 insertions(+), 13 deletions(-) diff --git a/crates/ui-canvas/tests/it/canvas/tool.rs b/crates/ui-canvas/tests/it/canvas/tool.rs index e593a9b84..7427a038c 100644 --- a/crates/ui-canvas/tests/it/canvas/tool.rs +++ b/crates/ui-canvas/tests/it/canvas/tool.rs @@ -19,6 +19,7 @@ use craft_ui::runtime::commands::{CommandGate, CommandSet as _}; use craft_ui::runtime::frame; use craft_ui::runtime::windows::WindowHost; use craft_ui::style::DataColor; +use craft_ui::text::NumberField; use craft_ui::testing::input::key_press; use craft_ui::testing::layout::in_region; use craft_ui::testing::{Fixture, ToolFixture, pointer, trial}; @@ -1145,6 +1146,47 @@ fn a_view_only_tool_drags_under_a_modal_that_keeps_the_canvas() { assert_eq!(tool.borrow().count(Call::Press), 0, "a drawing tool's press stays blocked: {:?}", tool.borrow().calls); } +/// Space over the canvas a dialog keeps open is the canvas's while a field in the dialog has focus (production pans +/// under a dialog whatever has focus): its momentary Hand pans the press held over it, and the selected tool sees none. +#[trial] +fn space_over_a_kept_canvas_pans_while_a_field_in_the_dialog_has_focus() { + let log = Log::default(); + let main = RefCell::new(Probe::new("main", &log)); + let (view, rect) = (Rc::new(Cell::new(None::)), Rc::new(Cell::new(Rect::NOTHING))); + let (shown_view, shown_rect) = (Rc::clone(&view), Rc::clone(&rect)); + let mut set = Momentary::new(NavigationOptions::default()); + let mut amount = Some(5.0); + let mut fixture = Fixture::new(move |ui| { + let room = ui.available_rect_before_wrap(); + let current = shown_view.get().unwrap_or_else(|| CanvasView::new(room, Point::new(600.0, 450.0), 4.0)); + let mut reply = None; + let shown = Canvas::new("Document").document(DOCUMENT).view(current).show_at(ui, room, |ui, body| { + if let Some(pass) = tool_pass(&body.env) { + reply = Some(ToolHost::new().momentary(&mut set).run(ui, &pass, &mut *main.borrow_mut())); + } + }); + shown_rect.set(room); + let id = shown.output.response.id; + let mut open = true; + Dialog::new("Keeps the canvas").opened(Bind::bound(&mut open)).show(ui, |ui, cx| { + cx.keep_view_open(id); + NumberField::new("Amount", Bind::bound(&mut amount)).show(ui); + }); + let reply = reply.unwrap_or_default(); + shown_view.set(Some(reply.view.or(shown.output.view).unwrap_or(current))); + }); + fixture.settle(); + assert!(fixture.focused().is_some(), "the dialog's field has focus"); + // Near the canvas's corner, clear of the centred dialog. + let at = rect.get().min + Vec2::splat(40.0); + let before = view.get().expect("the canvas ran").center; + fixture.frame(vec![pointer::moved(at)]).frame(vec![key_press(Key::Space, Modifiers::NONE)]); + fixture.frame(vec![pointer::press(at)]).frame(vec![pointer::moved(at + BY)]).frame(vec![pointer::moved(at + BY * 2.0)]); + fixture.frame(vec![pointer::release(at + BY * 2.0)]); + assert_ne!(view.get().expect("the canvas ran").center, before, "Space's Hand pans under the dialog"); + assert!(!log.borrow().iter().any(|call| call == "main press"), "the selected tool sees no press: {:?}", log.borrow()); +} + #[trial] fn matched_smart_lines_are_drawn_and_reported() { let shapes = |smart: bool, color: Option| { diff --git a/crates/ui-contract/src/runtime/commands/board.rs b/crates/ui-contract/src/runtime/commands/board.rs index a99c6621d..8dfa847d2 100644 --- a/crates/ui-contract/src/runtime/commands/board.rs +++ b/crates/ui-contract/src/runtime/commands/board.rs @@ -1202,7 +1202,7 @@ pub(crate) fn begin(ctx: &Context) { } } _ => { - let Some(hit) = keys.press(&shortcuts.layout, &shortcut.input, shortcuts.typing, Some((&mut matcher, now))) else { + let Some(hit) = keys.press(&shortcuts.layout, &shortcut.input, shortcuts.typing && !shortcut.canvas, Some((&mut matcher, now))) else { continue; }; let (app, instance) = with(ctx, |board| board.target_of(ctx, &hit.command, &board.here)); diff --git a/crates/ui-contract/src/runtime/keys.rs b/crates/ui-contract/src/runtime/keys.rs index 19e8deca2..27f4b4f2a 100644 --- a/crates/ui-contract/src/runtime/keys.rs +++ b/crates/ui-contract/src/runtime/keys.rs @@ -76,8 +76,8 @@ struct Press { release: Option, /// The place in `Batch::texts` of the text the press typed. text: Option, - /// The Space of the canvas a dialog keeps open (`runtime::sampler`): no component's routing reads it, and only the - /// held rule decides whether its shortcut runs. + /// The Space of the canvas a dialog keeps open (`runtime::sampler`): the canvas's press, which no component's + /// routing reads, no focused stop holds and no field types. canvas: bool, } @@ -89,6 +89,8 @@ pub(crate) struct Shortcut { pub(crate) phase: FirePhase, /// The index of the press in `Batch::presses`. press: Option, + /// The press is the canvas's Space ([`Press::canvas`]): it fires its command whatever the focused stop types. + pub(crate) canvas: bool, } /// What the intent routing did with one press. diff --git a/crates/ui-contract/src/runtime/keys/input.rs b/crates/ui-contract/src/runtime/keys/input.rs index 22a74a8ae..92e006689 100644 --- a/crates/ui-contract/src/runtime/keys/input.rs +++ b/crates/ui-contract/src/runtime/keys/input.rs @@ -68,8 +68,13 @@ impl Strip<'_> { let pushed = batch.presses.len(); let claims = batch.claimed.len(); let kept = self.press(batch, key, &input, repeat); - batch.last_press = - Some(LastPress { input, pressed: (batch.presses.len() > pushed).then_some(pushed), claimed: batch.claimed.len() > claims }); + // The canvas's Space keeps its text from every field, as a claim does. + let canvas = batch.presses.get(pushed).is_some_and(|press| press.canvas); + batch.last_press = Some(LastPress { + input, + pressed: (batch.presses.len() > pushed).then_some(pushed), + claimed: batch.claimed.len() > claims || canvas, + }); if kept { if let Some(press) = batch.presses.get_mut(pushed) { press.in_egui = Some(batch.egui_presses); @@ -100,7 +105,7 @@ impl Strip<'_> { if key == Some(Key::Enter) { batch.enter_down = Some(false); } - batch.shortcuts.push(Shortcut { input, phase: FirePhase::Release, press: None }); + batch.shortcuts.push(Shortcut { input, phase: FirePhase::Release, press: None, canvas: false }); true } KeyEvent::Text(text) => { @@ -147,7 +152,7 @@ impl Strip<'_> { // A component that reads every chord gets the event as the chord's press, the way the key made it. self.press(batch, key, &input, false); } else if !sent { - batch.shortcuts.push(Shortcut { input, phase: FirePhase::Press, press: None }); + batch.shortcuts.push(Shortcut { input, phase: FirePhase::Press, press: None, canvas: false }); } // The event stands for the chord, so the shortcut path is its only reader: egui does not copy or paste // on top of the command. @@ -251,12 +256,12 @@ impl Strip<'_> { false } PressUse::CanvasSpace => { - // The canvas has Space (`runtime::sampler`): only the shortcut dispatcher reads it, so the dialog's - // focused control does not activate. Its shortcut follows the held rule: a space a text field types is - // the field's. + // The canvas has Space while the pointer is over it (`runtime::sampler`): only the shortcut dispatcher reads + // it, so the dialog's focused control does not activate, and a focused field neither types its space nor + // keeps its momentary command from firing (production pans under a dialog whatever has focus). batch.presses.push(Press { key, mods, repeat, in_egui: None, release: None, text: None, canvas: true }); if !repeat { - batch.shortcuts.push(Shortcut { input: *input, phase: FirePhase::Press, press: Some(batch.presses.len() - 1) }); + batch.shortcuts.push(Shortcut { input: *input, phase: FirePhase::Press, press: Some(batch.presses.len() - 1), canvas: true }); } false } @@ -295,7 +300,7 @@ impl Strip<'_> { PressUse::Press => { batch.presses.push(Press { key, mods, repeat, in_egui: None, release: None, text: None, canvas: false }); if !repeat { - batch.shortcuts.push(Shortcut { input: *input, phase: FirePhase::Press, press: Some(batch.presses.len() - 1) }); + batch.shortcuts.push(Shortcut { input: *input, phase: FirePhase::Press, press: Some(batch.presses.len() - 1), canvas: false }); } true } diff --git a/crates/ui-contract/src/runtime/keys/route.rs b/crates/ui-contract/src/runtime/keys/route.rs index 845a736df..f13ceab29 100644 --- a/crates/ui-contract/src/runtime/keys/route.rs +++ b/crates/ui-contract/src/runtime/keys/route.rs @@ -200,7 +200,8 @@ fn owned_shortcuts(ctx: &Context, target: Option<&StopRec>, batch: &Batch, consu .presses .iter() .zip(consumed) - .map(|(press, taken)| holds_press(owner, target, press, *taken == Taken::Yes, press.text.is_some_and(|at| taken_texts[at]))) + // The canvas's Space is never the focused stop's. + .map(|(press, taken)| !press.canvas && holds_press(owner, target, press, *taken == Taken::Yes, press.text.is_some_and(|at| taken_texts[at]))) .collect(); // A shortcut without a press of its own (a release, or the clipboard event that stands for ⌘C) follows the owner by // its modifiers, as its press would.