photon-ui 0.2.0

Blazing fast minimal TUI
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
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
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
//! Photon UI — Comprehensive Multi-Page Demo
//!
//! Run with:
//! ```bash
//! cargo run --example demo
//! ```
//!
//! Showcases **every** Photon UI component across five pages:
//!
//! | Page | Components |
//! |------|-----------|
//! | 1    | Text, TruncatedText, Box, Markdown, Spacer + Layout primitives |
//! | 2    | Input, Editor, SelectList, SettingsList |
//! | 3    | Loader, CancellableLoader, Overlay |
//! | 4    | Layout engine, Theme + Button, Panel |
//! | 5    | Full dashboard — all components |
//!
//! # Keybindings
//!
//! | Key | Action |
//! |-----|--------|
//! | `1`–`5` | Switch demo page |
//! | `Tab` / `Shift+Tab` | Cycle focus |
//! | `t` | Toggle light/dark theme (on page 4) |
//! | `q` | Quit |
//! | `Ctrl+C` | Quit |
//!
//! Page-specific bindings are shown on each page.

use std::{
    cell::RefCell,
    collections::HashMap,
    rc::Rc,
    time::Duration,
};

use crossterm::event::{
    KeyCode,
    KeyModifiers,
};
use photon_ui::{
    Anchor,
    Event,
    InputResult,
    Overlay,
    OverlayConstraints,
    OverlayPosition,
    RenderError,
    Rendered,
    TUI,
    Terminal,
    components::{
        Box as BoxComponent,
        Breadcrumbs,
        Button,
        CancellableLoader,
        Div,
        Divider,
        Editor,
        Header,
        ImageWidget,
        Input,
        Loader,
        Markdown,
        Modal,
        Panel,
        ProgressBar,
        Segment,
        SelectList,
        SettingsList,
        Sidebar,
        SidebarItem,
        Spacer,
        StatusBar,
        Table,
        Tabs,
        Text,
        TreeNode,
        TreeView,
        TruncatedText,
        table::{
            Column,
            Row,
        },
    },
    layout::{
        Border,
        Constraint,
        Direction,
        Flex,
        Margin,
        Offset,
        Position,
        Rect,
        Size,
        Spacing,
        layout::Layout,
    },
    terminal::ProcessTerminal,
    theme::Theme,
};

const DEMO_MARKDOWN: &str = r#"# Photon UI

A **Rust** TUI library with:

- *Bold*, *italic*, and `inline code`
- Headings and paragraphs
- CommonMark support via pulldown-cmark
"#;

/// Wrapper that lets us tick a shared Loader while it's mounted in the TUI.
struct SharedLoader(Rc<RefCell<Loader>>);

impl photon_ui::Component for SharedLoader {
    fn render(&self, width: u16) -> Result<Rendered, RenderError> {
        self.0.borrow().render(width)
    }
}

/// Wrapper that lets us tick a shared CancellableLoader while it's mounted.
struct SharedCancellableLoader(Rc<RefCell<CancellableLoader>>);

impl photon_ui::Component for SharedCancellableLoader {
    fn render(&self, width: u16) -> Result<Rendered, RenderError> {
        self.0.borrow().render(width)
    }

    fn handle_input(&mut self, event: &Event) -> InputResult {
        self.0.borrow_mut().handle_input(event)
    }
}

/// A panel that fills its assigned rect with a solid background color.
struct ColoredPanel {
    label: String,
    bg: u8, // ANSI background color code (40-47)
}

impl ColoredPanel {
    fn new(label: &str, bg: u8) -> Self {
        Self {
            label: label.to_string(),
            bg,
        }
    }
}

impl photon_ui::Component for ColoredPanel {
    fn render(&self, width: u16) -> Result<Rendered, RenderError> {
        let rect = photon_ui::layout::Rect::new(0, 0, width, 1);
        self.render_rect(rect)
    }

    fn render_rect(&self, rect: photon_ui::layout::Rect) -> Result<Rendered, RenderError> {
        let mut lines = Vec::new();
        let label = format!(" {} ", self.label);
        let w = rect.width as usize;
        let first = format!("\x1b[{}m{:width$}\x1b[0m", self.bg, label, width = w);
        lines.push(first);
        for _ in 1..rect.height {
            lines.push(format!("\x1b[{}m{:width$}\x1b[0m", self.bg, "", width = w));
        }
        Ok(Rendered {
            lines,
            cursor: None,
            images: Vec::new(),
        })
    }
}

/// A component that renders a visual demonstration of layout primitives.
struct LayoutDemo;

