diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index df93dc6bac..4729b05ea4 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -148,6 +148,7 @@ impl VectorcraftApp { plugin_keys: Default::default(), placed_keys: Default::default(), view_moved: None, + hover_cursor: None, hover_object: None, grab: None, carry_target: None, diff --git a/crates/ui-egui/src/caches.rs b/crates/ui-egui/src/caches.rs index 6907b7ce17..1c5aa8324f 100644 --- a/crates/ui-egui/src/caches.rs +++ b/crates/ui-egui/src/caches.rs @@ -101,6 +101,9 @@ pub struct CanvasCache { /// The view the canvas last drew and when it last changed (egui's input time): while it moves, /// the canvas doesn't hit-test the art under the pointer ([`canvas::hover_wait`], #2452). pub view_moved: Option<(canvas::ViewKey, f64)>, + /// The cursor the active tool last gave over the canvas, with the tool and the modifier keys + /// it was for: kept while the view moves. + pub hover_cursor: Option<(&'static str, vectorcraft_tools::Mods, vectorcraft_tools::Cursor)>, /// The object the Eyedropper's click at the pointer would act on, which Object Highlighting /// outlines, with what it was found for: finding it again, which can sample a color, waits /// until one of those changes. diff --git a/crates/ui-egui/src/canvas/tests.rs b/crates/ui-egui/src/canvas/tests.rs index 3f1eddb392..2ed90409d7 100644 --- a/crates/ui-egui/src/canvas/tests.rs +++ b/crates/ui-egui/src/canvas/tests.rs @@ -6700,3 +6700,47 @@ fn a_move_stops_snapping_to_guides_hidden_during_it() { assert!((centre - xf.to_doc(near).x).abs() < 1e-4 || snaps, "{hide:?}: unsnapped, it follows the pointer: {centre}"); } } + +/// While the wheel pans the view under a resting pointer, the canvas keeps the tool's last cursor instead of asking the +/// tool for it in every frame, which hit-tests the art under the pointer each time (#2452). Once the view has been +/// still for `HOVER_STILL_SECS`, the tool is asked again. +#[test] +fn a_wheel_pan_under_a_resting_pointer_keeps_the_tool_cursor() { + let asks = || super::tool_adapter::CURSOR_ASKS.with(std::cell::Cell::get); + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); + for i in 0..20 { + let x = f64::from(i) * 15.0; + app.session.execute("shape.rectangle", &json!({"x": x, "y": 20, "width": 10, "height": 10})).unwrap(); + } + app.select_tool("selection"); + let ctx = crate::testing::ui_context(); + for _ in 0..3 { + app_step(&mut app, &ctx, vec![]); + } + let at = app.canvas_view().unwrap().rect.center(); + // The pointer comes in and rests, and the view is still: the tool gives its cursor. + for _ in 0..12 { + app_step(&mut app, &ctx, vec![egui::Event::PointerMoved(at)]); + } + assert!(asks() > 0, "the tool gives the cursor over a still view"); + let wheel = |dy: f32| egui::Event::MouseWheel { + unit: egui::MouseWheelUnit::Line, + delta: vec2(0.0, dy), + phase: egui::TouchPhase::Move, + modifiers: egui::Modifiers::default(), + source: egui::MouseWheelSource::Unknown, + }; + let before = (asks(), app.canvas_view().unwrap().center); + for i in 0..10 { + app_step(&mut app, &ctx, vec![wheel(if i % 2 == 0 { -2.0 } else { 3.0 })]); + } + assert_ne!(app.canvas_view().unwrap().center, before.1, "the wheel pans the view"); + assert_eq!(asks() - before.0, 0, "no frame of the pan asks the tool for its cursor"); + // Still for longer than HOVER_STILL_SECS (egui steps 1/60 s per frame here): the tool is asked again. + let before = asks(); + for _ in 0..20 { + app_step(&mut app, &ctx, vec![egui::Event::PointerMoved(at)]); + } + assert!(asks() > before, "once the view is still, the tool gives the cursor again"); +} diff --git a/crates/ui-egui/src/canvas/tool_adapter.rs b/crates/ui-egui/src/canvas/tool_adapter.rs index af12608aa5..cc62cc3fe6 100644 --- a/crates/ui-egui/src/canvas/tool_adapter.rs +++ b/crates/ui-egui/src/canvas/tool_adapter.rs @@ -18,6 +18,12 @@ use vectorcraft_tools::{Mods, PenPose, PointerEvent, PointerKind, ToolKey as Eng use super::{Ui, VectorcraftApp}; use crate::cursors::tool_cursor; +#[cfg(test)] +thread_local! { + /// How many times [`SessionTool`] asked the session for the tool's cursor on this thread. + pub(crate) static CURSOR_ASKS: Cell = const { Cell::new(0) }; +} + /// The tools that change only the view: they neither autoscroll nor take the document's presses under a modal. const VIEW_TOOLS: [&str; 3] = ["hand", "zoom", "rotateView"]; @@ -548,10 +554,31 @@ impl CanvasTool for SessionTool<'_> { // The document's overlays are shown by `overlays::show` after the host, in one list. } + /// While the view moves under the pointer, the cursor the tool last gave for the same tool and keys stays: asking + /// the tool again would hit-test the art under the pointer in every frame of a pan or zoom (#2452). fn cursor(&self, cx: &ToolContext<'_>) -> Cursor { let Some(space) = cx.space() else { return tool_cursor(vectorcraft_tools::Cursor::Arrow) }; let at = cx.pointer.map_or(Point::ZERO, |p| space.view.to_doc(p.pos)); - let c = self.app().session.cursor(at, tool_mods(cx.mods, cx.held(Key::Space)), self.view); + let mods = tool_mods(cx.mods, cx.held(Key::Space)); + let mut app = self.app(); + let wait = + super::cursor::hover_wait(&mut app.canvas.view_moved, super::ViewKey::of(&space.view), craft::ui::runtime::frame::clock(&self.ctx)); + let id = app.session.tool_id(); + let kept = app.canvas.hover_cursor.filter(|&(was, with, _)| wait.is_some() && (was, with) == (id, mods)).map(|(.., c)| c); + let c = match kept { + Some(c) => c, + None => { + #[cfg(test)] + CURSOR_ASKS.with(|n| n.set(n.get() + 1)); + let c = app.session.cursor(at, mods, self.view); + app.canvas.hover_cursor = Some((id, mods, c)); + c + } + }; + // Once the view has been still long enough, the next pass asks the tool again. + if let Some(secs) = wait { + self.ctx.request_repaint_after_secs(secs as f32); + } // PLAN: assumed the engine's own precise step is dropped (canvas-w25); the canvas's Caps Lock/preference state arrives as `cx.precise()`. tool_cursor(if cx.precise() { c.precise() } else { c }) }