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
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
//! Wave regression suite (STABILITY seat): backlog items 0220 / 0230 /
//! 0240 plus the 0170 rulings-subset compile guarantees (ADR-0003's
//! `Capabilities` constructor contract). Everything here drives the
//! PUBLIC API only — this file compiles as a separate crate, exactly
//! like a downstream application, which is the point: it pins the
//! surfaces applications depend on.

use abstracttui::app::{App, Driver, Modal, RunConfig};
use abstracttui::base::Size;
use abstracttui::layout::{Dimension, LayoutStyle};
use abstracttui::reactive::{create_root, flush_effects, Scope};
use abstracttui::term::{Capabilities, EnterOptions, GraphicsCaps, KittyFlags, MouseMode};
use abstracttui::testing::CaptureTerm;
use abstracttui::ui::{
    dyn_view_scoped, text, BufferCanvas, Element, Key, KeyChord, KeyEvent, UiEvent, UiTree,
};
use abstracttui::widgets::TextInput;

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

/// Fixed capabilities so the host environment never leaks into the
/// assertions — built through the ADR-0003 constructor, as any
/// downstream test must once `Capabilities` is `#[non_exhaustive]`.
fn test_config() -> RunConfig {
    RunConfig {
        caps: Some(Capabilities::with(|c| {
            c.truecolor = true;
            c.colors_256 = true;
            c.unicode_ok = true;
        })),
        enter: Some(EnterOptions {
            alternate_screen: true,
            hide_cursor: true,
            mouse: MouseMode::Off,
            bracketed_paste: false,
            focus_events: false,
            kitty_keyboard: KittyFlags(0),
        }),
        probe: false,
        ..RunConfig::default()
    }
}

/// App shell + captured scope for tests that open modals at runtime.
fn app_with_scope(size: Size) -> (App, Rc<RefCell<Option<Scope>>>) {
    let mut app = App::new(size);
    let scope_holder: Rc<RefCell<Option<Scope>>> = Rc::new(RefCell::new(None));
    let sh = scope_holder.clone();
    app.mount(move |cx| {
        *sh.borrow_mut() = Some(cx);
        Element::new().child(text("underneath")).build()
    })
    .expect("mount");
    (app, scope_holder)
}

// ---------------------------------------------------------------------
// 0220 — autofocus inside a dyn_view regeneration must not panic the
// reactive runtime, and focus must land on the node.
// ---------------------------------------------------------------------

/// The exact first-app composition: a theme-keyed `dyn_view_scoped`
/// wrapping a `TextInput` marked `.autofocus()`. Before the fix this
/// panicked ("dependency cycle") at mount: the dyn-subtree autofocus
/// path delivered FocusIn INSIDE the running dyn computation, and the
/// input's `focus_signal` write re-entered the running node via the
/// effect flush.
#[test]
fn autofocus_inside_dyn_view_regeneration_mounts_and_focuses() {
    let tokens = abstracttui::theme::default_theme().tokens;
    let mut tree = UiTree::new(Size::new(30, 4));
    let mut probe = None;
    let (root, ()) = create_root(|cx| {
        let theme_key = cx.signal(0u32);
        probe = Some(theme_key);
        let view = Element::new()
            .style(LayoutStyle::column())
            .child(dyn_view_scoped(
                LayoutStyle::default().height(Dimension::Cells(1)),
                move |scx| {
                    let _generation = theme_key.get(); // tracked: regen on change
                    TextInput::new()
                        .placeholder("composer")
                        .element(scx, &tokens)
                        .autofocus()
                        .build()
                },
            ))
            .build();
        // Initial mount: the autofocus node lives inside a dyn whose
        // effect runs nested in this mount — the panic fired here too.
        tree.mount(cx, view);
    });
    tree.layout();
    let before = tree
        .focused()
        .expect("initial mount delivers dyn-subtree autofocus");

    // The regeneration path: a theme-key write rebuilds the dyn subtree
    // (new instances, new autofocus node) inside a flush. Focus must
    // land on the REGENERATED node once the frame settles — without a
    // reactive-runtime panic.
    probe.expect("signal captured").set(1);
    flush_effects();
    tree.layout(); // frame phase L: parked autofocus delivers here
    let after = tree.focused().expect("focus landed after regeneration");
    assert_ne!(
        before, after,
        "focus points at the regenerated input, not the disposed one"
    );
    root.dispose();
}

