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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
use crate::context::{InternalEvent, ResourceContext};
use crate::events::EventMeta;
use crate::prelude::*;
#[cfg(debug_assertions)]
use crate::systems::compute_matched_rules;
use crate::systems::{binding_system, hover_system};
use crate::tree::{focus_backward, focus_forward, is_navigatable};
#[cfg(debug_assertions)]
use log::debug;
use std::any::Any;
use vizia_storage::LayoutParentIterator;
#[cfg(debug_assertions)]
use vizia_storage::ParentIterator;
use vizia_storage::TreeIterator;

const DOUBLE_CLICK_INTERVAL: Duration = Duration::from_millis(500);

/// Dispatches events to views and models.
///
/// The [EventManager] is responsible for taking the events in the event queue in cx
/// and dispatching them to views and models based on the target and propagation metadata of the event.
#[doc(hidden)]
pub struct EventManager {
    // Queue of events to be processed.
    event_queue: Vec<Event>,
}

impl Default for EventManager {
    fn default() -> Self {
        Self::new()
    }
}

impl EventManager {
    pub fn new() -> Self {
        EventManager { event_queue: Vec::with_capacity(10) }
    }

    /// Flush the event queue, dispatching events to their targets.
    /// Returns whether there are still more events to process, i.e. the event handlers sent events.
    pub fn flush_events(&mut self, cx: &mut Context) -> bool {
        // Clear the event queue in the event manager.
        self.event_queue.clear();

        // Move events from cx to event manager. This is so the cx can be passed
        // mutably to the view when handling events.
        self.event_queue.extend(cx.event_queue.drain(0..));

        // Loop over the events in the event queue.
        'events: for event in self.event_queue.iter_mut() {
            // Handle internal events.
            event.take(|internal_event, _| match internal_event {
                InternalEvent::Redraw => cx.needs_redraw(Entity::root()),
                InternalEvent::LoadImage { path, image, policy } => {
                    if let Some(image) = image.lock().unwrap().take() {
                        ResourceContext::new(cx).load_image(path, image, policy);
                    }
                }
            });

            // Send events to any global listeners.
            let mut global_listeners = vec![];
            std::mem::swap(&mut cx.global_listeners, &mut global_listeners);
            for listener in &global_listeners {
                cx.with_current(Entity::root(), |cx| listener(&mut EventContext::new(cx), event));
            }
            std::mem::swap(&mut cx.global_listeners, &mut global_listeners);

            // Send events to any local listeners.
            let listeners = cx.listeners.keys().copied().collect::<Vec<Entity>>();
            for entity in listeners {
                if let Some(listener) = cx.listeners.remove(&entity) {
                    if let Some(mut event_handler) = cx.views.remove(&entity) {
                        cx.with_current(entity, |cx| {
                            (listener)(event_handler.as_mut(), &mut EventContext::new(cx), event);
                        });

                        cx.views.insert(entity, event_handler);
                    }

                    cx.listeners.insert(entity, listener);
                }

                if event.meta.consumed {
                    continue 'events;
                }
            }

            // Handle state updates for window events.
            event.map(|window_event, meta| {
                if cx.windows.contains_key(&meta.origin) {
                    internal_state_updates(cx, window_event, meta);
                }
            });

            // Skip to next event if the current event was consumed when handling internal state updates.
            if event.meta.consumed {
                continue 'events;
            }

            let cx = &mut EventContext::new(cx);

            // Copy the target to prevent multiple mutable borrows error.
            let target = event.meta.target;

            // Send event to target.
            visit_entity(cx, target, event);

            // Skip to next event if the current event was consumed.
            if event.meta.consumed {
                continue 'events;
            }

            // Propagate up from target to root (not including the target).
            if event.meta.propagation == Propagation::Up {
                // Create a parent iterator and skip the first element which is the target.
                let iter = target.parent_iter(cx.tree).skip(1);

                for entity in iter {
                    // Send event to all ancestors of the target.
                    visit_entity(cx, entity, event);

                    // Skip to the next event if the current event was consumed.
                    if event.meta.consumed {
                        continue 'events;
                    }
                }
            }

            // Propagate the event down the subtree from the target (not including the target).
            if event.meta.propagation == Propagation::Subtree {
                // Create a branch (subtree) iterator and skip the first element which is the target.
                let iter = target.branch_iter(cx.tree).skip(1);

                for entity in iter {
                    // Send event to all entities in the subtree after the target.
                    visit_entity(cx, entity, event);

                    // Skip to the next event if the current event was consumed.
                    if event.meta.consumed {
                        continue 'events;
                    }
                }
            }
        }

