iced_core/mouse/
cursor.rs1use crate::{Point, Rectangle, Transformation, Vector};
2
3#[derive(Debug, Clone, Copy, PartialEq, Default)]
5pub enum Cursor {
6 Available(Point),
8
9 Levitating(Point),
11
12 #[default]
14 Unavailable,
15}
16
17impl Cursor {
18 pub fn position(self) -> Option<Point> {
20 match self {
21 Cursor::Available(position) => Some(position),
22 Cursor::Levitating(_) | Cursor::Unavailable => None,
23 }
24 }
25
26 pub fn position_over(self, bounds: Rectangle) -> Option<Point> {
32 self.position().filter(|p| bounds.contains(*p))
33 }
34
35 pub fn position_in(self, bounds: Rectangle) -> Option<Point> {
41 self.position_over(bounds)
42 .map(|p| p - Vector::new(bounds.x, bounds.y))
43 }
44
45 pub fn position_from(self, origin: Point) -> Option<Point> {
48 self.position().map(|p| p - Vector::new(origin.x, origin.y))
49 }
50
51 pub fn is_over(self, bounds: Rectangle) -> bool {
53 self.position_over(bounds).is_some()
54 }
55
56 pub fn is_levitating(self) -> bool {
58 matches!(self, Self::Levitating(_))
59 }
60
61 pub fn levitate(self) -> Self {
63 match self {
64 Self::Available(position) => Self::Levitating(position),
65 _ => self,
66 }
67 }
68
69 pub fn land(self) -> Self {
71 match self {
72 Cursor::Levitating(position) => Cursor::Available(position),
73 _ => self,
74 }
75 }
76}
77
78impl std::ops::Add<Vector> for Cursor {
79 type Output = Self;
80
81 fn add(self, translation: Vector) -> Self::Output {
82 match self {
83 Cursor::Available(point) => Cursor::Available(point + translation),
84 Cursor::Levitating(point) => {
85 Cursor::Levitating(point + translation)
86 }
87 Cursor::Unavailable => Cursor::Unavailable,
88 }
89 }
90}
91
92impl std::ops::Sub<Vector> for Cursor {
93 type Output = Self;
94
95 fn sub(self, translation: Vector) -> Self::Output {
96 match self {
97 Cursor::Available(point) => Cursor::Available(point - translation),
98 Cursor::Levitating(point) => {
99 Cursor::Levitating(point - translation)
100 }
101 Cursor::Unavailable => Cursor::Unavailable,
102 }
103 }
104}
105
106impl std::ops::Mul<Transformation> for Cursor {
107 type Output = Self;
108
109 fn mul(self, transformation: Transformation) -> Self {
110 match self {
111 Self::Available(position) => {
112 Self::Available(position * transformation)
113 }
114 Self::Levitating(position) => {
115 Self::Levitating(position * transformation)
116 }
117 Self::Unavailable => Self::Unavailable,
118 }
119 }
120}