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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
use dioxus_native_core::real_dom::NodeImmutable;
use dioxus_native_core::NodeId;
use dioxus_native_core::{prelude::NodeImmutableDioxusExt, tree::TreeRef};
use freya_dom::prelude::*;

use freya_engine::prelude::*;
use freya_node_state::{Fill, Style, ViewportState};
use itertools::sorted;

pub use crate::events::{DomEvent, NodesState, PlatformEvent};

use crate::types::{EventEmitter, EventsQueue, PotentialEvents};

use super::potential_event::PotentialEvent;

/// Process the events and emit them to the VirtualDOM
pub fn process_events(
    dom: &FreyaDOM,
    events: &mut EventsQueue,
    event_emitter: &EventEmitter,
    nodes_state: &mut NodesState,
    scale_factor: f64,
) {
    // 1. Get global events created from the incoming events
    let global_events = measure_global_events(events);

    // 2. Get potential events that could be emitted based on the elements layout and viewports
    let potential_events = measure_potential_event_listeners(events, dom);

    // 3. Get what events can be actually emitted based on what elements are listening
    let dom_events = measure_dom_events(potential_events, dom, scale_factor);

    // 4. Filter the dom events and get potential collateral events, e.g. mouseover -> mouseenter
    let (potential_collateral_events, mut to_emit_dom_events) =
        nodes_state.process_events(&dom_events, events);

    // 5. Get what collateral events can actually be emitted
    let to_emit_dom_collateral_events =
        measure_dom_events(potential_collateral_events, dom, scale_factor);

    let colateral_global_events = measure_colateral_global_events(&to_emit_dom_collateral_events);

    // 6. Join both the dom and colateral dom events and sort them
    to_emit_dom_events.extend(to_emit_dom_collateral_events);
    to_emit_dom_events.sort_unstable();

    // 7. Emit the DOM events
    for event in to_emit_dom_events {
        event_emitter.send(event).unwrap();
    }

    // 8. Emit the global events
    emit_global_events_listeners(
        global_events,
        colateral_global_events,
        dom,
        event_emitter,
        scale_factor,
    );

    // 9. Clear the events queue
    events.clear();
}

/// Measure colateral global events
pub fn measure_colateral_global_events(events: &[DomEvent]) -> Vec<DomEvent> {
    let mut global_events = Vec::default();
    for event in events {
        let Some(event_name) = event.name.get_global_event() else {
            continue;
        };
        let mut global_event = event.clone();
        global_event.name = event_name;
        global_event.layer = None;
        global_events.push(global_event);
    }
    global_events
}

/// Measure global events
pub fn measure_global_events(events: &EventsQueue) -> Vec<PlatformEvent> {
    let mut global_events = Vec::default();
    for event in events {
        let Some(event_name) = event.get_name().get_global_event() else {
            continue;
        };
        let mut global_event = event.clone();
        global_event.set_name(event_name);
        global_events.push(global_event);
    }
    global_events
}

/// Measure what potential event listeners could be triggered
pub fn measure_potential_event_listeners(events: &EventsQueue, fdom: &FreyaDOM) -> PotentialEvents {
    let mut potential_events = PotentialEvents::default();

    let layout = fdom.layout();
    let rdom = fdom.rdom();
    let layers = fdom.layers();

    // Propagate events from the top to the bottom
    for (layer, layer_nodes) in sorted(layers.layers().iter()) {
        for node_id in layer_nodes {
            let layout_node = layout.get(*node_id);
            if let Some(layout_node) = layout_node {
                'events: for event in events.iter() {
                    if let PlatformEvent::Keyboard { name, .. } = event {
                        let event_data = PotentialEvent {
                            node_id: *node_id,
                            layer: Some(*layer),
                            event: event.clone(),
                        };
                        potential_events.entry(*name).or_default().push(event_data);
                    } else {
                        let data = match event {
                            PlatformEvent::Mouse { name, cursor, .. } => Some((name, cursor)),
                            PlatformEvent::Wheel { name, cursor, .. } => Some((name, cursor)),
                            PlatformEvent::Touch { name, location, .. } => Some((name, location)),
                            PlatformEvent::File { name, cursor, .. } => Some((name, cursor)),
                            _ => None,
                        };
                        if let Some((name, cursor)) = data {
                            let cursor_is_inside = layout_node.area.contains(cursor.to_f32());

                            // Make sure the cursor is inside the node area
                            if cursor_is_inside {
                                let node = rdom.get(*node_id).unwrap();
                                let node_viewports = node.get::<ViewportState>().unwrap();

                                // Make sure the cursor is inside all the applicable viewports from the element
                                for viewport_id in &node_viewports.viewports {
                                    let viewport = layout.get(*viewport_id).unwrap().visible_area();
                                    if !viewport.contains(cursor.to_f32()) {
                                        continue 'events;
                                    }
                                }

                                let event_data = PotentialEvent {
                                    node_id: *node_id,
                                    layer: Some(*layer),
                                    event: event.clone(),
                                };

                                potential_events
                                    .entry(*name)
                                    .or_insert_with(Vec::new)
                                    .push(event_data);
                            }
                        }
                    }
                }
            }
        }
    }

    potential_events
}

