abstracttui 0.6.0

A reactive, compositor-grade terminal UI engine: fine-grained signals, layered rendering with damage tracking, images (kitty/iTerm2/sixel/mosaic), software-rasterized 3D (GLB), themes and animation.
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
//! ChoicePrompt unit tests (split file, `#[path]`-included as
//! `choice_prompt::tests`). The gate mounts on a real overlay store;
//! events route driver-style (overlays first — the modal owns
//! everything). The 0250 movement-vs-activation split, the
//! exactly-once resolution contract, and the 0297 disposal-safety law
//! are exercised through real dispatch.

use std::cell::RefCell;
use std::rc::Rc;

use super::parts::window_start;
use super::*;
use crate::base::{Point, Size};
use crate::reactive::{create_root, flush_effects, RootScope};
use crate::ui::{BufferCanvas, Key, KeyEvent, MouseButton, MouseEvent, MouseKind, UiEvent};

const VP: Size = Size::new(44, 16);

#[test]
fn window_start_generalizes_the_select_rule_to_variable_heights() {
    // Uniform heights: identical to the select windowing rule.
    let uniform = vec![1; 10];
    assert_eq!(window_start(&uniform, 4, 0), 0);
    assert_eq!(window_start(&uniform, 4, 3), 0);
    assert_eq!(window_start(&uniform, 4, 4), 1, "anchor rides the bottom");
    assert_eq!(window_start(&uniform, 4, 9), 6);
    // Variable heights: a 2-row option counts twice.
    let mixed = vec![2, 1, 2, 1];
    assert_eq!(window_start(&mixed, 3, 0), 0);
    assert_eq!(window_start(&mixed, 3, 1), 0, "2+1 fits a 3-row budget");
    assert_eq!(window_start(&mixed, 3, 2), 1, "1+2 fits; 2+1+2 does not");
    // A taller-than-budget anchor still wins the window.
    assert_eq!(window_start(&mixed, 1, 2), 2);
    // Degenerates.
    assert_eq!(window_start(&[], 4, 0), 0);
    assert_eq!(window_start(&[1], 4, 9), 0, "anchor clamps into range");
}

// ---------------------------------------------------------------- rig

struct Rig {
    root: RootScope,
    overlays: super::super::overlays::Overlays,
}

impl Rig {
    fn send(&mut self, ev: &UiEvent) {
        // Driver-order routing: overlays first (the modal owns
        // everything while open); the rig has no root-tree content.
        let _ = self.overlays.dispatch(ev);
        flush_effects();
    }

    fn key(&mut self, k: Key) {
        self.send(&UiEvent::Key(KeyEvent::plain(k)));
    }

    fn type_str(&mut self, s: &str) {
        for ch in s.chars() {
            self.key(Key::Char(ch));
        }
    }

    fn click(&mut self, x: i32, y: i32) {
        self.send(&UiEvent::Mouse(MouseEvent {
            pos: Point::new(x, y),
            kind: MouseKind::Down(MouseButton::Left),
            mods: crate::ui::Mods::NONE,
        }));
        self.send(&UiEvent::Mouse(MouseEvent {
            pos: Point::new(x, y),
            kind: MouseKind::Up(MouseButton::Left),
            mods: crate::ui::Mods::NONE,
        }));
    }

    fn wheel(&mut self, down: bool) {
        self.send(&UiEvent::Mouse(MouseEvent {
            pos: Point::new(VP.w / 2, VP.h / 2),
            kind: if down {
                MouseKind::ScrollDown
            } else {
                MouseKind::ScrollUp
            },
            mods: crate::ui::Mods::NONE,
        }));
    }

    /// The open modal, drawn into a canvas: (bounds, canvas). None =
    /// closed.
    fn render(&self) -> Option<(crate::base::Rect, BufferCanvas)> {
        let (tree, bounds) = {
            let store = self.overlays.store().borrow();
            store
                .meta
                .iter()
                .zip(&store.layers)
                .find_map(|(m, l)| match &m.content {
                    super::super::overlays::OverlayContent::Tree {
                        tree, modal: true, ..
                    } => Some((tree.handle(), l.bounds())),
                    _ => None,
                })?
        };
        let mut tree = tree.handle();
        tree.layout();
        let mut canvas = BufferCanvas::new(bounds.size());
        tree.draw(&mut canvas);
        Some((bounds, canvas))
    }

