From 99b97529b6a3309b95391dd33545cd5d160be600 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 3 Dec 2026 11:15:34 -0800 Subject: [PATCH] Canvas: the Space Hand ends when Space goes up while the canvas is away When Space (or Cmd+Space, Alt+Space) was let go while the canvas did not run, for example with a dialog or another tab in front, the shortcut board dropped the queued release and the momentary Hand or Zoom stayed in effect. Every tool chosen afterwards then acted as the Hand until Space was pressed and released again (#10709). The canvas now notes the keys that were down when a momentary hold started, and at its next pass ends the hold once none of them is down. A hold started with no key down, as automation does, lasts until its release. This is a temporary workaround for storytold/craft-libs#5056, which ends the hold inside the tool host. The helpers and the momentary_keys field go once the craft-libs pin includes it; the tests stay. --- crates/ui-egui/src/app.rs | 1 + crates/ui-egui/src/caches.rs | 3 + crates/ui-egui/src/canvas/tool_adapter.rs | 44 ++++++++++++- .../ui-egui/src/canvas/tool_adapter/tests.rs | 63 +++++++++++++++++++ 4 files changed, 108 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 442ddaffbf..ffee110c31 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -149,6 +149,7 @@ impl VectorcraftApp { spelling: Default::default(), text_nodes: Default::default(), momentary: None, + momentary_keys: vec![], hand: None, zoom_tool: None, selection_box: None, diff --git a/crates/ui-egui/src/caches.rs b/crates/ui-egui/src/caches.rs index 6dcec22bff..d2809d5846 100644 --- a/crates/ui-egui/src/caches.rs +++ b/crates/ui-egui/src/caches.rs @@ -81,6 +81,9 @@ pub struct CanvasCache { /// The Hand, Zoom and Rotate View tools the Space key and its chords lend the canvas, between /// passes; the tool adapter takes it out for its call and puts it back. pub momentary: Option, + /// The keys down when the held momentary command started; empty while none is held or when none started it. + /// Temporary, with `tool_adapter::end_missed_momentary` (craft-libs#5056). + pub momentary_keys: Vec, pub hand: Option, /// The selected Zoom tool keeps its press and drag rectangle between canvas passes. pub zoom_tool: Option, diff --git a/crates/ui-egui/src/canvas/tool_adapter.rs b/crates/ui-egui/src/canvas/tool_adapter.rs index 70f1080db5..3ca2456583 100644 --- a/crates/ui-egui/src/canvas/tool_adapter.rs +++ b/crates/ui-egui/src/canvas/tool_adapter.rs @@ -7,8 +7,8 @@ use craft::behavior::{CancelReason, CanvasView, Cursor, DropOperation, PointerKi use craft::keymap::{Chord, Key}; use craft::ui::canvas::ToolPath; use craft::ui::canvas::{ - Autoscroll, CanvasTool, Momentary, MomentaryShift, ToolAction, ToolClaims, ToolContext, ToolHost, ToolHostResponse, ToolKey, ToolPass, - ToolPointer, + Autoscroll, CanvasTool, Momentary, MomentaryCommands, MomentaryShift, ToolAction, ToolClaims, ToolContext, ToolHost, ToolHostResponse, ToolKey, + ToolPass, ToolPointer, }; use serde_json::json; use vectorcraft_engine::ViewInfo; @@ -615,6 +615,37 @@ impl CanvasTool for SessionTool<'_> { } } +/// Ends the held momentary command (Space's Hand, Cmd+Space's Zoom...) once none of the keys that were down when it +/// started is down, and returns the command still held. The shortcut board drops a queued release when the canvas +/// does not run on the frame the key goes up (a dialog or another tab in front). A hold that started with no key down +/// (automation) lasts until its release. +/// +/// Temporary: craft-libs#5056 ends the hold inside the tool host. Remove this function, [`note_momentary_keys`], +/// their calls and `CanvasCache::momentary_keys` once the craft-libs pin includes that change. +fn end_missed_momentary(ctx: &egui::Context, momentary: &mut Momentary, keys: &mut Vec) -> Option { + let held = momentary.held()?; + if keys.is_empty() { + return Some(held); + } + let down = craft::ui::runtime::keys::held(ctx); + if keys.iter().any(|key| down.contains(key)) { + return Some(held); + } + momentary.fired(held, craft::ui::runtime::commands::FirePhase::Release); + keys.clear(); + None +} + +/// Notes the keys down when a momentary command starts (or changes), for [`end_missed_momentary`]; `before` is the +/// command held before the tool host's pass. +fn note_momentary_keys(ctx: &egui::Context, before: Option, momentary: &Momentary, keys: &mut Vec) { + match momentary.held() { + None => keys.clear(), + Some(held) if before != Some(held) => *keys = craft::ui::runtime::keys::held(ctx), + Some(_) => {} + } +} + /// Runs the tool host on `tool` with the app's momentary set, which the app keeps across passes. A finger, once a pen /// has been seen, only navigates (palm rejection). fn host_run(app: &mut VectorcraftApp, ui: &Ui, pass: &ToolPass, tool: &mut dyn CanvasTool) -> ToolHostResponse { @@ -622,7 +653,9 @@ fn host_run(app: &mut VectorcraftApp, ui: &Ui, pass: &ToolPass, tool: &mut dyn C let mut momentary = app.canvas.momentary.take().unwrap_or_else(|| Momentary::new(options).shift(MomentaryShift::RotateView)); momentary.set_options(options); momentary.hand_selected(app.session.tool_id() == "hand"); + let before = end_missed_momentary(ui.ctx(), &mut momentary, &mut app.canvas.momentary_keys); let response = ToolHost::new().momentary(&mut momentary).navigates(crate::touch::finger_navigates_only()).run(ui, pass, tool); + note_momentary_keys(ui.ctx(), before, &momentary, &mut app.canvas.momentary_keys); app.canvas.momentary = Some(momentary); response } @@ -755,9 +788,14 @@ fn host_run_session(tool: &mut SessionTool<'_>, ui: &Ui, pass: &ToolPass) -> Too let mut momentary = app.canvas.momentary.take().unwrap_or_else(|| Momentary::new(options).shift(MomentaryShift::RotateView)); momentary.set_options(options); momentary.hand_selected(app.session.tool_id() == "hand"); + let mut keys = std::mem::take(&mut app.canvas.momentary_keys); drop(app); + let before = end_missed_momentary(ui.ctx(), &mut momentary, &mut keys); let response = ToolHost::new().momentary(&mut momentary).navigates(crate::touch::finger_navigates_only()).run(ui, pass, tool); - tool.app().canvas.momentary = Some(momentary); + note_momentary_keys(ui.ctx(), before, &momentary, &mut keys); + let mut app = tool.app(); + app.canvas.momentary = Some(momentary); + app.canvas.momentary_keys = keys; response } diff --git a/crates/ui-egui/src/canvas/tool_adapter/tests.rs b/crates/ui-egui/src/canvas/tool_adapter/tests.rs index 1ff48b983a..a9c60d1089 100644 --- a/crates/ui-egui/src/canvas/tool_adapter/tests.rs +++ b/crates/ui-egui/src/canvas/tool_adapter/tests.rs @@ -48,6 +48,69 @@ fn frame(app: &mut VectorcraftApp, ctx: &egui::Context, events: Vec out.textures_delta.clear(); } +/// A frame where the canvas does not run, as with a dialog or another tab in front. +fn frame_without_canvas(ctx: &egui::Context, events: Vec) { + let raw = + egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(800.0, 600.0))), events, ..Default::default() }; + let mut out = ctx.run_ui(raw, |_| {}); + out.textures_delta.clear(); +} + +fn space_key(pressed: bool) -> egui::Event { + egui::Event::Key { key: egui::Key::Space, physical_key: None, pressed, repeat: false, modifiers: egui::Modifiers::NONE } +} + +/// Space let go while the canvas is away for long enough that the shortcut board drops its queued release (two +/// frames) ends the momentary Hand at the canvas's next pass, and Space still held across such frames keeps it. The +/// Rectangle tool runs through the session's tool, and the Zoom tool through the framework's. +#[test] +fn space_let_go_while_the_canvas_is_away_ends_the_momentary_hand() { + for tool in ["rectangle", "zoom"] { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.run("file.new", serde_json::json!({"width": 400, "height": 300})).unwrap(); + app.select_tool(tool); + let ctx = crate::testing::ui_context(); + frame(&mut app, &ctx, vec![]); + frame(&mut app, &ctx, vec![space_key(true)]); + frame(&mut app, &ctx, vec![]); + assert!(view_tool_held(&app), "{tool}: Space down holds the Hand"); + frame_without_canvas(&ctx, vec![]); + frame_without_canvas(&ctx, vec![]); + frame(&mut app, &ctx, vec![]); + assert!(view_tool_held(&app), "{tool}: Space still down keeps the Hand across frames without the canvas"); + frame_without_canvas(&ctx, vec![space_key(false)]); + frame_without_canvas(&ctx, vec![]); + frame(&mut app, &ctx, vec![]); + assert!(!view_tool_held(&app), "{tool}: Space let go while the canvas was away ends the Hand"); + } +} + +/// A momentary Hand whose key is down at another physical position than the one its shortcut names (another keyboard +/// layout) stays held across passes while that key is down, and ends once it goes up while the canvas is away. +#[test] +fn a_momentary_hand_from_another_layout_position_lasts_while_its_key_is_down() { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.run("file.new", serde_json::json!({"width": 400, "height": 300})).unwrap(); + app.select_tool("rectangle"); + let ctx = crate::testing::ui_context(); + let key = |pressed| egui::Event::Key { + key: egui::Key::Space, + physical_key: Some(egui::Key::Backtick), + pressed, + repeat: false, + modifiers: egui::Modifiers::NONE, + }; + frame(&mut app, &ctx, vec![]); + frame(&mut app, &ctx, vec![key(true)]); + frame(&mut app, &ctx, vec![]); + frame(&mut app, &ctx, vec![]); + assert!(view_tool_held(&app), "the key still down keeps the Hand"); + frame_without_canvas(&ctx, vec![key(false)]); + frame_without_canvas(&ctx, vec![]); + frame(&mut app, &ctx, vec![]); + assert!(!view_tool_held(&app), "the key let go while the canvas was away ends the Hand"); +} + /// With the Live Paint Bucket and the pointer still, an Alt-only pass changes the cursor and back. #[test] fn bucket_modifier_only_hover_updates_the_visible_cursor() {