/// Several regenerations in a row: every generation ends focused on a
/// LIVE instance (typing works), never on a disposed corpse.
#[test]
fn repeated_dyn_regenerations_keep_autofocus_deterministic() {
    let tokens = abstracttui::theme::default_theme().tokens;
    let mut tree = UiTree::new(Size::new(30, 4));
    let mut probe = None;
    let (root, ()) = create_root(|cx| {
        let key = cx.signal(0u32);
        probe = Some(key);
        let view = Element::new()
            .child(dyn_view_scoped(LayoutStyle::default(), move |scx| {
                let _k = key.get();
                TextInput::new().element(scx, &tokens).autofocus().build()
            }))
            .build();
        tree.mount(cx, view);
    });
    let key = probe.expect("signal");
    for generation in 1..=3u32 {
        key.set(generation);
        flush_effects();
        tree.layout();
        assert!(
            tree.focused().is_some(),
            "generation {generation} ends focused"
        );
        // The focused node is alive and receiving: a typed character is
        // consumed by the input's own key handler.
        let consumed = tree.dispatch(&UiEvent::Key(KeyEvent::plain(Key::Char('x'))));
        assert!(consumed, "generation {generation} input consumes keys");
    }
    root.dispose();
}

// ---------------------------------------------------------------------
// 0230 — modal content shortcuts live from frame one.
// ---------------------------------------------------------------------

/// The full production path (bytes -> driver -> overlay routing): a
/// modal whose content root carries a shortcut and NO focusable
/// children. The chord must fire without the user pressing Tab first.
#[test]
fn modal_content_shortcut_fires_without_tab() {
    let size = Size::new(30, 8);
    let (mut app, scope_holder) = app_with_scope(size);
    let overlays = app.overlays();
    let mut term = CaptureTerm::new(size);
    let mut driver = Driver::new(&mut app, &mut term, test_config()).expect("driver");
    driver.turn(&mut app, &mut term).expect("frame 1");

    let fired: Rc<RefCell<u32>> = Rc::new(RefCell::new(0));
    let f2 = fired.clone();
    let cx = scope_holder.borrow().expect("scope");
    let _modal = Modal::open(&overlays, cx, size, Size::new(20, 4), move |_| {
        Element::new()
            .shortcut(KeyChord::plain(Key::Char('a')), move |_| {
                *f2.borrow_mut() += 1;
            })
            .child(text("approve? [a]"))
            .build()
    });
    driver.turn(&mut app, &mut term).expect("modal frame");

    // Frame one: the chord goes straight to the modal tree — no Tab.
    term.push_input(b"a");
    driver.turn(&mut app, &mut term).expect("chord turn");
    assert_eq!(
        *fired.borrow(),
        1,
        "content-root shortcut fired without Tab"
    );
}