impl photon_ui::Component for LayoutDemo {
    fn render(&self, width: u16) -> Result<Rendered, RenderError> {
        let mut screen = Rendered::empty();

        // Fill screen with blank lines up to our demo height
        let demo_height = 18u16;
        for _ in 0..demo_height {
            screen.lines.push("".to_string());
        }

        // ── Panel 1: Rect grid using blit_into_rect ──────────────────────
        let grid = Rect::new(0, 0, width / 2, 8);
        let top_left = Rect::new(grid.x, grid.y, grid.width / 2, grid.height / 2);
        let top_right = Rect::new(
            grid.x + grid.width / 2,
            grid.y,
            grid.width / 2,
            grid.height / 2,
        );
        let bot_left = Rect::new(
            grid.x,
            grid.y + grid.height / 2,
            grid.width / 2,
            grid.height / 2,
        );
        let bot_right = Rect::new(
            grid.x + grid.width / 2,
            grid.y + grid.height / 2,
            grid.width / 2,
            grid.height / 2,
        );

        let tl = Rendered {
            lines: vec![" TopLeft ".into(), format!(" {:?} ", top_left)],
            cursor: None,
            images: vec![],
        };
        tl.blit_into_rect(&mut screen, top_left);

        let tr = Rendered {
            lines: vec![" TopRight ".into(), format!(" {:?} ", top_right)],
            cursor: None,
            images: vec![],
        };
        tr.blit_into_rect(&mut screen, top_right);

        let bl = Rendered {
            lines: vec![
                " BotLeft ".into(),
                format!(" w:{} h:{} ", bot_left.width, bot_left.height),
            ],
            cursor: None,
            images: vec![],
        };
        bl.blit_into_rect(&mut screen, bot_left);

        let br = Rendered {
            lines: vec![" BotRight ".into(), format!(" area:{} ", bot_right.area())],
            cursor: None,
            images: vec![],
        };
        br.blit_into_rect(&mut screen, bot_right);

        // ── Panel 2: Margin inner/outer demo ─────────────────────────────
        let margin_rect = Rect::new(width / 2 + 1, 0, width.saturating_sub(width / 2 + 1), 8);
        let outer_label = Rendered {
            lines: vec![" Margin Demo ".into(), format!(" outer: {} ", margin_rect)],
            cursor: None,
            images: vec![],
        };
        outer_label.blit_into_rect(&mut screen, margin_rect);

        let inner = margin_rect.inner(Margin::new(2, 1));
        let inner_label = Rendered {
            lines: vec![" INNER ".into(), format!(" {:?} ", inner)],
            cursor: None,
            images: vec![],
        };
        inner_label.blit_into_rect(&mut screen, inner);

        // ── Panel 3: Position + Offset ───────────────────────────────────
        let pos_y = 9u16;
        let pos_rect = Rect::new(0, pos_y, width / 2, 4);
        let p = Position::new(5, 2);
        let o = Offset::new(3, -1);
        let moved = p + o;

        let pos_text = Rendered {
            lines: vec![
                " Position + Offset ".into(),
                format!(" p = {:?} ", p),
                format!(" o = {:?} ", o),
                format!(" p + o = {:?} ", moved),
            ],
            cursor: None,
            images: vec![],
        };
        pos_text.blit_into_rect(&mut screen, pos_rect);

        // ── Panel 4: Size + Constraint types ─────────────────────────────
        let size_y = 9u16;
        let size_rect = Rect::new(
            width / 2 + 1,
            size_y,
            width.saturating_sub(width / 2 + 1),
            4,
        );
        let s = Size::new(width / 3, 3);
        let constraints = vec![
            Constraint::Length(10),
            Constraint::Min(5),
            Constraint::Max(20),
            Constraint::Percentage(50),
        ];

        let size_text = Rendered {
            lines: vec![
                " Size & Constraints ".into(),
                format!(" size = {} (area={}) ", s, s.area()),
                format!(" {:?} ", constraints[0]),
                format!(" {:?} ", constraints[1]),
            ],
            cursor: None,
            images: vec![],
        };
        size_text.blit_into_rect(&mut screen, size_rect);

        // ── Panel 5: Direction, Flex, Spacing ────────────────────────────
        let meta_y = 14u16;
        let meta_rect = Rect::new(0, meta_y, width, 4);
        let dir = Direction::Horizontal;
        let flex = Flex::Start;
        let spacing = Spacing::Space(2);

        let meta_text = Rendered {
            lines: vec![
                " Direction / Flex / Spacing ".into(),
                format!(
                    " dir={:?}  perp={:?} | flex={:?} legacy={} | spacing={:?} ",
                    dir,
                    dir.perpendicular(),
                    flex,
                    flex.is_legacy(),
                    spacing
                ),
                format!(
                    " Rect rows:{} cols:{} positions:{} ",
                    grid.rows().count(),
                    grid.columns().count(),
                    grid.positions().count()
                ),
                " blit_into_rect compositing active → ".into(),
            ],
            cursor: None,
            images: vec![],
        };
        meta_text.blit_into_rect(&mut screen, meta_rect);

        Ok(screen)
    }
}

