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
use std::collections::{HashSet, BTreeMap};
use crate::{
    dom::{EventFilter, NotEventFilter, HoverEventFilter, FocusEventFilter, WindowEventFilter},
    callbacks:: {CallbackInfo, CallbackType, HitTestItem, UpdateScreen},
    id_tree::NodeId,
    ui_state::UiState,
    window::{
        AcceleratorKey, FullWindowState, CallbacksOfHitTest, DetermineCallbackResult,
    },
};

/// Determine which event / which callback(s) should be called and in which order
///
/// This function also updates / mutates the current window states `focused_node`
/// as well as the `window_state.previous_state`
pub fn determine_callbacks(
    window_state: &mut FullWindowState,
    hit_test_items: &[HitTestItem],
    ui_state: &UiState,
) -> CallbacksOfHitTest {

    use std::collections::BTreeSet;

    let mut needs_hover_redraw = false;
    let mut needs_hover_relayout = false;
    let mut nodes_with_callbacks: BTreeMap<NodeId, DetermineCallbackResult> = BTreeMap::new();

    let current_window_events = get_window_events(window_state);
    let current_hover_events = get_hover_events(&current_window_events);
    let current_focus_events = get_focus_events(&current_hover_events);

    let event_was_mouse_down    = current_window_events.contains(&WindowEventFilter::MouseDown);
    let event_was_mouse_release = current_window_events.contains(&WindowEventFilter::MouseUp);
    let event_was_mouse_enter   = current_window_events.contains(&WindowEventFilter::MouseEnter);
    let event_was_mouse_leave   = current_window_events.contains(&WindowEventFilter::MouseLeave);

    // Store the current window state so we can set it in this.previous_window_state later on
    let mut previous_state = Box::new(window_state.clone());
    previous_state.previous_window_state = None;

    // TODO: If the current mouse is down, but the event
    // wasn't a click, that means it was a drag

    // Figure out what the hovered NodeIds are
    let mut new_hit_node_ids: BTreeMap<NodeId, HitTestItem> = hit_test_items.iter().filter_map(|hit_test_item| {
        ui_state.tag_ids_to_node_ids
        .get(&hit_test_item.tag)
        .map(|node_id| (*node_id, hit_test_item.clone()))
    }).collect();

    if event_was_mouse_leave {
        new_hit_node_ids = BTreeMap::new();
    }

    // Figure out what the current focused NodeId is
    if event_was_mouse_down || event_was_mouse_release {

        // Find the first (closest to cursor in hierarchy) item that has a tabindex
        let closest_focus_node = hit_test_items.iter().rev()
        .find_map(|item| ui_state.tab_index_tags.get(&item.tag))
        .cloned();

        // Even if the focused node is None, we still have to update window_state.focused_node!
        window_state.focused_node = closest_focus_node.map(|(node_id, _tab_idx)| (ui_state.dom_id.clone(), node_id));
    }

    macro_rules! insert_only_non_empty_callbacks {
        ($node_id:expr, $hit_test_item:expr, $normal_hover_callbacks:expr) => ({
            if !$normal_hover_callbacks.is_empty() {
                let mut callback_result = nodes_with_callbacks.entry(*$node_id)
                .or_insert_with(|| DetermineCallbackResult::default());

                let item: Option<HitTestItem> = $hit_test_item;
                if let Some(hit_test_item) = item {
                    callback_result.hit_test_item = Some(hit_test_item);
                }
                callback_result.normal_callbacks.extend($normal_hover_callbacks.into_iter());
            }
        })
    }

    // Inserts the events from a given NodeId and an Option<HitTestItem> into the nodes_with_callbacks
    macro_rules! insert_callbacks {(
        $node_id:expr,
        $hit_test_item:expr,
        $hover_callbacks:ident,
        $current_hover_events:ident,
        $event_filter:ident
    ) => ({
            // BTreeMap<EventFilter, Callback>
            let mut normal_hover_callbacks = BTreeMap::new();

            // Insert all normal Hover events
            if let Some(ui_state_hover_event_filters) = ui_state.$hover_callbacks.get($node_id) {
                for current_hover_event in &$current_hover_events {
                    if let Some(callback) = ui_state_hover_event_filters.get(current_hover_event) {
                        normal_hover_callbacks.insert(EventFilter::$event_filter(*current_hover_event), callback.0);
                    }
                }
            }

            insert_only_non_empty_callbacks!($node_id, $hit_test_item, normal_hover_callbacks);
        })
    }

    // Insert all normal window events
    for (window_node_id, window_callbacks) in &ui_state.window_callbacks {
        let normal_window_callbacks = window_callbacks.iter()
            .filter(|(current_window_event, _)| current_window_events.contains(current_window_event))
            .map(|(current_window_event, callback)| (EventFilter::Window(*current_window_event), callback.0))
            .collect::<BTreeMap<_, _>>();
        insert_only_non_empty_callbacks!(window_node_id, None, normal_window_callbacks);
    }

    // Insert (normal + default) hover events
    for (hover_node_id, hit_test_item) in &new_hit_node_ids {
        insert_callbacks!(hover_node_id, Some(hit_test_item.clone()), hover_callbacks, current_hover_events, Hover);
    }

    // Insert (normal + default) focus events
    if let Some(current_focused_node) = &window_state.focused_node {
        insert_callbacks!(&current_focused_node.1, None, focus_callbacks, current_focus_events, Focus);
    }

    // If the last focused node and the current focused node aren't the same,
    // submit a FocusLost for the last node and a FocusReceived for the current one.
    let mut focus_received_lost_events: BTreeMap<NodeId, FocusEventFilter> = BTreeMap::new();
    match (window_state.focused_node.as_ref(), previous_state.focused_node.as_ref()) {
        (Some((cur_dom_id, cur_node_id)), None) => {
            if *cur_dom_id == ui_state.dom_id {
                focus_received_lost_events.insert(*cur_node_id, FocusEventFilter::FocusReceived);
            }
        },
        (None, Some((prev_dom_id, prev_node_id))) => {
            if *prev_dom_id == ui_state.dom_id {
                focus_received_lost_events.insert(*prev_node_id, FocusEventFilter::FocusLost);
            }
        },
        (Some(cur), Some(prev)) => {
            if *cur != *prev {
                let (cur_dom_id, cur_node_id) = cur;
                let (prev_dom_id, prev_node_id) = prev;
                if *cur_dom_id == ui_state.dom_id {
                    focus_received_lost_events.insert(*cur_node_id, FocusEventFilter::FocusReceived);
                }
                if *prev_dom_id == ui_state.dom_id {
                    focus_received_lost_events.insert(*prev_node_id, FocusEventFilter::FocusLost);
                }
            }
        }
        (None, None) => { },
    }

    // Insert FocusReceived / FocusLost
    for (node_id, focus_event) in &focus_received_lost_events {
        let current_focus_leave_events = [focus_event.clone()];
        insert_callbacks!(node_id, None, focus_callbacks, current_focus_leave_events, Focus);
    }

    let current_dom_id = ui_state.dom_id.clone();

    macro_rules! mouse_enter {
        ($node_id:expr, $hit_test_item:expr, $event_filter:ident) => ({

            let node_is_focused = window_state.focused_node == Some((current_dom_id.clone(), $node_id));

            // BTreeMap<EventFilter, Callback>
            let mut normal_callbacks = BTreeMap::new();

            // Insert all normal Hover(MouseEnter) events
            if let Some(ui_state_hover_event_filters) = ui_state.hover_callbacks.get(&$node_id) {
                if let Some(callback) = ui_state_hover_event_filters.get(&HoverEventFilter::$event_filter) {
                    normal_callbacks.insert(EventFilter::Hover(HoverEventFilter::$event_filter), callback.0);
                }
            }

            // Insert all normal Focus(MouseEnter) events
            if node_is_focused {
                if let Some(ui_state_focus_event_filters) = ui_state.focus_callbacks.get(&$node_id) {
                    if let Some(callback) = ui_state_focus_event_filters.get(&FocusEventFilter::$event_filter) {
                        normal_callbacks.insert(EventFilter::Focus(FocusEventFilter::$event_filter), callback.0);
                    }
                }
            }

            if !normal_callbacks.is_empty() {

                let mut callback_result = nodes_with_callbacks.entry($node_id)
                .or_insert_with(|| DetermineCallbackResult::default());

                callback_result.hit_test_item = Some($hit_test_item);
                callback_result.normal_callbacks.extend(normal_callbacks.into_iter());
            }

            if let Some((_, hover_group)) = ui_state.node_ids_to_tag_ids.get(&$node_id).and_then(|tag_for_this_node| {
                ui_state.tag_ids_to_hover_active_states.get(&tag_for_this_node)
            }) {
                // We definitely need to redraw (on any :hover) change
                needs_hover_redraw = true;
                // Only set this to true if the :hover group actually affects the layout
                if hover_group.affects_layout {
                    needs_hover_relayout = true;
                }
            }
        })
    }

    // Collect all On::MouseEnter nodes (for both hover and focus events)
    let onmouseenter_nodes: BTreeMap<NodeId, HitTestItem> = new_hit_node_ids.iter()
        .filter(|(current_node_id, _)| previous_state.hovered_nodes.get(&current_dom_id).and_then(|hn| hn.get(current_node_id)).is_none())
        .map(|(x, y)| (*x, y.clone()))
        .collect();

    let onmouseenter_empty = onmouseenter_nodes.is_empty();

    // Insert Focus(MouseEnter) and Hover(MouseEnter)
    for (node_id, hit_test_item) in onmouseenter_nodes {
        mouse_enter!(node_id, hit_test_item, MouseEnter);
    }

    // Collect all On::MouseLeave nodes (for both hover and focus events)
    let onmouseleave_nodes: BTreeMap<NodeId, HitTestItem> = match previous_state.hovered_nodes.get(&current_dom_id) {
        Some(hn) => {
            hn.iter()
            .filter(|(prev_node_id, _)| new_hit_node_ids.get(prev_node_id).is_none())
            .map(|(x, y)| (*x, y.clone()))
            .collect()
        },
        None => BTreeMap::new(),
    };

    let onmouseleave_empty = onmouseleave_nodes.is_empty();

    // Insert Focus(MouseEnter) and Hover(MouseEnter)
    for (node_id, hit_test_item) in onmouseleave_nodes {
        mouse_enter!(node_id, hit_test_item, MouseLeave);
    }

    // If the mouse is down, but was up previously or vice versa, that means
    // that a :hover or :active state may be invalidated. In that case we need
    // to redraw the screen anyways. Setting relayout to true here in order to
    let event_is_click_or_release = event_was_mouse_down || event_was_mouse_release;
    if event_is_click_or_release || event_was_mouse_enter || event_was_mouse_leave || !onmouseenter_empty || !onmouseleave_empty {
        needs_hover_redraw = true;
        needs_hover_relayout = true;
    }

    // Insert all Not-callbacks, we need to filter out all Hover and Focus callbacks
    // and then look at what callbacks were currently

    // In order to create the Not Events we have to record which events were fired and on what nodes
    // Then we need to go through the events and fire them if the event was present, but the NodeID was not
    let mut reverse_event_hover_normal_list = BTreeMap::<HoverEventFilter, BTreeSet<NodeId>>::new();
    let mut reverse_event_focus_normal_list = BTreeMap::<FocusEventFilter, BTreeSet<NodeId>>::new();

    for (node_id, DetermineCallbackResult { normal_callbacks, .. }) in &nodes_with_callbacks {
        for event_filter in normal_callbacks.keys() {
            match event_filter {
                EventFilter::Hover(h) => {
                    reverse_event_hover_normal_list.entry(*h).or_insert_with(|| BTreeSet::new()).insert(*node_id);
                },
                EventFilter::Focus(f) => {
                    reverse_event_focus_normal_list.entry(*f).or_insert_with(|| BTreeSet::new()).insert(*node_id);
                },
                _ => { },
            }
        }
    }

    // Insert NotEventFilter callbacks
    for (node_id, not_event_filter_callback_list) in &ui_state.not_callbacks {
        for (event_filter, event_callback) in not_event_filter_callback_list {
            // If we have the event filter, but we don't have the NodeID, then insert the callback
            match event_filter {
                NotEventFilter::Hover(h) => {
                    if let Some(on_node_ids) = reverse_event_hover_normal_list.get(&h) {
                        if !on_node_ids.contains(node_id) {
                            nodes_with_callbacks.entry(*node_id)
                            .or_insert_with(|| DetermineCallbackResult::default())
                            .normal_callbacks.insert(EventFilter::Not(*event_filter), event_callback.0);
                        }
                    }
                },
                NotEventFilter::Focus(_f) => {
                    // TODO: Same thing for focus
                }
            }
        }
    }

    window_state.hovered_nodes.insert(current_dom_id, new_hit_node_ids);
    window_state.previous_window_state = Some(previous_state);

    CallbacksOfHitTest {
        needs_redraw_anyways: needs_hover_redraw,
        needs_relayout_anyways: needs_hover_relayout,
        nodes_with_callbacks,
    }
}

