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
//! Keyboard input: the focused button, Tab between buttons, and key
//! handlers.
//!
//! The platform calls [`Window::keyboard_enter`], [`Window::key`] and
//! [`Window::keyboard_leave`] for the window that has the keyboard. A key
//! goes to the focused button's `on_key`, then to the window's, and if
//! neither handled it, Tab moves the focus and Enter or Space clicks the
//! focused button. Handlers run with the tree released, inside
//! `catch_unwind`.
use std::rc::Rc;
use crabshell_reactive::{Signal, untrack};
use crate::cx::Cx;
use crate::edit::Keyed;
use crate::input::{Handler, run_isolated};
use crate::text_input::AcceptHandler;
use crate::tree::{Change, NodeId, NodeKind, Tree, borrow};
use crate::window::Window;
/// A key press, or a repeat of a key held down.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct KeyEvent {
/// Which key, as the keyboard layout names it.
pub key: Key,
/// The text it types, if any, with Shift and the layout applied:
/// `"A"` for Shift+a. Keys like Enter and Ctrl combinations type none.
pub text: Option<String>,
/// The modifiers held.
pub modifiers: Modifiers,
/// Whether this is a repeat of a key held down.
pub repeat: bool,
}
/// A key, as the keyboard layout names it.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Key {
/// Enter or Return, on the main keys or the keypad.
Enter,
/// Escape.
Escape,
/// Tab, with or without Shift.
Tab,
/// Backspace.
Backspace,
/// Delete.
Delete,
/// The left arrow.
Left,
/// The right arrow.
Right,
/// The up arrow.
Up,
/// The down arrow.
Down,
/// Home.
Home,
/// End.
End,
/// Page Up.
PageUp,
/// Page Down.
PageDown,
/// A function key, from F1.
Function(u8),
/// A key that stands for a character, such as `'a'`, `'A'` with Shift,
/// or `' '` for the space bar.
Character(char),
/// Any other key, by its XKB keysym, such as `0xffe1` for the left
/// Shift.
Other(u32),
}
/// What a key asks of the clipboard.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Clipboard {
/// Offer this text as the clipboard's.
Copy(String),
/// Read the clipboard and [`paste`](Window::paste) it into the focused
/// field.
Paste,
}
/// The modifier keys held.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Modifiers {
/// Control.
pub ctrl: bool,
/// Alt.
pub alt: bool,
/// Shift.
pub shift: bool,
/// The logo key, also called Super or the Windows key.
pub logo: bool,
}
pub(crate) type KeyHandler = Rc<Handler<dyn Fn(&Cx, &KeyEvent) -> bool>>;
/// Who has the keyboard in a window.
#[derive(Default)]
pub(crate) struct Keyboard {
/// The window has the keyboard.
pub(crate) entered: bool,
/// The focused button or field, kept while the keyboard is elsewhere.
pub(crate) focused: Option<NodeId>,
/// The window's own `on_key`.
pub(crate) handler: Option<KeyHandler>,
}
impl Keyboard {
/// Forgets nodes that were removed from the tree.
pub(crate) fn forget(&mut self, removed: &[NodeId]) {
if self.focused.is_some_and(|id| removed.contains(&id)) {
self.focused = None;
}
}
}
impl Window {
/// The keyboard entered the window: its focused button shows its focus
/// style again.
pub fn keyboard_enter(&self) {
let mut tree = borrow(&self.tree);
tree.keyboard.entered = true;
let focused = tree.keyboard.focused;
tree.show_focus(focused, true);
}
/// The keyboard left the window. The focused button stays focused for
/// when it comes back.
pub fn keyboard_leave(&self) {
let mut tree = borrow(&self.tree);
tree.keyboard.entered = false;
let focused = tree.keyboard.focused;
tree.show_focus(focused, false);
}
/// A key was pressed or repeated. A focused text field edits with it
/// first. Then the focused button's `on_key` gets it, then the
/// window's; a handler returns `true` when it used the key. Otherwise
/// Tab and Shift+Tab move the focus between buttons and fields, and
/// Enter or Space clicks the focused button.
///
/// Returns what the platform should do with the clipboard: copy a
/// field's selection on Ctrl+C or Ctrl+X, or paste into the focused
/// field with [`paste`](Self::paste) on Ctrl+V.
pub fn key(&self, event: &KeyEvent) -> Option<Clipboard> {
let typed = borrow(&self.tree).input_key(event);
match typed {
Some(Typed::Edited(signal, text, clipboard)) => {
// A signal whose owner is gone just isn't written.
signal.try_set(text);
return clipboard;
}
Some(Typed::Clipboard(clipboard)) => return Some(clipboard),
Some(Typed::Moved) => return None,
Some(Typed::Accept(call)) => {
call.run();
return None;
}
None => {}
}
let (button, window) = {
let tree = self.tree.borrow();
(tree.focused_key_call(), tree.window_key_call())
};
for call in [button, window].into_iter().flatten() {
if call.run(event) {
return None;
}
}
let click = borrow(&self.tree).default_key(event);
if let Some(click) = click {
click.run();
}
None
}
/// The path of the focused button, for tests and logs.
pub fn focused(&self) -> Option<String> {
let tree = self.tree.borrow();
let id = tree.keyboard.focused?;
tree.node(id).map(|node| node.path.to_string())
}
}
impl Tree {
/// Marks `id`, a button or field, as showing focus or not. Unlike a
/// hover change, a layout this causes checks the hover again: buttons
/// may have moved under the pointer.
fn show_focus(&mut self, id: Option<NodeId>, shown: bool) {
let Some(id) = id else {
return;
};
if matches!(
self.node(id).map(|node| &node.kind),
Some(NodeKind::Input(_))
) {
self.update(id, |node| match &mut node.kind {
NodeKind::Input(input) if input.focused != shown => {
input.focused = shown;
Change::Paint
}
_ => Change::None,
});
} else {
self.update_button_style(id, |button| button.focused = shown);
}
}
/// Moves the focus to `id`. It shows once the window has the keyboard.
/// A hidden node, or one inside a hidden widget, is refused.
pub(crate) fn focus(&mut self, id: Option<NodeId>) {
if id.is_some_and(|id| !self.shown(id)) {
return;
}
let old = std::mem::replace(&mut self.keyboard.focused, id);
if old != id {
// What an input method was composing there is dropped with it.
if let Some(old) = old
&& matches!(self.node(old).map(|node| &node.kind), Some(NodeKind::Input(input)) if input.preedit.is_some())
{
self.update_input(old, |input| {
input.preedit = None;
input.refresh();
});
}
self.show_focus(old, false);
let entered = self.keyboard.entered;
self.show_focus(id, entered);
}
}
/// What `event` does to the focused text field, if one is focused and
/// uses the key.
fn input_key(&mut self, event: &KeyEvent) -> Option<Typed> {
let id = self.keyboard.focused?;
let NodeKind::Input(input) = &mut self.node_mut(id)?.kind else {
return None;
};
let keyed = input.edit.key(event, !input.password);
let accept = keyed == Keyed::Accept;
let typed = match keyed {
Keyed::Unused => return None,
Keyed::Moved => Typed::Moved,
Keyed::Edited => Typed::Edited(input.signal, input.edit.text.clone(), None),
Keyed::Copy(text) => Typed::Clipboard(Clipboard::Copy(text)),
Keyed::Cut(text) => Typed::Edited(
input.signal,
input.edit.text.clone(),
Some(Clipboard::Copy(text)),
),
Keyed::Paste => Typed::Clipboard(Clipboard::Paste),
Keyed::Accept => {
let handler = untrack(|| input.on_accept.try_with(Option::clone)).flatten();
let text = input.edit.text.clone();
let node = self.node(id)?;
// Without its own `on_accept`, Enter goes on to the window.
Typed::Accept(AcceptCall {
cx: node.cx.clone(),
path: Rc::clone(&node.path),
handler: handler?,
text,
})
}
};
if !accept {
self.update_input(id, |input| {
input.refresh();
});
}
Some(typed)
}
/// The enabled buttons and the fields in the order they are laid out:
/// depth first, in child order.
fn focusable(&self) -> Vec<NodeId> {
let mut found = Vec::new();
let mut pending = vec![self.root];
while let Some(id) = pending.pop() {
// A hidden widget is skipped with everything in it.
if self.node(id).is_some_and(|node| !node.visible) {
continue;
}
if let Some(node) = self.node(id)
&& match &node.kind {
NodeKind::Button(button) => button.enabled,
NodeKind::Input(_) => true,
_ => false,
}
{
found.push(id);
}
let mut children = self.taffy.children(id).unwrap_or_default();
children.reverse();
pending.extend(children);
}
found
}
fn focused_key_call(&self) -> Option<KeyCall> {
let id = self.keyboard.focused?;
let node = self.node(id)?;
let NodeKind::Button(button) = &node.kind else {
return None;
};
// Reading the handlers doesn't make anything depend on them.
let handler = untrack(|| button.handlers.try_with(|handlers| handlers.key.clone()))??;
Some(KeyCall {
cx: node.cx.clone(),
path: Rc::clone(&node.path),
handler,
})
}
fn window_key_call(&self) -> Option<KeyCall> {
let node = self.node(self.root)?;
Some(KeyCall {
cx: node.cx.clone(),
path: Rc::clone(&node.path),
handler: self.keyboard.handler.clone()?,
})
}
/// What a key nobody handled does: Tab moves the focus, Enter and Space
/// click the focused button.
fn default_key(&mut self, event: &KeyEvent) -> Option<crate::input::Call> {
match event.key {
Key::Tab => {
let buttons = self.focusable();
let at = self
.keyboard
.focused
.and_then(|id| buttons.iter().position(|button| *button == id));
let count = buttons.len();
let next = match (at, event.modifiers.shift) {
_ if count == 0 => None,
(None, false) => Some(0),
(None, true) => Some(count - 1),
(Some(at), false) => Some((at + 1) % count),
(Some(at), true) => Some((at + count - 1) % count),
};
self.focus(next.map(|next| buttons[next]));
None
}
Key::Enter | Key::Character(' ') => self.click(self.keyboard.focused?),
_ => None,
}
}
}
/// What a key did in a text field, once the tree is released.
enum Typed {
/// The text changed; the signal gets it. A cut also copies.
Edited(Signal<String>, String, Option<Clipboard>),
/// The platform should copy or paste.
Clipboard(Clipboard),
/// Only the caret or selection moved, already in the tree.
Moved,
/// Enter: the field's `on_accept` runs.
Accept(AcceptCall),
}
/// A field's `on_accept`, to run once the tree is released.
struct AcceptCall {
cx: Cx,
path: Rc<str>,
handler: AcceptHandler,
text: String,
}
impl AcceptCall {
fn run(self) {
run_isolated(&self.path, "on_accept", self.handler, |f| {
f(&self.cx, &self.text)
});
}
}
/// A key handler to run once the tree is released.
struct KeyCall {
cx: Cx,
path: Rc<str>,
handler: KeyHandler,
}
impl KeyCall {
/// Whether the handler used the key. One that panicked didn't.
fn run(self, event: &KeyEvent) -> bool {
run_isolated(&self.path, "on_key", self.handler, |f| f(&self.cx, event)).unwrap_or(false)
}
}