struct DemoApp {
    tui: TUI,
    page: usize,

    // Page 2: Input / Editor state
    input_text: String,
    editor_text: String,
    input_vim: bool,
    editor_vim: bool,

    // Page 3: List state
    list_selected: usize,
    settings_selected: usize,
    settings_values: Vec<bool>,

    // Page 3: Loader / Overlay state
    loader: Rc<RefCell<Loader>>,
    cancellable: Rc<RefCell<CancellableLoader>>,
    show_overlay: bool,

    // Page 4: Design system sub-page (false = buttons/panels, true = layout engine)
    design_subpage: bool,
}

impl DemoApp {
    fn new(tui: TUI) -> Self {
        let mut app = Self {
            tui,
            page: 1,
            input_text: String::new(),
            editor_text: "Hello, Photon UI!\nThis is the multi-line editor.".into(),
            input_vim: false,
            editor_vim: false,
            list_selected: 0,
            settings_selected: 0,
            settings_values: vec![true, false, true],
            loader: Rc::new(RefCell::new(Loader::new(
                "Loading...",
                Some("\x1b[36m".into()),
                None,
            ))),
            cancellable: Rc::new(RefCell::new(CancellableLoader::new(
                "Background task running...",
                Some("\x1b[33m".into()),
                Some("\x1b[90m".into()),
            ))),
            show_overlay: false,
            design_subpage: false,
        };
        app.load_page();
        app
    }

    fn load_page(&mut self) {
        self.tui.reset();

        // Header with page indicator (skip on pages 4-5 where layout owns all children)
        if self.page != 4 && self.page != 5 {
            let header = format!(
                " Photon UI Demo  |  Page {}/5  |  1-5=pages  Tab=focus  q=quit ",
                self.page
            );
            self.tui
                .mount(std::boxed::Box::new(Text::new(&header, 0, 0)));
            self.tui.mount(std::boxed::Box::new(Spacer::new(1)));
        }

        match self.page {
            | 1 => self.load_page_basics(),
            | 2 => self.load_page_input_and_lists(),
            | 3 => self.load_page_dynamic(),
            | 4 => self.load_page_design_system(),
            | 5 => self.load_page_dashboard(),
            | _ => {},
        }
    }

    fn load_page_basics(&mut self) {
        // ── Text components ──
        self.tui.mount(std::boxed::Box::new(Text::new(
            "Text component with pad_x=2, pad_y=1:",
            0,
            0,
        )));
        self.tui.mount(std::boxed::Box::new(Text::new(
            "  Indented content here",
            2,
            1,
        )));
        self.tui.mount(std::boxed::Box::new(Spacer::new(1)));

        self.tui.mount(std::boxed::Box::new(Text::new(
            "TruncatedText (narrow terminal will ellipsis):",
            0,
            0,
        )));
        self.tui.mount(std::boxed::Box::new(TruncatedText::new(
            "This is a very long line that will be truncated with an ellipsis if the terminal is not wide enough to display it all",
            2,
            0,
        )));
        self.tui.mount(std::boxed::Box::new(Spacer::new(1)));

        self.tui.mount(std::boxed::Box::new(Text::new(
            "Box with blue background:",
            0,
            0,
        )));
        self.tui.mount(std::boxed::Box::new(
            BoxComponent::new(2).with_background(|line, _w| format!("\x1b[44m{}\x1b[0m", line)),
        ));
        self.tui.mount(std::boxed::Box::new(Spacer::new(1)));

        self.tui
            .mount(std::boxed::Box::new(Text::new("Markdown rendering:", 0, 0)));
        self.tui
            .mount(std::boxed::Box::new(Markdown::new(DEMO_MARKDOWN)));
        self.tui.mount(std::boxed::Box::new(Spacer::new(1)));

        self.tui.mount(std::boxed::Box::new(Text::new(
            "Layout Primitives — blit_into_rect demo:",
            0,
            0,
        )));
        self.tui.mount(std::boxed::Box::new(LayoutDemo));
    }

