summaryrefslogtreecommitdiffstats
path: root/core/src/mouse
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector0193@gmail.com>2023-06-09 21:53:54 +0200
committerLibravatar GitHub <noreply@github.com>2023-06-09 21:53:54 +0200
commit60cd864d43be877a5eaee4f5ca32f41b9fb70b79 (patch)
tree01a7758d3b8a4d2434bba113df85c6cb2c3e2ca2 /core/src/mouse
parentc15f1b5f6575792cc89bb5fba2e613428397e46a (diff)
parent27639c4ce6161fa07986c2f1d472a8a259ae2129 (diff)
downloadiced-60cd864d43be877a5eaee4f5ca32f41b9fb70b79.tar.gz
iced-60cd864d43be877a5eaee4f5ca32f41b9fb70b79.tar.bz2
iced-60cd864d43be877a5eaee4f5ca32f41b9fb70b79.zip
Merge pull request #1904 from iced-rs/cursor-availability
Cursor availability
Diffstat (limited to 'core/src/mouse')
-rw-r--r--core/src/mouse/cursor.rs52
1 files changed, 52 insertions, 0 deletions
diff --git a/core/src/mouse/cursor.rs b/core/src/mouse/cursor.rs
new file mode 100644
index 00000000..203526e9
--- /dev/null
+++ b/core/src/mouse/cursor.rs
@@ -0,0 +1,52 @@
+use crate::{Point, Rectangle, Vector};
+
+/// The mouse cursor state.
+#[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,
+}
+
+impl Cursor {
+ /// Returns the absolute position of the [`Cursor`], if available.
+ pub fn position(self) -> Option<Point> {
+ 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<Point> {
+ 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<Point> {
+ 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<Point> {
+ 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()
+ }
+}