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