revue 2.71.1

A Vue-style TUI framework for Rust with CSS styling
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
//! Toast queue manager for centralized toast notifications
//!
//! Manages a queue of toasts with deduplication, positioning, and stacking control.
//!
//! # Example
//!
//! ```rust,ignore
//! use revue::widget::{ToastQueue, ToastEntry, ToastLevel, QueuePosition};
//!
//! // Create a toast queue
//! let mut queue = ToastQueue::new()
//!     .position(QueuePosition::TopRight)
//!     .max_visible(3)
//!     .stack_direction(StackDirection::Down);
//!
//! // Add toasts
//! queue.push("File saved", ToastLevel::Success);
//! queue.push_with_id("error-1", "Connection failed", ToastLevel::Error);
//!
//! // In tick handler
//! queue.tick();
//! ```

use super::toast::{ToastLevel, ToastPosition};
use crate::render::Cell;
use crate::style::Color;
use crate::utils::{char_width, truncate_to_width};
use crate::widget::theme::DISABLED_FG;
use crate::widget::traits::{RenderContext, View, WidgetProps};
use crate::{impl_props_builders, impl_styled_view};
use std::time::{Duration, Instant};

/// Stack direction for toasts
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum StackDirection {
    /// New toasts appear below existing ones
    #[default]
    Down,
    /// New toasts appear above existing ones
    Up,
}

/// Priority level for toasts (higher = more important)
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
pub enum ToastPriority {
    /// Low priority (can be suppressed)
    Low = 0,
    /// Normal priority
    #[default]
    Normal = 1,
    /// High priority (shows immediately)
    High = 2,
    /// Critical priority (cannot be dismissed)
    Critical = 3,
}

/// A toast entry in the queue
#[derive(Clone, Debug)]
pub struct ToastEntry {
    /// Unique ID for deduplication
    pub id: Option<String>,
    /// Toast message
    pub message: String,
    /// Toast level
    pub level: ToastLevel,
    /// Priority
    pub priority: ToastPriority,
    /// Duration to show (None = use default)
    pub duration: Option<Duration>,
    /// Time when toast was created
    pub created_at: Instant,
    /// Time when toast was shown
    pub shown_at: Option<Instant>,
    /// Whether toast is dismissible
    pub dismissible: bool,
}

impl ToastEntry {
    /// Create a new toast entry
    pub fn new(message: impl Into<String>, level: ToastLevel) -> Self {
        Self {
            id: None,
            message: message.into(),
            level,
            priority: ToastPriority::Normal,
            duration: None,
            created_at: Instant::now(),
            shown_at: None,
            dismissible: true,
        }
    }

    /// Set an ID for deduplication
    pub fn with_id(mut self, id: impl Into<String>) -> Self {
        self.id = Some(id.into());
        self
    }

    /// Set the priority
    pub fn with_priority(mut self, priority: ToastPriority) -> Self {
        self.priority = priority;
        self
    }

    /// Set custom duration
    pub fn with_duration(mut self, duration: Duration) -> Self {
        self.duration = Some(duration);
        self
    }

    /// Set whether dismissible
    pub fn dismissible(mut self, dismissible: bool) -> Self {
        self.dismissible = dismissible;
        self
    }

    /// Check if this toast has expired
    fn is_expired(&self, default_duration: Duration) -> bool {
        if let Some(shown) = self.shown_at {
            let duration = self.duration.unwrap_or(default_duration);
            shown.elapsed() >= duration
        } else {
            false
        }
    }
}

/// Centralized toast queue manager
pub struct ToastQueue {
    /// Queue of pending toasts
    queue: Vec<ToastEntry>,
    /// Currently visible toasts
    visible: Vec<ToastEntry>,
    /// Queue position
    position: ToastPosition,
    /// Stack direction
    stack_direction: StackDirection,
    /// Maximum visible toasts
    max_visible: usize,
    /// Default duration for toasts
    default_duration: Duration,
    /// Gap between toasts
    gap: u16,
    /// Toast width
    toast_width: u16,
    /// Enable deduplication
    deduplicate: bool,
    /// Pause on hover
    pause_on_hover: bool,
    /// Whether toast timers are currently paused
    paused: bool,
    /// Widget properties
    props: WidgetProps,
}