    /// The open modal as text rows. None = closed.
    fn modal(&self) -> Option<(crate::base::Rect, Vec<String>)> {
        let (bounds, canvas) = self.render()?;
        let rows = (0..bounds.h).map(|y| canvas.row_text(y)).collect();
        Some((bounds, rows))
    }

    /// (fg, bg) of the first cell of `needle` inside the modal.
    fn ink_at(&self, needle: &str) -> Option<(crate::base::Rgba, crate::base::Rgba)> {
        let (bounds, canvas) = self.render()?;
        for y in 0..bounds.h {
            let row = canvas.row_text(y);
            if let Some(ix) = row.find(needle) {
                let x = crate::text::width(&row[..ix]);
                let cell = canvas.cell(Point::new(x, y))?;
                return Some((cell.1, cell.2));
            }
        }
        None
    }

    /// A live handle to the open modal's UiTree (shared core).
    fn modal_tree(&self) -> Option<crate::ui::UiTree> {
        let store = self.overlays.store().borrow();
        store.meta.iter().find_map(|m| match &m.content {
            super::super::overlays::OverlayContent::Tree {
                tree, modal: true, ..
            } => Some(tree.handle()),
            _ => None,
        })
    }

    /// The modal's accessibility snapshot text ("" = closed).
    fn a11y(&self) -> String {
        match self.modal_tree() {
            Some(mut tree) => tree.accessibility_tree_text(),
            None => String::new(),
        }
    }

    fn modal_text(&self) -> String {
        self.modal()
            .map(|(_, rows)| rows.join("\n"))
            .unwrap_or_default()
    }

    fn is_open(&self) -> bool {
        self.modal().is_some()
    }

    /// Screen row (bounds-local) containing `needle`, as absolute
    /// viewport coordinates (x = column of the needle).
    fn find(&self, needle: &str) -> Option<(i32, i32)> {
        let (bounds, rows) = self.modal()?;
        for (y, row) in rows.iter().enumerate() {
            if let Some(ix) = row.find(needle) {
                let x = crate::text::width(&row[..ix]);
                return Some((bounds.x + x, bounds.y + y as i32));
            }
        }
        None
    }
}

fn rig_sized(vp: Size) -> Rig {
    super::super::viewport::publish_viewport(vp);
    let overlays = super::super::overlays::Overlays::new();
    overlays.ensure_root(vp);
    let (root, ()) = create_root(|_cx| {});
    flush_effects();
    Rig { root, overlays }
}

fn rig() -> Rig {
    rig_sized(VP)
}

/// Open a prompt on the rig's root scope, recording outcomes.
fn open_on(
    rig: &Rig,
    build: impl FnOnce(ChoicePrompt) -> ChoicePrompt,
) -> (Rc<RefCell<Vec<ChoiceOutcome>>>, ChoicePromptHandle) {
    let outcomes: Rc<RefCell<Vec<ChoiceOutcome>>> = Default::default();
    let sink = outcomes.clone();
    let prompt = build(
        ChoicePrompt::new("Proceed how?")
            .overlays(&rig.overlays)
            .on_resolve(move |o| sink.borrow_mut().push(o)),
    );
    let handle = prompt.open(rig.root.scope());
    flush_effects();
    (outcomes, handle)
}

fn basic(p: ChoicePrompt) -> ChoicePrompt {
    p.option("a", "Alpha")
        .option("b", "Beta")
        .option("c", "Gamma")
}

// ------------------------------------------------------------- single

