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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
//! Widget Gallery - Comprehensive Widget Showcase
//!
//! This example demonstrates ALL available widgets in the Astrelis UI system:
//! - Text (various sizes, colors, styles)
//! - Buttons (different colors, sizes, hover states)
//! - Text Inputs (editable fields with placeholders)
//! - Containers (flexbox layouts with various styles)
//! - Rows and Columns (layout primitives)
//! - Images (texture display)
//! - Tooltips (hover information)
//!
//! This is your one-stop reference for all widget types and their styling options.
use astrelis_core::logging;
use astrelis_core::profiling::{ProfilingBackend, init_profiling, new_frame};
use astrelis_render::{Color, GraphicsContext, RenderWindow, RenderWindowBuilder, wgpu};
use astrelis_ui::UiSystem;
use astrelis_winit::{
FrameTime, WindowId,
app::{App, AppCtx, run_app},
event::{Event, EventBatch, HandleStatus},
window::{WindowDescriptor, WinitPhysicalSize},
};
use std::sync::{Arc, RwLock};
struct WidgetGalleryApp {
window: RenderWindow,
window_id: WindowId,
ui: UiSystem,
counter: Arc<RwLock<i32>>,
counter_text_id: astrelis_ui::WidgetId,
input_value_id: astrelis_ui::WidgetId,
}
fn main() {
logging::init();
init_profiling(ProfilingBackend::PuffinHttp);
run_app(|ctx| {
let graphics_ctx =
GraphicsContext::new_owned_sync().expect("Failed to create graphics context");
let window = ctx
.create_window(WindowDescriptor {
title: "Widget Gallery - All UI Components".to_string(),
size: Some(WinitPhysicalSize::new(1400.0, 900.0)),
..Default::default()
})
.expect("Failed to create window");
let window = RenderWindowBuilder::new()
.color_format(wgpu::TextureFormat::Bgra8UnormSrgb)
.with_depth_default()
.build(window, graphics_ctx.clone())
.expect("Failed to create render window");
let window_id = window.id();
let size = window.physical_size();
let mut ui = UiSystem::from_window(graphics_ctx.clone(), &window);
ui.set_viewport(window.viewport());
let counter = Arc::new(RwLock::new(0));
// Build initial UI
let (counter_text_id, input_value_id) = build_gallery_ui(
&mut ui,
size.width as f32,
size.height as f32,
counter.clone(),
);
println!("\n═══════════════════════════════════════════════════════");
println!(" 🎨 WIDGET GALLERY - All UI Components");
println!("═══════════════════════════════════════════════════════");
println!("\n SHOWCASED WIDGETS:");
println!(" • Text (headings, paragraphs, colored text)");
println!(" • Buttons (primary, secondary, danger styles)");
println!(" • Text Inputs (editable fields)");
println!(" • Containers (layout boxes with styling)");
println!(" • Rows & Columns (flexbox layouts)");
println!(" • Images (texture display)");
println!(" • Tooltips (hover information)");
println!("\n CONTROLS:");
println!(" [+/-] Buttons Increment/decrement counter");
println!("\n Interact with buttons and inputs to see them in action!");
println!("═══════════════════════════════════════════════════════\n");
tracing::info!("Widget gallery initialized");
Box::new(WidgetGalleryApp {
window,
window_id,
ui,
counter,
counter_text_id,
input_value_id,
})
});
}
fn build_gallery_ui(
ui: &mut UiSystem,
width: f32,
height: f32,
counter: Arc<RwLock<i32>>,
) -> (astrelis_ui::WidgetId, astrelis_ui::WidgetId) {
let counter_text_id = astrelis_ui::WidgetId::new("counter_text");
let input_value_id = astrelis_ui::WidgetId::new("input_value");
let theme = ui.theme().clone();
let colors = &theme.colors;
ui.build(|root| {
root.container()
.width(width)
.height(height)
.padding(30.0)
.background_color(colors.background)
.child(|root| {
root.column()
.gap(25.0)
.child(|root| {
// Header
root.container()
.child(|root| {
root.column()
.gap(8.0)
.child(|root| {
root.text("Widget Gallery")
.size(36.0)
.color(colors.text_primary)
.bold()
.build()
})
.child(|root| {
root.text(
"A comprehensive showcase of all available UI widgets",
)
.size(14.0)
.color(colors.text_secondary)
.build()
})
.build()
})
.build()
})
.child(|root| {
// Two-column layout
root.row()
.gap(25.0)
.child(|root| {
// Left column
root.column()
.gap(20.0)
.child(|root| build_text_section(root, &theme))
.child(|root| {
build_button_section(root, &theme, counter, counter_text_id)
})
.child(|root| build_input_section(root, &theme))
.build()
})
.child(|root| {
// Right column
root.column()
.gap(20.0)
.child(|root| build_container_section(root, &theme))
.child(|root| build_layout_section(root, &theme))
.child(|root| build_spacing_section(root, &theme))
.child(|root| build_tooltip_section(root, &theme))
.build()
})
.build()
})
.build()
})
.build();
});
(counter_text_id, input_value_id)
}
fn build_text_section(
root: &mut astrelis_ui::UiBuilder,
theme: &astrelis_ui::Theme,
) -> astrelis_ui::NodeId {
let colors = &theme.colors;
root.container()
.background_color(colors.surface)
.border_radius(12.0)
.padding(20.0)
.child(|root| {
root.column()
.gap(15.0)
.child(|root| {
root.text("Text Widgets")
.size(24.0)
.color(colors.info)
.bold()
.build()
})
.child(|root| {
root.text("Large Heading")
.size(28.0)
.color(colors.text_primary)
.bold()
.build()
})
.child(|root| {
root.text("Medium Heading")
.size(20.0)
.color(colors.text_primary)
.bold()
.build()
})
.child(|root| {
root.text("Regular body text with default styling")
.size(14.0)
.color(colors.text_primary)
.build()
})
.child(|root| {
root.text("Small caption text for less important info")
.size(12.0)
.color(colors.text_secondary)
.build()
})
.child(|root| {
root.row()
.gap(10.0)
.child(|root| {
root.text("Colored:")
.size(14.0)
.color(colors.text_primary)
.build()
})
.child(|root| {
root.text("Red")
.size(14.0)
.color(Color::from_rgb_u8(255, 100, 100))
.build()
})
.child(|root| {
root.text("Green")
.size(14.0)
.color(Color::from_rgb_u8(100, 255, 100))
.build()
})
.child(|root| root.text("Blue").size(14.0).color(colors.info).build())
.build()
})
.build()
})
.build()
}
fn build_button_section(
root: &mut astrelis_ui::UiBuilder,
theme: &astrelis_ui::Theme,
counter: Arc<RwLock<i32>>,
counter_text_id: astrelis_ui::WidgetId,
) -> astrelis_ui::NodeId {
let colors = &theme.colors;
let counter_value = *counter.read().unwrap();
root.container()
.background_color(colors.surface)
.border_radius(12.0)
.padding(20.0)
.child(|root| {
root.column()
.gap(15.0)
.child(|root| {
root.text("Button Widgets")
.size(24.0)
.color(colors.info)
.bold()
.build()
})
.child(|root| {
root.text("Primary Buttons")
.size(14.0)
.color(colors.text_secondary)
.build()
})
.child(|root| {
root.row()
.gap(10.0)
.child(|root| {
root.button("Primary")
.background_color(colors.primary)
.padding(12.0)
.font_size(14.0)
.build()
})
.child(|root| {
root.button("Secondary")
.background_color(colors.surface)
.padding(12.0)
.font_size(14.0)
.build()
})
.build()
})
.child(|root| {
root.text("Success / Danger Buttons")
.size(14.0)
.color(colors.text_secondary)
.build()
})
.child(|root| {
root.row()
.gap(10.0)
.child(|root| {
root.button("Success")
.background_color(colors.success)
.padding(12.0)
.font_size(14.0)
.build()
})
.child(|root| {
root.button("Danger")
.background_color(colors.error)
.padding(12.0)
.font_size(14.0)
.build()
})
.build()
})
.child(|root| {
root.text("Interactive Counter")
.size(14.0)
.color(colors.text_secondary)
.margin(10.0)
.build()
})
.child(|root| {
let counter_dec = counter.clone();
let counter_inc = counter.clone();
root.container()
.background_color(colors.surface)
.border_radius(8.0)
.padding(12.0)
.justify_content(taffy::JustifyContent::Center)
.align_items(taffy::AlignItems::Center)
.child(|root| {
root.row()
.gap(10.0)
.child(|root| {
root.button("-")
.background_color(colors.error)
.padding(8.0)
.min_width(30.0)
.font_size(16.0)
.on_click(move || {
*counter_dec.write().unwrap() -= 1;
})
.build()
})
.child(|root| {
root.text(format!("Count: {}", counter_value))
.id(counter_text_id)
.size(16.0)
.color(colors.info)
.bold()
.build()
})
.child(|root| {
root.button("+")
.background_color(colors.success)
.padding(8.0)
.min_width(30.0)
.font_size(16.0)
.on_click(move || {
*counter_inc.write().unwrap() += 1;
})
.build()
})
.build()
})
.build()
})
.build()
})
.build()
}
fn build_input_section(
root: &mut astrelis_ui::UiBuilder,
theme: &astrelis_ui::Theme,
) -> astrelis_ui::NodeId {
let colors = &theme.colors;
root.container()
.background_color(colors.surface)
.border_radius(12.0)
.padding(20.0)
.child(|root| {
root.column()
.gap(15.0)
.child(|root| {
root.text("Text Input Widgets")
.size(24.0)
.color(colors.info)
.bold()
.build()
})
.child(|root| {
root.text("Username")
.size(12.0)
.color(colors.text_secondary)
.build()
})
.child(|root| {
root.text_input("Enter your username...")
.padding(10.0)
.min_width(300.0)
.build()
})
.child(|root| {
root.text("Email")
.size(12.0)
.color(colors.text_secondary)
.build()
})
.child(|root| {
root.text_input("user@example.com")
.padding(10.0)
.min_width(300.0)
.build()
})
.child(|root| {
root.text("Message")
.size(12.0)
.color(colors.text_secondary)
.build()
})
.child(|root| {
root.text_input("Type a message...")
.padding(10.0)
.min_width(300.0)
.build()
})
.build()
})
.build()
}
fn build_container_section(
root: &mut astrelis_ui::UiBuilder,
theme: &astrelis_ui::Theme,
) -> astrelis_ui::NodeId {
let colors = &theme.colors;
root.container()
.background_color(colors.surface)
.border_radius(12.0)
.padding(20.0)
.child(|root| {
root.column()
.gap(15.0)
.child(|root| {
root.text("Container Widgets")
.size(24.0)
.color(colors.info)
.bold()
.build()
})
.child(|root| {
root.text("Styled Containers")
.size(14.0)
.color(colors.text_secondary)
.build()
})
.child(|root| {
root.container()
.background_color(colors.border)
.border_radius(8.0)
.padding(15.0)
.child(|root| {
root.text("Box with rounded corners and padding")
.size(13.0)
.color(colors.text_primary)
.build()
})
.build()
})
.child(|root| {
// Success-tinted container
root.container()
.background_color(colors.surface)
.border_color(colors.success)
.border_width(2.0)
.border_radius(8.0)
.padding(15.0)
.child(|root| {
root.text("Box with border and custom color")
.size(13.0)
.color(colors.success)
.build()
})
.build()
})
.child(|root| {
// Error-tinted container
root.container()
.background_color(colors.surface)
.border_color(colors.error)
.border_width(3.0)
.border_radius(12.0)
.padding(15.0)
.child(|root| {
root.text("Box with thick border and large radius")
.size(13.0)
.color(colors.error)
.build()
})
.build()
})
.build()
})
.build()
}
fn build_layout_section(
root: &mut astrelis_ui::UiBuilder,
theme: &astrelis_ui::Theme,
) -> astrelis_ui::NodeId {
let colors = &theme.colors;
root.container()
.background_color(colors.surface)
.border_radius(12.0)
.padding(20.0)
.child(|root| {
root.column()
.gap(15.0)
.child(|root| {
root.text("Layout Widgets")
.size(24.0)
.color(colors.info)
.bold()
.build()
})
.child(|root| {
root.text("Row Layout (Horizontal)")
.size(14.0)
.color(colors.text_secondary)
.build()
})
.child(|root| {
root.row()
.gap(10.0)
.child(|root| {
root.container()
.background_color(Color::from_rgb_u8(200, 100, 100))
.width(60.0)
.height(60.0)
.border_radius(6.0)
.build()
})
.child(|root| {
root.container()
.background_color(Color::from_rgb_u8(100, 200, 100))
.width(60.0)
.height(60.0)
.border_radius(6.0)
.build()
})
.child(|root| {
root.container()
.background_color(Color::from_rgb_u8(100, 100, 200))
.width(60.0)
.height(60.0)
.border_radius(6.0)
.build()
})
.build()
})
.child(|root| {
root.text("Column Layout (Vertical)")
.size(14.0)
.color(colors.text_secondary)
.margin(10.0)
.build()
})
.child(|root| {
root.column()
.gap(8.0)
.child(|root| {
root.container()
.background_color(Color::from_rgb_u8(255, 180, 100))
.width(150.0)
.height(30.0)
.border_radius(6.0)
.build()
})
.child(|root| {
root.container()
.background_color(Color::from_rgb_u8(180, 100, 255))
.width(150.0)
.height(30.0)
.border_radius(6.0)
.build()
})
.child(|root| {
root.container()
.background_color(Color::from_rgb_u8(100, 255, 255))
.width(150.0)
.height(30.0)
.border_radius(6.0)
.build()
})
.build()
})
.build()
})
.build()
}
fn build_spacing_section(
root: &mut astrelis_ui::UiBuilder,
theme: &astrelis_ui::Theme,
) -> astrelis_ui::NodeId {
let colors = &theme.colors;
root.container()
.background_color(colors.surface)
.border_radius(12.0)
.padding(20.0)
.child(|root| {
root.column()
.gap(15.0)
.child(|root| {
root.text("Spacing Examples")
.size(24.0)
.color(colors.info)
.bold()
.build()
})
// Asymmetric padding example
.child(|root| {
root.text("Asymmetric Padding (more horizontal)")
.size(12.0)
.color(colors.text_secondary)
.build()
})
.child(|root| {
root.container()
.background_color(colors.border)
.border_radius(8.0)
.padding_x(40.0) // More horizontal padding
.padding_y(10.0) // Less vertical padding
.child(|root| {
root.text("Horizontal emphasis")
.size(13.0)
.color(colors.text_primary)
.build()
})
.build()
})
// Card with different top/bottom padding
.child(|root| {
root.text("Card Header/Content Padding")
.size(12.0)
.color(colors.text_secondary)
.margin_top(10.0)
.build()
})
.child(|root| {
root.column()
.background_color(colors.border)
.border_radius(8.0)
.padding_top(20.0)
.padding_bottom(10.0)
.padding_x(15.0)
.gap(8.0)
.child(|root| {
root.text("Card Title")
.size(16.0)
.color(colors.text_primary)
.bold()
.build()
})
.child(|root| {
root.text("Different top/bottom padding")
.size(12.0)
.color(colors.text_secondary)
.build()
})
.build()
})
// Per-side margin example
.child(|root| {
root.text("Per-Side Margins")
.size(12.0)
.color(colors.text_secondary)
.margin_top(10.0)
.build()
})
.child(|root| {
root.container()
.background_color(colors.border)
.border_radius(8.0)
.padding(10.0)
.child(|root| {
root.container()
.background_color(colors.primary)
.border_radius(6.0)
.margin_left(30.0) // Indented from left
.margin_right(10.0)
.padding(10.0)
.child(|root| {
root.text("Indented content")
.size(12.0)
.color(Color::WHITE)
.build()
})
.build()
})
.build()
})
.build()
})
.build()
}
fn build_tooltip_section(
root: &mut astrelis_ui::UiBuilder,
theme: &astrelis_ui::Theme,
) -> astrelis_ui::NodeId {
let colors = &theme.colors;
root.container()
.background_color(colors.surface)
.border_radius(12.0)
.padding(20.0)
.child(|root| {
root.column()
.gap(15.0)
.child(|root| {
root.text("Tooltips & Overlays")
.size(20.0)
.color(colors.info)
.bold()
.build()
})
.child(|root| {
root.text("Hover Information")
.size(14.0)
.color(colors.text_secondary)
.margin(10.0)
.build()
})
.child(|root| {
// Tooltip demonstration (API only - hover functionality in development)
root.row()
.gap(10.0)
.child(|root| {
root.tooltip("This is a tooltip with helpful information")
.font_size(12.0)
.text_color(colors.text_primary)
.background_color(colors.surface)
.padding(8.0)
.build()
})
.build()
})
.child(|root| {
root.text("Note: Tooltip API is implemented. Hover-triggered display is in development.")
.size(11.0)
.color(colors.text_disabled)
.margin(10.0)
.build()
})
.child(|root| {
root.text("Future: Tooltips will show on hover with customizable delay and positioning.")
.size(11.0)
.color(colors.text_secondary)
.margin(10.0)
.build()
})
.build()
})
.build()
}
impl App for WidgetGalleryApp {
fn update(&mut self, _ctx: &mut AppCtx, _time: &FrameTime) {
new_frame();
self.ui.update(0.016);
}
fn render(&mut self, _ctx: &mut AppCtx, window_id: WindowId, events: &mut EventBatch) {
if window_id != self.window_id {
return;
}
// Handle window resize
events.dispatch(|event| {
if let Event::WindowResized(size) = event {
self.window.resized(*size);
self.ui.set_viewport(self.window.viewport());
let (counter_text_id, input_value_id) = build_gallery_ui(
&mut self.ui,
size.width as f32,
size.height as f32,
self.counter.clone(),
);
self.counter_text_id = counter_text_id;
self.input_value_id = input_value_id;
return HandleStatus::consumed();
}
HandleStatus::ignored()
});
// Handle UI events (button clicks are handled by callbacks)
self.ui.handle_events(events);
// Update counter text every frame
let counter_value = *self.counter.read().unwrap();
self.ui
.update_text(self.counter_text_id, format!("Count: {}", counter_value));
// Begin frame and render with depth buffer for proper z-ordering
let bg = self.ui.theme().colors.background;
let Some(frame) = self.window.begin_frame() else {
return; // Surface not available
};
{
let mut pass = frame
.render_pass()
.clear_color(bg)
.with_window_depth()
.clear_depth(0.0)
.label("UI")
.build();
self.ui.render(pass.wgpu_pass());
}
// Frame auto-submits on drop
}
}