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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2026 FernTech
use super::*;
impl WidgetTree {
/// The content id of the tooltip anchored at `widget` or anywhere inside
/// it.
///
/// The attach helpers keep the content id to themselves, so a test that
/// needs to drive a tooltip's own surface (promote it, focus into it) has
/// no other way to name it. Matching the whole subtree, not just the id,
/// is what makes this work for composing controls: `Button` keeps focus on
/// its outer node but attaches its tooltip to an inner body root.
pub fn tooltip_content_within(&self, widget: WidgetId) -> Option<WidgetId> {
self.tooltips
.iter()
.find(|e| self.is_descendant_of(e.anchor_id, widget))
.map(|e| e.content_id)
}
/// Whether that tooltip has been promoted.
///
/// Promotion is the line between an informational tip and a panel the user
/// asked for: it decides the AT role, the dismiss behaviour, and whether
/// the surface takes a Tab stop.
pub fn tooltip_is_sticky_within(&self, widget: WidgetId) -> bool {
self.tooltips
.iter()
.any(|e| self.is_descendant_of(e.anchor_id, widget) && e.is_sticky)
}
/// Simulate a click at the center of a widget.
pub fn click(&mut self, id: WidgetId) {
self.synthesise_tap(id);
}
/// Synthesise a primary-button tap at the center of `id`'s
/// resolved bounds. The OS hands the click off to the widget tree
/// even though the click never went through the normal hit-test
/// path. Used by the Windows custom-title-bar backend when
/// `WM_NCHITTEST` reported `HTMINBUTTON`/`HTMAXBUTTON`/`HTCLOSE`
/// for an area covering a `ControlButton` — the OS treated the
/// area as non-client and `WM_LBUTTONDOWN`/`UP` never fired in
/// widget land, so we re-issue a synthetic primary-button down
/// + up on the right widget.
///
/// Equivalent semantics to [`Self::click`]; named differently so
/// production call sites read clearly.
///
/// The tap runs on a standalone dispatch, so a handler it reaches
/// cannot use the multi-window API. Call
/// [`synthesise_tap_with_ops`](Self::synthesise_tap_with_ops) from
/// anywhere that already holds a real
/// [`WindowOps`](crate::window::WindowOps) sink.
pub fn synthesise_tap(&mut self, id: WidgetId) {
let mut noop = crate::window::NoopWindowOps;
self.synthesise_tap_with_ops(id, &mut noop);
}
/// [`synthesise_tap`](Self::synthesise_tap), dispatched over the
/// caller's app-level [`WindowOps`](crate::window::WindowOps) sink.
///
/// A synthetic tap is a *nested* dispatch, and everything the tapped
/// widget does happens inside it — including the intent it sends and
/// the action that intent resolves to. Dispatching it standalone
/// therefore hands that action a context with no window sink:
/// `ctx.open_window` panics, and `find_window` / `focus_window` /
/// `close_window_by_id` silently do nothing. That is how keyboard
/// activation in a menu (Enter, Space, a mnemonic, type-ahead — all
/// four route through `EventContext::synthetic_click`) lost the
/// multi-window API that the same row reached fine by mouse.
pub fn synthesise_tap_with_ops(
&mut self,
id: WidgetId,
ops: &mut dyn crate::window::WindowOps,
) {
let center = self.arena.bounds(id).center();
self.dispatch_event_with_ops(
WidgetEvent::PointerDown {
position: center,
button: PointerButton::Primary,
modifiers: Modifiers::NONE,
},
&mut *ops,
);
self.dispatch_event_with_ops(
WidgetEvent::PointerUp {
position: center,
button: PointerButton::Primary,
modifiers: Modifiers::NONE,
},
&mut *ops,
);
}
/// Simulate pointer movement to a position.
pub fn pointer_move(&mut self, position: Point) {
self.dispatch_event(WidgetEvent::PointerMove { position });
}
/// Simulate a key press (down + up), carrying the text the platform
/// attaches to the key ([`Key::to_text`]).
///
/// That text is not decoration: Escape arrives as U+001B, and a widget
/// that inspects `text` behaves differently with it than without. This
/// helper used to send `text: None` for every key, so a whole class of
/// bug was invisible to every test in the workspace — a field that
/// swallowed Escape passed the suite while failing in the user's hands.
pub fn press_key(&mut self, key: Key, modifiers: Modifiers) {
self.dispatch_event(WidgetEvent::KeyDown {
key,
modifiers,
text: key.to_text().map(str::to_string),
});
self.dispatch_event(WidgetEvent::KeyUp { key, modifiers });
}
/// Simulate typing text into the focused widget.
pub fn type_text(&mut self, _widget: WidgetId, text: &str) {
for ch in text.chars() {
self.dispatch_event(WidgetEvent::KeyDown {
key: Key::Character(ch),
modifiers: Modifiers::NONE,
text: Some(ch.to_string()),
});
}
}
/// Simulate a pointer down at a specific position with a specific button.
pub fn pointer_down_button(&mut self, position: Point, button: PointerButton) {
self.dispatch_event(WidgetEvent::PointerDown {
position,
button,
modifiers: Modifiers::NONE,
});
}
/// Simulate a pointer up at a specific position with a specific button.
pub fn pointer_up_button(&mut self, position: Point, button: PointerButton) {
self.dispatch_event(WidgetEvent::PointerUp {
position,
button,
modifiers: Modifiers::NONE,
});
}
/// Simulate a drag from one position to another.
pub fn drag(&mut self, from: Point, to: Point) {
self.dispatch_event(WidgetEvent::PointerDown {
position: from,
button: PointerButton::Primary,
modifiers: Modifiers::NONE,
});
self.dispatch_event(WidgetEvent::PointerMove { position: to });
self.dispatch_event(WidgetEvent::PointerUp {
position: to,
button: PointerButton::Primary,
modifiers: Modifiers::NONE,
});
}
/// The draggable ancestors armed by the current pointer press — the
/// observable state of the cross-widget tap-vs-drag disambiguation (see
/// `arm_drag_observers`).
///
/// Empty when the press landed inside a
/// [`gesture_dead_zone`](crate::arena::WidgetNode::gesture_dead_zone), or when
/// the pressed widget carries its own drag (the innermost drag owns the
/// gesture). Exposed so an **app** built on teksilo can assert the same thing
/// this crate's own `gesture_dead_zone_blocks_ancestor_drag_arming` asserts —
/// that a press on an interactive control inside a draggable container cannot
/// start the container's drag. Read-only; test support.
pub fn armed_drag_observers(&self) -> &[WidgetId] {
&self.drag_observers
}
/// Get bounds of a child by index.
pub fn child_bounds(&self, parent: WidgetId, index: usize) -> Rect {
let children = self.children(parent);
self.bounds(children[index])
}
/// Get a child widget ID by index.
pub fn child_widget(&self, parent: WidgetId, index: usize) -> WidgetId {
self.children(parent)[index]
}
/// Advance the simulated clock by the given duration.
/// Triggers time-dependent behavior such as long-press gesture recognition
/// and tooltip timers. Enables deterministic testing without real delays.
pub fn advance_time(&mut self, duration: std::time::Duration) {
self.sim_clock += duration;
// Mirror the new sim_clock onto the overlay manager so any
// dismiss triggered by the process_* steps below stamps its
// sim-time start in lockstep with real time.
self.overlay_manager.set_sim_clock(self.sim_clock);
self.process_tooltips();
self.process_delayed_overlays();
self.process_pointer_leave_overlays();
self.process_auto_dismiss_overlays();
self.process_overlay_fade_dismissals_sim();
}
/// Get the current simulated clock value.
pub fn simulated_now(&self) -> std::time::Instant {
self.sim_clock
}
/// Total number of live tooltip attachments, dead ones included.
///
/// Distinct from `pending_tooltip_count`, which only counts entries with a
/// running dwell. This is the raw table size — the number that must stay
/// flat across rebuilds, since `attach_tooltip*` is called from `build()`
/// and the table is scanned on every pointer move, every layout pass and
/// once per widget in the accessibility walk.
pub fn tooltip_entry_count(&self) -> usize {
self.tooltips.len()
}
/// Every widget the arena still holds — active, dormant and orphaned alike.
///
/// The number a leak test must assert on. `active_widget_count` walks the
/// tree from its roots and so cannot see the failure mode that matters
/// here: a node kept alive in the arena with nothing pointing at it. A
/// parentless orphan (tooltip content is `ctx.add`ed, hence parentless by
/// construction) is invisible to every other count in this file, and to the
/// accessibility tree, while still paying for itself in the arena's slotmap
/// forever.
pub fn widget_count(&self) -> usize {
self.arena.len()
}
/// Tear down a widget and everything it owns — its subtree, its tooltip,
/// and the parentless content it built with
/// [`add_detached`](crate::build_context::BuildContext::add_detached).
///
/// The application-facing door is `BuildContext::destroy_subtree`; this is
/// the same call for tests that hold the tree directly.
pub fn destroy_subtree_for_testing(&mut self, id: WidgetId) {
self.destroy_subtree(id);
}
/// Mark a widget as needing repaint.
pub fn mark_needs_paint(&mut self, id: WidgetId) {
self.arena.mark_needs_paint(id);
}
/// Set a widget subtree as dormant.
pub fn set_dormant(&mut self, id: WidgetId) {
self.arena.set_dormant(id);
self.arena.mark_ancestors_need_layout(id);
self.cached_frame = None;
self.a11y_dirty = true;
}
/// Activate a dormant widget subtree.
pub fn activate(&mut self, id: WidgetId) {
self.arena.activate(id);
self.arena.mark_ancestors_need_layout(id);
self.cached_frame = None;
self.a11y_dirty = true;
}
/// Invalidate all per-widget paint caches (paint AND post-paint) and
/// the assembled frame cache. Forces every widget to repaint on the
/// next `render()` call. Used by the glyph-atlas eviction recovery:
/// after an eviction, any retained frame may hold quads whose atlas
/// UVs now point at recycled slots.
pub fn invalidate_all_paints(&mut self) {
for id in self.arena.active_ids() {
if let Some(node) = self.arena.get_mut(id) {
node.dirty.needs_paint = true;
node.cached_paint = None;
node.cached_post_paint = None;
}
}
self.cached_frame = None;
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::signal::Signal;
use crate::test_widgets::{FillWidget, InsetWidget};
#[test]
fn child_bounds_helper() {
let mut tree = WidgetTree::new();
let child = tree.add(FillWidget::new());
let parent = tree.add(InsetWidget::new(5.0).set_child(child));
tree.layout(SizeProposal::exact(100.0, 50.0));
let child_bounds = tree.child_bounds(parent, 0);
assert_eq!(child_bounds.x, 5.0);
}
#[test]
fn signal_get_set_and_derived() {
let text = Signal::new(String::new());
let is_empty = text.map(|value| value.is_empty());
assert!(is_empty.get());
text.set("hello".to_string());
assert!(!is_empty.get());
}
#[test]
fn advance_time_updates_simulated_clock() {
let mut tree = WidgetTree::new();
let start = tree.simulated_now();
tree.advance_time(std::time::Duration::from_millis(500));
let end = tree.simulated_now();
assert_eq!(
end.duration_since(start),
std::time::Duration::from_millis(500)
);
}
#[test]
fn animate_to_interpolates_over_time() {
let mut tree = WidgetTree::new();
let owner = tree.add(FillWidget::new());
let signal = Signal::<f32>::new_animated(0.0);
tree.register_animated_signal(&signal, owner);
signal.animate_to(
100.0,
std::time::Duration::from_millis(200),
teksilo_tokens::Easing::Linear,
);
tree.tick_animations(std::time::Duration::from_millis(100));
assert!(
(signal.get() - 50.0).abs() < 2.0,
"at 50%: {}",
signal.get()
);
tree.tick_animations(std::time::Duration::from_millis(100));
assert!(
(signal.get() - 100.0).abs() < 0.1,
"at 100%: {}",
signal.get()
);
assert!(!tree.has_active_animations());
}
#[test]
fn animate_to_with_easing() {
let mut tree = WidgetTree::new();
let owner = tree.add(FillWidget::new());
let signal = Signal::<f32>::new_animated(0.0);
tree.register_animated_signal(&signal, owner);
signal.animate_to(
100.0,
std::time::Duration::from_millis(200),
teksilo_tokens::Easing::EaseIn,
);
tree.tick_animations(std::time::Duration::from_millis(100));
assert!(
(signal.get() - 25.0).abs() < 2.0,
"ease-in at 50%: {}",
signal.get()
);
}
#[test]
fn animate_to_replaces_in_flight() {
let mut tree = WidgetTree::new();
let owner = tree.add(FillWidget::new());
let signal = Signal::<f32>::new_animated(0.0);
tree.register_animated_signal(&signal, owner);
signal.animate_to(
100.0,
std::time::Duration::from_millis(200),
teksilo_tokens::Easing::Linear,
);
tree.tick_animations(std::time::Duration::from_millis(100));
assert!((signal.get() - 50.0).abs() < 2.0);
signal.animate_to(
0.0,
std::time::Duration::from_millis(100),
teksilo_tokens::Easing::Linear,
);
tree.tick_animations(std::time::Duration::from_millis(50));
assert!(
(signal.get() - 25.0).abs() < 3.0,
"mid-replace: {}",
signal.get()
);
tree.tick_animations(std::time::Duration::from_millis(50));
assert!(
(signal.get() - 0.0).abs() < 0.5,
"end-replace: {}",
signal.get()
);
}
#[test]
fn animation_marks_widgets_dirty() {
let mut tree = WidgetTree::new();
let widget = tree.add(FillWidget::new());
let signal = Signal::<f32>::new_animated(100.0);
tree.register_animated_signal(&signal, widget);
signal.bind_to(
widget,
tree.binding_registry(),
crate::binding::BindingLevel::Relayout,
);
tree.layout(SizeProposal::exact(200.0, 100.0));
signal.animate_to(
0.0,
std::time::Duration::from_millis(100),
teksilo_tokens::Easing::Linear,
);
tree.tick_animations(std::time::Duration::from_millis(50));
assert!(tree.needs_redraw());
}
}