    fn load_page_input_and_lists(&mut self) {
        // ── Input ──
        let help = if self.input_vim {
            "Input: vim mode (i=insert, Esc=normal, h/l=move, x=delete) | v=toggle mode"
        } else {
            "Input: Emacs mode (Ctrl+A=start, Ctrl+E=end, Ctrl+K=kill, Ctrl+Y=yank) | v=toggle mode"
        };
        self.tui.mount(std::boxed::Box::new(Text::new(help, 0, 0)));

        let mut input = Input::new();
        input.set_text(&self.input_text);
        if self.input_vim {
            input.set_vim_mode_enabled(true);
        }
        self.tui.mount(std::boxed::Box::new(input));
        self.tui.mount(std::boxed::Box::new(Spacer::new(1)));

        let editor_help = if self.editor_vim {
            "Editor: vim mode (i=insert, Esc=normal, dd=delete line, yy=yank line) | V=toggle mode"
        } else {
            "Editor: Emacs mode (Ctrl+A=start, Ctrl+E=end, Ctrl+K=kill line) | V=toggle mode"
        };
        self.tui
            .mount(std::boxed::Box::new(Text::new(editor_help, 0, 0)));

        let mut editor = Editor::new();
        editor.set_text(&self.editor_text);
        if self.editor_vim {
            editor.set_vim_mode_enabled(true);
        }
        self.tui.mount(std::boxed::Box::new(editor));
        self.tui.mount(std::boxed::Box::new(Spacer::new(1)));

        // ── Lists ──
        self.tui.mount(std::boxed::Box::new(Text::new(
            "SelectList — j/k or arrows to navigate, Enter to select:",
            0,
            0,
        )));

        let mut list = SelectList::new(
            vec![
                "Rust programming language".into(),
                "Python scripting".into(),
                "TypeScript web dev".into(),
                "Go systems programming".into(),
                "Zig low-level".into(),
                "C++ game engines".into(),
                "Haskell functional".into(),
            ],
            4,
        );
        list.set_selected(self.list_selected);
        self.tui.mount(std::boxed::Box::new(list));
        self.tui.mount(std::boxed::Box::new(Spacer::new(1)));

        self.tui.mount(std::boxed::Box::new(Text::new(
            "SettingsList — j/k or arrows, Enter/Space to toggle:",
            0,
            0,
        )));

        let mut settings = SettingsList::new(vec![
            ("Enable dark mode".into(), self.settings_values[0]),
            ("Show line numbers".into(), self.settings_values[1]),
            ("Auto-save on exit".into(), self.settings_values[2]),
        ]);
        settings.set_selected(self.settings_selected);
        self.tui.mount(std::boxed::Box::new(settings));
    }

    fn load_page_dynamic(&mut self) {
        self.tui.mount(std::boxed::Box::new(Text::new(
            "Loader (auto-ticking spinner):",
            0,
            0,
        )));
        self.tui
            .mount(std::boxed::Box::new(SharedLoader(self.loader.clone())));
        self.tui.mount(std::boxed::Box::new(Spacer::new(1)));

        self.tui.mount(std::boxed::Box::new(Text::new(
            "CancellableLoader — press Ctrl+C to cancel:",
            0,
            0,
        )));
        self.tui.mount(std::boxed::Box::new(SharedCancellableLoader(
            self.cancellable.clone(),
        )));
        self.tui.mount(std::boxed::Box::new(Spacer::new(1)));

        let overlay_hint = if self.show_overlay {
            "Overlay: ACTIVE  |  Press 'o' to close"
        } else {
            "Overlay: hidden  |  Press 'o' to open a centered popup"
        };
        self.tui
            .mount(std::boxed::Box::new(Text::new(overlay_hint, 0, 0)));

        if self.show_overlay {
            self.tui.add_overlay(Overlay {
                content: std::boxed::Box::new(Text::new(
                    "  Overlay Popup! Press 'o' to close  ",
                    0,
                    0,
                )),
                position: OverlayPosition::Anchor(Anchor::Center),
                constraints: OverlayConstraints {
                    min_width: 30,
                    max_height: 3,
                    margin: 2,
                    offset_x: 0,
                    offset_y: 0,
                    visible: None,
                },
            });
        }
    }

