ui-contract: a press of any button runs on the click run, as the old apps counted clicks; only a primary press carries the count

This commit is contained in:
bflatastic
2026-11-23 04:28:57 +07:00
parent 0d980bf749
commit 5bfcb2249d
2 changed files with 36 additions and 4 deletions
+9 -4
View File
@@ -100,7 +100,7 @@ pub struct PassPointer {
#[derive(Default)]
pub(crate) struct Memory {
/// The latest primary press: where, when, its click count and the modifiers held with it.
/// The latest press of any button: where, when, its place in the click run and the modifiers held with it.
last_press: Option<(Pos2, f64, u32, Mods)>,
press_targets: PressTargets,
/// A cancel a service requested for the running pass.
@@ -260,10 +260,15 @@ pub(crate) fn begin(ctx: &Context, hit: HitSizes, latched: craft_keymap::Mods) {
read.events.iter().any(|event| matches!(event, PointerEvent::Pressed { button: PointerButton::Middle | PointerButton::Secondary, .. }));
let (events, targets, touched) = PerViewport::<Memory>::with(ctx, viewport, |memory| {
let mut events = read.events.clone();
// Every button's press runs on the click run, as a click of any button did in the old apps: a middle-button pan
// click between two clicks makes the second a triple click. Only a primary press carries its count.
for event in &mut events {
if let PointerEvent::Pressed { button: PointerButton::Primary, pos, time, clicks, .. } = event {
*clicks = count_clicks(memory.last_press, *pos, *time, mods, hit.slop);
memory.last_press = Some((*pos, *time, *clicks, mods));
if let PointerEvent::Pressed { button, pos, time, clicks, .. } = event {
let counted = count_clicks(memory.last_press, *pos, *time, mods, hit.slop);
memory.last_press = Some((*pos, *time, counted, mods));
if *button == PointerButton::Primary {
*clicks = counted;
}
}
}
if read.primary_pressed || other_pressed {
@@ -573,3 +573,30 @@ fn a_click_carries_the_modifiers_its_release_carried() {
assert_eq!(pick, released, "Shift is the pick's in the release pass only: {seen:?}");
}
}
/// A press of another button counts in the click run as a primary one does: a primary click, a middle click (a pan the
/// view tools take) and a primary click make the last a triple click, as the old apps counted them. Only primary
/// presses carry the count.
#[trial]
fn a_middle_click_between_primary_clicks_counts_in_the_click_run() {
let at = Pos2::new(50.0, 50.0);
let counts: Rc<RefCell<Vec<u32>>> = Rc::default();
let sink = counts.clone();
let mut show = move |ui: &mut Ui| {
if let Some(press) = craft_ui::runtime::pointer::frame(ui.ctx()).press.filter(|press| press.button == craft_behavior::PointerButton::Primary)
{
sink.borrow_mut().push(press.clicks);
}
};
let mut frames = Frames::new();
frames.idle(2, &mut show);
frames.frame(vec![pointer::moved(at)], &mut show);
for button in ["primary", "middle", "primary"] {
let (down, up) =
if button == "middle" { (pointer::middle(at, true), pointer::middle(at, false)) } else { (pointer::press(at), pointer::release(at)) };
frames.frame(vec![down], &mut show);
frames.frame(vec![up], &mut show);
}
let counts = counts.borrow();
assert!(counts.len() == 2 && counts[1] == counts[0] + 2, "the middle press counts in the run: {counts:?}");
}