impl ToastQueue {
    /// Create a new toast queue
    pub fn new() -> Self {
        Self {
            queue: Vec::new(),
            visible: Vec::new(),
            position: ToastPosition::TopRight,
            stack_direction: StackDirection::Down,
            max_visible: 5,
            default_duration: Duration::from_secs(4),
            gap: 1,
            toast_width: 40,
            deduplicate: true,
            pause_on_hover: false,
            paused: false,
            props: WidgetProps::new(),
        }
    }

    /// Set the position
    pub fn position(mut self, position: ToastPosition) -> Self {
        self.position = position;
        self
    }

    /// Set stack direction
    pub fn stack_direction(mut self, direction: StackDirection) -> Self {
        self.stack_direction = direction;
        self
    }

    /// Set maximum visible toasts
    pub fn max_visible(mut self, max: usize) -> Self {
        self.max_visible = max;
        self
    }

    /// Set default duration
    pub fn default_duration(mut self, duration: Duration) -> Self {
        self.default_duration = duration;
        self
    }

    /// Set gap between toasts
    pub fn gap(mut self, gap: u16) -> Self {
        self.gap = gap;
        self
    }

    /// Set toast width
    pub fn toast_width(mut self, width: u16) -> Self {
        self.toast_width = width;
        self
    }

    /// Enable/disable deduplication
    pub fn deduplicate(mut self, deduplicate: bool) -> Self {
        self.deduplicate = deduplicate;
        self
    }

    /// Enable/disable pause on hover
    pub fn pause_on_hover(mut self, pause: bool) -> Self {
        self.pause_on_hover = pause;
        self
    }

    /// Pause toast timers (call when mouse enters toast area)
    pub fn pause(&mut self) {
        if self.pause_on_hover {
            self.paused = true;
        }
    }

    /// Resume toast timers (call when mouse leaves toast area)
    pub fn resume(&mut self) {
        self.paused = false;
    }

    /// Check if paused
    pub fn is_paused(&self) -> bool {
        self.paused
    }

    /// Handle mouse events for pause-on-hover
    pub fn handle_mouse(
        &mut self,
        event: &crate::event::MouseEvent,
        area: crate::layout::Rect,
    ) -> bool {
        if !self.pause_on_hover {
            return false;
        }
        let in_area = event.x >= area.x
            && event.x < area.x + area.width
            && event.y >= area.y
            && event.y < area.y + area.height;
        if in_area {
            self.pause();
        } else {
            self.resume();
        }
        in_area
    }

    /// Push a simple toast
    pub fn push(&mut self, message: impl Into<String>, level: ToastLevel) {
        self.push_entry(ToastEntry::new(message, level));
    }

    /// Push a toast with an ID for deduplication
    pub fn push_with_id(
        &mut self,
        id: impl Into<String>,
        message: impl Into<String>,
        level: ToastLevel,
    ) {
        self.push_entry(ToastEntry::new(message, level).with_id(id));
    }

    /// Push an info toast
    pub fn info(&mut self, message: impl Into<String>) {
        self.push(message, ToastLevel::Info);
    }

    /// Push a success toast
    pub fn success(&mut self, message: impl Into<String>) {
        self.push(message, ToastLevel::Success);
    }

    /// Push a warning toast
    pub fn warning(&mut self, message: impl Into<String>) {
        self.push(message, ToastLevel::Warning);
    }

    /// Push an error toast
    pub fn error(&mut self, message: impl Into<String>) {
        self.push(message, ToastLevel::Error);
    }

    /// Push a toast entry
    pub fn push_entry(&mut self, entry: ToastEntry) {
        // Check for duplicates
        if self.deduplicate {
            if let Some(ref id) = entry.id {
                // Check if ID already exists
                let exists = self.visible.iter().any(|t| t.id.as_ref() == Some(id))
                    || self.queue.iter().any(|t| t.id.as_ref() == Some(id));
                if exists {
                    return;
                }
            }
        }

        // Insert based on priority
        let pos = self
            .queue
            .iter()
            .position(|t| t.priority < entry.priority)
            .unwrap_or(self.queue.len());
        self.queue.insert(pos, entry);
    }

