buoyant 0.7.0

SwiftUI-like UIs in Rust for embedded devices
Documentation
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
use core::{cell::Cell, time::Duration};

use crate::{
    focus::{self, FocusAction, FocusGroup, RoleSet},
    primitives::Point,
    render::ContentShape,
};

/// An interaction event that can be handled by a view.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub enum Event {
    /// A touch event.
    Touch(embedded_touch::Touch),
    /// A scroll event with the given offset.
    Scroll(Point),
    /// A request to move focus, often driven by navigational buttons
    /// or an encoder.
    Focus {
        /// The focus action to perform.
        action: FocusAction,
        /// The focus group this event targets.
        group: FocusGroup,
    },
    /// A key was pressed.
    KeyDown(Key),
    /// A key was released.
    KeyUp(Key),
}

/// A key press event.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Key {
    /// A character key.
    Character(char),
    /// The up arrow key.
    UpArrow,
    /// The down arrow key.
    DownArrow,
    /// The left arrow key.
    LeftArrow,
    /// The right arrow key.
    RightArrow,
    /// The escape key.
    Escape,
    /// The backspace key.
    Backspace,
    /// The delete key.
    Delete,
}

impl From<FocusAction> for Event {
    fn from(action: FocusAction) -> Self {
        Self::Focus {
            action,
            group: focus::GROUP_0,
        }
    }
}

impl Event {
    /// Returns a new event with the specified offset applied to any point-based data.
    #[must_use]
    pub fn offset(&self, offset: Point) -> Self {
        let mut event = self.clone();
        match &mut event {
            Self::Touch(touch) => {
                touch.location += offset.into();
            }
            Self::Scroll(_) | Self::Focus { .. } | Self::KeyDown(_) | Self::KeyUp(_) => {}
        }
        event
    }

    /// Returns a new event with the specified focus group set on Focus events.
    ///
    /// Non-focus events are returned unchanged.
    #[must_use]
    pub fn with_focus_group(mut self, group: FocusGroup) -> Self {
        if let Self::Focus {
            group: ref mut g, ..
        } = self
        {
            *g = group;
        }
        self
    }
}

/// The result of handling an event.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum EventResult {
    /// Focus successfully moved to a new element
    Handled {
        /// The content shape of the focused element
        shape: ContentShape,
        /// The element which handled the event has focus
        request_focus: bool,
        /// The group of the focused element
        group: FocusGroup,
    },
    /// The event was not handled, or focus not obtained
    #[default]
    Deferred,
}

impl EventResult {
    /// Creates a new `EventResult` indicating the event was handled but focus was not obtained.
    #[must_use]
    pub const fn handled_unfocused() -> Self {
        Self::Handled {
            shape: ContentShape::Empty,
            request_focus: false,
            group: focus::GROUP_0,
        }
    }

    /// Creates a new `EventResult` indicating the event was handled with focus.
    #[must_use]
    pub const fn handled_focused(shape: ContentShape) -> Self {
        Self::Handled {
            shape,
            request_focus: true,
            group: focus::GROUP_0,
        }
    }

    /// Returns a new [`EventResult`] with the specified focus group.
    #[must_use]
    pub const fn with_group(mut self, group: FocusGroup) -> Self {
        let Self::Handled {
            group: event_group, ..
        } = &mut self
        else {
            return Self::Deferred;
        };
        *event_group = group;
        self
    }

    /// Returns true if the event was handled (not deferred).
    #[must_use]
    pub const fn is_handled(&self) -> bool {
        matches!(self, Self::Handled { .. })
    }

    /// Returns true if this result is from an element requesting focus.
    #[must_use]
    pub const fn requested_focus(&self) -> bool {
        matches!(
            self,
            Self::Handled {
                request_focus: true,
                ..
            }
        )
    }

    /// Returns the content shape if this result has one.
    #[must_use]
    pub fn shape(&self) -> Option<&ContentShape> {
        match self {
            Self::Handled { shape, .. } => Some(shape),
            Self::Deferred => None,
        }
    }

