1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-26 22:53:14 -04:00

Implemented distance threshold for double/triple clicks (#7817)

This change introduces a distance check to double and triple clicks.
Previously, double/triple clicks were determined solely by timing,
allowing clicks on different UI elements to trigger a
double_clicked()/triple_clicked() event.

By requiring consecutive clicks to occur within a specific radius
(max_multiple_click_dist), we prevent double_clicked()/triple_clicked()
events from triggering when a user clicks on two different, distant UI
elements in rapid succession.

<!--
Please read the "Making a PR" section of
[`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/main/CONTRIBUTING.md)
before opening a Pull Request!

* Keep your PR:s small and focused.
* The PR title is what ends up in the changelog, so make it descriptive!
* If applicable, add a screenshot or gif.
* If it is a non-trivial addition, consider adding a demo for it to
`egui_demo_lib`, or a new example.
* Do NOT open PR:s from your `master` branch, as that makes it hard for
maintainers to test and add commits to your PR.
* Remember to run `cargo fmt` and `cargo clippy`.
* Open the PR as a draft until you have self-reviewed it and run
`./scripts/check.sh`.
* When you have addressed a PR comment, mark it as resolved.

Please be patient! I will review your PR, but my time is limited!
-->

* Closes <https://github.com/emilk/egui/issues/3508>
* [x] I have followed the instructions in the PR template
This commit is contained in:
Belu Antonie-Gabriel
2026-01-07 17:20:16 +02:00
committed by GitHub
parent f8e763378a
commit a9e92525c0

View File

@@ -1037,6 +1037,10 @@ pub struct PointerState {
/// This could also be the trigger point for a long-touch.
pub(crate) started_decidedly_dragging: bool,
/// Where did the last click originate?
/// `None` if no mouse click occurred.
last_click_pos: Option<Pos2>,
/// When did the pointer get click last?
/// Used to check for double-clicks.
last_click_time: f64,
@@ -1074,6 +1078,7 @@ impl Default for PointerState {
press_start_time: None,
has_moved_too_much_for_a_click: false,
started_decidedly_dragging: false,
last_click_pos: None,
last_click_time: f64::NEG_INFINITY,
last_last_click_time: f64::NEG_INFINITY,
last_move_time: f64::NEG_INFINITY,
@@ -1149,10 +1154,18 @@ impl PointerState {
let clicked = self.could_any_button_be_click();
let click = if clicked {
let double_click =
(time - self.last_click_time) < self.options.max_double_click_delay;
let click_dist_sq = self
.last_click_pos
.map_or(0.0, |last_pos| last_pos.distance_sq(pos));
let double_click = (time - self.last_click_time)
< self.options.max_double_click_delay
&& click_dist_sq
< self.options.max_click_dist * self.options.max_click_dist;
let triple_click = (time - self.last_last_click_time)
< (self.options.max_double_click_delay * 2.0);
< (self.options.max_double_click_delay * 2.0)
&& click_dist_sq
< self.options.max_click_dist * self.options.max_click_dist;
let count = if triple_click {
3
} else if double_click {
@@ -1163,6 +1176,7 @@ impl PointerState {
self.last_last_click_time = self.last_click_time;
self.last_click_time = time;
self.last_click_pos = Some(pos);
Some(Click {
pos,
@@ -1630,6 +1644,7 @@ impl PointerState {
press_start_time,
has_moved_too_much_for_a_click,
started_decidedly_dragging,
last_click_pos,
last_click_time,
last_last_click_time,
pointer_events,
@@ -1655,6 +1670,7 @@ impl PointerState {
ui.label(format!(
"started_decidedly_dragging: {started_decidedly_dragging}"
));
ui.label(format!("last_click_pos: {last_click_pos:#?}"));
ui.label(format!("last_click_time: {last_click_time:#?}"));
ui.label(format!("last_last_click_time: {last_last_click_time:#?}"));
ui.label(format!("last_move_time: {last_move_time:#?}"));