From 3a0d34c0240f4421737a6a08761f99d6f8140d02 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 4 Mar 2023 05:37:11 +0100 Subject: Create `iced_widget` subcrate and re-organize the whole codebase --- core/src/mouse/click.rs | 76 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 core/src/mouse/click.rs (limited to 'core/src/mouse') diff --git a/core/src/mouse/click.rs b/core/src/mouse/click.rs new file mode 100644 index 00000000..4a7d796c --- /dev/null +++ b/core/src/mouse/click.rs @@ -0,0 +1,76 @@ +//! Track mouse clicks. +use crate::time::Instant; +use crate::Point; + +/// A mouse click. +#[derive(Debug, Clone, Copy)] +pub struct Click { + kind: Kind, + position: Point, + time: Instant, +} + +/// The kind of mouse click. +#[derive(Debug, Clone, Copy)] +pub enum Kind { + /// A single click + Single, + + /// A double click + Double, + + /// A triple click + Triple, +} + +impl Kind { + fn next(&self) -> Kind { + match self { + Kind::Single => Kind::Double, + Kind::Double => Kind::Triple, + Kind::Triple => Kind::Double, + } + } +} + +impl Click { + /// Creates a new [`Click`] with the given position and previous last + /// [`Click`]. + pub fn new(position: Point, previous: Option) -> Click { + let time = Instant::now(); + + let kind = if let Some(previous) = previous { + if previous.is_consecutive(position, time) { + previous.kind.next() + } else { + Kind::Single + } + } else { + Kind::Single + }; + + Click { + kind, + position, + time, + } + } + + /// Returns the [`Kind`] of [`Click`]. + pub fn kind(&self) -> Kind { + self.kind + } + + fn is_consecutive(&self, new_position: Point, time: Instant) -> bool { + let duration = if time > self.time { + Some(time - self.time) + } else { + None + }; + + self.position == new_position + && duration + .map(|duration| duration.as_millis() <= 300) + .unwrap_or(false) + } +} -- cgit From 1816c985fad2ead8dc1e7b62dc8e4bafeed856b2 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 14 Mar 2023 11:11:17 +0100 Subject: Fix `clippy` lints for Rust 1.68 --- core/src/mouse/interaction.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) (limited to 'core/src/mouse') diff --git a/core/src/mouse/interaction.rs b/core/src/mouse/interaction.rs index 664147a7..57da93fe 100644 --- a/core/src/mouse/interaction.rs +++ b/core/src/mouse/interaction.rs @@ -1,7 +1,8 @@ /// The interaction of a mouse cursor. -#[derive(Debug, Eq, PartialEq, Clone, Copy, PartialOrd, Ord)] +#[derive(Debug, Eq, PartialEq, Clone, Copy, PartialOrd, Ord, Default)] #[allow(missing_docs)] pub enum Interaction { + #[default] Idle, Pointer, Grab, @@ -12,9 +13,3 @@ pub enum Interaction { ResizingHorizontally, ResizingVertically, } - -impl Default for Interaction { - fn default() -> Interaction { - Interaction::Idle - } -} -- cgit From 7e7e66586d990788ffd77b17e98357e74252f497 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Apr 2023 04:37:39 +0200 Subject: Show `NotAllowed` as mouse icon when hovering a disabled `TextInput` --- core/src/mouse/interaction.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'core/src/mouse') diff --git a/core/src/mouse/interaction.rs b/core/src/mouse/interaction.rs index 57da93fe..072033fd 100644 --- a/core/src/mouse/interaction.rs +++ b/core/src/mouse/interaction.rs @@ -12,4 +12,5 @@ pub enum Interaction { Grabbing, ResizingHorizontally, ResizingVertically, + NotAllowed, } -- cgit From 5802c957972ab972567d7f9a2f35a49bd9fb2cc3 Mon Sep 17 00:00:00 2001 From: bbb651 Date: Sat, 15 Apr 2023 00:45:30 +0300 Subject: Make mouse::Button::Other take u16 instead of u8 On wayland keys correspond to , and they are past the limit of u8, causing the back and forward buttons to be 20 and 19 which definitely isn't right (they should all be around 0x110..=0x117). --- core/src/mouse/button.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/src/mouse') diff --git a/core/src/mouse/button.rs b/core/src/mouse/button.rs index aeb8a55d..3eec7f42 100644 --- a/core/src/mouse/button.rs +++ b/core/src/mouse/button.rs @@ -11,5 +11,5 @@ pub enum Button { Middle, /// Some other button. - Other(u8), + Other(u16), } -- cgit From 34451bff185d8875f55747ee97ed746828e30f40 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 8 Jun 2023 20:11:59 +0200 Subject: Implement basic cursor availability --- core/src/mouse/cursor.rs | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 core/src/mouse/cursor.rs (limited to 'core/src/mouse') diff --git a/core/src/mouse/cursor.rs b/core/src/mouse/cursor.rs new file mode 100644 index 00000000..d4cb415d --- /dev/null +++ b/core/src/mouse/cursor.rs @@ -0,0 +1,51 @@ +use crate::{Point, Rectangle, Vector}; + +/// The mouse cursor state. +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum Cursor { + /// The cursor has a defined position. + Available(Point), + + /// The cursor is currently unavailable (i.e. out of bounds or busy). + Unavailable, +} + +impl Cursor { + /// Returns the absolute position of the [`Cursor`], if available. + pub fn position(self) -> Option { + match self { + Cursor::Available(position) => Some(position), + Cursor::Unavailable => None, + } + } + + /// Returns the absolute position of the [`Cursor`], if available and inside + /// the given bounds. + /// + /// If the [`Cursor`] is not over the provided bounds, this method will + /// return `None`. + pub fn position_over(self, bounds: &Rectangle) -> Option { + self.position().filter(|p| bounds.contains(*p)) + } + + /// Returns the relative position of the [`Cursor`] inside the given bounds, + /// if available. + /// + /// If the [`Cursor`] is not over the provided bounds, this method will + /// return `None`. + pub fn position_in(self, bounds: &Rectangle) -> Option { + self.position_over(bounds) + .map(|p| p - Vector::new(bounds.x, bounds.y)) + } + + /// Returns the relative position of the [`Cursor`] from the given origin, + /// if available. + pub fn position_from(self, origin: Point) -> Option { + self.position().map(|p| p - Vector::new(origin.x, origin.y)) + } + + /// Returns true if the [`Cursor`] is over the given `bounds`. + pub fn is_over(self, bounds: &Rectangle) -> bool { + self.position_over(bounds).is_some() + } +} -- cgit From 5c8cfb411ed0a9a6e55bd1193cd7e97252e63d28 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 8 Jun 2023 20:16:46 +0200 Subject: Take `Rectangle` by value in `Cursor` API --- core/src/mouse/cursor.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'core/src/mouse') diff --git a/core/src/mouse/cursor.rs b/core/src/mouse/cursor.rs index d4cb415d..223ed96d 100644 --- a/core/src/mouse/cursor.rs +++ b/core/src/mouse/cursor.rs @@ -24,7 +24,7 @@ impl Cursor { /// /// If the [`Cursor`] is not over the provided bounds, this method will /// return `None`. - pub fn position_over(self, bounds: &Rectangle) -> Option { + pub fn position_over(self, bounds: Rectangle) -> Option { self.position().filter(|p| bounds.contains(*p)) } @@ -33,7 +33,7 @@ impl Cursor { /// /// If the [`Cursor`] is not over the provided bounds, this method will /// return `None`. - pub fn position_in(self, bounds: &Rectangle) -> Option { + pub fn position_in(self, bounds: Rectangle) -> Option { self.position_over(bounds) .map(|p| p - Vector::new(bounds.x, bounds.y)) } @@ -45,7 +45,7 @@ impl Cursor { } /// Returns true if the [`Cursor`] is over the given `bounds`. - pub fn is_over(self, bounds: &Rectangle) -> bool { + pub fn is_over(self, bounds: Rectangle) -> bool { self.position_over(bounds).is_some() } } -- cgit From aba98e49654852281ed17bedd1becac6f9db8700 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 8 Jun 2023 20:35:40 +0200 Subject: Extend cursor availability to the shell level --- core/src/mouse/cursor.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'core/src/mouse') diff --git a/core/src/mouse/cursor.rs b/core/src/mouse/cursor.rs index 223ed96d..203526e9 100644 --- a/core/src/mouse/cursor.rs +++ b/core/src/mouse/cursor.rs @@ -1,12 +1,13 @@ use crate::{Point, Rectangle, Vector}; /// The mouse cursor state. -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, Copy, PartialEq, Default)] pub enum Cursor { /// The cursor has a defined position. Available(Point), /// The cursor is currently unavailable (i.e. out of bounds or busy). + #[default] Unavailable, } -- cgit