    /// Returns the result offset by the given amount if it has a shape.
    #[must_use]
    pub fn with_offset(mut self, offset: Point) -> Self {
        match &mut self {
            Self::Handled { shape, .. } => {
                shape.offset(offset);
            }
            Self::Deferred => (),
        }
        self
    }
}

/// Context provided to views when handling events.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EventContext {
    /// The time since the application started.
    pub app_time: Duration,
    /// Whether a redraw has been requested.
    pub redraw_requested: Cell<bool>,
    /// Whether a view rebuild has been requested.
    pub view_rebuild_requested: Cell<bool>,
    /// Which roles are currently active.
    pub roles: RoleSet,
}

impl EventContext {
    /// Creates a new `EventContext` with the given application time.
    #[must_use]
    pub const fn new(app_time: Duration) -> Self {
        Self {
            app_time,
            redraw_requested: Cell::new(false),
            view_rebuild_requested: Cell::new(false),
            roles: RoleSet::any(),
        }
    }

    #[must_use]
    pub fn with_roles(mut self, roles: impl Into<RoleSet>) -> Self {
        self.roles = roles.into();
        self
    }

    /// Indicates to the render loop that the view needs to be rebuilt.
    pub fn request_view_rebuild(&self) {
        self.view_rebuild_requested.set(true);
    }

    /// This flag indicates the view should be redrawn even if no animations were reported as
    /// active.
    ///
    /// This should be set when a view directly modifies the render tree state
    /// without requesting a view recompute, e.g. scrollview dragging.
    pub fn request_redraw(&self) {
        self.redraw_requested.set(true);
    }
}

#[cfg(feature = "embedded-graphics-simulator")]
pub mod simulator {
    use crate::{event::Key, primitives::Point};

    use super::Event;
    use embedded_graphics_simulator::{SimulatorEvent, sdl2::Keycode};
    use embedded_touch::{Phase, PointerButton, Tool, Touch, TouchPoint};

    /// Tracks mouse state and converts simulator events to touch events.
    #[derive(Debug, Default)]
    pub struct MouseTracker {
        touch: Option<Touch>,
    }

    impl MouseTracker {
        /// Creates a new mouse tracker.
        #[must_use]
        pub fn new() -> Self {
            Self { touch: None }
        }

        /// Processes a simulator event and returns the corresponding event type.
        pub fn process_event(&mut self, event: SimulatorEvent) -> Option<Event> {
            match event {
                SimulatorEvent::MouseButtonDown { point, mouse_btn } => {
                    let touch = Touch {
                        id: 0,
                        location: TouchPoint::new(point.x, point.y),
                        phase: Phase::Started,
                        tool: Tool::Pointer {
                            button: map_button(mouse_btn),
                        },
                    };
                    self.touch = Some(touch.clone());
                    Some(Event::Touch(touch))
                }
                SimulatorEvent::MouseButtonUp { point, mouse_btn } => {
                    let touch = Touch {
                        id: 0,
                        location: TouchPoint::new(point.x, point.y),
                        phase: Phase::Ended,
                        tool: Tool::Pointer {
                            button: map_button(mouse_btn),
                        },
                    };

                    self.touch = None;
                    Some(Event::Touch(touch))
                }
                SimulatorEvent::MouseMove { point } => {
                    if let Some(touch) = &mut self.touch {
                        touch.location = TouchPoint::new(point.x, point.y);
                        touch.phase = Phase::Moved;
                        Some(Event::Touch(touch.clone()))
                    } else {
                        let touch = Touch {
                            id: 0,
                            location: TouchPoint::new(point.x, point.y),
                            phase: Phase::Hovering(None),
                            tool: Tool::Pointer {
                                button: PointerButton::None,
                            },
                        };

                        Some(Event::Touch(touch))
                    }
                }
                SimulatorEvent::MouseWheel {
                    scroll_delta,
                    direction,
                } => {
                    if direction == embedded_graphics_simulator::sdl2::MouseWheelDirection::Flipped
                    {
                        Some(Event::Scroll(Point::new(
                            scroll_delta.x * 4,
                            scroll_delta.y * 4,
                        )))
                    } else {
                        Some(Event::Scroll(Point::new(
                            -scroll_delta.x * 4,
                            -scroll_delta.y * 4,
                        )))
                    }
                }
                SimulatorEvent::Quit => None,
                SimulatorEvent::KeyDown {
                    keycode,
                    keymod: _,
                    repeat: _,
                } => keycode.try_into().ok().map(Event::KeyDown),
                SimulatorEvent::KeyUp {
                    keycode,
                    keymod: _,
                    repeat: _,
                } => keycode.try_into().ok().map(Event::KeyUp),
            }
        }
    }

