1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-28 07:23:13 -04:00

Add Sense::LONG_CLICK

This commit is contained in:
lucasmerlin
2025-10-12 19:45:14 +02:00
parent 96470fabee
commit d92b204691
4 changed files with 24 additions and 9 deletions

View File

@@ -682,6 +682,7 @@ fn install_touchstart(runner_ref: &WebRunner, target: &EventTarget) -> Result<()
|event: web_sys::TouchEvent, runner| {
let mut should_stop_propagation = true;
let mut should_prevent_default = true;
push_touches(runner, egui::TouchPhase::Start, &event);
if let Some((pos, _)) = primary_touch_pos(runner, &event) {
let egui_event = egui::Event::PointerButton {
pos,
@@ -691,10 +692,9 @@ fn install_touchstart(runner_ref: &WebRunner, target: &EventTarget) -> Result<()
};
should_stop_propagation = (runner.web_options.should_stop_propagation)(&egui_event);
should_prevent_default = (runner.web_options.should_prevent_default)(&egui_event);
runner.input.raw.events.push(egui_event);
// runner.input.raw.events.push(egui_event);
}
push_touches(runner, egui::TouchPhase::Start, &event);
runner.needs_repaint.repaint_asap();
// Use web options to tell if the web event should be propagated to parent elements based on the egui event.
@@ -721,7 +721,7 @@ fn install_touchmove(runner_ref: &WebRunner, target: &EventTarget) -> Result<(),
(runner.web_options.should_stop_propagation)(&egui_event);
let should_prevent_default =
(runner.web_options.should_prevent_default)(&egui_event);
runner.input.raw.events.push(egui_event);
// runner.input.raw.events.push(egui_event);
push_touches(runner, egui::TouchPhase::Move, &event);
runner.needs_repaint.repaint();
@@ -746,6 +746,7 @@ fn install_touchend(runner_ref: &WebRunner, target: &EventTarget) -> Result<(),
runner,
egui::pos2(touch.client_x() as f32, touch.client_y() as f32),
) {
push_touches(runner, egui::TouchPhase::End, &event);
// First release mouse to click:
let mut should_stop_propagation = true;
let mut should_prevent_default = true;
@@ -758,15 +759,14 @@ fn install_touchend(runner_ref: &WebRunner, target: &EventTarget) -> Result<(),
should_stop_propagation &=
(runner.web_options.should_stop_propagation)(&egui_event);
should_prevent_default &= (runner.web_options.should_prevent_default)(&egui_event);
runner.input.raw.events.push(egui_event);
// runner.input.raw.events.push(egui_event);
// Then remove hover effect:
should_stop_propagation &=
(runner.web_options.should_stop_propagation)(&egui::Event::PointerGone);
should_prevent_default &=
(runner.web_options.should_prevent_default)(&egui::Event::PointerGone);
runner.input.raw.events.push(egui::Event::PointerGone);
// runner.input.raw.events.push(egui::Event::PointerGone);
push_touches(runner, egui::TouchPhase::End, &event);
runner.needs_repaint.repaint_asap();

View File

@@ -145,6 +145,7 @@ pub(crate) fn interact(
if let Some(widget) = interaction
.potential_click_id
.and_then(|id| widgets.get(id))
&& widget.sense.senses_long_click()
{
dragged = None;
clicked = Some(widget.id);

View File

@@ -19,6 +19,14 @@ bitflags::bitflags! {
/// Anything interactive + labels that can be focused
/// for the benefit of screen readers.
const FOCUSABLE = 1<<2;
/// Sense long clicks
///
/// By default, anything that senses clicks also senses long clicks.
/// You can remove this flag if you want to sense clicks but not long clicks.
///
/// Sensing for long clicks might cause problems when you need to sense precise drags
const LONG_CLICK = 1<<3;
}
}
@@ -53,10 +61,10 @@ impl Sense {
Self::FOCUSABLE
}
/// Sense clicks and hover, but not drags.
/// Sense clicks, long clicks and hover, but not drags.
#[inline]
pub fn click() -> Self {
Self::CLICK | Self::FOCUSABLE
Self::CLICK | Self::FOCUSABLE | Self::LONG_CLICK
}
/// Sense drags and hover, but not clicks.
@@ -75,7 +83,7 @@ impl Sense {
/// See [`crate::PointerState::is_decidedly_dragging`] for details.
#[inline]
pub fn click_and_drag() -> Self {
Self::CLICK | Self::FOCUSABLE | Self::DRAG
Self::CLICK | Self::LONG_CLICK | Self::FOCUSABLE | Self::DRAG
}
/// Returns true if we sense either clicks or drags.
@@ -89,6 +97,11 @@ impl Sense {
self.contains(Self::CLICK)
}
#[inline]
pub fn senses_long_click(&self) -> bool {
self.contains(Self::LONG_CLICK)
}
#[inline]
pub fn senses_drag(&self) -> bool {
self.contains(Self::DRAG)

View File

@@ -5,6 +5,7 @@ use eframe::glow;
#[cfg(target_arch = "wasm32")]
use core::any::Any;
use log::info;
#[derive(Default)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]