/// A modal with an autofocus input: the input is focused from frame one
/// (typing works immediately) AND content-root shortcuts still fire —
/// the root stays on the dispatch path.
#[test]
fn modal_autofocus_wins_and_root_shortcuts_stay_reachable() {
    let tokens = abstracttui::theme::default_theme().tokens;
    let size = Size::new(30, 8);
    let (app, scope_holder) = app_with_scope(size);
    let overlays = app.overlays();

    let escaped: Rc<RefCell<u32>> = Rc::new(RefCell::new(0));
    let e2 = escaped.clone();
    let value_probe: Rc<RefCell<Option<abstracttui::reactive::Signal<String>>>> =
        Rc::new(RefCell::new(None));
    let v2 = value_probe.clone();
    let cx = scope_holder.borrow().expect("scope");
    let modal = Modal::open(&overlays, cx, size, Size::new(24, 5), move |mcx| {
        let value = mcx.signal(String::new());
        *v2.borrow_mut() = Some(value);
        Element::new()
            .style(LayoutStyle::column())
            .shortcut(KeyChord::plain(Key::Escape), move |_| {
                *e2.borrow_mut() += 1;
            })
            .child(text("name:"))
            .child(
                TextInput::new()
                    .value(value)
                    .element(mcx, &tokens)
                    .autofocus()
                    .build(),
            )
            .build()
    });

    let mut tree = modal.layer().tree().expect("modal layer holds a tree");
    // The autofocus input owns the keyboard from frame one...
    tree.dispatch(&UiEvent::Key(KeyEvent::plain(Key::Char('x'))));
    let typed = value_probe
        .borrow()
        .expect("value signal captured")
        .get_untracked();
    assert_eq!(typed, "x", "typing reaches the autofocused input");
    // ...and the content root's Escape still resolves via the path.
    tree.dispatch(&UiEvent::Key(KeyEvent::plain(Key::Escape)));
    assert_eq!(
        *escaped.borrow(),
        1,
        "root shortcut reachable while the input is focused"
    );
}

// ---------------------------------------------------------------------
// 0240 — overflowing modal content must not silently erase fixed rows
// (the popups half: declared fixed sizes are floors inside modal trees).
// ---------------------------------------------------------------------

/// The first-app probe: a small modal whose column holds a title row, an
/// overflowing middle, a button row and a hint row. Before the fix the
/// button and hint rows solved to ZERO height — invisible controls in an
/// approval dialog. The floor keeps every declared fixed row visible;
/// the grow middle absorbs the loss.
#[test]
fn modal_fixed_rows_survive_content_overflow() {
    let size = Size::new(30, 8);
    let (app, scope_holder) = app_with_scope(size);
    let overlays = app.overlays();

    let cx = scope_holder.borrow().expect("scope");
    // 30x6 modal, 1-cell panel padding: 4 content rows for a column
    // asking title(1) + middle(grow, 40-line content) + buttons(1) +
    // hint(1) — overflow pressure by construction.
    let modal = Modal::open(&overlays, cx, size, Size::new(30, 6), |_| {
        let tall = (0..40).map(|i| format!("arg line {i}")).collect::<Vec<_>>();
        let fixed_row = |label: &str| {
            Element::new()
                .style(LayoutStyle::default().height(Dimension::Cells(1)))
                .child(text(label))
                .build()
        };
        Element::new()
            .style(LayoutStyle::column())
            .child(fixed_row("Approve tool?"))
            .child(
                Element::new()
                    .style(LayoutStyle::default().grow(1.0))
                    .child(text(tall.join("\n")))
                    .build(),
            )
            .child(fixed_row("[a]pprove [d]eny"))
            .child(fixed_row("esc closes"))
            .build()
    });

    let mut tree = modal.layer().tree().expect("modal layer holds a tree");
    let mut canvas = BufferCanvas::new(Size::new(30, 6));
    tree.draw(&mut canvas);
    let screen: Vec<String> = (0..6).map(|y| canvas.row_text(y)).collect();
    let flat = screen.join("\n");
    for needle in ["Approve tool?", "[a]pprove [d]eny", "esc closes"] {
        assert!(
            flat.contains(needle),
            "fixed row {needle:?} must stay visible; screen:\n{flat}"
        );
    }
}

