Skip to main content

freya_core/events/
data.rs

1use std::{
2    cell::RefCell,
3    ops::{
4        Deref,
5        Div,
6    },
7    path::PathBuf,
8    rc::Rc,
9};
10
11use torin::prelude::{
12    Area,
13    CursorPoint,
14    Size2D,
15};
16
17use crate::data::{
18    StyleState,
19    TextStyleState,
20};
21
22#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
23pub enum MouseButton {
24    Left,
25    Right,
26    Middle,
27    Back,
28    Forward,
29    Other(u16),
30}
31
32#[derive(Debug, Clone, PartialEq, Default)]
33pub struct MouseEventData {
34    pub global_location: CursorPoint,
35    pub element_location: CursorPoint,
36    pub button: Option<MouseButton>,
37}
38
39/// Data of a Keyboard event.
40#[derive(Debug, Clone, PartialEq)]
41pub struct KeyboardEventData {
42    pub key: keyboard_types::Key,
43    pub code: keyboard_types::Code,
44    pub modifiers: keyboard_types::Modifiers,
45}
46
47impl KeyboardEventData {
48    pub fn new(
49        key: keyboard_types::Key,
50        code: keyboard_types::Code,
51        modifiers: keyboard_types::Modifiers,
52    ) -> Self {
53        Self {
54            key,
55            code,
56            modifiers,
57        }
58    }
59}
60
61impl KeyboardEventData {
62    /// Try to get the text of the key
63    pub fn try_as_str(&self) -> Option<&str> {
64        if let keyboard_types::Key::Character(c) = &self.key {
65            Some(c)
66        } else {
67            None
68        }
69    }
70}
71
72pub struct Event<D> {
73    pub(crate) data: D,
74    pub(crate) propagate: Rc<RefCell<bool>>,
75    pub(crate) default: Rc<RefCell<bool>>,
76}
77
78impl<D> Deref for Event<D> {
79    type Target = D;
80
81    fn deref(&self) -> &Self::Target {
82        &self.data
83    }
84}
85
86impl<D> Event<D> {
87    pub fn map<NewD>(self, data: impl FnOnce(D) -> NewD) -> Event<NewD> {
88        Event {
89            data: data(self.data),
90            propagate: self.propagate,
91            default: self.default,
92        }
93    }
94
95    pub fn try_map<NewD>(self, data: impl FnOnce(D) -> Option<NewD>) -> Option<Event<NewD>> {
96        Some(Event {
97            data: data(self.data)?,
98            propagate: self.propagate,
99            default: self.default,
100        })
101    }
102
103    pub fn data(&self) -> &D {
104        &self.data
105    }
106
107    pub fn stop_propagation(&self) {
108        *self.propagate.borrow_mut() = false;
109    }
110
111    pub fn prevent_default(&self) {
112        *self.default.borrow_mut() = false;
113    }
114
115    #[must_use]
116    pub fn get_prevent_default(&self) -> Rc<RefCell<bool>> {
117        self.default.clone()
118    }
119}
120
121/// Data of a Sized event.
122#[derive(Debug, Clone, PartialEq, Default)]
123pub struct SizedEventData {
124    pub area: Area,
125    pub visible_area: Area,
126    pub inner_sizes: Size2D,
127}
128
129impl SizedEventData {
130    pub fn div(&mut self, rhs: f32) {
131        self.area = self.area.div(rhs);
132        self.visible_area = self.visible_area.div(rhs);
133        self.inner_sizes = self.inner_sizes.div(rhs);
134    }
135}
136
137impl SizedEventData {
138    pub fn new(area: Area, visible_area: Area, inner_sizes: Size2D) -> Self {
139        Self {
140            area,
141            visible_area,
142            inner_sizes,
143        }
144    }
145}
146
147/// Data of a Styled event.
148#[derive(Debug, Clone, PartialEq)]
149pub struct StyledEventData {
150    pub style: StyleState,
151    pub text_style: TextStyleState,
152}
153
154#[derive(Debug, Clone, PartialEq, Copy)]
155pub enum WheelSource {
156    Device,
157    Custom,
158}
159
160/// Data of a Wheel event.
161#[derive(Debug, Clone, PartialEq)]
162pub struct WheelEventData {
163    pub source: WheelSource,
164    pub delta_x: f64,
165    pub delta_y: f64,
166    pub global_location: CursorPoint,
167    pub element_location: CursorPoint,
168}
169
170impl WheelEventData {
171    pub fn new(
172        delta_x: f64,
173        delta_y: f64,
174        source: WheelSource,
175        global_location: CursorPoint,
176        element_location: CursorPoint,
177    ) -> Self {
178        Self {
179            delta_x,
180            delta_y,
181            source,
182            global_location,
183            element_location,
184        }
185    }
186}
187
188#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
189pub enum TouchPhase {
190    Started,
191    Moved,
192    Ended,
193    Cancelled,
194}
195
196#[derive(Debug, Clone, Copy, PartialEq)]
197pub enum Force {
198    Calibrated {
199        force: f64,
200        max_possible_force: f64,
201        altitude_angle: Option<f64>,
202    },
203    Normalized(f64),
204}
205
206/// Data of a Touch event.
207#[derive(Debug, Clone, PartialEq)]
208pub struct TouchEventData {
209    pub global_location: CursorPoint,
210    pub element_location: CursorPoint,
211    pub finger_id: u64,
212    pub phase: TouchPhase,
213    pub force: Option<Force>,
214}
215
216impl TouchEventData {
217    pub fn new(
218        global_location: CursorPoint,
219        element_location: CursorPoint,
220        finger_id: u64,
221        phase: TouchPhase,
222        force: Option<Force>,
223    ) -> Self {
224        Self {
225            global_location,
226            element_location,
227            finger_id,
228            phase,
229            force,
230        }
231    }
232}
233
234/// Data of a pointer event.
235#[derive(Debug, Clone, PartialEq)]
236pub enum PointerEventData {
237    Mouse(MouseEventData),
238    Touch(TouchEventData),
239}
240
241impl PointerEventData {
242    pub fn global_location(&self) -> CursorPoint {
243        match self {
244            Self::Mouse(m) => m.global_location,
245            Self::Touch(t) => t.global_location,
246        }
247    }
248
249    pub fn element_location(&self) -> CursorPoint {
250        match self {
251            Self::Mouse(m) => m.element_location,
252            Self::Touch(t) => t.element_location,
253        }
254    }
255
256    pub fn button(&self) -> Option<MouseButton> {
257        match self {
258            Self::Mouse(m) => m.button,
259            Self::Touch(_) => None,
260        }
261    }
262
263    /// Whether this is a touch event or a primary (left) mouse button event.
264    pub fn is_primary(&self) -> bool {
265        match self {
266            Self::Mouse(m) => m.button == Some(MouseButton::Left),
267            Self::Touch(_) => true,
268        }
269    }
270}
271
272#[derive(Debug, Clone, PartialEq)]
273pub struct ImePreeditEventData {
274    pub text: String,
275    pub cursor: Option<(usize, usize)>,
276}
277
278impl ImePreeditEventData {
279    pub(crate) fn new(text: String, cursor: Option<(usize, usize)>) -> Self {
280        Self { text, cursor }
281    }
282}
283
284#[derive(Debug, Clone, PartialEq)]
285pub struct FileEventData {
286    pub cursor: CursorPoint,
287    pub file_path: Option<PathBuf>,
288}
289
290impl FileEventData {
291    pub(crate) fn new(cursor: CursorPoint, file_path: Option<PathBuf>) -> Self {
292        Self { cursor, file_path }
293    }
294}
295
296#[derive(Debug, Clone, PartialEq)]
297pub enum EventType {
298    Mouse(MouseEventData),
299    Keyboard(KeyboardEventData),
300    Sized(SizedEventData),
301    Styled(StyledEventData),
302    Wheel(WheelEventData),
303    Touch(TouchEventData),
304    Pointer(PointerEventData),
305    ImePreedit(ImePreeditEventData),
306    File(FileEventData),
307}