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
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<NewD>(self, data: impl FnOnce(D) -> NewD) -> Event<NewD> {
83        Event {
84            data: data(self.data),
85            propagate: self.propagate,
86            default: self.default,
87        }
88    }
89
90    pub fn try_map<NewD>(self, data: impl FnOnce(D) -> Option<NewD>) -> Option<Event<NewD>> {
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    #[must_use]
111    pub fn get_prevent_default(&self) -> Rc<RefCell<bool>> {
112        self.default.clone()
113    }
114}
115
116/// Data of a Sized event.
117#[derive(Debug, Clone, PartialEq, Default)]
118pub struct SizedEventData {
119    pub area: Area,
120    pub visible_area: Area,
121    pub inner_sizes: Size2D,
122}
123
124impl SizedEventData {
125    pub fn div(&mut self, rhs: f32) {
126        self.area = self.area.div(rhs);
127        self.visible_area = self.visible_area.div(rhs);
128        self.inner_sizes = self.inner_sizes.div(rhs);
129    }
130}
131
132impl SizedEventData {
133    pub fn new(area: Area, visible_area: Area, inner_sizes: Size2D) -> Self {
134        Self {
135            area,
136            visible_area,
137            inner_sizes,
138        }
139    }
140}
141
142#[derive(Debug, Clone, PartialEq, Copy)]
143pub enum WheelSource {
144    Device,
145    Custom,
146}
147
148/// Data of a Wheel event.
149#[derive(Debug, Clone, PartialEq)]
150pub struct WheelEventData {
151    pub source: WheelSource,
152    pub delta_x: f64,
153    pub delta_y: f64,
154}
155
156impl WheelEventData {
157    pub fn new(delta_x: f64, delta_y: f64, source: WheelSource) -> Self {
158        Self {
159            delta_x,
160            delta_y,
161            source,
162        }
163    }
164}
165
166#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
167pub enum TouchPhase {
168    Started,
169    Moved,
170    Ended,
171    Cancelled,
172}
173
174#[derive(Debug, Clone, Copy, PartialEq)]
175pub enum Force {
176    Calibrated {
177        force: f64,
178        max_possible_force: f64,
179        altitude_angle: Option<f64>,
180    },
181    Normalized(f64),
182}
183
184/// Data of a Touch event.
185#[derive(Debug, Clone, PartialEq)]
186pub struct TouchEventData {
187    pub global_location: CursorPoint,
188    pub element_location: CursorPoint,
189    pub finger_id: u64,
190    pub phase: TouchPhase,
191    pub force: Option<Force>,
192}
193
194impl TouchEventData {
195    pub fn new(
196        global_location: CursorPoint,
197        element_location: CursorPoint,
198        finger_id: u64,
199        phase: TouchPhase,
200        force: Option<Force>,
201    ) -> Self {
202        Self {
203            global_location,
204            element_location,
205            finger_id,
206            phase,
207            force,
208        }
209    }
210}
211
212/// Data of a pointer event.
213#[derive(Debug, Clone, PartialEq)]
214pub enum PointerEventData {
215    Mouse(MouseEventData),
216    Touch(TouchEventData),
217}
218
219impl PointerEventData {
220    pub fn global_location(&self) -> CursorPoint {
221        match self {
222            Self::Mouse(m) => m.global_location,
223            Self::Touch(t) => t.global_location,
224        }
225    }
226
227    pub fn element_location(&self) -> CursorPoint {
228        match self {
229            Self::Mouse(m) => m.element_location,
230            Self::Touch(t) => t.element_location,
231        }
232    }
233
234    pub fn button(&self) -> Option<MouseButton> {
235        match self {
236            Self::Mouse(m) => m.button,
237            Self::Touch(_) => None,
238        }
239    }
240}
241
242#[derive(Debug, Clone, PartialEq)]
243pub struct ImePreeditEventData {
244    pub text: String,
245    pub cursor: Option<(usize, usize)>,
246}
247
248impl ImePreeditEventData {
249    pub(crate) fn new(text: String, cursor: Option<(usize, usize)>) -> Self {
250        Self { text, cursor }
251    }
252}
253
254#[derive(Debug, Clone, PartialEq)]
255pub struct FileEventData {
256    pub cursor: CursorPoint,
257    pub file_path: Option<PathBuf>,
258}
259
260impl FileEventData {
261    pub(crate) fn new(cursor: CursorPoint, file_path: Option<PathBuf>) -> Self {
262        Self { cursor, file_path }
263    }
264}
265
266#[derive(Debug, Clone, PartialEq)]
267pub enum EventType {
268    Mouse(MouseEventData),
269    Keyboard(KeyboardEventData),
270    Sized(SizedEventData),
271    Wheel(WheelEventData),
272    Touch(TouchEventData),
273    Pointer(PointerEventData),
274    ImePreedit(ImePreeditEventData),
275    File(FileEventData),
276}