/// Opt-out honored: an author who explicitly allows a row to vanish
/// (`min_h(0)`) keeps that behavior — the floor only applies where the
/// author said nothing.
#[test]
fn modal_fixed_row_floor_respects_explicit_min() {
    let size = Size::new(30, 8);
    let (app, scope_holder) = app_with_scope(size);
    let overlays = app.overlays();

    let cx = scope_holder.borrow().expect("scope");
    let modal = Modal::open(&overlays, cx, size, Size::new(30, 6), |_| {
        let tall = (0..40).map(|i| format!("line {i}")).collect::<Vec<_>>();
        Element::new()
            .style(LayoutStyle::column())
            .child(
                Element::new()
                    // Explicit opt-out: this row MAY collapse.
                    .style(LayoutStyle::default().height(Dimension::Cells(1)).min_h(0))
                    .child(text("collapsible"))
                    .build(),
            )
            .child(
                Element::new()
                    .style(LayoutStyle::default().grow(1.0))
                    .child(text(tall.join("\n")))
                    .build(),
            )
            .child(
                Element::new()
                    .style(LayoutStyle::default().height(Dimension::Cells(1)))
                    .child(text("kept row"))
                    .build(),
            )
            .build()
    });

    let mut tree = modal.layer().tree().expect("modal tree");
    let mut canvas = BufferCanvas::new(Size::new(30, 6));
    tree.draw(&mut canvas);
    let flat: String = (0..6).map(|y| canvas.row_text(y) + "\n").collect();
    assert!(
        !flat.contains("collapsible"),
        "explicit min_h(0) keeps the author's collapse; screen:\n{flat}"
    );
    assert!(
        flat.contains("kept row"),
        "unopted fixed row stays visible; screen:\n{flat}"
    );
}

// ---------------------------------------------------------------------
// 0170 rulings subset — ADR-0003: Capabilities/GraphicsCaps are
// non_exhaustive; downstream construction goes through `with`.
// ---------------------------------------------------------------------

/// Downstream-style construction: adding a capability field must never
/// break this test (this file compiles as a separate crate, exactly
/// like an application).
#[test]
fn capabilities_construct_via_with_and_grow_without_breakage() {
    let caps = Capabilities::with(|c| {
        c.truecolor = true;
        c.colors_256 = true;
        c.unicode_ok = true;
    });
    assert!(caps.truecolor && caps.colors_256 && caps.unicode_ok);
    assert!(caps.deferred_wrap, "unset fields keep their defaults");

    let gfx = GraphicsCaps::with(|g| {
        g.kitty_graphics = true;
    });
    assert!(gfx.kitty_graphics && !gfx.sixel);

    // The read side stays plain field access, and detection still works.
    let from_env = Capabilities::detect_env_with(&|_| None);
    assert!(from_env.dumb, "empty environment is honestly dumb");
}

// ---------------------------------------------------------------------
// 2026-07-22 dashboard incident — zero-collapse diagnostics must reach
// the in-app notices lane, never stderr mid-session (stderr shares the
// terminal with the alternate screen and corrupts the live frame), and
// must not spam under dyn regeneration (covered at the layout layer by
// `zero_collapse_dedup_survives_dyn_regeneration`).
// ---------------------------------------------------------------------