    fn map_button(mouse_btn: embedded_graphics_simulator::sdl2::MouseButton) -> PointerButton {
        match mouse_btn {
            embedded_graphics_simulator::sdl2::MouseButton::Left => PointerButton::Primary,
            embedded_graphics_simulator::sdl2::MouseButton::Right => PointerButton::Secondary,
            embedded_graphics_simulator::sdl2::MouseButton::Middle => PointerButton::Tertiary,
            _ => PointerButton::None,
        }
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub struct UnknownKeycode;

    impl TryFrom<embedded_graphics_simulator::sdl2::Keycode> for Key {
        type Error = UnknownKeycode;

        #[allow(clippy::too_many_lines)]
        fn try_from(
            value: embedded_graphics_simulator::sdl2::Keycode,
        ) -> Result<Self, Self::Error> {
            // FIXME: this is incomplete, sometimes wrong
            match value {
                // Arrow keys
                Keycode::RIGHT => Ok(Self::RightArrow),
                Keycode::LEFT => Ok(Self::LeftArrow),
                Keycode::DOWN => Ok(Self::DownArrow),
                Keycode::UP => Ok(Self::UpArrow),

                // Special keys
                Keycode::ESCAPE => Ok(Self::Escape),
                Keycode::DELETE => Ok(Self::Delete),
                Keycode::BACKSPACE | Keycode::KP_BACKSPACE => Ok(Self::Backspace),

                // Whitespace and control
                Keycode::TAB | Keycode::KP_TAB => Ok(Self::Character('\t')),
                Keycode::RETURN | Keycode::KP_ENTER => Ok(Self::Character('\n')),
                Keycode::SPACE | Keycode::KP_SPACE => Ok(Self::Character(' ')),

                // Punctuation (combined regular and keypad)
                Keycode::EXCLAIM | Keycode::KP_EXCLAM => Ok(Self::Character('!')),
                Keycode::QUOTEDBL => Ok(Self::Character('"')),
                Keycode::HASH | Keycode::KP_HASH => Ok(Self::Character('#')),
                Keycode::DOLLAR => Ok(Self::Character('$')),
                Keycode::PERCENT | Keycode::KP_PERCENT => Ok(Self::Character('%')),
                Keycode::AMPERSAND | Keycode::KP_AMPERSAND => Ok(Self::Character('&')),
                Keycode::QUOTE => Ok(Self::Character('\'')),
                Keycode::LEFTPAREN | Keycode::KP_LEFTPAREN => Ok(Self::Character('(')),
                Keycode::RIGHTPAREN | Keycode::KP_RIGHTPAREN => Ok(Self::Character(')')),
                Keycode::ASTERISK | Keycode::KP_MULTIPLY => Ok(Self::Character('*')),
                Keycode::PLUS | Keycode::KP_PLUS => Ok(Self::Character('+')),
                Keycode::COMMA | Keycode::KP_COMMA => Ok(Self::Character(',')),
                Keycode::MINUS | Keycode::KP_MINUS => Ok(Self::Character('-')),
                Keycode::PERIOD | Keycode::KP_PERIOD => Ok(Self::Character('.')),
                Keycode::SLASH | Keycode::KP_DIVIDE => Ok(Self::Character('/')),
                Keycode::COLON | Keycode::KP_COLON => Ok(Self::Character(':')),
                Keycode::SEMICOLON => Ok(Self::Character(';')),
                Keycode::LESS | Keycode::KP_LESS => Ok(Self::Character('<')),
                Keycode::EQUALS | Keycode::KP_EQUALS => Ok(Self::Character('=')),
                Keycode::GREATER | Keycode::KP_GREATER => Ok(Self::Character('>')),
                Keycode::QUESTION => Ok(Self::Character('?')),
                Keycode::AT | Keycode::KP_AT => Ok(Self::Character('@')),
                Keycode::LEFTBRACKET => Ok(Self::Character('[')),
                Keycode::BACKSLASH => Ok(Self::Character('\\')),
                Keycode::RIGHTBRACKET => Ok(Self::Character(']')),
                Keycode::CARET | Keycode::KP_XOR => Ok(Self::Character('^')),
                Keycode::UNDERSCORE => Ok(Self::Character('_')),
                Keycode::BACKQUOTE => Ok(Self::Character('`')),
                Keycode::KP_LEFTBRACE => Ok(Self::Character('{')),
                Keycode::KP_RIGHTBRACE => Ok(Self::Character('}')),
                Keycode::KP_VERTICALBAR => Ok(Self::Character('|')),

                // Numbers (combined regular and keypad)
                Keycode::NUM_0 | Keycode::KP_0 => Ok(Self::Character('0')),
                Keycode::NUM_1 | Keycode::KP_1 => Ok(Self::Character('1')),
                Keycode::NUM_2 | Keycode::KP_2 => Ok(Self::Character('2')),
                Keycode::NUM_3 | Keycode::KP_3 => Ok(Self::Character('3')),
                Keycode::NUM_4 | Keycode::KP_4 => Ok(Self::Character('4')),
                Keycode::NUM_5 | Keycode::KP_5 => Ok(Self::Character('5')),
                Keycode::NUM_6 | Keycode::KP_6 => Ok(Self::Character('6')),
                Keycode::NUM_7 | Keycode::KP_7 => Ok(Self::Character('7')),
                Keycode::NUM_8 | Keycode::KP_8 => Ok(Self::Character('8')),
                Keycode::NUM_9 | Keycode::KP_9 => Ok(Self::Character('9')),

                // Letters (combined regular and keypad)
                Keycode::A | Keycode::KP_A => Ok(Self::Character('a')),
                Keycode::B | Keycode::KP_B => Ok(Self::Character('b')),
                Keycode::C | Keycode::KP_C => Ok(Self::Character('c')),
                Keycode::D | Keycode::KP_D => Ok(Self::Character('d')),
                Keycode::E | Keycode::KP_E => Ok(Self::Character('e')),
                Keycode::F | Keycode::KP_F => Ok(Self::Character('f')),
                Keycode::G => Ok(Self::Character('g')),
                Keycode::H => Ok(Self::Character('h')),
                Keycode::I => Ok(Self::Character('i')),
                Keycode::J => Ok(Self::Character('j')),
                Keycode::K => Ok(Self::Character('k')),
                Keycode::L => Ok(Self::Character('l')),
                Keycode::M => Ok(Self::Character('m')),
                Keycode::N => Ok(Self::Character('n')),
                Keycode::O => Ok(Self::Character('o')),
                Keycode::P => Ok(Self::Character('p')),
                Keycode::Q => Ok(Self::Character('q')),
                Keycode::R => Ok(Self::Character('r')),
                Keycode::S => Ok(Self::Character('s')),
                Keycode::T => Ok(Self::Character('t')),
                Keycode::U => Ok(Self::Character('u')),
                Keycode::V => Ok(Self::Character('v')),
                Keycode::W => Ok(Self::Character('w')),
                Keycode::X => Ok(Self::Character('x')),
                Keycode::Y => Ok(Self::Character('y')),
                Keycode::Z => Ok(Self::Character('z')),

                _ => Err(UnknownKeycode),
            }
        }
    }
}