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
//! Keyboard, pointer and text input.
//!
//! The window host turns platform events into [`Event`]s. The state of the
//! current frame is in [`Context::input`](crate::Context::input).
use crate::accessibility::Action;
use crate::context::Id;
use crate::geometry::{Vec2, vec2};
/// A key that widgets react to. Letters arrive lowercase in [`Key::Char`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[allow(missing_docs)]
pub enum Key {
Enter,
Escape,
Tab,
Space,
Backspace,
Delete,
Left,
Right,
Up,
Down,
Home,
End,
PageUp,
PageDown,
/// A character key, lowercase.
Char(char),
}
/// Modifier keys held during a key press.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Modifiers {
/// Shift.
pub shift: bool,
/// Control.
pub ctrl: bool,
/// Alt, or Option on macOS.
pub alt: bool,
/// The key for shortcuts: Command on macOS, Control elsewhere.
pub command: bool,
}
/// Something that happened since the last frame. Positions are in points.
#[derive(Clone, Debug)]
pub enum Event {
/// The pointer moved to this position.
PointerMoved(Vec2),
/// The pointer left the window.
PointerLeft,
/// The primary button or a touch went down.
PointerDown,
/// The primary button or a touch went up.
PointerUp,
/// The secondary button went down.
SecondaryDown,
/// The wheel or touchpad scrolled by this many points.
Scroll(Vec2),
/// A key was pressed.
Key(Key),
/// Text was typed.
Text(String),
/// The modifier keys changed.
Modifiers(Modifiers),
/// The window gained (`true`) or lost focus.
Focus(bool),
/// Text was pasted from the clipboard.
Paste(String),
/// Text an input method is composing and has not committed yet. The
/// range is the cursor or selection inside it, in bytes. An empty string
/// ends the composition.
Preedit(String, Option<(usize, usize)>),
/// A request from assistive technology, such as a screen reader.
Access(Id, Action),
}
/// Text an input method is composing, shown at the caret of the focused
/// field.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Preedit {
/// The text so far.
pub text: String,
/// The cursor or selection inside the text, in bytes.
pub cursor: Option<(usize, usize)>,
}
/// Input state of the current frame.
#[derive(Clone, Debug, Default)]
pub struct Input {
/// Pointer position, or `None` outside the window.
pub pointer: Option<Vec2>,
/// How far the pointer moved since the last frame.
pub delta: Vec2,
/// The primary button is down.
pub down: bool,
/// The primary button went down this frame.
pub pressed: bool,
/// The primary button went up this frame.
pub released: bool,
/// The secondary button went down this frame.
pub secondary_clicked: bool,
/// This frame's press is the second of a double click.
pub double_clicked: bool,
/// Where the last press started.
pub press_origin: Vec2,
/// Wheel movement this frame. Shift turns vertical movement sideways.
pub scroll: Vec2,
/// Keys pressed this frame. Widgets remove the ones they handle with
/// [`Input::take_key`].
pub keys: Vec<(Key, Modifiers)>,
/// Text typed or pasted this frame.
pub text: String,
/// Modifier keys held now.
pub modifiers: Modifiers,
/// The window has keyboard focus.
pub focused: bool,
/// Text an input method is composing. Persists across frames.
pub preedit: Option<Preedit>,
last_press: f64,
/// A press and its release in the same frame: the release waits one frame.
deferred_up: bool,
}
impl Input {
/// Creates the state of a focused window without input.
pub fn new() -> Self {
Self { focused: true, last_press: -10.0, ..Default::default() }
}
/// Starts a frame: clears what lasted one frame and applies `events`.
pub(crate) fn begin(&mut self, events: &[Event], time: f64) {
self.delta = Vec2::ZERO;
self.pressed = false;
self.released = false;
self.secondary_clicked = false;
self.double_clicked = false;
self.scroll = Vec2::ZERO;
self.keys.clear();
self.text.clear();
if std::mem::take(&mut self.deferred_up) {
self.released = true;
self.down = false;
}
for e in events {
match e {
Event::PointerMoved(p) => {
if let Some(old) = self.pointer {
self.delta += *p - old;
}
self.pointer = Some(*p);
}
Event::PointerLeft => {
if !self.down {
self.pointer = None;
}
}
Event::PointerDown => {
self.pressed = true;
self.down = true;
let p = self.pointer.unwrap_or(vec2(-1.0, -1.0));
self.double_clicked =
time - self.last_press < 0.35 && (p - self.press_origin).length() < 5.0;
self.last_press = if self.double_clicked { -10.0 } else { time };
self.press_origin = p;
}
Event::PointerUp => {
if self.pressed {
self.deferred_up = true;
} else {
self.released = true;
self.down = false;
}
}
Event::SecondaryDown => self.secondary_clicked = true,
Event::Scroll(d) => self.scroll += *d,
Event::Key(k) => self.keys.push((*k, self.modifiers)),
Event::Text(t) => self.text.push_str(t),
Event::Paste(t) => self.text.push_str(t),
Event::Preedit(t, c) => {
self.preedit = (!t.is_empty()).then(|| Preedit { text: t.clone(), cursor: *c });
}
Event::Access(..) => {}
Event::Modifiers(m) => self.modifiers = *m,
Event::Focus(f) => {
self.focused = *f;
if !*f && self.down {
self.down = false;
self.released = true;
}
}
}
}
// Shift turns a mouse wheel sideways.
if self.modifiers.shift && self.scroll.x == 0.0 {
self.scroll = vec2(self.scroll.y, 0.0);
}
}
/// Another frame is needed to deliver everything.
pub(crate) fn pending(&self) -> bool {
self.deferred_up
}
/// Returns `true` if `key` was pressed this frame and no widget took it.
pub fn key(&self, key: Key) -> bool {
self.keys.iter().any(|x| x.0 == key)
}
/// Returns `true` if `key` was pressed this frame, and removes it so
/// widgets drawn later do not see it.
pub fn take_key(&mut self, key: Key) -> bool {
let n = self.keys.len();
self.keys.retain(|x| x.0 != key);
n != self.keys.len()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn click_within_one_frame_is_not_lost() {
let mut i = Input::new();
i.begin(&[Event::PointerMoved(vec2(1.0, 1.0)), Event::PointerDown, Event::PointerUp], 0.0);
assert!(i.pressed && i.down && !i.released && i.pending());
i.begin(&[], 0.016);
assert!(!i.pressed && !i.down && i.released);
}
#[test]
fn double_click() {
let mut i = Input::new();
for (t, want) in [(0.0, false), (0.2, true), (0.3, false)] {
i.begin(&[Event::PointerMoved(vec2(1.0, 1.0)), Event::PointerDown], t);
assert_eq!(i.double_clicked, want);
i.begin(&[Event::PointerUp], t + 0.05);
}
}
}