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
//! widgets — the widget gallery: every shipped widget, live states.
//!
//! Demonstrates: the §3 style guide in motion — Tab moves real keyboard
//! focus (buttons/input/list show the selection-pair focus state), hover
//! works with the mouse, a disabled button sits in text_faint outside the
//! focus order, and the whole gallery restyles on theme cycle.
//!
//! Keys: Tab/Shift+Tab focus · Enter/Space activate · arrows in list/tabs
//! · Ctrl+T cycle theme · F2 spin · q quit (Ctrl+C always).
//!
//! Docs: docs/api.md § "widgets — the built-in library".
//!
//! OWNER: DESIGN (gallery); interactive widgets are REACT's, visual ones
//! DESIGN's — this file exercises both through the public API only.
mod common;
use abstracttui::prelude::*;
use abstracttui::theme::themes;
use abstracttui::widgets::{SpinnerKind, TitleAlign, Tone};
const BORDER_KINDS: [(BorderKind, &str); 4] = [
(BorderKind::Plain, "plain"),
(BorderKind::Rounded, "rounded"),
(BorderKind::Double, "double"),
(BorderKind::Heavy, "heavy"),
];
fn main() -> abstracttui::base::Result<()> {
if !abstracttui::term::have_tty() {
println!("widgets: needs an interactive terminal — skipping cleanly");
return Ok(());
}
let mut app = App::new(Size::new(100, 32));
let quitter = app.quitter();
app.mount(move |cx| {
let theme = use_theme(cx);
// Durable state: created ONCE on the mount scope, bound into the
// widgets by signal — so list selection, typed text and counters
// survive the theme-rebuild of the widget tree below.
let clicks = cx.signal(0u32);
let name = cx.signal(String::new());
let pick = cx.signal(0usize);
let tab = cx.signal(0usize);
let spin = cx.signal(0u64);
let theme_ix = cx.signal(0usize);
// Alive-mask for the closable panels row (app-owned: whether a
// panel can close is the app's decision — and so is restoring).
let panes = cx.signal(0b111u32);
Element::new()
.style(LayoutStyle::column().padding(Edges::all(1)).gap(1))
.shortcut(KeyChord::plain(Key::Char('q')), move |_| quitter.quit())
.shortcut(KeyChord::new(Mods::CTRL, Key::Char('t')), move |_| {
theme_ix.update(|i| *i = (*i + 1) % themes().len());
set_theme_by_id(themes()[theme_ix.get_untracked()].id);
})
.shortcut(KeyChord::plain(Key::F(2)), move |_| {
spin.update(|t| *t += 1)
})
// ONE outer Dyn reads the theme: a switch rebuilds the widget
// tree with freshly resolved tokens (interactive widgets
// resolve tokens at element() time — damage contract §5).
.child(dyn_view(LayoutStyle::default().grow(1.0), move || {
let t = theme.get().tokens;
let label = theme.get().label;
Element::new()
.style(LayoutStyle::column().gap(1))
// Header: logo + active-theme badge. shrink(0.0):
// incompressible chrome — without it, overflow
// pressure from the tab panels can silently steal
// the tagline row (§"A row vanishes" recipe).
.child(
Element::new()
.style(LayoutStyle::row().gap(2).h(2).shrink(0.0))
.child(Logo::new().tagline(true).element(&t).build())
.child(Badge::new(label).tone(Tone::Accent).element(&t).build())
.build(),
)
.child(
Tabs::new()
.tab("interactive", {
move || {
interactive_panel(
cx,
&theme.get().tokens,
clicks,
name,
pick,
panes,
)
}
})
.tab("visual", {
move || visual_panel(cx, &theme.get().tokens, spin.get())
})
.active(tab)
.layout(LayoutStyle::column().grow(1.0))
.element(cx, &t)
.build(),
)
.child(text(
"tab focus · enter/space activate · ctrl+t theme · f2 spin · q quit",
))
.child(
Element::new()
.style_signal(move || {
let vp = abstracttui::app::current_viewport();
if vp.w >= common::MIN_SIZE.w && vp.h >= common::MIN_SIZE.h {
LayoutStyle::default().w(0).h(0).shrink(0.0)
} else {
common::overlay_layout()
}
})
.draw(move |canvas, rect| {
let vp = abstracttui::app::current_viewport();
if vp.w >= common::MIN_SIZE.w && vp.h >= common::MIN_SIZE.h {
return;
}
let t = theme.get().tokens;
common::too_small(canvas, rect, common::MIN_SIZE, &t);
})
.build(),
)
.build()
}))
.build()
})?;
app.run()
}
/// REACT's widgets under the §3 state table: focus = selection pair,
/// hover = accent ink, disabled = text_faint outside the focus order —
/// plus the panel ✕ (click it: the survivors re-flex into the space).
fn interactive_panel(
cx: Scope,
t: &TokenSet,
clicks: Signal<u32>,
name: Signal<String>,
pick: Signal<usize>,
panes: Signal<u32>,
) -> View {
let items: Vec<String> = [
"abstract-dark",
"observer-night",
"catppuccin-mocha",
"rose-pine",
"tokyo-night",
"nord",
"one-dark",
"dracula",
"monokai",
"gruvbox",
]
.iter()
.map(|s| s.to_string())
.collect();
// The fixed rows carry shrink(0.0): under height pressure the LIST
// block below gives (it scrolls honestly) instead of these rows
// silently collapsing.
Element::new()
.style(LayoutStyle::column().gap(1).padding(Edges::all(1)))
.child(
Element::new()
.style(LayoutStyle::row().gap(2).h(1).shrink(0.0))
.child(
Button::new("click me")
.on_click(move || clicks.update(|c| *c += 1))
.element(cx, t)
.build(),
)
.child(
Button::new("disabled")
.disabled(true)
.element(cx, t)
.build(),
)
.child(dyn_view(LayoutStyle::default().h(1), move || {
text(format!("clicks: {}", clicks.get()))
}))
.build(),
)
.child(
TextInput::new()
.value(name)
.placeholder("type your name, Enter submits…")
.layout(LayoutStyle::default().w(44).h(1).shrink(0.0))
.element(cx, t)
.build(),
)
.child(dyn_view(
LayoutStyle::default().h(1).shrink(0.0),
move || {
text(format!("hello, {}!", {
let n = name.get();
if n.is_empty() {
"stranger".to_string()
} else {
n
}
}))
},
))
.child(closable_panes_row(cx, t, panes))
.child(
Block::new()
.title("list — arrows move, wheel scrolls")
.fill(t.surface)
.layout(LayoutStyle::column().grow(1.0).min_h(6))
.child(
List::new(items)
.selection(pick)
.layout(LayoutStyle::default().grow(1.0))
.element(cx, t)
.build(),
)
.element(t)
.build(),
)
.build()
}
/// The panel ✕: each pane opts into
/// `Block::on_close`, the callback clears its bit, the row's dyn
/// re-renders and the SURVIVORS RE-FLEX into the freed space. Closing
/// is the app's decision — and so is coming back (the restore button).
fn closable_panes_row(cx: Scope, t: &TokenSet, panes: Signal<u32>) -> View {
let t = *t;
dyn_view(LayoutStyle::default().h(4).shrink(0.0), move || {
let mask = panes.get();
if mask == 0 {
return Element::new()
.style(LayoutStyle::row().gap(2).h(4))
.child(
Button::new("restore panes")
.on_click(move || panes.set(0b111))
.element(cx, &t)
.build(),
)
.child(text("all panes closed — the ✕ freed their space"))
.build();
}
let mut row = Element::new().style(LayoutStyle::row().gap(1).h(4));
for (i, label) in ["pane 1 — click ✕", "pane 2", "pane 3"].iter().enumerate() {
if mask & (1 << i) == 0 {
continue;
}
row = row.child(
Block::new()
.border(BorderKind::Rounded)
.title(*label)
.fill(t.surface)
.on_close(move || panes.update(|m| *m &= !(1 << i)))
.layout(LayoutStyle::column().grow(1.0))
.child(text("survivors re-flex"))
.element(&t)
.build(),
);
}
row.build()
})
}
/// DESIGN's visual widgets, inside a Scroll so overflow is explorable.
fn visual_panel(cx: Scope, t: &TokenSet, spin_frame: u64) -> View {
let mut blocks = Element::new().style(LayoutStyle::row().gap(1).h(5));
for (i, (kind, name)) in BORDER_KINDS.iter().enumerate() {
blocks = blocks.child(
Block::new()
.border(*kind)
.title(*name)
.title_align(TitleAlign::Left)
.focused(i == 1) // the rounded one wears the focus ring
.fill(t.surface)
.layout(LayoutStyle::column().grow(1.0))
.child(text(if i == 1 { "focused" } else { "" }))
.element(t)
.build(),
);
}
let mut badges = Element::new().style(LayoutStyle::row().gap(1).h(1));
for (tone, name) in [
(Tone::Accent, "accent"),
(Tone::Ok, "ok"),
(Tone::Warn, "warn"),
(Tone::Error, "error"),
(Tone::Info, "info"),
(Tone::Muted, "muted"),
] {
badges = badges.child(Badge::new(name).tone(tone).element(t).build());
}
let mut spinners = Element::new().style(LayoutStyle::row().gap(3).h(1));
for (kind, name) in [
(SpinnerKind::Dots, "dots"),
(SpinnerKind::Braille, "braille"),
(SpinnerKind::Line, "line"),
] {
spinners = spinners.child(
Spinner::new()
.kind(kind)
.frame(spin_frame)
.label(name)
.element(t)
.build(),
);
}
let _ = cx;
// Plain column — the panel's 18 rows fit the tab at the demo size,
// and the interactive tab's List already demonstrates scrolling.
// (A Scroll wrapper here rendered only its bar: scroll content
// that is a plain element tree currently solves to zero width —
// see reviews/wave12/visual-to-code-handoff.md §2.)
Element::new()
.style(LayoutStyle::column().gap(1).padding(Edges::all(1)))
.child(blocks.build())
.child(badges.build())
.child(Separator::horizontal().label("progress").element(t).build())
.child(Progress::new(0.35).element(t).build())
.child(Progress::new(0.72).ramp(true).element(t).build())
.child(Progress::new(0.93).ramp(true).element(t).build())
.child(
Separator::horizontal()
.label("spinners — f2 advances")
.element(t)
.build(),
)
.child(spinners.build())
.build()
}