1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use std::{any::Any, rc::Rc};

use dioxus_core::ElementId;
use dioxus_native_core::NodeId;
use freya_elements::{
    elements::PlatformEventData,
    events::{
        pointer::PointerType, FileData, KeyboardData, MouseData, PointerData, TouchData, WheelData,
    },
};
use torin::prelude::*;

use crate::{events::PlatformEvent, prelude::PotentialEvent};

use super::event_name::EventName;

/// Event emitted to the DOM.
#[derive(Debug, Clone, PartialEq)]
pub struct DomEvent {
    pub name: EventName,
    pub node_id: NodeId,
    pub element_id: ElementId,
    pub data: DomEventData,
    pub bubbles: bool,
    pub layer: Option<i16>,
}

impl Eq for DomEvent {}

impl PartialOrd for DomEvent {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for DomEvent {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.name.cmp(&other.name)
    }
}

impl DomEvent {
    pub fn new(
        PotentialEvent {
            node_id,
            layer,
            event,
        }: PotentialEvent,
        element_id: ElementId,
        node_area: Option<Area>,
        scale_factor: f64,
    ) -> Self {
        let name = event.get_name();

        let bubbles = name.does_bubble();

        match event {
            PlatformEvent::Mouse { cursor, button, .. } => {
                let screen_coordinates = cursor / scale_factor;
                let element_x =
                    (cursor.x - node_area.unwrap_or_default().min_x() as f64) / scale_factor;
                let element_y =
                    (cursor.y - node_area.unwrap_or_default().min_y() as f64) / scale_factor;

                let event_data = if name.is_pointer() {
                    DomEventData::Pointer(PointerData::new(
                        screen_coordinates,
                        (element_x, element_y).into(),
                        PointerType::Mouse {
                            trigger_button: button,
                        },
                    ))
                } else {
                    DomEventData::Mouse(MouseData::new(
                        screen_coordinates,
                        (element_x, element_y).into(),
                        button,
                    ))
                };

                Self {
                    node_id,
                    element_id,
                    name,
                    data: event_data,
                    bubbles,
                    layer,
                }
            }
            PlatformEvent::Wheel { scroll, .. } => Self {
                node_id,
                element_id,
                name,
                data: DomEventData::Wheel(WheelData::new(scroll.x, scroll.y)),
                bubbles,
                layer,
            },
            PlatformEvent::Keyboard {
                ref key,
                code,
                modifiers,
                ..
            } => Self {
                node_id,
                element_id,
                name,
                data: DomEventData::Keyboard(KeyboardData::new(key.clone(), code, modifiers)),
                bubbles,
                layer,
            },
            PlatformEvent::Touch {
                location,
                finger_id,
                phase,
                force,
                ..
            } => {
                let element_x = location.x - node_area.unwrap_or_default().min_x() as f64;
                let element_y = location.y - node_area.unwrap_or_default().min_y() as f64;

                let event_data = if name.is_pointer() {
                    DomEventData::Pointer(PointerData::new(
                        location,
                        (element_x, element_y).into(),
                        PointerType::Touch {
                            finger_id,
                            phase,
                            force,
                        },
                    ))
                } else {
                    DomEventData::Touch(TouchData::new(
                        location,
                        (element_x, element_y).into(),
                        finger_id,
                        phase,
                        force,
                    ))
                };

                Self {
                    node_id,
                    element_id,
                    name,
                    data: event_data,
                    bubbles,
                    layer,
                }
            }
            PlatformEvent::File {
                name, file_path, ..
            } => {
                let event_data = DomEventData::File(FileData { file_path });

                Self {
                    node_id,
                    element_id,
                    name,
                    data: event_data,
                    bubbles,
                    layer,
                }
            }
        }
    }
}

/// Data of a DOM event.
#[derive(Debug, Clone, PartialEq)]
pub enum DomEventData {
    Mouse(MouseData),
    Keyboard(KeyboardData),
    Wheel(WheelData),
    Touch(TouchData),
    Pointer(PointerData),
    File(FileData),
}

impl DomEventData {
    pub fn any(self) -> Rc<dyn Any> {
        match self {
            DomEventData::Mouse(m) => Rc::new(PlatformEventData::new(Box::new(m))),
            DomEventData::Keyboard(k) => Rc::new(PlatformEventData::new(Box::new(k))),
            DomEventData::Wheel(w) => Rc::new(PlatformEventData::new(Box::new(w))),
            DomEventData::Touch(t) => Rc::new(PlatformEventData::new(Box::new(t))),
            DomEventData::Pointer(p) => Rc::new(PlatformEventData::new(Box::new(p))),
            DomEventData::File(fd) => Rc::new(PlatformEventData::new(Box::new(fd))),
        }
    }
}