Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

ui: tolerate finger jitter on New Document double-tap

Raincloud249a3baabe44
diff --git a/crates/ui-egui/src/new_doc_ui.rs b/crates/ui-egui/src/new_doc_ui.rs
index e615620d4..07727abdb 100644
--- a/crates/ui-egui/src/new_doc_ui.rs
+++ b/crates/ui-egui/src/new_doc_ui.rs
@@ -424,7 +424,8 @@ fn saved_presets(app: &mut crate::PhotocraftApp, ui: &mut egui::Ui, f: &mut Map<
let response = ui.add_sized(vec2(422.0, 32.0), label).on_hover_text(&preset.name);
if response.clicked() {
apply_saved_preset(f, preset);
- create |= response.double_clicked() || response.triple_clicked();
+ // Touch-tolerant double-tap (finger jitter > egui's 6 px); see `widgets::second_click`.
+ create |= widgets::second_click(ui, &response);
}
if widgets::secondary_button(ui, tl!("Delete"), 76.0).clicked() {
match app.run("document.presets.delete", json!({"name":preset.name})) {
@@ -594,7 +595,8 @@ pub fn body(app: &mut crate::PhotocraftApp, ui: &mut egui::Ui, f: &mut Map<Strin
Card::Builtin(p) => apply_preset(f, p, &units),
Card::Recent(settings) => apply_settings(f, settings),
}
- create |= resp.double_clicked() || resp.triple_clicked();
+ // Touch-tolerant double-tap (finger jitter > egui's 6 px); see `widgets::second_click`.
+ create |= widgets::second_click(ui, &resp);
}
}
});
diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs
index b393c0787..2d84a8461 100644
--- a/crates/ui-egui/src/widgets.rs
+++ b/crates/ui-egui/src/widgets.rs
@@ -975,26 +975,37 @@ pub(crate) fn wheel_notches_of(ui: &Ui, id: egui::Id, hovered: bool, shift_notch
/// Windows' default double-click time (Photoshop's; egui's own default is 0.3 s).
const DOUBLE_CLICK_S: f64 = 0.5;
-/// Whether this click on `resp` completes a double-click, counted the Windows way (Photoshop):
-/// a click soon after a lone click at the same spot; the click after a double-click starts over.
-/// (egui would call a third quick click a triple-click even when the first two weren't a double.)
-fn second_click(ui: &Ui, resp: &Response) -> bool {
+/// Finger jitter between taps easily exceeds egui's 6 px `max_click_dist`; ~32 px is about a third of an inch at 96 DPI and matches common touch hit-slop (Windows' mouse SM_CXDOUBLECLK is only 4 px and is not used for touch).
+const TOUCH_MAX_CLICK_DIST: f32 = 32.0;
+
+/// True when this frame's pointer work came from a touch contact (still down, or a Touch event including End on the click frame).
+fn pointer_from_touch(ui: &Ui) -> bool {
+ ui.input(|i| i.any_touches() || i.events.iter().any(|e| matches!(e, egui::Event::Touch { .. })))
+}
+
+/// Whether this click on `resp` completes a double-click / double-tap, counted the Windows way (Photoshop): a click soon after a lone click near the same spot; the click after a double starts over.
+///
+/// Time window is [`DOUBLE_CLICK_S`] (0.5 s). Mouse uses egui's `max_click_dist` (6 px); touch uses [`TOUCH_MAX_CLICK_DIST`] so finger jitter between taps still counts. Call on every click for that widget so the first tap is remembered.
+pub fn second_click(ui: &Ui, resp: &Response) -> bool {
if !resp.clicked() {
return false;
}
let key = resp.id.with("pc-last-click");
let (time, pos) = ui.input(|i| (i.time, i.pointer.interact_pos()));
- let dist = ui.ctx().options(|o| o.input_options.max_click_dist);
- let last: Option<(f64, Pos2)> = ui.data(|d| d.get_temp(key));
+ let touch = pointer_from_touch(ui);
+ let mouse_dist = ui.ctx().options(|o| o.input_options.max_click_dist);
+ let last: Option<(f64, Pos2, bool)> = ui.data(|d| d.get_temp(key));
+ // Looser distance if either tap in the pair was touch (End clears `any_touches` but leaves Touch in `events`).
+ let dist = if touch || last.is_some_and(|(_, _, t)| t) { TOUCH_MAX_CLICK_DIST.max(mouse_dist) } else { mouse_dist };
let double = match (last, pos) {
- (Some((t, p)), Some(q)) => time - t < DOUBLE_CLICK_S && p.distance(q) < dist,
+ (Some((t, p, _)), Some(q)) => time - t < DOUBLE_CLICK_S && p.distance(q) < dist,
_ => false,
};
ui.data_mut(|d| {
if let (false, Some(q)) = (double, pos) {
- d.insert_temp(key, (time, q));
+ d.insert_temp(key, (time, q, touch));
} else {
- d.remove::<(f64, Pos2)>(key);
+ d.remove::<(f64, Pos2, bool)>(key);
}
});
double
@@ -1902,6 +1913,101 @@ mod tests {
out.textures_delta.clear();
}
}
+
+ /// One primary press+release at `pos`. When `touch`, emit Touch Start/End with the pointer events (as egui-winit does) so [`super::second_click`] uses the touch distance.
+ fn pointer_click(ctx: &egui::Context, time: f64, pos: egui::Pos2, touch: bool) -> bool {
+ let area = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(200.0, 200.0));
+ let button = |pressed| egui::Event::PointerButton {
+ pos,
+ button: egui::PointerButton::Primary,
+ pressed,
+ modifiers: egui::Modifiers::NONE,
+ };
+ let touch_ev = |phase| egui::Event::Touch {
+ device_id: egui::TouchDeviceId(1),
+ id: egui::TouchId(1),
+ phase,
+ pos,
+ force: None,
+ };
+ let mut saw = false;
+ for (t, mut events) in [
+ (time, {
+ let mut v = vec![egui::Event::PointerMoved(pos), button(true)];
+ if touch {
+ v.insert(0, touch_ev(egui::TouchPhase::Start));
+ }
+ v
+ }),
+ (time + 0.02, {
+ let mut v = vec![button(false)];
+ if touch {
+ v.insert(0, touch_ev(egui::TouchPhase::End));
+ }
+ v
+ }),
+ ] {
+ let mut raw = egui::RawInput {
+ time: Some(t),
+ screen_rect: Some(area),
+ ..Default::default()
+ };
+ raw.events.append(&mut events);
+ let mut out = ctx.run_ui(raw, |ui| {
+ let r = ui.allocate_rect(area, egui::Sense::click());
+ saw |= super::second_click(ui, &r);
+ });
+ out.textures_delta.clear();
+ }
+ saw
+ }
+
+ /// #2274 touch: two taps ~20 px apart (past egui's 6 px mouse limit, inside our touch slop) within 0.5 s count as a double; the same mouse path does not.
+ #[test]
+ fn second_click_tolerates_touch_jitter_but_keeps_mouse_tight() {
+ let area = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(200.0, 200.0));
+ let a = egui::pos2(50.0, 50.0);
+ let b = egui::pos2(70.0, 50.0); // 20 px — past max_click_dist (6), inside TOUCH_MAX_CLICK_DIST (32)
+ for touch in [false, true] {
+ let ctx = egui::Context::default();
+ let mut out = ctx.run_ui(
+ egui::RawInput {
+ time: Some(0.0),
+ screen_rect: Some(area),
+ ..Default::default()
+ },
+ |ui| {
+ ui.allocate_rect(area, egui::Sense::click());
+ },
+ );
+ out.textures_delta.clear();
+ assert!(!pointer_click(&ctx, 0.1, a, touch), "first click only arms the pair");
+ let second = pointer_click(&ctx, 0.25, b, touch);
+ assert_eq!(second, touch, "touch={touch}: 20 px jitter is a double only for touch");
+ }
+ }
+
+ /// A mouse double past the time window is not a double, even at the same pixel.
+ #[test]
+ fn second_click_respects_the_half_second_window() {
+ let area = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(200.0, 200.0));
+ let p = egui::pos2(40.0, 40.0);
+ let ctx = egui::Context::default();
+ let mut out = ctx.run_ui(
+ egui::RawInput {
+ time: Some(0.0),
+ screen_rect: Some(area),
+ ..Default::default()
+ },
+ |ui| {
+ ui.allocate_rect(area, egui::Sense::click());
+ },
+ );
+ out.textures_delta.clear();
+ assert!(!pointer_click(&ctx, 0.1, p, false));
+ assert!(!pointer_click(&ctx, 0.1 + super::DOUBLE_CLICK_S + 0.05, p, false), "past Windows' 0.5 s window");
+ }
+
use egui_kittest::{Harness, kittest::Queryable};
/// #3789: a plain text field is one boxed, outline-free input whose text-selection colour stays