saorsa-tui 0.3.0

Retained-mode, CSS-styled terminal UI framework
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
//! Overlay management for modal dialogs, toasts, and tooltips.
//!
//! Provides [`ScreenStack`] to manage a stack of overlay layers with
//! automatic z-indexing, position resolution, and optional background dimming.

use crate::compositor::Layer;
use crate::geometry::{Position, Rect, Size};
use crate::segment::Segment;
use crate::style::Style;

/// Unique overlay identifier.
pub type OverlayId = u64;

/// Placement of an overlay relative to an anchor element.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Placement {
    /// Above the anchor.
    Above,
    /// Below the anchor.
    Below,
    /// Left of the anchor.
    Left,
    /// Right of the anchor.
    Right,
}

/// Position strategy for an overlay.
#[derive(Debug, Clone, PartialEq)]
pub enum OverlayPosition {
    /// Centered on the screen.
    Center,
    /// At a fixed position.
    At(Position),
    /// Anchored relative to a rectangle.
    Anchored {
        /// The anchor rectangle.
        anchor: Rect,
        /// Placement relative to the anchor.
        placement: Placement,
    },
}

/// Configuration for an overlay.
#[derive(Debug, Clone)]
pub struct OverlayConfig {
    /// How the overlay should be positioned.
    pub position: OverlayPosition,
    /// Size of the overlay content.
    pub size: Size,
    /// Z-index offset from the stack's base z-index.
    pub z_offset: i32,
    /// Whether to insert a dim layer behind this overlay.
    pub dim_background: bool,
}

struct OverlayEntry {
    id: OverlayId,
    config: OverlayConfig,
    lines: Vec<Vec<Segment>>,
}

/// Manages a stack of overlay layers with auto z-indexing.
///
/// Overlays are rendered in insertion order. Each overlay receives a unique
/// z-index spaced 10 apart from the base. Dim layers are inserted one
/// z-level below overlays that request background dimming.
pub struct ScreenStack {
    overlays: Vec<OverlayEntry>,
    next_id: OverlayId,
    base_z: i32,
}

impl ScreenStack {
    /// Creates an empty screen stack with base z-index 1000.
    pub fn new() -> Self {
        Self {
            overlays: Vec::new(),
            next_id: 1,
            base_z: 1000,
        }
    }

    /// Pushes an overlay onto the stack.
    ///
    /// Returns a unique [`OverlayId`] for later removal.
    pub fn push(&mut self, config: OverlayConfig, lines: Vec<Vec<Segment>>) -> OverlayId {
        let id = self.next_id;
        self.next_id += 1;
        self.overlays.push(OverlayEntry { id, config, lines });
        id
    }

    /// Removes the topmost overlay from the stack.
    pub fn pop(&mut self) -> Option<OverlayId> {
        self.overlays.pop().map(|e| e.id)
    }

    /// Removes a specific overlay by ID.
    ///
    /// Returns `true` if the overlay was found and removed.
    pub fn remove(&mut self, id: OverlayId) -> bool {
        let before = self.overlays.len();
        self.overlays.retain(|e| e.id != id);
        self.overlays.len() < before
    }

    /// Removes all overlays.
    pub fn clear(&mut self) {
        self.overlays.clear();
    }

    /// Returns the number of overlays in the stack.
    pub fn len(&self) -> usize {
        self.overlays.len()
    }

    /// Returns `true` if the stack has no overlays.
    pub fn is_empty(&self) -> bool {
        self.overlays.is_empty()
    }

