egui/input_state/mod.rs
1mod touch_state;
2mod wheel_state;
3
4use crate::{
5 SafeAreaInsets,
6 emath::{NumExt as _, Pos2, Rect, Vec2, vec2},
7 util::History,
8};
9use crate::{
10 data::input::{
11 Event, EventFilter, KeyboardShortcut, Modifiers, NUM_POINTER_BUTTONS, PointerButton,
12 RawInput, TouchDeviceId, ViewportInfo,
13 },
14 input_state::wheel_state::WheelState,
15};
16use std::{
17 collections::{BTreeMap, HashSet},
18 time::Duration,
19};
20
21pub use crate::Key;
22pub use touch_state::MultiTouchInfo;
23use touch_state::TouchState;
24
25#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
26#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
27pub enum SurrenderFocusOn {
28 /// Surrender focus if the user _presses_ somewhere outside the focused widget.
29 Presses,
30
31 /// Surrender focus if the user _clicks_ somewhere outside the focused widget.
32 #[default]
33 Clicks,
34
35 /// Never surrender focus.
36 Never,
37}
38
39impl SurrenderFocusOn {
40 pub fn ui(&mut self, ui: &mut crate::Ui) {
41 ui.horizontal(|ui| {
42 ui.selectable_value(self, Self::Presses, "Presses")
43 .on_hover_text(
44 "Surrender focus if the user presses somewhere outside the focused widget.",
45 );
46 ui.selectable_value(self, Self::Clicks, "Clicks")
47 .on_hover_text(
48 "Surrender focus if the user clicks somewhere outside the focused widget.",
49 );
50 ui.selectable_value(self, Self::Never, "Never")
51 .on_hover_text("Never surrender focus.");
52 });
53 }
54}
55
56/// Options for input state handling.
57#[derive(Clone, Copy, Debug, PartialEq)]
58#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
59pub struct InputOptions {
60 /// Multiplier for the scroll speed when reported in [`crate::MouseWheelUnit::Line`]s.
61 pub line_scroll_speed: f32,
62
63 /// Controls the speed at which we zoom in when doing ctrl/cmd + scroll.
64 pub scroll_zoom_speed: f32,
65
66 /// After a pointer-down event, if the pointer moves more than this, it won't become a click.
67 pub max_click_dist: f32,
68
69 /// If the pointer is down for longer than this it will no longer register as a click.
70 ///
71 /// If a touch is held for this many seconds while still, then it will register as a
72 /// "long-touch" which is equivalent to a secondary click.
73 ///
74 /// This is to support "press and hold for context menu" on touch screens.
75 pub max_click_duration: f64,
76
77 /// The new pointer press must come within this many seconds from previous pointer release
78 /// for double click (or when this value is doubled, triple click) to count.
79 pub max_double_click_delay: f64,
80
81 /// When this modifier is down, all scroll events are treated as zoom events.
82 ///
83 /// The default is CTRL/CMD, and it is STRONGLY recommended to NOT change this.
84 pub zoom_modifier: Modifiers,
85
86 /// When this modifier is down, all scroll events are treated as horizontal scrolls,
87 /// and when combined with [`Self::zoom_modifier`] it will result in zooming
88 /// on only the horizontal axis.
89 ///
90 /// The default is SHIFT, and it is STRONGLY recommended to NOT change this.
91 pub horizontal_scroll_modifier: Modifiers,
92
93 /// When this modifier is down, all scroll events are treated as vertical scrolls,
94 /// and when combined with [`Self::zoom_modifier`] it will result in zooming
95 /// on only the vertical axis.
96 pub vertical_scroll_modifier: Modifiers,
97
98 /// When should we surrender focus from the focused widget?
99 pub surrender_focus_on: SurrenderFocusOn,
100}
101
102impl Default for InputOptions {
103 fn default() -> Self {
104 // TODO(emilk): figure out why these constants need to be different on web and on native (winit).
105 let is_web = cfg!(target_arch = "wasm32");
106 let line_scroll_speed = if is_web {
107 8.0
108 } else {
109 40.0 // Scroll speed decided by consensus: https://github.com/emilk/egui/issues/461
110 };
111
112 Self {
113 line_scroll_speed,
114 scroll_zoom_speed: 1.0 / 200.0,
115 max_click_dist: 6.0,
116 max_click_duration: 0.8,
117 max_double_click_delay: 0.3,
118 zoom_modifier: Modifiers::COMMAND,
119 horizontal_scroll_modifier: Modifiers::SHIFT,
120 vertical_scroll_modifier: Modifiers::ALT,
121 surrender_focus_on: SurrenderFocusOn::default(),
122 }
123 }
124}
125
126impl InputOptions {
127 /// Show the options in the ui.
128 pub fn ui(&mut self, ui: &mut crate::Ui) {
129 let Self {
130 line_scroll_speed,
131 scroll_zoom_speed,
132 max_click_dist,
133 max_click_duration,
134 max_double_click_delay,
135 zoom_modifier,
136 horizontal_scroll_modifier,
137 vertical_scroll_modifier,
138 surrender_focus_on,
139 } = self;
140 crate::Grid::new("InputOptions")
141 .num_columns(2)
142 .striped(true)
143 .show(ui, |ui| {
144 ui.label("Line scroll speed");
145 ui.add(crate::DragValue::new(line_scroll_speed).range(0.0..=f32::INFINITY))
146 .on_hover_text(
147 "How many lines to scroll with each tick of the mouse wheel",
148 );
149 ui.end_row();
150
151 ui.label("Scroll zoom speed");
152 ui.add(
153 crate::DragValue::new(scroll_zoom_speed)
154 .range(0.0..=f32::INFINITY)
155 .speed(0.001),
156 )
157 .on_hover_text("How fast to zoom with ctrl/cmd + scroll");
158 ui.end_row();
159
160 ui.label("Max click distance");
161 ui.add(crate::DragValue::new(max_click_dist).range(0.0..=f32::INFINITY))
162 .on_hover_text(
163 "If the pointer moves more than this, it won't become a click",
164 );
165 ui.end_row();
166
167 ui.label("Max click duration");
168 ui.add(
169 crate::DragValue::new(max_click_duration)
170 .range(0.1..=f64::INFINITY)
171 .speed(0.1),
172 )
173 .on_hover_text(
174 "If the pointer is down for longer than this it will no longer register as a click",
175 );
176 ui.end_row();
177
178 ui.label("Max double click delay");
179 ui.add(
180 crate::DragValue::new(max_double_click_delay)
181 .range(0.01..=f64::INFINITY)
182 .speed(0.1),
183 )
184 .on_hover_text("Max time interval for double click to count");
185 ui.end_row();
186
187 ui.label("zoom_modifier");
188 zoom_modifier.ui(ui);
189 ui.end_row();
190
191 ui.label("horizontal_scroll_modifier");
192 horizontal_scroll_modifier.ui(ui);
193 ui.end_row();
194
195 ui.label("vertical_scroll_modifier");
196 vertical_scroll_modifier.ui(ui);
197 ui.end_row();
198
199 ui.label("surrender_focus_on");
200 surrender_focus_on.ui(ui);
201 ui.end_row();
202
203 });
204 }
205}
206
207/// Input state that egui updates each frame.
208///
209/// You can access this with [`crate::Context::input`].
210///
211/// You can check if `egui` is using the inputs using
212/// [`crate::Context::wants_pointer_input`] and [`crate::Context::wants_keyboard_input`].
213#[derive(Clone, Debug)]
214#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
215pub struct InputState {
216 /// The raw input we got this frame from the backend.
217 pub raw: RawInput,
218
219 /// State of the mouse or simple touch gestures which can be mapped to mouse operations.
220 pub pointer: PointerState,
221
222 /// State of touches, except those covered by `PointerState` (like clicks and drags).
223 /// (We keep a separate [`TouchState`] for each encountered touch device.)
224 touch_states: BTreeMap<TouchDeviceId, TouchState>,
225
226 // ----------------------------------------------
227 // Scrolling:
228 #[cfg_attr(feature = "serde", serde(skip))]
229 wheel: WheelState,
230
231 /// How many points the user scrolled, smoothed over a few frames.
232 ///
233 /// The delta dictates how the _content_ should move.
234 ///
235 /// A positive X-value indicates the content is being moved right,
236 /// as when swiping right on a touch-screen or track-pad with natural scrolling.
237 ///
238 /// A positive Y-value indicates the content is being moved down,
239 /// as when swiping down on a touch-screen or track-pad with natural scrolling.
240 ///
241 /// [`crate::ScrollArea`] will both read and write to this field, so that
242 /// at the end of the frame this will be zero if a scroll-area consumed the delta.
243 pub smooth_scroll_delta: Vec2,
244
245 /// Zoom scale factor this frame (e.g. from ctrl-scroll or pinch gesture).
246 ///
247 /// * `zoom = 1`: no change.
248 /// * `zoom < 1`: pinch together
249 /// * `zoom > 1`: pinch spread
250 zoom_factor_delta: f32,
251
252 /// Rotation in radians this frame, measuring clockwise (e.g. from a rotation gesture).
253 rotation_radians: f32,
254
255 // ----------------------------------------------
256 /// Position and size of the egui area.
257 ///
258 /// This is including the area that may be covered by the `safe_area_insets`.
259 viewport_rect: Rect,
260
261 /// The safe area insets, subtracted from the `viewport_rect` in [`Self::content_rect`].
262 safe_area_insets: SafeAreaInsets,
263
264 /// Also known as device pixel ratio, > 1 for high resolution screens.
265 pub pixels_per_point: f32,
266
267 /// Maximum size of one side of a texture.
268 ///
269 /// This depends on the backend.
270 pub max_texture_side: usize,
271
272 /// Time in seconds. Relative to whatever. Used for animation.
273 pub time: f64,
274
275 /// Time since last frame, in seconds.
276 ///
277 /// This can be very unstable in reactive mode (when we don't paint each frame).
278 /// For animations it is therefore better to use [`Self::stable_dt`].
279 pub unstable_dt: f32,
280
281 /// Estimated time until next frame (provided we repaint right away).
282 ///
283 /// Used for animations to get instant feedback (avoid frame delay).
284 /// Should be set to the expected time between frames when painting at vsync speeds.
285 ///
286 /// On most integrations this has a fixed value of `1.0 / 60.0`, so it is not a very accurate estimate.
287 pub predicted_dt: f32,
288
289 /// Time since last frame (in seconds), but gracefully handles the first frame after sleeping in reactive mode.
290 ///
291 /// In reactive mode (available in e.g. `eframe`), `egui` only updates when there is new input
292 /// or something is animating.
293 /// This can lead to large gaps of time (sleep), leading to large [`Self::unstable_dt`].
294 ///
295 /// If `egui` requested a repaint the previous frame, then `egui` will use
296 /// `stable_dt = unstable_dt;`, but if `egui` did not not request a repaint last frame,
297 /// then `egui` will assume `unstable_dt` is too large, and will use
298 /// `stable_dt = predicted_dt;`.
299 ///
300 /// This means that for the first frame after a sleep,
301 /// `stable_dt` will be a prediction of the delta-time until the next frame,
302 /// and in all other situations this will be an accurate measurement of time passed
303 /// since the previous frame.
304 ///
305 /// Note that a frame can still stall for various reasons, so `stable_dt` can
306 /// still be unusually large in some situations.
307 ///
308 /// When animating something, it is recommended that you use something like
309 /// `stable_dt.min(0.1)` - this will give you smooth animations when the framerate is good
310 /// (even in reactive mode), but will avoid large jumps when framerate is bad,
311 /// and will effectively slow down the animation when FPS drops below 10.
312 pub stable_dt: f32,
313
314 /// The native window has the keyboard focus (i.e. is receiving key presses).
315 ///
316 /// False when the user alt-tab away from the application, for instance.
317 pub focused: bool,
318
319 /// Which modifier keys are down at the start of the frame?
320 pub modifiers: Modifiers,
321
322 /// The keys that are currently being held down.
323 ///
324 /// Keys released this frame are NOT considered down.
325 pub keys_down: HashSet<Key>,
326
327 /// In-order events received this frame
328 pub events: Vec<Event>,
329
330 /// Input state management configuration.
331 ///
332 /// This gets copied from `egui::Options` at the start of each frame for convenience.
333 options: InputOptions,
334}
335
336impl Default for InputState {
337 fn default() -> Self {
338 Self {
339 raw: Default::default(),
340 pointer: Default::default(),
341 touch_states: Default::default(),
342
343 wheel: Default::default(),
344 smooth_scroll_delta: Vec2::ZERO,
345 zoom_factor_delta: 1.0,
346 rotation_radians: 0.0,
347
348 viewport_rect: Rect::from_min_size(Default::default(), vec2(10_000.0, 10_000.0)),
349 safe_area_insets: Default::default(),
350 pixels_per_point: 1.0,
351 max_texture_side: 2048,
352 time: 0.0,
353 unstable_dt: 1.0 / 60.0,
354 predicted_dt: 1.0 / 60.0,
355 stable_dt: 1.0 / 60.0,
356 focused: false,
357 modifiers: Default::default(),
358 keys_down: Default::default(),
359 events: Default::default(),
360 options: Default::default(),
361 }
362 }
363}
364
365impl InputState {
366 #[must_use]
367 pub fn begin_pass(
368 mut self,
369 mut new: RawInput,
370 requested_immediate_repaint_prev_frame: bool,
371 pixels_per_point: f32,
372 options: InputOptions,
373 ) -> Self {
374 profiling::function_scope!();
375
376 let time = new.time.unwrap_or(self.time + new.predicted_dt as f64);
377 let unstable_dt = (time - self.time) as f32;
378
379 let stable_dt = if requested_immediate_repaint_prev_frame {
380 // we should have had a repaint straight away,
381 // so this should be trustable.
382 unstable_dt
383 } else {
384 new.predicted_dt
385 };
386
387 let safe_area_insets = new.safe_area_insets.unwrap_or(self.safe_area_insets);
388 let viewport_rect = new.screen_rect.unwrap_or(self.viewport_rect);
389 self.create_touch_states_for_new_devices(&new.events);
390 for touch_state in self.touch_states.values_mut() {
391 touch_state.begin_pass(time, &new, self.pointer.interact_pos);
392 }
393 let pointer = self.pointer.begin_pass(time, &new, options);
394
395 let mut keys_down = self.keys_down;
396 let mut zoom_factor_delta = 1.0; // TODO(emilk): smoothing for zoom factor
397 let mut rotation_radians = 0.0;
398
399 self.wheel.smooth_wheel_delta = Vec2::ZERO;
400
401 for event in &mut new.events {
402 match event {
403 Event::Key {
404 key,
405 pressed,
406 repeat,
407 ..
408 } => {
409 if *pressed {
410 let first_press = keys_down.insert(*key);
411 *repeat = !first_press;
412 } else {
413 keys_down.remove(key);
414 }
415 }
416 Event::MouseWheel {
417 unit,
418 delta,
419 phase,
420 modifiers,
421 } => {
422 self.wheel.on_wheel_event(
423 viewport_rect,
424 &options,
425 time,
426 *unit,
427 *delta,
428 *phase,
429 *modifiers,
430 );
431 }
432 Event::Zoom(factor) => {
433 zoom_factor_delta *= *factor;
434 }
435 Event::Rotate(radians) => {
436 rotation_radians += *radians;
437 }
438 Event::WindowFocused(false) => {
439 // Example: pressing `Cmd+S` brings up a save-dialog (e.g. using rfd),
440 // but we get no key-up event for the `S` key (in winit).
441 // This leads to `S` being mistakenly marked as down when we switch back to the app.
442 // So we take the safe route and just clear all the keys and modifiers when
443 // the app loses focus.
444 keys_down.clear();
445 }
446 _ => {}
447 }
448 }
449
450 let mut smooth_scroll_delta = Vec2::ZERO;
451
452 {
453 let dt = stable_dt.at_most(0.1);
454 self.wheel.after_events(time, dt);
455
456 let is_zoom = self.wheel.modifiers.matches_any(options.zoom_modifier);
457
458 if is_zoom {
459 zoom_factor_delta *= (options.scroll_zoom_speed
460 * (self.wheel.smooth_wheel_delta.x + self.wheel.smooth_wheel_delta.y))
461 .exp();
462 } else {
463 smooth_scroll_delta = self.wheel.smooth_wheel_delta;
464 }
465 }
466
467 Self {
468 pointer,
469 touch_states: self.touch_states,
470
471 wheel: self.wheel,
472 smooth_scroll_delta,
473 zoom_factor_delta,
474 rotation_radians,
475
476 viewport_rect,
477 safe_area_insets,
478 pixels_per_point,
479 max_texture_side: new.max_texture_side.unwrap_or(self.max_texture_side),
480 time,
481 unstable_dt,
482 predicted_dt: new.predicted_dt,
483 stable_dt,
484 focused: new.focused,
485 modifiers: new.modifiers,
486 keys_down,
487 events: new.events.clone(), // TODO(emilk): remove clone() and use raw.events
488 raw: new,
489 options,
490 }
491 }
492
493 /// Info about the active viewport
494 #[inline]
495 pub fn viewport(&self) -> &ViewportInfo {
496 self.raw.viewport()
497 }
498
499 /// Returns the region of the screen that is safe for content rendering
500 ///
501 /// Returns the `viewport_rect` with the `safe_area_insets` removed.
502 ///
503 /// If you want to render behind e.g. the dynamic island on iOS, use [`Self::viewport_rect`].
504 ///
505 /// See also [`RawInput::safe_area_insets`].
506 #[inline(always)]
507 pub fn content_rect(&self) -> Rect {
508 self.viewport_rect - self.safe_area_insets
509 }
510
511 /// Returns the full area available to egui, including parts that might be partially covered,
512 /// for example, by the OS status bar or notches (see [`Self::safe_area_insets`]).
513 ///
514 /// Usually you want to use [`Self::content_rect`] instead.
515 ///
516 /// This rectangle includes e.g. the dynamic island on iOS.
517 /// If you want to only render _below_ the that (not behind), then you should use
518 /// [`Self::content_rect`] instead.
519 ///
520 /// See also [`RawInput::safe_area_insets`].
521 pub fn viewport_rect(&self) -> Rect {
522 self.viewport_rect
523 }
524
525 /// Position and size of the egui area.
526 #[deprecated(
527 note = "screen_rect has been split into viewport_rect() and content_rect(). You likely should use content_rect()"
528 )]
529 pub fn screen_rect(&self) -> Rect {
530 self.content_rect()
531 }
532
533 /// Get the safe area insets.
534 ///
535 /// This represents the area of the screen covered by status bars, navigation controls, notches,
536 /// or other items that obscure part of the screen.
537 ///
538 /// See [`Self::content_rect`] to get the `viewport_rect` with the safe area insets removed.
539 pub fn safe_area_insets(&self) -> SafeAreaInsets {
540 self.safe_area_insets
541 }
542
543 /// How many points the user scrolled, smoothed over a few frames.
544 ///
545 /// The delta dictates how the _content_ should move.
546 ///
547 /// A positive X-value indicates the content is being moved right,
548 /// as when swiping right on a touch-screen or track-pad with natural scrolling.
549 ///
550 /// A positive Y-value indicates the content is being moved down,
551 /// as when swiping down on a touch-screen or track-pad with natural scrolling.
552 ///
553 /// [`crate::ScrollArea`] will both read and write to this field, so that
554 /// at the end of the frame this will be zero if a scroll-area consumed the delta.
555 pub fn smooth_scroll_delta(&self) -> Vec2 {
556 self.smooth_scroll_delta
557 }
558
559 /// Uniform zoom scale factor this frame (e.g. from ctrl-scroll or pinch gesture).
560 /// * `zoom = 1`: no change
561 /// * `zoom < 1`: pinch together
562 /// * `zoom > 1`: pinch spread
563 ///
564 /// If your application supports non-proportional zooming,
565 /// then you probably want to use [`Self::zoom_delta_2d`] instead.
566 #[inline(always)]
567 pub fn zoom_delta(&self) -> f32 {
568 // If a multi touch gesture is detected, it measures the exact and linear proportions of
569 // the distances of the finger tips. It is therefore potentially more accurate than
570 // `zoom_factor_delta` which is based on the `ctrl-scroll` event which, in turn, may be
571 // synthesized from an original touch gesture.
572 self.multi_touch()
573 .map_or(self.zoom_factor_delta, |touch| touch.zoom_delta)
574 }
575
576 /// 2D non-proportional zoom scale factor this frame (e.g. from ctrl-scroll or pinch gesture).
577 ///
578 /// For multitouch devices the user can do a horizontal or vertical pinch gesture.
579 /// In these cases a non-proportional zoom factor is a available.
580 /// In other cases, this reverts to `Vec2::splat(self.zoom_delta())`.
581 ///
582 /// For horizontal pinches, this will return `[z, 1]`,
583 /// for vertical pinches this will return `[1, z]`,
584 /// and otherwise this will return `[z, z]`,
585 /// where `z` is the zoom factor:
586 /// * `zoom = 1`: no change
587 /// * `zoom < 1`: pinch together
588 /// * `zoom > 1`: pinch spread
589 #[inline(always)]
590 pub fn zoom_delta_2d(&self) -> Vec2 {
591 // If a multi touch gesture is detected, it measures the exact and linear proportions of
592 // the distances of the finger tips. It is therefore potentially more accurate than
593 // `zoom_factor_delta` which is based on the `ctrl-scroll` event which, in turn, may be
594 // synthesized from an original touch gesture.
595 if let Some(multi_touch) = self.multi_touch() {
596 multi_touch.zoom_delta_2d
597 } else {
598 let mut zoom = Vec2::splat(self.zoom_factor_delta);
599
600 let is_horizontal = self
601 .modifiers
602 .matches_any(self.options.horizontal_scroll_modifier);
603 let is_vertical = self
604 .modifiers
605 .matches_any(self.options.vertical_scroll_modifier);
606
607 if is_horizontal && !is_vertical {
608 // Horizontal-only zooming.
609 zoom.y = 1.0;
610 }
611 if !is_horizontal && is_vertical {
612 // Vertical-only zooming.
613 zoom.x = 1.0;
614 }
615
616 zoom
617 }
618 }
619
620 /// Rotation in radians this frame, measuring clockwise (e.g. from a rotation gesture).
621 #[inline(always)]
622 pub fn rotation_delta(&self) -> f32 {
623 self.multi_touch()
624 .map_or(self.rotation_radians, |touch| touch.rotation_delta)
625 }
626
627 /// Panning translation in pixels this frame (e.g. from scrolling or a pan gesture)
628 ///
629 /// The delta indicates how the **content** should move.
630 ///
631 /// A positive X-value indicates the content is being moved right, as when swiping right on a touch-screen or track-pad with natural scrolling.
632 ///
633 /// A positive Y-value indicates the content is being moved down, as when swiping down on a touch-screen or track-pad with natural scrolling.
634 #[inline(always)]
635 pub fn translation_delta(&self) -> Vec2 {
636 self.multi_touch().map_or_else(
637 || self.smooth_scroll_delta(),
638 |touch| touch.translation_delta,
639 )
640 }
641
642 /// True if there is an active scroll action that might scroll more when using [`Self::smooth_scroll_delta`].
643 pub fn is_scrolling(&self) -> bool {
644 self.wheel.is_scrolling()
645 }
646
647 /// How long has it been (in seconds) since the last scroll event?
648 #[inline(always)]
649 pub fn time_since_last_scroll(&self) -> f32 {
650 (self.time - self.wheel.last_wheel_event) as f32
651 }
652
653 /// The [`crate::Context`] will call this at the beginning of each frame to see if we need a repaint.
654 ///
655 /// Returns how long to wait for a repaint.
656 ///
657 /// NOTE: It's important to call this immediately after [`Self::begin_pass`] since calls to
658 /// [`Self::consume_key`] will remove events from the vec, meaning those key presses wouldn't
659 /// cause a repaint.
660 pub(crate) fn wants_repaint_after(&self) -> Option<Duration> {
661 if self.pointer.wants_repaint()
662 || self.wheel.unprocessed_wheel_delta.abs().max_elem() > 0.2
663 || !self.events.is_empty()
664 || !self.raw.hovered_files.is_empty()
665 || !self.raw.dropped_files.is_empty()
666 {
667 // Immediate repaint
668 return Some(Duration::ZERO);
669 }
670
671 if self.any_touches() && !self.pointer.is_decidedly_dragging() {
672 // We need to wake up and check for press-and-hold for the context menu.
673 if let Some(press_start_time) = self.pointer.press_start_time {
674 let press_duration = self.time - press_start_time;
675 if self.options.max_click_duration.is_finite()
676 && press_duration < self.options.max_click_duration
677 {
678 let secs_until_menu = self.options.max_click_duration - press_duration;
679 return Some(Duration::from_secs_f64(secs_until_menu));
680 }
681 }
682 }
683
684 None
685 }
686
687 /// Count presses of a key. If non-zero, the presses are consumed, so that this will only return non-zero once.
688 ///
689 /// Includes key-repeat events.
690 ///
691 /// This uses [`Modifiers::matches_logically`] to match modifiers,
692 /// meaning extra Shift and Alt modifiers are ignored.
693 /// Therefore, you should match most specific shortcuts first,
694 /// i.e. check for `Cmd-Shift-S` ("Save as…") before `Cmd-S` ("Save"),
695 /// so that a user pressing `Cmd-Shift-S` won't trigger the wrong command!
696 pub fn count_and_consume_key(&mut self, modifiers: Modifiers, logical_key: Key) -> usize {
697 let mut count = 0usize;
698
699 self.events.retain(|event| {
700 let is_match = matches!(
701 event,
702 Event::Key {
703 key: ev_key,
704 modifiers: ev_mods,
705 pressed: true,
706 ..
707 } if *ev_key == logical_key && ev_mods.matches_logically(modifiers)
708 );
709
710 count += is_match as usize;
711
712 !is_match
713 });
714
715 count
716 }
717
718 /// Check for a key press. If found, `true` is returned and the key pressed is consumed, so that this will only return `true` once.
719 ///
720 /// Includes key-repeat events.
721 ///
722 /// This uses [`Modifiers::matches_logically`] to match modifiers,
723 /// meaning extra Shift and Alt modifiers are ignored.
724 /// Therefore, you should match most specific shortcuts first,
725 /// i.e. check for `Cmd-Shift-S` ("Save as…") before `Cmd-S` ("Save"),
726 /// so that a user pressing `Cmd-Shift-S` won't trigger the wrong command!
727 pub fn consume_key(&mut self, modifiers: Modifiers, logical_key: Key) -> bool {
728 self.count_and_consume_key(modifiers, logical_key) > 0
729 }
730
731 /// Check if the given shortcut has been pressed.
732 ///
733 /// If so, `true` is returned and the key pressed is consumed, so that this will only return `true` once.
734 ///
735 /// This uses [`Modifiers::matches_logically`] to match modifiers,
736 /// meaning extra Shift and Alt modifiers are ignored.
737 /// Therefore, you should match most specific shortcuts first,
738 /// i.e. check for `Cmd-Shift-S` ("Save as…") before `Cmd-S` ("Save"),
739 /// so that a user pressing `Cmd-Shift-S` won't trigger the wrong command!
740 pub fn consume_shortcut(&mut self, shortcut: &KeyboardShortcut) -> bool {
741 let KeyboardShortcut {
742 modifiers,
743 logical_key,
744 } = *shortcut;
745 self.consume_key(modifiers, logical_key)
746 }
747
748 /// Was the given key pressed this frame?
749 ///
750 /// Includes key-repeat events.
751 pub fn key_pressed(&self, desired_key: Key) -> bool {
752 self.num_presses(desired_key) > 0
753 }
754
755 /// How many times was the given key pressed this frame?
756 ///
757 /// Includes key-repeat events.
758 pub fn num_presses(&self, desired_key: Key) -> usize {
759 self.events
760 .iter()
761 .filter(|event| {
762 matches!(
763 event,
764 Event::Key { key, pressed: true, .. }
765 if *key == desired_key
766 )
767 })
768 .count()
769 }
770
771 /// Is the given key currently held down?
772 ///
773 /// Keys released this frame are NOT considered down.
774 pub fn key_down(&self, desired_key: Key) -> bool {
775 self.keys_down.contains(&desired_key)
776 }
777
778 /// Was the given key released this frame?
779 pub fn key_released(&self, desired_key: Key) -> bool {
780 self.events.iter().any(|event| {
781 matches!(
782 event,
783 Event::Key {
784 key,
785 pressed: false,
786 ..
787 } if *key == desired_key
788 )
789 })
790 }
791
792 /// Also known as device pixel ratio, > 1 for high resolution screens.
793 #[inline(always)]
794 pub fn pixels_per_point(&self) -> f32 {
795 self.pixels_per_point
796 }
797
798 /// Size of a physical pixel in logical gui coordinates (points).
799 #[inline(always)]
800 pub fn physical_pixel_size(&self) -> f32 {
801 1.0 / self.pixels_per_point()
802 }
803
804 /// How imprecise do we expect the mouse/touch input to be?
805 /// Returns imprecision in points.
806 #[inline(always)]
807 pub fn aim_radius(&self) -> f32 {
808 // TODO(emilk): multiply by ~3 for touch inputs because fingers are fat
809 self.physical_pixel_size()
810 }
811
812 /// Returns details about the currently ongoing multi-touch gesture, if any. Note that this
813 /// method returns `None` for single-touch gestures (click, drag, …).
814 ///
815 /// ```
816 /// # use egui::emath::Rot2;
817 /// # egui::__run_test_ui(|ui| {
818 /// let mut zoom = 1.0; // no zoom
819 /// let mut rotation = 0.0; // no rotation
820 /// let multi_touch = ui.input(|i| i.multi_touch());
821 /// if let Some(multi_touch) = multi_touch {
822 /// zoom *= multi_touch.zoom_delta;
823 /// rotation += multi_touch.rotation_delta;
824 /// }
825 /// let transform = zoom * Rot2::from_angle(rotation);
826 /// # });
827 /// ```
828 ///
829 /// By far not all touch devices are supported, and the details depend on the `egui`
830 /// integration backend you are using. `eframe` web supports multi touch for most mobile
831 /// devices, but not for a `Trackpad` on `MacOS`, for example. The backend has to be able to
832 /// capture native touch events, but many browsers seem to pass such events only for touch
833 /// _screens_, but not touch _pads._
834 ///
835 /// Refer to [`MultiTouchInfo`] for details about the touch information available.
836 ///
837 /// Consider using `zoom_delta()` instead of `MultiTouchInfo::zoom_delta` as the former
838 /// delivers a synthetic zoom factor based on ctrl-scroll events, as a fallback.
839 pub fn multi_touch(&self) -> Option<MultiTouchInfo> {
840 // In case of multiple touch devices simply pick the touch_state of the first active device
841 self.touch_states.values().find_map(|t| t.info())
842 }
843
844 /// True if there currently are any fingers touching egui.
845 pub fn any_touches(&self) -> bool {
846 self.touch_states.values().any(|t| t.any_touches())
847 }
848
849 /// True if we have ever received a touch event.
850 pub fn has_touch_screen(&self) -> bool {
851 !self.touch_states.is_empty()
852 }
853
854 /// Scans `events` for device IDs of touch devices we have not seen before,
855 /// and creates a new [`TouchState`] for each such device.
856 fn create_touch_states_for_new_devices(&mut self, events: &[Event]) {
857 for event in events {
858 if let Event::Touch { device_id, .. } = event {
859 self.touch_states
860 .entry(*device_id)
861 .or_insert_with(|| TouchState::new(*device_id));
862 }
863 }
864 }
865
866 pub fn accesskit_action_requests(
867 &self,
868 id: crate::Id,
869 action: accesskit::Action,
870 ) -> impl Iterator<Item = &accesskit::ActionRequest> {
871 let accesskit_id = id.accesskit_id();
872 self.events.iter().filter_map(move |event| {
873 if let Event::AccessKitActionRequest(request) = event
874 && request.target_node == accesskit_id
875 && request.target_tree == accesskit::TreeId::ROOT
876 && request.action == action
877 {
878 return Some(request);
879 }
880 None
881 })
882 }
883
884 pub fn consume_accesskit_action_requests(
885 &mut self,
886 id: crate::Id,
887 mut consume: impl FnMut(&accesskit::ActionRequest) -> bool,
888 ) {
889 let accesskit_id = id.accesskit_id();
890 self.events.retain(|event| {
891 if let Event::AccessKitActionRequest(request) = event
892 && request.target_node == accesskit_id
893 && request.target_tree == accesskit::TreeId::ROOT
894 {
895 return !consume(request);
896 }
897 true
898 });
899 }
900
901 pub fn has_accesskit_action_request(&self, id: crate::Id, action: accesskit::Action) -> bool {
902 self.accesskit_action_requests(id, action).next().is_some()
903 }
904
905 pub fn num_accesskit_action_requests(&self, id: crate::Id, action: accesskit::Action) -> usize {
906 self.accesskit_action_requests(id, action).count()
907 }
908
909 /// Get all events that matches the given filter.
910 pub fn filtered_events(&self, filter: &EventFilter) -> Vec<Event> {
911 self.events
912 .iter()
913 .filter(|event| filter.matches(event))
914 .cloned()
915 .collect()
916 }
917
918 /// A long press is something we detect on touch screens
919 /// to trigger a secondary click (context menu).
920 ///
921 /// Returns `true` only on one frame.
922 pub(crate) fn is_long_touch(&self) -> bool {
923 self.any_touches() && self.pointer.is_long_press()
924 }
925}
926
927// ----------------------------------------------------------------------------
928
929/// A pointer (mouse or touch) click.
930#[derive(Clone, Debug, PartialEq)]
931#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
932pub(crate) struct Click {
933 pub pos: Pos2,
934
935 /// 1 or 2 (double-click) or 3 (triple-click)
936 pub count: u32,
937
938 /// Allows you to check for e.g. shift-click
939 pub modifiers: Modifiers,
940}
941
942impl Click {
943 pub fn is_double(&self) -> bool {
944 self.count == 2
945 }
946
947 pub fn is_triple(&self) -> bool {
948 self.count == 3
949 }
950}
951
952#[derive(Clone, Debug, PartialEq)]
953#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
954pub(crate) enum PointerEvent {
955 Moved(Pos2),
956 Pressed {
957 position: Pos2,
958 button: PointerButton,
959 },
960 Released {
961 click: Option<Click>,
962 button: PointerButton,
963 },
964}
965
966impl PointerEvent {
967 pub fn is_press(&self) -> bool {
968 matches!(self, Self::Pressed { .. })
969 }
970
971 pub fn is_release(&self) -> bool {
972 matches!(self, Self::Released { .. })
973 }
974
975 pub fn is_click(&self) -> bool {
976 matches!(self, Self::Released { click: Some(_), .. })
977 }
978}
979
980/// Mouse or touch state.
981///
982/// To access the methods of [`PointerState`] you can use the [`crate::Context::input`] function
983///
984/// ```rust
985/// # let ctx = egui::Context::default();
986/// let latest_pos = ctx.input(|i| i.pointer.latest_pos());
987/// let is_pointer_down = ctx.input(|i| i.pointer.any_down());
988/// ```
989///
990#[derive(Clone, Debug)]
991#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
992pub struct PointerState {
993 /// Latest known time
994 time: f64,
995
996 // Consider a finger tapping a touch screen.
997 // What position should we report?
998 // The location of the touch, or `None`, because the finger is gone?
999 //
1000 // For some cases we want the first: e.g. to check for interaction.
1001 // For showing tooltips, we want the latter (no tooltips, since there are no fingers).
1002 /// Latest reported pointer position.
1003 /// When tapping a touch screen, this will be `None`.
1004 latest_pos: Option<Pos2>,
1005
1006 /// Latest position of the mouse, but ignoring any [`Event::PointerGone`]
1007 /// if there were interactions this frame.
1008 /// When tapping a touch screen, this will be the location of the touch.
1009 interact_pos: Option<Pos2>,
1010
1011 /// How much the pointer moved compared to last frame, in points.
1012 delta: Vec2,
1013
1014 /// How much the mouse moved since the last frame, in unspecified units.
1015 /// Represents the actual movement of the mouse, without acceleration or clamped by screen edges.
1016 /// May be unavailable on some integrations.
1017 motion: Option<Vec2>,
1018
1019 /// Current velocity of pointer.
1020 velocity: Vec2,
1021
1022 /// Current direction of pointer.
1023 direction: Vec2,
1024
1025 /// Recent movement of the pointer.
1026 /// Used for calculating velocity of pointer.
1027 pos_history: History<Pos2>,
1028
1029 /// Buttons currently down, excluding those released this frame.
1030 down: [bool; NUM_POINTER_BUTTONS],
1031
1032 /// Where did the current click/drag originate?
1033 /// `None` if no mouse button is down.
1034 press_origin: Option<Pos2>,
1035
1036 /// When did the current click/drag originate?
1037 /// `None` if no mouse button is down.
1038 press_start_time: Option<f64>,
1039
1040 /// Set to `true` if the pointer has moved too much (since being pressed)
1041 /// for it to be registered as a click.
1042 pub(crate) has_moved_too_much_for_a_click: bool,
1043
1044 /// Did [`Self::is_decidedly_dragging`] go from `false` to `true` this frame?
1045 ///
1046 /// This could also be the trigger point for a long-touch.
1047 pub(crate) started_decidedly_dragging: bool,
1048
1049 /// Where did the last click originate?
1050 /// `None` if no mouse click occurred.
1051 last_click_pos: Option<Pos2>,
1052
1053 /// When did the pointer get click last?
1054 /// Used to check for double-clicks.
1055 last_click_time: f64,
1056
1057 /// When did the pointer get click two clicks ago?
1058 /// Used to check for triple-clicks.
1059 last_last_click_time: f64,
1060
1061 /// When was the pointer last moved?
1062 /// Used for things like showing hover ui/tooltip with a delay.
1063 last_move_time: f64,
1064
1065 /// All button events that occurred this frame
1066 pub(crate) pointer_events: Vec<PointerEvent>,
1067
1068 /// Input state management configuration.
1069 ///
1070 /// This gets copied from `egui::Options` at the start of each frame for convenience.
1071 options: InputOptions,
1072}
1073
1074impl Default for PointerState {
1075 fn default() -> Self {
1076 Self {
1077 time: -f64::INFINITY,
1078 latest_pos: None,
1079 interact_pos: None,
1080 delta: Vec2::ZERO,
1081 motion: None,
1082 velocity: Vec2::ZERO,
1083 direction: Vec2::ZERO,
1084 pos_history: History::new(2..1000, 0.1),
1085 down: Default::default(),
1086 press_origin: None,
1087 press_start_time: None,
1088 has_moved_too_much_for_a_click: false,
1089 started_decidedly_dragging: false,
1090 last_click_pos: None,
1091 last_click_time: f64::NEG_INFINITY,
1092 last_last_click_time: f64::NEG_INFINITY,
1093 last_move_time: f64::NEG_INFINITY,
1094 pointer_events: vec![],
1095 options: Default::default(),
1096 }
1097 }
1098}
1099
1100impl PointerState {
1101 #[must_use]
1102 pub(crate) fn begin_pass(mut self, time: f64, new: &RawInput, options: InputOptions) -> Self {
1103 let was_decidedly_dragging = self.is_decidedly_dragging();
1104
1105 self.time = time;
1106 self.options = options;
1107
1108 self.pointer_events.clear();
1109
1110 let old_pos = self.latest_pos;
1111 self.interact_pos = self.latest_pos;
1112 if self.motion.is_some() {
1113 self.motion = Some(Vec2::ZERO);
1114 }
1115
1116 let mut clear_history_after_velocity_calculation = false;
1117 for event in &new.events {
1118 match event {
1119 Event::PointerMoved(pos) => {
1120 let pos = *pos;
1121
1122 self.latest_pos = Some(pos);
1123 self.interact_pos = Some(pos);
1124
1125 if let Some(press_origin) = self.press_origin {
1126 self.has_moved_too_much_for_a_click |=
1127 press_origin.distance(pos) > self.options.max_click_dist;
1128 }
1129
1130 self.last_move_time = time;
1131 self.pointer_events.push(PointerEvent::Moved(pos));
1132 }
1133 Event::PointerButton {
1134 pos,
1135 button,
1136 pressed,
1137 modifiers,
1138 } => {
1139 let pos = *pos;
1140 let button = *button;
1141 let pressed = *pressed;
1142 let modifiers = *modifiers;
1143
1144 self.latest_pos = Some(pos);
1145 self.interact_pos = Some(pos);
1146
1147 if pressed {
1148 // Start of a drag: we want to track the velocity for during the drag
1149 // and ignore any incoming movement
1150 self.pos_history.clear();
1151 }
1152
1153 if pressed {
1154 self.press_origin = Some(pos);
1155 self.press_start_time = Some(time);
1156 self.has_moved_too_much_for_a_click = false;
1157 self.pointer_events.push(PointerEvent::Pressed {
1158 position: pos,
1159 button,
1160 });
1161 } else {
1162 // Released
1163 let clicked = self.could_any_button_be_click();
1164
1165 let click = if clicked {
1166 let click_dist_sq = self
1167 .last_click_pos
1168 .map_or(0.0, |last_pos| last_pos.distance_sq(pos));
1169
1170 let double_click = (time - self.last_click_time)
1171 < self.options.max_double_click_delay
1172 && click_dist_sq
1173 < self.options.max_click_dist * self.options.max_click_dist;
1174 let triple_click = (time - self.last_last_click_time)
1175 < (self.options.max_double_click_delay * 2.0)
1176 && click_dist_sq
1177 < self.options.max_click_dist * self.options.max_click_dist;
1178 let count = if triple_click {
1179 3
1180 } else if double_click {
1181 2
1182 } else {
1183 1
1184 };
1185
1186 self.last_last_click_time = self.last_click_time;
1187 self.last_click_time = time;
1188 self.last_click_pos = Some(pos);
1189
1190 Some(Click {
1191 pos,
1192 count,
1193 modifiers,
1194 })
1195 } else {
1196 None
1197 };
1198
1199 self.pointer_events
1200 .push(PointerEvent::Released { click, button });
1201
1202 self.press_origin = None;
1203 self.press_start_time = None;
1204 }
1205
1206 self.down[button as usize] = pressed; // must be done after the above call to `could_any_button_be_click`
1207 }
1208 Event::PointerGone => {
1209 self.latest_pos = None;
1210 // When dragging a slider and the mouse leaves the viewport, we still want the drag to work,
1211 // so we don't treat this as a `PointerEvent::Released`.
1212 // NOTE: we do NOT clear `self.interact_pos` here. It will be cleared next frame.
1213
1214 // Delay the clearing until after the final velocity calculation, so we can
1215 // get the final velocity when `drag_stopped` is true.
1216 clear_history_after_velocity_calculation = true;
1217 }
1218 Event::MouseMoved(delta) => *self.motion.get_or_insert(Vec2::ZERO) += *delta,
1219 _ => {}
1220 }
1221 }
1222
1223 self.delta = if let (Some(old_pos), Some(new_pos)) = (old_pos, self.latest_pos) {
1224 new_pos - old_pos
1225 } else {
1226 Vec2::ZERO
1227 };
1228
1229 if let Some(pos) = self.latest_pos {
1230 self.pos_history.add(time, pos);
1231 } else {
1232 // we do not clear the `pos_history` here, because it is exactly when a finger has
1233 // released from the touch screen that we may want to assign a velocity to whatever
1234 // the user tried to throw.
1235 }
1236
1237 self.pos_history.flush(time);
1238
1239 self.velocity = if self.pos_history.len() >= 3 && self.pos_history.duration() > 0.01 {
1240 self.pos_history.velocity().unwrap_or_default()
1241 } else {
1242 Vec2::default()
1243 };
1244 if self.velocity != Vec2::ZERO {
1245 self.last_move_time = time;
1246 }
1247 if clear_history_after_velocity_calculation {
1248 self.pos_history.clear();
1249 }
1250
1251 self.direction = self.pos_history.velocity().unwrap_or_default().normalized();
1252
1253 self.started_decidedly_dragging = self.is_decidedly_dragging() && !was_decidedly_dragging;
1254
1255 self
1256 }
1257
1258 fn wants_repaint(&self) -> bool {
1259 !self.pointer_events.is_empty() || self.delta != Vec2::ZERO
1260 }
1261
1262 /// How much the pointer moved compared to last frame, in points.
1263 #[inline(always)]
1264 pub fn delta(&self) -> Vec2 {
1265 self.delta
1266 }
1267
1268 /// How much the mouse moved since the last frame, in unspecified units.
1269 /// Represents the actual movement of the mouse, without acceleration or clamped by screen edges.
1270 /// May be unavailable on some integrations.
1271 #[inline(always)]
1272 pub fn motion(&self) -> Option<Vec2> {
1273 self.motion
1274 }
1275
1276 /// Current velocity of pointer.
1277 ///
1278 /// This is smoothed over a few frames,
1279 /// but can be ZERO when frame-rate is bad.
1280 #[inline(always)]
1281 pub fn velocity(&self) -> Vec2 {
1282 self.velocity
1283 }
1284
1285 /// Current direction of the pointer.
1286 ///
1287 /// This is less sensitive to bad framerate than [`Self::velocity`].
1288 #[inline(always)]
1289 pub fn direction(&self) -> Vec2 {
1290 self.direction
1291 }
1292
1293 /// Where did the current click/drag originate?
1294 /// `None` if no mouse button is down.
1295 #[inline(always)]
1296 pub fn press_origin(&self) -> Option<Pos2> {
1297 self.press_origin
1298 }
1299
1300 /// How far has the pointer moved since the start of the drag (if any)?
1301 pub fn total_drag_delta(&self) -> Option<Vec2> {
1302 Some(self.latest_pos? - self.press_origin?)
1303 }
1304
1305 /// When did the current click/drag originate?
1306 /// `None` if no mouse button is down.
1307 #[inline(always)]
1308 pub fn press_start_time(&self) -> Option<f64> {
1309 self.press_start_time
1310 }
1311
1312 /// Latest reported pointer position.
1313 /// When tapping a touch screen, this will be `None`.
1314 #[inline(always)]
1315 pub fn latest_pos(&self) -> Option<Pos2> {
1316 self.latest_pos
1317 }
1318
1319 /// If it is a good idea to show a tooltip, where is pointer?
1320 #[inline(always)]
1321 pub fn hover_pos(&self) -> Option<Pos2> {
1322 self.latest_pos
1323 }
1324
1325 /// If you detect a click or drag and wants to know where it happened, use this.
1326 ///
1327 /// Latest position of the mouse, but ignoring any [`Event::PointerGone`]
1328 /// if there were interactions this frame.
1329 /// When tapping a touch screen, this will be the location of the touch.
1330 #[inline(always)]
1331 pub fn interact_pos(&self) -> Option<Pos2> {
1332 self.interact_pos
1333 }
1334
1335 /// Do we have a pointer?
1336 ///
1337 /// `false` if the mouse is not over the egui area, or if no touches are down on touch screens.
1338 #[inline(always)]
1339 pub fn has_pointer(&self) -> bool {
1340 self.latest_pos.is_some()
1341 }
1342
1343 /// Is the pointer currently still?
1344 /// This is smoothed so a few frames of stillness is required before this returns `true`.
1345 #[inline(always)]
1346 pub fn is_still(&self) -> bool {
1347 self.velocity == Vec2::ZERO
1348 }
1349
1350 /// Is the pointer currently moving?
1351 /// This is smoothed so a few frames of stillness is required before this returns `false`.
1352 #[inline]
1353 pub fn is_moving(&self) -> bool {
1354 self.velocity != Vec2::ZERO
1355 }
1356
1357 /// How long has it been (in seconds) since the pointer was last moved?
1358 #[inline(always)]
1359 pub fn time_since_last_movement(&self) -> f32 {
1360 (self.time - self.last_move_time) as f32
1361 }
1362
1363 /// How long has it been (in seconds) since the pointer was clicked?
1364 #[inline(always)]
1365 pub fn time_since_last_click(&self) -> f32 {
1366 (self.time - self.last_click_time) as f32
1367 }
1368
1369 /// Was any pointer button pressed (`!down -> down`) this frame?
1370 ///
1371 /// This can sometimes return `true` even if `any_down() == false`
1372 /// because a press can be shorted than one frame.
1373 pub fn any_pressed(&self) -> bool {
1374 self.pointer_events.iter().any(|event| event.is_press())
1375 }
1376
1377 /// Was any pointer button released (`down -> !down`) this frame?
1378 pub fn any_released(&self) -> bool {
1379 self.pointer_events.iter().any(|event| event.is_release())
1380 }
1381
1382 /// Was the button given pressed this frame?
1383 pub fn button_pressed(&self, button: PointerButton) -> bool {
1384 self.pointer_events
1385 .iter()
1386 .any(|event| matches!(event, &PointerEvent::Pressed{button: b, ..} if button == b))
1387 }
1388
1389 /// Was the button given released this frame?
1390 pub fn button_released(&self, button: PointerButton) -> bool {
1391 self.pointer_events
1392 .iter()
1393 .any(|event| matches!(event, &PointerEvent::Released{button: b, ..} if button == b))
1394 }
1395
1396 /// Was the primary button pressed this frame?
1397 pub fn primary_pressed(&self) -> bool {
1398 self.button_pressed(PointerButton::Primary)
1399 }
1400
1401 /// Was the secondary button pressed this frame?
1402 pub fn secondary_pressed(&self) -> bool {
1403 self.button_pressed(PointerButton::Secondary)
1404 }
1405
1406 /// Was the primary button released this frame?
1407 pub fn primary_released(&self) -> bool {
1408 self.button_released(PointerButton::Primary)
1409 }
1410
1411 /// Was the secondary button released this frame?
1412 pub fn secondary_released(&self) -> bool {
1413 self.button_released(PointerButton::Secondary)
1414 }
1415
1416 /// Is any pointer button currently down?
1417 ///
1418 /// Buttons released this frame are NOT considered down.
1419 pub fn any_down(&self) -> bool {
1420 self.down.iter().any(|&down| down)
1421 }
1422
1423 /// Were there any type of click this frame?
1424 pub fn any_click(&self) -> bool {
1425 self.pointer_events.iter().any(|event| event.is_click())
1426 }
1427
1428 /// Was the given pointer button given clicked this frame?
1429 ///
1430 /// Returns true on double- and triple- clicks too.
1431 pub fn button_clicked(&self, button: PointerButton) -> bool {
1432 self.pointer_events
1433 .iter()
1434 .any(|event| matches!(event, &PointerEvent::Released { button: b, click: Some(_) } if button == b))
1435 }
1436
1437 /// Was the button given double clicked this frame?
1438 pub fn button_double_clicked(&self, button: PointerButton) -> bool {
1439 self.pointer_events.iter().any(|event| {
1440 matches!(
1441 &event,
1442 PointerEvent::Released {
1443 click: Some(click),
1444 button: b,
1445 } if *b == button && click.is_double()
1446 )
1447 })
1448 }
1449
1450 /// Was the button given triple clicked this frame?
1451 pub fn button_triple_clicked(&self, button: PointerButton) -> bool {
1452 self.pointer_events.iter().any(|event| {
1453 matches!(
1454 &event,
1455 PointerEvent::Released {
1456 click: Some(click),
1457 button: b,
1458 } if *b == button && click.is_triple()
1459 )
1460 })
1461 }
1462
1463 /// Was the primary button clicked this frame?
1464 pub fn primary_clicked(&self) -> bool {
1465 self.button_clicked(PointerButton::Primary)
1466 }
1467
1468 /// Was the secondary button clicked this frame?
1469 pub fn secondary_clicked(&self) -> bool {
1470 self.button_clicked(PointerButton::Secondary)
1471 }
1472
1473 /// Is this button currently down?
1474 ///
1475 /// Buttons released this frame are NOT considered down.
1476 #[inline(always)]
1477 pub fn button_down(&self, button: PointerButton) -> bool {
1478 self.down[button as usize]
1479 }
1480
1481 /// If the pointer button is down, will it register as a click when released?
1482 ///
1483 /// See also [`Self::is_decidedly_dragging`].
1484 pub fn could_any_button_be_click(&self) -> bool {
1485 if self.any_down() || self.any_released() {
1486 if self.has_moved_too_much_for_a_click {
1487 return false;
1488 }
1489
1490 if let Some(press_start_time) = self.press_start_time
1491 && self.time - press_start_time > self.options.max_click_duration
1492 {
1493 return false;
1494 }
1495
1496 true
1497 } else {
1498 false
1499 }
1500 }
1501
1502 /// Just because the mouse is down doesn't mean we are dragging.
1503 /// We could be at the start of a click.
1504 /// But if the mouse is down long enough, or has moved far enough,
1505 /// then we consider it a drag.
1506 ///
1507 /// This function can return true on the same frame the drag is released,
1508 /// but NOT on the first frame it was started.
1509 ///
1510 /// See also [`Self::could_any_button_be_click`].
1511 pub fn is_decidedly_dragging(&self) -> bool {
1512 (self.any_down() || self.any_released())
1513 && !self.any_pressed()
1514 && !self.could_any_button_be_click()
1515 && !self.any_click()
1516 }
1517
1518 /// A long press is something we detect on touch screens
1519 /// to trigger a secondary click (context menu).
1520 ///
1521 /// Returns `true` only on one frame.
1522 pub(crate) fn is_long_press(&self) -> bool {
1523 self.started_decidedly_dragging
1524 && !self.has_moved_too_much_for_a_click
1525 && self.button_down(PointerButton::Primary)
1526 && self.press_start_time.is_some_and(|press_start_time| {
1527 self.time - press_start_time > self.options.max_click_duration
1528 })
1529 }
1530
1531 /// Is the primary button currently down?
1532 ///
1533 /// Buttons released this frame are NOT considered down.
1534 #[inline(always)]
1535 pub fn primary_down(&self) -> bool {
1536 self.button_down(PointerButton::Primary)
1537 }
1538
1539 /// Is the secondary button currently down?
1540 ///
1541 /// Buttons released this frame are NOT considered down.
1542 #[inline(always)]
1543 pub fn secondary_down(&self) -> bool {
1544 self.button_down(PointerButton::Secondary)
1545 }
1546
1547 /// Is the middle button currently down?
1548 ///
1549 /// Buttons released this frame are NOT considered down.
1550 #[inline(always)]
1551 pub fn middle_down(&self) -> bool {
1552 self.button_down(PointerButton::Middle)
1553 }
1554
1555 /// Is the mouse moving in the direction of the given rect?
1556 pub fn is_moving_towards_rect(&self, rect: &Rect) -> bool {
1557 if self.is_still() {
1558 return false;
1559 }
1560
1561 if let Some(pos) = self.hover_pos() {
1562 let dir = self.direction();
1563 if dir != Vec2::ZERO {
1564 return rect.intersects_ray(pos, self.direction());
1565 }
1566 }
1567 false
1568 }
1569}
1570
1571impl InputState {
1572 pub fn ui(&self, ui: &mut crate::Ui) {
1573 let Self {
1574 raw,
1575 pointer,
1576 touch_states,
1577 wheel,
1578 smooth_scroll_delta,
1579 rotation_radians,
1580 zoom_factor_delta,
1581 viewport_rect,
1582 safe_area_insets,
1583 pixels_per_point,
1584 max_texture_side,
1585 time,
1586 unstable_dt,
1587 predicted_dt,
1588 stable_dt,
1589 focused,
1590 modifiers,
1591 keys_down,
1592 events,
1593 options: _,
1594 } = self;
1595
1596 if let Some(style) = ui.style_mut().text_styles.get_mut(&crate::TextStyle::Body) {
1597 style.family = crate::FontFamily::Monospace;
1598 }
1599
1600 ui.collapsing("Raw Input", |ui| raw.ui(ui));
1601
1602 crate::containers::CollapsingHeader::new("🖱 Pointer")
1603 .default_open(false)
1604 .show(ui, |ui| {
1605 pointer.ui(ui);
1606 });
1607
1608 for (device_id, touch_state) in touch_states {
1609 ui.collapsing(format!("Touch State [device {}]", device_id.0), |ui| {
1610 touch_state.ui(ui);
1611 });
1612 }
1613
1614 crate::containers::CollapsingHeader::new("⬍ Scroll")
1615 .default_open(false)
1616 .show(ui, |ui| {
1617 wheel.ui(ui);
1618 });
1619
1620 ui.label(format!("smooth_scroll_delta: {smooth_scroll_delta:4.1}x"));
1621 ui.label(format!("zoom_factor_delta: {zoom_factor_delta:4.2}x"));
1622 ui.label(format!("rotation_radians: {rotation_radians:.3} radians"));
1623
1624 ui.label(format!("viewport_rect: {viewport_rect:?} points"));
1625 ui.label(format!("safe_area_insets: {safe_area_insets:?} points"));
1626 ui.label(format!(
1627 "{pixels_per_point} physical pixels for each logical point"
1628 ));
1629 ui.label(format!(
1630 "max texture size (on each side): {max_texture_side}"
1631 ));
1632 ui.label(format!("time: {time:.3} s"));
1633 ui.label(format!(
1634 "time since previous frame: {:.1} ms",
1635 1e3 * unstable_dt
1636 ));
1637 ui.label(format!("predicted_dt: {:.1} ms", 1e3 * predicted_dt));
1638 ui.label(format!("stable_dt: {:.1} ms", 1e3 * stable_dt));
1639 ui.label(format!("focused: {focused}"));
1640 ui.label(format!("modifiers: {modifiers:#?}"));
1641 ui.label(format!("keys_down: {keys_down:?}"));
1642 ui.scope(|ui| {
1643 ui.set_min_height(150.0);
1644 ui.label(format!("events: {events:#?}"))
1645 .on_hover_text("key presses etc");
1646 });
1647 }
1648}
1649
1650impl PointerState {
1651 pub fn ui(&self, ui: &mut crate::Ui) {
1652 let Self {
1653 time: _,
1654 latest_pos,
1655 interact_pos,
1656 delta,
1657 motion,
1658 velocity,
1659 direction,
1660 pos_history: _,
1661 down,
1662 press_origin,
1663 press_start_time,
1664 has_moved_too_much_for_a_click,
1665 started_decidedly_dragging,
1666 last_click_pos,
1667 last_click_time,
1668 last_last_click_time,
1669 pointer_events,
1670 last_move_time,
1671 options: _,
1672 } = self;
1673
1674 ui.label(format!("latest_pos: {latest_pos:?}"));
1675 ui.label(format!("interact_pos: {interact_pos:?}"));
1676 ui.label(format!("delta: {delta:?}"));
1677 ui.label(format!("motion: {motion:?}"));
1678 ui.label(format!(
1679 "velocity: [{:3.0} {:3.0}] points/sec",
1680 velocity.x, velocity.y
1681 ));
1682 ui.label(format!("direction: {direction:?}"));
1683 ui.label(format!("down: {down:#?}"));
1684 ui.label(format!("press_origin: {press_origin:?}"));
1685 ui.label(format!("press_start_time: {press_start_time:?} s"));
1686 ui.label(format!(
1687 "has_moved_too_much_for_a_click: {has_moved_too_much_for_a_click}"
1688 ));
1689 ui.label(format!(
1690 "started_decidedly_dragging: {started_decidedly_dragging}"
1691 ));
1692 ui.label(format!("last_click_pos: {last_click_pos:#?}"));
1693 ui.label(format!("last_click_time: {last_click_time:#?}"));
1694 ui.label(format!("last_last_click_time: {last_last_click_time:#?}"));
1695 ui.label(format!("last_move_time: {last_move_time:#?}"));
1696 ui.label(format!("pointer_events: {pointer_events:?}"));
1697 }
1698}