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
//! An element that takes every key of the window: one built with `.captures_keys(true)` (see
//! `NodeUpdate::captures_keys`). While one is in the tree a key goes to its own `on_key_down` and `on_key_up`
//! handlers alone, before anything the window does with a key: no default of the runtime (Tab, Enter, Escape, the keys
//! of a focus group, a row of menus' Alt, typing), no other element's handlers. A host that handles keys before the
//! window (the key equivalents of a Mac's menu) asks [`keys_captured`] first, so that the accelerators of the app's
//! menu are not run either.
use std::sync::atomic::{AtomicUsize, Ordering};
use super::{Tree, find_node_by_path};
use crate::{
app::events::{EventControl, KeyboardEvent},
node::Node,
};
/// How many windows of the process have an element that takes every key.
static CAPTURING_WINDOWS: AtomicUsize = AtomicUsize::new(0);
/// Whether an element of a window of the process takes every key now. Asked by a Mac's menu before it takes a key.
#[cfg_attr(not(target_os = "macos"), allow(dead_code))]
pub(crate) fn keys_captured() -> bool {
CAPTURING_WINDOWS.load(Ordering::Relaxed) > 0
}
/// The path from `node` to the first element under it, in the order of the tree, that takes every key: not one that
/// is inert or disabled, or in one that is.
fn capturing_path(node: &Node, path: &mut Vec<usize>) -> bool {
if node.is_inert() || node.is_disabled() {
return false;
}
if node.captures_keys() {
return true;
}
for (index, child) in node.children().iter().enumerate() {
path.push(index);
if capturing_path(child, path) {
return true;
}
path.pop();
}
false
}
impl Tree {
/// The path of the element that takes every key, where one does.
fn key_capture(&self) -> Option<Vec<usize>> {
// Looked for through the whole tree, after every build and with every key: not where no node ever took keys.
if !crate::node::node::key_capture_in_use() {
return None;
}
let root = self.root.as_ref()?;
let mut path = Vec::new();
capturing_path(root, &mut path).then_some(path)
}
/// Notes for the process whether this window has an element that takes every key: after a rebuild, which may
/// have built one or taken it away.
pub(super) fn note_key_capture(&mut self) {
self.set_key_capture(self.key_capture().is_some());
}
pub(super) fn set_key_capture(&mut self, captures: bool) {
if captures != self.keys_captured {
self.keys_captured = captures;
if captures {
CAPTURING_WINDOWS.fetch_add(1, Ordering::Relaxed);
} else {
CAPTURING_WINDOWS.fetch_sub(1, Ordering::Relaxed);
}
}
}
/// Gives a key that went down (`down`) or up to the element that takes every key, alone. Returns whether one
/// took it: the window does nothing else with the key then.
#[allow(clippy::too_many_arguments)]
pub(super) fn dispatch_captured_key(
&mut self,
down: bool,
key: &str,
code: &str,
shift: bool,
ctrl: bool,
alt: bool,
meta: bool,
) -> bool {
let Some(path) = self.key_capture() else {
self.set_key_capture(false);
return false;
};
let Some(node) = self.root.as_ref().and_then(|root| find_node_by_path(root, &path)) else {
return false;
};
let mut event = KeyboardEvent {
key: key.to_owned(),
code: code.to_owned(),
shift,
ctrl,
alt,
meta,
target_id: node.node_id(),
text_input_focused: false,
composing: false,
focus_within: true,
focus_takes_enter: false,
control: EventControl::new(),
};
let handlers = if down {
&node.events.on_key_down
} else {
&node.events.on_key_up
};
for handler in handlers {
handler.call(&mut event);
if event.immediate_propagation_stopped() {
break;
}
}
// A key taken here is no Alt alone, nor one pressed with it.
self.alt_alone = false;
self.needs_redraw = true;
self.apply_reactive_updates_after_event();
self.note_key_capture();
true
}
}