#[test]
fn single_arrows_move_candidate_and_enter_commits_once() {
    let mut r = rig();
    let (outcomes, handle) = open_on(&r, basic);
    assert!(r.is_open(), "gate opened");
    assert!(handle.is_open());
    let text = r.modal_text();
    assert!(text.contains("Proceed how?"), "prompt renders: {text}");
    assert!(text.contains("● Alpha"), "candidate glyph on row 0: {text}");

    // Movement is NOT activation (0250): arrows resolve nothing.
    r.key(Key::Down);
    r.key(Key::Down);
    assert!(outcomes.borrow().is_empty(), "moves never resolve");
    assert!(r.modal_text().contains("● Gamma"), "candidate followed");

    r.key(Key::Up); // back to Beta
    r.key(Key::Enter);
    assert_eq!(
        outcomes.borrow().as_slice(),
        [ChoiceOutcome::Answered(ChoiceAnswer {
            selected: vec!["b".into()],
            other: None,
        })],
        "Enter commits the candidate"
    );
    assert!(!r.is_open(), "modal closed on resolve");
    assert!(!handle.is_open());

    // Exactly-once: further input reaches a closed gate.
    r.key(Key::Enter);
    handle.cancel();
    assert_eq!(outcomes.borrow().len(), 1, "resolution is exactly-once");
}

#[test]
fn single_click_selects_then_click_on_selected_commits() {
    let mut r = rig();
    let (outcomes, _h) = open_on(&r, basic);
    let (bx, by) = r.find("Beta").expect("Beta row");
    r.click(bx, by);
    assert!(outcomes.borrow().is_empty(), "first click only selects");
    assert!(r.modal_text().contains("● Beta"), "candidate moved");
    r.click(bx, by);
    assert_eq!(
        outcomes.borrow().as_slice(),
        [ChoiceOutcome::Answered(ChoiceAnswer {
            selected: vec!["b".into()],
            other: None,
        })],
        "click-on-selected commits"
    );
    assert!(!r.is_open());
}

#[test]
fn number_keys_jump_single_and_jump_toggle_multiple() {
    let mut r = rig();
    let (outcomes, _h) = open_on(&r, basic);
    r.key(Key::Char('3'));
    assert!(outcomes.borrow().is_empty(), "jump never commits");
    assert!(r.modal_text().contains("● Gamma"), "3 jumped the candidate");
    r.key(Key::Char('9')); // out of range: ignored
    assert!(r.modal_text().contains("● Gamma"));
    r.key(Key::Enter);
    assert_eq!(outcomes.borrow().len(), 1);

    // Multiple: jump + toggle.
    let mut r = rig();
    let (outcomes, _h) = open_on(&r, |p| basic(p).allow_multiple(true));
    r.key(Key::Char('2'));
    let text = r.modal_text();
    assert!(text.contains("☑ Beta"), "2 jump-toggled Beta: {text}");
    r.key(Key::Enter);
    assert_eq!(
        outcomes.borrow().as_slice(),
        [ChoiceOutcome::Answered(ChoiceAnswer {
            selected: vec!["b".into()],
            other: None,
        })]
    );
}

// ------------------------------------------------------------ endings

#[test]
fn escape_resolves_cancelled_never_silent() {
    let mut r = rig();
    let (outcomes, handle) = open_on(&r, basic);
    r.key(Key::Escape);
    assert_eq!(
        outcomes.borrow().as_slice(),
        [ChoiceOutcome::Cancelled],
        "Esc is an explicit outcome"
    );
    assert!(!r.is_open(), "modal closed");
    assert!(!handle.is_open());
}

#[test]
fn outside_press_does_not_dismiss_the_gate() {
    let mut r = rig();
    let (outcomes, _h) = open_on(&r, basic);
    // Press far outside the panel: swallowed by the modal, gate stays.
    r.click(0, 0);
    r.click(VP.w - 1, VP.h - 1);
    assert!(outcomes.borrow().is_empty(), "no resolution");
    assert!(r.is_open(), "a decision gate has explicit endings only");
}