    /// Update the queue (call on each tick)
    pub fn tick(&mut self) {
        // Remove expired toasts (unless paused)
        if !self.paused {
            self.visible
                .retain(|t| !t.is_expired(self.default_duration));
        }

        // Move toasts from queue to visible
        while self.visible.len() < self.max_visible && !self.queue.is_empty() {
            let mut entry = self.queue.remove(0);
            entry.shown_at = Some(Instant::now());
            self.visible.push(entry);
        }
    }

    /// Dismiss a specific toast by ID
    pub fn dismiss(&mut self, id: &str) {
        self.visible.retain(|t| t.id.as_deref() != Some(id));
        self.queue.retain(|t| t.id.as_deref() != Some(id));
    }

    /// Dismiss the first visible toast
    pub fn dismiss_first(&mut self) {
        if !self.visible.is_empty() {
            let first = &self.visible[0];
            if first.dismissible {
                self.visible.remove(0);
            }
        }
    }

    /// Dismiss all toasts
    pub fn dismiss_all(&mut self) {
        self.visible.retain(|t| !t.dismissible);
        self.queue.retain(|t| !t.dismissible);
    }

    /// Clear all toasts (including non-dismissible)
    pub fn clear(&mut self) {
        self.visible.clear();
        self.queue.clear();
    }

    /// Get count of visible toasts
    pub fn visible_count(&self) -> usize {
        self.visible.len()
    }

    /// Get count of pending toasts
    pub fn pending_count(&self) -> usize {
        self.queue.len()
    }

    /// Get total toast count
    pub fn total_count(&self) -> usize {
        self.visible.len() + self.queue.len()
    }

    /// Check if queue is empty
    pub fn is_empty(&self) -> bool {
        self.visible.is_empty() && self.queue.is_empty()
    }

    /// Calculate toast height
    fn toast_height(&self) -> u16 {
        3 // border + content
    }

    /// Calculate base position
    fn calculate_base_position(&self, area_width: u16, area_height: u16) -> (u16, u16) {
        let margin = 1u16;
        let toast_w = self.toast_width;
        let total_height = (self.visible.len() as u16) * (self.toast_height() + self.gap);

        let x = match self.position {
            ToastPosition::TopLeft | ToastPosition::BottomLeft => margin,
            ToastPosition::TopCenter | ToastPosition::BottomCenter => {
                area_width.saturating_sub(toast_w) / 2
            }
            ToastPosition::TopRight | ToastPosition::BottomRight => {
                area_width.saturating_sub(toast_w + margin)
            }
        };

        let y = match self.position {
            ToastPosition::TopLeft | ToastPosition::TopCenter | ToastPosition::TopRight => margin,
            ToastPosition::BottomLeft
            | ToastPosition::BottomCenter
            | ToastPosition::BottomRight => area_height.saturating_sub(total_height + margin),
        };

        (x, y)
    }