pub fn get_window_events(window_state: &FullWindowState) -> HashSet<WindowEventFilter> {

    use crate::window::CursorPosition::*;

    let mut events_vec = HashSet::<WindowEventFilter>::new();

    let previous_window_state = match &window_state.previous_window_state {
        Some(s) => s,
        None => return events_vec,
    };

    // mouse move events

    match (previous_window_state.mouse_state.cursor_position, window_state.mouse_state.cursor_position) {
        (InWindow(_), OutOfWindow) |
        (InWindow(_), Uninitialized) => {
            events_vec.insert(WindowEventFilter::MouseLeave);
        },
        (OutOfWindow, InWindow(_)) |
        (Uninitialized, InWindow(_)) => {
            events_vec.insert(WindowEventFilter::MouseEnter);
        },
        (InWindow(a), InWindow(b)) => {
            if a != b {
                events_vec.insert(WindowEventFilter::MouseOver);
            }
        },
        _ => { },
    }

    // click events

    if window_state.mouse_state.mouse_down() && !previous_window_state.mouse_state.mouse_down() {
        events_vec.insert(WindowEventFilter::MouseDown);
    }

    if window_state.mouse_state.left_down && !previous_window_state.mouse_state.left_down {
        events_vec.insert(WindowEventFilter::LeftMouseDown);
    }

    if window_state.mouse_state.right_down && !previous_window_state.mouse_state.right_down {
        events_vec.insert(WindowEventFilter::RightMouseDown);
    }

    if window_state.mouse_state.middle_down && !previous_window_state.mouse_state.middle_down {
        events_vec.insert(WindowEventFilter::MiddleMouseDown);
    }

    if previous_window_state.mouse_state.mouse_down() && !window_state.mouse_state.mouse_down() {
        events_vec.insert(WindowEventFilter::MouseUp);
    }

    if previous_window_state.mouse_state.left_down && !window_state.mouse_state.left_down {
        events_vec.insert(WindowEventFilter::LeftMouseUp);
    }

    if previous_window_state.mouse_state.right_down && !window_state.mouse_state.right_down {
        events_vec.insert(WindowEventFilter::RightMouseUp);
    }

    if previous_window_state.mouse_state.middle_down && !window_state.mouse_state.middle_down {
        events_vec.insert(WindowEventFilter::MiddleMouseUp);
    }

    // scroll events

    let is_scroll_previous =
        previous_window_state.mouse_state.scroll_x.is_some() ||
        previous_window_state.mouse_state.scroll_y.is_some();

    let is_scroll_now =
        window_state.mouse_state.scroll_x.is_some() ||
        window_state.mouse_state.scroll_y.is_some();

    if !is_scroll_previous && is_scroll_now {
        events_vec.insert(WindowEventFilter::ScrollStart);
    }

    if is_scroll_now {
        events_vec.insert(WindowEventFilter::Scroll);
    }

    if is_scroll_previous && !is_scroll_now {
        events_vec.insert(WindowEventFilter::ScrollEnd);
    }

    // keyboard events

    if previous_window_state.keyboard_state.current_virtual_keycode.is_none() && window_state.keyboard_state.current_virtual_keycode.is_some() {
        events_vec.insert(WindowEventFilter::VirtualKeyDown);
    }

    if window_state.keyboard_state.current_char.is_some() {
        events_vec.insert(WindowEventFilter::TextInput);
    }

    if previous_window_state.keyboard_state.current_virtual_keycode.is_some() && window_state.keyboard_state.current_virtual_keycode.is_none() {
        events_vec.insert(WindowEventFilter::VirtualKeyUp);
    }

    // misc events

    if previous_window_state.hovered_file.is_none() && window_state.hovered_file.is_some() {
        events_vec.insert(WindowEventFilter::HoveredFile);
    }

    if previous_window_state.hovered_file.is_some() && window_state.hovered_file.is_none() {
        if window_state.dropped_file.is_some() {
            events_vec.insert(WindowEventFilter::DroppedFile);
        } else {
            events_vec.insert(WindowEventFilter::HoveredFileCancelled);
        }
    }

    events_vec
}