        binding_system(cx);

        // Return true if there are new events in the queue.
        !cx.event_queue.is_empty()
    }
}

fn visit_entity(cx: &mut EventContext, entity: Entity, event: &mut Event) {
    // Send event to models attached to the entity
    if let Some(ids) = cx
        .data
        .get(&entity)
        .map(|model_data_store| model_data_store.models.keys().cloned().collect::<Vec<_>>())
    {
        for id in ids {
            if let Some(mut model) = cx
                .data
                .get_mut(&entity)
                .and_then(|model_data_store| model_data_store.models.remove(&id))
            {
                cx.current = entity;

                model.event(cx, event);

                cx.data
                    .get_mut(&entity)
                    .and_then(|model_data_store| model_data_store.models.insert(id, model));
            }
        }
    }

    // Return early if the event was consumed by a model
    if event.meta.consumed {
        return;
    }

    // Send event to the view attached to the entity
    if let Some(mut view) = cx.views.remove(&entity) {
        cx.current = entity;
        view.event(cx, event);

        cx.views.insert(entity, view);
    }
}

/// Update the internal state of the cx based on received window event and emit window event to relevant target.
fn internal_state_updates(cx: &mut Context, window_event: &WindowEvent, meta: &mut EventMeta) {
    cx.current = meta.target;

    match window_event {
        WindowEvent::Drop(drop_data) => {
            cx.drop_data = Some(drop_data.clone());
        }

        WindowEvent::MouseMove(x, y) => {
            if !x.is_nan() && !y.is_nan() {
                cx.mouse.previous_cursor_x = cx.mouse.cursor_x;
                cx.mouse.previous_cursor_y = cx.mouse.cursor_y;
                cx.mouse.cursor_x = *x;
                cx.mouse.cursor_y = *y;

                hover_system(cx, meta.origin);

                mutate_direct_or_up(meta, cx.captured, cx.hovered, false);
            }

            // if cx.mouse.cursor_x != cx.mouse.previous_cursor_x
            //     || cx.mouse.cursor_y != cx.mouse.previous_cursor_y
            // {
            // }

            // if let Some(dropped_file) = cx.dropped_file.take() {
            //     emit_direct_or_up(
            //         cx,
            //         WindowEvent::DroppedFile(dropped_file),
            //         cx.captured,
            //         cx.hovered,
            //         true,
            //     );
            // }
        }
        WindowEvent::MouseDown(button) => {
            // do direct state-updates
            match button {
                MouseButton::Left => {
                    cx.mouse.left.state = MouseButtonState::Pressed;

                    cx.mouse.left.pos_down = (cx.mouse.cursor_x, cx.mouse.cursor_y);
                    cx.mouse.left.pressed = cx.hovered;
                    cx.triggered = cx.hovered;

                    let disabled = cx.style.disabled.get(cx.hovered).copied().unwrap_or_default();

                    if let Some(pseudo_classes) = cx.style.pseudo_classes.get_mut(cx.triggered) {
                        if !disabled {
                            pseudo_classes.set(PseudoClassFlags::ACTIVE, true);
                            cx.needs_restyle(cx.triggered);
                        }
                    }
                    let focusable = cx
                        .style
                        .abilities
                        .get(cx.hovered)
                        .filter(|abilities| abilities.contains(Abilities::FOCUSABLE))
                        .is_some();

                    // Reset drag data
                    cx.drop_data = None;

                    cx.with_current(if focusable { cx.hovered } else { cx.focused }, |cx| {
                        cx.focus_with_visibility(false)
                    });
                }
                MouseButton::Right => {
                    cx.mouse.right.state = MouseButtonState::Pressed;
                    cx.mouse.right.pos_down = (cx.mouse.cursor_x, cx.mouse.cursor_y);
                    cx.mouse.right.pressed = cx.hovered;
                }
                MouseButton::Middle => {
                    cx.mouse.middle.state = MouseButtonState::Pressed;
                    cx.mouse.middle.pos_down = (cx.mouse.cursor_x, cx.mouse.cursor_y);
                    cx.mouse.middle.pressed = cx.hovered;
                }
                _ => {}
            }

            // emit trigger events
            if matches!(button, MouseButton::Left) {
                emit_direct_or_up(
                    cx,
                    WindowEvent::PressDown { mouse: true },
                    cx.captured,
                    cx.triggered,
                    true,
                );
            }

            // track double/triple -click
            let new_click_time = Instant::now();
            let click_duration = new_click_time - cx.click_time;
            let new_click_pos = (cx.mouse.cursor_x, cx.mouse.cursor_y);
            if click_duration <= DOUBLE_CLICK_INTERVAL
                && new_click_pos == cx.click_pos
                && *button == cx.click_button
            {
                if cx.clicks <= 2 {
                    cx.clicks += 1;
                    let event = if cx.clicks == 3 {
                        WindowEvent::MouseTripleClick(*button)
                    } else {
                        WindowEvent::MouseDoubleClick(*button)
                    };
                    meta.consume();
                    emit_direct_or_up(cx, event, cx.captured, cx.hovered, true);
                }
            } else {
                cx.clicks = 1;
            }
            cx.click_time = new_click_time;
            cx.click_pos = new_click_pos;
            cx.click_button = *button;
            mutate_direct_or_up(meta, cx.captured, cx.hovered, true);
        }
        WindowEvent::MouseUp(button) => {
            match button {
                MouseButton::Left => {
                    cx.mouse.left.pos_up = (cx.mouse.cursor_x, cx.mouse.cursor_y);
                    cx.mouse.left.released = cx.hovered;
                    cx.mouse.left.state = MouseButtonState::Released;
                }
                MouseButton::Right => {
                    cx.mouse.right.pos_up = (cx.mouse.cursor_x, cx.mouse.cursor_y);
                    cx.mouse.right.released = cx.hovered;
                    cx.mouse.right.state = MouseButtonState::Released;
                }
                MouseButton::Middle => {
                    cx.mouse.middle.pos_up = (cx.mouse.cursor_x, cx.mouse.cursor_y);
                    cx.mouse.middle.released = cx.hovered;
                    cx.mouse.middle.state = MouseButtonState::Released;
                }
                _ => {}
            }

            if matches!(button, MouseButton::Left) {
                if cx.hovered == cx.triggered {
                    let disabled = cx.style.disabled.get(cx.hovered).copied().unwrap_or_default();

                    if !disabled {
                        emit_direct_or_up(
                            cx,
                            WindowEvent::Press { mouse: true },
                            cx.captured,
                            cx.triggered,
                            true,
                        );
                    }
                }

                if let Some(pseudo_classes) = cx.style.pseudo_classes.get_mut(cx.triggered) {
                    pseudo_classes.set(PseudoClassFlags::ACTIVE, false);
                }

                cx.needs_restyle(cx.triggered);

                cx.triggered = Entity::null();
            }

            mutate_direct_or_up(meta, cx.captured, cx.hovered, true);
        }
        WindowEvent::MouseScroll(_, _) => {
            meta.target = cx.hovered;
        }
        WindowEvent::KeyDown(code, _) => {
            meta.target = cx.focused;

            #[cfg(debug_assertions)]
            if *code == Code::KeyP && cx.modifiers.ctrl() {
                for entity in TreeIterator::full(&cx.tree) {
                    if let Some(model_data_store) = cx.data.get(&entity) {
                        if !model_data_store.models.is_empty() {
                            debug!("Models for {}", entity);
                            for (_, model) in model_data_store.models.iter() {
                                debug!("M: {:?}", model.name())
                            }
                        }

                        if !model_data_store.stores.is_empty() {
                            debug!("Stores for {}", entity);
                            for (_, store) in model_data_store.stores.iter() {
                                debug!("S: [{}] - Observers {:?}", store.name(), store.observers())
                            }
                        }
                    }
                }
            }

            #[cfg(debug_assertions)]
            if *code == Code::KeyI && cx.modifiers.ctrl() {
                debug!("Entity tree");
                let (tree, views, cache) = (&cx.tree, &cx.views, &cx.cache);
                let has_next_sibling = |entity| tree.get_next_sibling(entity).is_some();
                let root_indents = |entity: Entity| {
                    let parent_iter = ParentIterator::new(tree, Some(entity));
                    parent_iter
                        .skip(1)
                        .collect::<Vec<_>>()
                        .into_iter()
                        .rev()
                        .skip(1)
                        .map(|entity| if has_next_sibling(entity) { "│   " } else { "    " })
                        .collect::<String>()
                };
                let local_idents =
                    |entity| if has_next_sibling(entity) { "├── " } else { "└── " };
                let indents = |entity| root_indents(entity) + local_idents(entity);

                for entity in TreeIterator::full(tree).skip(1) {
                    if let Some(element_name) = views.get(&entity).and_then(|view| view.element()) {
                        let w = cache.get_bounds(entity).w;
                        let h = cache.get_bounds(entity).h;
                        let classes = cx.style.classes.get(entity);
                        let mut class_names = String::new();
                        if let Some(classes) = classes {
                            for class in classes.iter() {
                                class_names += &format!(".{}", class);
                            }
                        }
                        println!(
                            "{}{} {}{} [x: {} y: {} w: {} h: {}]",
                            indents(entity),
                            entity,
                            element_name,
                            class_names,
                            cache.get_bounds(entity).x,
                            cache.get_bounds(entity).y,
                            if w == f32::MAX { "inf".to_string() } else { w.to_string() },
                            if h == f32::MAX { "inf".to_string() } else { h.to_string() },
                        );
                    } else if let Some(binding_name) =
                        cx.bindings.get(&entity).map(|binding| format!("{:?}", binding))
                    {
                        println!(
                            "{}{} binding observing {}",
                            indents(entity),
                            entity,
                            binding_name,
                        );
                    } else {
                        println!(
                            "{}{} {}",
                            indents(entity),
                            entity,
                            if views.get(&entity).is_some() {
                                "unnamed view"
                            } else {
                                "no binding or view"
                            }
                        );
                    }
                }
            }

            #[cfg(debug_assertions)]
            if *code == Code::KeyS
                && cx.modifiers == Modifiers::CTRL | Modifiers::SHIFT | Modifiers::ALT
            {
                let mut result = vec![];
                compute_matched_rules(cx, cx.hovered, &mut result);

                let entity = cx.hovered;
                debug!("/* Matched rules for Entity: {} Parent: {:?} View: {} posx: {} posy: {} width: {} height: {}",
                    entity,
                    entity.parent(&cx.tree),
                    cx
                        .views
                        .get(&entity)
                        .map_or("<None>", |view| view.element().unwrap_or("<Unnamed>")),
                    cx.cache.get_posx(entity),
                    cx.cache.get_posy(entity),
                    cx.cache.get_width(entity),
                    cx.cache.get_height(entity)
                );
                for rule in result.into_iter() {
                    for selectors in cx.style.rules.iter() {
                        if *selectors.0 == rule.0 {
                            debug!("{:?}", selectors.1);
                        }
                    }
                }
            }

            #[cfg(debug_assertions)]
            if *code == Code::KeyT
                && cx.modifiers == Modifiers::CTRL | Modifiers::SHIFT | Modifiers::ALT
            {
                // debug!("Loaded font face info:");
                // for face in cx.text_context.font_system().db().faces() {
                //     debug!(
                //         "family: {:?}\npost_script_name: {:?}\nstyle: {:?}\nweight: {:?}\nstretch: {:?}\nmonospaced: {:?}\n",
                //         face.families,
                //         face.post_script_name,
                //         face.style,
                //         face.weight,
                //         face.stretch,
                //         face.monospaced,
                //     );
                // }
            }

            if *code == Code::F5 {
                EventContext::new(cx).reload_styles().unwrap();
            }

            if *code == Code::Tab {
                let lock_focus_to = cx.tree.lock_focus_within(cx.focused);
                if cx.modifiers.shift() {
                    let prev_focused = if let Some(prev_focused) =
                        focus_backward(&cx.tree, &cx.style, cx.focused, lock_focus_to)
                    {
                        prev_focused
                    } else {
                        TreeIterator::full(&cx.tree)
                            .filter(|node| {
                                is_navigatable(&cx.tree, &cx.style, *node, lock_focus_to)
                            })
                            .next_back()
                            .unwrap_or(Entity::root())
                    };

                    if prev_focused != cx.focused {
                        cx.set_focus_pseudo_classes(cx.focused, false, true);
                        cx.set_focus_pseudo_classes(prev_focused, true, true);
                        cx.event_queue.push_back(
                            Event::new(WindowEvent::FocusOut)
                                .target(cx.focused)
                                .origin(Entity::root()),
                        );
                        cx.event_queue.push_back(
                            Event::new(WindowEvent::FocusIn)
                                .target(prev_focused)
                                .origin(Entity::root()),
                        );

                        cx.focused = prev_focused;

                        if let Some(pseudo_classes) = cx.style.pseudo_classes.get_mut(cx.triggered)
                        {
                            pseudo_classes.set(PseudoClassFlags::ACTIVE, false);
                            cx.needs_restyle(cx.triggered);
                        }
                        cx.triggered = Entity::null();
                    }
                } else {
                    let next_focused = if let Some(next_focused) =
                        focus_forward(&cx.tree, &cx.style, cx.focused, lock_focus_to)
                    {
                        next_focused
                    } else {
                        TreeIterator::full(&cx.tree)
                            .find(|node| is_navigatable(&cx.tree, &cx.style, *node, lock_focus_to))
                            .unwrap_or(Entity::root())
                    };

                    if next_focused != cx.focused {
                        cx.set_focus_pseudo_classes(cx.focused, false, true);
                        cx.set_focus_pseudo_classes(next_focused, true, true);
                        cx.event_queue.push_back(
                            Event::new(WindowEvent::FocusOut)
                                .target(cx.focused)
                                .origin(Entity::root()),
                        );
                        cx.event_queue.push_back(
                            Event::new(WindowEvent::FocusIn)
                                .target(next_focused)
                                .origin(Entity::root()),
                        );

                        cx.focused = next_focused;

                        if let Some(pseudo_classes) = cx.style.pseudo_classes.get_mut(cx.triggered)
                        {
                            pseudo_classes.set(PseudoClassFlags::ACTIVE, false);
                            cx.needs_restyle(cx.triggered);
                        }
                        cx.triggered = Entity::null();
                    }
                }
            }

            if matches!(*code, Code::Enter | Code::NumpadEnter) {
                cx.triggered = cx.focused;
                if let Some(pseudo_classes) = cx.style.pseudo_classes.get_mut(cx.triggered) {
                    pseudo_classes.set(PseudoClassFlags::ACTIVE, true);
                }
                cx.with_current(cx.focused, |cx| cx.emit(WindowEvent::PressDown { mouse: false }));
            }
        }
        WindowEvent::KeyUp(code, _) => {
            meta.target = cx.focused;
            if matches!(code, Code::Enter | Code::NumpadEnter) {
                if cx.focused == cx.triggered {
                    cx.with_current(cx.triggered, |cx| {
                        cx.emit(WindowEvent::Press { mouse: false })
                    });
                }
                if let Some(pseudo_classes) = cx.style.pseudo_classes.get_mut(cx.triggered) {
                    pseudo_classes.set(PseudoClassFlags::ACTIVE, false);
                }
                cx.needs_restyle(cx.triggered);
                cx.triggered = Entity::null();
            }
        }
        WindowEvent::CharInput(_) => {
            meta.target = cx.focused;
        }
        WindowEvent::FocusOut => {
            cx.set_focus_pseudo_classes(cx.focused, false, true);
            cx.focused = Entity::null();
        }
        WindowEvent::FocusIn => {
            cx.focused = meta.target;
            cx.set_focus_pseudo_classes(cx.focused, true, true);
        }
        WindowEvent::MouseEnter => {
            if let Some(pseudo_class) = cx.style.pseudo_classes.get_mut(meta.origin) {
                pseudo_class.set(PseudoClassFlags::OVER, true);
            }
        }
        WindowEvent::MouseLeave => {
            if let Some(pseudo_class) = cx.style.pseudo_classes.get_mut(meta.origin) {
                pseudo_class.set(PseudoClassFlags::OVER, false);
            }

            let parent_iter = LayoutParentIterator::new(&cx.tree, cx.hovered);
            for ancestor in parent_iter {
                if let Some(pseudo_classes) = cx.style.pseudo_classes.get_mut(ancestor) {
                    pseudo_classes.set(PseudoClassFlags::HOVER, false);
                    cx.style.needs_restyle(ancestor);
                }
            }

            cx.hovered = Entity::null();
        }

        _ => {}
    }
}

fn mutate_direct_or_up(meta: &mut EventMeta, direct: Entity, up: Entity, root: bool) {
    if direct != Entity::null() {
        meta.target = direct;
        meta.propagation = Propagation::Direct;
    } else if up != Entity::root() || root {
        meta.target = up;
        meta.propagation = Propagation::Up;
    } else {
        meta.consume();
    }
}

fn emit_direct_or_up<M: Any + Send>(
    cx: &mut Context,
    message: M,
    direct: Entity,
    up: Entity,
    root: bool,
) {
    let mut event = Event::new(message);
    mutate_direct_or_up(&mut event.meta, direct, up, root);
    cx.emit_custom(event);
}