Skip to main content

slt/context/
runtime.rs

1use super::*;
2
3impl Context {
4    pub(crate) fn new(
5        events: Vec<Event>,
6        width: u32,
7        height: u32,
8        state: &mut FrameState,
9        theme: Theme,
10    ) -> Self {
11        let hook_states = &mut state.hook_states;
12        let named_states = std::mem::take(&mut state.named_states);
13        // Issue #215: hand off the keyed-state map for this frame. Same
14        // lifetime as `named_states`: moved out at frame start, moved back
15        // at frame end (see `run_frame_kernel`).
16        let keyed_states = std::mem::take(&mut state.keyed_states);
17        // Issue #262: hand off the partial-chord buffer for this frame. Same
18        // lifetime as `keyed_states`: moved out at frame start, moved back at
19        // frame end (see `run_frame_kernel`).
20        let chord = std::mem::take(&mut state.chord_states);
21        // Issue #248: hand off the scheduler timer table for this frame. Same
22        // lifetime as `named_states`: moved out at frame start, moved back at
23        // frame end (where untouched slots are GC'd; see `run_frame_kernel`).
24        let scheduler = std::mem::take(&mut state.scheduler);
25        // Issue #234: hand off the async task registry for this frame. Same
26        // lifetime as `scheduler`: moved out at frame start, moved back at
27        // frame end (see `run_frame_kernel`).
28        #[cfg(feature = "async")]
29        let async_tasks = std::mem::take(&mut state.async_tasks);
30        let screen_hook_map = std::mem::take(&mut state.screen_hook_map);
31        let focus = &mut state.focus;
32        // Issue #217: name→index map from the previous frame, used to resolve
33        // `focus_by_name(name)` at frame start. We move it out so the
34        // `register_focusable_named` calls in this frame can rebuild a fresh
35        // `focus_name_map`. The fresh map is swapped back into
36        // `focus_name_map_prev` at frame end.
37        let focus_name_map_prev = std::mem::take(&mut focus.focus_name_map_prev);
38        let pending_focus_name = focus.pending_focus_name.take();
39        let prev_focus_index = focus.prev_focus_index;
40        let layout_feedback = &mut state.layout_feedback;
41        let diagnostics = &mut state.diagnostics;
42        let consumed = vec![false; events.len()];
43
44        // Single wall-clock sample for this frame, reused for double-click
45        // timing below and for `frame_instant` (the timer/scheduler clock).
46        let frame_now = std::time::Instant::now();
47        let mut mouse_pos = layout_feedback.last_mouse_pos;
48        let mut click_pos = None;
49        let mut right_click_pos = None;
50        let mut double_click_pos = None;
51        let mut scroll_pos = None;
52        let mut scroll_delta_frame: i32 = 0;
53        for event in &events {
54            if let Event::Mouse(mouse) = event {
55                mouse_pos = Some((mouse.x, mouse.y));
56                match mouse.kind {
57                    MouseKind::Down(MouseButton::Left) => {
58                        click_pos = Some((mouse.x, mouse.y));
59                        // v0.21.1: a left click on the same cell as the previous
60                        // click, within `DOUBLE_CLICK_WINDOW`, is a double-click.
61                        // Clear the tracker after firing so a third click starts
62                        // a fresh pair (no triple-counting).
63                        let pos = (mouse.x, mouse.y);
64                        let is_double = layout_feedback.last_click_pos == Some(pos)
65                            && layout_feedback.last_click_at.is_some_and(|t| {
66                                frame_now.duration_since(t) <= crate::DOUBLE_CLICK_WINDOW
67                            });
68                        if is_double {
69                            double_click_pos = Some(pos);
70                            layout_feedback.last_click_at = None;
71                            layout_feedback.last_click_pos = None;
72                        } else {
73                            layout_feedback.last_click_at = Some(frame_now);
74                            layout_feedback.last_click_pos = Some(pos);
75                        }
76                    }
77                    MouseKind::Down(MouseButton::Right) => {
78                        // Issue #208: capture last right-click position so
79                        // `response_for` can hit-test against per-widget rects.
80                        right_click_pos = Some((mouse.x, mouse.y));
81                    }
82                    // v0.21.1: accumulate net vertical wheel delta + the cursor
83                    // position, hover-gated per-widget by `response_for`.
84                    MouseKind::ScrollUp => {
85                        scroll_pos = Some((mouse.x, mouse.y));
86                        scroll_delta_frame = scroll_delta_frame.saturating_add(1);
87                    }
88                    MouseKind::ScrollDown => {
89                        scroll_pos = Some((mouse.x, mouse.y));
90                        scroll_delta_frame = scroll_delta_frame.saturating_sub(1);
91                    }
92                    _ => {}
93                }
94            }
95        }
96
97        let mut focus_index = focus.focus_index;
98        if let Some((mx, my)) = click_pos {
99            let mut best: Option<(usize, u64)> = None;
100            for &(fid, rect) in &layout_feedback.prev_focus_rects {
101                if mx >= rect.x && mx < rect.right() && my >= rect.y && my < rect.bottom() {
102                    let area = rect.width as u64 * rect.height as u64;
103                    if best.is_none_or(|(_, ba)| area < ba) {
104                        best = Some((fid, area));
105                    }
106                }
107            }
108            if let Some((fid, _)) = best {
109                focus_index = fid;
110            }
111        }
112
113        // Issue #217: resolve a pending `focus_by_name(...)` request against
114        // the previous frame's `name → index` map. If the name wasn't
115        // registered last frame, we keep the request pending for the next
116        // frame so a widget that registers later can still receive focus.
117        // If the request resolves, we consume it.
118        let mut still_pending: Option<String> = None;
119        if let Some(name) = pending_focus_name {
120            if let Some(&resolved) = focus_name_map_prev.get(&name) {
121                focus_index = resolved;
122            } else {
123                still_pending = Some(name);
124            }
125        }
126
127        // Reuse `commands_buf` capacity from the previous frame (issue #150).
128        // `mem::take` swaps an empty Vec into `state.commands_buf`; we then
129        // clear (no-op when reclaimed from a `build_tree` drain, defensive
130        // when reclaimed from the quit path that ran without `build_tree`)
131        // and reuse the allocation. After `build_tree(&mut ctx.commands)`
132        // drains the Vec in place, the empty (but capacity-bearing) Vec is
133        // moved back into `state.commands_buf` at frame end inside
134        // `run_frame_kernel`.
135        let mut commands = std::mem::take(&mut state.commands_buf);
136        commands.clear();
137
138        // Issue #204: reuse the six per-frame `Vec`/`HashSet` allocations
139        // (`context_stack`, `deferred_draws`, `rollback.group_stack`,
140        // `rollback.text_color_stack`, `pending_tooltips`, `hovered_groups`).
141        // Same `mem::take` pattern as `commands_buf` (#150). Each buffer is
142        // empty at frame end (asserted at `run_frame_kernel`) — `mem::take`
143        // hands a `Default::default()` empty back to the state, the Vec/HashSet
144        // we move into `Context` keeps its capacity from the prior frame, and
145        // `clear()` here is a no-op except as a defensive guard against future
146        // refactors that might leak items past the assertions.
147        let mut context_stack = std::mem::take(&mut state.context_stack_buf);
148        context_stack.clear();
149        let mut deferred_draws = std::mem::take(&mut state.deferred_draws_buf);
150        deferred_draws.clear();
151        let mut group_stack = std::mem::take(&mut state.group_stack_buf);
152        group_stack.clear();
153        let mut text_color_stack = std::mem::take(&mut state.text_color_stack_buf);
154        text_color_stack.clear();
155        let mut pending_tooltips = std::mem::take(&mut state.pending_tooltips_buf);
156        pending_tooltips.clear();
157        let hovered_groups = std::mem::take(&mut state.hovered_groups_buf);
158        // `hovered_groups` is `clear()`-ed inside `build_hovered_groups`
159        // immediately below, so we do not pre-clear here — capacity is
160        // preserved across frames.
161
162        // Issue #273: hand off the previous frame's `cached` region keys and a
163        // recycled (cleared) buffer to record this frame's keys into. Both
164        // round-trip back into `FrameState` at frame end. Empty (zero
165        // overhead) for apps that never call `cached`.
166        let region_versions_prev = std::mem::take(&mut state.region_versions);
167        let mut region_versions_cur = std::mem::take(&mut state.region_versions_buf);
168        region_versions_cur.clear();
169
170        let mut ctx = Self {
171            commands,
172            events,
173            consumed,
174            should_quit: false,
175            area_width: width,
176            area_height: height,
177            tick: diagnostics.tick,
178            focus_index,
179            hook_states: std::mem::take(hook_states),
180            named_states,
181            keyed_states,
182            chord,
183            context_stack,
184            prev_focus_count: focus.prev_focus_count,
185            prev_modal_focus_start: focus.prev_modal_focus_start,
186            prev_modal_focus_count: focus.prev_modal_focus_count,
187            prev_scroll_infos: std::mem::take(&mut layout_feedback.prev_scroll_infos),
188            prev_scroll_rects: std::mem::take(&mut layout_feedback.prev_scroll_rects),
189            prev_hit_map: std::mem::take(&mut layout_feedback.prev_hit_map),
190            prev_group_rects: std::mem::take(&mut layout_feedback.prev_group_rects),
191            prev_focus_groups: std::mem::take(&mut layout_feedback.prev_focus_groups),
192            mouse_pos,
193            click_pos,
194            right_click_pos,
195            double_click_pos,
196            scroll_pos,
197            scroll_delta_frame,
198            prev_modal_active: focus.prev_modal_active,
199            clipboard_text: None,
200            debug: diagnostics.debug_mode,
201            debug_layer: diagnostics.debug_layer,
202            inspector_mode: diagnostics.inspector_mode,
203            theme,
204            is_real_terminal: false,
205            // Issue #264: conservative default; overwritten by the probed
206            // snapshot in `run_frame_kernel` on a real terminal.
207            #[cfg(feature = "crossterm")]
208            capabilities: crate::terminal::Capabilities::default(),
209            deferred_draws,
210            rollback: ContextRollbackState {
211                last_text_idx: None,
212                focus_count: 0,
213                last_focusable_id: None,
214                pending_focusable_id: None,
215                interaction_count: 0,
216                scroll_count: 0,
217                group_count: 0,
218                group_stack,
219                overlay_depth: 0,
220                modal_active: false,
221                modal_focus_start: 0,
222                modal_focus_count: 0,
223                hook_cursor: 0,
224                dark_mode: theme.is_dark,
225                notification_queue: std::mem::take(&mut diagnostics.notification_queue),
226                text_color_stack,
227            },
228            pending_tooltips,
229            hovered_groups,
230            region_versions_prev,
231            region_versions_cur,
232            region_cache_hits: 0,
233            region_cache_misses: 0,
234            scroll_lines_per_event: 1,
235            screen_hook_map,
236            widget_theme: WidgetTheme::new(),
237            prev_focus_index,
238            focus_name_map_prev,
239            focus_name_map: std::collections::HashMap::new(),
240            pending_focus_name: still_pending,
241            // Issue #248: sample a single wall-clock "now" for every timer
242            // method called this frame. v0.21.1: reuse the `frame_now` sampled
243            // above (also used for double-click timing) so the frame has one
244            // coherent clock reading.
245            frame_instant: frame_now,
246            scheduler,
247            // Issue #234: async task registry round-tripped like `scheduler`.
248            #[cfg(feature = "async")]
249            async_tasks,
250        };
251        ctx.build_hovered_groups();
252        ctx
253    }
254
255    fn build_hovered_groups(&mut self) {
256        self.hovered_groups.clear();
257        if let Some(pos) = self.mouse_pos {
258            for (name, rect) in &self.prev_group_rects {
259                if pos.0 >= rect.x
260                    && pos.0 < rect.x + rect.width
261                    && pos.1 >= rect.y
262                    && pos.1 < rect.y + rect.height
263                {
264                    self.hovered_groups.insert(std::sync::Arc::clone(name));
265                }
266            }
267        }
268    }
269
270    /// Set how many lines each scroll event moves. Default is 1.
271    pub fn set_scroll_speed(&mut self, lines: u32) {
272        self.scroll_lines_per_event = lines.max(1);
273    }
274
275    /// Get the current scroll speed (lines per scroll event).
276    pub fn scroll_speed(&self) -> u32 {
277        self.scroll_lines_per_event
278    }
279
280    /// Get the current focus index.
281    ///
282    /// Widget indices are assigned in the order [`register_focusable()`](Self::register_focusable) is called.
283    /// Indices are 0-based and wrap at [`focus_count()`](Self::focus_count).
284    pub fn focus_index(&self) -> usize {
285        self.focus_index
286    }
287
288    /// Set the focus index to a specific focusable widget.
289    ///
290    /// Widget indices are assigned in the order [`register_focusable()`](Self::register_focusable) is called
291    /// (0-based). If `index` exceeds the number of focusable widgets it will
292    /// be clamped by the modulo in [`register_focusable`](Self::register_focusable).
293    ///
294    /// # Example
295    ///
296    /// ```no_run
297    /// # slt::run(|ui: &mut slt::Context| {
298    /// // Focus the second focusable widget (index 1)
299    /// ui.set_focus_index(1);
300    /// # });
301    /// ```
302    pub fn set_focus_index(&mut self, index: usize) {
303        self.focus_index = index;
304    }
305
306    /// Get the number of focusable widgets registered in the previous frame.
307    ///
308    /// Returns 0 on the very first frame. Useful together with
309    /// [`set_focus_index()`](Self::set_focus_index) for programmatic focus control.
310    ///
311    /// Note: this intentionally reads `prev_focus_count` (the settled count
312    /// from the last completed frame) rather than `focus_count` (the
313    /// still-incrementing counter for the current frame).
314    #[allow(clippy::misnamed_getters)]
315    pub fn focus_count(&self) -> usize {
316        self.prev_focus_count
317    }
318
319    /// Advance keyboard focus one step, honoring an active modal's focus trap.
320    /// `forward` selects next vs previous; both wrap. Shared by
321    /// [`focus_next`](Self::focus_next) / [`focus_prev`](Self::focus_prev) and
322    /// the `Tab`/`Shift+Tab` handler in `process_focus_keys` (v0.21.1).
323    pub(crate) fn advance_focus(&mut self, forward: bool) {
324        if self.prev_modal_active && self.prev_modal_focus_count > 0 {
325            let mut modal_local = self.focus_index.saturating_sub(self.prev_modal_focus_start);
326            modal_local %= self.prev_modal_focus_count;
327            let next = if forward {
328                (modal_local + 1) % self.prev_modal_focus_count
329            } else if modal_local == 0 {
330                self.prev_modal_focus_count - 1
331            } else {
332                modal_local - 1
333            };
334            self.focus_index = self.prev_modal_focus_start + next;
335        } else if self.prev_focus_count > 0 {
336            self.focus_index = if forward {
337                (self.focus_index + 1) % self.prev_focus_count
338            } else if self.focus_index == 0 {
339                self.prev_focus_count - 1
340            } else {
341                self.focus_index - 1
342            };
343        }
344    }
345
346    /// Move keyboard focus to the next focusable widget (wrapping), exactly as
347    /// pressing `Tab` would. Honors an active modal's focus trap. Pairs with
348    /// [`set_focus_index`](Self::set_focus_index) / [`focus_count`](Self::focus_count)
349    /// for programmatic focus control (e.g. an app-level shortcut). Available
350    /// since v0.21.1.
351    ///
352    /// # Example
353    ///
354    /// ```no_run
355    /// # slt::run(|ui: &mut slt::Context| {
356    /// // Advance focus on a custom shortcut (e.g. a vim-style 'j').
357    /// if ui.key('j') {
358    ///     ui.focus_next();
359    /// }
360    /// # });
361    /// ```
362    pub fn focus_next(&mut self) {
363        self.advance_focus(true);
364    }
365
366    /// Move keyboard focus to the previous focusable widget (wrapping), exactly
367    /// as `Shift+Tab` would. Honors an active modal's focus trap. Available
368    /// since v0.21.1.
369    pub fn focus_prev(&mut self) {
370        self.advance_focus(false);
371    }
372
373    /// Move focus to the next focusable widget belonging to the named focus
374    /// group, wrapping within the group. If focus is currently outside the
375    /// group it jumps to the group's first member. No-op if the group had no
376    /// focusable widgets on the previous frame.
377    ///
378    /// Focus groups are declared with [`group`](Self::group); this is the
379    /// scoped counterpart to [`focus_next`](Self::focus_next) for building a
380    /// focus trap around a panel or sub-form without a modal. Available since
381    /// v0.21.1.
382    pub fn focus_next_in_group(&mut self, group: &str) {
383        self.advance_focus_in_group(group, true);
384    }
385
386    /// Move focus to the previous focusable widget in the named group
387    /// (wrapping). See [`focus_next_in_group`](Self::focus_next_in_group).
388    /// Available since v0.21.1.
389    pub fn focus_prev_in_group(&mut self, group: &str) {
390        self.advance_focus_in_group(group, false);
391    }
392
393    fn advance_focus_in_group(&mut self, group: &str, forward: bool) {
394        // Membership comes from the previous frame's `index -> group` table,
395        // the same source `is_group_focused` consults. Indices are valid
396        // focus indices (0..prev_focus_count).
397        let members: Vec<usize> = self
398            .prev_focus_groups
399            .iter()
400            .enumerate()
401            .filter_map(|(idx, g)| match g.as_deref() {
402                Some(name) if name == group => Some(idx),
403                _ => None,
404            })
405            .collect();
406        if members.is_empty() {
407            return;
408        }
409        let new_pos = match members.iter().position(|&m| m == self.focus_index) {
410            Some(p) => {
411                if forward {
412                    (p + 1) % members.len()
413                } else if p == 0 {
414                    members.len() - 1
415                } else {
416                    p - 1
417                }
418            }
419            // Focus is outside the group: jump to its first member.
420            None => 0,
421        };
422        self.focus_index = members[new_pos];
423    }
424
425    /// Read-only snapshot of the terminal's negotiated capabilities
426    /// (issue #264).
427    ///
428    /// Populated once at session enter via a DA1/DA2/XTGETTCAP probe. This is
429    /// **diagnostics-only**: image rendering already routes through the
430    /// automatic blitter ladder (Kitty > Sixel > sextant > half-block), so app
431    /// code is never required to branch on the returned value. On a headless
432    /// backend (e.g. [`TestBackend`](crate::TestBackend)) or piped stdout, the
433    /// probe is skipped and every field is a conservative default.
434    ///
435    /// Available since `0.21.0`.
436    ///
437    /// # Example
438    ///
439    /// ```no_run
440    /// # slt::run(|ui: &mut slt::Context| {
441    /// let caps = ui.capabilities();
442    /// // e.g. surface a "truecolor: on" line in a diagnostics panel.
443    /// let _ = caps.truecolor;
444    /// # });
445    /// ```
446    #[cfg(feature = "crossterm")]
447    #[cfg_attr(docsrs, doc(cfg(feature = "crossterm")))]
448    pub fn capabilities(&self) -> &crate::terminal::Capabilities {
449        &self.capabilities
450    }
451
452    pub(crate) fn process_focus_keys(&mut self) {
453        // Scan for Tab / Shift+Tab / BackTab, recording the direction of each
454        // and consuming the event. The mutation (`advance_focus`) is applied
455        // after the scan: it borrows `&mut self` wholesale, which cannot run
456        // while `self.events` is iterated by reference. Collecting first
457        // preserves the original "each Tab advances once" semantics.
458        let mut actions: Vec<bool> = Vec::new();
459        for (i, event) in self.events.iter().enumerate() {
460            if self.consumed[i] {
461                continue;
462            }
463            if let Event::Key(key) = event {
464                if key.kind != KeyEventKind::Press {
465                    continue;
466                }
467                if key.code == KeyCode::Tab && !key.modifiers.contains(KeyModifiers::SHIFT) {
468                    actions.push(true);
469                    self.consumed[i] = true;
470                } else if (key.code == KeyCode::Tab && key.modifiers.contains(KeyModifiers::SHIFT))
471                    || key.code == KeyCode::BackTab
472                {
473                    actions.push(false);
474                    self.consumed[i] = true;
475                }
476            }
477        }
478        for forward in actions {
479            self.advance_focus(forward);
480        }
481    }
482
483    /// Render a custom [`Widget`].
484    ///
485    /// Calls [`Widget::ui`] with this context and returns the widget's response.
486    pub fn widget<W: Widget>(&mut self, w: &mut W) -> W::Response {
487        w.ui(self)
488    }
489
490    /// Wrap child widgets in a panic boundary.
491    ///
492    /// If the closure panics, the panic is caught and an error message is
493    /// rendered in place of the children. The app continues running.
494    ///
495    /// # Example
496    ///
497    /// ```no_run
498    /// # slt::run(|ui: &mut slt::Context| {
499    /// ui.error_boundary(|ui| {
500    ///     ui.text("risky widget");
501    /// });
502    /// # });
503    /// ```
504    pub fn error_boundary(&mut self, f: impl FnOnce(&mut Context)) {
505        self.error_boundary_with(f, |ui, msg| {
506            ui.styled(
507                format!("⚠ Error: {msg}"),
508                Style::new().fg(ui.theme.error).bold(),
509            );
510        });
511    }
512
513    /// Like [`error_boundary`](Self::error_boundary), but renders a custom
514    /// fallback instead of the default error message.
515    ///
516    /// The fallback closure receives the panic message as a [`String`].
517    ///
518    /// # Example
519    ///
520    /// ```no_run
521    /// # slt::run(|ui: &mut slt::Context| {
522    /// ui.error_boundary_with(
523    ///     |ui| {
524    ///         ui.text("risky widget");
525    ///     },
526    ///     |ui, msg| {
527    ///         ui.text(format!("Recovered from panic: {msg}"));
528    ///     },
529    /// );
530    /// # });
531    /// ```
532    pub fn error_boundary_with(
533        &mut self,
534        f: impl FnOnce(&mut Context),
535        fallback: impl FnOnce(&mut Context, String),
536    ) {
537        let snapshot = ContextCheckpoint::capture(self);
538
539        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
540            f(self);
541        }));
542
543        match result {
544            Ok(()) => {}
545            Err(panic_info) => {
546                if self.is_real_terminal {
547                    #[cfg(feature = "crossterm")]
548                    {
549                        let _ = crossterm::terminal::enable_raw_mode();
550                        let _ = crossterm::execute!(
551                            std::io::stdout(),
552                            crossterm::terminal::EnterAlternateScreen
553                        );
554                    }
555
556                    #[cfg(not(feature = "crossterm"))]
557                    {}
558                }
559
560                snapshot.restore(self);
561
562                let msg = if let Some(s) = panic_info.downcast_ref::<&str>() {
563                    (*s).to_string()
564                } else if let Some(s) = panic_info.downcast_ref::<String>() {
565                    s.clone()
566                } else {
567                    "widget panicked".to_string()
568                };
569
570                fallback(self, msg);
571            }
572        }
573    }
574
575    /// Reserve the next interaction slot without emitting a marker command.
576    pub(crate) fn reserve_interaction_slot(&mut self) -> usize {
577        let id = self.rollback.interaction_count;
578        self.rollback.interaction_count += 1;
579        id
580    }
581
582    /// Advance the interaction counter for structural commands that still
583    /// participate in hit-map indexing.
584    pub(crate) fn skip_interaction_slot(&mut self) {
585        self.reserve_interaction_slot();
586    }
587
588    /// Issue #273: record a [`ContainerBuilder::cached`] region's version key
589    /// at its (declaration-ordered) call site and classify it as a hit or
590    /// miss versus the previous frame.
591    ///
592    /// Returns `true` if `version_key` matches the value this call site
593    /// recorded last frame (a hit), `false` on a key change, a brand-new slot,
594    /// the first frame, or after a resize (all misses).
595    ///
596    /// This is purely an *author-declared stability signal*: the caller still
597    /// re-runs its closure every frame, so output stays byte-identical and the
598    /// immediate-mode invariant is preserved exactly. The hit/miss result is
599    /// recorded for diagnostics ([`Context::region_cache_hits`] /
600    /// [`Context::region_cache_misses`]) and to give a future cell-level cache
601    /// a sound, principle-preserving gate. See the type-level docs on
602    /// [`ContainerBuilder::cached`] for the full design rationale.
603    pub(crate) fn record_cached_region(&mut self, version_key: u64) -> bool {
604        let idx = self.region_versions_cur.len();
605        let hit = self
606            .region_versions_prev
607            .get(idx)
608            .is_some_and(|&prev| prev == version_key);
609        self.region_versions_cur.push(version_key);
610        if hit {
611            self.region_cache_hits = self.region_cache_hits.saturating_add(1);
612        } else {
613            self.region_cache_misses = self.region_cache_misses.saturating_add(1);
614        }
615        hit
616    }
617
618    /// Number of [`ContainerBuilder::cached`] regions this frame whose version
619    /// key was unchanged from the previous frame (cache hits).
620    ///
621    /// Diagnostics for the opt-in streaming cache (issue #273). A region is a
622    /// hit when its author-supplied `version_key` matches the value the same
623    /// call site recorded last frame; it misses on a key change, a new call
624    /// site, the first frame, or after a terminal resize.
625    ///
626    /// Since 0.21.0.
627    ///
628    /// # Example
629    /// ```no_run
630    /// # slt::run(|ui: &mut slt::Context| {
631    /// ui.container().cached(42, |ui| {
632    ///     ui.text("stable chrome");
633    /// });
634    /// let _hits = ui.region_cache_hits();
635    /// # });
636    /// ```
637    pub fn region_cache_hits(&self) -> u32 {
638        self.region_cache_hits
639    }
640
641    /// Number of [`ContainerBuilder::cached`] regions this frame whose version
642    /// key changed (or was new / first-frame / post-resize) — cache misses.
643    ///
644    /// The counterpart to [`Context::region_cache_hits`]. See issue #273.
645    ///
646    /// Since 0.21.0.
647    ///
648    /// # Example
649    /// ```no_run
650    /// # slt::run(|ui: &mut slt::Context| {
651    /// ui.container().cached(7, |ui| {
652    ///     ui.text("chrome");
653    /// });
654    /// let _misses = ui.region_cache_misses();
655    /// # });
656    /// ```
657    pub fn region_cache_misses(&self) -> u32 {
658        self.region_cache_misses
659    }
660
661    /// Reserve the next interaction ID and emit a marker command.
662    pub(crate) fn next_interaction_id(&mut self) -> usize {
663        let id = self.reserve_interaction_slot();
664        self.commands.push(Command::InteractionMarker(id));
665        id
666    }
667
668    /// Allocate a click/hover interaction slot and return the [`Response`].
669    ///
670    /// Use this in custom widgets to detect mouse clicks and hovers without
671    /// wrapping content in a container. Call it immediately before the text,
672    /// rich text, link, or container that should own the interaction rect.
673    /// Each call reserves one slot in the hit-test map, so the call order
674    /// must be stable across frames.
675    pub fn interaction(&mut self) -> Response {
676        if (self.rollback.modal_active || self.prev_modal_active)
677            && self.rollback.overlay_depth == 0
678        {
679            return Response::none();
680        }
681        let id = self.next_interaction_id();
682        self.response_for(id)
683    }
684
685    /// Compute and consume the `(gained_focus, lost_focus)` edge flags for the
686    /// widget most recently registered via [`register_focusable`].
687    ///
688    /// If that focusable lined up with the previously-focused widget index from
689    /// the prior frame, the focus change since maps directly to gained/lost.
690    /// Takes (consumes) the `last_focusable_id` marker so a single
691    /// `register_focusable` powers exactly one transition computation.
692    ///
693    /// Shared by [`begin_widget_interaction`](Self::begin_widget_interaction)
694    /// and the widgets that assemble their `Response` by hand rather than
695    /// through it (`text_input`, `slider`, `number_input`) — issue #208 left
696    /// those three reporting `gained_focus`/`lost_focus` as always-false; this
697    /// closes that gap (v0.21.1).
698    pub(crate) fn focus_transitions(&mut self, focused: bool) -> (bool, bool) {
699        if let Some(this_id) = self.rollback.last_focusable_id.take() {
700            let was_focused = self
701                .prev_focus_index
702                .map(|prev| prev == this_id)
703                .unwrap_or(false);
704            (focused && !was_focused, !focused && was_focused)
705        } else {
706            (false, false)
707        }
708    }
709
710    pub(crate) fn begin_widget_interaction(&mut self, focused: bool) -> (usize, Response) {
711        let interaction_id = self.next_interaction_id();
712        let mut response = self.response_for(interaction_id);
713        response.focused = focused;
714        let (gained, lost) = self.focus_transitions(focused);
715        response.gained_focus = gained;
716        response.lost_focus = lost;
717        (interaction_id, response)
718    }
719
720    pub(crate) fn consume_indices<I>(&mut self, indices: I)
721    where
722        I: IntoIterator<Item = usize>,
723    {
724        for index in indices {
725            self.consumed[index] = true;
726        }
727    }
728
729    pub(crate) fn available_key_presses(
730        &self,
731    ) -> impl Iterator<Item = (usize, &crate::event::KeyEvent)> + '_ {
732        self.events.iter().enumerate().filter_map(|(i, event)| {
733            if self.consumed[i] {
734                return None;
735            }
736            match event {
737                Event::Key(key) if key.kind == KeyEventKind::Press => Some((i, key)),
738                _ => None,
739            }
740        })
741    }
742
743    pub(crate) fn available_pastes(&self) -> impl Iterator<Item = (usize, &str)> + '_ {
744        self.events.iter().enumerate().filter_map(|(i, event)| {
745            if self.consumed[i] {
746                return None;
747            }
748            match event {
749                Event::Paste(text) => Some((i, text.as_str())),
750                _ => None,
751            }
752        })
753    }
754
755    pub(crate) fn left_clicks_in_rect(
756        &self,
757        rect: Rect,
758    ) -> impl Iterator<Item = (usize, &crate::event::MouseEvent)> + '_ {
759        self.mouse_events_in_rect(rect).filter_map(|(i, mouse)| {
760            if matches!(mouse.kind, MouseKind::Down(MouseButton::Left)) {
761                Some((i, mouse))
762            } else {
763                None
764            }
765        })
766    }
767
768    pub(crate) fn mouse_events_in_rect(
769        &self,
770        rect: Rect,
771    ) -> impl Iterator<Item = (usize, &crate::event::MouseEvent)> + '_ {
772        self.events
773            .iter()
774            .enumerate()
775            .filter_map(move |(i, event)| {
776                if self.consumed[i] {
777                    return None;
778                }
779
780                let Event::Mouse(mouse) = event else {
781                    return None;
782                };
783
784                if mouse.x < rect.x
785                    || mouse.x >= rect.right()
786                    || mouse.y < rect.y
787                    || mouse.y >= rect.bottom()
788                {
789                    return None;
790                }
791
792                Some((i, mouse))
793            })
794    }
795
796    pub(crate) fn left_clicks_for_interaction(
797        &self,
798        interaction_id: usize,
799    ) -> Option<(Rect, Vec<(usize, &crate::event::MouseEvent)>)> {
800        let rect = self.prev_hit_map.get(interaction_id).copied()?;
801        let clicks = self.left_clicks_in_rect(rect).collect();
802        Some((rect, clicks))
803    }
804
805    pub(crate) fn consume_activation_keys(&mut self, focused: bool) -> bool {
806        if !focused {
807            return false;
808        }
809
810        // Activation keys (Enter / Space) are typically 0–1 per frame and
811        // bounded above by the simultaneous-keypress count from the input
812        // pipeline (well under 8 in practice). A `SmallVec` with an 8-slot
813        // inline capacity eliminates the per-focusable `Vec<usize>` heap
814        // allocation that showed up on every focused widget × every frame.
815        // Spillover beyond 8 falls back to the heap automatically. Closes #135.
816        let consumed: smallvec::SmallVec<[usize; 8]> = self
817            .available_key_presses()
818            .filter_map(|(i, key)| {
819                if matches!(key.code, KeyCode::Enter | KeyCode::Char(' ')) {
820                    Some(i)
821                } else {
822                    None
823                }
824            })
825            .collect();
826        let activated = !consumed.is_empty();
827        if activated {
828            // `consume_indices` takes `IntoIterator<Item = usize>` — `SmallVec`
829            // satisfies that bound directly, no signature change needed.
830            self.consume_indices(consumed);
831        }
832        activated
833    }
834
835    /// Register a widget as focusable and return whether it currently has focus.
836    ///
837    /// Call this in custom widgets that need keyboard focus. Each call increments
838    /// the internal focus counter, so the call order must be stable across frames.
839    ///
840    /// # Slot reservation by `register_focusable_named`
841    ///
842    /// If [`register_focusable_named`](Self::register_focusable_named) was
843    /// called immediately before this call, it has already allocated a
844    /// slot and bound a name to it; this call **reuses** that slot
845    /// instead of allocating a fresh one. That keeps the name binding
846    /// pointed at the widget the user sees rather than at a dummy slot.
847    pub fn register_focusable(&mut self) -> bool {
848        if (self.rollback.modal_active || self.prev_modal_active)
849            && self.rollback.overlay_depth == 0
850        {
851            self.rollback.last_focusable_id = None;
852            // Drop any pending reservation: the suppressed widget never
853            // attached, so reusing the reserved id from a later widget in
854            // the same frame would silently rebind the name to the wrong
855            // slot.
856            self.rollback.pending_focusable_id = None;
857            return false;
858        }
859        // Issue #217 follow-up: if `register_focusable_named` reserved a
860        // slot for us, reuse it (and skip the FocusMarker push — it was
861        // already emitted when the reservation was made). Otherwise,
862        // allocate a fresh slot the normal way.
863        let (id, freshly_allocated) =
864            if let Some(reserved) = self.rollback.pending_focusable_id.take() {
865                (reserved, false)
866            } else {
867                let id = self.rollback.focus_count;
868                self.rollback.focus_count += 1;
869                (id, true)
870            };
871        // Issue #208: remember this widget's focus id so the immediately
872        // following `begin_widget_interaction` call can compare against
873        // `prev_focus_index` and emit gained/lost focus signals.
874        self.rollback.last_focusable_id = Some(id);
875        if freshly_allocated {
876            self.commands.push(Command::FocusMarker(id));
877        }
878        if self.prev_modal_active
879            && self.prev_modal_focus_count > 0
880            && self.rollback.modal_active
881            && self.rollback.overlay_depth > 0
882        {
883            let mut modal_local_id = id.saturating_sub(self.rollback.modal_focus_start);
884            modal_local_id %= self.prev_modal_focus_count;
885            let mut modal_focus_idx = self.focus_index.saturating_sub(self.prev_modal_focus_start);
886            modal_focus_idx %= self.prev_modal_focus_count;
887            return modal_local_id == modal_focus_idx;
888        }
889        if self.prev_focus_count == 0 {
890            return true;
891        }
892        self.focus_index % self.prev_focus_count == id
893    }
894
895    /// Create persistent state that survives across frames.
896    ///
897    /// Returns a `State<T>` handle. Access with `state.get(ui)` / `state.get_mut(ui)`.
898    ///
899    /// # Rules
900    /// - Must be called in the same order every frame (like React hooks)
901    /// - Do NOT call inside if/else that changes between frames
902    ///
903    /// # Example
904    /// ```ignore
905    /// let count = ui.use_state(|| 0i32);
906    /// let val = count.get(ui);
907    /// ui.text(format!("Count: {val}"));
908    /// if ui.button("+1").clicked {
909    ///     *count.get_mut(ui) += 1;
910    /// }
911    /// ```
912    pub fn use_state<T: 'static>(&mut self, init: impl FnOnce() -> T) -> State<T> {
913        let idx = self.rollback.hook_cursor;
914        self.rollback.hook_cursor += 1;
915
916        if idx >= self.hook_states.len() {
917            self.hook_states.push(Box::new(init()));
918        }
919
920        State::from_idx(idx)
921    }
922
923    /// Component-local persistent state keyed by a stable id.
924    ///
925    /// Unlike [`use_state`](Self::use_state), this is **not order-dependent** —
926    /// the value is looked up by `id` instead of call position. Safe to call
927    /// inside conditional branches or reusable component functions.
928    ///
929    /// Returns a `State<T>` handle. Access with `state.get(ui)` /
930    /// `state.get_mut(ui)`. Persists across frames.
931    ///
932    /// # Scoping
933    ///
934    /// Keys are `&'static str` and live in a single global namespace per
935    /// `Context` (no automatic per-component scoping). Two calls with the same
936    /// `id` in the same frame share the same value, regardless of where they
937    /// occur in the tree. Pick unique ids — for example, prefix with a
938    /// component name (`"counter::value"`).
939    ///
940    /// # Naming
941    ///
942    /// The no-suffix form takes an `init` closure, matching
943    /// [`use_state`](Self::use_state)`(init)` and
944    /// [`use_state_keyed`](Self::use_state_keyed)`(id, init)`. Use
945    /// [`use_state_named_default`](Self::use_state_named_default) for the
946    /// `T: Default` shorthand.
947    ///
948    /// # Example
949    ///
950    /// ```no_run
951    /// fn counter(ui: &mut slt::Context) {
952    ///     let count = ui.use_state_named("counter::value", || 0i32);
953    ///     ui.text(format!("Count: {}", count.get(ui)));
954    ///     if ui.button("+1").clicked {
955    ///         *count.get_mut(ui) += 1;
956    ///     }
957    /// }
958    /// ```
959    pub fn use_state_named<T: 'static>(
960        &mut self,
961        id: &'static str,
962        init: impl FnOnce() -> T,
963    ) -> State<T> {
964        self.named_states
965            .entry(id)
966            .or_insert_with(|| Box::new(init()));
967        State::from_named(id)
968    }
969
970    /// Like [`use_state_named`](Self::use_state_named), but uses
971    /// [`Default::default()`] to initialize the value on first call.
972    ///
973    /// Mirrors [`use_state_keyed_default`](Self::use_state_keyed_default): the
974    /// `_default` suffix means "no init closure, `T: Default` required".
975    ///
976    /// # Example
977    ///
978    /// ```no_run
979    /// # slt::run(|ui: &mut slt::Context| {
980    /// let value = ui.use_state_named_default::<i32>("counter::value");
981    /// ui.text(format!("{}", value.get(ui)));
982    /// # });
983    /// ```
984    pub fn use_state_named_default<T: 'static + Default>(&mut self, id: &'static str) -> State<T> {
985        self.use_state_named(id, T::default)
986    }
987
988    /// Deprecated alias for [`use_state_named`](Self::use_state_named).
989    ///
990    /// **Deprecated since 0.21.0**: the `_named` family now follows the
991    /// "no-suffix = init closure" convention so it matches
992    /// [`use_state`](Self::use_state) and
993    /// [`use_state_keyed`](Self::use_state_keyed). The init-closure form is now
994    /// spelled `use_state_named(id, init)`; the `T: Default` shorthand is
995    /// [`use_state_named_default`](Self::use_state_named_default).
996    ///
997    /// # Example
998    ///
999    /// ```no_run
1000    /// # slt::run(|ui: &mut slt::Context| {
1001    /// // Old: ui.use_state_named_with("counter::value", || 0i32)
1002    /// let count = ui.use_state_named("counter::value", || 0i32);
1003    /// ui.text(format!("{}", count.get(ui)));
1004    /// # });
1005    /// ```
1006    #[deprecated(
1007        since = "0.21.0",
1008        note = "Renamed to `use_state_named` — the no-suffix form now takes the init closure, matching `use_state` / `use_state_keyed`."
1009    )]
1010    pub fn use_state_named_with<T: 'static>(
1011        &mut self,
1012        id: &'static str,
1013        init: impl FnOnce() -> T,
1014    ) -> State<T> {
1015        self.use_state_named(id, init)
1016    }
1017
1018    /// Smoothly animate between `0.0` and `1.0` driven by a boolean.
1019    ///
1020    /// Returns the current interpolated value (0.0..=1.0). When `value` is
1021    /// `true` the result tweens toward `1.0`; when `false` it tweens back
1022    /// toward `0.0`. The transition duration defaults to
1023    /// [`DEFAULT_ANIMATE_TICKS`](crate::anim::DEFAULT_ANIMATE_TICKS) (12 ticks
1024    /// ≈ 200 ms at 60 Hz). Use [`Context::animate_value`] for custom duration
1025    /// or non-binary targets.
1026    ///
1027    /// State is stored in the per-context named-state map under `id`. The
1028    /// id is `&'static str` (single global namespace per context), matching
1029    /// [`Context::use_state_named`]. Pick a unique key per call site — two
1030    /// `animate_bool` calls with the same id share state.
1031    ///
1032    /// On the first call, the value snaps to the target with no visible
1033    /// transition (so widgets that mount in their final state don't pop).
1034    ///
1035    /// # Example
1036    /// ```ignore
1037    /// let opacity = ui.animate_bool("sidebar::visible", is_open);
1038    /// // 0.0 ≤ opacity ≤ 1.0; use as alpha or visibility threshold.
1039    /// ```
1040    ///
1041    /// # See also
1042    ///
1043    /// - [`animate_value`](Self::animate_value) — the underlying primitive this
1044    ///   delegates to; use it for a custom duration or a non-binary target.
1045    /// - [`Tween`](crate::Tween) — full control over easing and lifecycle.
1046    pub fn animate_bool(&mut self, id: &'static str, value: bool) -> f64 {
1047        let target = if value { 1.0 } else { 0.0 };
1048        self.animate_value(id, target, crate::anim::DEFAULT_ANIMATE_TICKS)
1049    }
1050
1051    /// Smoothly animate a `f64` value toward `target` over `duration_ticks`.
1052    ///
1053    /// Uses a linear-easing [`crate::Tween`] stored implicitly in the
1054    /// per-context named-state map under `id`. Returns the current
1055    /// interpolated value. On the first call the value snaps to `target`
1056    /// with no visible transition; on subsequent calls when `target`
1057    /// changes the tween is rebuilt starting from the current interpolated
1058    /// value, so retargeting mid-flight does not produce a jump.
1059    ///
1060    /// `duration_ticks == 0` snaps immediately to the new target.
1061    ///
1062    /// # Panics
1063    ///
1064    /// Panics if `id` is already bound in the named-state map to a value of a
1065    /// different type (e.g. a [`use_state_named`](Self::use_state_named) call
1066    /// reused the same id), since the stored entry then fails to downcast to
1067    /// the internal animation state:
1068    ///
1069    /// ```text
1070    /// animate_value: id {id} is already used for a different state type
1071    /// ```
1072    ///
1073    /// Pick a unique id per call site to avoid the collision.
1074    ///
1075    /// # Example
1076    /// ```ignore
1077    /// let bar_height = ui.animate_value("loading::bar", target_height, 30);
1078    /// ui.bar(bar_height);
1079    /// ```
1080    ///
1081    /// # Comparison with `Tween`
1082    /// Use this shorthand when you want zero boilerplate and linear easing
1083    /// is acceptable. For custom easing, a non-static key, or
1084    /// non-tick-based control, construct a [`crate::Tween`] explicitly via
1085    /// [`Context::use_state_named`](Self::use_state_named).
1086    ///
1087    /// # See also
1088    ///
1089    /// - [`animate_bool`](Self::animate_bool) — boolean-driven shorthand that
1090    ///   tweens between `0.0` and `1.0`.
1091    /// - [`Tween`](crate::Tween) — explicit easing and lifecycle control.
1092    pub fn animate_value(&mut self, id: &'static str, target: f64, duration_ticks: u64) -> f64 {
1093        let tick = self.tick;
1094        let entry = self
1095            .named_states
1096            .entry(id)
1097            .or_insert_with(|| Box::new(crate::anim::AnimState::new(target, tick)));
1098        let state = entry
1099            .downcast_mut::<crate::anim::AnimState>()
1100            .unwrap_or_else(|| {
1101                panic!(
1102                    "animate_value: id {:?} is already used for a different state type",
1103                    id
1104                )
1105            });
1106        state.sample(target, duration_ticks, tick)
1107    }
1108
1109    /// One-shot frame-clock timer (issue #248).
1110    ///
1111    /// Returns `true` exactly once — on the first frame at or after `dur` has
1112    /// elapsed since the first `schedule` call for `id` — and `false` on every
1113    /// other frame, both before and after. Re-arm by calling
1114    /// [`cancel`](Self::cancel) and then `schedule` again.
1115    ///
1116    /// Wall-clock based ([`std::time::Instant`] sampled once at frame start),
1117    /// so it works with the default feature set and without the `async`
1118    /// feature. Precision is bounded by the run loop's `tick_rate` (the
1119    /// deadline is observed on the next frame after it elapses), so durations
1120    /// well below the frame cadence are not meaningful.
1121    ///
1122    /// The id lives in the same per-context namespace as
1123    /// [`use_state_named`](Self::use_state_named): pick a unique key per call
1124    /// site.
1125    ///
1126    /// # Example
1127    /// ```no_run
1128    /// use std::time::Duration;
1129    ///
1130    /// slt::run(|ui: &mut slt::Context| {
1131    ///     if ui.schedule("splash::dismiss", Duration::from_millis(800)) {
1132    ///         // Runs once, ~800ms after the first frame that called this.
1133    ///         ui.text("Splash dismissed.");
1134    ///     }
1135    /// })?;
1136    /// # Ok::<_, std::io::Error>(())
1137    /// ```
1138    pub fn schedule(&mut self, id: &'static str, dur: std::time::Duration) -> bool {
1139        let now = self.frame_instant;
1140        let slot = self
1141            .scheduler
1142            .named
1143            .entry(id)
1144            .or_insert_with(|| SchedulerSlot {
1145                started: now,
1146                kind: SchedKind::Once {
1147                    deadline: now + dur,
1148                    fired: false,
1149                },
1150                touched_this_frame: false,
1151            });
1152        slot.touched_this_frame = true;
1153        match &mut slot.kind {
1154            SchedKind::Once { deadline, fired } if !*fired && now >= *deadline => {
1155                *fired = true;
1156                true
1157            }
1158            // Not yet due, already fired, or a re-used id bound to a different
1159            // timer kind: do not fire (a typo can't crash the app).
1160            _ => false,
1161        }
1162    }
1163
1164    /// Recurring frame-clock timer (issue #248).
1165    ///
1166    /// Returns the number of whole `dur` intervals that elapsed since the
1167    /// previous frame this `id` was sampled: `0` on most frames, `1` typically,
1168    /// and `> 1` if the frame loop stalled past several intervals — so no ticks
1169    /// are silently dropped. The internal clock advances by exactly the
1170    /// returned number of intervals each frame, so counts never drift.
1171    ///
1172    /// Wall-clock based and `async`-free, like [`schedule`](Self::schedule).
1173    ///
1174    /// # Example
1175    /// ```no_run
1176    /// use std::time::Duration;
1177    ///
1178    /// slt::run(|ui: &mut slt::Context| {
1179    ///     let ticks = ui.every("clock::second", Duration::from_secs(1));
1180    ///     if ticks > 0 {
1181    ///         // Advance a once-per-second animation by `ticks` steps.
1182    ///     }
1183    /// })?;
1184    /// # Ok::<_, std::io::Error>(())
1185    /// ```
1186    pub fn every(&mut self, id: &'static str, dur: std::time::Duration) -> u32 {
1187        let now = self.frame_instant;
1188        let interval = dur.max(std::time::Duration::from_nanos(1));
1189        let slot = self
1190            .scheduler
1191            .named
1192            .entry(id)
1193            .or_insert_with(|| SchedulerSlot {
1194                started: now,
1195                kind: SchedKind::Every {
1196                    interval,
1197                    last: now,
1198                },
1199                touched_this_frame: false,
1200            });
1201        slot.touched_this_frame = true;
1202        match &mut slot.kind {
1203            SchedKind::Every { interval, last } => {
1204                let elapsed = now.saturating_duration_since(*last);
1205                let fired = crate::widgets::intervals_elapsed(elapsed, *interval);
1206                if fired > 0 {
1207                    // Advance by exactly the intervals reported so counts never
1208                    // drift, even across stalled frames.
1209                    *last += *interval * fired;
1210                }
1211                fired
1212            }
1213            _ => 0,
1214        }
1215    }
1216
1217    /// Debounce timer — the typeahead / search-as-you-type primitive (#248).
1218    ///
1219    /// Each frame where `dirty == true` resets the quiet window to `dur`.
1220    /// Returns `true` exactly once on the first frame after `dur` of quiet (no
1221    /// `dirty`), then stays `false` until the next dirty frame re-arms it. This
1222    /// mirrors Textual's `@work(exclusive=True)` debounce: collapse a burst of
1223    /// keystrokes so only the final, settled query runs.
1224    ///
1225    /// Wall-clock based and `async`-free, like [`schedule`](Self::schedule).
1226    ///
1227    /// # Example
1228    /// ```no_run
1229    /// use std::time::Duration;
1230    /// use slt::TextInputState;
1231    ///
1232    /// let mut query = TextInputState::with_placeholder("Search...");
1233    /// slt::run(move |ui: &mut slt::Context| {
1234    ///     // `resp.changed` is true on the keystroke frame -> the dirty signal.
1235    ///     let resp = ui.text_input(&mut query);
1236    ///     // Fire the search only after 250ms of no typing.
1237    ///     if ui.debounce("search::run", Duration::from_millis(250), resp.changed) {
1238    ///         // run_search(&query.value());
1239    ///     }
1240    /// })?;
1241    /// # Ok::<_, std::io::Error>(())
1242    /// ```
1243    pub fn debounce(&mut self, id: &'static str, dur: std::time::Duration, dirty: bool) -> bool {
1244        let now = self.frame_instant;
1245        let slot = self
1246            .scheduler
1247            .named
1248            .entry(id)
1249            .or_insert_with(|| SchedulerSlot {
1250                started: now,
1251                kind: SchedKind::Debounce {
1252                    dur,
1253                    deadline: now + dur,
1254                    fired: false,
1255                },
1256                touched_this_frame: false,
1257            });
1258        slot.touched_this_frame = true;
1259        match &mut slot.kind {
1260            SchedKind::Debounce {
1261                dur: slot_dur,
1262                deadline,
1263                fired,
1264            } => {
1265                *slot_dur = dur;
1266                if dirty {
1267                    // Re-arm the quiet window from this frame.
1268                    *deadline = now + dur;
1269                    *fired = false;
1270                    false
1271                } else if !*fired && now >= *deadline {
1272                    *fired = true;
1273                    true
1274                } else {
1275                    false
1276                }
1277            }
1278            _ => false,
1279        }
1280    }
1281
1282    /// Exclusive-group claim — cancel stale work on supersede (issue #248).
1283    ///
1284    /// Within a `group`, only the most-recently-claimed `id` returns `true`;
1285    /// once a newer `id` claims the group, every prior `id` returns `false`
1286    /// from then on. Use it to cancel an in-flight typeahead query when a newer
1287    /// query supersedes it: pair with [`debounce`](Self::debounce) to fire the
1288    /// settled query, then guard the work with `exclusive` so only the latest
1289    /// claim proceeds.
1290    ///
1291    /// # Example
1292    /// ```no_run
1293    /// use std::time::Duration;
1294    ///
1295    /// slt::run(|ui: &mut slt::Context| {
1296    ///     let query_id = "q-42"; // e.g. a per-keystroke sequence id
1297    ///     if ui.exclusive("search", query_id) {
1298    ///         // Only the latest claimed query runs; older ones are cancelled.
1299    ///     }
1300    /// })?;
1301    /// # Ok::<_, std::io::Error>(())
1302    /// ```
1303    pub fn exclusive(&mut self, group: &'static str, id: &str) -> bool {
1304        let entry = self
1305            .scheduler
1306            .exclusive
1307            .entry(group.to_string())
1308            .or_default();
1309        if entry.winner == id {
1310            // The reigning claim re-polls itself: still the winner.
1311            return true;
1312        }
1313        if entry.retired.contains(id) {
1314            // A previously-superseded id can never win again: stale work stays
1315            // cancelled even if re-polled.
1316            return false;
1317        }
1318        // A new id supersedes the group: retire the old winner (if any) and
1319        // become the active claim.
1320        if !entry.winner.is_empty() {
1321            let old = std::mem::take(&mut entry.winner);
1322            entry.retired.insert(old);
1323        }
1324        entry.winner = id.to_string();
1325        true
1326    }
1327
1328    /// Drop the scheduler slot for `id`, re-arming it on the next
1329    /// [`schedule`](Self::schedule) / [`every`](Self::every) /
1330    /// [`debounce`](Self::debounce) call (issue #248).
1331    ///
1332    /// Accepts both `&'static str` and runtime-`String` ids: clears the slot
1333    /// from the named map and the dynamic-id map.
1334    ///
1335    /// # Example
1336    /// ```no_run
1337    /// use std::time::Duration;
1338    ///
1339    /// slt::run(|ui: &mut slt::Context| {
1340    ///     if ui.schedule("retry", Duration::from_secs(5)) {
1341    ///         // ...
1342    ///     }
1343    ///     if ui.key('r') {
1344    ///         ui.cancel("retry"); // next `schedule("retry", ..)` starts fresh
1345    ///     }
1346    /// })?;
1347    /// # Ok::<_, std::io::Error>(())
1348    /// ```
1349    pub fn cancel(&mut self, id: &str) {
1350        self.scheduler.named.remove(id);
1351        self.scheduler.keyed.remove(id);
1352    }
1353
1354    /// Wall-clock time elapsed since `id` was first scheduled, or `None` if no
1355    /// live timer slot exists for `id` (issue #248).
1356    ///
1357    /// Useful for progress UIs ("retrying in 3s…") that want the raw elapsed
1358    /// duration rather than a fire/no-fire signal. Measured against the same
1359    /// frame instant the timer methods use.
1360    ///
1361    /// # Example
1362    /// ```no_run
1363    /// use std::time::Duration;
1364    ///
1365    /// slt::run(|ui: &mut slt::Context| {
1366    ///     ui.schedule("upload", Duration::from_secs(30));
1367    ///     if let Some(elapsed) = ui.elapsed("upload") {
1368    ///         ui.text(format!("Uploading for {}s", elapsed.as_secs()));
1369    ///     }
1370    /// })?;
1371    /// # Ok::<_, std::io::Error>(())
1372    /// ```
1373    pub fn elapsed(&self, id: &str) -> Option<std::time::Duration> {
1374        let started = self
1375            .scheduler
1376            .named
1377            .get(id)
1378            .or_else(|| self.scheduler.keyed.get(id))
1379            .map(|slot| slot.started)?;
1380        Some(self.frame_instant.saturating_duration_since(started))
1381    }
1382
1383    /// Push a value onto the context stack for the duration of `body`.
1384    ///
1385    /// Inside `body`, child widgets can call
1386    /// [`use_context::<T>()`](Self::use_context) or
1387    /// [`try_use_context::<T>()`](Self::try_use_context) to look up the
1388    /// nearest provided value of type `T`. Provides cascade in LIFO order:
1389    /// nested calls with the same `T` shadow outer ones.
1390    ///
1391    /// The value is automatically popped when `body` returns — including on
1392    /// panic, so the context stack is always restored.
1393    ///
1394    /// # Example
1395    ///
1396    /// ```ignore
1397    /// struct Theme { accent: slt::Color }
1398    /// ui.provide(Theme { accent: slt::Color::Red }, |ui| {
1399    ///     // Any widget here can `let theme = ui.use_context::<Theme>();`
1400    ///     render_button(ui);
1401    /// });
1402    /// ```
1403    pub fn provide<T: 'static, R>(&mut self, value: T, body: impl FnOnce(&mut Context) -> R) -> R {
1404        self.context_stack
1405            .push(Box::new(value) as Box<dyn std::any::Any>);
1406
1407        // catch_unwind ensures the entry is popped even if `body` panics, so
1408        // the context stack is never left with leaked frames. We re-panic
1409        // afterwards so the panic propagates normally to outer scopes.
1410        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| body(self)));
1411
1412        // Pop in both success and panic paths.
1413        self.context_stack.pop();
1414
1415        match result {
1416            Ok(value) => value,
1417            Err(panic) => std::panic::resume_unwind(panic),
1418        }
1419    }
1420
1421    /// Spawn a fire-and-forget async task from inside the frame closure.
1422    ///
1423    /// Returns a [`TaskHandle<T>`](crate::TaskHandle) you store and pass to
1424    /// [`poll`](Self::poll) on later frames to retrieve the result. This closes
1425    /// the ergonomics gap of the channel pattern (`run_async` + an external
1426    /// `Sender`) for the common case: "click a button, kick off one async call,
1427    /// show its result next frame" — without wiring a channel yourself.
1428    ///
1429    /// **Dropping the returned handle cancels the in-flight task.** Keep it
1430    /// alive (e.g. in `use_state`) for as long as you care about the result.
1431    /// Each handle carries a unique id, so two `TaskHandle<String>` live at the
1432    /// same time never cross their results.
1433    ///
1434    /// Requires the `async` feature and an active Tokio runtime — call it
1435    /// inside [`run_async`](crate::run_async) /
1436    /// [`run_async_with`](crate::run_async_with), which inject the runtime
1437    /// handle.
1438    ///
1439    /// # Panics
1440    ///
1441    /// Panics if no Tokio runtime was injected (e.g. when called from the sync
1442    /// [`run`](crate::run) loop or `TestBackend` without a runtime).
1443    ///
1444    /// # Example
1445    ///
1446    /// ```no_run
1447    /// # #[cfg(feature = "async")]
1448    /// # async fn run() -> std::io::Result<()> {
1449    /// use slt::{Context, RunConfig, TaskHandle};
1450    ///
1451    /// async fn fetch() -> String {
1452    ///     // e.g. an HTTP request
1453    ///     "result".to_string()
1454    /// }
1455    ///
1456    /// slt::run_async_with(RunConfig::default(), |ui: &mut Context, _: &mut Vec<()>| {
1457    ///     // One handle, stored across frames via `use_state`.
1458    ///     let handle = ui.use_state(|| None::<TaskHandle<String>>);
1459    ///
1460    ///     if ui.button("Fetch").clicked && handle.get(ui).is_none() {
1461    ///         *handle.get_mut(ui) = Some(ui.spawn(async { fetch().await }));
1462    ///     }
1463    ///
1464    ///     // Take the handle out of state to poll it: `ui.poll` needs `&mut ui`,
1465    ///     // which cannot coexist with a `&TaskHandle` borrowed from `ui`'s own
1466    ///     // state. Put it back if the task is still pending.
1467    ///     if let Some(h) = handle.get_mut(ui).take() {
1468    ///         match ui.poll(&h) {
1469    ///             Some(result) => {
1470    ///                 ui.text(format!("Got: {result}"));
1471    ///             }
1472    ///             None => {
1473    ///                 *handle.get_mut(ui) = Some(h);
1474    ///                 ui.text("Loading...");
1475    ///             }
1476    ///         }
1477    ///     }
1478    /// })?;
1479    /// # Ok(())
1480    /// # }
1481    /// ```
1482    #[cfg(feature = "async")]
1483    #[cfg_attr(docsrs, doc(cfg(feature = "async")))]
1484    pub fn spawn<T: Send + 'static>(
1485        &mut self,
1486        fut: impl std::future::Future<Output = T> + Send + 'static,
1487    ) -> TaskHandle<T> {
1488        self.async_tasks.spawn(fut)
1489    }
1490
1491    /// Poll a [`TaskHandle`](crate::TaskHandle) for its result.
1492    ///
1493    /// Returns `Some(result)` exactly once — on the first frame after the task
1494    /// completes — then `None` on every subsequent call. Returns `None` while
1495    /// the task is still in flight.
1496    ///
1497    /// Pairs with [`spawn`](Self::spawn). Requires the `async` feature.
1498    ///
1499    /// # Example
1500    ///
1501    /// ```no_run
1502    /// # #[cfg(feature = "async")]
1503    /// # fn ex(ui: &mut slt::Context, handle: &slt::TaskHandle<u32>) {
1504    /// if let Some(value) = ui.poll(handle) {
1505    ///     ui.text(format!("done: {value}"));
1506    /// }
1507    /// # }
1508    /// ```
1509    #[cfg(feature = "async")]
1510    #[cfg_attr(docsrs, doc(cfg(feature = "async")))]
1511    pub fn poll<T: 'static>(&mut self, handle: &TaskHandle<T>) -> Option<T> {
1512        self.async_tasks.poll::<T>(handle.id())
1513    }
1514
1515    /// Look up the nearest provided value of type `T` on the context stack.
1516    ///
1517    /// Searches from the top of the stack (most-recent
1518    /// [`provide`](Self::provide)) downward. Returns the first match.
1519    ///
1520    /// # Panics
1521    ///
1522    /// Panics if no value of type `T` is currently provided. Use
1523    /// [`try_use_context`](Self::try_use_context) for a non-panicking variant.
1524    pub fn use_context<T: 'static>(&self) -> &T {
1525        self.try_use_context::<T>().unwrap_or_else(|| {
1526            panic!(
1527                "no context of type {} was provided; use ui.provide(value, |ui| ...) in a parent scope",
1528                std::any::type_name::<T>()
1529            )
1530        })
1531    }
1532
1533    /// Like [`use_context`](Self::use_context), but returns `None` instead of
1534    /// panicking when no value of type `T` is on the stack.
1535    pub fn try_use_context<T: 'static>(&self) -> Option<&T> {
1536        self.context_stack
1537            .iter()
1538            .rev()
1539            .find_map(|entry| entry.downcast_ref::<T>())
1540    }
1541
1542    /// Memoize a computed value. Recomputes only when `deps` changes.
1543    ///
1544    /// Returns a [`Memo<T>`] *index handle*, mirroring [`use_state`]'s
1545    /// [`State<T>`]. The handle holds **no** borrow of `ui`, so it composes with
1546    /// later `ui.*` calls — read the value on demand with `.get(ui)` /
1547    /// `.copied(ui)`.
1548    ///
1549    /// Before v0.21.0 this returned `&T`, a live borrow of `&mut Context` that
1550    /// could not be held across subsequent `ui.*` mutations. That form is now
1551    /// [`use_memo_ref`](Self::use_memo_ref) (deprecated). Migrate
1552    /// `let x = *ui.use_memo(&d, f);` to `let x = ui.use_memo(&d, f).copied(ui);`.
1553    ///
1554    /// [`use_state`]: Self::use_state
1555    ///
1556    /// # Panics
1557    ///
1558    /// Panics if the hook slot at this call position was previously used for a
1559    /// different hook (a rules-of-hooks / call-order violation), since the
1560    /// type-erased slot then fails to downcast to `MemoSlot<T>`:
1561    ///
1562    /// ```text
1563    /// Hook type mismatch at index {idx}: expected {type}. Hooks must be called in the same order every frame.
1564    /// ```
1565    ///
1566    /// Keep hook calls in the same order every frame — do not call this inside
1567    /// an `if`/`else` whose branch changes between frames.
1568    ///
1569    /// # Example
1570    /// ```no_run
1571    /// # slt::run(|ui: &mut slt::Context| {
1572    /// let count = ui.use_state(|| 0i32);
1573    /// let count_val = *count.get(ui);
1574    /// let doubled = ui.use_memo(&count_val, |c| c * 2);
1575    /// // The handle survives an intervening `ui.*` call (this is the whole point).
1576    /// ui.text("doubled:");
1577    /// ui.text(format!("{}", doubled.copied(ui)));
1578    /// # });
1579    /// ```
1580    pub fn use_memo<T: 'static, D: PartialEq + Clone + 'static>(
1581        &mut self,
1582        deps: &D,
1583        compute: impl FnOnce(&D) -> T,
1584    ) -> Memo<T> {
1585        let idx = self.rollback.hook_cursor;
1586        self.rollback.hook_cursor += 1;
1587
1588        // First call at this slot: allocate fresh state. Deps are stored
1589        // type-erased so the read path (`Memo::get`) can downcast `MemoSlot<T>`
1590        // without restating `D`.
1591        if idx >= self.hook_states.len() {
1592            self.hook_states.push(Box::new(MemoSlot {
1593                deps: Box::new(deps.clone()),
1594                value: compute(deps),
1595            }));
1596            return Memo::from_idx(idx);
1597        }
1598
1599        // Slot already exists: it must be the same `MemoSlot<T>` shape we used
1600        // last frame, or the caller broke the rules-of-hooks contract.
1601        match self.hook_states[idx].downcast_mut::<MemoSlot<T>>() {
1602            Some(slot) => {
1603                // Compare against the previous (type-erased) deps. A failed
1604                // downcast of the stored deps to `&D` is treated as stale so the
1605                // value is recomputed rather than silently kept.
1606                let stale = slot
1607                    .deps
1608                    .downcast_ref::<D>()
1609                    .map(|prev| *prev != *deps)
1610                    .unwrap_or(true);
1611                if stale {
1612                    slot.deps = Box::new(deps.clone());
1613                    slot.value = compute(deps);
1614                }
1615            }
1616            None => panic!(
1617                "Hook type mismatch at index {}: expected {}. Hooks must be called in the same order every frame.",
1618                idx,
1619                std::any::type_name::<MemoSlot<T>>()
1620            ),
1621        }
1622        Memo::from_idx(idx)
1623    }
1624
1625    /// Deprecated `&T`-returning form of [`use_memo`](Self::use_memo).
1626    ///
1627    /// **Deprecated since 0.21.0**: [`use_memo`](Self::use_memo) now returns a
1628    /// [`Memo<T>`] handle that does not borrow `ui`, so it composes with later
1629    /// `ui.*` calls. This alias preserves the original behaviour (returning a
1630    /// `&T` borrow of `ui`) for callers that cannot migrate immediately; the
1631    /// borrow keeps `ui` immutably borrowed until the reference is dropped.
1632    ///
1633    /// Migrate `let x = *ui.use_memo_ref(&d, f);` to
1634    /// `let x = ui.use_memo(&d, f).copied(ui);` (or `.get(ui)` for a reference).
1635    ///
1636    /// # Panics
1637    ///
1638    /// Panics if the hook slot at this call position was previously used for a
1639    /// different hook (a rules-of-hooks / call-order violation), since the
1640    /// type-erased slot then fails to downcast to `(D, T)`:
1641    ///
1642    /// ```text
1643    /// Hook type mismatch at index {idx}: expected {type}. Hooks must be called in the same order every frame.
1644    /// ```
1645    ///
1646    /// # Example
1647    /// ```no_run
1648    /// # slt::run(|ui: &mut slt::Context| {
1649    /// # #[allow(deprecated)]
1650    /// let doubled = *ui.use_memo_ref(&21i32, |c| c * 2);
1651    /// ui.text(format!("{doubled}"));
1652    /// # });
1653    /// ```
1654    #[deprecated(
1655        since = "0.21.0",
1656        note = "use_memo now returns a Memo<T> handle; call `.get(ui)` / `.copied(ui)`"
1657    )]
1658    pub fn use_memo_ref<T: 'static, D: PartialEq + Clone + 'static>(
1659        &mut self,
1660        deps: &D,
1661        compute: impl FnOnce(&D) -> T,
1662    ) -> &T {
1663        let idx = self.rollback.hook_cursor;
1664        self.rollback.hook_cursor += 1;
1665
1666        // First call at this slot: allocate fresh state.
1667        if idx >= self.hook_states.len() {
1668            let value = compute(deps);
1669            self.hook_states.push(Box::new((deps.clone(), value)));
1670            return self.hook_states[idx]
1671                .downcast_ref::<(D, T)>()
1672                .map(|(_, v)| v)
1673                .expect("freshly inserted slot must downcast to its own type");
1674        }
1675
1676        // Slot already exists: it must be the same `(D, T)` shape we used last
1677        // frame, or the caller broke the rules-of-hooks contract.
1678        //
1679        // Single downcast on the cache-hit path (closes #133): use
1680        // `downcast_mut` to update deps/value in place when they change, and
1681        // return `&stored.1` directly — eliminating the redundant second
1682        // `downcast_ref` that ran on every call regardless of cache state.
1683        match self.hook_states[idx].downcast_mut::<(D, T)>() {
1684            Some(stored) => {
1685                if stored.0 != *deps {
1686                    stored.0 = deps.clone();
1687                    stored.1 = compute(deps);
1688                }
1689                &stored.1
1690            }
1691            None => panic!(
1692                "Hook type mismatch at index {}: expected {}. Hooks must be called in the same order every frame.",
1693                idx,
1694                std::any::type_name::<(D, T)>()
1695            ),
1696        }
1697    }
1698
1699    /// Returns `light` color if current theme is light mode, `dark` color if dark mode.
1700    pub fn light_dark(&self, light: Color, dark: Color) -> Color {
1701        if self.theme.is_dark { dark } else { light }
1702    }
1703
1704    /// Show a toast notification without managing ToastState.
1705    ///
1706    /// # Examples
1707    /// ```
1708    /// # use slt::*;
1709    /// # TestBackend::new(80, 24).render(|ui| {
1710    /// ui.notify("File saved!", ToastLevel::Success);
1711    /// # });
1712    /// ```
1713    pub fn notify(&mut self, message: &str, level: ToastLevel) {
1714        let tick = self.tick;
1715        self.rollback
1716            .notification_queue
1717            .push((message.to_string(), level, tick));
1718    }
1719
1720    pub(crate) fn render_notifications(&mut self) {
1721        let tick = self.tick;
1722        self.rollback
1723            .notification_queue
1724            .retain(|(_, _, created)| tick.saturating_sub(*created) < 180);
1725        if self.rollback.notification_queue.is_empty() {
1726            return;
1727        }
1728
1729        // The `overlay` closure captures `self` mutably, so we cannot keep an
1730        // immutable borrow of `self.rollback.notification_queue` alive across
1731        // the call. Move the queue out for the render, then move it back —
1732        // no `String::clone` per notification, no intermediate `Vec` alloc.
1733        // Closes the non-empty path of #138.
1734        let queue = std::mem::take(&mut self.rollback.notification_queue);
1735        let theme = self.theme;
1736
1737        let _ = self.overlay(|ui| {
1738            let _ = ui.row(|ui| {
1739                ui.spacer();
1740                let _ = ui.col(|ui| {
1741                    for (message, level, _) in queue.iter().rev() {
1742                        let color = match level {
1743                            ToastLevel::Info => theme.primary,
1744                            ToastLevel::Success => theme.success,
1745                            ToastLevel::Warning => theme.warning,
1746                            ToastLevel::Error => theme.error,
1747                        };
1748                        let mut line = String::with_capacity(2 + message.len());
1749                        line.push_str("● ");
1750                        line.push_str(message);
1751                        ui.styled(line, Style::new().fg(color));
1752                    }
1753                });
1754            });
1755        });
1756
1757        // Restore the queue so subsequent frames can re-render until each
1758        // entry's TTL expires above.
1759        self.rollback.notification_queue = queue;
1760    }
1761
1762    // ----------------------------------------------------------------
1763    // v0.20.0 hooks: keyed state, effects, named focus, key gating
1764    // ----------------------------------------------------------------
1765
1766    /// Component-local persistent state keyed by a runtime string.
1767    ///
1768    /// Unlike [`use_state_named`](Self::use_state_named), `id` can be a
1769    /// runtime value such as `format!("row-{i}")`. The key is converted to
1770    /// `String` once per call. The hot path (key already present) performs
1771    /// **zero string allocations beyond the [`Into<String>`] conversion at
1772    /// the call site** — first looking up by `&str`, only allocating a
1773    /// fresh map key on first insert. Together: at most **one allocation
1774    /// per call, regardless of cache state**.
1775    ///
1776    /// # When to use
1777    /// - Per-item state in a dynamic list where positional [`use_state`]
1778    ///   would break if items are reordered or filtered.
1779    /// - Reusable component functions called with a runtime discriminator.
1780    ///
1781    /// # Namespace
1782    /// Keys live in a single global namespace per `Context`. Prefix them
1783    /// to avoid collisions: `format!("my_component::item-{i}")`.
1784    ///
1785    /// # Stale entries
1786    /// Removed items leak their state until the `Context` is dropped (or
1787    /// the program exits). For long-running sessions with churn, manage
1788    /// state externally via a single `Vec<T>` in [`use_state`].
1789    ///
1790    /// # Example
1791    ///
1792    /// ```ignore
1793    /// for (i, item) in items.iter().enumerate() {
1794    ///     let row_state = ui.use_state_keyed(format!("row-{i}"), || ItemState::default());
1795    ///     // ...
1796    /// }
1797    /// ```
1798    ///
1799    /// [`use_state`]: Self::use_state
1800    pub fn use_state_keyed<T: 'static>(
1801        &mut self,
1802        id: impl Into<String>,
1803        init: impl FnOnce() -> T,
1804    ) -> State<T> {
1805        let key: String = id.into();
1806        // Lookup by `&str` first to avoid cloning on the hot
1807        // (already-populated) path. Only on first insert do we clone the
1808        // key into the map; otherwise the original `key` String is the
1809        // sole allocation and is moved into `State::from_keyed`.
1810        if !self.keyed_states.contains_key(key.as_str()) {
1811            self.keyed_states.insert(key.clone(), Box::new(init()));
1812        }
1813        State::from_keyed(key)
1814    }
1815
1816    /// Like [`use_state_keyed`](Self::use_state_keyed), but uses
1817    /// [`Default::default()`] to initialize the value on first call.
1818    ///
1819    /// # Example
1820    ///
1821    /// ```ignore
1822    /// let counter = ui.use_state_keyed_default::<i32>(format!("c-{i}"));
1823    /// ```
1824    pub fn use_state_keyed_default<T: Default + 'static>(
1825        &mut self,
1826        id: impl Into<String>,
1827    ) -> State<T> {
1828        self.use_state_keyed(id, T::default)
1829    }
1830
1831    /// Run a side-effecting closure when `deps` changes.
1832    ///
1833    /// On the **first frame** the hook slot is encountered, `f` is called
1834    /// unconditionally. On **subsequent frames**, `f` is only called when
1835    /// `*deps != stored_deps`. The hook is **positional** (same ordering
1836    /// rules as [`use_state`](Self::use_state)).
1837    ///
1838    /// # Fire-and-forget semantics
1839    ///
1840    /// There is no cleanup callback. If setup resources need teardown,
1841    /// store a handle in [`use_state`](Self::use_state) and drop it on
1842    /// a later frame.
1843    ///
1844    /// # Caveat: `error_boundary` re-fire
1845    ///
1846    /// Effects placed inside an [`error_boundary`](Self::error_boundary)
1847    /// scope can re-fire when the boundary catches a panic and rolls back
1848    /// the hook slots. For non-idempotent side effects (network requests,
1849    /// payments) put the effect outside the boundary or guard with an
1850    /// idempotency key.
1851    ///
1852    /// # Panics
1853    ///
1854    /// Panics if the hook slot at this call position was previously used for a
1855    /// different hook (a rules-of-hooks / call-order violation), since the
1856    /// type-erased slot then fails to downcast to the deps type `D`:
1857    ///
1858    /// ```text
1859    /// Hook type mismatch at index {idx}: expected {type}. Hooks must be called in the same order every frame.
1860    /// ```
1861    ///
1862    /// # Common patterns
1863    ///
1864    /// ```ignore
1865    /// // Run once on first frame:
1866    /// ui.use_effect(|_| initialize_logger(), &());
1867    ///
1868    /// // Run when `selected_tab` changes:
1869    /// ui.use_effect(|tab| load_tab_data(*tab), &selected_tab);
1870    /// ```
1871    pub fn use_effect<D: PartialEq + Clone + 'static>(&mut self, f: impl FnOnce(&D), deps: &D) {
1872        let idx = self.rollback.hook_cursor;
1873        self.rollback.hook_cursor += 1;
1874
1875        if idx >= self.hook_states.len() {
1876            // First encounter: run the effect, then store the deps so we
1877            // can detect future changes.
1878            f(deps);
1879            self.hook_states.push(Box::new(deps.clone()));
1880            return;
1881        }
1882
1883        match self.hook_states[idx].downcast_mut::<D>() {
1884            Some(stored) => {
1885                if *stored != *deps {
1886                    f(deps);
1887                    *stored = deps.clone();
1888                }
1889            }
1890            None => panic!(
1891                "Hook type mismatch at index {idx}: expected {}. \
1892                 Hooks must be called in the same order every frame.",
1893                std::any::type_name::<D>()
1894            ),
1895        }
1896    }
1897
1898    /// Register a focusable slot bound to a stable string name.
1899    ///
1900    /// Returns `true` if the registered slot currently has focus, exactly
1901    /// like [`register_focusable`](Self::register_focusable) — but also
1902    /// records the `name → slot` mapping so other code can later call
1903    /// [`focus_by_name`](Self::focus_by_name) and
1904    /// [`focused_name`](Self::focused_name).
1905    ///
1906    /// # How the slot is shared with the widget that follows
1907    ///
1908    /// Every SLT widget that takes focus (`button`, `text_input`,
1909    /// `tabs`, …) internally calls `register_focusable()` to claim its
1910    /// own slot. To keep the name pointed at the **widget the user
1911    /// sees**, this call:
1912    ///
1913    /// 1. allocates a slot eagerly (so the name binding works even when
1914    ///    no widget follows — useful for tests and for custom focusable
1915    ///    regions),
1916    /// 2. records the `name → slot` mapping into the frame's
1917    ///    `focus_name_map` (first-write-wins on duplicate names within
1918    ///    a frame),
1919    /// 3. **reserves** the slot id so the next `register_focusable()`
1920    ///    on the same frame *reuses* it instead of allocating a fresh
1921    ///    slot — that's how `text_input(&mut state)` placed right after
1922    ///    inherits the name.
1923    ///
1924    /// Names are re-registered each frame; the previous frame's map is
1925    /// kept under `focus_name_map_prev` so [`focus_by_name`](Context::focus_by_name) can resolve
1926    /// a name that has already been registered.
1927    ///
1928    /// # Two valid usage shapes
1929    ///
1930    /// **Shape A — name a widget that follows immediately** (the common
1931    /// pattern; the widget reuses the reserved slot):
1932    ///
1933    /// ```ignore
1934    /// let _ = ui.register_focusable_named("search");
1935    /// let _ = ui.text_input(&mut search_state);
1936    /// // later: ui.focus_by_name("search") jumps to the text_input
1937    /// ```
1938    ///
1939    /// **Shape B — register a named focusable region with no inner
1940    /// widget** (e.g. a custom render area that handles its own keys
1941    /// when focused):
1942    ///
1943    /// ```ignore
1944    /// let focused = ui.register_focusable_named("canvas");
1945    /// if focused { /* react to keys via key_presses_when */ }
1946    /// ```
1947    pub fn register_focusable_named(&mut self, name: &str) -> bool {
1948        // Modal/overlay suppression: when a modal is active and we're not
1949        // inside it, focusables outside the modal must be invisible to
1950        // tab/click cycling. Drop the registration entirely (no slot
1951        // allocation, no name binding, no reservation leak).
1952        if (self.rollback.modal_active || self.prev_modal_active)
1953            && self.rollback.overlay_depth == 0
1954        {
1955            self.rollback.pending_focusable_id = None;
1956            return false;
1957        }
1958        // Eagerly allocate the slot — symmetric with `register_focusable`,
1959        // so the slot exists even when no widget follows.
1960        let id = self.rollback.focus_count;
1961        self.rollback.focus_count += 1;
1962        self.rollback.last_focusable_id = Some(id);
1963        self.commands.push(Command::FocusMarker(id));
1964        // First-write-wins on duplicate names within a single frame —
1965        // a second `register_focusable_named("dup")` keeps the first
1966        // slot bound to the name and orphans its own slot's name binding.
1967        self.focus_name_map.entry(name.to_string()).or_insert(id);
1968        // Reserve `id` for the very next `register_focusable()` call to
1969        // reuse, so widgets like `text_input` placed immediately after
1970        // share the named slot rather than allocating a fresh one.
1971        // Last-write-wins on the reservation: stacking two
1972        // `register_focusable_named` calls without an intervening widget
1973        // leaves the second slot reserved (the first slot stays bound to
1974        // its name in `focus_name_map`, just without a widget attached).
1975        self.rollback.pending_focusable_id = Some(id);
1976        // Same focus-index prediction as `register_focusable`.
1977        if self.prev_modal_active
1978            && self.prev_modal_focus_count > 0
1979            && self.rollback.modal_active
1980            && self.rollback.overlay_depth > 0
1981        {
1982            let mut modal_local_id = id.saturating_sub(self.rollback.modal_focus_start);
1983            modal_local_id %= self.prev_modal_focus_count;
1984            let mut modal_focus_idx = self.focus_index.saturating_sub(self.prev_modal_focus_start);
1985            modal_focus_idx %= self.prev_modal_focus_count;
1986            return modal_local_id == modal_focus_idx;
1987        }
1988        if self.prev_focus_count == 0 {
1989            return true;
1990        }
1991        self.focus_index % self.prev_focus_count == id
1992    }
1993
1994    /// Request focus on the named widget.
1995    ///
1996    /// If the named widget was registered last frame the focus change
1997    /// takes effect at the **start of the next frame** (one-frame delay
1998    /// is the deferred-command pattern used throughout SLT). If the name
1999    /// has never been registered, the request stays pending: the next
2000    /// frame to register that name receives focus.
2001    ///
2002    /// Returns `true` if the call **will** resolve — i.e. the name was
2003    /// either registered earlier in this frame (via
2004    /// [`register_focusable_named`](Self::register_focusable_named)) or in
2005    /// the previous frame. Returns `false` only when the name has not been
2006    /// seen by either frame, in which case the request stays pending until
2007    /// some future frame registers the name.
2008    ///
2009    /// # Example
2010    ///
2011    /// ```ignore
2012    /// if ui.button("Find").clicked {
2013    ///     ui.focus_by_name("search");
2014    /// }
2015    /// ```
2016    pub fn focus_by_name(&mut self, name: &str) -> bool {
2017        // Resolve against either the previous frame's settled map or the
2018        // in-progress map being built right now. The latter handles the
2019        // common "register, then focus_by_name in the same frame" pattern
2020        // that callers naturally expect to return `true`.
2021        //
2022        // The actual focus change still lands at the start of the next
2023        // frame via `focus_name_map_prev` lookup in `Context::new`. The
2024        // return value is purely about resolvability: "true" means the name
2025        // is known and the focus shift will land next frame; "false" means
2026        // the request is pending a future registration.
2027        let resolved =
2028            self.focus_name_map_prev.contains_key(name) || self.focus_name_map.contains_key(name);
2029        // Always store the request — even if it resolved this frame, the
2030        // next-frame plumbing (`Context::new`) is what actually applies
2031        // the index. We use take/replace so the caller cannot stack two
2032        // pending names; the most recent wins.
2033        self.pending_focus_name = Some(name.to_string());
2034        resolved
2035    }
2036
2037    /// Return the name of the currently focused widget, if it was
2038    /// registered with
2039    /// [`register_focusable_named`](Self::register_focusable_named) this
2040    /// frame.
2041    ///
2042    /// Returns `None` if the focused widget used the unnamed
2043    /// [`register_focusable`](Self::register_focusable) API or if no widget
2044    /// has focus.
2045    pub fn focused_name(&self) -> Option<&str> {
2046        // Search this frame's map for the entry whose index equals
2047        // `focus_index`. The map is small (one entry per named focusable),
2048        // so a linear scan is fine — typical apps register <50 names.
2049        self.focus_name_map
2050            .iter()
2051            .find_map(|(name, &idx)| (idx == self.focus_index).then_some(name.as_str()))
2052    }
2053
2054    /// Iterate unconsumed key-press events, gated on `active`.
2055    ///
2056    /// When `active` is `false`, returns an empty iterator. When `active`
2057    /// is `true`, behaves identically to the internal
2058    /// `available_key_presses`. The returned indices are valid for
2059    /// [`consume_event`](Self::consume_event).
2060    ///
2061    /// This is the **preferred pattern** for focus-gated keyboard handling
2062    /// in custom widgets. Because the iterator borrows `self.events`
2063    /// immutably, collect the indices first and consume them after the
2064    /// loop:
2065    ///
2066    /// ```ignore
2067    /// let focused = ui.register_focusable();
2068    /// let mut hits: Vec<usize> = Vec::new();
2069    /// for (i, key) in ui.key_presses_when(focused) {
2070    ///     if key.code == slt::KeyCode::Enter {
2071    ///         hits.push(i);
2072    ///         // ... handle Enter ...
2073    ///     }
2074    /// }
2075    /// for i in hits { ui.consume_event(i); }
2076    /// ```
2077    pub fn key_presses_when(
2078        &self,
2079        active: bool,
2080    ) -> impl Iterator<Item = (usize, &crate::event::KeyEvent)> + '_ {
2081        // The `!active` short-circuit at the head of the predicate yields
2082        // an empty iterator at zero allocation cost when the widget isn't
2083        // focused. Indices are still drawn from `self.events` so callers
2084        // can pass them straight to `consume_event`.
2085        self.events
2086            .iter()
2087            .enumerate()
2088            .filter_map(move |(i, event)| {
2089                if !active {
2090                    return None;
2091                }
2092                if self.consumed.get(i).copied().unwrap_or(true) {
2093                    return None;
2094                }
2095                match event {
2096                    Event::Key(key) if key.kind == KeyEventKind::Press => Some((i, key)),
2097                    _ => None,
2098                }
2099            })
2100    }
2101
2102    /// Mark the event at `index` as consumed.
2103    ///
2104    /// Public counterpart to the crate-internal `consume_indices`. Use
2105    /// this in custom widgets after handling an event yielded by
2106    /// [`key_presses_when`](Self::key_presses_when) so subsequent widgets
2107    /// don't react to the same key. Out-of-range indices are silently
2108    /// ignored (matching the iterator-pair semantics).
2109    pub fn consume_event(&mut self, index: usize) {
2110        if let Some(slot) = self.consumed.get_mut(index) {
2111            *slot = true;
2112        }
2113    }
2114
2115    // ── Issue #233: in-frame static-log append ───────────────────────────
2116    //
2117    // The runtime holds the buffer inside `named_states` under a reserved
2118    // sentinel key. `Context::new` (owned by another agent) does not need to
2119    // initialise this field — `or_insert_with` handles first-call creation,
2120    // and `lib::run_frame_kernel` drains the buffer back into `FrameState`
2121    // for the run-loop to consume.
2122
2123    /// Append a line that will be flushed to terminal scrollback **before**
2124    /// the dynamic frame content (issue #233).
2125    ///
2126    /// Lines accumulated this frame are written via the active runtime — for
2127    /// [`crate::run_static`] / [`crate::run_static_with`], they are printed
2128    /// above the inline dynamic area as committed scrollback. For full-screen
2129    /// runtimes ([`crate::run`], [`crate::run_async`]) and inline mode
2130    /// ([`crate::run_inline`]), the buffer is silently dropped after a debug
2131    /// warning is emitted on the first call per frame, since those modes have
2132    /// no scrollback area to write to.
2133    ///
2134    /// The headless [`crate::TestBackend`] accumulates the lines into the
2135    /// frame state where they can be drained by tests via
2136    /// [`Context::take_static_log`] (or by inspecting the buffer when
2137    /// constructing a custom backend).
2138    ///
2139    /// # Order
2140    ///
2141    /// `static_log` may be called any number of times per frame. Lines are
2142    /// flushed in call order, all before the dynamic frame for the same
2143    /// tick.
2144    ///
2145    /// # Example
2146    ///
2147    /// ```
2148    /// # use slt::*;
2149    /// # TestBackend::new(40, 4).render(|ui| {
2150    /// ui.static_log("event 1");
2151    /// ui.static_log(format!("event {}", 2));
2152    /// ui.text("dynamic content");
2153    /// # });
2154    /// ```
2155    pub fn static_log(&mut self, line: impl Into<String>) {
2156        let entry = self
2157            .named_states
2158            .entry(STATIC_LOG_KEY)
2159            .or_insert_with(|| Box::new(Vec::<String>::new()) as Box<dyn std::any::Any>);
2160        if let Some(buf) = entry.downcast_mut::<Vec<String>>() {
2161            buf.push(line.into());
2162        }
2163    }
2164
2165    /// Drain and return the queued static-log lines for the current frame
2166    /// (issue #233). Used by tests / external backends to inspect what
2167    /// `ui.static_log(...)` emitted during a [`crate::TestBackend::render`]
2168    /// call.
2169    pub fn take_static_log(&mut self) -> Vec<String> {
2170        if let Some(boxed) = self.named_states.get_mut(STATIC_LOG_KEY)
2171            && let Some(buf) = boxed.downcast_mut::<Vec<String>>()
2172        {
2173            return std::mem::take(buf);
2174        }
2175        Vec::new()
2176    }
2177
2178    // ── Issue #236: widget keymap publishing ─────────────────────────────
2179
2180    /// Publish a widget's keymap so the framework can show it in the help
2181    /// overlay (issue #236).
2182    ///
2183    /// Each call registers `(name, bindings)` for the current frame. Widgets
2184    /// implementing [`crate::keymap::WidgetKeyHelp`] typically forward their
2185    /// `key_help()` slice here:
2186    ///
2187    /// ```
2188    /// # use slt::*;
2189    /// # use slt::keymap::WidgetKeyHelp;
2190    /// struct Counter;
2191    /// impl WidgetKeyHelp for Counter {
2192    ///     fn key_help(&self) -> &'static [(&'static str, &'static str)] {
2193    ///         const HELP: &[(&str, &str)] = &[("↑", "increment"), ("↓", "decrement")];
2194    ///         HELP
2195    ///     }
2196    /// }
2197    /// # TestBackend::new(40, 4).render(|ui| {
2198    /// let counter = Counter;
2199    /// ui.publish_keymap("counter", counter.key_help());
2200    /// # });
2201    /// ```
2202    ///
2203    /// The registry is reset at the start of every frame (the first call on a
2204    /// new tick clears stale entries). Both calls in the same frame
2205    /// accumulate; calls across frames do not leak.
2206    pub fn publish_keymap(
2207        &mut self,
2208        name: &'static str,
2209        bindings: &'static [(&'static str, &'static str)],
2210    ) {
2211        // The registry is cleared at frame start by `run_frame_kernel`
2212        // (issue #236) — see `clear_keymap_registry` in `lib.rs`. We just
2213        // need to insert/append here.
2214        let entry = self
2215            .named_states
2216            .entry(KEYMAP_REGISTRY_KEY)
2217            .or_insert_with(|| {
2218                Box::new(Vec::<crate::keymap::PublishedKeymap>::new()) as Box<dyn std::any::Any>
2219            });
2220        if let Some(vec) = entry.downcast_mut::<Vec<crate::keymap::PublishedKeymap>>() {
2221            vec.push(crate::keymap::PublishedKeymap::new(name, bindings));
2222        }
2223    }
2224
2225    /// Return all keymaps published this frame (issue #236).
2226    ///
2227    /// Empty if no widget called [`Context::publish_keymap`] yet on the
2228    /// current frame. The registry is reset at the start of every frame.
2229    pub fn published_keymaps(&self) -> &[crate::keymap::PublishedKeymap] {
2230        if let Some(boxed) = self.named_states.get(KEYMAP_REGISTRY_KEY)
2231            && let Some(vec) = boxed.downcast_ref::<Vec<crate::keymap::PublishedKeymap>>()
2232        {
2233            return vec;
2234        }
2235        &[]
2236    }
2237
2238    /// Render an automatic keymap-help overlay listing every widget keymap
2239    /// published this frame (issue #236).
2240    ///
2241    /// Pass `open = true` to render the overlay (typically gated on a
2242    /// `?` / `F1` keypress). When `open` is `false`, this method is a
2243    /// no-op. The overlay groups bindings by widget name and dismisses
2244    /// when the next frame is rendered with `open = false`.
2245    ///
2246    /// # Example
2247    ///
2248    /// ```
2249    /// # use slt::*;
2250    /// # TestBackend::new(40, 12).render(|ui| {
2251    /// const RICHLOG: &[(&str, &str)] = &[("↑/k", "scroll up"), ("↓/j", "scroll down")];
2252    /// ui.publish_keymap("rich_log", RICHLOG);
2253    /// // Show the help overlay when '?' is pressed
2254    /// let show = ui.key('?');
2255    /// ui.keymap_help_overlay(show);
2256    /// # });
2257    /// ```
2258    pub fn keymap_help_overlay(&mut self, open: bool) {
2259        if !open {
2260            return;
2261        }
2262
2263        let entries: Vec<crate::keymap::PublishedKeymap> = self.published_keymaps().to_vec();
2264        if entries.is_empty() {
2265            return;
2266        }
2267
2268        let theme = self.theme;
2269        let _ = self.modal(|ui| {
2270            ui.styled("Keyboard shortcuts", Style::new().bold().fg(theme.primary));
2271            ui.text("");
2272            for entry in &entries {
2273                ui.styled(entry.name, Style::new().bold().fg(theme.text));
2274                for (key, desc) in entry.bindings {
2275                    let line = format!("  {key:<14}  {desc}");
2276                    ui.styled(line, Style::new().fg(theme.text_dim));
2277                }
2278                ui.text("");
2279            }
2280            ui.styled(
2281                "Press Esc / ? to close",
2282                Style::new().fg(theme.text_dim).italic(),
2283            );
2284        });
2285    }
2286}
2287
2288// Sentinel keys reused from `lib.rs` so the two reads/writes can never drift.
2289use crate::{
2290    KEYMAP_REGISTRY_NAMED_STATE_KEY as KEYMAP_REGISTRY_KEY,
2291    STATIC_LOG_NAMED_STATE_KEY as STATIC_LOG_KEY,
2292};