    /// Resolves an overlay position to absolute screen coordinates.
    pub fn resolve_position(position: &OverlayPosition, size: Size, screen: Size) -> Position {
        match position {
            OverlayPosition::Center => {
                let x = screen.width.saturating_sub(size.width) / 2;
                let y = screen.height.saturating_sub(size.height) / 2;
                Position::new(x, y)
            }
            OverlayPosition::At(pos) => *pos,
            OverlayPosition::Anchored { anchor, placement } => match placement {
                Placement::Above => {
                    let x = anchor
                        .position
                        .x
                        .saturating_add(anchor.size.width / 2)
                        .saturating_sub(size.width / 2);
                    let y = anchor.position.y.saturating_sub(size.height);
                    Position::new(x, y)
                }
                Placement::Below => {
                    let x = anchor
                        .position
                        .x
                        .saturating_add(anchor.size.width / 2)
                        .saturating_sub(size.width / 2);
                    let y = anchor.position.y.saturating_add(anchor.size.height);
                    Position::new(x, y)
                }
                Placement::Left => {
                    let x = anchor.position.x.saturating_sub(size.width);
                    let y = anchor
                        .position
                        .y
                        .saturating_add(anchor.size.height / 2)
                        .saturating_sub(size.height / 2);
                    Position::new(x, y)
                }
                Placement::Right => {
                    let x = anchor.position.x.saturating_add(anchor.size.width);
                    let y = anchor
                        .position
                        .y
                        .saturating_add(anchor.size.height / 2)
                        .saturating_sub(size.height / 2);
                    Position::new(x, y)
                }
            },
        }
    }

    /// Applies all overlays as layers to a compositor.
    ///
    /// Each overlay is added with its resolved position and z-index.
    /// Overlays with `dim_background` get a full-screen dim layer inserted
    /// one z-level below them.
    pub fn apply_to_compositor(
        &self,
        compositor: &mut crate::compositor::Compositor,
        screen: Size,
    ) {
        for (i, entry) in self.overlays.iter().enumerate() {
            let z = self.base_z + (i as i32) * 10 + entry.config.z_offset;

            if entry.config.dim_background {
                compositor.add_layer(create_dim_layer(screen, z - 1));
            }

            let pos = Self::resolve_position(&entry.config.position, entry.config.size, screen);
            let region = Rect::new(
                pos.x,
                pos.y,
                entry.config.size.width,
                entry.config.size.height,
            );
            compositor.add_layer(Layer::new(entry.id, region, z, entry.lines.clone()));
        }
    }
}

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

