tui-lipan 0.2.0

Opinionated, component-based TUI framework for Rust - declarative components, reconciliation, layout engine, focus, overlays, and rich widgets on top of ratatui.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Arc;
use std::time::Duration;
use web_time::Instant;

use crate::animation::{Easing, Transition};
use crate::callback::Callback;
use crate::core::element::Element;
use crate::core::node::{NodeId, WidgetNode};
use crate::style::{Length, Padding, Style};
use crate::widgets::{Toast, ToastCopyAffordance};

/// Controls whether overlay-like widget content renders at the root or inline.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum OverlayScope {
    /// Render at root level using the overlay pipeline.
    #[default]
    RootPortal,
    /// Render inline at the declaration location inside the normal tree.
    Local,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
/// Unique identifier for an overlay entry.
pub struct OverlayId(u64);

impl OverlayId {
    pub(crate) fn value(self) -> u64 {
        self.0
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum OverlayLayer {
    Modal = 0,
    Popover = 1,
    Toast = 2,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub(crate) enum PointerCapture {
    #[default]
    None,
    RectOnly,
    BackdropFullScreen,
}

/// Toast positioning on screen.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ToastPlacement {
    /// Top-left corner.
    TopStart,
    /// Top-center.
    TopCenter,
    /// Top-right corner.
    TopEnd,
    /// Bottom-left corner.
    BottomStart,
    /// Bottom-center.
    BottomCenter,
    /// Bottom-right corner (default).
    #[default]
    BottomEnd,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum DismissPolicy {
    None,
    #[default]
    ClickOutside,
    ClickInside,
    ClickOutsideOrEscape,
}

impl DismissPolicy {
    pub(crate) fn dismiss_on_click_inside(self) -> bool {
        matches!(self, Self::ClickInside)
    }

    pub(crate) fn dismiss_on_click_outside(self) -> bool {
        matches!(self, Self::ClickOutside | Self::ClickOutsideOrEscape)
    }

    pub(crate) fn dismiss_on_escape(self) -> bool {
        matches!(self, Self::ClickOutsideOrEscape)
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum OverlayPlacement {
    Center {
        /// Height to reserve when centering vertically, instead of the content's own height.
        /// The content is top-aligned within that reserved band, so its top edge stays fixed
        /// as it grows and shrinks (e.g. a filtering command palette). Content taller than the
        /// band keeps the same top edge and extends past the band's bottom; `max_height` is
        /// what bounds it. Without this, the overlay centers by its actual height and a
        /// shrinking modal drifts toward the middle.
        reserve_height: Option<Length>,
    },
    Stacked {
        placement: ToastPlacement,
        gap: u16,
        margin: Padding,
    },
}

#[derive(Clone)]
pub(crate) struct Portal {
    pub(crate) layer: OverlayLayer,
    pub(crate) content: Box<Element>,
    pub(crate) placement: OverlayPlacement,
    pub(crate) dismiss_policy: DismissPolicy,
    pub(crate) on_close: Option<Callback<()>>,
    pub(crate) backdrop: Option<Style>,
    pub(crate) captures_focus: bool,
    pub(crate) auto_focus: bool,
    pub(crate) captures_pointer: PointerCapture,
}

#[derive(Clone)]
pub(crate) struct PortalNode {
    pub(crate) content: Box<NodeId>,
}

impl WidgetNode for PortalNode {}

impl crate::layout::hash::LayoutHash for Portal {
    fn layout_hash(
        &self,
        hasher: &mut impl std::hash::Hasher,
        recurse: &dyn Fn(&Element) -> Option<u64>,
    ) -> Option<()> {
        use std::hash::Hash;
        self.layer.hash(hasher);
        self.captures_focus.hash(hasher);
        self.auto_focus.hash(hasher);
        self.captures_pointer.hash(hasher);
        recurse(self.content.as_ref())?.hash(hasher);
        Some(())
    }
}

#[derive(Clone)]
pub(crate) struct OverlayEntry {
    pub(crate) id: OverlayId,
    pub(crate) order: u64,
    pub(crate) layer: OverlayLayer,
    pub(crate) content: Element,
    pub(crate) placement: OverlayPlacement,
    pub(crate) dismiss_policy: DismissPolicy,
    pub(crate) on_dismiss: Option<Callback<()>>,
    pub(crate) created_at: Instant,
    pub(crate) timeout: Option<Duration>,
    pub(crate) captures_focus: bool,
    pub(crate) auto_focus: bool,
    pub(crate) backdrop: Option<Style>,
    pub(crate) captures_pointer: PointerCapture,
    pub(crate) opacity_transition: Option<Transition<f32>>,
    transition_tick_at: Option<Instant>,
    pub(crate) pending_dismiss: bool,
    pub(crate) copy_text: Option<Arc<str>>,
    pub(crate) copy_zone_right_padding: Option<u16>,
    pub(crate) copy_feedback_until: Option<Instant>,
    hover_remaining: Option<Duration>,
}

impl OverlayEntry {
    pub(crate) fn opacity(&self) -> f32 {
        self.opacity_transition
            .as_ref()
            .map(Transition::current)
            .unwrap_or(if self.pending_dismiss { 0.0 } else { 1.0 })
            .clamp(0.0, 1.0)
    }

    pub(crate) fn copy_feedback_active(&self) -> bool {
        self.copy_feedback_until
            .is_some_and(|deadline| Instant::now() < deadline)
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct TickResult {
    pub(crate) dirty: bool,
    pub(crate) has_active_transitions: bool,
}

pub(crate) struct OverlayManager {
    entries: Vec<OverlayEntry>,
    next_id: u64,
    generation: u64,
    inline_mode: bool,
    toast_placement: ToastPlacement,
    toast_gap: u16,
    toast_margin: Padding,
}

impl OverlayManager {
    pub(crate) fn new() -> Self {
        Self {
            entries: Vec::new(),
            next_id: 0,
            generation: 0,
            inline_mode: false,
            toast_placement: ToastPlacement::BottomEnd,
            toast_gap: 1,
            toast_margin: Padding::BORDER,
        }
    }

    fn enter_transition() -> Transition<f32> {
        Transition::new(0.0, 1.0, Duration::from_millis(150), Easing::EaseOutQuad)
    }

    fn enter_transition_from(from: f32) -> Transition<f32> {
        Transition::new(
            from.clamp(0.0, 1.0),
            1.0,
            Duration::from_millis(150),
            Easing::EaseOutQuad,
        )
    }

    fn exit_transition(from: f32) -> Transition<f32> {
        Transition::new(
            from.clamp(0.0, 1.0),
            0.0,
            Duration::from_millis(100),
            Easing::EaseInQuad,
        )
    }

    fn begin_dismiss(entry: &mut OverlayEntry, now: Instant) -> bool {
        if entry.pending_dismiss {
            return false;
        }
        let opacity = entry.opacity();
        entry.pending_dismiss = true;
        entry.opacity_transition = Some(Self::exit_transition(opacity));
        entry.transition_tick_at = Some(now);
        true
    }

    /// Monotonically increasing counter bumped on every mutation to `entries`.
    /// Callers can cache a clone of the entries and skip re-cloning while the
    /// generation stays the same.
    pub(crate) fn generation(&self) -> u64 {
        self.generation
    }

    fn bump_generation(&mut self) {
        self.generation = self.generation.wrapping_add(1);
    }

    fn allocate_id(&mut self) -> OverlayId {
        let id = OverlayId(self.next_id);
        self.next_id = self.next_id.wrapping_add(1);
        id
    }

    pub(crate) fn set_inline_mode(&mut self, inline_mode: bool) {
        self.inline_mode = inline_mode;
        if inline_mode && !self.entries.is_empty() {
            self.entries.clear();
            self.bump_generation();
        }
    }

    pub(crate) fn entries(&self) -> &[OverlayEntry] {
        &self.entries
    }

    pub(crate) fn has_active_transitions(&self) -> bool {
        self.entries
            .iter()
            .any(|entry| entry.opacity_transition.is_some())
    }

    pub(crate) fn push(&mut self, mut entry: OverlayEntry) -> OverlayId {
        let id = self.allocate_id();
        if self.inline_mode {
            return id;
        }
        entry.id = id;
        entry.order = id.value();
        entry.created_at = Instant::now();
        entry.pending_dismiss = false;
        entry.opacity_transition = Some(Self::enter_transition());
        entry.transition_tick_at = Some(entry.created_at);
        self.entries.push(entry);
        self.bump_generation();
        id
    }

    pub(crate) fn dismiss(&mut self, id: OverlayId) -> bool {
        if let Some(entry) = self.entries.iter_mut().find(|entry| entry.id == id) {
            let changed = Self::begin_dismiss(entry, Instant::now());
            if changed {
                self.bump_generation();
            }
            return true;
        }
        false
    }

    pub(crate) fn dismiss_immediately(&mut self, id: OverlayId) -> bool {
        let Some(index) = self.entries.iter().position(|entry| entry.id == id) else {
            return false;
        };
        let mut entry = self.entries.remove(index);
        if let Some(callback) = entry.on_dismiss.take() {
            callback.emit(());
        }
        self.bump_generation();
        true
    }

    /// Restart an entry's dismissal countdown in place, leaving everything the renderer reads
    /// untouched: no new `order`, no replayed enter transition, and no generation bump.
    ///
    /// `created_at` feeds only the expiry check in [`Self::tick`], which reads `self.entries`
    /// directly, so a cached overlay snapshot holding the previous value stays correct to render
    /// from. That is what makes a renew free: the toast looks identical, it just lives longer.
    ///
    /// Returns `false` when the toast is gone or already fading, since neither can be extended -
    /// callers that still want it on screen must push a fresh one.
    pub(crate) fn renew(&mut self, id: OverlayId) -> bool {
        let Some(entry) = self.entries.iter_mut().find(|entry| entry.id == id) else {
            return false;
        };
        if entry.pending_dismiss {
            return false;
        }
        entry.created_at = Instant::now();
        if entry.hover_remaining.is_some() {
            entry.hover_remaining = entry.timeout;
        }
        true
    }

    pub(crate) fn set_hovered_toast(&mut self, hovered: Option<OverlayId>) -> bool {
        let now = Instant::now();
        let mut dirty = false;

        for entry in &mut self.entries {
            if entry.layer != OverlayLayer::Toast {
                continue;
            }

            let should_hover = Some(entry.id) == hovered;
            if should_hover && entry.hover_remaining.is_none() {
                let remaining = if entry.pending_dismiss {
                    // A toast caught during its exit gets a short grace period after the pointer
                    // leaves instead of disappearing immediately or restarting its full lifetime.
                    entry
                        .timeout
                        .map(|timeout| timeout.min(Duration::from_secs(1)))
                } else {
                    entry
                        .timeout
                        .map(|timeout| timeout.saturating_sub(now.duration_since(entry.created_at)))
                };
                entry.hover_remaining = remaining;

                if entry.pending_dismiss {
                    let opacity = entry.opacity();
                    entry.pending_dismiss = false;
                    entry.opacity_transition = Some(Self::enter_transition_from(opacity));
                    entry.transition_tick_at = Some(now);
                    dirty = true;
                }
            } else if !should_hover
                && let Some(remaining) = entry.hover_remaining.take()
                && let Some(timeout) = entry.timeout
            {
                entry.created_at = now
                    .checked_sub(timeout.saturating_sub(remaining))
                    .unwrap_or(now);
            }
        }

        if dirty {
            self.bump_generation();
        }
        dirty
    }

    pub(crate) fn tick_at(&mut self, now: Instant) -> TickResult {
        let mut result = TickResult::default();

        self.entries.retain_mut(|entry| {
            if !entry.pending_dismiss && entry.hover_remaining.is_none() {
                let expired = entry
                    .timeout
                    .map(|timeout| now.duration_since(entry.created_at) >= timeout)
                    .unwrap_or(false);
                if expired {
                    result.dirty |= Self::begin_dismiss(entry, now);
                }
            }

            if let Some(deadline) = entry.copy_feedback_until {
                if now >= deadline {
                    entry.copy_feedback_until = None;
                    result.dirty = true;
                } else {
                    result.has_active_transitions = true;
                }
            }

            if let Some(transition) = entry.opacity_transition.as_mut() {
                let delta = entry
                    .transition_tick_at
                    .replace(now)
                    .map_or(Duration::ZERO, |last| now.saturating_duration_since(last));
                let before = transition.current();
                let complete = transition.tick(delta);
                let after = transition.current();
                if (after - before).abs() > f32::EPSILON {
                    result.dirty = true;
                }

                if complete {
                    if entry.pending_dismiss {
                        if let Some(cb) = entry.on_dismiss.take() {
                            cb.emit(());
                        }
                        result.dirty = true;
                        return false;
                    }
                    entry.opacity_transition = None;
                    entry.transition_tick_at = None;
                } else {
                    result.has_active_transitions = true;
                }
            }

            true
        });

        if self.has_active_transitions() {
            result.has_active_transitions = true;
        }

        if result.dirty {
            self.bump_generation();
        }
        result
    }

    pub(crate) fn trigger_copy_feedback(&mut self, id: OverlayId, duration: Duration) -> bool {
        if duration.is_zero() {
            return false;
        }
        let Some(entry) = self.entries.iter_mut().find(|entry| entry.id == id) else {
            return false;
        };
        if entry.pending_dismiss || entry.copy_text.is_none() {
            return false;
        }
        entry.copy_feedback_until = Some(Instant::now() + duration);
        self.bump_generation();
        true
    }

    pub(crate) fn set_toast_placement(&mut self, placement: ToastPlacement) {
        self.toast_placement = placement;
    }

    pub(crate) fn set_toast_gap(&mut self, gap: u16) {
        self.toast_gap = gap;
    }

    pub(crate) fn set_toast_margin(&mut self, margin: Padding) {
        self.toast_margin = margin;
    }

    pub(crate) fn toast_config(&self) -> (ToastPlacement, u16, Padding) {
        (self.toast_placement, self.toast_gap, self.toast_margin)
    }

    pub(crate) fn dismiss_toasts(&mut self) {
        let mut changed = false;
        let now = Instant::now();
        for entry in &mut self.entries {
            if entry.layer == OverlayLayer::Toast {
                changed |= Self::begin_dismiss(entry, now);
            }
        }
        if changed {
            self.bump_generation();
        }
    }

    pub(crate) fn push_toast(&mut self, toast: Toast) -> OverlayId {
        if self.inline_mode {
            crate::debug::internal_log!("[tui-lipan] inline mode: toast suppressed");
            return self.allocate_id();
        }

        let (placement, gap, margin) = self.toast_config();
        let duration = toast.duration;
        let copy_text = toast.copyable.then(|| toast.message.clone());
        let copy_zone_right_padding = if toast.copyable
            && toast.border
            && matches!(toast.copy_affordance, ToastCopyAffordance::BorderGlyph)
        {
            Some(toast.header_padding.right)
        } else {
            None
        };
        let dismiss_policy = if toast.dismiss_on_click {
            DismissPolicy::ClickInside
        } else {
            DismissPolicy::None
        };
        let content = toast.into_element();
        let entry = OverlayEntry {
            id: OverlayId(0),
            order: 0,
            layer: OverlayLayer::Toast,
            content,
            placement: OverlayPlacement::Stacked {
                placement,
                gap,
                margin,
            },
            dismiss_policy,
            on_dismiss: None,
            created_at: Instant::now(),
            timeout: Some(Duration::from_secs_f64(duration)),
            captures_focus: false,
            auto_focus: false,
            backdrop: None,
            captures_pointer: PointerCapture::None,
            opacity_transition: None,
            transition_tick_at: None,
            pending_dismiss: false,
            copy_text,
            copy_zone_right_padding,
            copy_feedback_until: None,
            hover_remaining: None,
        };
        self.push(entry)
    }
}

pub(crate) fn hovered_toast(
    tree: &crate::core::node::NodeTree,
    x: u16,
    y: u16,
) -> Option<OverlayId> {
    tree.overlay_roots().iter().rev().find_map(|root| {
        (root.layer == OverlayLayer::Toast
            && tree.is_valid(root.id)
            && tree.node(root.id).rect.contains(x as i16, y as i16))
        .then_some(root.overlay_id)
        .flatten()
    })
}

/// Handle for showing toast notifications via `ctx.toast()`.
#[derive(Clone)]
pub struct ToastHandle {
    manager: Rc<RefCell<OverlayManager>>,
}

impl ToastHandle {
    pub(crate) fn new(manager: Rc<RefCell<OverlayManager>>) -> Self {
        Self { manager }
    }

    /// Push a toast and return its ID.
    pub fn push(&self, toast: Toast) -> OverlayId {
        self.manager.borrow_mut().push_toast(toast)
    }

    /// Dismiss a specific toast by ID.
    pub fn dismiss(&self, id: OverlayId) {
        let _ = self.manager.borrow_mut().dismiss(id);
    }

    /// Dismiss a specific toast synchronously, without its exit transition.
    ///
    /// Use this when replacing an existing toast in place would otherwise leave the fading toast
    /// visible beside its replacement.
    pub fn dismiss_immediately(&self, id: OverlayId) {
        let _ = self.manager.borrow_mut().dismiss_immediately(id);
    }

    /// Restart a toast's countdown without redrawing it, returning whether it was still alive.
    ///
    /// Use this instead of dismiss-and-push when the replacement toast would render identically:
    /// pushing assigns a fresh order and replays the enter transition, so an unchanged message
    /// visibly blinks and can jump past its neighbors. A renew keeps it exactly where it is.
    ///
    /// `false` means the toast expired or is already fading, so the caller should [`Self::push`]
    /// a new one.
    pub fn renew(&self, id: OverlayId) -> bool {
        self.manager.borrow_mut().renew(id)
    }

    /// Clear all active toasts.
    pub fn clear(&self) {
        self.manager.borrow_mut().dismiss_toasts();
    }
}

#[cfg(test)]
mod tests {
    use std::thread;

    use super::*;

    #[test]
    fn toast_copy_feedback_marks_entry_active() {
        let mut manager = OverlayManager::new();
        let id = manager.push_toast(Toast::new("copy me").copyable(true));

        assert!(manager.trigger_copy_feedback(id, Duration::from_millis(150)));
        assert!(manager.entries[0].copy_feedback_active());
    }

    #[test]
    fn toast_copy_feedback_tick_clears_expired_flash() {
        let mut manager = OverlayManager::new();
        let id = manager.push_toast(Toast::new("copy me").copyable(true));

        assert!(manager.trigger_copy_feedback(id, Duration::from_millis(1)));
        thread::sleep(Duration::from_millis(5));
        let tick = manager.tick_at(Instant::now());

        assert!(tick.dirty);
        assert!(!manager.entries[0].copy_feedback_active());
        assert!(manager.entries[0].copy_feedback_until.is_none());
    }

    #[test]
    fn renew_extends_the_countdown_without_disturbing_the_render() {
        let mut manager = OverlayManager::new();
        let id = manager.push_toast(Toast::new("message").duration(10.0));
        settle_entry_transition(&mut manager.entries[0]);
        let order = manager.entries[0].order;
        let generation = manager.generation();
        let created_at = manager.entries[0].created_at;

        thread::sleep(Duration::from_millis(5));
        assert!(manager.renew(id));

        assert!(manager.entries[0].created_at > created_at);
        // Everything the renderer reads must be untouched, and the cached overlay snapshot must
        // stay valid - a renew that bumped the generation would force a pointless re-clone.
        assert_eq!(manager.entries[0].order, order);
        assert_eq!(manager.generation(), generation);
        assert!(manager.entries[0].opacity_transition.is_none());
    }

    #[test]
    fn renew_keeps_a_toast_alive_past_its_original_timeout() {
        let mut manager = OverlayManager::new();
        let id = manager.push_toast(Toast::new("held").duration(0.01));

        thread::sleep(Duration::from_millis(5));
        assert!(manager.renew(id));
        thread::sleep(Duration::from_millis(7));
        manager.tick_at(Instant::now());

        assert!(
            !manager.entries[0].pending_dismiss,
            "the renewed deadline has not passed yet"
        );
    }

    #[test]
    fn renew_reports_failure_for_a_toast_that_cannot_be_extended() {
        let mut manager = OverlayManager::new();
        let missing = manager.push_toast(Toast::new("gone"));
        assert!(manager.dismiss_immediately(missing));
        assert!(
            !manager.renew(missing),
            "an expired toast cannot be renewed"
        );

        let fading = manager.push_toast(Toast::new("fading"));
        assert!(manager.dismiss(fading));
        assert!(
            !manager.renew(fading),
            "a toast mid-exit cannot be pulled back"
        );
    }

    #[test]
    fn renew_does_not_reorder_a_toast_among_its_neighbors() {
        let mut manager = OverlayManager::new();
        let first = manager.push_toast(Toast::new("first"));
        let second = manager.push_toast(Toast::new("second"));

        assert!(manager.renew(first));

        let ids: Vec<_> = manager.entries.iter().map(|entry| entry.id).collect();
        assert_eq!(ids, vec![first, second]);
    }

    #[test]
    fn hovering_pauses_and_then_resumes_the_remaining_timeout() {
        let mut manager = OverlayManager::new();
        let id = manager.push_toast(Toast::new("held").duration(3.0));
        settle_entry_transition(&mut manager.entries[0]);
        age_entry_timeout(&mut manager.entries[0], Duration::from_millis(2_500));

        assert!(!manager.set_hovered_toast(Some(id)));
        let remaining = manager.entries[0]
            .hover_remaining
            .expect("hover should capture the remaining timeout");
        assert!(remaining <= Duration::from_millis(500));

        age_entry_timeout(&mut manager.entries[0], Duration::from_secs(10));
        manager.tick_at(Instant::now());
        assert!(!manager.entries[0].pending_dismiss);

        assert!(!manager.set_hovered_toast(None));
        age_entry_timeout(
            &mut manager.entries[0],
            remaining + Duration::from_millis(1),
        );
        manager.tick_at(Instant::now());
        assert!(manager.entries[0].pending_dismiss);
    }

    #[test]
    fn hovering_during_exit_revives_toast_with_post_hover_grace() {
        let mut manager = OverlayManager::new();
        let id = manager.push_toast(Toast::new("caught").duration(3.0));
        settle_entry_transition(&mut manager.entries[0]);
        age_entry_timeout(&mut manager.entries[0], Duration::from_secs(3));
        manager.tick_at(Instant::now());
        age_entry_transition(&mut manager.entries[0], Duration::from_millis(50));
        manager.tick_at(Instant::now());
        let faded_opacity = manager.entries[0].opacity();

        assert!(manager.set_hovered_toast(Some(id)));
        assert!(!manager.entries[0].pending_dismiss);
        assert_eq!(
            manager.entries[0].hover_remaining,
            Some(Duration::from_secs(1))
        );
        assert_eq!(manager.entries[0].opacity(), faded_opacity);

        assert!(!manager.set_hovered_toast(None));
        age_entry_timeout(&mut manager.entries[0], Duration::from_millis(999));
        manager.tick_at(Instant::now());
        assert!(!manager.entries[0].pending_dismiss);
        age_entry_timeout(&mut manager.entries[0], Duration::from_millis(2));
        manager.tick_at(Instant::now());
        assert!(manager.entries[0].pending_dismiss);
    }

    #[test]
    fn immediate_dismiss_removes_toast_without_an_exit_transition() {
        let mut manager = OverlayManager::new();
        let first = manager.push_toast(Toast::new("first"));
        let second = manager.push_toast(Toast::new("second"));

        assert!(manager.dismiss_immediately(first));
        assert_eq!(manager.entries.len(), 1);
        assert_eq!(manager.entries[0].id, second);
        assert!(!manager.entries[0].pending_dismiss);
    }

    #[test]
    fn normal_dismiss_fades_from_the_current_opacity_before_removal() {
        let mut manager = OverlayManager::new();
        let id = manager.push_toast(Toast::new("message"));
        settle_entry_transition(&mut manager.entries[0]);

        assert!(manager.dismiss(id));
        let entry = &manager.entries[0];
        assert!(entry.pending_dismiss);
        assert_eq!(entry.opacity(), 1.0);

        age_entry_transition(&mut manager.entries[0], Duration::from_millis(50));
        manager.tick_at(Instant::now());
        assert_eq!(manager.entries.len(), 1);
        assert!(manager.entries[0].opacity() > 0.0);
        assert!(manager.entries[0].opacity() < 1.0);

        age_entry_transition(&mut manager.entries[0], Duration::from_millis(100));
        manager.tick_at(Instant::now());
        assert!(manager.entries.is_empty());
    }

    #[test]
    fn timeout_starts_exit_transition_without_consuming_it_in_the_same_tick() {
        let mut manager = OverlayManager::new();
        manager.push_toast(Toast::new("message").duration(0.0));
        settle_entry_transition(&mut manager.entries[0]);

        manager.tick_at(Instant::now());

        assert_eq!(manager.entries.len(), 1);
        assert!(manager.entries[0].pending_dismiss);
        assert_eq!(manager.entries[0].opacity(), 1.0);
    }

    fn settle_entry_transition(entry: &mut OverlayEntry) {
        entry.opacity_transition = None;
        entry.transition_tick_at = None;
    }

    fn age_entry_transition(entry: &mut OverlayEntry, age: Duration) {
        let now = Instant::now();
        entry.transition_tick_at = Some(now.checked_sub(age).unwrap_or(now));
    }

    fn age_entry_timeout(entry: &mut OverlayEntry, age: Duration) {
        entry.created_at = entry
            .created_at
            .checked_sub(age)
            .unwrap_or(entry.created_at);
    }
}