ui-canvas: ToolInput::clicks injects a double or triple click through the window host's click injection

This commit is contained in:
bflatastic
2026-11-22 04:47:22 +07:00
parent a7a3b72eb0
commit ecb5efdebd
2 changed files with 50 additions and 10 deletions
+22 -3
View File
@@ -158,6 +158,19 @@ pub enum ToolInput {
/// The modifiers held.
mods: Mods,
},
/// `count` presses and releases of `button` at `pos`, one pass each, after a move there: two make a double click,
/// three a triple click, as the canvas's press counts presses close together in time and place.
#[non_exhaustive]
Click {
/// The button.
button: PointerButton,
/// The pointer.
pos: Pos2,
/// How many presses.
count: u32,
/// The modifiers held.
mods: Mods,
},
/// A key goes down with no key event: the passes from this one on run with the key held
/// ([`ToolContext::held`]) until its [`ToolInput::LetGo`].
#[non_exhaustive]
@@ -189,6 +202,11 @@ impl ToolInput {
ToolInput::Up { pos, mods: Mods::NONE }
}
/// `count` mouse clicks of `button` at `pos` in a row, with no modifiers: 2 is a double click, 3 a triple click.
pub const fn clicks(button: PointerButton, pos: Pos2, count: u32) -> Self {
ToolInput::Click { button, pos, count, mods: Mods::NONE }
}
/// `key` going down, held until its [`ToolInput::let_go`].
pub const fn hold(key: Key) -> Self {
ToolInput::Hold { key }
@@ -203,7 +221,7 @@ impl ToolInput {
#[must_use]
pub fn mods(mut self, held: Mods) -> Self {
match &mut self {
ToolInput::Down { mods, .. } | ToolInput::Move { mods, .. } | ToolInput::Up { mods, .. } => *mods = held,
ToolInput::Down { mods, .. } | ToolInput::Move { mods, .. } | ToolInput::Up { mods, .. } | ToolInput::Click { mods, .. } => *mods = held,
ToolInput::Hold { .. } | ToolInput::LetGo { .. } => {}
}
self
@@ -219,12 +237,12 @@ impl ToolInput {
self
}
/// The input as a pen's, with its reading `pen`; a button coming up and a held key have none.
/// The input as a pen's, with its reading `pen`; a button coming up, clicks and a held key have none.
#[must_use]
pub fn sample(mut self, pen: PointerSample) -> Self {
match &mut self {
ToolInput::Down { sample, .. } | ToolInput::Move { sample, .. } => *sample = Some(pen),
ToolInput::Up { .. } | ToolInput::Hold { .. } | ToolInput::LetGo { .. } => {}
ToolInput::Up { .. } | ToolInput::Click { .. } | ToolInput::Hold { .. } | ToolInput::LetGo { .. } => {}
}
self
}
@@ -723,6 +741,7 @@ impl<'m> ToolHost<'m> {
}
ToolInput::Move { pos, sample, mods, rest } => host.inject_pointer(viewport, InjectedPointer::move_resting(pos, mods, sample, rest)),
ToolInput::Up { pos, mods } => host.inject_pointer(viewport, InjectedPointer::up(pos, mods)),
ToolInput::Click { button, pos, count, mods } => host.inject_click(viewport, pos, button, count, mods, false),
ToolInput::Hold { key } => host.inject_hold(viewport, key),
ToolInput::LetGo { key } => host.inject_let_go(viewport, key),
}
+28 -7
View File
@@ -827,15 +827,17 @@ fn a_held_space_ends_when_the_injected_input_is_cancelled() {
assert_eq!(rig.main.borrow().space_drags, [true, false], "Space is held for the first drag and not after the cancel");
}
/// A tool that records the pen pressure of its press and drags.
/// A tool that records the pen pressure of its press and drags, and the click count of each press.
#[derive(Default)]
struct PenProbe {
pressures: Vec<f32>,
kinds: Vec<PointerKind>,
clicks: Vec<u32>,
}
impl CanvasTool for PenProbe {
fn press(&mut self, pointer: &ToolPointer, _: &mut ToolContext<'_>) -> ToolAction {
self.clicks.push(pointer.clicks);
self.pressures.push(pointer.sample.pressure);
self.kinds.push(pointer.kind);
ToolAction::Claimed
@@ -857,12 +859,9 @@ impl CanvasTool for PenProbe {
}
}
/// An injected pen reading reaches the tool as a tablet's does: the press and the drag carry the pressure their steps
/// were injected with, and the pointer kind of a pen.
#[trial]
fn an_injected_pen_reading_reaches_the_tool() {
let probe = Rc::new(RefCell::new(PenProbe::default()));
let (shown_probe, canvas) = (Rc::clone(&probe), Rc::new(Cell::new(Id::NULL)));
/// A canvas showing `probe` as its tool; the canvas's id is in the returned cell after the first pass.
fn probe_fixture(probe: &Rc<RefCell<PenProbe>>) -> (Fixture, Rc<Cell<Id>>) {
let (shown_probe, canvas) = (Rc::clone(probe), Rc::new(Cell::new(Id::NULL)));
let shown_canvas = Rc::clone(&canvas);
let mut fixture = Fixture::new(move |ui| {
let room = ui.available_rect_before_wrap();
@@ -874,6 +873,28 @@ fn an_injected_pen_reading_reaches_the_tool() {
shown_canvas.set(shown.output.response.id);
});
fixture.settle();
(fixture, canvas)
}
/// Injected clicks in a row reach the tool as one press each, counted as a double and then a triple click.
#[trial]
fn injected_clicks_reach_the_tool_as_a_multi_click() {
let probe = Rc::new(RefCell::new(PenProbe::default()));
let (mut fixture, canvas) = probe_fixture(&probe);
let at = fixture.by_role(Role::CANVAS)[0].rect.center();
ToolHost::inject(&fixture.ctx().clone(), canvas.get(), ToolInput::clicks(PointerButton::Primary, at, 3));
fixture.run_injected();
let clicks = probe.borrow().clicks.clone();
assert!(clicks.len() == 3 && clicks.windows(2).all(|pair| pair[1] > pair[0]), "each press counts one more: {clicks:?}");
assert_eq!(clicks.last(), Some(&3), "the third press is a triple click");
}
/// An injected pen reading reaches the tool as a tablet's does: the press and the drag carry the pressure their steps
/// were injected with, and the pointer kind of a pen.
#[trial]
fn an_injected_pen_reading_reaches_the_tool() {
let probe = Rc::new(RefCell::new(PenProbe::default()));
let (mut fixture, canvas) = probe_fixture(&probe);
let at = fixture.by_role(Role::CANVAS)[0].rect.center();
let ctx = fixture.ctx().clone();
for input in [