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