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
//! Backend-agnostic app code for the Board (kanban) example
//! ([`tui_board`] / [`gtk_board`]).
//!
//! [`BoardApp`] demonstrates a three-column kanban board with cards in
//! each column, badge icons showing CI / review status, and a decision
//! hint on the selected card. It uses [`BoardModel`] for state and
//! [`Backend::draw_board`] for rendering.
//!
//! Controls:
//! - j / ↓ — move selection down
//! - k / ↑ — move selection up
//! - h / ← — jump to previous column
//! - l / → — jump to next column
//! - g — jump to first card
//! - G — jump to last card
//! - q / Esc — quit
use std::cell::RefCell;
use quadraui::{
AppLogic, Backend, BadgeStatus, BoardAction, BoardCard, BoardColumn, BoardLayout, BoardModel,
CardBadge, InteractionState, Key, MoveDir, NamedKey, Reaction, Rect, StatusBar,
StatusBarSegment, UiEvent, WidgetId,
};
pub struct BoardApp {
model: BoardModel,
last_message: String,
/// Cached board layout from the last render pass, used for click dispatch.
///
/// Wrapped in `RefCell` so that `render(&self, …)` can update it while
/// keeping the `AppLogic` signature immutable. The `BoardLayout` is set
/// every frame by `render` and read in `handle` for mouse hit-testing —
/// avoids calling `backend.draw_board` outside an `enter_frame_scope`.
last_layout: RefCell<Option<BoardLayout>>,
}
impl BoardApp {
pub fn new() -> Self {
Self {
model: demo_board(),
last_message: "j/k=move h/l=col g/G=top/bottom q=quit".into(),
last_layout: RefCell::new(None),
}
}
fn status_bar(&self) -> StatusBar {
let selected_title = self
.model
.selected_card_id
.as_ref()
.and_then(|id| {
self.model.columns.iter().find_map(|col| {
col.cards
.iter()
.find(|c| &c.id == id)
.map(|c| c.title.clone())
})
})
.unwrap_or_default();
let right_text = if selected_title.is_empty() {
" q=quit ".into()
} else {
format!(" {} │ q=quit ", selected_title)
};
StatusBar {
id: WidgetId::new("status"),
left_segments: vec![quadraui::StatusBarSegment {
text: format!(" {} ", self.last_message),
fg: quadraui::Color::rgb(255, 255, 255),
bg: quadraui::Color::rgb(40, 80, 120),
bold: false,
action_id: None,
}],
right_segments: vec![StatusBarSegment {
text: right_text,
fg: quadraui::Color::rgb(200, 200, 200),
bg: quadraui::Color::rgb(40, 80, 120),
bold: false,
action_id: None,
}],
}
}
}
impl Default for BoardApp {
fn default() -> Self {
Self::new()
}
}
impl AppLogic for BoardApp {
type AreaId = ();
fn render(&self, backend: &mut dyn Backend, _area: ()) {
let viewport = backend.viewport();
let lh = backend.line_height();
// Status bar at bottom.
let status_h = lh;
let status_rect = Rect::new(0.0, viewport.height - status_h, viewport.width, status_h);
let _ = backend.draw_status_bar_interactive(
status_rect,
&self.status_bar(),
&InteractionState::new(),
);
// Board fills the rest of the screen.
let board_rect = Rect::new(0.0, 0.0, viewport.width, viewport.height - status_h);
let layout = backend.draw_board(board_rect, &self.model);
// Cache the layout so `handle` can hit-test mouse events without
// calling `draw_board` outside a frame scope.
*self.last_layout.borrow_mut() = Some(layout);
}
fn handle(&mut self, event: UiEvent, _backend: &mut dyn Backend) -> Reaction {
match event {
UiEvent::KeyPressed {
key: Key::Char('q'),
..
}
| UiEvent::KeyPressed {
key: Key::Named(NamedKey::Escape),
..
} => Reaction::Exit,
UiEvent::KeyPressed { key, .. } => {
let action = key_to_board_action(&key);
if let Some(act) = action {
self.apply_board_action(act);
Reaction::Redraw
} else {
Reaction::Continue
}
}
UiEvent::MouseDown { position, .. } => {
use quadraui::BoardHit;
// Use the layout cached by the last `render` call. If no render
// has run yet (first event before first frame), skip hit-testing.
let hit = self
.last_layout
.borrow()
.as_ref()
.map(|l| l.hit_test(position.x, position.y))
.unwrap_or(BoardHit::Empty);
match hit {
BoardHit::Card(id) => {
self.model.selected_card_id = Some(id);
let title = self
.model
.columns
.iter()
.find_map(|col| {
col.cards
.iter()
.find(|c| c.id == id)
.map(|c| c.title.clone())
})
.unwrap_or_default();
self.last_message = format!("Selected: {}", title);
Reaction::Redraw
}
BoardHit::ColumnHeader(_) | BoardHit::Empty => Reaction::Continue,
}
}
UiEvent::WindowResized { .. } => Reaction::Redraw,
_ => Reaction::Continue,
}
}
}
impl BoardApp {
fn apply_board_action(&mut self, action: BoardAction) {
match action {
BoardAction::MoveSelection(dir) => {
self.model.move_selection(dir);
// After a horizontal column move, ensure the newly-selected
// column is scrolled into view.
if matches!(dir, MoveDir::Left | MoveDir::Right) {
self.clamp_col_scroll();
}
}
BoardAction::JumpToTop => {
self.model.jump_to_top();
}
BoardAction::JumpToBottom => {
self.model.jump_to_bottom();
}
_ => {}
}
}
/// Adjust `col_scroll_offset` so the currently-selected column is visible.
///
/// Uses the layout cached by the last `render` call to know how many
/// columns are visible. A no-op when no layout is cached yet.
fn clamp_col_scroll(&mut self) {
let Some(col_idx) = self.model.selected_col_index() else {
return;
};
// Read visible column count from the cached layout, then drop the borrow
// before mutating `self.model`.
let visible = {
let borrowed = self.last_layout.borrow();
match borrowed.as_ref() {
Some(l) => l.columns.len(),
None => return,
}
};
let offset = &mut self.model.col_scroll_offset;
if col_idx < *offset {
*offset = col_idx;
} else if visible > 0 && col_idx >= *offset + visible {
*offset = col_idx + 1 - visible;
}
}
}
fn key_to_board_action(key: &Key) -> Option<BoardAction> {
match key {
Key::Char('j') | Key::Named(NamedKey::Down) => {
Some(BoardAction::MoveSelection(MoveDir::Down))
}
Key::Char('k') | Key::Named(NamedKey::Up) => Some(BoardAction::MoveSelection(MoveDir::Up)),
Key::Char('h') | Key::Named(NamedKey::Left) => {
Some(BoardAction::MoveSelection(MoveDir::Left))
}
Key::Char('l') | Key::Named(NamedKey::Right) => {
Some(BoardAction::MoveSelection(MoveDir::Right))
}
Key::Char('g') => Some(BoardAction::JumpToTop),
Key::Char('G') => Some(BoardAction::JumpToBottom),
_ => None,
}
}
// ── Demo data ───────────────────────────────────────────────────────────────
/// Shorthand for building a [`CardBadge`] in the demo data below. The
/// board primitive doesn't assign meaning to `label` or badge order —
/// this demo happens to use single-letter workflow-step labels.
fn badge(label: &str, status: BadgeStatus) -> CardBadge {
CardBadge {
label: label.into(),
status,
}
}
fn demo_board() -> BoardModel {
let backlog = BoardColumn {
id: WidgetId::new("col:backlog"),
title: "Backlog".into(),
cards: vec![
BoardCard {
id: WidgetId::new("card:1"),
title: "Improve search indexing perf".into(),
labels: vec!["perf".into()],
badges: vec![
badge("P", BadgeStatus::Passed),
badge("W", BadgeStatus::Pending),
],
hint: None,
},
BoardCard {
id: WidgetId::new("card:2"),
title: "Add dark-mode toggle".into(),
labels: vec!["ui".into()],
badges: vec![badge("P", BadgeStatus::Running)],
hint: Some("Waiting for design sign-off".into()),
},
BoardCard {
id: WidgetId::new("card:3"),
title: "Fix memory leak in parser".into(),
labels: vec!["bug".into()],
badges: vec![],
hint: None,
},
],
scroll_offset: 0,
};
let in_progress = BoardColumn {
id: WidgetId::new("col:in-progress"),
title: "In Progress".into(),
cards: vec![
BoardCard {
id: WidgetId::new("card:4"),
title: "Refactor auth middleware".into(),
labels: vec!["refactor".into(), "security".into()],
badges: vec![
badge("P", BadgeStatus::Passed),
badge("W", BadgeStatus::Running),
badge("T", BadgeStatus::Pending),
],
hint: None,
},
BoardCard {
id: WidgetId::new("card:5"),
title: "Upgrade dependency stack".into(),
labels: vec!["deps".into()],
badges: vec![
badge("P", BadgeStatus::Passed),
badge("W", BadgeStatus::Passed),
badge("T", BadgeStatus::Blocked),
],
hint: Some("blocked: upstream async-std 2.0 compat".into()),
},
],
scroll_offset: 0,
};
let review = BoardColumn {
id: WidgetId::new("col:review"),
title: "Review".into(),
cards: vec![
BoardCard {
id: WidgetId::new("card:6"),
title: "Paginate API list endpoints".into(),
labels: vec!["api".into()],
badges: vec![
badge("P", BadgeStatus::Passed),
badge("W", BadgeStatus::Passed),
badge("T", BadgeStatus::Passed),
badge("R", BadgeStatus::Warning),
],
hint: Some("reviewer: needs integration test".into()),
},
BoardCard {
id: WidgetId::new("card:7"),
title: "Export CSV for analytics".into(),
labels: vec!["feature".into()],
badges: vec![
badge("P", BadgeStatus::Passed),
badge("W", BadgeStatus::Passed),
badge("T", BadgeStatus::Passed),
badge("R", BadgeStatus::Running),
],
hint: None,
},
],
scroll_offset: 0,
};
BoardModel {
id: WidgetId::new("board:main"),
columns: vec![backlog, in_progress, review],
selected_card_id: Some(WidgetId::new("card:4")),
col_scroll_offset: 0,
}
}