    fn load_page_design_system(&mut self) {
        if self.design_subpage {
            // ── Layout engine demo ──
            self.tui.set_layout(Layout::vertical([
                Constraint::Length(4),
                Constraint::Min(3),
                Constraint::Length(4),
            ]));
            self.tui.mount(Box::new(ColoredPanel::new(
                "Photon UI Demo  |  Page 4/4  |  l=toggle  Tab=focus  q=quit",
                44,
            )));
            self.tui.mount(Box::new(ColoredPanel::new(
                "Middle (Min 3) — fills remaining",
                42,
            )));
            self.tui
                .mount(Box::new(ColoredPanel::new("Bottom panel (Length 4)", 41)));
        } else {
            // ── Buttons ──
            let theme_name = match Theme::current() {
                | Theme::Light => "Light",
                | Theme::Dark => "Dark",
            };
            self.tui.mount(Box::new(Text::new(
                &format!(
                    "Beam Design System  |  Theme: {}  |  Press 't' to toggle, 'l' for layout demo",
                    theme_name
                ),
                0,
                0,
            )));
            self.tui.mount(Box::new(Spacer::new(1)));

            self.tui.mount(Box::new(Text::new("Primary:", 0, 0)));
            self.tui.mount(Box::new(Button::primary("Primary button")));
            self.tui.mount(Box::new(Spacer::new(1)));

            self.tui.mount(Box::new(Text::new("Dark:", 0, 0)));
            self.tui.mount(Box::new(Button::dark("Dark button")));
            self.tui.mount(Box::new(Spacer::new(1)));

            self.tui.mount(Box::new(Text::new("Cream:", 0, 0)));
            self.tui.mount(Box::new(Button::cream("Cream button")));
            self.tui.mount(Box::new(Spacer::new(1)));

            self.tui.mount(Box::new(Text::new("Ghost:", 0, 0)));
            self.tui.mount(Box::new(Button::ghost("Ghost button")));
            self.tui.mount(Box::new(Spacer::new(1)));

            self.tui.mount(Box::new(Text::new("Text:", 0, 0)));
            self.tui.mount(Box::new(Button::text("Text button")));
            self.tui.mount(Box::new(Spacer::new(1)));

            // ── Panels ──
            self.tui.mount(Box::new(
                Panel::new()
                    .title("Rounded")
                    .lines(vec!["Default rounded corners".into(), "╭─╮ │ │ ╰─╯".into()]),
            ));
            self.tui.mount(Box::new(Spacer::new(1)));

            self.tui.mount(Box::new(
                Panel::new()
                    .border(photon_ui::layout::Border::THIN)
                    .title("Thin")
                    .lines(vec![
                        "Sharp corners with thin lines".into(),
                        "┌─┐ │ │ └─┘".into(),
                    ]),
            ));
        }
    }