    /// Render a single toast
    fn render_toast(&self, ctx: &mut RenderContext, entry: &ToastEntry, x: u16, y: u16) {
        let area = ctx.area;
        let toast_w = self.toast_width.min(area.width.saturating_sub(x));
        let toast_h = self.toast_height();

        if x >= area.width || y >= area.height {
            return;
        }

        let color = entry.level.color();
        let bg = entry.level.bg_color();

        // Draw border
        // Top
        let mut top_left = Cell::new('');
        top_left.fg = Some(color);
        top_left.bg = Some(bg);
        ctx.set(x, y, top_left);

        for i in 1..toast_w.saturating_sub(1) {
            let mut cell = Cell::new('');
            cell.fg = Some(color);
            cell.bg = Some(bg);
            ctx.set(x + i, y, cell);
        }

        let mut top_right = Cell::new('');
        top_right.fg = Some(color);
        top_right.bg = Some(bg);
        ctx.set(x + toast_w - 1, y, top_right);

        // Bottom
        let mut bottom_left = Cell::new('');
        bottom_left.fg = Some(color);
        bottom_left.bg = Some(bg);
        ctx.set(x, y + toast_h - 1, bottom_left);

        for i in 1..toast_w.saturating_sub(1) {
            let mut cell = Cell::new('');
            cell.fg = Some(color);
            cell.bg = Some(bg);
            ctx.set(x + i, y + toast_h - 1, cell);
        }

        let mut bottom_right = Cell::new('');
        bottom_right.fg = Some(color);
        bottom_right.bg = Some(bg);
        ctx.set(x + toast_w - 1, y + toast_h - 1, bottom_right);

        // Sides and fill
        for row in 1..toast_h.saturating_sub(1) {
            let mut left = Cell::new('');
            left.fg = Some(color);
            left.bg = Some(bg);
            ctx.set(x, y + row, left);

            let mut right = Cell::new('');
            right.fg = Some(color);
            right.bg = Some(bg);
            ctx.set(x + toast_w - 1, y + row, right);

            for col in 1..toast_w.saturating_sub(1) {
                let mut fill = Cell::new(' ');
                fill.bg = Some(bg);
                ctx.set(x + col, y + row, fill);
            }
        }

        // Content
        let content_x = x + 2;
        let content_y = y + 1;

        // Icon
        let mut icon_cell = Cell::new(entry.level.icon());
        icon_cell.fg = Some(color);
        icon_cell.bg = Some(bg);
        ctx.set(content_x, content_y, icon_cell);

        // Message
        let msg_x = content_x + 2;
        let max_msg_width = toast_w.saturating_sub(5) as usize;
        let truncated_msg = truncate_to_width(&entry.message, max_msg_width);
        let mut dx: u16 = 0;
        for ch in truncated_msg.chars() {
            let cw = char_width(ch) as u16;
            if dx + cw > max_msg_width as u16 {
                break;
            }
            let mut cell = Cell::new(ch);
            cell.fg = Some(Color::WHITE);
            cell.bg = Some(bg);
            ctx.set(msg_x + dx, content_y, cell);
            dx += cw;
        }

        // Dismiss hint for dismissible toasts
        if entry.dismissible && toast_w > 10 {
            let dismiss_x = x + toast_w - 3;
            let mut dismiss = Cell::new('×');
            dismiss.fg = Some(DISABLED_FG);
            dismiss.bg = Some(bg);
            ctx.set(dismiss_x, content_y, dismiss);
        }
    }
}

impl Default for ToastQueue {
    fn default() -> Self {
        Self::new()
    }
}

impl View for ToastQueue {
    crate::impl_view_meta!("ToastQueue");

    fn render(&self, ctx: &mut RenderContext) {
        if self.visible.is_empty() {
            return;
        }

        let area = ctx.area;
        let (base_x, base_y) = self.calculate_base_position(area.width, area.height);

        for (i, entry) in self.visible.iter().enumerate() {
            let offset = (i as u16) * (self.toast_height() + self.gap);
            let y = match self.stack_direction {
                StackDirection::Down => base_y + offset,
                StackDirection::Up => base_y.saturating_sub(offset),
            };

            if y < area.height {
                self.render_toast(ctx, entry, base_x, y);
            }
        }
    }
}

// Getters for testing
impl ToastQueue {
    #[doc(hidden)]
    pub fn get_queue(&self) -> &[ToastEntry] {
        &self.queue
    }

    #[doc(hidden)]
    pub fn get_visible(&self) -> &[ToastEntry] {
        &self.visible
    }

    #[doc(hidden)]
    pub fn get_position(&self) -> ToastPosition {
        self.position
    }

    #[doc(hidden)]
    pub fn get_stack_direction(&self) -> StackDirection {
        self.stack_direction
    }

    #[doc(hidden)]
    pub fn get_max_visible(&self) -> usize {
        self.max_visible
    }

    #[doc(hidden)]
    pub fn get_default_duration(&self) -> Duration {
        self.default_duration
    }

    #[doc(hidden)]
    pub fn get_gap(&self) -> u16 {
        self.gap
    }

    #[doc(hidden)]
    pub fn get_toast_width(&self) -> u16 {
        self.toast_width
    }

    #[doc(hidden)]
    pub fn get_deduplicate(&self) -> bool {
        self.deduplicate
    }
}

impl_styled_view!(ToastQueue);
impl_props_builders!(ToastQueue);

/// Create a new toast queue
pub fn toast_queue() -> ToastQueue {
    ToastQueue::new()
}