/// 1330: the composer shape. A `TextArea`-like child that declared
/// `shrink(0.0)` inside a box one row shorter than it keeps its rows
/// and is solved OUTSIDE the box, where the next sibling overpaints
/// them — silently, before this notice existed. The row the user loses
/// is the one the caret is on, so the failure reads as "the panel is
/// too short AND it doesn't follow my text". The engine names it.
#[cfg(debug_assertions)]
#[test]
fn main_axis_overflow_reaches_the_notices_lane() {
    use abstracttui::app::use_startup_notices;

    create_root(|_| {
        let size = Size::new(40, 10);
        let mut app = App::new(size);
        type NoticeSignal = abstracttui::reactive::Signal<Vec<String>>;
        let lane: Rc<RefCell<Option<NoticeSignal>>> = Rc::new(RefCell::new(None));
        let lane_in = lane.clone();
        app.mount(move |cx| {
            *lane_in.borrow_mut() = Some(use_startup_notices(cx));
            // The composer row, one row SHORTER than the widget inside
            // it — exactly what the solver hands an app whose chrome
            // column is under overflow pressure.
            let composer = Element::new()
                .style(LayoutStyle::column().h(3))
                .child(
                    Element::new()
                        .style(LayoutStyle::default().h(4).shrink(0.0))
                        .child(text("composer"))
                        .build(),
                )
                .build();
            Element::new()
                .style(LayoutStyle::column())
                .child(composer)
                .child(
                    Element::new()
                        .style(LayoutStyle::default().h(1))
                        .child(text("status row"))
                        .build(),
                )
                .build()
        })
        .expect("mount");

        let mut term = CaptureTerm::new(size);
        let mut driver = Driver::new(&mut app, &mut term, test_config()).expect("driver");
        driver.turn(&mut app, &mut term).expect("turn 1");
        driver.turn(&mut app, &mut term).expect("turn 2");
        flush_effects();

        let signal = lane.borrow().expect("notices signal captured");
        let notes = signal.get();
        let overflow: Vec<&String> = notes
            .iter()
            .filter(|n| n.contains("solved OUTSIDE its parent"))
            .collect();
        assert_eq!(
            overflow.len(),
            1,
            "exactly one overflow notice in the lane: {notes:?}"
        );
        // Same situation across turns is one fact, not one per frame.
        driver.turn(&mut app, &mut term).expect("turn 3");
        driver.turn(&mut app, &mut term).expect("turn 4");
        flush_effects();
        let notes2 = signal.get();
        let overflow2: Vec<&String> = notes2
            .iter()
            .filter(|n| n.contains("solved OUTSIDE its parent"))
            .collect();
        assert_eq!(overflow2.len(), 1, "no re-report across turns: {notes2:?}");
        app.shutdown();
    });
}

/// A column that crushes a declared 1-row child produces exactly one
/// startup notice, delivered through the notices signal at the next
/// phase U — the surface an app can actually render.
#[cfg(debug_assertions)]
#[test]
fn zero_collapse_notice_reaches_the_notices_lane() {
    use abstracttui::app::use_startup_notices;

    create_root(|_| {
        let size = Size::new(20, 6);
        let mut app = App::new(size);
        type NoticeSignal = abstracttui::reactive::Signal<Vec<String>>;
        let lane: Rc<RefCell<Option<NoticeSignal>>> = Rc::new(RefCell::new(None));
        let lane_in = lane.clone();
        app.mount(move |cx| {
            *lane_in.borrow_mut() = Some(use_startup_notices(cx));
            Element::new()
                .style(LayoutStyle::column())
                .child(
                    Element::new()
                        .style(LayoutStyle::default().h(600))
                        .child(text("filler"))
                        .build(),
                )
                .child(
                    Element::new()
                        .style(LayoutStyle::default().h(1))
                        .child(text("status row"))
                        .build(),
                )
                .build()
        })
        .expect("mount");

        let mut term = CaptureTerm::new(size);
        let mut driver = Driver::new(&mut app, &mut term, test_config()).expect("driver");
        // Turn 1 renders (phase L records the collapse); turn 2's phase U
        // forwards it into the notices lane.
        driver.turn(&mut app, &mut term).expect("turn 1");
        driver.turn(&mut app, &mut term).expect("turn 2");
        flush_effects();

        let signal = lane.borrow().expect("notices signal captured");
        let notes = signal.get();
        let collapse: Vec<&String> = notes
            .iter()
            .filter(|n| n.contains("collapsed to 0"))
            .collect();
        assert_eq!(
            collapse.len(),
            1,
            "exactly one collapse notice in the lane: {notes:?}"
        );
        // More turns must not repeat it (same situation, dedup holds).
        driver.turn(&mut app, &mut term).expect("turn 3");
        driver.turn(&mut app, &mut term).expect("turn 4");
        flush_effects();
        let notes2 = signal.get();
        let collapse2: Vec<&String> = notes2
            .iter()
            .filter(|n| n.contains("collapsed to 0"))
            .collect();
        assert_eq!(collapse2.len(), 1, "no re-report across turns: {notes2:?}");
        app.shutdown();
    });
}