Skip to content

Commit

Permalink
Add Pointer::is_decidedly_dragging and could_any_button_be_click (#…
Browse files Browse the repository at this point in the history
…2979)

* Add Pointer::is_decidedly_dragging and could_any_button_be_click

This allows users to distinguish between click and drags
while they are not yet done.

* Fix warning in eframe

* fix typo
  • Loading branch information
emilk authored May 8, 2023
1 parent faf3136 commit e9fa6c8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions crates/eframe/src/native/epi_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ impl EpiIntegration {
}
}

#[allow(clippy::unused_self)]
pub fn save(&mut self, _app: &mut dyn epi::App, _window: &winit::window::Window) {
#[cfg(feature = "persistence")]
if let Some(storage) = self.frame.storage_mut() {
Expand Down
21 changes: 19 additions & 2 deletions crates/egui/src/input_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,8 +894,9 @@ impl PointerState {
}

/// If the pointer button is down, will it register as a click when released?
#[inline(always)]
pub(crate) fn could_any_button_be_click(&self) -> bool {
///
/// See also [`Self::is_decidedly_dragging`].
pub fn could_any_button_be_click(&self) -> bool {
if !self.any_down() {
return false;
}
Expand All @@ -913,6 +914,22 @@ impl PointerState {
true
}

/// Just because the mouse is down doesn't mean we are dragging.
/// We could be at the start of a click.
/// But if the mouse is down long enough, or has moved far enough,
/// then we consider it a drag.
///
/// This function can return true on the same frame the drag is released,
/// but NOT on the first frame it was started.
///
/// See also [`Self::could_any_button_be_click`].
pub fn is_decidedly_dragging(&self) -> bool {
(self.any_down() || self.any_released())
&& !self.any_pressed()
&& !self.could_any_button_be_click()
&& !self.any_click()
}

/// Is the primary button currently down?
#[inline(always)]
pub fn primary_down(&self) -> bool {
Expand Down
5 changes: 5 additions & 0 deletions crates/egui/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,11 @@ impl Memory {
self.interaction.drag_id = Some(id);
}

#[inline(always)]
pub fn stop_dragging(&mut self) {
self.interaction.drag_id = None;
}

/// Forget window positions, sizes etc.
/// Can be used to auto-layout windows.
pub fn reset_areas(&mut self) {
Expand Down

0 comments on commit e9fa6c8

Please sign in to comment.