diff --git a/crates/ui-egui/src/caches.rs b/crates/ui-egui/src/caches.rs index 06c564f4c8..71b7003d7a 100644 --- a/crates/ui-egui/src/caches.rs +++ b/crates/ui-egui/src/caches.rs @@ -80,7 +80,7 @@ pub struct CanvasCache { pub text_nodes: canvas::marks::TextNodes, /// 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, + pub momentary: Option<(egui::Id, craft::ui::canvas::Momentary)>, 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.rs b/crates/ui-egui/src/canvas.rs index 08f859b025..5bd1db18cc 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -116,6 +116,8 @@ pub(crate) fn show(app: &mut VectorcraftApp, ui: &mut Ui) { } let full = ui.available_rect_before_wrap(); if crate::menus::home_showing(app) { + // A hidden canvas cannot receive the release of its momentary shortcut. + app.canvas.momentary = None; // No type editor shows to lose the keys: the marked text stays as typed. crate::type_editing::keep_marked_text(app); home(app, ui, full); @@ -124,6 +126,7 @@ pub(crate) fn show(app: &mut VectorcraftApp, ui: &mut Ui) { // A document opened straight through the session has no view yet. app.sync_views(); let Some(win) = doc_windows::current(app).filter(|_| app.session.active().is_some()) else { + app.canvas.momentary = None; crate::type_editing::keep_marked_text(app); // No document and Show The Home Screen When No Documents Are Open off: an empty window. let env = Env::of(ui); @@ -148,6 +151,10 @@ struct Body { /// Window `win`'s document in the framework canvas, filling the rest of `ui`. True when the canvas /// background got a press this pass, which asks for a pane's window to become the current one. pub(crate) fn show_document(app: &mut VectorcraftApp, ui: &mut Ui, win: Win, kind: BodyKind) -> bool { + if kind == BodyKind::Live && app.canvas.momentary.as_ref().is_some_and(|(owner, _)| *owner != canvas_id(win)) { + // The shortcut board sends key-up to the canvas that received key-down. + app.canvas.momentary = None; + } let area = ui.available_rect_before_wrap(); let presenting = app.ui.screen_mode == 3; let scaling = app.ui_scaling(); diff --git a/crates/ui-egui/src/canvas/tests.rs b/crates/ui-egui/src/canvas/tests.rs index 41d0e93837..45b4eb4409 100644 --- a/crates/ui-egui/src/canvas/tests.rs +++ b/crates/ui-egui/src/canvas/tests.rs @@ -152,6 +152,32 @@ fn frame(app: &mut VectorcraftApp, ctx: &egui::Context, events: Vec out.textures_delta.clear(); } +/// Key-up belongs to the original canvas, even when Home or another document replaces it. +#[test] +fn releasing_space_away_from_its_canvas_restores_the_selected_tool() { + use egui::{Event, Key, Modifiers}; + let space = |pressed| Event::Key { key: Key::Space, physical_key: Some(Key::Space), pressed, repeat: false, modifiers: Modifiers::NONE }; + for next in ["app.home", "file.new"] { + let ctx = crate::testing::ui_context(); + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.run("file.new", json!({})).unwrap(); + frame(&mut app, &ctx, vec![]); + let at = app.canvas_view().unwrap().rect.center(); + frame(&mut app, &ctx, vec![Event::PointerMoved(at)]); + frame(&mut app, &ctx, vec![space(true)]); + frame(&mut app, &ctx, vec![]); + assert!(tool_adapter::view_tool_held(&app), "{next}: Space lends the Hand"); + app.run(next, json!({})).unwrap(); + frame(&mut app, &ctx, vec![]); + frame(&mut app, &ctx, vec![space(false)]); + app.ui.home = None; + app.run("tool.select", json!({"tool": "directSelection"})).unwrap(); + frame(&mut app, &ctx, vec![Event::PointerMoved(at)]); + assert!(!tool_adapter::view_tool_held(&app), "{next}: a departed canvas must not lend its Hand to the new pass"); + assert_eq!(app.session.tool_id(), "directSelection"); + } +} + /// A press between a Live Paint Bucket triple-click's clicks that the view tools take (a /// middle-button pan, a pan with Space held) counts as a press that paints nothing: the /// triple-click matches the fill the face had before the first click. diff --git a/crates/ui-egui/src/canvas/tool_adapter.rs b/crates/ui-egui/src/canvas/tool_adapter.rs index 2e33ef9cf8..927a153e11 100644 --- a/crates/ui-egui/src/canvas/tool_adapter.rs +++ b/crates/ui-egui/src/canvas/tool_adapter.rs @@ -612,11 +612,11 @@ impl CanvasTool for SessionTool<'_> { /// has been seen, only navigates (palm rejection). fn host_run(app: &mut VectorcraftApp, ui: &Ui, pass: &ToolPass, tool: &mut dyn CanvasTool) -> ToolHostResponse { let options = crate::app::view::navigation(&app.session.prefs); - let mut momentary = app.canvas.momentary.take().unwrap_or_else(|| Momentary::new(options).shift(MomentaryShift::RotateView)); + let mut momentary = app.canvas.momentary.take().map(|(_, m)| m).unwrap_or_else(|| Momentary::new(options).shift(MomentaryShift::RotateView)); momentary.set_options(options); momentary.hand_selected(app.session.tool_id() == "hand"); let response = ToolHost::new().momentary(&mut momentary).navigates(crate::touch::finger_navigates_only()).run(ui, pass, tool); - app.canvas.momentary = Some(momentary); + app.canvas.momentary = Some((pass.id, momentary)); response } @@ -734,12 +734,12 @@ fn host_run_session(tool: &mut SessionTool<'_>, ui: &Ui, pass: &ToolPass) -> Too let mut app = tool.app(); // The host borrows the tool and the app both through the cell, one call at a time. let options = crate::app::view::navigation(&app.session.prefs); - let mut momentary = app.canvas.momentary.take().unwrap_or_else(|| Momentary::new(options).shift(MomentaryShift::RotateView)); + let mut momentary = app.canvas.momentary.take().map(|(_, m)| m).unwrap_or_else(|| Momentary::new(options).shift(MomentaryShift::RotateView)); momentary.set_options(options); momentary.hand_selected(app.session.tool_id() == "hand"); drop(app); let response = ToolHost::new().momentary(&mut momentary).navigates(crate::touch::finger_navigates_only()).run(ui, pass, tool); - tool.app().canvas.momentary = Some(momentary); + tool.app().canvas.momentary = Some((pass.id, momentary)); response } @@ -776,7 +776,7 @@ pub(crate) fn in_gesture(app: &VectorcraftApp, ctx: &egui::Context) -> bool { /// Whether a momentary view tool (Space, Cmd+Space, ...) is in effect. pub(crate) fn view_tool_held(app: &VectorcraftApp) -> bool { - app.canvas.momentary.as_ref().is_some_and(|m| m.held().is_some()) + app.canvas.momentary.as_ref().is_some_and(|(_, m)| m.held().is_some()) } /// Send a pointer event to the active tool and act on UI requests (dialogs, tool switches).