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#[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 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#[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#[derive(Debug, Clone, PartialEq)]
150pub struct WheelEventData {
151 pub source: WheelSource,
152 pub delta_x: f64,
153 pub delta_y: f64,
154 pub global_location: CursorPoint,
155 pub element_location: CursorPoint,
156}
157
158impl WheelEventData {
159 pub fn new(
160 delta_x: f64,
161 delta_y: f64,
162 source: WheelSource,
163 global_location: CursorPoint,
164 element_location: CursorPoint,
165 ) -> Self {
166 Self {
167 delta_x,
168 delta_y,
169 source,
170 global_location,
171 element_location,
172 }
173 }
174}
175
176#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
177pub enum TouchPhase {
178 Started,
179 Moved,
180 Ended,
181 Cancelled,
182}
183
184#[derive(Debug, Clone, Copy, PartialEq)]
185pub enum Force {
186 Calibrated {
187 force: f64,
188 max_possible_force: f64,
189 altitude_angle: Option<f64>,
190 },
191 Normalized(f64),
192}
193
194#[derive(Debug, Clone, PartialEq)]
196pub struct TouchEventData {
197 pub global_location: CursorPoint,
198 pub element_location: CursorPoint,
199 pub finger_id: u64,
200 pub phase: TouchPhase,
201 pub force: Option<Force>,
202}
203
204impl TouchEventData {
205 pub fn new(
206 global_location: CursorPoint,
207 element_location: CursorPoint,
208 finger_id: u64,
209 phase: TouchPhase,
210 force: Option<Force>,
211 ) -> Self {
212 Self {
213 global_location,
214 element_location,
215 finger_id,
216 phase,
217 force,
218 }
219 }
220}
221
222#[derive(Debug, Clone, PartialEq)]
224pub enum PointerEventData {
225 Mouse(MouseEventData),
226 Touch(TouchEventData),
227}
228
229impl PointerEventData {
230 pub fn global_location(&self) -> CursorPoint {
231 match self {
232 Self::Mouse(m) => m.global_location,
233 Self::Touch(t) => t.global_location,
234 }
235 }
236
237 pub fn element_location(&self) -> CursorPoint {
238 match self {
239 Self::Mouse(m) => m.element_location,
240 Self::Touch(t) => t.element_location,
241 }
242 }
243
244 pub fn button(&self) -> Option<MouseButton> {
245 match self {
246 Self::Mouse(m) => m.button,
247 Self::Touch(_) => None,
248 }
249 }
250}
251
252#[derive(Debug, Clone, PartialEq)]
253pub struct ImePreeditEventData {
254 pub text: String,
255 pub cursor: Option<(usize, usize)>,
256}
257
258impl ImePreeditEventData {
259 pub(crate) fn new(text: String, cursor: Option<(usize, usize)>) -> Self {
260 Self { text, cursor }
261 }
262}
263
264#[derive(Debug, Clone, PartialEq)]
265pub struct FileEventData {
266 pub cursor: CursorPoint,
267 pub file_path: Option<PathBuf>,
268}
269
270impl FileEventData {
271 pub(crate) fn new(cursor: CursorPoint, file_path: Option<PathBuf>) -> Self {
272 Self { cursor, file_path }
273 }
274}
275
276#[derive(Debug, Clone, PartialEq)]
277pub enum EventType {
278 Mouse(MouseEventData),
279 Keyboard(KeyboardEventData),
280 Sized(SizedEventData),
281 Wheel(WheelEventData),
282 Touch(TouchEventData),
283 Pointer(PointerEventData),
284 ImePreedit(ImePreeditEventData),
285 File(FileEventData),
286}