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
use crate::Vec2;
use std::cell::Cell;
use std::rc::Rc;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct PointerId(pub u64);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PointerKind {
Mouse,
Touch,
Pen,
}
#[derive(Clone, Copy, Debug)]
pub enum PointerButton {
Primary, // Left mouse, touch
Secondary, // Right mouse
Tertiary, // Middle mouse
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PointerEventPass {
/// Top-down pass: ancestor -> descendant. Allows ancestors to preview or
/// intercept events before descendants see them.
Initial,
/// Bottom-up pass: descendant -> ancestor. The primary pass where gesture
/// handlers react to and consume events. A child that consumes its event
/// prevents the parent from reacting (Compose's requireUnconsumed).
Main,
/// Top-down pass: ancestor -> descendant. Allows descendants to learn
/// about events consumed by ancestors during the Main pass.
Final,
}
#[derive(Clone, Copy, Debug)]
pub enum PointerEventKind {
Down(PointerButton),
Up(PointerButton),
Move,
Cancel,
Enter,
Leave,
}
#[derive(Clone, Debug)]
pub struct PointerEvent {
pub id: PointerId,
pub kind: PointerKind,
pub event: PointerEventKind,
/// Position relative to `origin` (hit-region top-left), in physical px.
pub position: Vec2,
/// Top-left of the hit region this event is being delivered to (physical px).
pub origin: Vec2,
pub pressure: f32,
pub modifiers: Modifiers,
/// Shared consumed state -> every clone of this event points to the same
/// Cell. Calling `consume()` on any clone marks it consumed for all clones.
pub consumed: Rc<Cell<bool>>,
}
impl PointerEvent {
pub fn new(
id: PointerId,
kind: PointerKind,
event: PointerEventKind,
position: Vec2,
pressure: f32,
modifiers: Modifiers,
) -> Self {
Self {
id,
kind,
event,
position,
origin: Vec2::ZERO,
pressure,
modifiers,
consumed: Rc::new(Cell::new(false)),
}
}
/// Absolute position in window/surface physical pixels.
pub fn position_in_window(&self) -> Vec2 {
self.position + self.origin
}
/// Mark this event as consumed. Once consumed, subsequent handlers in the
/// same pass should skip processing it (equivalent to Compose's
/// `PointerInputChange.consume()`).
pub fn consume(&self) {
self.consumed.set(true);
}
/// Returns `true` if `consume()` was called on this event or any clone of it.
pub fn is_consumed(&self) -> bool {
self.consumed.get()
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Modifiers {
pub shift: bool,
pub ctrl: bool,
pub alt: bool,
pub meta: bool, // Cmd on Mac, Win key on Windows
pub command: bool, // egui like (Cmd on macOS, Ctrl elsewhere)
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Key {
Character(char),
Enter,
Tab,
Backspace,
Delete,
Insert,
Escape,
ArrowLeft,
ArrowRight,
ArrowUp,
ArrowDown,
Home,
End,
PageUp,
PageDown,
Space,
F(u8), // F1-F12
Unknown,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum KeyEventType {
/// Key pressed down.
Down,
/// Key released.
Up,
/// Unknown or unsupported event type.
Unknown,
}
#[derive(Clone, Debug)]
pub struct KeyEvent {
pub key: Key,
pub modifiers: Modifiers,
pub is_repeat: bool,
/// Whether this is a key-down or key-up event.
pub event_type: KeyEventType,
/// UTF-16 code point for character keys, or 0 for non-characters.
/// Matches Compose's `utf16CodePoint`.
pub utf16_code_point: u16,
}
#[derive(Clone, Debug)]
pub struct TextInputEvent {
pub text: String,
}
#[derive(Clone, Debug)]
pub enum ImeEvent {
/// IME composition started
Start,
/// Composition text updated
Update {
text: String,
cursor: Option<(usize, usize)>, // (start, end) of composition range
},
/// Composition committed (finalized)
Commit(String),
/// Composition cancelled
Cancel,
}
#[derive(Clone, Debug)]
pub enum InputEvent {
Pointer(PointerEvent),
Key(KeyEvent),
Text(TextInputEvent),
Ime(ImeEvent),
Gamepad(GamepadEvent),
}
/// Opaque gamepad handle. Backend-local index, stable for the connection
/// lifetime. Survives across frames; invalid after [`GamepadEvent::Disconnected`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct GamepadId(pub u32);
/// Standard-layout buttons (SDL gamecontroller mapping positions).
/// Backends translate hardware codes to these; unknown buttons are dropped.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum GamepadButton {
/// Bottom face button (A / Cross). UI default: activate.
South,
/// Right face button (B / Circle). UI default: back.
East,
/// Left face button (X / Square).
West,
/// Top face button (Y / Triangle).
North,
Start,
Select,
LeftShoulder,
RightShoulder,
LeftStick,
RightStick,
DPadUp,
DPadDown,
DPadLeft,
DPadRight,
}
/// Analog axes, normalized to -1.0..=1.0. Triggers report 0.0..=1.0.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum GamepadAxis {
LeftStickX,
LeftStickY,
RightStickX,
RightStickY,
LeftTrigger,
RightTrigger,
}
#[derive(Clone, Debug)]
pub enum GamepadEvent {
Connected {
id: GamepadId,
name: String,
},
Disconnected {
id: GamepadId,
},
Button {
id: GamepadId,
button: GamepadButton,
pressed: bool,
},
Axis {
id: GamepadId,
axis: GamepadAxis,
/// -1.0..=1.0 (sticks) or 0.0..=1.0 (triggers). Backends deadzone.
value: f32,
},
}
impl GamepadEvent {
pub fn id(&self) -> GamepadId {
match *self {
GamepadEvent::Connected { id, .. } => id,
GamepadEvent::Disconnected { id } => id,
GamepadEvent::Button { id, .. } => id,
GamepadEvent::Axis { id, .. } => id,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
pub enum InputMode {
#[default]
Touch,
/// Keyboard, Tab, arrow/D-pad, or other non-pointer navigation.
Keyboard,
}
thread_local! {
static INPUT_MODE: Cell<InputMode> = const { Cell::new(InputMode::Touch) };
}
/// Current input mode (Compose `InputModeManager.inputMode`).
///
/// Composition-local override ([`crate::locals::with_input_mode`]) wins over
/// the thread default.
#[inline]
pub fn input_mode() -> InputMode {
crate::locals::local_input_mode().unwrap_or_else(|| INPUT_MODE.get())
}
/// Force the global default input mode (no frame request). Prefer
/// [`request_input_mode`] from event handlers.
#[inline]
pub fn set_input_mode_default(mode: InputMode) {
INPUT_MODE.set(mode);
}
/// Request a new input mode. Returns `true` if the mode changed.
///
/// On change, requests a frame so focus chrome can appear/disappear.
pub fn request_input_mode(mode: InputMode) -> bool {
let prev = INPUT_MODE.get();
if prev == mode {
return false;
}
INPUT_MODE.set(mode);
crate::frame_clock::request_frame();
true
}
/// `true` when focus indication should paint (focused **and** keyboard mode).
#[inline]
pub fn is_focus_visible(focused: bool) -> bool {
focused && input_mode() == InputMode::Keyboard
}
#[cfg(test)]
mod input_mode_tests {
use super::*;
use crate::frame_clock::take_frame_request;
use crate::modifier::{Interaction, MutableInteractionSource};
#[test]
fn request_input_mode_changes_and_requests_frame() {
set_input_mode_default(InputMode::Touch);
let _ = take_frame_request();
assert!(!request_input_mode(InputMode::Touch));
assert!(!take_frame_request());
assert!(request_input_mode(InputMode::Keyboard));
assert_eq!(input_mode(), InputMode::Keyboard);
assert!(take_frame_request());
assert!(request_input_mode(InputMode::Touch));
assert_eq!(input_mode(), InputMode::Touch);
set_input_mode_default(InputMode::Touch);
}
#[test]
fn focus_visible_requires_keyboard_mode() {
set_input_mode_default(InputMode::Touch);
let src = MutableInteractionSource::new();
src.emit(Interaction::Focus);
assert!(src.source().collect_is_focused());
assert!(!src.source().collect_is_focus_visible());
set_input_mode_default(InputMode::Keyboard);
assert!(src.source().collect_is_focus_visible());
set_input_mode_default(InputMode::Touch);
src.emit(Interaction::Unfocus);
}
#[test]
fn with_input_mode_overrides_global() {
set_input_mode_default(InputMode::Touch);
crate::locals::with_input_mode(InputMode::Keyboard, || {
assert_eq!(input_mode(), InputMode::Keyboard);
});
assert_eq!(input_mode(), InputMode::Touch);
}
}