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
use crate::event_handler::{KeyboardEvent, MouseEvent, WidgetEvent};
use crate::state::environment::Environment;
use crate::state::state_sync::StateSync;
use crate::widget::common_widget::CommonWidget;
use crate::state::global_state::GlobalState;

pub trait Event<S>: CommonWidget<S> + StateSync<S> where S: GlobalState {
    /// A function that will be called when a mouse event occurs.
    /// It will only get called on the events where the cursor is inside.
    /// Return true if the event is consumed, and will thus not be delegated to other
    /// widgets.
    fn handle_mouse_event(&mut self, event: &MouseEvent, consumed: &bool, env: &mut Environment<S>, global_state: &mut S);

    /// A function that will get called when a keyboard event occurs.
    /// This event will be given to all widgets, no matter if they are in focus or not.
    /// This is because the focus will be decided by the widgets themselves.
    fn handle_keyboard_event(&mut self, event: &KeyboardEvent, env: &mut Environment<S>, global_state: &mut S);

    /// This will get called if there are event that are not covered by the other functions.
    /// This will get delegated to all widgets.
    /// It will never get called with mouse or keyboard events.
    /// TODO: Separate touch events. And add global state
    fn handle_other_event(&mut self, event: &WidgetEvent);

    fn process_mouse_event(&mut self, event: &MouseEvent, consumed: &bool, env: &mut Environment<S>, global_state: &mut S);

    fn process_mouse_event_default(&mut self, event: &MouseEvent, consumed: &bool, env: &mut Environment<S>, global_state: &mut S) {
        self.update_all_widget_state(env, global_state);

        if !*consumed {
            self.handle_mouse_event(event, consumed, env, global_state);
        }

        self.insert_local_state(env);

        for child in self.get_proxied_children() {
            child.process_mouse_event(event, &consumed, env, global_state);
            if *consumed { return () }
        }



        self.update_local_widget_state(env)
    }

    fn process_keyboard_event(&mut self, event: &KeyboardEvent, env: &mut Environment<S>, global_state: &mut S);

    fn process_keyboard_event_default(&mut self, event: &KeyboardEvent, env: &mut Environment<S>, global_state: &mut S) {
        self.update_all_widget_state(env, global_state);

        self.handle_keyboard_event(event, env, global_state);

        self.insert_local_state(env);

        for child in self.get_proxied_children() {
            child.process_keyboard_event(event, env, global_state);
        }

        self.update_local_widget_state(env)
    }

    fn process_other_event(&mut self, event: &WidgetEvent, env: &mut Environment<S>, global_state: &mut S);

    fn process_other_event_default(&mut self, event: &WidgetEvent, env: &mut Environment<S>, global_state: &mut S) {
        self.update_all_widget_state(env, global_state);

        self.handle_other_event(event);

        self.insert_local_state(env);

        for child in self.get_proxied_children() {
            child.process_other_event(event, env, global_state);
        }

        self.update_local_widget_state(env)
    }
}