fn is_node_parent_of(rdom: &DioxusDOM, node: NodeId, parent_node: NodeId) -> bool {
    let mut stack = vec![parent_node];
    while let Some(id) = stack.pop() {
        let tree = rdom.tree_ref();
        let mut children = tree.children_ids(id);
        drop(tree);
        if children.contains(&node) {
            return true;
        }

        if rdom.contains(id) {
            children.reverse();
            stack.extend(children.iter());
        }
    }

    false
}

/// Measure what DOM events could be emitted
fn measure_dom_events(
    potential_events: PotentialEvents,
    fdom: &FreyaDOM,
    scale_factor: f64,
) -> Vec<DomEvent> {
    let mut new_events = Vec::new();
    let rdom = fdom.rdom();

    // Iterate over all the events
    for (event_name, event_nodes) in potential_events {
        let collateral_events = event_name.get_collateral_events();

        let mut valid_events: Vec<PotentialEvent> = Vec::new();

        // Iterate over the collateral events (including the source)
        'event: for collateral_event in collateral_events {
            let mut child_node: Option<NodeId> = None;

            let listeners = rdom.get_listening_sorted(&collateral_event);

            // Iterate over the event nodes
            for PotentialEvent {
                node_id,
                event,
                layer,
            } in event_nodes.iter().rev()
            {
                let Some(node) = rdom.get(*node_id) else {
                    continue;
                };

                // Iterate over the event listeners
                for listener in &listeners {
                    if listener.id() == *node_id {
                        let valid_node = if let Some(child_node) = child_node {
                            is_node_parent_of(rdom, child_node, *node_id)
                        } else {
                            true
                        };

                        if valid_node {
                            let mut valid_event = event.clone();
                            valid_event.set_name(collateral_event);
                            valid_events.push(PotentialEvent {
                                node_id: *node_id,
                                event: valid_event,
                                layer: *layer,
                            });

                            // Stack events that do not bubble up
                            if event.get_name().does_bubble() {
                                continue 'event;
                            }
                        }
                    }
                }

                let Style { background, .. } = &*node.get::<Style>().unwrap();

                if background != &Fill::Color(Color::TRANSPARENT)
                    && !event.get_name().does_go_through_solid()
                {
                    // If the background isn't transparent,
                    // we must make sure that next nodes are parent of it
                    // This only matters for events that bubble up (e.g. cursor movement events)
                    child_node = Some(*node_id);
                }
            }
        }

        for potential_event in valid_events {
            let layout = fdom.layout();
            let layout_node = layout.get(potential_event.node_id);
            if let Some(layout_node) = layout_node {
                let node_ref = fdom.rdom().get(potential_event.node_id).unwrap();
                let element_id = node_ref.mounted_id().unwrap();
                let event = DomEvent::new(
                    potential_event,
                    element_id,
                    Some(layout_node.visible_area()),
                    scale_factor,
                );
                new_events.push(event);
            }
        }
    }

    new_events
}

/// Emit global events
fn emit_global_events_listeners(
    global_events: Vec<PlatformEvent>,
    global_colateral_events: Vec<DomEvent>,
    fdom: &FreyaDOM,
    event_emitter: &EventEmitter,
    scale_factor: f64,
) {
    for global_event in global_events {
        let event_name = global_event.get_name();
        let listeners = fdom.rdom().get_listening_sorted(&event_name);

        for listener in listeners {
            let element_id = listener.mounted_id().unwrap();
            let event = DomEvent::new(
                PotentialEvent {
                    node_id: listener.id(),
                    layer: None,
                    event: global_event.clone(),
                },
                element_id,
                None,
                scale_factor,
            );
            event_emitter.send(event).unwrap();
        }
    }
    for colateral_global_event in global_colateral_events {
        event_emitter.send(colateral_global_event).unwrap();
    }
}