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
//! Widget Gallery - Interactive showcase of all Revue widgets
//!
//! Run with: cargo run --example gallery
//!
//! Navigate with:
//! - Tab/Shift+Tab: Switch categories
//! - Up/Down: Navigate widgets
//! - Enter: Toggle widget demo
//! - q: Quit
use revue::prelude::*;
use revue::widget::*;
fn main() -> Result<()> {
let mut app = App::builder().build();
let gallery = Gallery::new();
app.run_with_handler(gallery, |event: &KeyEvent, state: &mut Gallery| {
state.handle_event(event)
})
}
// =============================================================================
// Gallery State
// =============================================================================
struct Gallery {
/// Current category index
category: usize,
/// Current widget index within category
widget_idx: usize,
/// Categories with their widgets
categories: Vec<Category>,
}
struct Category {
name: &'static str,
icon: &'static str,
widgets: Vec<WidgetDemo>,
}
struct WidgetDemo {
name: &'static str,
description: &'static str,
}
impl Gallery {
fn new() -> Self {
Self {
category: 0,
widget_idx: 0,
categories: vec![
Category {
name: "Basic",
icon: "□",
widgets: vec![
WidgetDemo {
name: "Text",
description: "Display styled text content",
},
WidgetDemo {
name: "Button",
description: "Clickable button with states",
},
WidgetDemo {
name: "Input",
description: "Text input field",
},
WidgetDemo {
name: "Checkbox",
description: "Boolean toggle checkbox",
},
WidgetDemo {
name: "Radio",
description: "Single selection from options",
},
WidgetDemo {
name: "Switch",
description: "iOS-style toggle switch",
},
],
},
Category {
name: "Layout",
icon: "⊞",
widgets: vec![
WidgetDemo {
name: "VStack",
description: "Vertical stack layout",
},
WidgetDemo {
name: "HStack",
description: "Horizontal stack layout",
},
WidgetDemo {
name: "Grid",
description: "CSS Grid layout",
},
WidgetDemo {
name: "Splitter",
description: "Resizable split panes",
},
WidgetDemo {
name: "Scroll",
description: "Scrollable content area",
},
WidgetDemo {
name: "Tabs",
description: "Tabbed content panels",
},
],
},
Category {
name: "Data",
icon: "▤",
widgets: vec![
WidgetDemo {
name: "Table",
description: "Data table with sorting",
},
WidgetDemo {
name: "List",
description: "Scrollable item list",
},
WidgetDemo {
name: "Tree",
description: "Hierarchical tree view",
},
WidgetDemo {
name: "DataGrid",
description: "Editable data grid",
},
WidgetDemo {
name: "VirtualList",
description: "Virtualized large list",
},
],
},
Category {
name: "Charts",
icon: "▁",
widgets: vec![
WidgetDemo {
name: "BarChart",
description: "Vertical/horizontal bars",
},
WidgetDemo {
name: "Sparkline",
description: "Inline mini chart",
},
WidgetDemo {
name: "Gauge",
description: "Circular gauge meter",
},
WidgetDemo {
name: "Heatmap",
description: "2D color intensity map",
},
],
},
Category {
name: "Feedback",
icon: "◐",
widgets: vec![
WidgetDemo {
name: "Progress",
description: "Progress bar indicator",
},
WidgetDemo {
name: "Spinner",
description: "Loading spinner",
},
WidgetDemo {
name: "Toast",
description: "Notification toast",
},
WidgetDemo {
name: "Modal",
description: "Modal dialog overlay",
},
WidgetDemo {
name: "Skeleton",
description: "Loading placeholder",
},
],
},
],
}
}
fn handle_event(&mut self, event: &KeyEvent) -> bool {
match event.key {
Key::Char('q') | Key::Escape => return false,
Key::Tab => {
if event.shift {
self.category = self.category.saturating_sub(1);
} else {
self.category = (self.category + 1).min(self.categories.len() - 1);
}
self.widget_idx = 0;
}
Key::Up => {
self.widget_idx = self.widget_idx.saturating_sub(1);
}
Key::Down => {
let max = self.categories[self.category]
.widgets
.len()
.saturating_sub(1);
self.widget_idx = (self.widget_idx + 1).min(max);
}
_ => {}
}
true
}
fn current_widget(&self) -> Option<&WidgetDemo> {
self.categories
.get(self.category)
.and_then(|c| c.widgets.get(self.widget_idx))
}
}
impl View for Gallery {
fn render(&self, ctx: &mut RenderContext) {
let cat = &self.categories[self.category];
// Main layout
vstack()
.gap(1)
// Header
.child(
vstack()
.child(Text::new("Revue Widget Gallery").bold())
.child(Text::muted("Interactive showcase of 70+ widgets")),
)
// Category tabs
.child(self.render_tabs())
// Content
.child(
hstack()
.gap(2)
.child(self.render_widget_list(cat))
.child(self.render_preview()),
)
// Footer
.child(Text::muted("[Tab] Category [↑↓] Navigate [q] Quit"))
.render(ctx);
}
}
impl Gallery {
fn render_tabs(&self) -> impl View {
let mut row = hstack().gap(2);
for (i, cat) in self.categories.iter().enumerate() {
let label = format!("{} {}", cat.icon, cat.name);
let is_active = i == self.category;
let tab = if is_active {
Text::new(format!("[{}]", label)).bold()
} else {
Text::muted(format!(" {} ", label))
};
row = row.child(tab);
}
row
}
fn render_widget_list(&self, cat: &Category) -> impl View {
let mut list = vstack();
for (i, widget) in cat.widgets.iter().enumerate() {
let is_selected = i == self.widget_idx;
let prefix = if is_selected { "▶ " } else { " " };
let line = format!("{}{}", prefix, widget.name);
let text = if is_selected {
Text::new(line).bold()
} else {
Text::new(line)
};
list = list.child(text);
}
Border::rounded()
.child(list)
.title(format!(" {} Widgets ", cat.name))
}
fn render_preview(&self) -> impl View {
if let Some(widget) = self.current_widget() {
let content = vstack()
.gap(1)
.child(Text::new(widget.name).bold())
.child(Text::muted(widget.description))
.child(Text::new(""))
.child(self.render_demo(widget.name));
Border::rounded().child(content).title(" Preview ")
} else {
Border::rounded()
.child(Text::new("Select a widget"))
.title(" Preview ")
}
}
fn render_demo(&self, name: &str) -> Box<dyn View> {
match name {
"Text" => Box::new(demo_text()),
"Button" => Box::new(demo_button()),
"Progress" => Box::new(demo_progress()),
"Spinner" => Box::new(demo_spinner()),
"Checkbox" => Box::new(demo_checkbox()),
"Switch" => Box::new(demo_switch()),
"Badge" => Box::new(demo_badge()),
"Gauge" => Box::new(demo_gauge()),
_ => Box::new(demo_placeholder(name)),
}
}
}
// =============================================================================
// Demo Widgets
// =============================================================================
fn demo_text() -> impl View {
vstack()
.child(Text::new("Normal text"))
.child(Text::new("Bold text").bold())
.child(Text::muted("Muted text"))
}
fn demo_button() -> impl View {
hstack()
.gap(2)
.child(Button::primary("Primary"))
.child(Button::new("Secondary"))
.child(Button::new("Disabled").disabled(true))
}
fn demo_progress() -> impl View {
vstack()
.gap(1)
.child(Progress::new(0.3))
.child(Progress::new(0.7))
}
fn demo_spinner() -> impl View {
hstack()
.gap(2)
.child(Spinner::new())
.child(Text::new("Loading..."))
}
fn demo_checkbox() -> impl View {
vstack()
.child(Checkbox::new("Option A").checked(true))
.child(Checkbox::new("Option B").checked(false))
.child(Checkbox::new("Option C").checked(true))
}
fn demo_switch() -> impl View {
vstack()
.child(Switch::new().on(true).label("Enabled"))
.child(Switch::new().on(false).label("Disabled"))
}
fn demo_badge() -> impl View {
hstack()
.gap(1)
.child(Badge::new("New"))
.child(Badge::new("Hot").variant(BadgeVariant::Warning))
.child(Badge::new("Error").variant(BadgeVariant::Error))
}
fn demo_gauge() -> impl View {
Gauge::new().value(0.65).label("CPU")
}
fn demo_placeholder(name: &str) -> impl View {
vstack()
.child(Text::muted(format!("{} widget demo", name)))
.child(Text::new("Coming soon..."))
}