ui-canvas: ToolHost::navigates: a press the host says navigates (a finger once a pen has been seen) runs the momentary Hand, latched for its gesture

This commit is contained in:
bflatastic
2026-11-23 19:35:36 +07:00
parent 95e4ec879e
commit f8714b2374
3 changed files with 52 additions and 10 deletions
+11 -4
View File
@@ -155,17 +155,24 @@ impl Momentary {
/// the Hand into Zoom In, and Alt turns either into Zoom Out. While `in_gesture` (a drag runs), the tool of the
/// pass before keeps the drag, whatever the keys do.
pub fn tool(&mut self, mods: Mods, in_gesture: bool) -> Option<MomentaryTool> {
self.tool_holding(None, mods, in_gesture)
self.tool_holding(None, false, mods, in_gesture)
}
/// [`tool`](Self::tool) with `injected`, the command that the keys injected input holds with no key event
/// ([`ToolInput::hold`](super::ToolInput::hold)) run through the keymap: it counts before the command whose window
/// key is down, as the real key would.
pub(crate) fn tool_holding(&mut self, injected: Option<MomentaryCommands>, mods: Mods, in_gesture: bool) -> Option<MomentaryTool> {
/// key is down, as the real key would. With `navigates` ([`ToolHost::navigates`](super::ToolHost::navigates)) the
/// Hand is in effect whatever the keys hold.
pub(crate) fn tool_holding(
&mut self,
injected: Option<MomentaryCommands>,
navigates: bool,
mods: Mods,
in_gesture: bool,
) -> Option<MomentaryTool> {
if in_gesture {
return self.latched;
}
let tool = self.pick(injected.or(self.held), mods);
let tool = if navigates { Some(MomentaryTool::Hand) } else { self.pick(injected.or(self.held), mods) };
self.latched = tool;
tool
}
+13 -2
View File
@@ -460,6 +460,7 @@ pub fn tool_pass(env: &EnvView<'_>) -> Option<ToolPass> {
pub struct ToolHost<'m> {
disabled: bool,
momentary: Option<&'m mut Momentary>,
navigates: bool,
command_stand_in: Option<&'m mut (dyn CanvasTool + 'm)>,
alt_stand_in: Option<&'m mut (dyn CanvasTool + 'm)>,
smart_guide_color: Option<DataColor>,
@@ -490,6 +491,16 @@ impl<'m> ToolHost<'m> {
self
}
/// Whether the pointer of this pass navigates instead of using the tool: a press then runs the momentary Hand of
/// [`momentary`](Self::momentary), latched for its gesture, whatever tool is selected or key is held. The host
/// says so for the input its platform reports, such as a finger once a pen has been seen (palm rejection);
/// without a momentary set it changes nothing.
#[must_use]
pub fn navigates(mut self, navigates: bool) -> Self {
self.navigates = navigates;
self
}
/// The tool a press runs when `key` is held at the press and the selected tool does not take it, latched for the
/// gesture. Order: `Momentary` first, then Command, then Alt.
#[must_use]
@@ -517,7 +528,7 @@ impl<'m> ToolHost<'m> {
/// measuring run, so the host and what the app does with its response never run in one.
pub fn run(self, ui: &Ui, pass: &ToolPass, tool: &mut dyn CanvasTool) -> ToolHostResponse {
let ctx = ui.ctx().clone();
let ToolHost { disabled, mut momentary, mut command_stand_in, mut alt_stand_in, smart_guide_color } = self;
let ToolHost { disabled, mut momentary, navigates, mut command_stand_in, mut alt_stand_in, smart_guide_color } = self;
let env = Env::of(ui);
let key = state_id(pass.id);
let mut state = with_session(&ctx, key, |slot: &mut HostState| slot.clone());
@@ -539,7 +550,7 @@ impl<'m> ToolHost<'m> {
momentary.fired(command, event.phase);
}
let injected = held_momentary(&ctx, pass.id, env.view().command_chain(), &holds, input.mods);
momentary.tool_holding(injected, input.mods, state.gesture.is_some())
momentary.tool_holding(injected, navigates, input.mods, state.gesture.is_some())
}
None => {
fired.for_each(drop);
+28 -4
View File
@@ -1184,12 +1184,14 @@ fn momentary_canvas(main: Probe) -> CanvasRig<Id> {
/// `momentary_canvas` with `body` as the canvas's body.
fn momentary_canvas_with(main: Probe, body: impl FnMut(&mut egui::Ui, &mut craft_ui::contract::BodyCx<'_>) + 'static) -> CanvasRig<Id> {
momentary_canvas_under(NavigationOptions::default(), main, body)
momentary_canvas_under(NavigationOptions::default(), Rc::default(), main, body)
}
/// `momentary_canvas_with` under the navigation choices `options`.
/// `momentary_canvas_with` under the navigation choices `options`, its host told in each pass whether the pointer
/// `navigates`.
fn momentary_canvas_under(
options: NavigationOptions,
navigates: Rc<Cell<bool>>,
main: Probe,
mut body: impl FnMut(&mut egui::Ui, &mut craft_ui::contract::BodyCx<'_>) + 'static,
) -> CanvasRig<Id> {
@@ -1204,7 +1206,7 @@ fn momentary_canvas_under(
let shown = Canvas::new("Document").document(DOCUMENT).view(current).show(ui, |ui, cx| {
body(ui, cx);
if let Some(pass) = tool_pass(&cx.env) {
reply = Some(ToolHost::new().momentary(&mut set).run(ui, &pass, &mut *main.borrow_mut()));
reply = Some(ToolHost::new().momentary(&mut set).navigates(navigates.get()).run(ui, &pass, &mut *main.borrow_mut()));
}
});
shown_canvas.set(shown.output.response.id);
@@ -1240,6 +1242,28 @@ fn a_queued_space_held_over_a_press_pans_with_the_hand_and_the_press_after_its_r
assert!(!calls.iter().any(|call| call == "main drag"), "the Hand's drag never reaches the selected tool: {calls:?}");
}
/// Palm rejection: a press the host says navigates (a finger once a pen has been seen) pans with the Hand to its
/// release, even once the host stops saying so mid-drag, and never reaches the selected tool; the next press that does
/// not navigate is the tool's.
#[trial]
fn a_press_the_host_says_navigates_pans_with_the_hand_to_its_release() {
let log = Log::default();
let navigates = Rc::new(Cell::new(true));
let (mut fixture, view, _) = momentary_canvas_under(NavigationOptions::default(), Rc::clone(&navigates), Probe::new("main", &log), |_, _| {});
let at = fixture.by_role(Role::CANVAS)[0].rect.center();
let before = view.get().expect("the canvas ran").center;
fixture.frame(vec![pointer::moved(at)]).frame(vec![pointer::press(at)]).frame(vec![pointer::moved(at + BY * 0.5)]);
navigates.set(false);
fixture.frame(vec![pointer::moved(at + BY)]).frame(vec![pointer::release(at + BY)]).settle();
assert_ne!(view.get().expect("the canvas ran").center, before, "the navigating press pans the view");
let calls = log.borrow().clone();
let pressed = |call: &String| ["main press", "main drag", "main release"].contains(&call.as_str());
assert!(!calls.iter().any(pressed), "the navigating press never reaches the selected tool: {calls:?}");
fixture.frame(vec![pointer::press(at)]).frame(vec![pointer::release(at)]).settle();
let calls = log.borrow().clone();
assert_eq!(calls.iter().filter(|call| *call == "main press").count(), 1, "the next press is the selected tool's: {calls:?}");
}
/// The zoom rectangle of a momentary Zoom that Cmd+Space started keeps being sized by the drag once Cmd is let go, and
/// moves with Space once Space is let go and pressed again, as a marquee does: the release zooms to the moved
/// rectangle. Space held since the press sizes it until let go.
@@ -1249,7 +1273,7 @@ fn space_pressed_again_moves_a_cmd_space_zoom_rectangle() {
let mut options = NavigationOptions::default();
options.scrubby_zoom = false;
options.animated_zoom = false;
let (mut fixture, view, _) = momentary_canvas_under(options, Probe::new("main", &log), |_, _| {});
let (mut fixture, view, _) = momentary_canvas_under(options, Rc::default(), Probe::new("main", &log), |_, _| {});
let room = fixture.by_role(Role::CANVAS)[0].rect;
let at = room.center() - Vec2::new(180.0, 90.0);
let before = view.get().expect("the canvas ran").zoom;