    fn load_page_dashboard(&mut self) {
        self.tui.set_layout(Layout::vertical([
            Constraint::Length(1), // Header
            Constraint::Length(1), // Breadcrumbs
            Constraint::Min(10),   // DashboardBody
            Constraint::Length(1), // StatusBar
        ]));

        self.tui
            .mount(Box::new(Header::new("Dashboard Demo").action("q:quit")));
        self.tui.mount(Box::new(Breadcrumbs::new(vec![
            "Home",
            "Dashboard",
            "Overview",
        ])));

        // ── DashboardBody: Sidebar + MainContent ──
        let mut dashboard_body = Div::new(Layout::horizontal([
            Constraint::Length(16), // Sidebar
            Constraint::Min(10),    // MainContent
        ]));

        let mut sidebar_column = Div::new(Layout::vertical([
            Constraint::Length(4), // Nav items
            Constraint::Min(1),    // Collapsible tags panel
        ]));
        let sidebar = Sidebar::new(vec![
            SidebarItem::new("Overview").icon("📊"),
            SidebarItem::new("Files").icon("📁"),
            SidebarItem::new("Settings").icon("⚙️"),
            SidebarItem::new("Profile").icon("👤"),
        ]);
        sidebar_column.push(Box::new(sidebar));

        let tags_panel = Div::new(Layout::vertical([
            Constraint::Length(1),
            Constraint::Length(1),
            Constraint::Length(1),
        ]))
        .border(Border::ROUNDED)
        .title("Tags")
        .collapsible(true)
        .child(Box::new(Text::new("rust", 0, 0)))
        .child(Box::new(Text::new("tui", 0, 0)))
        .child(Box::new(Text::new("cli", 0, 0)));
        sidebar_column.push(Box::new(tags_panel));
        dashboard_body.push(Box::new(sidebar_column));

        // ── MainContent (vertical) with Dividers ──
        // Total = 24 lines. Each component gets exactly the height it needs
        // so nothing is clipped.
        let mut main_content = Div::new(Layout::vertical([
            Constraint::Length(1), // Tabs
            Constraint::Length(1), // Divider
            Constraint::Length(1), // ProgressBar row
            Constraint::Length(1), // Divider
            Constraint::Length(4), // DataRow  (Table 2 rows + header + sep = 4)
            Constraint::Length(1), // Divider
            Constraint::Length(2), // FormsRow
            Constraint::Length(1), // Divider
            Constraint::Length(3), // ListsRow (SelectList 3 visible)
            Constraint::Length(1), // Divider
            Constraint::Length(1), // ButtonsRow
            Constraint::Length(1), // Divider
            Constraint::Length(5), // ContentRow (Panel top + 3 lines + bottom = 5)
            Constraint::Length(1), // Divider
            Constraint::Length(1), // SystemRow
        ]));

        main_content.push(Box::new(Tabs::new(vec!["Overview", "Resources", "Logs"])));
        main_content.push(Box::new(Divider::horizontal()));

        // ProgressBar row
        let mut progress_row = Div::new(Layout::horizontal([
            Constraint::Percentage(50),
            Constraint::Percentage(50),
        ]));
        progress_row.push(Box::new(ProgressBar::new("CPU", 0.45).width(15)));
        progress_row.push(Box::new(ProgressBar::new("RAM", 1.0).width(15)));
        main_content.push(Box::new(progress_row));
        main_content.push(Box::new(Divider::horizontal().labeled("Data")));

        // DataRow: Table + TreeView
        let mut data_row = Div::new(Layout::horizontal([
            Constraint::Percentage(50),
            Constraint::Percentage(50),
        ]));
        let table = Table::new(
            vec![
                Column::new("name", "Name").width(15),
                Column::new("status", "Status").width(10),
                Column::new("size", "Size").width(8),
            ],
            vec![
                Row::new(HashMap::from([
                    ("name".to_string(), "src/main.rs".to_string()),
                    ("status".to_string(), "active".to_string()),
                    ("size".to_string(), "12.4KB".to_string()),
                ])),
                Row::new(HashMap::from([
                    ("name".to_string(), "Cargo.toml".to_string()),
                    ("status".to_string(), "active".to_string()),
                    ("size".to_string(), "1.2KB".to_string()),
                ])),
            ],
        );
        data_row.push(Box::new(table));
        let tree = TreeView::new(vec![
            TreeNode::new("src")
                .child(TreeNode::new("main.rs"))
                .child(TreeNode::new("lib.rs")),
            TreeNode::new("tests").child(TreeNode::new("integration.rs")),
        ]);
        data_row.push(Box::new(tree));
        main_content.push(Box::new(data_row));
        main_content.push(Box::new(Divider::horizontal().labeled("Forms")));

        // FormsRow: Input + Editor
        let mut forms_row = Div::new(Layout::horizontal([
            Constraint::Percentage(50),
            Constraint::Percentage(50),
        ]));
        let mut input = Input::new();
        input.set_text("Search components...");
        forms_row.push(Box::new(input));
        let mut editor = Editor::new();
        editor.set_text("fn main() {\n    println!(\"Hello\");\n}");
        forms_row.push(Box::new(editor));
        main_content.push(Box::new(forms_row));
        main_content.push(Box::new(Divider::horizontal().labeled("Lists")));

        // ListsRow: SelectList + SettingsList
        let mut lists_row = Div::new(Layout::horizontal([
            Constraint::Percentage(50),
            Constraint::Percentage(50),
        ]));
        let mut select_list = SelectList::new(
            vec![
                "Rust".into(),
                "Python".into(),
                "TypeScript".into(),
                "Go".into(),
                "Zig".into(),
            ],
            3,
        );
        select_list.set_selected(1);
        lists_row.push(Box::new(select_list));
        let mut settings = SettingsList::new(vec![
            ("Dark mode".into(), true),
            ("Auto-save".into(), false),
            ("Notifications".into(), true),
        ]);
        settings.set_selected(0);
        lists_row.push(Box::new(settings));
        main_content.push(Box::new(lists_row));
        main_content.push(Box::new(Divider::horizontal()));

        // ButtonsRow
        let mut buttons_row = Div::new(Layout::horizontal([
            Constraint::Length(12),
            Constraint::Length(12),
            Constraint::Length(12),
            Constraint::Length(12),
        ]));
        buttons_row.push(Box::new(Button::primary("Primary")));
        buttons_row.push(Box::new(Button::dark("Dark")));
        buttons_row.push(Box::new(Button::ghost("Ghost")));
        buttons_row.push(Box::new(Button::text("Text")));
        main_content.push(Box::new(buttons_row));
        main_content.push(Box::new(Divider::horizontal().labeled("Content")));

        // ContentRow: Markdown + Panel
        let mut content_row = Div::new(Layout::horizontal([
            Constraint::Percentage(50),
            Constraint::Percentage(50),
        ]));
        content_row.push(Box::new(Markdown::new(DEMO_MARKDOWN)));
        content_row.push(Box::new(Panel::new().title("Info").lines(vec![
            "\x1b[1m\x1b[97mPhoton UI v0.1.0\x1b[0m".into(),
            "A Rust TUI library".into(),
            "\x1b[31mBuilt with ♥\x1b[0m".into(),
        ])));
        main_content.push(Box::new(content_row));
        main_content.push(Box::new(Divider::horizontal().labeled("System")));

        // SystemRow: Loader + CancellableLoader + ImageWidget + Status + TruncatedText
        let mut system_row = Div::new(Layout::horizontal([
            Constraint::Length(14),
            Constraint::Length(20),
            Constraint::Length(10),
            Constraint::Length(12),
            Constraint::Min(5),
        ]));
        system_row.push(Box::new(SharedLoader(self.loader.clone())));
        system_row.push(Box::new(SharedCancellableLoader(self.cancellable.clone())));
        system_row.push(Box::new(ImageWidget::new(
            vec![],
            "image/png",
            Some("[image]".to_string()),
        )));
        system_row.push(Box::new(Text::new("\x1b[32m●\x1b[0m online", 0, 0)));
        system_row.push(Box::new(TruncatedText::new(
            "This is a very long line that will be truncated with an ellipsis if the terminal is not wide enough to display it all",
            0,
            0,
        )));
        main_content.push(Box::new(system_row));

        dashboard_body.push(Box::new(main_content));
        self.tui.mount(Box::new(dashboard_body));

        self.tui.mount(Box::new(
            StatusBar::new()
                .left(Segment::new("MODE: normal"))
                .right(Segment::new("1-5:pages  q:quit")),
        ));
    }