/// Creates a full-screen dim layer for background dimming.
pub fn create_dim_layer(screen: Size, z_index: i32) -> Layer {
    let dim_style = Style::new().dim(true);
    let mut lines = Vec::new();
    for _ in 0..screen.height {
        lines.push(vec![Segment::styled(
            " ".repeat(screen.width as usize),
            dim_style.clone(),
        )]);
    }
    Layer::new(
        0,
        Rect::new(0, 0, screen.width, screen.height),
        z_index,
        lines,
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn empty_stack() {
        let stack = ScreenStack::new();
        assert!(stack.is_empty());
        assert!(stack.is_empty());
    }

    #[test]
    fn push_increments_len() {
        let mut stack = ScreenStack::new();
        let config = OverlayConfig {
            position: OverlayPosition::Center,
            size: Size::new(10, 5),
            z_offset: 0,
            dim_background: false,
        };
        let id = stack.push(config, vec![vec![Segment::new("hi")]]);
        assert!(id == 1);
        assert!(stack.len() == 1);
    }

    #[test]
    fn pop_returns_topmost() {
        let mut stack = ScreenStack::new();
        let config = OverlayConfig {
            position: OverlayPosition::Center,
            size: Size::new(10, 5),
            z_offset: 0,
            dim_background: false,
        };
        let _id1 = stack.push(config.clone(), vec![]);
        let id2 = stack.push(config, vec![]);
        assert!(stack.pop() == Some(id2));
        assert!(stack.len() == 1);
    }

    #[test]
    fn pop_empty_returns_none() {
        let mut stack = ScreenStack::new();
        assert!(stack.pop().is_none());
    }

    #[test]
    fn remove_by_id() {
        let mut stack = ScreenStack::new();
        let config = OverlayConfig {
            position: OverlayPosition::Center,
            size: Size::new(10, 5),
            z_offset: 0,
            dim_background: false,
        };
        let id1 = stack.push(config.clone(), vec![]);
        let _id2 = stack.push(config, vec![]);
        assert!(stack.remove(id1));
        assert!(stack.len() == 1);
    }

    #[test]
    fn remove_nonexistent_returns_false() {
        let mut stack = ScreenStack::new();
        assert!(!stack.remove(999));
    }

    #[test]
    fn clear_removes_all() {
        let mut stack = ScreenStack::new();
        let config = OverlayConfig {
            position: OverlayPosition::Center,
            size: Size::new(10, 5),
            z_offset: 0,
            dim_background: false,
        };
        stack.push(config.clone(), vec![]);
        stack.push(config, vec![]);
        stack.clear();
        assert!(stack.is_empty());
    }

    #[test]
    fn resolve_center() {
        let pos = ScreenStack::resolve_position(
            &OverlayPosition::Center,
            Size::new(20, 10),
            Size::new(80, 24),
        );
        assert!(pos.x == 30);
        assert!(pos.y == 7);
    }

    #[test]
    fn resolve_at() {
        let pos = ScreenStack::resolve_position(
            &OverlayPosition::At(Position::new(5, 3)),
            Size::new(20, 10),
            Size::new(80, 24),
        );
        assert!(pos.x == 5);
        assert!(pos.y == 3);
    }

    #[test]
    fn resolve_anchored_below() {
        let anchor = Rect::new(30, 5, 10, 2);
        let pos = ScreenStack::resolve_position(
            &OverlayPosition::Anchored {
                anchor,
                placement: Placement::Below,
            },
            Size::new(20, 3),
            Size::new(80, 24),
        );
        // x centered: 30 + 5 - 10 = 25
        assert!(pos.x == 25);
        // y below: 5 + 2 = 7
        assert!(pos.y == 7);
    }

    #[test]
    fn resolve_anchored_above() {
        let anchor = Rect::new(30, 10, 10, 2);
        let pos = ScreenStack::resolve_position(
            &OverlayPosition::Anchored {
                anchor,
                placement: Placement::Above,
            },
            Size::new(20, 3),
            Size::new(80, 24),
        );
        assert!(pos.x == 25);
        assert!(pos.y == 7); // 10 - 3 = 7
    }

    #[test]
    fn resolve_anchored_right() {
        let anchor = Rect::new(10, 10, 5, 4);
        let pos = ScreenStack::resolve_position(
            &OverlayPosition::Anchored {
                anchor,
                placement: Placement::Right,
            },
            Size::new(8, 3),
            Size::new(80, 24),
        );
        assert!(pos.x == 15); // 10 + 5
        assert!(pos.y == 11); // 10 + 2 - 1
    }

    #[test]
    fn dim_layer_covers_screen() {
        let layer = create_dim_layer(Size::new(80, 24), 999);
        assert!(layer.z_index == 999);
        assert!(layer.region.size.width == 80);
        assert!(layer.region.size.height == 24);
        assert!(layer.lines.len() == 24);
    }

    #[test]
    fn dim_layer_style_is_dim() {
        let layer = create_dim_layer(Size::new(10, 2), 500);
        assert!(layer.lines.len() == 2);
        assert!(layer.lines[0][0].style.dim);
    }

    #[test]
    fn apply_to_compositor_adds_layers() {
        let mut stack = ScreenStack::new();
        let config = OverlayConfig {
            position: OverlayPosition::Center,
            size: Size::new(10, 3),
            z_offset: 0,
            dim_background: false,
        };
        stack.push(config, vec![vec![Segment::new("test")]]);

        let mut compositor = crate::compositor::Compositor::new(80, 24);
        stack.apply_to_compositor(&mut compositor, Size::new(80, 24));

        let mut buf = crate::buffer::ScreenBuffer::new(Size::new(80, 24));
        compositor.compose(&mut buf);

        // Check content appears near center (x=35, y=10)
        match buf.get(35, 10) {
            Some(cell) => assert!(cell.grapheme == "t"),
            None => unreachable!(),
        }
    }

    #[test]
    fn apply_with_dim_background() {
        let mut stack = ScreenStack::new();
        let config = OverlayConfig {
            position: OverlayPosition::At(Position::new(5, 5)),
            size: Size::new(10, 3),
            z_offset: 0,
            dim_background: true,
        };
        stack.push(config, vec![vec![Segment::new("modal")]]);

        let mut compositor = crate::compositor::Compositor::new(80, 24);
        stack.apply_to_compositor(&mut compositor, Size::new(80, 24));

        let mut buf = crate::buffer::ScreenBuffer::new(Size::new(80, 24));
        compositor.compose(&mut buf);

        // Corner should have dim style from dim layer
        match buf.get(0, 0) {
            Some(cell) => assert!(cell.style.dim),
            None => unreachable!(),
        }
        // Overlay content should be visible
        match buf.get(5, 5) {
            Some(cell) => assert!(cell.grapheme == "m"),
            None => unreachable!(),
        }
    }

    // --- Integration tests: full overlay pipeline ---

    #[test]
    fn modal_centered_on_screen() {
        use crate::widget::modal::Modal;

        let modal = Modal::new("Test", 20, 5);
        let lines = modal.render_to_lines();
        let config = modal.to_overlay_config();

        let mut stack = ScreenStack::new();
        stack.push(config, lines);

        let screen = Size::new(80, 24);
        let mut compositor = crate::compositor::Compositor::new(80, 24);
        stack.apply_to_compositor(&mut compositor, screen);

        let mut buf = crate::buffer::ScreenBuffer::new(screen);
        compositor.compose(&mut buf);

        // Center x = (80-20)/2 = 30, y = (24-5)/2 = 9
        // Top-left corner should be the border character
        match buf.get(30, 9) {
            Some(cell) => assert!(cell.grapheme == ""),
            None => unreachable!(),
        }
    }

    #[test]
    fn modal_with_dim_background_pipeline() {
        use crate::widget::modal::Modal;

        let modal = Modal::new("Dim", 20, 5);
        let lines = modal.render_to_lines();
        let config = modal.to_overlay_config();
        // Confirm dim is set
        assert!(config.dim_background);

        let mut stack = ScreenStack::new();
        stack.push(config, lines);

        let screen = Size::new(80, 24);
        let mut compositor = crate::compositor::Compositor::new(80, 24);
        stack.apply_to_compositor(&mut compositor, screen);

        let mut buf = crate::buffer::ScreenBuffer::new(screen);
        compositor.compose(&mut buf);

        // Corner (outside modal) should have dim style
        match buf.get(0, 0) {
            Some(cell) => assert!(cell.style.dim),
            None => unreachable!(),
        }
    }

    #[test]
    fn toast_at_top_right_pipeline() {
        use crate::widget::toast::Toast;

        let toast = Toast::new("Saved!").with_width(10);
        let lines = toast.render_to_lines();
        let screen = Size::new(80, 24);
        let config = toast.to_overlay_config(screen);

        let mut stack = ScreenStack::new();
        stack.push(config, lines);

        let mut compositor = crate::compositor::Compositor::new(80, 24);
        stack.apply_to_compositor(&mut compositor, screen);

        let mut buf = crate::buffer::ScreenBuffer::new(screen);
        compositor.compose(&mut buf);

        // Toast at top-right: x = 80-10 = 70, y = 0
        match buf.get(70, 0) {
            Some(cell) => assert!(cell.grapheme == "S"),
            None => unreachable!(),
        }
    }

    #[test]
    fn tooltip_below_anchor_pipeline() {
        use crate::overlay::Placement;
        use crate::widget::tooltip::Tooltip;

        let anchor = Rect::new(30, 5, 10, 2);
        let tooltip = Tooltip::new("hint", anchor).with_placement(Placement::Below);
        let lines = tooltip.render_to_lines();
        let screen = Size::new(80, 24);
        let config = tooltip.to_overlay_config(screen);

        let mut stack = ScreenStack::new();
        stack.push(config, lines);

        let mut compositor = crate::compositor::Compositor::new(80, 24);
        stack.apply_to_compositor(&mut compositor, screen);

        let mut buf = crate::buffer::ScreenBuffer::new(screen);
        compositor.compose(&mut buf);

        // Below anchor: y = 5 + 2 = 7, x centered: 30 + 5 - 2 = 33
        match buf.get(33, 7) {
            Some(cell) => assert!(cell.grapheme == "h"),
            None => unreachable!(),
        }
    }

    #[test]
    fn two_modals_stacked() {
        let mut stack = ScreenStack::new();

        // First modal at (10, 5)
        let config1 = OverlayConfig {
            position: OverlayPosition::At(Position::new(10, 5)),
            size: Size::new(10, 3),
            z_offset: 0,
            dim_background: false,
        };
        stack.push(config1, vec![vec![Segment::new("first")]]);

        // Second modal at same position (on top)
        let config2 = OverlayConfig {
            position: OverlayPosition::At(Position::new(10, 5)),
            size: Size::new(10, 3),
            z_offset: 0,
            dim_background: false,
        };
        stack.push(config2, vec![vec![Segment::new("second")]]);

        let screen = Size::new(80, 24);
        let mut compositor = crate::compositor::Compositor::new(80, 24);
        stack.apply_to_compositor(&mut compositor, screen);

        let mut buf = crate::buffer::ScreenBuffer::new(screen);
        compositor.compose(&mut buf);

        // Topmost (second) should be visible
        match buf.get(10, 5) {
            Some(cell) => assert!(cell.grapheme == "s"),
            None => unreachable!(),
        }
    }

    #[test]
    fn modal_plus_toast_z_order() {
        use crate::widget::modal::Modal;
        use crate::widget::toast::Toast;

        let modal = Modal::new("M", 20, 5);
        let modal_lines = modal.render_to_lines();
        let modal_config = modal.to_overlay_config();

        let toast = Toast::new("Toast!").with_width(10);
        let screen = Size::new(80, 24);
        let toast_lines = toast.render_to_lines();
        let toast_config = toast.to_overlay_config(screen);

        let mut stack = ScreenStack::new();
        stack.push(modal_config, modal_lines);
        stack.push(toast_config, toast_lines);

        let mut compositor = crate::compositor::Compositor::new(80, 24);
        stack.apply_to_compositor(&mut compositor, screen);

        let mut buf = crate::buffer::ScreenBuffer::new(screen);
        compositor.compose(&mut buf);

        // Toast at top-right should be visible (higher z since added second)
        match buf.get(70, 0) {
            Some(cell) => assert!(cell.grapheme == "T"),
            None => unreachable!(),
        }
    }

    #[test]
    fn remove_modal_clears_dim() {
        let mut stack = ScreenStack::new();
        let config = OverlayConfig {
            position: OverlayPosition::Center,
            size: Size::new(10, 3),
            z_offset: 0,
            dim_background: true,
        };
        let id = stack.push(config, vec![vec![Segment::new("x")]]);

        // Remove it
        assert!(stack.remove(id));
        assert!(stack.is_empty());

        let screen = Size::new(80, 24);
        let mut compositor = crate::compositor::Compositor::new(80, 24);
        stack.apply_to_compositor(&mut compositor, screen);

        let mut buf = crate::buffer::ScreenBuffer::new(screen);
        compositor.compose(&mut buf);

        // No dim layer should be present — corner should be blank
        match buf.get(0, 0) {
            Some(cell) => assert!(!cell.style.dim),
            None => unreachable!(),
        }
    }

    #[test]
    fn clear_removes_all_overlays() {
        let mut stack = ScreenStack::new();
        let config = OverlayConfig {
            position: OverlayPosition::At(Position::new(0, 0)),
            size: Size::new(5, 1),
            z_offset: 0,
            dim_background: false,
        };
        stack.push(config.clone(), vec![vec![Segment::new("A")]]);
        stack.push(config, vec![vec![Segment::new("B")]]);
        stack.clear();

        let screen = Size::new(80, 24);
        let mut compositor = crate::compositor::Compositor::new(80, 24);
        stack.apply_to_compositor(&mut compositor, screen);

        let mut buf = crate::buffer::ScreenBuffer::new(screen);
        compositor.compose(&mut buf);

        // Should be blank (no overlays)
        match buf.get(0, 0) {
            Some(cell) => assert!(cell.grapheme == " "),
            None => unreachable!(),
        }
    }
}