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
//! XAML's `CInputManager` last-input-device record, which menus read to pick their padding states, and `CFocusManager`'s record of how the focus arrived, which decides whether the system focus visual shows.
use std::rc::Rc;
use waft_embedder::PointerDeviceKind;
use waft_foundation::Listener;
use waft_foundation::{App, Handle, ListenableObject};
use waft_gestures::{GestureBinding, PointerEvent, PointerRoute};
use waft_widgets::{AnyFocusNode, FocusManager, KeyEventResult};
/// WinUI's input categories needed by menu layout on the desktop host.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) enum InputDevice {
/// No user input has been observed yet.
#[default]
None,
/// A mouse or trackpad pointer.
Mouse,
/// Direct touch input uses the larger padding.
Touch,
/// Stylus input uses narrow padding.
Pen,
/// Keyboard activation uses narrow padding.
Keyboard,
}
/// Per-App input history; listeners observe the same native routes used by FocusManager.
#[derive(Default)]
struct InputDeviceTracker {
/// Device associated with the latest observed input.
last: InputDevice,
/// The focused node and how its focus arrived, recorded for the node that had the primary
/// focus when last read.
focus: Option<(AnyFocusNode, FocusState)>,
/// The cause a control named for the focus it is giving a node, until the focus arrives.
named: Option<(AnyFocusNode, FocusState)>,
/// Prevents registering the observers more than once.
installed: bool,
}
impl InputDevice {
/// Starts tracking before an opening control can receive input; `FocusState::coerce_programmatic` reads `None` until then.
pub(crate) fn initialize(app: &mut App) {
let tracker = app.singleton::<InputDeviceTracker>();
if app.get(tracker).installed {
return;
}
app.get_mut(tracker).installed = true;
let route = PointerRoute::new(move |app, event| {
if matches!(event, PointerEvent::Added(_) | PointerEvent::Removed(_)) {
return;
}
Self::pointer(app, event.kind());
});
GestureBinding::instance(app)
.pointer_router(app)
.add_global_route(app, route, None);
FocusManager::instance(app).add_early_key_event_handler(
app,
Rc::new(move |app, _| {
app.get_mut(tracker).last = Self::Keyboard;
KeyEventResult::Ignored
}),
);
// Records each focus change as it happens, so that later input can't stand in for its
// cause before anything reads it.
FocusManager::instance(app).add_listener(
app,
Listener::new(|app| {
FocusState::current(app);
}),
);
}
/// Raw pointer handlers can open a menu before PointerRouter runs its global observers.
pub(crate) fn pointer(app: &mut App, kind: PointerDeviceKind) {
let input = match kind {
PointerDeviceKind::Touch => Self::Touch,
PointerDeviceKind::Stylus | PointerDeviceKind::InvertedStylus => Self::Pen,
PointerDeviceKind::Mouse | PointerDeviceKind::Trackpad => Self::Mouse,
PointerDeviceKind::Unknown => return,
};
let tracker = app.singleton::<InputDeviceTracker>();
app.get_mut(tracker).last = input;
}
/// Reads the input category when a menu is opened.
pub(crate) fn current(app: &mut App) -> Self {
let tracker: Handle<InputDeviceTracker> = app.singleton();
app.get(tracker).last
}
/// MenuFlyoutItemBase::GetShouldBeNarrow.
pub(crate) fn is_narrow(self) -> bool {
matches!(self, Self::Mouse | Self::Pen | Self::Keyboard)
}
}
/// XAML `FocusState` as `CFocusManager::CoerceFocusState` records it for the focused element: the cause of the focus change, which decides whether the system focus visual is drawn.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) enum FocusState {
/// Pointer input, or programmatic focus while the last input was not a keyboard; no focus visual.
#[default]
Pointer,
/// Keyboard input, or programmatic focus while the last input was a keyboard; the focus visual shows.
Keyboard,
}
impl FocusState {
/// `CFocusManager::CoerceFocusState` for `FocusState::Programmatic`: the last input device decides, and only a keyboard yields `Keyboard`.
pub(crate) fn coerce_programmatic(app: &mut App) -> Self {
match InputDevice::current(app) {
InputDevice::Keyboard => Self::Keyboard,
InputDevice::None | InputDevice::Mouse | InputDevice::Touch | InputDevice::Pen => {
Self::Pointer
}
}
}
/// Whether `CFocusManager` draws the system focus visual for this state (`GetRealFocusStateForFocusedElement() == FocusState::Keyboard`).
pub(crate) fn shows_focus_visual(self) -> bool {
self == Self::Keyboard
}
/// How the current focus arrived, as `CFocusManager` records it for the focused element: the
/// cause a control named for it, else the input that moved it. A focus change is applied in a
/// microtask right after the event that asked for it, so the first read after it, at the
/// latest the focus manager's listener, finds that event as the last input.
pub(crate) fn current(app: &mut App) -> Self {
let primary = FocusManager::instance(app).primary_focus(app);
let tracker: Handle<InputDeviceTracker> = app.singleton();
let record = app.get(tracker);
if let Some((node, state)) = record.focus
&& Some(node) == primary
{
return state;
}
let state = match record.named {
Some((node, state)) if Some(node) == primary => state,
_ => Self::coerce_programmatic(app),
};
let record = app.get_mut(tracker);
record.focus = primary.map(|node| (node, state));
if record.named.is_some_and(|(node, _)| Some(node) == primary) {
record.named = None;
}
state
}
/// Names `state` as the cause of the focus a control is giving `node`, as XAML's
/// `Focus(state)` does; the control then focuses `node` as it would. Focus already on `node`
/// moves nothing to hear, so the record changes at once, and the control builds again.
pub(crate) fn name(app: &mut App, node: AnyFocusNode, state: Self) {
let tracker: Handle<InputDeviceTracker> = app.singleton();
if node.has_primary_focus(app) {
app.get_mut(tracker).focus = Some((node, state));
} else {
app.get_mut(tracker).named = Some((node, state));
}
}
}