    /// Advance animation frames. Call periodically from the event loop.
    fn tick(&mut self) {
        if self.page == 3 || self.page == 5 {
            self.loader.borrow_mut().tick();
            self.cancellable.borrow_mut().tick();
        }
    }

    /// Handle an input event. Returns `false` when the app should quit.
    fn handle_input(&mut self, event: &Event) -> bool {
        // Global navigation
        if let Event::Key(key) = event {
            match key.code {
                | KeyCode::Char('1') => {
                    self.page = 1;
                    self.load_page();
                    return true;
                },
                | KeyCode::Char('2') => {
                    self.page = 2;
                    self.load_page();
                    return true;
                },
                | KeyCode::Char('3') => {
                    self.page = 3;
                    self.load_page();
                    return true;
                },
                | KeyCode::Char('4') => {
                    self.page = 4;
                    self.load_page();
                    return true;
                },
                | KeyCode::Char('5') => {
                    self.page = 5;
                    self.load_page();
                    return true;
                },
                | KeyCode::Char('q') if key.modifiers.is_empty() => return false,
                | KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                    return false;
                },
                | _ => {},
            }
        }

        // Page-specific global keys
        if self.page == 2 {
            if let Event::Key(key) = event {
                if key.code == KeyCode::Char('v') && key.modifiers.is_empty() {
                    self.input_vim = !self.input_vim;
                    self.load_page();
                    return true;
                }
                if key.code == KeyCode::Char('V') && key.modifiers.contains(KeyModifiers::SHIFT) {
                    self.editor_vim = !self.editor_vim;
                    self.load_page();
                    return true;
                }
            }
        }

        if self.page == 3 {
            if let Event::Key(key) = event {
                if key.code == KeyCode::Char('o') && key.modifiers.is_empty() {
                    self.show_overlay = !self.show_overlay;
                    self.load_page();
                    return true;
                }
            }
        }

        if self.page == 4 {
            if let Event::Key(key) = event {
                if key.code == KeyCode::Char('t') && key.modifiers.is_empty() {
                    let next = match Theme::current() {
                        | Theme::Light => Theme::Dark,
                        | Theme::Dark => Theme::Light,
                    };
                    Theme::set(next);
                    self.load_page();
                    return true;
                }
                if key.code == KeyCode::Char('l') && key.modifiers.is_empty() {
                    self.design_subpage = !self.design_subpage;
                    self.load_page();
                    return true;
                }
            }
        }

        if self.page == 5 {
            if let Event::Key(key) = event {
                if key.code == KeyCode::Char('m') && key.modifiers.is_empty() {
                    if self.tui.modal_active() {
                        self.tui.dismiss_modal();
                    } else {
                        let modal_content = Div::new(Layout::vertical([
                            Constraint::Length(1),
                            Constraint::Length(1),
                        ]))
                        .child(Box::new(Text::new("This is a modal dialog.", 0, 0)))
                        .child(Box::new(Text::new(
                            "Press Esc to dismiss.",
                            0,
                            0,
                        )));
                        self.tui.show_modal(Box::new(
                            Modal::new(Box::new(modal_content)).title("Modal"),
                        ));
                    }
                    return true;
                }
            }
        }