pub fn get_hover_events(input: &HashSet<WindowEventFilter>) -> HashSet<HoverEventFilter> {
    input.iter().filter_map(|window_event| window_event.to_hover_event_filter()).collect()
}

pub fn get_focus_events(input: &HashSet<HoverEventFilter>) -> HashSet<FocusEventFilter> {
    input.iter().filter_map(|hover_event| hover_event.to_focus_event_filter()).collect()
}

/// Utility function that, given the current keyboard state and a list of
/// keyboard accelerators + callbacks, checks what callback can be invoked
/// and the first matching callback. This leads to very readable
/// (but still type checked) code like this:
///
/// ```no_run,ignore
/// use azul::prelude::{AcceleratorKey::*, VirtualKeyCode::*};
///
/// fn my_Callback(info: CallbackInfo) -> UpdateScreen {
///     keymap(info, &[
///         [vec![Ctrl, S], save_document],
///         [vec![Ctrl, N], create_new_document],
///         [vec![Ctrl, O], open_new_file],
///         [vec![Ctrl, Shift, N], create_new_window],
///     ])
/// }
/// ```
pub fn keymap<T>(
    info: CallbackInfo,
    events: &[(Vec<AcceleratorKey>, CallbackType)]
) -> UpdateScreen {

    let keyboard_state = info.get_keyboard_state().clone();

    events
        .iter()
        .filter(|(keymap_character, _)| {
            keymap_character
                .iter()
                .all(|keymap_char| keymap_char.matches(&keyboard_state))
        })
        .next()
        .and_then(|(_, callback)| (callback)(info))
}