freya_elements/events/mouse.rs
1use torin::geometry::CursorPoint;
2pub use winit::event::MouseButton;
3
4use crate::{
5 events::ErasedEventData,
6 impl_event,
7};
8impl_event! [
9 MouseData;
10
11 /// The `click` event fires when the user starts and ends a click in an element with the left button of the mouse.
12 ///
13 /// Event Data: [`MouseData`](crate::events::MouseData)
14 ///
15 /// ### Example
16 ///
17 /// ```rust, no_run
18 /// # use freya::prelude::*;
19 /// fn app() -> Element {
20 /// rsx!(
21 /// rect {
22 /// width: "100",
23 /// height: "100",
24 /// background: "red",
25 /// onclick: |_| println!("Clicked!")
26 /// }
27 /// )
28 /// }
29 /// ```
30 onclick
31
32 /// The `globalclick` event fires when the user clicks anywhere.
33 /// Note that this fires for all mouse buttons.
34 /// You can check the specific variant with the [`MouseData`](crate::events::MouseData)'s `trigger_button` property.
35 ///
36 /// Event Data: [`MouseData`](crate::events::MouseData)
37 ///
38 /// ### Example
39 ///
40 /// ```rust, no_run
41 /// # use freya::prelude::*;
42 /// fn app() -> Element {
43 /// rsx!(
44 /// rect {
45 /// onglobalclick: |_| println!("Clicked somewhere else!")
46 /// }
47 /// rect {
48 /// width: "100",
49 /// height: "100",
50 /// background: "red",
51 /// onclick: |_| println!("Clicked!")
52 /// }
53 /// )
54 /// }
55 /// ```
56 onglobalclick
57
58 /// The `click` event fires when the user clicks an element with the middle button of the mouse.
59 ///
60 /// Event Data: [`MouseData`](crate::events::MouseData)
61 ///
62 /// ### Example
63 ///
64 /// ```rust, no_run
65 /// # use freya::prelude::*;
66 /// fn app() -> Element {
67 /// rsx!(
68 /// rect {
69 /// width: "100",
70 /// height: "100",
71 /// background: "red",
72 /// onmiddleclick: |_| println!("Clicked!")
73 /// }
74 /// )
75 /// }
76 /// ```
77 onmiddleclick
78
79 /// The `click` event fires when the user clicks an element with the right button of the mouse.
80 ///
81 /// Event Data: [`MouseData`](crate::events::MouseData)
82 ///
83 /// ### Example
84 ///
85 /// ```rust, no_run
86 /// # use freya::prelude::*;
87 /// fn app() -> Element {
88 /// rsx!(
89 /// rect {
90 /// width: "100",
91 /// height: "100",
92 /// background: "red",
93 /// onrightclick: |_| println!("Clicked!")
94 /// }
95 /// )
96 /// }
97 /// ```
98 onrightclick
99
100 /// The `mouseup` event fires when the user ends the click in an element with the left button of the mouse.
101 ///
102 /// Event Data: [`MouseData`](crate::events::MouseData)
103 ///
104 /// ### Example
105 ///
106 /// ```rust, no_run
107 /// # use freya::prelude::*;
108 /// fn app() -> Element {
109 /// rsx!(
110 /// rect {
111 /// width: "100",
112 /// height: "100",
113 /// background: "red",
114 /// onmouseup: |_| println!("Clicked!")
115 /// }
116 /// )
117 /// }
118 /// ```
119 onmouseup
120
121 /// The `mousedown` event fires when the user starts clicking an element.
122 /// Note that this fires for all mouse buttons.
123 /// You can check the specific variant with the [`MouseData`](crate::events::MouseData)'s `trigger_button` property.
124 ///
125 /// Event Data: [`MouseData`](crate::events::MouseData)
126 ///
127 /// ### Example
128 ///
129 /// ```rust, no_run
130 /// # use freya::prelude::*;
131 /// fn app() -> Element {
132 /// rsx!(
133 /// rect {
134 /// width: "100",
135 /// height: "100",
136 /// background: "red",
137 /// onmousedown: |_| println!("Started clicking!")
138 /// }
139 /// )
140 /// }
141 /// ```
142 onmousedown
143
144 /// The `globalmousedown` event fires when the user starts clicking anywhere.
145 /// Note that this fires for all mouse buttons.
146 /// You can check the specific variant with the [`MouseData`](crate::events::MouseData)'s `trigger_button` property.
147 ///
148 /// Event Data: [`MouseData`](crate::events::MouseData)
149 ///
150 /// ### Example
151 ///
152 /// ```rust, no_run
153 /// # use freya::prelude::*;
154 /// fn app() -> Element {
155 /// rsx!(
156 /// rect {
157 /// onglobalmousedown: |_| println!("Started clicking somewhere else!")
158 /// }
159 /// rect {
160 /// width: "100",
161 /// height: "100",
162 /// background: "red",
163 /// onmousedown: |_| println!("Started clicking!")
164 /// }
165 /// )
166 /// }
167 /// ```
168 onglobalmousedown
169
170 /// The `mousemove` event fires when the user moves the mouse over an element.
171 /// Unlike [`onmouseenter`](crate::events::onmouseenter()), this fires even if the user was already hovering over
172 /// the element. For that reason, it's less efficient.
173 ///
174 /// Event Data: [`MouseData`](crate::events::MouseData)
175 ///
176 /// ### Example
177 ///
178 /// ```rust, no_run
179 /// # use freya::prelude::*;
180 /// fn app() -> Element {
181 /// rsx!(
182 /// rect {
183 /// width: "100",
184 /// height: "100",
185 /// background: "red",
186 /// onmousemove: |_| println!("Hovering!")
187 /// }
188 /// )
189 /// }
190 /// ```
191 onmousemove
192
193 /// The `globalmousemove` event fires when the user moves the mouse anywhere in the app.
194 ///
195 /// Event Data: [`MouseData`](crate::events::MouseData)
196 ///
197 /// ### Example
198 ///
199 /// ```rust, no_run
200 /// # use freya::prelude::*;
201 /// fn app() -> Element {
202 /// rsx!(
203 /// rect {
204 /// onglobalmousemove: |_| println!("Moving the mouse anywhere!")
205 /// }
206 /// rect {
207 /// width: "100",
208 /// height: "100",
209 /// background: "red",
210 /// onmousemove: |_| println!("Moving the mouse here!")
211 /// }
212 /// )
213 /// }
214 /// ```
215 onglobalmousemove
216
217 /// The `mouseleave` event fires when the user stops hovering an element.
218 ///
219 /// Event Data: [`MouseData`](crate::events::MouseData)
220 ///
221 /// ### Example
222 ///
223 /// ```rust, no_run
224 /// # use freya::prelude::*;
225 /// fn app() -> Element {
226 /// rsx!(
227 /// rect {
228 /// width: "100",
229 /// height: "100",
230 /// background: "red",
231 /// onmouseleave: |_| println!("Stopped hovering!")
232 /// }
233 /// )
234 /// }
235 /// ```
236 onmouseleave
237
238 /// The `mouseenter` event fires when the user starts hovering an element.
239 ///
240 /// Event Data: [`MouseData`](crate::events::MouseData)
241 ///
242 /// ### Example
243 ///
244 /// ```rust, no_run
245 /// # use freya::prelude::*;
246 /// fn app() -> Element {
247 /// rsx!(
248 /// rect {
249 /// width: "100",
250 /// height: "100",
251 /// background: "red",
252 /// onmouseenter: |_| println!("Started hovering!")
253 /// }
254 /// )
255 /// }
256 /// ```
257 onmouseenter
258];
259
260/// Data of a Mouse event.
261#[derive(Debug, Clone, PartialEq)]
262pub struct MouseData {
263 pub screen_coordinates: CursorPoint,
264 pub element_coordinates: CursorPoint,
265 pub trigger_button: Option<MouseButton>,
266}
267
268impl MouseData {
269 pub fn new(
270 screen_coordinates: CursorPoint,
271 element_coordinates: CursorPoint,
272 trigger_button: Option<MouseButton>,
273 ) -> Self {
274 Self {
275 screen_coordinates,
276 element_coordinates,
277 trigger_button,
278 }
279 }
280}
281
282impl MouseData {
283 /// Get the mouse coordinates relative to the window bounds.
284 pub fn get_screen_coordinates(&self) -> CursorPoint {
285 self.screen_coordinates
286 }
287
288 /// Get the mouse coordinates relatives to the element bounds.
289 pub fn get_element_coordinates(&self) -> CursorPoint {
290 self.element_coordinates
291 }
292
293 /// Get the button that triggered this event.
294 pub fn get_trigger_button(&self) -> Option<MouseButton> {
295 self.trigger_button
296 }
297}
298
299impl From<&ErasedEventData> for MouseData {
300 fn from(val: &ErasedEventData) -> Self {
301 val.downcast::<MouseData>().cloned().unwrap()
302 }
303}