zest_core/event.rs
1//! Runtime input and output events.
2
3use embedded_graphics::prelude::*;
4
5/// State of a non-touch button-like input.
6#[derive(Copy, Clone, Debug, PartialEq, Eq)]
7pub enum ButtonState {
8 /// The input was pressed.
9 Pressed,
10 /// The input was released.
11 Released,
12 /// The input repeated while held.
13 Repeated,
14}
15
16/// Keyboard key identifier.
17#[derive(Copy, Clone, Debug, PartialEq, Eq)]
18pub enum Key {
19 /// Printable character input.
20 Char(char),
21 /// Enter / return.
22 Enter,
23 /// Escape / back.
24 Escape,
25 /// Tab.
26 Tab,
27 /// Backspace.
28 Backspace,
29 /// Delete.
30 Delete,
31 /// Left arrow.
32 Left,
33 /// Right arrow.
34 Right,
35 /// Up arrow.
36 Up,
37 /// Down arrow.
38 Down,
39 /// Home.
40 Home,
41 /// End.
42 End,
43 /// Page up.
44 PageUp,
45 /// Page down.
46 PageDown,
47}
48
49/// Keyboard input delivered to screens.
50#[derive(Copy, Clone, Debug, PartialEq, Eq)]
51pub struct KeyEvent {
52 /// Which key changed.
53 pub key: Key,
54 /// Press/release/repeat state.
55 pub state: ButtonState,
56}
57
58/// Rotary encoder step input.
59#[derive(Copy, Clone, Debug, PartialEq, Eq)]
60pub struct EncoderEvent {
61 /// Signed step delta. Positive and negative directions are backend-defined.
62 pub delta: i32,
63}
64
65/// Semantic UI action independent of any particular hardware input.
66#[derive(Copy, Clone, Debug, PartialEq, Eq)]
67pub enum UiAction {
68 /// Activate the focused control.
69 Activate,
70 /// Cancel, dismiss, or go back.
71 Cancel,
72 /// Move focus forward.
73 FocusNext,
74 /// Move focus backward.
75 FocusPrevious,
76 /// Increase a value or move right/down in a control-specific way.
77 Increment,
78 /// Decrease a value or move left/up in a control-specific way.
79 Decrement,
80 /// Move focus or selection left.
81 NavigateLeft,
82 /// Move focus or selection right.
83 NavigateRight,
84 /// Move focus or selection up.
85 NavigateUp,
86 /// Move focus or selection down.
87 NavigateDown,
88 /// Open a surface such as a dropdown or menu.
89 Open,
90 /// Close a surface such as a dropdown or menu.
91 Close,
92}
93
94/// Touch event delivered to screens.
95///
96/// Re-exported so apps don't need a separate crate for the touch type.
97/// Apps that already have their own `TouchEvent` (e.g. from a touch driver
98/// crate) can implement `From` into this one.
99#[derive(Copy, Clone, Debug, PartialEq, Eq)]
100pub struct TouchEvent {
101 /// Screen-space coordinates of the touch.
102 pub point: Point,
103 /// Whether this is a touch-down, ongoing, or release.
104 pub phase: TouchPhase,
105}
106
107/// Phase of a touch event.
108#[derive(Copy, Clone, Debug, PartialEq, Eq)]
109pub enum TouchPhase {
110 /// Finger just landed on the screen.
111 Down,
112 /// Finger is still on the screen (moved).
113 Moved,
114 /// Finger left the screen.
115 Up,
116}
117
118/// Input from the platform main loop into the [`Runtime`](crate::runtime::Runtime).
119#[derive(Copy, Clone, Debug, PartialEq, Eq)]
120pub enum InputEvent {
121 /// A touch event.
122 Touch(TouchEvent),
123 /// A key event.
124 Key(KeyEvent),
125 /// A rotary encoder event.
126 Encoder(EncoderEvent),
127 /// A semantic UI action.
128 Action(UiAction),
129}
130
131/// What the runtime did this tick, instructing the platform main loop on
132/// next steps.
133#[derive(Copy, Clone, Debug, PartialEq, Eq)]
134pub enum TickResult {
135 /// Nothing changed; the platform can sleep until the next event.
136 Idle,
137 /// Visual changed; redraw and flush.
138 NeedsRedraw,
139 /// Layout changed (and so the visual); a full layout + draw pass is
140 /// required. The runtime handles this internally; the platform just
141 /// needs to redraw and flush.
142 NeedsLayout,
143}
144
145impl TickResult {
146 /// True iff the runtime wants the platform to call `draw` this tick.
147 #[must_use]
148 pub fn wants_draw(self) -> bool {
149 matches!(self, Self::NeedsRedraw | Self::NeedsLayout)
150 }
151}