From c73932e21c443ab1927e18d588c5ef1b37764018 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 27 Mar 2024 16:01:26 +0100 Subject: [PATCH 1/2] Fix `InputState::any_touches` returning `true` too often Add `InputState::has_touch_screen` to query if there ever has been any touches (which is what `any_touches` used to return). --- crates/egui/src/input_state.rs | 5 +++++ crates/egui/src/input_state/touch_state.rs | 5 +++++ crates/egui/src/widgets/label.rs | 2 +- crates/egui/src/widgets/text_edit/builder.rs | 2 +- 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/crates/egui/src/input_state.rs b/crates/egui/src/input_state.rs index b2284d45c2e..488e84e9f3a 100644 --- a/crates/egui/src/input_state.rs +++ b/crates/egui/src/input_state.rs @@ -485,6 +485,11 @@ impl InputState { /// True if there currently are any fingers touching egui. pub fn any_touches(&self) -> bool { + self.touch_states.values().any(|t| t.any_touches()) + } + + /// True if we have ever receved a touch event. + pub fn has_touch_screen(&self) -> bool { !self.touch_states.is_empty() } diff --git a/crates/egui/src/input_state/touch_state.rs b/crates/egui/src/input_state/touch_state.rs index 2a77a4d371b..43053f821e5 100644 --- a/crates/egui/src/input_state/touch_state.rs +++ b/crates/egui/src/input_state/touch_state.rs @@ -177,6 +177,11 @@ impl TouchState { } } + /// Are there currently any fingers touching the surface? + pub fn any_touches(&self) -> bool { + !self.active_touches.is_empty() + } + pub fn info(&self) -> Option { self.gesture_state.as_ref().map(|state| { // state.previous can be `None` when the number of simultaneous touches has just diff --git a/crates/egui/src/widgets/label.rs b/crates/egui/src/widgets/label.rs index f5bb7ac425a..10cefc5fb6a 100644 --- a/crates/egui/src/widgets/label.rs +++ b/crates/egui/src/widgets/label.rs @@ -128,7 +128,7 @@ impl Label { // dragging select text, or scroll the enclosing [`ScrollArea`] (if any)? // Since currently copying selected text in not supported on `eframe` web, // we prioritize touch-scrolling: - let allow_drag_to_select = ui.input(|i| !i.any_touches()); + let allow_drag_to_select = ui.input(|i| !i.has_touch_screen()); let mut select_sense = if allow_drag_to_select { Sense::click_and_drag() diff --git a/crates/egui/src/widgets/text_edit/builder.rs b/crates/egui/src/widgets/text_edit/builder.rs index e5745389082..799774096ff 100644 --- a/crates/egui/src/widgets/text_edit/builder.rs +++ b/crates/egui/src/widgets/text_edit/builder.rs @@ -527,7 +527,7 @@ impl<'t> TextEdit<'t> { // Since currently copying selected text in not supported on `eframe` web, // we prioritize touch-scrolling: let allow_drag_to_select = - ui.input(|i| !i.any_touches()) || ui.memory(|mem| mem.has_focus(id)); + ui.input(|i| !i.has_touch_screen()) || ui.memory(|mem| mem.has_focus(id)); let sense = if interactive { if allow_drag_to_select { From 7a69ab952b983d959f00af19576b14644ebb4304 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 27 Mar 2024 16:06:31 +0100 Subject: [PATCH 2/2] Fixed typo --- crates/egui/src/input_state.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/egui/src/input_state.rs b/crates/egui/src/input_state.rs index 488e84e9f3a..a9c1b314383 100644 --- a/crates/egui/src/input_state.rs +++ b/crates/egui/src/input_state.rs @@ -488,7 +488,7 @@ impl InputState { self.touch_states.values().any(|t| t.any_touches()) } - /// True if we have ever receved a touch event. + /// True if we have ever received a touch event. pub fn has_touch_screen(&self) -> bool { !self.touch_states.is_empty() }