        self.tui.handle_input(event);
        true
    }

    fn render(&mut self) -> std::io::Result<()> {
        self.tui.render_frame()
    }
}

/// Attempt to detect terminal background color via OSC 11 query.
/// Returns `true` for dark, `false` for light, `None` if detection fails.
fn detect_dark_background() -> Option<bool> {
    use std::{
        io::{
            self,
            Read,
            Write,
        },
        time::{
            Duration,
            Instant,
        },
    };

    // Send OSC 11 query
    let mut stdout = io::stdout();
    stdout.write_all(b"\x1b]11;?\x07").ok()?;
    stdout.flush().ok()?;

    // Give the terminal a moment to respond
    std::thread::sleep(Duration::from_millis(50));

    let mut response = Vec::new();
    let mut buf = [0u8; 128];
    let start = Instant::now();

    while start.elapsed() < Duration::from_millis(200) {
        match io::stdin().read(&mut buf) {
            | Ok(0) => break,
            | Ok(n) => {
                response.extend_from_slice(&buf[..n]);
                if response.contains(&0x07) {
                    break;
                }
            },
            | Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
                std::thread::sleep(Duration::from_millis(10));
            },
            | Err(_) => break,
        }
    }

    if response.is_empty() {
        return None;
    }

    let s = String::from_utf8_lossy(&response);

    // Parse \x1b]11;rgb:RRRR/GGGG/BBBB\x07 or \x1b]11;rgb:RR/GG/BB\x07
    let rgb_idx = s.find("rgb:")?;
    let rgb_part = &s[rgb_idx + 4..];
    let end_idx = rgb_part
        .find(|c: char| c == '\x07' || c == '\x1b')
        .unwrap_or(rgb_part.len());
    let rgb = &rgb_part[..end_idx];

    let channels: Vec<&str> = rgb.split('/').collect();
    if channels.len() != 3 {
        return None;
    }

    let hex2 = |s: &str| u8::from_str_radix(&s[..2.min(s.len())], 16).ok();
    let r = hex2(channels[0])? as f32 / 255.0;
    let g = hex2(channels[1])? as f32 / 255.0;
    let b = hex2(channels[2])? as f32 / 255.0;

    // Relative luminance (sRGB)
    let luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b;
    Some(luminance < 0.5)
}

/// Detect the terminal's background color and return the matching theme.
fn detect_terminal_theme() -> Theme {
    // Try OSC 11 query first
    if let Some(is_dark) = detect_dark_background() {
        return if is_dark { Theme::Dark } else { Theme::Light };
    }

    // Fall back to COLORFGBG (xterm, rxvt, etc.)
    if let Ok(fgbg) = std::env::var("COLORFGBG") {
        let parts: Vec<&str> = fgbg.split(';').collect();
        if let Some(bg) = parts.get(1) {
            if let Ok(n) = bg.parse::<u8>() {
                // xterm: 0-7 are dark colors, 8-15 are light
                return if n <= 7 { Theme::Dark } else { Theme::Light };
            }
        }
    }

    Theme::Dark
}

fn main() -> std::io::Result<()> {
    let mut term = ProcessTerminal::new();
    term.start()?;

    // Auto-detect terminal background and set theme before rendering
    let detected = detect_terminal_theme();
    Theme::set(detected);

    let tui = TUI::new(std::boxed::Box::new(term));
    let mut app = DemoApp::new(tui);

    // Initial render
    app.render()?;

    // Event loop
    loop {
        // Tick animations at ~10 fps
        app.tick();
        app.render()?;

        if crossterm::event::poll(Duration::from_millis(100))? {
            let event = match crossterm::event::read()? {
                | crossterm::event::Event::Key(key) => Event::Key(key),
                | crossterm::event::Event::Resize(w, h) => Event::Resize(w, h),
                | crossterm::event::Event::Mouse(m) => Event::Mouse(m),
                | crossterm::event::Event::Paste(p) => Event::Paste(p),
                | crossterm::event::Event::FocusGained => Event::FocusGained,
                | crossterm::event::Event::FocusLost => Event::FocusLost,
            };

            if !app.handle_input(&event) {
                break;
            }
            app.render()?;
        }
    }

    // CRITICAL: restore terminal state (leave alternate screen, disable raw mode,
    // show cursor)
    app.tui.stop()?;

    Ok(())
}