ori_core/views/
clickable.rs

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
use std::marker::PhantomData;

use ori_macro::Build;

use crate::{
    context::{BuildCx, DrawCx, EventCx, LayoutCx, RebuildCx},
    event::{Event, Key, PointerButton},
    layout::{Size, Space},
    rebuild::Rebuild,
    view::{Pod, PodState, View},
};

/// Create a new [`Clickable`], that calls `on_press` when pressed.
pub fn on_press<T, V, F>(content: V, on_press: F) -> Clickable<T, V, F>
where
    V: View<T>,
    F: FnMut(&mut EventCx, &mut T) + 'static,
{
    Clickable::new(content, ClickEvent::Press, on_press)
}

/// Create a new [`Clickable`], that calls `on_release` when released.
pub fn on_release<T, V, F>(content: V, on_release: F) -> Clickable<T, V, F>
where
    V: View<T>,
    F: FnMut(&mut EventCx, &mut T) + 'static,
{
    Clickable::new(content, ClickEvent::Release, on_release)
}

/// Create a new [`Clickable`], that calls `on_click` when clicked.
pub fn on_click<T, V, F>(content: V, on_click: F) -> Clickable<T, V, F>
where
    V: View<T>,
    F: FnMut(&mut EventCx, &mut T) + 'static,
{
    Clickable::new(content, ClickEvent::Click, on_click)
}

/// A click event.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ClickEvent {
    /// The press event.
    Press,

    /// The release event.
    Release,

    /// The click event.
    Click,
}

/// A click handler.
#[derive(Build, Rebuild)]
pub struct Clickable<T, V, F>
where
    V: View<T>,
    F: FnMut(&mut EventCx, &mut T) + 'static,
{
    /// The content.
    pub content: Pod<V>,

    /// Whether the item should be clickable when it's descendants are clicked.
    ///
    /// Defaults to `true`.
    pub descendants: bool,

    /// The button to listen for.
    ///
    /// If `Some` the callbacks will only be called when this button is pressed.
    pub button: Option<PointerButton>,

    /// The event to listen for.
    pub event: ClickEvent,

    /// The callback.
    #[build(ignore)]
    pub callback: F,

    marker: PhantomData<fn() -> T>,
}

impl<T, V, F> Clickable<T, V, F>
where
    V: View<T>,
    F: FnMut(&mut EventCx, &mut T) + 'static,
{
    /// Create a new [`Clickable`].
    pub fn new(content: V, event: ClickEvent, callback: F) -> Self {
        Self {
            content: Pod::new(content),
            descendants: true,
            button: None,
            event,
            callback,
            marker: PhantomData,
        }
    }

    fn is_button(&self, button: PointerButton) -> bool {
        self.button.map_or(true, |b| b == button)
    }
}

impl<T, V, F> View<T> for Clickable<T, V, F>
where
    V: View<T>,
    F: FnMut(&mut EventCx, &mut T) + 'static,
{
    type State = PodState<T, V>;

    fn build(&mut self, cx: &mut BuildCx, data: &mut T) -> Self::State {
        self.content.build(cx, data)
    }

    fn rebuild(&mut self, content: &mut Self::State, cx: &mut RebuildCx, data: &mut T, old: &Self) {
        Rebuild::rebuild(self, cx, old);

        self.content.rebuild(content, cx, data, &old.content);
    }

    fn event(
        &mut self,
        content: &mut Self::State,
        cx: &mut EventCx,
        data: &mut T,
        event: &Event,
    ) -> bool {
        let is_hovered = content.is_hovered() || (content.has_hovered() && self.descendants);
        let mut handled = false;

        match event {
            Event::PointerPressed(e) if is_hovered && self.is_button(e.button) => {
                if self.event == ClickEvent::Press {
                    (self.callback)(cx, data);
                    handled = true;
                }

                content.set_active(true);
            }

            Event::PointerPressed(_) if !is_hovered => {
                content.set_focused(false);
            }

            Event::PointerReleased(e) if content.is_active() && self.is_button(e.button) => {
                if self.event == ClickEvent::Release {
                    (self.callback)(cx, data);
                    handled = true;
                }

                if e.clicked && self.event == ClickEvent::Click {
                    (self.callback)(cx, data);
                    handled = true;
                }

                content.set_active(false);
            }

            Event::KeyPressed(e) if content.is_focused() => {
                if e.is_key(Key::Enter) || e.is_key(' ') {
                    if matches!(self.event, ClickEvent::Press | ClickEvent::Click) {
                        (self.callback)(cx, data);
                        handled = true;
                    }

                    content.set_active(true);
                }
            }

            Event::KeyReleased(e) if content.is_active() => {
                if e.is_key(Key::Enter) || e.is_key(' ') {
                    if self.event == ClickEvent::Release {
                        (self.callback)(cx, data);
                        handled = true;
                    }

                    content.set_active(false);
                }
            }

            _ => {}
        }

        self.content.event_maybe(handled, content, cx, data, event)
    }

    fn layout(
        &mut self,
        content: &mut Self::State,
        cx: &mut LayoutCx,
        data: &mut T,
        space: Space,
    ) -> Size {
        self.content.layout(content, cx, data, space)
    }

    fn draw(&mut self, content: &mut Self::State, cx: &mut DrawCx, data: &mut T) {
        self.content.draw(content, cx, data);
    }
}