#[test]
fn cancel_button_resolves_and_handle_cancel_is_idempotent() {
    let mut r = rig();
    let (outcomes, handle) = open_on(&r, basic);
    let (cx, cy) = r.find("Cancel").expect("Cancel button");
    r.click(cx, cy);
    assert_eq!(outcomes.borrow().as_slice(), [ChoiceOutcome::Cancelled]);
    assert!(!r.is_open());
    handle.cancel();
    handle.cancel();
    assert_eq!(outcomes.borrow().len(), 1, "cancel after resolve no-ops");
}

#[test]
fn handle_cancel_resolves_cancelled_exactly_once() {
    let r = rig();
    let (outcomes, handle) = open_on(&r, basic);
    assert!(handle.is_open());
    handle.cancel();
    flush_effects();
    assert_eq!(outcomes.borrow().as_slice(), [ChoiceOutcome::Cancelled]);
    assert!(!r.is_open(), "programmatic cancel closes the modal");
    handle.cancel();
    assert_eq!(outcomes.borrow().len(), 1);
}

// ----------------------------------------------------------- multiple

#[test]
fn multiple_space_toggles_enter_commits_canonical_order() {
    let mut r = rig();
    let (outcomes, _h) = open_on(&r, |p| basic(p).allow_multiple(true));
    let text = r.modal_text();
    assert!(text.contains("☐ Alpha"), "checkbox glyphs: {text}");
    assert!(text.contains("Confirm"), "Confirm button in multiple mode");

    // Toggle Gamma first, then Alpha — the ANSWER canonicalizes to
    // option order regardless of toggle order.
    r.key(Key::Down);
    r.key(Key::Down);
    r.key(Key::Char(' '));
    r.key(Key::Home);
    r.key(Key::Char(' '));
    let text = r.modal_text();
    assert!(
        text.contains("☑ Alpha") && text.contains("☑ Gamma"),
        "{text}"
    );
    assert!(outcomes.borrow().is_empty(), "toggles never resolve (0250)");

    r.key(Key::Enter);
    assert_eq!(
        outcomes.borrow().as_slice(),
        [ChoiceOutcome::Answered(ChoiceAnswer {
            selected: vec!["a".into(), "c".into()],
            other: None,
        })],
        "canonical option order"
    );
}

#[test]
fn multiple_confirm_button_commits_and_empty_set_is_legal() {
    let mut r = rig();
    let (outcomes, _h) = open_on(&r, |p| basic(p).allow_multiple(true));
    let (cx, cy) = r.find("Confirm").expect("Confirm button");
    r.click(cx, cy);
    assert_eq!(
        outcomes.borrow().as_slice(),
        [ChoiceOutcome::Answered(ChoiceAnswer {
            selected: vec![],
            other: None,
        })],
        "an empty set is the caller's to judge — the gate reports it"
    );
}

#[test]
fn multiple_click_toggles_and_checked_seeds_apply() {
    let mut r = rig();
    let (outcomes, _h) = open_on(&r, |p| {
        basic(p).allow_multiple(true).checked(["b"]).initial("b")
    });
    assert!(r.modal_text().contains("☑ Beta"), "seeded check renders");
    let (x, y) = r.find("Alpha").expect("Alpha row");
    r.click(x, y);
    assert!(r.modal_text().contains("☑ Alpha"), "click toggles on");
    r.click(x, y);
    assert!(r.modal_text().contains("☐ Alpha"), "click toggles off");
    assert!(outcomes.borrow().is_empty());
    r.key(Key::Enter);
    assert_eq!(
        outcomes.borrow().as_slice(),
        [ChoiceOutcome::Answered(ChoiceAnswer {
            selected: vec!["b".into()],
            other: None,
        })]
    );
}

// Other lane, disposal/reopen, windowing, render honesty, sequences:
// the flows sibling (same rig — file budget split).
#[path = "choice_prompt_tests_flows.rs"]
mod flows;

// Wave-5 cycle-2 folds (shortcut letters, must-choose mode, layered
// Esc, danger tint, a11y tree, focus affordance): the c2 sibling.
#[path = "choice_prompt_tests_c2.rs"]
mod c2;

// First-app 0271 folds (body_width, dismiss_label, handle.retire):
// the c3 sibling.
#[path = "choice_prompt_tests_c3.rs"]
mod c3;