diff --git a/crates/ui-canvas/src/canvas/tool_host.rs b/crates/ui-canvas/src/canvas/tool_host.rs index 635194131a..c7ac8c2799 100644 --- a/crates/ui-canvas/src/canvas/tool_host.rs +++ b/crates/ui-canvas/src/canvas/tool_host.rs @@ -399,11 +399,34 @@ struct HostState { /// The tip the tool's last hover asked for: the tool answers again only when the pointer moves, so it stands /// while the pointer rests, until a press, a gesture or the pointer leaving drops it. tip: Option, + /// The box the running gesture's last press or drag asked for (its hud, its readout): a pass with neither, where + /// the pointer rested or something else repainted, shows it again, so the box does not blink out between moves. + gesture_box: Option, /// A press ran since the last hover: the next resting pass hovers at the pointer even when it did not move, so the /// tool answers again for where the pointer rests and the tip the press dropped comes back. rehover: bool, } +/// The rows a gesture's press or drag asked to show beside the pointer. +#[derive(Clone, Debug, Default)] +struct GestureBox { + hud: Option>, + readout: Option>, +} + +/// The gesture's box stands between its moves: a pass whose press or drag `answered` keeps what it asked for, one with +/// neither shows the last box again. The box ends with the gesture. +fn keep_gesture_box(state: &mut HostState, requests: &mut ToolRequests, answered: bool) { + if state.gesture.is_none() { + state.gesture_box = None; + } else if answered || requests.hud.is_some() || requests.readout.is_some() { + state.gesture_box = Some(GestureBox { hud: requests.hud.clone(), readout: requests.readout.clone() }); + } else if let Some(shown) = state.gesture_box.clone() { + requests.hud = shown.hud; + requests.readout = shown.readout; + } +} + fn state_id(canvas: Id) -> Id { global("craft-ui tool host", canvas) } @@ -608,6 +631,7 @@ impl<'m> ToolHost<'m> { pending_drag: None, ended_session: false, hovered: false, + answered: false, }; run.call(None, |tool, cx| tool.each_pass(cx)); run.hover_scrolls = run.pick_as(None).autoscrolls_on_hover(); @@ -622,7 +646,8 @@ impl<'m> ToolHost<'m> { run.keys(&ctx, pass.id, env.direction.is_rtl()); run.menu_key(); } - let Run { mut response, requests, momentary: momentary_tool, hovered, .. } = run; + let Run { mut response, mut requests, momentary: momentary_tool, hovered, answered, .. } = run; + keep_gesture_box(&mut state, &mut requests, answered); let before = space.map(|space| space.target()); if let Some(past) = response.past_edge { let step = frame_step(input.time - state.time.unwrap_or(input.time)); @@ -1028,6 +1053,8 @@ struct Run<'a, 't> { pending_drag: Option, /// The gesture whose release or cancel is being delivered started a drag session. ended_session: bool, + /// The tool had a press or a drag call this pass: what it asked to show beside the pointer replaces the gesture's box. + answered: bool, } impl<'a, 't: 'a> Run<'a, 't> { @@ -1197,6 +1224,7 @@ impl<'a, 't: 'a> Run<'a, 't> { let id = next_gesture_id(self.ctx); self.ending = Some(id); self.pending_drag = None; + self.answered = true; let mut action = self.call_as(who, Some(pointer), |tool, cx| tool.press(&pointer, cx)); if action == ToolAction::Ignored && who.is_none() @@ -1235,6 +1263,7 @@ impl<'a, 't: 'a> Run<'a, 't> { let Some(active) = self.state.gesture.as_mut() else { return }; active.last = pointer; active.dragged = true; + self.answered = true; self.call(Some(pointer), |tool, cx| tool.drag(&pointer, cx)); self.report(GesturePhase::Update); let who = self.state.gesture.and_then(|active| active.stand_in); diff --git a/crates/ui-canvas/tests/it/canvas/tool.rs b/crates/ui-canvas/tests/it/canvas/tool.rs index 34b3c8525c..2074e70bbe 100644 --- a/crates/ui-canvas/tests/it/canvas/tool.rs +++ b/crates/ui-canvas/tests/it/canvas/tool.rs @@ -1982,6 +1982,74 @@ fn a_drags_readout_is_not_reported_in_the_pass_its_release_ends_the_gesture() { assert!(!reported.get(), "the release pass reports none"); } +/// A tool whose drag asks for a readout while `asks` holds and for none once it is cleared. +struct Pausing { + asks: Rc>, +} + +impl CanvasTool for Pausing { + fn press(&mut self, _: &ToolPointer, _: &mut ToolContext<'_>) -> ToolAction { + ToolAction::Claimed + } + + fn drag(&mut self, _: &ToolPointer, cx: &mut ToolContext<'_>) { + if self.asks.get() { + cx.readout(vec![(craft_ui::runtime::env::Label::from("W"), "123 px".to_owned())]); + } + } + + fn release(&mut self, _: &ToolPointer, _: &mut ToolContext<'_>) {} + + fn cancel(&mut self, _: CancelReason, _: &mut ToolContext<'_>) {} + + fn paint(&self, _: &Draw<'_>, _: &ToolContext<'_>) {} + + fn command_form(&self) -> ToolPath { + ToolPath::Command("the Pause command") + } +} + +#[trial] +fn a_drags_readout_stands_in_the_passes_between_its_moves() { + let reported = Rc::new(Cell::new(false)); + let seen = Rc::clone(&reported); + let asks = Rc::new(Cell::new(true)); + let tool = RefCell::new(Pausing { asks: Rc::clone(&asks) }); + let mut fixture = Fixture::new(move |ui| { + let room = ui.available_rect_before_wrap(); + let mut reply = None; + let _ = Canvas::new("Document").document(DOCUMENT).view(CanvasView::new(room, Point::new(600.0, 450.0), 0.5)).show(ui, |ui, body| { + if let Some(pass) = tool_pass(&body.env) { + reply = Some(ToolHost::new().run(ui, &pass, &mut *tool.borrow_mut())); + } + }); + seen.set(reply.unwrap_or_default().readout.is_some()); + }); + fixture.settle(); + let centre = fixture.by_role(Role::CANVAS).into_iter().next().expect("a canvas").center(); + fixture.frame(vec![pointer::moved(centre)]); + fixture.frame(vec![pointer::press(centre)]); + for step in 1..=4 { + fixture.frame(vec![pointer::moved(centre + BY * (step as f32 / 4.0))]); + } + // A pass with no move (the pointer rests, the ants or anything else repaint) gives the tool no drag call. + for _ in 0..3 { + fixture.frame(Vec::new()); + assert!(reported.get(), "the readout stays while the pointer rests mid-drag"); + assert!(fixture.texts().iter().any(|run| run.text.contains("123 px")), "and it still paints"); + } + asks.set(false); + fixture.frame(vec![pointer::moved(centre + BY * 1.25)]); + assert!(!reported.get(), "a drag that asks for none drops it"); + fixture.frame(Vec::new()); + assert!(!reported.get(), "and it does not come back while the pointer rests"); + asks.set(true); + fixture.frame(vec![pointer::moved(centre + BY * 1.5)]); + fixture.frame(vec![pointer::release(centre + BY * 1.5)]); + fixture.frame(Vec::new()); + assert!(!reported.get(), "the readout ends with its gesture"); +} + #[trial] fn show_transformation_values_gates_the_readouts_paint_and_never_the_hud() { assert_eq!(readout_during_a_drag(false, None), (true, true), "by default a tool's readout paints and is reported");