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#[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 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#[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#[derive(Debug, Clone, PartialEq, Default)]
149pub struct VisibleEventData {
150 pub area: Area,
151}
152
153impl VisibleEventData {
154 pub fn new(area: Area) -> Self {
155 Self { area }
156 }
157
158 pub fn div(&mut self, rhs: f32) {
159 self.area = self.area.div(rhs);
160 }
161}
162
163#[derive(Debug, Clone, PartialEq)]
165pub struct StyledEventData {
166 pub style: StyleState,
167 pub text_style: TextStyleState,
168}
169
170#[derive(Debug, Clone, PartialEq, Copy)]
171pub enum WheelSource {
172 Device,
173 Custom,
174}
175
176#[derive(Debug, Clone, PartialEq)]
178pub struct WheelEventData {
179 pub source: WheelSource,
180 pub delta_x: f64,
181 pub delta_y: f64,
182 pub global_location: CursorPoint,
183 pub element_location: CursorPoint,
184}
185
186impl WheelEventData {
187 pub fn new(
188 delta_x: f64,
189 delta_y: f64,
190 source: WheelSource,
191 global_location: CursorPoint,
192 element_location: CursorPoint,
193 ) -> Self {
194 Self {
195 delta_x,
196 delta_y,
197 source,
198 global_location,
199 element_location,
200 }
201 }
202}
203
204#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
205pub enum TouchPhase {
206 Started,
207 Moved,
208 Ended,
209 Cancelled,
210}
211
212#[derive(Debug, Clone, Copy, PartialEq)]
213pub enum Force {
214 Calibrated {
215 force: f64,
216 max_possible_force: f64,
217 altitude_angle: Option<f64>,
218 },
219 Normalized(f64),
220}
221
222#[derive(Debug, Clone, PartialEq)]
224pub struct TouchEventData {
225 pub global_location: CursorPoint,
226 pub element_location: CursorPoint,
227 pub finger_id: u64,
228 pub phase: TouchPhase,
229 pub force: Option<Force>,
230}
231
232impl TouchEventData {
233 pub fn new(
234 global_location: CursorPoint,
235 element_location: CursorPoint,
236 finger_id: u64,
237 phase: TouchPhase,
238 force: Option<Force>,
239 ) -> Self {
240 Self {
241 global_location,
242 element_location,
243 finger_id,
244 phase,
245 force,
246 }
247 }
248}
249
250#[derive(Debug, Clone, PartialEq)]
252pub enum PointerEventData {
253 Mouse(MouseEventData),
254 Touch(TouchEventData),
255}
256
257impl PointerEventData {
258 pub fn global_location(&self) -> CursorPoint {
259 match self {
260 Self::Mouse(m) => m.global_location,
261 Self::Touch(t) => t.global_location,
262 }
263 }
264
265 pub fn element_location(&self) -> CursorPoint {
266 match self {
267 Self::Mouse(m) => m.element_location,
268 Self::Touch(t) => t.element_location,
269 }
270 }
271
272 pub fn button(&self) -> Option<MouseButton> {
273 match self {
274 Self::Mouse(m) => m.button,
275 Self::Touch(_) => None,
276 }
277 }
278
279 pub fn is_primary(&self) -> bool {
281 match self {
282 Self::Mouse(m) => m.button == Some(MouseButton::Left),
283 Self::Touch(_) => true,
284 }
285 }
286}
287
288#[derive(Debug, Clone, PartialEq)]
289pub struct ImePreeditEventData {
290 pub text: String,
291 pub cursor: Option<(usize, usize)>,
292}
293
294impl ImePreeditEventData {
295 pub(crate) fn new(text: String, cursor: Option<(usize, usize)>) -> Self {
296 Self { text, cursor }
297 }
298}
299
300#[derive(Debug, Clone, PartialEq)]
301pub struct FileEventData {
302 pub cursor: CursorPoint,
303 pub file_paths: Vec<PathBuf>,
304}
305
306impl FileEventData {
307 pub(crate) fn new(cursor: CursorPoint, file_paths: Vec<PathBuf>) -> Self {
308 Self { cursor, file_paths }
309 }
310}
311
312#[derive(Debug, Clone, PartialEq)]
313pub enum EventType {
314 Mouse(MouseEventData),
315 Keyboard(KeyboardEventData),
316 Sized(SizedEventData),
317 Visible(VisibleEventData),
318 Styled(StyledEventData),
319 Wheel(WheelEventData),
320 Touch(TouchEventData),
321 Pointer(PointerEventData),
322 ImePreedit(ImePreeditEventData),
323 File(FileEventData),
324}