teksilo_core/widget/event_context.rs
1// SPDX-License-Identifier: MPL-2.0
2// SPDX-FileCopyrightText: 2026 FernTech
3
4use crate::pointer::touch_action::TouchAction;
5use crate::widget_id::WidgetId;
6
7use super::CursorIcon;
8
9/// Selects which overlay-dismissal pathway runs after a handler
10/// returns. Last-write-wins: each `dismiss_*_overlays()` setter
11/// overwrites the previous choice. `None` (the default) falls
12/// through to draining individual ids from `overlay_dismissals`.
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub(crate) enum DismissScope {
15 /// Dismiss every overlay in the stack, including hosts.
16 All,
17 /// Dismiss every overlay whose content is *not* a host surface
18 /// (`Tooltip`, `Dialog`, `AlertDialog`). Used by popover triggers
19 /// and pre-show cleanup.
20 AllExceptHosts,
21 /// Walk up from the source widget's containing overlay,
22 /// dismissing menu-like overlays and stopping at the first host
23 /// surface. Used by menu / dropdown item activation.
24 SelfChain,
25 /// Dismiss the topmost overlay only.
26 Top,
27}
28
29/// One queued "reveal this rectangle" request, drained after the handler
30/// returns and turned into a [`WidgetEvent::ScrollIntoView`] per clipping
31/// ancestor.
32///
33/// A struct rather than a tuple because the three modifiers (margin, alignment,
34/// motion) are independent and positional tuples of that width stop being
35/// readable at the call site.
36///
37/// [`WidgetEvent::ScrollIntoView`]: crate::event::WidgetEvent::ScrollIntoView
38#[derive(Debug, Clone, Copy, PartialEq)]
39pub(crate) struct ScrollRevealRequest {
40 /// The target, in absolute tree (window) coordinates.
41 pub(crate) rect: teksilo_canvas::Rect,
42 /// Breathing room to keep around the target, in logical pixels.
43 pub(crate) margin: f32,
44 /// Where the target should come to rest vertically.
45 pub(crate) align: crate::event::ScrollAlign,
46 /// Whether to jump or glide.
47 pub(crate) motion: crate::event::ScrollMotion,
48 /// **Whose** ancestors to walk, when that is not the widget whose handler
49 /// queued this.
50 ///
51 /// `None` means the source widget, which is right whenever a widget reveals
52 /// something inside itself. It is wrong — silently — whenever the rect belongs
53 /// to a *different* widget: a find banner's Next button asking for a match in
54 /// the prose, a toolbar revealing a row in the list below it. Those walk the
55 /// button's ancestors, which do not include the scroll container the rect lives
56 /// in, so nothing scrolls and the reveal is a no-op no error reports.
57 pub(crate) from: Option<crate::widget_id::WidgetId>,
58}
59
60/// Context available during event handling.
61pub struct EventContext<'ops> {
62 pub(crate) cursor_request: Option<CursorIcon>,
63 pub(crate) tree_mutations: Vec<TreeMutation>,
64 pub(crate) idle_callbacks: Vec<crate::idle::IdleCallback>,
65 pub(crate) modal_requests: Vec<crate::modal::ModalRequest>,
66 pub(crate) dismiss_modal: bool,
67 pub(crate) overlay_requests: Vec<crate::overlay::OverlayRequest>,
68 pub(crate) overlay_dismissals: Vec<crate::overlay::OverlayId>,
69 /// Content widget ids whose currently-shown overlay (if any) should
70 /// be dismissed. Resolved to an `OverlayId` via
71 /// `OverlayManager::find_by_content` at drain time. Lets a handler
72 /// dismiss an overlay it can only identify by content (e.g. a single
73 /// reusable tooltip surface) — the symmetric companion to
74 /// [`cancel_delayed_overlay`](EventContext::cancel_delayed_overlay).
75 pub(crate) overlay_content_dismissals: Vec<crate::widget_id::WidgetId>,
76 /// Overlay requests that name a z-band other than the default. Kept apart
77 /// from [`overlay_requests`](Self::overlay_requests) rather than carried on
78 /// `OverlayRequest` itself: the band is a property of the *show*, not of
79 /// the request, and every existing construction site of the struct would
80 /// otherwise have to name it.
81 pub(crate) overlay_band_requests:
82 Vec<(crate::overlay::OverlayRequest, crate::overlay::OverlayBand)>,
83 /// New placements for overlays named by their content root. A
84 /// caret-anchored overlay has to be re-placed as the caret moves, and
85 /// `position_overlays` re-reads the placement it was shown with — so
86 /// without this an affordance follows nothing.
87 pub(crate) overlay_placement_updates:
88 Vec<(crate::widget_id::WidgetId, crate::overlay::OverlayPlacement)>,
89 /// Overlay ids whose `auto_dismiss_after` timer should be paused
90 /// or resumed after the handler returns (`true` = pause, `false`
91 /// = resume). Drained by `WidgetTree::collect_from_ctx` against
92 /// `OverlayManager::pause_auto_dismiss` / `resume_auto_dismiss`,
93 /// after the dismissals in the same drain: a pause aimed at an
94 /// overlay the same handler dismissed is silently dropped. Used
95 /// by `ToastHost` for hover-pause.
96 pub(crate) overlay_pause_requests: Vec<(crate::overlay::OverlayId, bool)>,
97 /// The dismissal scope chosen by the handler, if any. Set by
98 /// `dismiss_all_overlays()` / `dismiss_all_except_hosts()` /
99 /// `dismiss_self_overlay_chain()` / `dismiss_top_overlay()` —
100 /// last setter wins. `None` falls through to draining the
101 /// per-id `overlay_dismissals` vec instead.
102 pub(crate) dismiss_scope: Option<DismissScope>,
103 /// Request to capture (`true`) or release (`false`) a pointer, and which
104 /// one. `None` for the pointer means the one whose sample this handler is
105 /// serving — the default, and what every pre-multi-touch call site means.
106 pub(crate) pointer_capture: Option<(Option<crate::pointer::PointerId>, bool)>,
107 /// The widget currently holding the capture of the pointer being
108 /// dispatched, as the tree knew it when this context was made. Read by
109 /// [`owns_pointer`](EventContext::owns_pointer).
110 pub(crate) pointer_captor: Option<WidgetId>,
111 /// Whether the capture request above came from a **widget handler** rather
112 /// than from framework plumbing.
113 ///
114 /// The distinction is the whole of A4's "explicit capture is an
115 /// arbitration act": the gesture arena and the drag pipeline both capture
116 /// the pointer for their own bookkeeping, and neither is a widget staking
117 /// a claim. Only a `capture_pointer()` written in a handler enrols its
118 /// caller as a [`MemberRole::RawDrag`](crate::gesture::MemberRole::RawDrag)
119 /// competitor.
120 pub(crate) explicit_capture: bool,
121 /// A recognizer on this node produced a gesture that **owns the rest of
122 /// the press** — a drag or a swipe, as opposed to a tap, which completes
123 /// the press rather than claiming it. Set by `dispatch_recognized_gesture`
124 /// and read by `collect_from_ctx`, which decides the pointer's sequence in
125 /// the recognizer's favour.
126 pub(crate) recognized_owning_gesture: bool,
127 /// Arbitration acts the handler performed on the sequence owning the
128 /// pointer it is serving, in the order it performed them. Applied by
129 /// `WidgetTree::collect_from_ctx` against that sequence.
130 pub(crate) gesture_acts: Vec<GestureAct>,
131 /// The handler asked for its pointer's whole interaction to be revoked.
132 /// Queued by `WidgetTree::collect_from_ctx` onto the cancel funnel, so it
133 /// runs after this dispatch rather than under it. Last reason wins.
134 pub(crate) cancel_pointer_request: Option<crate::pointer::CancelReason>,
135 /// The node whose handler is running, when the dispatcher knows it.
136 /// `None` for a context made outside per-node dispatch (a gesture timer, a
137 /// key-capture callback, an async completion).
138 pub(crate) dispatch_node: Option<WidgetId>,
139 /// Delayed overlay requests (request, delay, optional focus target,
140 /// whether to dismiss sibling overlays when it finally shows).
141 pub(crate) delayed_overlay_requests: Vec<(
142 crate::overlay::OverlayRequest,
143 std::time::Duration,
144 Option<crate::widget_id::WidgetId>,
145 bool,
146 )>,
147 /// Timed overlay requests (request, auto-dismiss delay).
148 pub(crate) timed_overlay_requests: Vec<(crate::overlay::OverlayRequest, std::time::Duration)>,
149 /// Reveal overlay requests (request, caller-owned animated progress
150 /// signal, tween duration). The framework shows the overlay, then
151 /// drives `progress` 0 → 1 on show and 1 → 0 on dismiss, deferring
152 /// the actual stack removal until the roll-back tween completes —
153 /// the same deferral the fade path uses, minus the opacity scope.
154 /// The caller applies `progress` however it wants (e.g. an `Unroll`
155 /// width). See [`show_overlay_with_reveal`](EventContext::show_overlay_with_reveal).
156 pub(crate) reveal_overlay_requests: Vec<(
157 crate::overlay::OverlayRequest,
158 crate::signal::Signal<f32>,
159 std::time::Duration,
160 )>,
161 /// Dismiss descendant overlays of the source widget's containing overlay.
162 /// Optionally preserve the subtree rooted at a specific content widget ID.
163 pub(crate) dismiss_descendant_overlays: Vec<Option<crate::widget_id::WidgetId>>,
164 /// Cancel pending delayed overlays by content widget ID.
165 pub(crate) cancel_delayed_overlays: Vec<crate::widget_id::WidgetId>,
166 /// Overlays whose safe triangle should be armed at the current
167 /// pointer position once this handler returns. See
168 /// [`EventContext::arm_overlay_safe_region`].
169 pub(crate) safe_region_arm_requests: Vec<crate::widget_id::WidgetId>,
170 /// Widget IDs that need repainting (cross-widget signal propagation).
171 pub(crate) repaint_requests: Vec<crate::widget_id::WidgetId>,
172 /// Synthetic clicks to dispatch on target widgets after event processing.
173 pub(crate) synthetic_clicks: Vec<crate::widget_id::WidgetId>,
174 /// Focus requests — transfer focus to a specific widget (e.g., overlay content on open).
175 pub(crate) focus_requests: Vec<crate::widget_id::WidgetId>,
176 /// Focus-into requests — move focus to the *first focusable descendant* of
177 /// the given widget, with no fallback to the widget itself when the subtree
178 /// has none. The "dive into this region's content" intent (Enter on a tab
179 /// header → into the tab panel), distinct from `focus_requests` which
180 /// focuses the container itself as a last resort.
181 pub(crate) focus_into_requests: Vec<crate::widget_id::WidgetId>,
182 /// Rect-based "scroll this into view" requests, in **absolute tree
183 /// (window) coordinates**. Queued by [`ensure_visible`](EventContext::ensure_visible)
184 /// / [`ensure_visible_with_margin`](EventContext::ensure_visible_with_margin).
185 /// Drained in `collect_from_ctx`, which walks the ancestors of the widget
186 /// whose handler queued the request and dispatches
187 /// [`WidgetEvent::ScrollIntoView`](crate::event::WidgetEvent::ScrollIntoView)
188 /// to every `clips_children` scroll container that doesn't already fully
189 /// contain the rect — the same ancestor-walk the focus path uses, but
190 /// with a caller-supplied rectangle instead of a widget's own bounds
191 /// (so a caret, a virtualized row, or a scrolled-off tab header can be
192 /// revealed even though it is not itself a distinct focused node).
193 pub(crate) scroll_into_view_requests: Vec<ScrollRevealRequest>,
194 /// Widget-id-based "scroll this into view" requests, queued by
195 /// [`ensure_widget_visible`](EventContext::ensure_widget_visible) /
196 /// [`ensure_widget_visible_with_margin`](EventContext::ensure_widget_visible_with_margin).
197 /// Drained in `collect_from_ctx`, which resolves each id to its current
198 /// absolute arena bounds and walks *that widget's* ancestors (the target
199 /// widget itself excluded) dispatching
200 /// [`WidgetEvent::ScrollIntoView`](crate::event::WidgetEvent::ScrollIntoView).
201 /// The convenience form of the rect API for a target that is a real
202 /// mounted, non-virtualized child (a radio tile, a tab header) whose bounds
203 /// the framework already knows — the caller need not compute the rect.
204 pub(crate) scroll_widget_into_view_requests: Vec<(crate::widget_id::WidgetId, f32)>,
205 /// Keyboard-highlight tooltip requests: surface the tooltip of the given
206 /// (menu) item immediately and dismiss the previously-highlighted one.
207 /// Drained after the handler (see `event_dispatch_impl`). Only the last
208 /// entry per handler is honoured — a handler sets one highlight per key.
209 pub(crate) highlight_tooltip_requests: Vec<crate::widget_id::WidgetId>,
210 /// Drag start request: (source_widget_id, payload, optional_preview_widget).
211 pub(crate) drag_start_request: Option<(
212 crate::widget_id::WidgetId,
213 crate::drag_payload::DragPayload,
214 Option<Box<dyn crate::widget::Widget>>,
215 )>,
216 /// Cancel any active drag session.
217 pub(crate) cancel_drag: bool,
218 /// Whether the drag session active while this context is live was
219 /// started by an external (OS) drag. Read via `drag_is_external()`.
220 /// `false` for hand-constructed contexts and when no drag is active.
221 pub(crate) drag_is_external: bool,
222 /// Replace the tree-level theme. Drained after dispatch; triggers a
223 /// composite-widget rebuild and full repaint.
224 pub(crate) theme_request: Option<crate::styles::Theme>,
225 /// Request that the app follow the OS theme (native / system mode).
226 /// Drained after dispatch; the app switches to `ThemeMode::Native` and
227 /// recomputes the theme from the current OS colours. Parameterless so
228 /// `teksilo-widgets` never needs the app-layer `ThemeMode` enum.
229 pub(crate) follow_system_request: bool,
230 /// Replace the tree-level locale identifier. Drained after dispatch;
231 /// triggers a composite-widget rebuild and full repaint.
232 pub(crate) locale_request: Option<String>,
233 /// Set the user-controlled text-scale factor. Drained after dispatch and
234 /// fanned out to every window; grows all text without a rebuild.
235 pub(crate) text_scale_request: Option<f32>,
236 /// Set by `request_frame()`; consumed by the event dispatcher which
237 /// forwards it to `WidgetTree::request_frame()` so the next layout
238 /// pass advances the per-frame tick signal.
239 pub(crate) frame_requested: bool,
240 /// Optional reference to the tree's app-state registry, so handlers
241 /// can look up application-scoped values via `app_state::<T>()`.
242 /// Populated by the dispatcher before running each handler; `None`
243 /// for hand-constructed contexts in tests.
244 pub(crate) app_context: Option<std::rc::Rc<crate::event_source::TreeAppContext>>,
245 /// App-level window-ops sink. Injected by the dispatcher so
246 /// handlers can reach the multi-window API (`open_window`,
247 /// `focus_window`, …) synchronously. For `EventContext`
248 /// instances constructed outside a dispatch (standalone trees,
249 /// tests) this is `None` and the multi-window methods no-op /
250 /// return `None`.
251 pub(crate) window_ops: Option<&'ops mut dyn crate::window::WindowOps>,
252 /// [`WindowState`](crate::window::WindowState) for the window
253 /// this tree belongs to. Cloned from the tree at construction.
254 /// `None` for standalone trees.
255 pub(crate) current_window: Option<crate::window::WindowState>,
256 /// Snapshot of the tree's occlusion-aware window-active state
257 /// (`focused AND not occluded`) at construction time. Distinct from
258 /// `current_window.focused()` (raw OS focus, no occlusion). Read by
259 /// [`window_active`](Self::window_active). Defaults `true` (matches the
260 /// tree's initial value) for standalone / test contexts.
261 pub(crate) tree_window_active: bool,
262 /// Last `PointerMove` position observed by the tree. Snapshotted
263 /// at handler-invocation time so widgets that don't see the live
264 /// pointer event (e.g. an `on_hover` callback that fires on the
265 /// boundary edge) can still query "where is the cursor right
266 /// now". Read by the safe-triangle submenu hover gate.
267 pub(crate) tree_pointer_position: Option<teksilo_canvas::Point>,
268 /// True when the in-flight pointer press's hit target is a strict
269 /// descendant of the widget whose handler is currently running and that
270 /// descendant carries its own tap gesture (a chevron, checkbox, inline
271 /// button). Set per-node by the dispatcher for `PointerDown`/`PointerUp`.
272 /// Read via [`press_claimed_by_interactive_child`](Self::press_claimed_by_interactive_child).
273 pub(crate) press_claimed_by_interactive_child: bool,
274 /// Per-content-widget overlay bounds — and armed safe-triangle apex,
275 /// when the overlay has one — snapshotted at handler invocation. A
276 /// flat vec is fine: open overlays are typically 0–3 per tree. Read
277 /// by [`EventContext::overlay_bounds_for_content`] and
278 /// [`EventContext::overlay_safe_region_armed`].
279 pub(crate) overlay_bounds_snapshot: Vec<(
280 WidgetId,
281 teksilo_canvas::Rect,
282 Option<teksilo_canvas::Point>,
283 )>,
284 /// The widget holding focus when this batch began. Part of the same
285 /// per-dispatch snapshot as the two above, and read by
286 /// [`focused`](EventContext::focused).
287 pub(crate) focused_widget: Option<WidgetId>,
288 /// Intents queued by handlers via `send_intent`. Drained by the
289 /// tree after event dispatch and routed source-widget → root.
290 pub(crate) pending_intents: Vec<crate::intent::Intent>,
291 /// The dispatcher sets this to the appropriate
292 /// [`IntentSource`](crate::telemetry::IntentSource) before
293 /// invoking a typed handler (menu select → `Menu`, AccessKit
294 /// action → `Accessibility`, on_tap / button activation →
295 /// `Handler`, …). `send_intent` reads it and stamps the intent
296 /// before queuing. `None` outside a managed handler — bare
297 /// programmatic sends keep their `Intent::source` value
298 /// (default `Programmatic`).
299 pub(crate) current_source: Option<crate::telemetry::IntentSource>,
300 /// Key-capture callback armed via `ctx.begin_key_capture(...)`.
301 /// The callback + its shared slot are installed on the tree by
302 /// `collect_from_ctx`. Only one per ctx; the last caller wins.
303 pub(crate) pending_key_capture: Option<crate::shortcut::KeyCaptureSlot>,
304 /// Set to request cancellation of any armed key capture.
305 pub(crate) cancel_key_capture: bool,
306 /// Deferred mutations to the tree's [`ShortcutRegistry`](crate::shortcut::ShortcutRegistry),
307 /// typically issued by settings-UI buttons to rebind or clear
308 /// overrides. Applied in `collect_from_ctx` after the handler
309 /// returns.
310 pub(crate) pending_shortcut_mutations: Vec<ShortcutMutation>,
311 /// Requests that the app-level event loop close the window this
312 /// tree belongs to. Drained after dispatch via
313 /// `WidgetTree::take_close_window_request`. Routed through the
314 /// window's close guard (if any) — see
315 /// [`WindowConfig::on_close_requested`](crate::window::WindowConfig::on_close_requested).
316 pub(crate) close_window_requested: bool,
317 /// Like [`close_window_requested`](Self::close_window_requested), but
318 /// **bypasses** the window's close guard. Set by
319 /// [`close_window_forced`](Self::close_window_forced). Drained after
320 /// dispatch via `WidgetTree::take_force_close_request`. The escape
321 /// hatch a confirmation dialog uses once the user confirms.
322 pub(crate) force_close_requested: bool,
323 /// Set by [`request_accessibility_update`](EventContext::request_accessibility_update);
324 /// drained in `collect_from_ctx` to set `WidgetTree::a11y_dirty`, forcing the
325 /// next `sync_accessibility` to re-walk the AccessKit tree. The general lever for a
326 /// composing widget that restructured its subtree in a way that changes the AT tree
327 /// (relayout alone no longer re-walks AT).
328 pub(crate) request_a11y_update: bool,
329 /// Set by [`request_soft_keyboard`](EventContext::request_soft_keyboard);
330 /// drained in `collect_from_ctx` onto the tree, from where the app layer
331 /// takes it once per dispatch — after the IME-allowance reconcile, which
332 /// is the only place that knows whether re-asserting would cancel a live
333 /// composition.
334 pub(crate) soft_keyboard_request: Option<bool>,
335 /// Messages queued by [`announce`](EventContext::announce) /
336 /// [`announce_with`](EventContext::announce_with), drained into the tree's
337 /// own live regions by `collect_from_ctx`. See [`crate::announcer`].
338 pub(crate) announcements: Vec<(String, crate::announcer::Politeness)>,
339 /// Layout direction (LTR/RTL) of the hosting tree, snapshotted at
340 /// handler-invocation time by `make_event_context`. Read via
341 /// [`is_rtl`](EventContext::is_rtl) so pointer / keyboard / drag
342 /// handlers can mirror their x-axis logic live — a runtime locale
343 /// switch dirties the tree but does **not** rebuild, so direction
344 /// must be read here rather than captured at `build()` time.
345 /// Defaults to `LeftToRight` for hand-constructed (test) contexts.
346 pub(crate) layout_direction: crate::environment::LayoutDirection,
347 /// What the tree knows about the sample being dispatched: which pointer
348 /// produced it, where it was, and — for a scroll — its phase and source.
349 /// Snapshotted by `make_event_context` from the tree's in-flight sample.
350 /// Holds its default (a mouse at the epoch) for hand-constructed contexts
351 /// and for handlers run outside a pointer dispatch (a timer, an
352 /// accessibility action).
353 pub(crate) input: crate::pointer::InputSnapshot,
354 /// The frozen [`TouchAction`] for the gesture being handled. Defaults to
355 /// [`TouchAction::AUTO`] for a hand-constructed context and for every
356 /// handler today, since no dispatch path populates this yet — see
357 /// [`touch_action`](EventContext::touch_action) and
358 /// `crate::pointer::touch_action`.
359 pub(crate) touch_action: TouchAction,
360 /// The framework press held by the pointer being dispatched, as the router
361 /// tracks it: `(inside, pending)`. `None` when that pointer holds no press
362 /// — every handler outside a press, and every hand-constructed context.
363 /// Read by [`is_pressed`](EventContext::is_pressed) and its two siblings.
364 pub(crate) press: Option<(bool, bool)>,
365 /// Debug-only WCAG 3.2.1 guard: `Some(flag)` where `flag` is set while a
366 /// focus-change dispatch is running. `open_window` / `focus_window` warn if
367 /// invoked while it reads `true` (a focus handler changing context). `None`
368 /// for hand-constructed (test) contexts.
369 pub(crate) in_focus_dispatch: Option<std::rc::Rc<std::cell::Cell<bool>>>,
370}
371
372/// One arbitration act a handler performed on its pointer's sequence.
373///
374/// Queued on the context and applied in order by
375/// `WidgetTree::collect_from_ctx`, so a handler that claims and then rejects
376/// leaves the sequence in the state its last word describes.
377#[derive(Debug, Clone, Copy, PartialEq, Eq)]
378pub(crate) enum GestureAct {
379 /// [`EventContext::claim_gesture`].
380 Claim,
381 /// [`EventContext::reject_gesture`].
382 Reject,
383 /// [`EventContext::hold_gesture`].
384 Hold,
385 /// [`EventContext::release_gesture`].
386 Release,
387}
388
389/// Deferred edit to the tree's shortcut registry, queued on an
390/// `EventContext` and applied in `collect_from_ctx`.
391#[derive(Debug, Clone)]
392pub(crate) enum ShortcutMutation {
393 RebindPrimary {
394 id: String,
395 keystroke: Option<crate::shortcut::KeyStroke>,
396 },
397 RebindSecondary {
398 id: String,
399 keystroke: Option<crate::shortcut::KeyStroke>,
400 },
401 ClearOverride {
402 id: String,
403 },
404}
405
406/// A structural change to the widget tree, deferred until after event dispatch.
407pub(crate) enum TreeMutation {
408 SetDormant(WidgetId),
409 Activate(WidgetId),
410 Destroy(WidgetId),
411 /// Typed mutable access to a mounted widget, applied in
412 /// `apply_tree_mutations` where `&mut arena` is live. The boxed closure
413 /// downcasts the node's `as_any_mut()` to the requested concrete type;
414 /// `dirty` selects the post-mutation re-render level.
415 WithWidgetMut {
416 id: WidgetId,
417 dirty: crate::binding::BindingLevel,
418 apply: Box<dyn FnOnce(&mut dyn std::any::Any)>,
419 },
420 /// Re-run one widget's `build()` **now**, inside `apply_tree_mutations`,
421 /// rather than marking it for the next layout pass. See
422 /// [`EventContext::materialize_now`].
423 MaterializeNow(WidgetId),
424 /// `Space` on a data view's focused row: run the keyboard-toggle action
425 /// published inside `row`, or `fallback` when the row publishes none.
426 ///
427 /// Deferred rather than resolved in the handler because finding the action
428 /// means walking the row's subtree, and `EventContext` is a command buffer
429 /// with no view of the arena. Carrying the fallback keeps the decision in
430 /// one place: whether a row has a checkbox is a fact about the tree, not
431 /// something the key handler can know.
432 RowSpaceActivate {
433 row: WidgetId,
434 fallback: std::rc::Rc<dyn Fn()>,
435 },
436}
437
438// Manual `Debug`: the `WithWidgetMut` closure is not `Debug`.
439impl std::fmt::Debug for TreeMutation {
440 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
441 match self {
442 Self::SetDormant(id) => f.debug_tuple("SetDormant").field(id).finish(),
443 Self::Activate(id) => f.debug_tuple("Activate").field(id).finish(),
444 Self::MaterializeNow(id) => f.debug_tuple("MaterializeNow").field(id).finish(),
445 Self::Destroy(id) => f.debug_tuple("Destroy").field(id).finish(),
446 Self::WithWidgetMut { id, dirty, .. } => f
447 .debug_struct("WithWidgetMut")
448 .field("id", id)
449 .field("dirty", dirty)
450 .finish_non_exhaustive(),
451 Self::RowSpaceActivate { row, .. } => f
452 .debug_struct("RowSpaceActivate")
453 .field("row", row)
454 .finish_non_exhaustive(),
455 }
456 }
457}
458
459impl<'ops> EventContext<'ops> {
460 pub(crate) fn new() -> Self {
461 Self {
462 cursor_request: None,
463 tree_mutations: Vec::new(),
464 idle_callbacks: Vec::new(),
465 modal_requests: Vec::new(),
466 dismiss_modal: false,
467 overlay_requests: Vec::new(),
468 overlay_dismissals: Vec::new(),
469 overlay_content_dismissals: Vec::new(),
470 overlay_band_requests: Vec::new(),
471 overlay_placement_updates: Vec::new(),
472 overlay_pause_requests: Vec::new(),
473 dismiss_scope: None,
474 pointer_capture: None,
475 pointer_captor: None,
476 dispatch_node: None,
477 delayed_overlay_requests: Vec::new(),
478 timed_overlay_requests: Vec::new(),
479 reveal_overlay_requests: Vec::new(),
480 dismiss_descendant_overlays: Vec::new(),
481 cancel_delayed_overlays: Vec::new(),
482 safe_region_arm_requests: Vec::new(),
483 repaint_requests: Vec::new(),
484 synthetic_clicks: Vec::new(),
485 focus_requests: Vec::new(),
486 focus_into_requests: Vec::new(),
487 scroll_into_view_requests: Vec::new(),
488 scroll_widget_into_view_requests: Vec::new(),
489 highlight_tooltip_requests: Vec::new(),
490 drag_start_request: None,
491 cancel_drag: false,
492 drag_is_external: false,
493 theme_request: None,
494 follow_system_request: false,
495 locale_request: None,
496 text_scale_request: None,
497 frame_requested: false,
498 app_context: None,
499 pending_intents: Vec::new(),
500 current_source: None,
501 pending_key_capture: None,
502 cancel_key_capture: false,
503 pending_shortcut_mutations: Vec::new(),
504 close_window_requested: false,
505 force_close_requested: false,
506 request_a11y_update: false,
507 soft_keyboard_request: None,
508 announcements: Vec::new(),
509 window_ops: None,
510 current_window: None,
511 tree_window_active: true,
512 tree_pointer_position: None,
513 press_claimed_by_interactive_child: false,
514 overlay_bounds_snapshot: Vec::new(),
515 focused_widget: None,
516 layout_direction: crate::environment::LayoutDirection::LeftToRight,
517 input: crate::pointer::InputSnapshot::default(),
518 touch_action: TouchAction::AUTO,
519 press: None,
520 in_focus_dispatch: None,
521 explicit_capture: false,
522 recognized_owning_gesture: false,
523 gesture_acts: Vec::new(),
524 cancel_pointer_request: None,
525 }
526 }
527
528 /// Record the [`TouchAction`] frozen at press for the sequence owning the
529 /// pointer being dispatched. Called by `make_event_context`.
530 pub(crate) fn with_touch_action(mut self, action: TouchAction) -> Self {
531 self.touch_action = action;
532 self
533 }
534
535 /// Record the framework press held by the pointer being dispatched, as
536 /// `(inside, pending)`. Called by `make_event_context`.
537 pub(crate) fn with_press(mut self, press: Option<(bool, bool)>) -> Self {
538 self.press = press;
539 self
540 }
541
542 /// Whether the pointer being dispatched holds a press whose visual is
543 /// showing — inside its tap boundary and past any press-feedback delay.
544 ///
545 /// The framework already drives the pressed node's own
546 /// [`pressed_signal`](crate::BuildContext::pressed_signal) from the same
547 /// state; this is for a handler that has to *branch* on the press rather
548 /// than paint it. `false` outside a press.
549 pub fn is_pressed(&self) -> bool {
550 matches!(self.press, Some((true, false)))
551 }
552
553 /// Whether the pointer being dispatched holds a press that has not left
554 /// its tap boundary. Unlike [`is_pressed`](Self::is_pressed) this is still
555 /// true during the press-feedback delay: the press is real, only its
556 /// visual is being withheld.
557 pub fn press_is_inside(&self) -> bool {
558 matches!(self.press, Some((true, _)))
559 }
560
561 /// Whether the pointer being dispatched holds a press whose feedback delay
562 /// has not elapsed — the press is inside a pan claimant and the framework
563 /// is waiting to see whether it becomes a scroll.
564 pub fn press_pending(&self) -> bool {
565 matches!(self.press, Some((_, true)))
566 }
567
568 /// Record what the tree knows about the sample being dispatched. Called by
569 /// `make_event_context` once per event batch.
570 pub(crate) fn with_input_snapshot(mut self, input: crate::pointer::InputSnapshot) -> Self {
571 self.input = input;
572 self
573 }
574
575 /// Record who holds the capture of the pointer being dispatched, so
576 /// [`owns_pointer`](Self::owns_pointer) can answer without a tree lookup.
577 pub(crate) fn with_pointer_captor(mut self, captor: Option<WidgetId>) -> Self {
578 self.pointer_captor = captor;
579 self
580 }
581
582 /// Record which node's handler is about to run.
583 pub(crate) fn with_dispatch_node(mut self, node: WidgetId) -> Self {
584 self.dispatch_node = Some(node);
585 self
586 }
587
588 /// The pointer that produced the event being handled.
589 ///
590 /// Two dispatches have a pointer without having a sample, and both report
591 /// it: a gesture the timer recognised — a hold — reports the **contact that
592 /// held**, and a drag-and-drop handler (`on_drag_hover` / `on_drag_tick` /
593 /// `on_drag_leave` / `on_drop`) reports the pointer **that started the
594 /// drag**, which is what makes it right inside a tick fired from a layout
595 /// pass or an OS drag phase delivered from a platform thread. Outside any
596 /// pointer, scroll, gesture or drag dispatch — an assistive-technology
597 /// action, a hand-constructed test context — this is the mouse at the tree
598 /// epoch, which is the same answer every such handler got before pointers
599 /// were distinguishable.
600 pub fn pointer(&self) -> crate::pointer::PointerInfo {
601 self.input.pointer
602 }
603
604 /// What kind of device is pointing: mouse, finger, stylus.
605 ///
606 /// The one question most handlers actually need — it is what decides
607 /// whether a hover affordance is reachable, whether a target needs slop,
608 /// and which gesture profile governs.
609 pub fn pointer_kind(&self) -> teksilo_tokens::PointerKind {
610 self.input.pointer.kind
611 }
612
613 /// Where the pointer was, in window-logical coordinates, when the event
614 /// being handled was produced.
615 ///
616 /// `None` for an event that carries no position — a keyboard-driven
617 /// scroll, a wheel notch (which routes by hover rather than by position),
618 /// anything dispatched outside a pointer sample. Distinct from
619 /// [`tree_pointer_position`](Self::tree_pointer_position), which reports
620 /// where the pointer is *at this instant* regardless of what is being
621 /// dispatched.
622 pub fn pointer_position(&self) -> Option<teksilo_canvas::Point> {
623 self.input.position
624 }
625
626 /// Where in a continuous scroll gesture the event being handled sits.
627 ///
628 /// [`ScrollPhase::Discrete`](crate::pointer::ScrollPhase::Discrete) — a
629 /// self-contained wheel notch — for everything that is not a phased
630 /// gesture, which is every scroll Teksilo produced before the touch
631 /// programme.
632 pub fn scroll_phase(&self) -> crate::pointer::ScrollPhase {
633 self.input.scroll_phase
634 }
635
636 /// What produced the scroll being handled: a notched wheel, a precision
637 /// trackpad, a synthesised touch pan, or the app itself.
638 pub fn scroll_source(&self) -> crate::pointer::ScrollSource {
639 self.input.scroll_source
640 }
641
642 /// The [`TouchAction`] governing the gesture being handled.
643 ///
644 /// The value is **frozen at press** for the whole gesture's lifetime: the
645 /// router computes it once, from `WidgetTree::effective_touch_action` of
646 /// the pressed target, and stores it on that pointer's
647 /// [`PointerSequence`](crate::gesture::PointerSequence), so a handler never
648 /// re-reads a subtree that may have rebuilt mid-gesture.
649 ///
650 /// [`TouchAction::AUTO`] — the neutral value — outside a press, and for a
651 /// hand-constructed context. A mouse never consults this at all. See
652 /// `crate::pointer::touch_action`.
653 pub fn touch_action(&self) -> TouchAction {
654 self.touch_action
655 }
656
657 /// Snapshot the hosting tree's layout direction. Called by
658 /// `make_event_context` once per event batch so x-axis handlers
659 /// (resize, drag-reorder, arrow-key navigation) can mirror under
660 /// RTL without a rebuild.
661 pub(crate) fn with_layout_direction(
662 mut self,
663 direction: crate::environment::LayoutDirection,
664 ) -> Self {
665 self.layout_direction = direction;
666 self
667 }
668
669 /// Layout direction of the hosting tree at dispatch time.
670 pub fn layout_direction(&self) -> crate::environment::LayoutDirection {
671 self.layout_direction
672 }
673
674 /// Whether the hosting tree is laid out right-to-left. Mirrors
675 /// [`LayoutContext::is_rtl`](crate::widget::LayoutContext::is_rtl)
676 /// for the event-dispatch side.
677 pub fn is_rtl(&self) -> bool {
678 self.layout_direction == crate::environment::LayoutDirection::RightToLeft
679 }
680
681 /// The widget that held focus when this event batch began.
682 ///
683 /// A snapshot, not a live read: it answers what the tree's focus was at
684 /// dispatch time, so a handler that has already called
685 /// [`request_focus`](EventContext::request_focus) still sees the old
686 /// value. That is the useful reading for a handler deciding *whether* to
687 /// act on the focused widget.
688 ///
689 /// `None` when nothing is focused, and also for an `EventContext` built
690 /// outside `WidgetTree::make_event_context`, which is what a hand-made
691 /// test context is. Treat it as `None`-safe, like the other snapshots.
692 ///
693 /// The reason this exists: a widget-scoped shortcut fires before the
694 /// focused widget sees the key, so a container that binds a key which its
695 /// own children also handle has no other way to yield to them.
696 /// `MessageBox` is the case that asked for it, where Enter is bound to the
697 /// default button and must not answer for the button the user has actually
698 /// tabbed to.
699 pub fn focused(&self) -> Option<WidgetId> {
700 self.focused_widget
701 }
702
703 /// Attach a per-dispatch snapshot of read-only tree query state
704 /// (current pointer position, overlay bounds, focus). Called by
705 /// `WidgetTree::make_event_context` once per event batch. Test
706 /// `EventContext`s that don't go through that path stay with
707 /// empty snapshots — handlers must treat every read as `None`-
708 /// safe.
709 pub(crate) fn with_query_snapshot(
710 mut self,
711 pointer: Option<teksilo_canvas::Point>,
712 overlays: Vec<(
713 WidgetId,
714 teksilo_canvas::Rect,
715 Option<teksilo_canvas::Point>,
716 )>,
717 focused: Option<WidgetId>,
718 ) -> Self {
719 self.tree_pointer_position = pointer;
720 self.overlay_bounds_snapshot = overlays;
721 self.focused_widget = focused;
722 self
723 }
724
725 /// Attach the app-level window-ops sink and the hosting tree's
726 /// [`WindowState`](crate::window::WindowState). Called by the
727 /// dispatcher once per event batch so handlers can reach the
728 /// multi-window API synchronously.
729 pub(crate) fn with_window_context(
730 mut self,
731 ops: &'ops mut dyn crate::window::WindowOps,
732 current_window: Option<crate::window::WindowState>,
733 ) -> Self {
734 self.window_ops = Some(ops);
735 self.current_window = current_window;
736 self
737 }
738
739 /// Snapshot the tree's occlusion-aware window-active state. Called by
740 /// `make_event_context` once per event batch so handlers can read
741 /// [`window_active`](Self::window_active).
742 pub(crate) fn with_window_active(mut self, active: bool) -> Self {
743 self.tree_window_active = active;
744 self
745 }
746
747 /// Attach the tree's shared "inside focus dispatch" flag (WCAG 3.2.1
748 /// debug guard). See [`EventContext::open_window`].
749 pub(crate) fn with_focus_dispatch_flag(
750 mut self,
751 flag: std::rc::Rc<std::cell::Cell<bool>>,
752 ) -> Self {
753 self.in_focus_dispatch = Some(flag);
754 self
755 }
756
757 /// Debug-only: warn (once per call) if a context change is being made from
758 /// inside a focus-change dispatch — a WCAG 3.2.1 (On Focus) anti-pattern.
759 /// Compiled out entirely in release builds.
760 #[inline]
761 fn warn_if_context_change_in_focus_dispatch(&self, what: &str) {
762 #[cfg(debug_assertions)]
763 if self.in_focus_dispatch.as_ref().is_some_and(|f| f.get()) {
764 eprintln!(
765 "[teksilo a11y] WCAG 3.2.1 (On Focus): `{what}` was called from \
766 inside an on_focus handler. Changing context (opening/focusing a \
767 window, navigating) merely because a control received focus \
768 surprises keyboard users tabbing through the UI. Move this to an \
769 explicit activation handler (on_tap / on_activate / a shortcut)."
770 );
771 }
772 let _ = what;
773 }
774
775 /// Attach the tree's app-state registry so handlers can look up
776 /// application-scoped values (`ClipboardHandle`, `SharedTypesetter`,
777 /// …). Called by the dispatcher once per event batch.
778 pub(crate) fn with_app_context(
779 mut self,
780 ctx: std::rc::Rc<crate::event_source::TreeAppContext>,
781 ) -> Self {
782 self.app_context = Some(ctx);
783 self
784 }
785
786 /// Record whether the drag session active while this context is live
787 /// originated from an external (OS) drag. Set by `make_event_context`.
788 pub(crate) fn with_drag_external(mut self, is_external: bool) -> Self {
789 self.drag_is_external = is_external;
790 self
791 }
792
793 /// Whether a drag is currently in flight that was started by an external
794 /// (OS) drag-and-drop (files / text / URLs from another application),
795 /// rather than by an in-app `start_drag`. Useful in `on_drag_leave` /
796 /// `on_drag_tick` handlers, which don't receive the payload directly;
797 /// in `on_drag_hover` / `on_drop` prefer `payload.is_external()`.
798 pub fn drag_is_external(&self) -> bool {
799 self.drag_is_external
800 }
801
802 /// Look up an application-scoped value by type. Mirrors
803 /// `BuildContext::app_state`. Returns `None` when the handler was
804 /// invoked without a registry (hand-constructed `EventContext` in
805 /// tests, or when no value of that type was registered).
806 pub fn app_state<T: 'static>(&self) -> Option<&T> {
807 self.app_context
808 .as_ref()
809 .and_then(|ctx| ctx.app_state::<T>())
810 }
811
812 /// Borrow the [`AppEventPoster`](crate::AppEventPoster) installed
813 /// by the framework. Used by integrations that need to post
814 /// typed payloads back to the UI loop from a worker thread
815 /// (`teksilo_platform::file_dialog`'s `RfdAsyncBackend`, future
816 /// async-result features). Returns `None` for hand-constructed
817 /// `EventContext`s in tests.
818 pub fn poster(&self) -> Option<&std::sync::Arc<dyn crate::AppEventPoster>> {
819 self.app_context.as_ref().and_then(|ctx| ctx.poster())
820 }
821
822 /// Ask the tree to pump one more frame after this handler returns.
823 /// Use from event handlers that kick off per-frame work (pending
824 /// document events to drain, drag-select auto-scroll, caret blink
825 /// restart on focus). See `WidgetTree::request_frame` for the
826 /// draw-when-needed contract.
827 pub fn request_frame(&mut self) {
828 self.frame_requested = true;
829 }
830
831 /// Dispatch an [`Intent`](crate::intent::Intent) as if the source
832 /// widget pressed its keyboard shortcut. The framework walks
833 /// source-widget → root after the current handler returns,
834 /// invoking any matching [`Action`](crate::action::Action) it
835 /// finds. Unmatched intents are silently dropped.
836 ///
837 /// The intent's `source` is overridden by the dispatcher's
838 /// current handler-source label (`current_source`) when one is
839 /// active. This is how the framework distinguishes
840 /// `IntentSource::Handler` (button taps, generic on_tap) from
841 /// `IntentSource::Menu`, `IntentSource::Accessibility`, etc.
842 /// Programmatic callers outside any handler pass through with
843 /// `IntentSource::Programmatic` (the default).
844 pub fn send_intent(&mut self, intent: impl Into<crate::intent::Intent>) {
845 let mut intent: crate::intent::Intent = intent.into();
846 if let Some(src) = self.current_source {
847 intent.source = src;
848 }
849 self.pending_intents.push(intent);
850 }
851
852 /// Run a closure with the given `IntentSource` active. Any
853 /// `ctx.send_intent(...)` issued from within the closure will
854 /// be tagged with this source instead of the dispatcher's
855 /// default (`Handler` / `Shortcut` / `Accessibility`).
856 ///
857 /// The previous source is restored after the closure returns.
858 /// Panic during the closure unwinds the dispatcher's whole
859 /// frame, so the EventContext is destroyed before the next
860 /// dispatch — no need for a panic-safe drop guard.
861 ///
862 /// Used by framework widgets that want a more specific source
863 /// label than the default — `MenuItem` wraps its activation
864 /// handler to emit `IntentSource::Menu`, etc.
865 pub fn with_intent_source<R>(
866 &mut self,
867 source: crate::telemetry::IntentSource,
868 f: impl FnOnce(&mut Self) -> R,
869 ) -> R {
870 let prev = self.current_source.replace(source);
871 let r = f(self);
872 self.current_source = prev;
873 r
874 }
875
876 /// Arm a one-shot key-capture callback, returning a
877 /// [`CaptureHandle`](crate::shortcut::CaptureHandle) whose `Drop`
878 /// cancels the capture if it hasn't fired yet. The next `KeyDown`
879 /// bypasses shortcut resolution and invokes the callback with:
880 /// - the captured [`KeyStroke`](crate::shortcut::KeyStroke)
881 /// - mutable access to the registry (rebinds in-place)
882 /// - a mutable [`EventContext`] (emit commands, send intents,
883 /// dismiss overlays, …)
884 ///
885 /// The handle must be stored somewhere with an appropriate
886 /// lifetime (typically in the calling widget's state) or the
887 /// capture will be cancelled immediately when the returned
888 /// handle drops at end of scope.
889 pub fn begin_key_capture(
890 &mut self,
891 callback: impl FnOnce(
892 crate::shortcut::KeyStroke,
893 &mut crate::shortcut::ShortcutRegistry,
894 &mut EventContext,
895 ) + 'static,
896 ) -> crate::shortcut::CaptureHandle {
897 let slot: crate::shortcut::KeyCaptureSlot =
898 std::rc::Rc::new(std::cell::RefCell::new(Some(Box::new(callback))));
899 self.pending_key_capture = Some(slot.clone());
900 self.cancel_key_capture = false;
901 crate::shortcut::CaptureHandle::new(slot)
902 }
903
904 /// Cancel any key capture armed earlier in this handler or via
905 /// `WidgetTree::begin_key_capture` before the handler ran.
906 pub fn cancel_key_capture(&mut self) {
907 self.pending_key_capture = None;
908 self.cancel_key_capture = true;
909 }
910
911 /// Queue a deferred rebind of the primary keystroke for the
912 /// registered shortcut with the given id. Applied by the tree
913 /// after the current handler returns. Use `None` to explicitly
914 /// unbind the slot.
915 pub fn rebind_shortcut_primary(
916 &mut self,
917 id: impl Into<String>,
918 keystroke: Option<crate::shortcut::KeyStroke>,
919 ) {
920 self.pending_shortcut_mutations
921 .push(ShortcutMutation::RebindPrimary {
922 id: id.into(),
923 keystroke,
924 });
925 }
926
927 /// Queue a deferred rebind of the secondary keystroke for the
928 /// registered shortcut with the given id.
929 pub fn rebind_shortcut_secondary(
930 &mut self,
931 id: impl Into<String>,
932 keystroke: Option<crate::shortcut::KeyStroke>,
933 ) {
934 self.pending_shortcut_mutations
935 .push(ShortcutMutation::RebindSecondary {
936 id: id.into(),
937 keystroke,
938 });
939 }
940
941 /// Queue a deferred clear of any user override for the given
942 /// shortcut id, restoring its declared defaults.
943 pub fn clear_shortcut_override(&mut self, id: impl Into<String>) {
944 self.pending_shortcut_mutations
945 .push(ShortcutMutation::ClearOverride { id: id.into() });
946 }
947
948 /// Request that the application close the window this tree
949 /// belongs to. Drained by the app event loop after the handler
950 /// returns. Typical use: title-bar close button handlers.
951 ///
952 /// This is a *guarded* close: if the window declared a close guard
953 /// via
954 /// [`WindowConfig::on_close_requested`](crate::window::WindowConfig::on_close_requested)
955 /// or [`can_close`](crate::window::WindowConfig::can_close), that
956 /// guard runs first and may veto the close. To skip the guard (e.g.
957 /// from the confirmation dialog the guard itself opened), use
958 /// [`close_window_forced`](Self::close_window_forced).
959 pub fn close_window(&mut self) {
960 self.close_window_requested = true;
961 }
962
963 /// Request that the application close this tree's window
964 /// **unconditionally**, bypassing any close guard declared via
965 /// [`WindowConfig::on_close_requested`](crate::window::WindowConfig::on_close_requested)
966 /// / [`can_close`](crate::window::WindowConfig::can_close).
967 ///
968 /// This is the second half of the veto-then-reissue pattern: the
969 /// guard returns [`CloseResponse::Veto`](crate::window::CloseResponse::Veto)
970 /// and opens a confirmation dialog; the dialog's "close anyway"
971 /// button calls `close_window_forced` so the window actually closes
972 /// without re-triggering the guard.
973 pub fn close_window_forced(&mut self) {
974 self.force_close_requested = true;
975 }
976
977 // -------------------- Multi-window API --------------------
978
979 /// The [`WindowState`](crate::window::WindowState) for the window
980 /// hosting this handler. `None` only for handlers run outside
981 /// of an app (hand-constructed `EventContext` in tests).
982 /// Cursor position at the moment this handler was invoked. `None`
983 /// when no `PointerMove` has reached the tree yet, or when the
984 /// context was constructed without a tree-side snapshot (e.g.
985 /// hand-built `EventContext`s in tests). Used by the safe-triangle
986 /// submenu hover gate.
987 pub fn tree_pointer_position(&self) -> Option<teksilo_canvas::Point> {
988 self.tree_pointer_position
989 }
990
991 /// True when the in-flight pointer press's hit target is a strict
992 /// descendant of THIS handler's widget that carries its own tap gesture
993 /// (chevron, checkbox, inline button). A row/container that selects on
994 /// press should early-return `EventResponse::Ignored` when this is set, so
995 /// the press belongs to the inner control, not the row. Only meaningful
996 /// inside `on_pointer_event` handlers for `PointerDown`/`PointerUp`.
997 pub fn press_claimed_by_interactive_child(&self) -> bool {
998 self.press_claimed_by_interactive_child
999 }
1000
1001 /// Look up the bounds rect of an open overlay by its root content
1002 /// widget id. Returns `None` when no such overlay is currently
1003 /// active. The snapshot is taken once per dispatch; mid-handler
1004 /// `show_overlay` calls will not appear here. Used by the
1005 /// safe-triangle submenu hover gate.
1006 pub fn overlay_bounds_for_content(&self, content_id: WidgetId) -> Option<teksilo_canvas::Rect> {
1007 self.overlay_bounds_snapshot
1008 .iter()
1009 .find(|(cid, _, _)| *cid == content_id)
1010 .map(|(_, r, _)| *r)
1011 }
1012
1013 /// Whether a safe-triangle traversal toward the overlay rooted at
1014 /// `content_id` is still live — i.e. whether the user may still be on
1015 /// their way to that submenu.
1016 ///
1017 /// A widget whose hover would otherwise tear the overlay down (a
1018 /// sibling menu row switching the selection) asks this first and
1019 /// stands aside while it is `true`, leaving the dismissal to the
1020 /// overlay's own pointer-leave grace — which tests the cone on every
1021 /// sample and closes the overlay one `delay` after the pointer stops
1022 /// heading there.
1023 ///
1024 /// **This is deliberately the armed window, not a point-in-cone
1025 /// test.** A sibling row's hover fires exactly once, at the instant
1026 /// the pointer crosses onto it — a pixel or two from the apex, where
1027 /// the cone is a needle — so answering "is this one sample inside the
1028 /// cone" made a single quantized step final, and any departure
1029 /// steeper than the cone (which is most of them, for a wide menu with
1030 /// a short submenu) killed the submenu the moment the pointer left
1031 /// the trigger row. Whether *this* sample is inside the cone is the
1032 /// framework's question, asked continuously; the widget's question is
1033 /// only whether to get out of the way.
1034 ///
1035 /// `false` when no region is armed and when its budget is spent.
1036 ///
1037 /// Arm the region with
1038 /// [`arm_overlay_safe_region`](Self::arm_overlay_safe_region).
1039 pub fn overlay_safe_region_armed(&self, content_id: WidgetId) -> bool {
1040 self.overlay_bounds_snapshot
1041 .iter()
1042 .find(|(cid, _, _)| *cid == content_id)
1043 .is_some_and(|(_, _, apex)| apex.is_some())
1044 }
1045
1046 pub fn window(&self) -> Option<&crate::window::WindowState> {
1047 self.current_window.as_ref()
1048 }
1049
1050 /// Whether the host window is currently active (`focused AND not
1051 /// occluded`) — the occlusion-aware companion to
1052 /// `self.window().map(|w| w.focused())` (raw OS focus). Snapshotted at
1053 /// context construction. Matches [`BuildContext::window_active`].
1054 ///
1055 /// [`BuildContext::window_active`]: crate::build_context::BuildContext::window_active
1056 pub fn window_active(&self) -> bool {
1057 self.tree_window_active
1058 }
1059
1060 /// Open a new window, creating the winit-level surface
1061 /// synchronously. The returned id is immediately valid for
1062 /// [`focus_window`](Self::focus_window),
1063 /// [`window_state`](Self::window_state), and
1064 /// [`find_window`](Self::find_window).
1065 ///
1066 /// Panics when called from a handler on a standalone `WidgetTree`
1067 /// (no app context) — tests should not invoke this method.
1068 pub fn open_window(
1069 &mut self,
1070 config: crate::window::WindowConfig,
1071 ) -> crate::window::TeksiloWindowId {
1072 self.warn_if_context_change_in_focus_dispatch("open_window");
1073 self.window_ops
1074 .as_deref_mut()
1075 .expect("open_window called outside of a dispatch")
1076 .open_window(config)
1077 }
1078
1079 /// Find a window by the string id assigned via
1080 /// [`WindowConfig::id`](crate::window::WindowConfig::id). Returns
1081 /// `None` if no open window carries that id.
1082 pub fn find_window(&self, string_id: &str) -> Option<crate::window::TeksiloWindowId> {
1083 self.window_ops.as_deref()?.find_window(string_id)
1084 }
1085
1086 /// Read the [`WindowState`](crate::window::WindowState) for a
1087 /// specific window.
1088 pub fn window_state(
1089 &self,
1090 id: crate::window::TeksiloWindowId,
1091 ) -> Option<crate::window::WindowState> {
1092 self.window_ops.as_deref()?.window_state(id)
1093 }
1094
1095 /// Snapshot of every live window's state.
1096 pub fn windows(&self) -> Vec<crate::window::WindowState> {
1097 self.window_ops
1098 .as_deref()
1099 .map(|o| o.windows())
1100 .unwrap_or_default()
1101 }
1102
1103 /// Raise a window to the front and give it keyboard focus.
1104 pub fn focus_window(&mut self, id: crate::window::TeksiloWindowId) {
1105 self.warn_if_context_change_in_focus_dispatch("focus_window");
1106 if let Some(ops) = self.window_ops.as_deref_mut() {
1107 ops.focus_window(id);
1108 }
1109 }
1110
1111 /// Request an xdg-activation token for `id` (see
1112 /// [`WindowOps::request_activation_token`](crate::window::WindowOps::request_activation_token)).
1113 /// `cb` fires once with the token string, or `None` where the platform can't
1114 /// provide one — used to hand a token to a child process ("open in new
1115 /// window") or an IPC peer that will raise itself on Wayland.
1116 pub fn request_activation_token(
1117 &mut self,
1118 id: crate::window::TeksiloWindowId,
1119 cb: Box<dyn FnOnce(Option<String>)>,
1120 ) {
1121 if let Some(ops) = self.window_ops.as_deref_mut() {
1122 ops.request_activation_token(id, cb);
1123 } else {
1124 cb(None);
1125 }
1126 }
1127
1128 /// Request an activation token for the **current** window (see
1129 /// [`WindowOps::request_activation_token_self`](crate::window::WindowOps::request_activation_token_self)).
1130 /// Use this from a widget handler to mint a token from *this* focused window
1131 /// to hand to another window or process — it works mid-dispatch, unlike the
1132 /// id-based variant.
1133 pub fn request_activation_token_self(&mut self, cb: Box<dyn FnOnce(Option<String>)>) {
1134 if let Some(ops) = self.window_ops.as_deref_mut() {
1135 ops.request_activation_token_self(cb);
1136 } else {
1137 cb(None);
1138 }
1139 }
1140
1141 /// Close a specific window by id. Equivalent to
1142 /// [`close_window`](Self::close_window) when `id` is the current
1143 /// window's id.
1144 pub fn close_window_by_id(&mut self, id: crate::window::TeksiloWindowId) {
1145 if let Some(ops) = self.window_ops.as_deref_mut() {
1146 ops.close_window_by_id(id);
1147 }
1148 }
1149
1150 /// Report the focused text widget's caret rectangle (window-logical
1151 /// pixels) so the platform can position the OS IME candidate window at
1152 /// the insertion point. Text-editing widgets call this whenever the
1153 /// caret moves. No-op outside a dispatch / on a standalone tree.
1154 pub fn set_ime_cursor_area(&mut self, area: teksilo_canvas::Rect) {
1155 if let Some(ops) = self.window_ops.as_deref_mut() {
1156 ops.set_ime_cursor_area(area);
1157 }
1158 }
1159
1160 /// Resolve the platform parent handle of the window currently
1161 /// dispatching the event. Used by native-dialog integrations
1162 /// (`teksilo_platform::file_dialog`) to parent OS dialogs to the
1163 /// originating Teksilo window.
1164 ///
1165 /// Returns `None` when called from a standalone `WidgetTree` (no
1166 /// app-level `WindowOps` sink), or when the platform refuses to
1167 /// surface a handle (rare; mostly during teardown).
1168 pub fn parent_window_handle(&self) -> Option<crate::raw_handle::ParentHandle> {
1169 self.window_ops.as_deref()?.current_parent_handle()
1170 }
1171
1172 /// Request a cursor icon change.
1173 pub fn set_cursor(&mut self, cursor: CursorIcon) {
1174 self.cursor_request = Some(cursor);
1175 }
1176
1177 /// Set a widget subtree as dormant (preserves state, releases rendering).
1178 pub fn set_dormant(&mut self, id: WidgetId) {
1179 self.tree_mutations.push(TreeMutation::SetDormant(id));
1180 }
1181
1182 /// Activate a dormant widget subtree.
1183 pub fn activate(&mut self, id: WidgetId) {
1184 self.tree_mutations.push(TreeMutation::Activate(id));
1185 }
1186
1187 /// Destroy a widget subtree (removes from arena entirely, state is gone).
1188 pub fn destroy(&mut self, id: WidgetId) {
1189 self.tree_mutations.push(TreeMutation::Destroy(id));
1190 }
1191
1192 /// Imperatively mutate a mounted widget by id, downcasting to the
1193 /// concrete type `W`.
1194 ///
1195 /// The mutation is **deferred**: the closure runs after the handler
1196 /// returns, inside `apply_tree_mutations`, where the framework holds
1197 /// `&mut` arena access (a handler cannot re-borrow the arena to reach
1198 /// another node, so this is the only safe channel — the same model as
1199 /// [`destroy`](Self::destroy)). After the closure runs, the target is
1200 /// dirty-marked at `dirty` so the mutation takes visual effect.
1201 ///
1202 /// The target widget must override `Widget::as_any_mut` to return
1203 /// `Some(self)`. If the id is gone or is not a `W`, the closure is a
1204 /// no-op in release and a `debug_assert` failure in debug — it never
1205 /// silently mutates the wrong widget.
1206 ///
1207 /// Use it for per-view state a handler can't otherwise reach — e.g.
1208 /// `SceneView::ensure_visible(...)` (camera) after the view is mounted:
1209 /// ```ignore
1210 /// ctx.with_widget_mut::<SceneView>(view_id, BindingLevel::Relayout, |v| {
1211 /// v.ensure_visible(card_rect, 40.0);
1212 /// });
1213 /// ```
1214 /// For scene *content*, prefer the shared `SceneModel` handle (`view.model()`)
1215 /// — its mutators are `&self`, so a handler holding a clone can drive the
1216 /// scene directly and every attached view reconciles, no `with_widget_mut`
1217 /// needed.
1218 pub fn with_widget_mut<W: 'static>(
1219 &mut self,
1220 id: WidgetId,
1221 dirty: crate::binding::BindingLevel,
1222 f: impl FnOnce(&mut W) + 'static,
1223 ) {
1224 self.tree_mutations.push(TreeMutation::WithWidgetMut {
1225 id,
1226 dirty,
1227 apply: Box::new(move |any| match any.downcast_mut::<W>() {
1228 Some(w) => f(w),
1229 None => debug_assert!(
1230 false,
1231 "with_widget_mut: widget {id:?} is not the requested type (or does not \
1232 override Widget::as_any_mut)"
1233 ),
1234 }),
1235 });
1236 }
1237
1238 /// Re-run one widget's `build()` **during this handler's drain**, before
1239 /// overlays are shown and before focus requests are applied — rather than
1240 /// dirty-marking it for the next layout pass, which is what every other
1241 /// rebuild trigger does.
1242 ///
1243 /// It exists for one shape:
1244 /// [`DeferredSubtree`](crate::deferred_subtree::DeferredSubtree) content
1245 /// that a handler is *about to depend on*. Opening a popover activates its
1246 /// content, shows an overlay anchored to it, and moves focus into it — all
1247 /// three inside the same drain (see `collect_from_ctx`). A deferred panel
1248 /// marked for rebuild would not exist yet at any of those points: the
1249 /// overlay would be measured against an empty node and
1250 /// `first_focusable_descendant` would find nothing to focus, so the popover
1251 /// would open in the wrong place and swallow the keyboard. Materializing
1252 /// here closes that window, and makes deferred content behave exactly like
1253 /// the eagerly-built content it replaces.
1254 ///
1255 /// Cheap to call redundantly: a `DeferredSubtree` that is already
1256 /// materialized returns its existing child, so a second open costs one
1257 /// `build()` of the host and nothing below it.
1258 ///
1259 /// Not a general "rebuild this widget now" door — reach for
1260 /// [`with_widget_mut`](Self::with_widget_mut) or a `Rebuild` binding for
1261 /// ordinary reactive updates, which are correctly served by the next
1262 /// layout pass.
1263 /// `Space` on a data view's focused row: activate the row's published
1264 /// keyboard toggle — the checkbox `StandardListItem` embeds, most often —
1265 /// or run `fallback` when the row publishes none.
1266 ///
1267 /// A row's controls are out of the Tab order, so this is the only keyboard
1268 /// route to them; `fallback` is what `Space` means on a row without one,
1269 /// which for the data views is "toggle the selection".
1270 pub fn row_space_activate(&mut self, row: WidgetId, fallback: std::rc::Rc<dyn Fn()>) {
1271 self.tree_mutations
1272 .push(TreeMutation::RowSpaceActivate { row, fallback });
1273 }
1274
1275 pub fn materialize_now(&mut self, id: WidgetId) {
1276 self.tree_mutations.push(TreeMutation::MaterializeNow(id));
1277 }
1278
1279 /// Request that the AccessKit tree be re-walked after this handler
1280 /// returns. Use after a mutation that changes the accessibility tree
1281 /// **shape** in a way the framework doesn't already detect (relayout
1282 /// alone no longer re-walks AT; only events that change the AT tree
1283 /// — focus, overlays, locale/shortcut rebinds — set the dirty flag).
1284 /// The companion `BuildContext::request_accessibility_update` covers
1285 /// the build-time path.
1286 pub fn request_accessibility_update(&mut self) {
1287 self.request_a11y_update = true;
1288 }
1289
1290 /// Ask the platform to raise its on-screen keyboard.
1291 ///
1292 /// For the case the desktop convention has no answer for: a *finger*
1293 /// landing in a text field, where there is no physical keyboard and no
1294 /// focus change the accessibility layer would notice on its own.
1295 ///
1296 /// The request is honoured **only where it can do no harm**. Where the
1297 /// platform's keyboard follows the framework's IME-allowance reconcile
1298 /// ([`SoftKeyboardSupport::ViaAccessibility`](crate::window::SoftKeyboardSupport::ViaAccessibility)),
1299 /// the request resolves to nothing — always, not merely while a composition
1300 /// happens to be live. That reconcile *is* the request, and the only thing
1301 /// an explicit ask could add is a re-assertion of IME allowance, which is
1302 /// what cancels a composition mid-word. Nothing on this path calls
1303 /// `set_ime_allowed`, and that is what makes placing a caret with a finger
1304 /// mid-composition safe. Where the framework has no keyboard request to
1305 /// send at all the request is dropped; ask
1306 /// [`soft_keyboard_support`](Self::soft_keyboard_support) first if the
1307 /// widget needs to offer a fallback.
1308 pub fn request_soft_keyboard(&mut self) {
1309 self.soft_keyboard_request = Some(true);
1310 }
1311
1312 /// Ask the platform to dismiss its on-screen keyboard.
1313 ///
1314 /// Only a platform reporting
1315 /// [`SoftKeyboardSupport::Explicit`](crate::window::SoftKeyboardSupport::Explicit)
1316 /// can honour this; elsewhere there is no dismiss request to send, and a
1317 /// keyboard that rose on the IME enable goes away on the matching disable
1318 /// when focus leaves the text surface.
1319 pub fn dismiss_soft_keyboard(&mut self) {
1320 self.soft_keyboard_request = Some(false);
1321 }
1322
1323 /// What the host platform can do about an on-screen keyboard.
1324 ///
1325 /// [`SoftKeyboardSupport::None`](crate::window::SoftKeyboardSupport::None)
1326 /// on a standalone tree and on every platform the framework has no keyboard
1327 /// request to send on — which, on the desktop, is most of them.
1328 pub fn soft_keyboard_support(&self) -> crate::window::SoftKeyboardSupport {
1329 self.window_ops
1330 .as_deref()
1331 .map(|ops| ops.soft_keyboard_support())
1332 .unwrap_or_default()
1333 }
1334
1335 /// Speak `message` to the screen reader, politely.
1336 ///
1337 /// For anything the user needs told that is not the name of a widget: a
1338 /// completed action, a new count, the result of an undo, a row that moved.
1339 /// Sighted users read those off the screen; a screen-reader user is told
1340 /// only what the framework says out loud.
1341 ///
1342 /// ```ignore
1343 /// ctx.announce(tr!(event_added(title = title.clone())));
1344 /// ```
1345 ///
1346 /// Takes `impl Into<String>`, so `tr!(…)` works directly.
1347 /// `LocalizedString` is deliberately not the parameter type: an
1348 /// announcement is an event, not a label, and
1349 /// re-resolving it on a later language switch would re-speak it. See
1350 /// [`crate::announcer`].
1351 ///
1352 /// **Do not pair this with a toast on the same path.** `Toast` is already a
1353 /// correct live region, so doing both says everything twice.
1354 pub fn announce(&mut self, message: impl Into<String>) {
1355 self.announce_with(message, crate::announcer::Politeness::Polite);
1356 }
1357
1358 /// Speak `message` to the screen reader at the given urgency.
1359 ///
1360 /// [`Politeness::Assertive`](crate::announcer::Politeness::Assertive)
1361 /// interrupts whatever is being spoken. Reserve it for something the user
1362 /// must not miss and cannot recover by re-reading the screen — a failure, a
1363 /// refusal, a destructive result. Everything else is
1364 /// [`Polite`](crate::announcer::Politeness::Polite), which is what
1365 /// [`announce`](Self::announce) uses.
1366 pub fn announce_with(
1367 &mut self,
1368 message: impl Into<String>,
1369 politeness: crate::announcer::Politeness,
1370 ) {
1371 self.announcements.push((message.into(), politeness));
1372 }
1373
1374 /// Show an overlay (tooltip, menu, popover).
1375 pub fn show_overlay(&mut self, request: crate::overlay::OverlayRequest) {
1376 self.overlay_requests.push(request);
1377 }
1378
1379 /// Show an overlay in an explicit z-band.
1380 ///
1381 /// [`show_overlay`](Self::show_overlay) is this with
1382 /// [`Standard`](crate::overlay::OverlayBand::Standard). The other band is
1383 /// for the touch text affordances, which must render above the editor's
1384 /// `clips_children` ancestor, below every menu, and outside the
1385 /// outside-press dismissal that every caret-moving tap would otherwise
1386 /// trigger. Their lifetime is the controller's — see
1387 /// [`TouchSelection::dismiss`](crate::text_touch::TouchSelection::dismiss).
1388 ///
1389 /// Showing content that is already up is a no-op, so a host may call this
1390 /// on every raise without tracking whether it has.
1391 pub fn show_overlay_in_band(
1392 &mut self,
1393 request: crate::overlay::OverlayRequest,
1394 band: crate::overlay::OverlayBand,
1395 ) {
1396 self.overlay_band_requests.push((request, band));
1397 }
1398
1399 /// Re-place the currently-shown overlay whose content root is `content_id`.
1400 ///
1401 /// Content-keyed for the same reason
1402 /// [`dismiss_overlay_by_content`](Self::dismiss_overlay_by_content) is:
1403 /// [`show_overlay`](Self::show_overlay) returns nothing, so a handler
1404 /// cannot learn the [`OverlayId`](crate::overlay::OverlayId) it created. A
1405 /// no-op when no overlay is showing that content.
1406 pub fn update_overlay_placement_by_content(
1407 &mut self,
1408 content_id: crate::widget_id::WidgetId,
1409 placement: crate::overlay::OverlayPlacement,
1410 ) {
1411 self.overlay_placement_updates.push((content_id, placement));
1412 }
1413
1414 /// Show an overlay whose reveal/dismiss is animated by a
1415 /// caller-owned progress signal.
1416 ///
1417 /// `progress` must be an animated `Signal<f32>` (created with
1418 /// [`Signal::new_animated`](crate::signal::Signal::new_animated) or
1419 /// [`BuildContext::animated_signal`](crate::build_context::BuildContext::animated_signal)).
1420 /// The framework shows the overlay, tweens `progress` 0 → 1 over
1421 /// `duration`, and on any dismiss path tweens it 1 → 0 while
1422 /// **deferring** the overlay's removal (and its content's dormancy)
1423 /// until the roll-back completes — the same window the fade path
1424 /// uses, but with no opacity applied. The caller binds `progress`
1425 /// to whatever paints the reveal (e.g. an
1426 /// [`Unroll`](https://docs.rs/teksilo) width), and is responsible for
1427 /// resetting it to `0.0` before the show if a prior reveal left it
1428 /// at `1.0`.
1429 ///
1430 /// Under `prefers-reduced-motion`, skip this and use
1431 /// [`show_overlay`](Self::show_overlay) with the progress pinned at
1432 /// `1.0` so there is no tween and dismissal is immediate.
1433 pub fn show_overlay_with_reveal(
1434 &mut self,
1435 request: crate::overlay::OverlayRequest,
1436 progress: crate::signal::Signal<f32>,
1437 duration: std::time::Duration,
1438 ) {
1439 self.reveal_overlay_requests
1440 .push((request, progress, duration));
1441 }
1442
1443 /// Show an overlay that dismisses automatically after `duration`.
1444 pub fn show_overlay_for(
1445 &mut self,
1446 request: crate::overlay::OverlayRequest,
1447 duration: std::time::Duration,
1448 ) {
1449 self.timed_overlay_requests.push((request, duration));
1450 }
1451
1452 /// Dismiss an overlay by ID.
1453 pub fn dismiss_overlay(&mut self, id: crate::overlay::OverlayId) {
1454 self.overlay_dismissals.push(id);
1455 }
1456
1457 /// Dismiss the currently-shown overlay whose content root is
1458 /// `content_id`, if one is active. No-op when no overlay is showing
1459 /// that content. Use this to dismiss an overlay you can only name by
1460 /// its content widget — the symmetric companion to
1461 /// [`cancel_delayed_overlay`](Self::cancel_delayed_overlay), which
1462 /// cancels a *pending* delayed show for the same content. Together
1463 /// they let a caller fully retract a reusable tooltip surface
1464 /// (shown or pending) without tracking the `OverlayId`.
1465 pub fn dismiss_overlay_by_content(&mut self, content_id: crate::widget_id::WidgetId) {
1466 self.overlay_content_dismissals.push(content_id);
1467 }
1468
1469 /// Queue a request to pause an overlay's `auto_dismiss_after`
1470 /// timer. Drained by the framework after this handler returns —
1471 /// equivalent to calling
1472 /// [`OverlayManager::pause_auto_dismiss`](crate::overlay::OverlayManager::pause_auto_dismiss)
1473 /// at the next safe point. Idempotent.
1474 ///
1475 /// Used by `ToastHost` for hover-pause: on pointer-enter the
1476 /// host queues `pause_overlay_auto_dismiss(id)` for every live
1477 /// toast; on pointer-leave it queues `resume_overlay_auto_dismiss`.
1478 pub fn pause_overlay_auto_dismiss(&mut self, id: crate::overlay::OverlayId) {
1479 self.overlay_pause_requests.push((id, true));
1480 }
1481
1482 /// Queue a request to resume an overlay's `auto_dismiss_after`
1483 /// timer paused via
1484 /// [`pause_overlay_auto_dismiss`](Self::pause_overlay_auto_dismiss).
1485 /// Idempotent on un-paused overlays.
1486 pub fn resume_overlay_auto_dismiss(&mut self, id: crate::overlay::OverlayId) {
1487 self.overlay_pause_requests.push((id, false));
1488 }
1489
1490 /// Dismiss all active overlays (e.g., after a menu item is activated).
1491 pub fn dismiss_all_overlays(&mut self) {
1492 self.dismiss_scope = Some(DismissScope::All);
1493 }
1494
1495 /// Dismiss the source widget's containing overlay and any ancestor
1496 /// overlays in the chain that are menu-like (anything that isn't a
1497 /// `Role::Tooltip`, `Role::Dialog`, or `Role::AlertDialog`),
1498 /// preserving an outer composite tooltip or modal hosting the
1499 /// popover. Use for menu / dropdown item activation that wants to
1500 /// close the menu cascade without disturbing the host surface.
1501 pub fn dismiss_self_overlay_chain(&mut self) {
1502 self.dismiss_scope = Some(DismissScope::SelfChain);
1503 }
1504
1505 /// Dismiss every overlay whose content is *not* a host surface
1506 /// (`Role::Tooltip`, `Role::Dialog`, `Role::AlertDialog`),
1507 /// preserving an outer composite tooltip or modal hosting the
1508 /// trigger. Use for popover triggers and pre-show cleanup that
1509 /// want to close stale popovers / menus without taking a hosting
1510 /// surface with them.
1511 pub fn dismiss_all_except_hosts(&mut self) {
1512 self.dismiss_scope = Some(DismissScope::AllExceptHosts);
1513 }
1514
1515 /// Dismiss the topmost overlay only (e.g., closing a submenu while
1516 /// keeping the parent menu open).
1517 pub fn dismiss_top_overlay(&mut self) {
1518 self.dismiss_scope = Some(DismissScope::Top);
1519 }
1520
1521 /// Dismiss descendant overlays of the source widget's containing overlay.
1522 /// Useful for closing sibling submenu branches while keeping the current
1523 /// parent menu open.
1524 pub fn dismiss_child_overlays(&mut self) {
1525 self.dismiss_descendant_overlays.push(None);
1526 }
1527
1528 /// Dismiss descendant overlays of the source widget's containing overlay,
1529 /// preserving the subtree rooted at `content_id` if it is already open.
1530 pub fn dismiss_child_overlays_except(&mut self, content_id: crate::widget_id::WidgetId) {
1531 self.dismiss_descendant_overlays.push(Some(content_id));
1532 }
1533
1534 /// Request an idle callback to be run during the next idle period.
1535 /// Use this for incremental work that takes 5-50ms — too short for a
1536 /// background thread, too long for a single frame.
1537 pub fn request_idle_callback(
1538 &mut self,
1539 callback: impl FnOnce(crate::idle::IdleDeadline) + 'static,
1540 ) {
1541 self.idle_callbacks.push(Box::new(callback));
1542 }
1543
1544 /// Request framework-owned modal presentation.
1545 ///
1546 /// The widget tree records the request together with the originating
1547 /// widget, and the application layer can later resolve `Auto` into a
1548 /// concrete presentation backend.
1549 pub fn present_modal(&mut self, request: crate::modal::ModalRequest) {
1550 self.modal_requests.push(request);
1551 }
1552
1553 /// Synchronously open a modal as a native window — the single
1554 /// unified path for native-window modals. Callers that don't
1555 /// care whether the modal lands in-tree or in a native window
1556 /// use [`present_modal`](Self::present_modal), which routes
1557 /// `ModalPresentation::Auto` through the framework's picker.
1558 ///
1559 /// Returns the new window's id, or `None` when called outside a
1560 /// dispatch context (standalone trees). The window's parent is
1561 /// the current window; focus target and title / size from the
1562 /// request are honored.
1563 ///
1564 /// Only `ModalContent::Deferred` is supported here — an
1565 /// `ExistingWidget` id wouldn't make sense in a fresh tree.
1566 pub fn open_modal(
1567 &mut self,
1568 request: crate::modal::ModalRequest,
1569 ) -> Option<crate::window::TeksiloWindowId> {
1570 let parent = self.current_window.as_ref()?.id();
1571 let crate::modal::ModalContent::Deferred(builder) = request.content else {
1572 return None;
1573 };
1574 let mut config = crate::window::WindowConfig::new().modal(crate::window::ModalConfig {
1575 parent,
1576 focus_target: request.focus_target,
1577 });
1578 if let Some(title) = request.title {
1579 config = config.title(title);
1580 }
1581 if let Some((w, h)) = request.size {
1582 config = config.size(w, h);
1583 }
1584 let config = config.root(move |tree, _state| builder(tree));
1585 Some(self.open_window(config))
1586 }
1587
1588 /// Dismiss the current framework-owned modal presentation.
1589 pub fn dismiss_modal(&mut self) {
1590 self.dismiss_modal = true;
1591 }
1592
1593 /// Show an overlay after a delay. The widget tree checks pending delayed
1594 /// overlays during `layout()` and shows them once the delay elapses.
1595 /// Use this for submenu hover-open delays.
1596 ///
1597 /// The content widget should already be added to the tree (typically
1598 /// dormant). It will be activated automatically when the delay elapses.
1599 pub fn show_overlay_after(
1600 &mut self,
1601 request: crate::overlay::OverlayRequest,
1602 delay: std::time::Duration,
1603 ) {
1604 self.delayed_overlay_requests
1605 .push((request, delay, None, false));
1606 }
1607
1608 /// Show an overlay after a delay and move focus when it opens.
1609 pub fn show_overlay_after_with_focus(
1610 &mut self,
1611 request: crate::overlay::OverlayRequest,
1612 delay: std::time::Duration,
1613 focus_target: crate::widget_id::WidgetId,
1614 ) {
1615 self.delayed_overlay_requests
1616 .push((request, delay, Some(focus_target), false));
1617 }
1618
1619 /// Show an overlay after a delay, move focus when it opens, and
1620 /// dismiss the anchor's sibling overlays **at that moment** rather
1621 /// than when the request was made.
1622 ///
1623 /// This is the hover-switch between two submenu triggers in the same
1624 /// menu. Dismissing eagerly at hover-enter closes the submenu the
1625 /// user is still walking toward as soon as the pointer crosses a
1626 /// neighbouring trigger; deferring the dismissal to the moment the
1627 /// new submenu actually opens means a pointer merely passing through
1628 /// costs nothing, and one that settles gets the swap on the same
1629 /// frame — no window with two submenus on screen.
1630 pub fn show_overlay_after_replacing_siblings(
1631 &mut self,
1632 request: crate::overlay::OverlayRequest,
1633 delay: std::time::Duration,
1634 focus_target: crate::widget_id::WidgetId,
1635 ) {
1636 self.delayed_overlay_requests
1637 .push((request, delay, Some(focus_target), true));
1638 }
1639
1640 /// Request a repaint on a specific widget. Use this when an event handler
1641 /// on one widget changes state that affects a different widget's appearance
1642 /// (e.g., keyboard navigation highlighting items in an overlay).
1643 pub fn request_repaint(&mut self, id: crate::widget_id::WidgetId) {
1644 self.repaint_requests.push(id);
1645 }
1646
1647 /// Programmatically click a widget (synthetic PointerDown + PointerUp at
1648 /// its center). Use this for keyboard activation of a child widget, e.g.,
1649 /// Enter on a keyboard-focused menu item.
1650 pub fn synthetic_click(&mut self, id: crate::widget_id::WidgetId) {
1651 self.synthetic_clicks.push(id);
1652 }
1653
1654 /// Transfer focus to a specific widget. Use this when opening overlay
1655 /// content (menus, dialogs) that should receive keyboard events.
1656 pub fn request_focus(&mut self, id: crate::widget_id::WidgetId) {
1657 self.focus_requests.push(id);
1658 }
1659
1660 /// Move focus **into** the content of `id`: focus its first focusable
1661 /// descendant in tab order. Unlike [`request_focus`](Self::request_focus),
1662 /// this does **not** fall back to focusing `id` itself when the subtree has
1663 /// no focusable descendant — it is a no-op in that case, so an empty region
1664 /// never traps focus on a non-interactive container.
1665 ///
1666 /// Use this for "dive into this region" gestures, e.g. pressing Enter on a
1667 /// focused tab header to move focus into the tab's content panel. A panel
1668 /// with focusable content lands on its first control; a panel that opted
1669 /// into focusability itself (no inner controls) lands on the panel; a bare
1670 /// panel with neither leaves focus where it was.
1671 pub fn request_focus_into(&mut self, id: crate::widget_id::WidgetId) {
1672 self.focus_into_requests.push(id);
1673 }
1674
1675 /// Scroll the given rectangle into view inside every enclosing scroll
1676 /// container, walking outward from the widget whose handler is running.
1677 ///
1678 /// `rect` is in **absolute tree (window) coordinates** — the same space
1679 /// the arena stores widget bounds in. After the handler returns, the
1680 /// framework walks the current widget's ancestors and, for each
1681 /// `clips_children` scroll container whose viewport does not already
1682 /// fully contain `rect`, dispatches
1683 /// [`WidgetEvent::ScrollIntoView`](crate::event::WidgetEvent::ScrollIntoView)
1684 /// so the container adjusts its offset. Nested scroll areas each get a
1685 /// turn (outermost included), exactly like the focus-driven path.
1686 ///
1687 /// Unlike the automatic focus follow — which can only reveal a *focused
1688 /// widget's own bounds* — this lets a widget reveal an arbitrary interior
1689 /// rectangle it computed itself: a text caret, a virtualized list/table
1690 /// row (which is not a distinct focusable node), or a scrolled-off tab
1691 /// header. The widget remains responsible for scrolling its *own* interior
1692 /// viewport; `ensure_visible` handles the enclosing containers. It is a
1693 /// no-op when there is no scroll container above the widget, or when every
1694 /// container already shows the rect.
1695 ///
1696 /// See [`ensure_visible_with_margin`](Self::ensure_visible_with_margin) to
1697 /// keep breathing room around the target.
1698 pub fn ensure_visible(&mut self, rect: teksilo_canvas::Rect) {
1699 self.scroll_into_view_requests.push(ScrollRevealRequest {
1700 rect,
1701 margin: 0.0,
1702 align: crate::event::ScrollAlign::Minimal,
1703 motion: crate::event::ScrollMotion::Instant,
1704 from: None,
1705 });
1706 }
1707
1708 /// [`ensure_visible`](Self::ensure_visible), for a rect that belongs to
1709 /// **another** widget.
1710 ///
1711 /// The framework walks `owner`'s ancestors rather than the handling widget's.
1712 /// That distinction is the whole of it, and getting it wrong fails silently:
1713 /// a find banner's Next button sits *beside* the scrolling page, not inside
1714 /// it, so a reveal walked from the button climbs out through the banner and
1715 /// never meets the scroll container the match is in. The match is selected,
1716 /// the counter moves, and the viewport does not follow.
1717 ///
1718 /// The same reasoning [`ensure_widget_visible`](Self::ensure_widget_visible)
1719 /// already records for the id-based form; this is its rect-based twin, for a
1720 /// target that is an interior span rather than a mounted child.
1721 ///
1722 /// `rect` is in absolute tree (window) coordinates.
1723 pub fn ensure_visible_from(
1724 &mut self,
1725 owner: crate::widget_id::WidgetId,
1726 rect: teksilo_canvas::Rect,
1727 ) {
1728 self.scroll_into_view_requests.push(ScrollRevealRequest {
1729 rect,
1730 margin: 0.0,
1731 align: crate::event::ScrollAlign::Minimal,
1732 motion: crate::event::ScrollMotion::Instant,
1733 from: Some(owner),
1734 });
1735 }
1736
1737 /// Like [`ensure_visible`](Self::ensure_visible), but keeps `margin`
1738 /// logical pixels of breathing room around `rect` on every edge, so the
1739 /// target does not sit flush against the viewport boundary (the caret at
1740 /// the bottom line, the selected row at the fold). `rect` is in absolute
1741 /// tree (window) coordinates.
1742 pub fn ensure_visible_with_margin(&mut self, rect: teksilo_canvas::Rect, margin: f32) {
1743 self.scroll_into_view_requests.push(ScrollRevealRequest {
1744 rect,
1745 margin: margin.max(0.0),
1746 align: crate::event::ScrollAlign::Minimal,
1747 motion: crate::event::ScrollMotion::Instant,
1748 from: None,
1749 });
1750 }
1751
1752 /// **Pin** `rect` at `fraction` of the way down the innermost enclosing
1753 /// scroll container — `0.0` flush with the top, `0.5` centred, `1.0` flush
1754 /// with the bottom — instead of merely revealing it.
1755 ///
1756 /// The difference from [`ensure_visible`](Self::ensure_visible) is that this
1757 /// scrolls **even when the target is already visible**. That is what makes
1758 /// it usable for typewriter scrolling: a caret that only moved the view once
1759 /// it fell off the edge would not be pinned to anything.
1760 ///
1761 /// Only the **innermost** clipping ancestor aligns; any further ancestors
1762 /// out fall back to a minimal reveal, since an outer container's job is to
1763 /// bring the inner viewport on screen, not to align a rectangle it does not
1764 /// own.
1765 ///
1766 /// `fraction` is clamped to `0.0..=1.0`. The container additionally clamps
1767 /// to its own scroll range, so a target near the start or end of the content
1768 /// lands as close to `fraction` as the range permits — see the scroll
1769 /// container's `scroll_past_end` for buying range past the content's end so
1770 /// the last line can still reach the pin.
1771 ///
1772 /// `rect` is in absolute tree (window) coordinates.
1773 pub fn ensure_visible_aligned(
1774 &mut self,
1775 rect: teksilo_canvas::Rect,
1776 fraction: f32,
1777 motion: crate::event::ScrollMotion,
1778 ) {
1779 self.scroll_into_view_requests.push(ScrollRevealRequest {
1780 rect,
1781 margin: 0.0,
1782 align: crate::event::ScrollAlign::Fraction(fraction.clamp(0.0, 1.0)),
1783 motion,
1784 from: None,
1785 });
1786 }
1787
1788 /// [`ensure_visible_aligned`](Self::ensure_visible_aligned), for a rect that
1789 /// belongs to **another** widget — see [`ensure_visible_from`](Self::ensure_visible_from)
1790 /// for why the distinction exists and how it fails when it is missed.
1791 pub fn ensure_visible_aligned_from(
1792 &mut self,
1793 owner: crate::widget_id::WidgetId,
1794 rect: teksilo_canvas::Rect,
1795 fraction: f32,
1796 motion: crate::event::ScrollMotion,
1797 ) {
1798 self.scroll_into_view_requests.push(ScrollRevealRequest {
1799 rect,
1800 margin: 0.0,
1801 align: crate::event::ScrollAlign::Fraction(fraction.clamp(0.0, 1.0)),
1802 motion,
1803 from: Some(owner),
1804 });
1805 }
1806
1807 /// Scroll a specific mounted widget into view inside every enclosing
1808 /// scroll container — the id-based companion to
1809 /// [`ensure_visible`](Self::ensure_visible).
1810 ///
1811 /// The framework resolves `id` to its current absolute bounds after the
1812 /// handler returns and walks *that widget's* ancestors (never `id`
1813 /// itself), dispatching
1814 /// [`WidgetEvent::ScrollIntoView`](crate::event::WidgetEvent::ScrollIntoView)
1815 /// to each `clips_children` container that doesn't already show it.
1816 ///
1817 /// Use this when the target you want revealed is a real, non-virtualized
1818 /// child whose bounds the arena already knows — a selected radio tile, a
1819 /// tab header — so you don't have to compute a rect. For a target that has
1820 /// no distinct node (a text caret) or that may not be realized (a
1821 /// virtualized list/table row), use [`ensure_visible`](Self::ensure_visible)
1822 /// with an analytic rect instead. No-op if `id` is not currently mounted.
1823 pub fn ensure_widget_visible(&mut self, id: crate::widget_id::WidgetId) {
1824 self.scroll_widget_into_view_requests.push((id, 0.0));
1825 }
1826
1827 /// Like [`ensure_widget_visible`](Self::ensure_widget_visible), but keeps
1828 /// `margin` logical pixels of breathing room around the widget.
1829 pub fn ensure_widget_visible_with_margin(
1830 &mut self,
1831 id: crate::widget_id::WidgetId,
1832 margin: f32,
1833 ) {
1834 self.scroll_widget_into_view_requests
1835 .push((id, margin.max(0.0)));
1836 }
1837
1838 /// Surface the tooltip of a keyboard-highlighted item immediately (no
1839 /// dwell), dismissing the previously-highlighted item's tooltip. Used by
1840 /// `MenuList` on arrow-key navigation so a menu item's rich/composite
1841 /// tooltip is reachable by keyboard — real focus stays on the menu panel,
1842 /// so this is keyed on the item id rather than on focus. Pass the item's
1843 /// own widget id; a tooltip-less item simply dismisses the previous one.
1844 pub fn show_highlight_tooltip(&mut self, id: crate::widget_id::WidgetId) {
1845 self.highlight_tooltip_requests.push(id);
1846 }
1847
1848 /// Cancel a pending delayed overlay by its content widget ID.
1849 /// Call this when the hover ends before the delay elapses.
1850 pub fn cancel_delayed_overlay(&mut self, content_id: crate::widget_id::WidgetId) {
1851 self.cancel_delayed_overlays.push(content_id);
1852 }
1853
1854 /// Arm the "safe triangle" of the open overlay rooted at
1855 /// `content_id`, with its apex at the current pointer position.
1856 ///
1857 /// Call this from the anchor's hover-leave handler: the pointer is
1858 /// then exactly at the point the diagonal toward the overlay
1859 /// starts. While the pointer sits inside the triangle spanned by
1860 /// that apex and the overlay's near edge, the overlay's
1861 /// pointer-leave grace is held off; leaving the triangle starts the
1862 /// grace and re-entering it cancels the grace again, so a wobble
1863 /// mid-diagonal costs nothing. Throughout — cone or no cone, until
1864 /// the pointer arrives or the framework's budget runs out —
1865 /// [`overlay_safe_region_armed`](Self::overlay_safe_region_armed)
1866 /// reports `true` so sibling widgets stand aside and let that one
1867 /// re-evaluated grace own the dismissal.
1868 ///
1869 /// No-ops when the overlay is not open (a submenu whose hover-open
1870 /// delay was cancelled before it ever showed) or when no pointer
1871 /// position is known.
1872 pub fn arm_overlay_safe_region(&mut self, content_id: crate::widget_id::WidgetId) {
1873 self.safe_region_arm_requests.push(content_id);
1874 }
1875
1876 /// Capture **the pointer this handler is serving**: its subsequent
1877 /// `PointerMove` and `PointerUp` are routed to this widget regardless of
1878 /// hit test, until the capture is released.
1879 ///
1880 /// Capture is per pointer. Two fingers pressing two widgets hold two
1881 /// independent captures, and each is released only by its own Up or
1882 /// Cancel — so a second contact lifting can no longer steal the first
1883 /// one's stream. A mouse call site is unaffected: there is one mouse, and
1884 /// this captures it.
1885 /// **Also an arbitration act.** Taking the pointer from an undecided
1886 /// [`PointerSequence`](crate::gesture::PointerSequence) enrols this widget
1887 /// as a [`MemberRole::RawDrag`](crate::gesture::MemberRole::RawDrag)
1888 /// competitor, and for a precise pointer with no eligible pan competitor
1889 /// it decides the sequence outright — which is what makes the splitter
1890 /// handle, the dock resize handle and the table column grip (all of which
1891 /// answer `Ignored` from `on_pointer_event` and work from `PointerMove`
1892 /// with no recognizer at all) first-class competitors rather than widgets
1893 /// the arbitration cannot see.
1894 pub fn capture_pointer(&mut self) {
1895 self.pointer_capture = Some((None, true));
1896 self.explicit_capture = true;
1897 }
1898
1899 /// Capture a *named* pointer, for a handler driving a pointer other than
1900 /// the one whose sample it is serving.
1901 pub fn capture_pointer_id(&mut self, pointer: crate::pointer::PointerId) {
1902 self.pointer_capture = Some((Some(pointer), true));
1903 self.explicit_capture = true;
1904 }
1905
1906 /// Capture the pointer as **framework plumbing**, without staking an
1907 /// arbitration claim.
1908 ///
1909 /// The gesture arena takes the pointer for the Down..Up window so a
1910 /// recognizer keeps seeing moves that leave the widget's bounds, and the
1911 /// drag pipeline takes it for the life of a drag. Neither is a widget
1912 /// saying "this press is mine"; routing them through the public
1913 /// [`capture_pointer`](Self::capture_pointer) would enrol every
1914 /// arena-bearing node as a `RawDrag` member and decide every mouse
1915 /// sequence at press.
1916 pub(crate) fn capture_pointer_implicit(&mut self) {
1917 self.pointer_capture = Some((None, true));
1918 }
1919
1920 /// Claim the pointer sequence for the widget whose handler is running:
1921 /// arbitration ends, every other competitor is cancelled.
1922 ///
1923 /// The explicit form of what a recognizer does when it recognizes. Use it
1924 /// from an application recognizer that decides by its own rules.
1925 pub fn claim_gesture(&mut self) {
1926 self.gesture_acts.push(GestureAct::Claim);
1927 }
1928
1929 /// Withdraw the widget whose handler is running from the sequence. It can
1930 /// no longer win this press; its peers carry on.
1931 pub fn reject_gesture(&mut self) {
1932 self.gesture_acts.push(GestureAct::Reject);
1933 }
1934
1935 /// Defer this widget's own decision without withdrawing: no peer may win
1936 /// while a member is holding.
1937 ///
1938 /// **The framework never holds.** This exists for an application
1939 /// recognizer awaiting an answer it does not have yet (a hit test against
1940 /// an off-thread model, a network round trip). The hold auto-releases at
1941 /// [`GestureProfile::max_hold`](teksilo_tokens::GestureProfile::max_hold)
1942 /// — 250 ms — so a holder that never answers cannot strand the press.
1943 pub fn hold_gesture(&mut self) {
1944 self.gesture_acts.push(GestureAct::Hold);
1945 }
1946
1947 /// End this widget's hold, putting it back in the running.
1948 pub fn release_gesture(&mut self) {
1949 self.gesture_acts.push(GestureAct::Release);
1950 }
1951
1952 /// Revoke the whole interaction of the pointer this handler is serving,
1953 /// for `reason`.
1954 ///
1955 /// The widget's own way into the cancel funnel, for a widget that knows
1956 /// the interaction can no longer mean anything — the document under a text
1957 /// drag was reloaded, the row being reordered was deleted by a peer. Every
1958 /// competitor is cancelled, the capture is given back, and a
1959 /// [`PointerCancel`](crate::event::WidgetEvent::PointerCancel) is
1960 /// delivered, all **after** this handler returns: a cancel taken inline
1961 /// would unwind the very sample the handler is standing on.
1962 ///
1963 /// Distinct from [`reject_gesture`](Self::reject_gesture), which withdraws
1964 /// only *this* widget and lets its peers carry on with a pointer that is
1965 /// still perfectly alive.
1966 pub fn cancel_pointer_sequence(&mut self, reason: crate::pointer::CancelReason) {
1967 self.cancel_pointer_request = Some(reason);
1968 }
1969
1970 /// Release the capture of the pointer this handler is serving. Its events
1971 /// resume normal hit-test dispatch.
1972 pub fn release_pointer(&mut self) {
1973 self.pointer_capture = Some((None, false));
1974 }
1975
1976 /// Whether the widget whose handler is running already holds the capture
1977 /// of the pointer it is serving.
1978 ///
1979 /// `true` also immediately after a [`capture_pointer`](Self::capture_pointer)
1980 /// in the same handler, even though the tree does not apply the request
1981 /// until the handler returns — asking "do I own this pointer?" after
1982 /// claiming it must not answer no.
1983 pub fn owns_pointer(&self) -> bool {
1984 match self.pointer_capture {
1985 Some((None, capture)) => capture,
1986 _ => self.dispatch_node.is_some() && self.dispatch_node == self.pointer_captor,
1987 }
1988 }
1989
1990 /// Start a drag-and-drop operation from the given source widget.
1991 ///
1992 /// The `payload` carries the data being dragged. During the drag:
1993 /// - `PointerMove` events update the drag position and fire `on_drag_hover`
1994 /// on widgets under the pointer that have drop handlers
1995 /// - `PointerUp` fires `on_drop` on the target widget (if any)
1996 /// - `Escape` cancels the drag
1997 pub fn start_drag(
1998 &mut self,
1999 source_widget: crate::widget_id::WidgetId,
2000 payload: crate::drag_payload::DragPayload,
2001 ) {
2002 self.drag_start_request = Some((source_widget, payload, None));
2003 }
2004
2005 /// Start a drag-and-drop with a preview widget that follows the pointer.
2006 pub fn start_drag_with_preview(
2007 &mut self,
2008 source_widget: crate::widget_id::WidgetId,
2009 payload: crate::drag_payload::DragPayload,
2010 preview: Box<dyn crate::widget::Widget>,
2011 ) {
2012 self.drag_start_request = Some((source_widget, payload, Some(preview)));
2013 }
2014
2015 /// Cancel the active drag-and-drop session (if any).
2016 pub fn cancel_drag(&mut self) {
2017 self.cancel_drag = true;
2018 }
2019
2020 /// Replace the tree-level theme. Composite widgets are rebuilt so any
2021 /// derived values they captured at build time pick up the new tokens,
2022 /// and all widgets are marked dirty for repaint.
2023 ///
2024 /// An explicit theme also turns **off** OS-following: the app's theme
2025 /// mode is reset to manual, so a later OS light/dark change won't
2026 /// override the chosen theme.
2027 pub fn set_theme(&mut self, theme: crate::styles::Theme) {
2028 self.theme_request = Some(theme);
2029 }
2030
2031 /// Switch the application to follow the OS theme (native / system mode):
2032 /// the app adopts the OS's colours and tracks OS light/dark changes at
2033 /// runtime. On platforms without OS-colour support it falls back to
2034 /// following the built-in light/dark presets.
2035 ///
2036 /// This is the counterpart to [`set_theme`](Self::set_theme): calling
2037 /// `set_theme` pins a fixed theme (manual mode), while this resumes
2038 /// OS-following. Parameterless by design, so widgets need not reference
2039 /// the app-layer theme-mode enum.
2040 pub fn follow_system_theme(&mut self) {
2041 self.follow_system_request = true;
2042 }
2043
2044 /// Replace the tree-level locale identifier. Composite widgets are
2045 /// rebuilt so any tr! lookups picked up at build time are re-evaluated
2046 /// against the new locale.
2047 pub fn set_locale(&mut self, locale: impl Into<String>) {
2048 self.locale_request = Some(locale.into());
2049 }
2050
2051 /// Set the user-controlled global text-scale factor (`1.0` = 100 %).
2052 ///
2053 /// The change is applied app-wide (every window) after the handler returns,
2054 /// mirroring [`set_theme`](Self::set_theme) / [`set_locale`](Self::set_locale).
2055 /// All text grows uniformly without a rebuild. Persist the value through
2056 /// `ctx.settings()` (e.g. `teksilo_settings::TEXT_SCALE_KEY`) so it survives
2057 /// a restart — the `TextScaleControl` widget does both for you.
2058 pub fn set_text_scale(&mut self, factor: f32) {
2059 self.text_scale_request = Some(factor);
2060 }
2061}
2062
2063#[cfg(test)]
2064mod multi_window_tests {
2065 use super::*;
2066 use crate::window::state::WindowStateInit;
2067 use crate::window::{
2068 NoopWindowOps, TeksiloWindowId, WindowConfig, WindowOps, WindowPlacement, WindowState,
2069 };
2070 use std::cell::RefCell;
2071 use std::rc::Rc;
2072
2073 /// Recording implementation of `WindowOps` so tests can assert
2074 /// that `EventContext` routes each method through the trait.
2075 #[derive(Default)]
2076 struct RecordingOps {
2077 open_calls: RefCell<Vec<WindowConfig>>,
2078 focus_calls: RefCell<Vec<TeksiloWindowId>>,
2079 close_calls: RefCell<Vec<TeksiloWindowId>>,
2080 next_id: RefCell<u64>,
2081 // A fake registry so `find_window` / `window_state` / `windows`
2082 // can return values.
2083 states: RefCell<Vec<WindowState>>,
2084 }
2085
2086 impl RecordingOps {
2087 fn alloc_id(&self) -> TeksiloWindowId {
2088 let mut n = self.next_id.borrow_mut();
2089 *n += 1;
2090 TeksiloWindowId::new(*n)
2091 }
2092 }
2093
2094 impl WindowOps for RecordingOps {
2095 fn open_window(&mut self, config: WindowConfig) -> TeksiloWindowId {
2096 let id = self.alloc_id();
2097 let state = WindowState::new(WindowStateInit {
2098 id,
2099 string_id: config.string_id.clone(),
2100 placement: config.initial_placement,
2101 title: config.title.clone(),
2102 size: config.size,
2103 position: config.position.unwrap_or((0, 0)),
2104 focused: true,
2105 resizable: config.resizable,
2106 always_on_top: config.always_on_top,
2107 });
2108 self.states.borrow_mut().push(state);
2109 self.open_calls.borrow_mut().push(config);
2110 id
2111 }
2112
2113 fn find_window(&self, string_id: &str) -> Option<TeksiloWindowId> {
2114 self.states
2115 .borrow()
2116 .iter()
2117 .find(|s| s.string_id() == Some(string_id))
2118 .map(|s| s.id())
2119 }
2120
2121 fn window_state(&self, id: TeksiloWindowId) -> Option<WindowState> {
2122 self.states.borrow().iter().find(|s| s.id() == id).cloned()
2123 }
2124
2125 fn windows(&self) -> Vec<WindowState> {
2126 self.states.borrow().clone()
2127 }
2128
2129 fn focus_window(&mut self, id: TeksiloWindowId) {
2130 self.focus_calls.borrow_mut().push(id);
2131 }
2132
2133 fn close_window_by_id(&mut self, id: TeksiloWindowId) {
2134 self.close_calls.borrow_mut().push(id);
2135 }
2136 }
2137
2138 fn make_state(id: u64, string_id: Option<&str>) -> WindowState {
2139 WindowState::new(WindowStateInit {
2140 id: TeksiloWindowId::new(id),
2141 string_id: string_id.map(String::from),
2142 placement: WindowPlacement::Floating,
2143 title: "Test".into(),
2144 size: (800, 600),
2145 position: (0, 0),
2146 focused: true,
2147 resizable: true,
2148 always_on_top: false,
2149 })
2150 }
2151
2152 #[test]
2153 fn window_returns_current_window_state() {
2154 let state = make_state(1, Some("main"));
2155 let mut noop = NoopWindowOps;
2156 let ctx = EventContext::new().with_window_context(&mut noop, Some(state.clone()));
2157 assert_eq!(ctx.window().unwrap().id(), TeksiloWindowId::new(1));
2158 assert_eq!(ctx.window().unwrap().string_id(), Some("main"));
2159 }
2160
2161 #[test]
2162 fn window_is_none_without_context() {
2163 let ctx = EventContext::new();
2164 assert!(ctx.window().is_none());
2165 }
2166
2167 #[test]
2168 fn open_window_routes_through_ops() {
2169 let mut ops = RecordingOps::default();
2170 let main_state = make_state(1, Some("main"));
2171 let returned_id = {
2172 let mut ctx = EventContext::new().with_window_context(&mut ops, Some(main_state));
2173 ctx.open_window(WindowConfig::new().id("help").title("Help"))
2174 };
2175 assert_eq!(ops.open_calls.borrow().len(), 1);
2176 assert_eq!(
2177 ops.open_calls.borrow()[0].string_id.as_deref(),
2178 Some("help")
2179 );
2180 // Recording ops allocates ids 2+; 1 was reserved for `main`
2181 // only in this test — Recording's counter starts from 0, so the
2182 // first alloc yields 1.
2183 assert_eq!(returned_id, TeksiloWindowId::new(1));
2184 }
2185
2186 #[test]
2187 fn find_window_routes_through_ops() {
2188 let mut ops = RecordingOps::default();
2189 ops.states.borrow_mut().push(make_state(7, Some("foo")));
2190 let main_state = make_state(1, Some("main"));
2191 let ctx = EventContext::new().with_window_context(&mut ops, Some(main_state));
2192 assert_eq!(ctx.find_window("foo"), Some(TeksiloWindowId::new(7)));
2193 assert!(ctx.find_window("missing").is_none());
2194 }
2195
2196 #[test]
2197 fn focus_window_records_via_ops() {
2198 let mut ops = RecordingOps::default();
2199 let main_state = make_state(1, None);
2200 {
2201 let mut ctx = EventContext::new().with_window_context(&mut ops, Some(main_state));
2202 ctx.focus_window(TeksiloWindowId::new(42));
2203 }
2204 assert_eq!(
2205 ops.focus_calls.borrow().as_slice(),
2206 &[TeksiloWindowId::new(42)]
2207 );
2208 }
2209
2210 #[test]
2211 fn close_window_by_id_records_via_ops() {
2212 let mut ops = RecordingOps::default();
2213 let main_state = make_state(1, None);
2214 {
2215 let mut ctx = EventContext::new().with_window_context(&mut ops, Some(main_state));
2216 ctx.close_window_by_id(TeksiloWindowId::new(9));
2217 }
2218 assert_eq!(
2219 ops.close_calls.borrow().as_slice(),
2220 &[TeksiloWindowId::new(9)]
2221 );
2222 }
2223
2224 #[test]
2225 fn close_window_sets_guarded_flag_only() {
2226 let mut ctx = EventContext::new();
2227 ctx.close_window();
2228 assert!(
2229 ctx.close_window_requested,
2230 "close_window must raise the guarded-close flag"
2231 );
2232 assert!(
2233 !ctx.force_close_requested,
2234 "close_window must NOT raise the forced-close flag"
2235 );
2236 }
2237
2238 #[test]
2239 fn close_window_forced_sets_force_flag_only() {
2240 let mut ctx = EventContext::new();
2241 ctx.close_window_forced();
2242 assert!(
2243 ctx.force_close_requested,
2244 "close_window_forced must raise the forced-close flag"
2245 );
2246 assert!(
2247 !ctx.close_window_requested,
2248 "close_window_forced must NOT raise the guarded-close flag"
2249 );
2250 }
2251
2252 /// End-to-end: a handler calling `close_window_forced` during
2253 /// dispatch must transfer the flag onto the `WidgetTree` (the
2254 /// `collect_from_ctx` teardown), where the app loop drains it via
2255 /// `take_force_close_request` — separately from the guarded
2256 /// `take_close_window_request` flag.
2257 #[test]
2258 fn forced_close_flag_propagates_to_tree_and_drains_independently() {
2259 use crate::test_widgets::FillWidget;
2260 use crate::widget_tree::WidgetTree;
2261
2262 // `run_with_event_context` only runs the `collect_from_ctx`
2263 // teardown (which transfers ctx flags onto the tree) when the
2264 // tree has a root to anchor on, so give it one.
2265 let mut tree = WidgetTree::new();
2266 tree.add(FillWidget::new());
2267 tree.run_with_event_context(&mut NoopWindowOps, |ctx| ctx.close_window_forced());
2268 assert!(
2269 tree.take_force_close_request(),
2270 "forced-close flag must reach the tree"
2271 );
2272 assert!(
2273 !tree.take_close_window_request(),
2274 "a forced close must not also raise the guarded flag"
2275 );
2276
2277 // And the guarded path stays on its own channel.
2278 let mut tree = WidgetTree::new();
2279 tree.add(FillWidget::new());
2280 tree.run_with_event_context(&mut NoopWindowOps, |ctx| ctx.close_window());
2281 assert!(tree.take_close_window_request());
2282 assert!(!tree.take_force_close_request());
2283 }
2284
2285 #[test]
2286 fn windows_enumerates_via_ops() {
2287 let mut ops = RecordingOps::default();
2288 ops.states.borrow_mut().push(make_state(1, Some("a")));
2289 ops.states.borrow_mut().push(make_state(2, Some("b")));
2290 let main_state = make_state(1, Some("a"));
2291 let ctx = EventContext::new().with_window_context(&mut ops, Some(main_state));
2292 let ids: Vec<_> = ctx.windows().iter().map(|s| s.id()).collect();
2293 assert_eq!(ids, vec![TeksiloWindowId::new(1), TeksiloWindowId::new(2)]);
2294 }
2295
2296 #[test]
2297 fn standalone_context_returns_empty_windows_and_none_lookups() {
2298 let ctx = EventContext::new();
2299 assert!(ctx.find_window("anything").is_none());
2300 assert!(ctx.window_state(TeksiloWindowId::new(1)).is_none());
2301 assert!(ctx.windows().is_empty());
2302 }
2303
2304 #[test]
2305 #[should_panic(expected = "open_window called outside of a dispatch")]
2306 fn open_window_on_standalone_context_panics() {
2307 let mut ctx = EventContext::new();
2308 let _ = ctx.open_window(WindowConfig::new());
2309 }
2310
2311 #[test]
2312 fn open_modal_builds_window_config_from_request() {
2313 use crate::modal::{ModalContent, ModalRequest};
2314 let mut ops = RecordingOps::default();
2315 let main_state = make_state(1, Some("main"));
2316 let built_widget = Rc::new(RefCell::new(false));
2317 let built_widget_flag = built_widget.clone();
2318 let request = ModalRequest {
2319 content: ModalContent::Deferred(Box::new(move |_tree| {
2320 *built_widget_flag.borrow_mut() = true;
2321 // Return a dummy WidgetId — not used in this test since
2322 // the RecordingOps doesn't actually build the tree.
2323 crate::widget_id::WidgetId::default()
2324 })),
2325 presentation: crate::modal::ModalPresentation::NativeWindow,
2326 close_behavior: crate::modal::ModalCloseBehavior::default(),
2327 title: Some("Confirm".to_string()),
2328 size: Some((420, 180)),
2329 focus_target: None,
2330 on_dismiss: None,
2331 };
2332 {
2333 let mut ctx = EventContext::new().with_window_context(&mut ops, Some(main_state));
2334 let id = ctx.open_modal(request);
2335 assert!(id.is_some());
2336 }
2337 // open_modal is a thin wrapper over open_window — the config
2338 // it built must reach RecordingOps::open_window.
2339 let calls = ops.open_calls.borrow();
2340 assert_eq!(calls.len(), 1);
2341 let cfg = &calls[0];
2342 assert_eq!(cfg.title, "Confirm");
2343 assert_eq!(cfg.size, (420, 180));
2344 assert!(cfg.is_modal());
2345 assert_eq!(cfg.modal_parent(), Some(TeksiloWindowId::new(1)));
2346 // Cell is just to let us observe something reachable via cfg.root_builder;
2347 // the builder hasn't been called yet (RecordingOps records the config
2348 // but doesn't build the tree).
2349 let _ = built_widget;
2350 }
2351
2352 #[test]
2353 fn open_modal_requires_current_window() {
2354 use crate::modal::{ModalContent, ModalRequest};
2355 let mut ops = RecordingOps::default();
2356 let mut ctx = EventContext::new().with_window_context(&mut ops, None);
2357 let request = ModalRequest {
2358 content: ModalContent::Deferred(Box::new(|_tree| {
2359 crate::widget_id::WidgetId::default()
2360 })),
2361 presentation: crate::modal::ModalPresentation::NativeWindow,
2362 close_behavior: crate::modal::ModalCloseBehavior::default(),
2363 title: None,
2364 size: None,
2365 focus_target: None,
2366 on_dismiss: None,
2367 };
2368 assert!(ctx.open_modal(request).is_none());
2369 }
2370}