gpui-ui-kit 0.5.10

A reusable UI component library for GPUI applications
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
//! Wizard component for multi-step workflows
//!
//! Provides a step-by-step wizard with:
//! - Step indicators with status (not visited, active, completed, error, skipped)
//! - Navigation buttons (Back/Next/Finish/Cancel)
//! - Form validation support per step
//! - Step dependencies (can only advance if validation passes)
//! - Async operation support with progress tracking
//! - Cancelable operations

use crate::ComponentTheme;
use crate::button::{Button, ButtonSize, ButtonVariant};
use crate::progress::{Progress, ProgressSize, ProgressVariant};
use crate::theme::ThemeExt;
use gpui::prelude::*;
use gpui::*;

/// Status of a wizard step
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum StepStatus {
    /// Step has not been visited yet
    #[default]
    NotVisited,
    /// Step is currently active
    Active,
    /// Step has been completed successfully
    Completed,
    /// Step has an error
    Error,
    /// Step was skipped
    Skipped,
}

/// Theme colors for wizard styling
#[derive(Debug, Clone, ComponentTheme)]
pub struct WizardTheme {
    /// Background color for step indicators
    #[theme(default = 0x2a2a2aff, from = surface)]
    pub step_bg: Rgba,
    /// Background color for completed step
    #[theme(default = 0x22c55eff, from = success)]
    pub step_completed_bg: Rgba,
    /// Background color for active step
    #[theme(default = 0x007accff, from = accent)]
    pub step_active_bg: Rgba,
    /// Background color for error step
    #[theme(default = 0xef4444ff, from = error)]
    pub step_error_bg: Rgba,
    /// Text color for step numbers
    #[theme(default = 0xffffffff, from = text_primary)]
    pub step_text: Rgba,
    /// Text color for step labels
    #[theme(default = 0x888888ff, from = text_muted)]
    pub label_text: Rgba,
    /// Text color for active step label
    #[theme(default = 0xffffffff, from = text_primary)]
    pub label_active_text: Rgba,
    /// Connector line color
    #[theme(default = 0x3a3a3aff, from = border)]
    pub connector_color: Rgba,
    /// Connector color for completed steps
    #[theme(default = 0x22c55eff, from = success)]
    pub connector_completed_color: Rgba,
    /// Border color for steps
    #[theme(default = 0x3a3a3aff, from = border)]
    pub step_border: Rgba,
}

/// A single step in the wizard
#[derive(Clone)]
pub struct WizardStep {
    /// Unique identifier for this step
    pub id: SharedString,
    /// Display label for this step
    pub label: SharedString,
    /// Optional description
    pub description: Option<SharedString>,
    /// Optional icon (emoji or text)
    pub icon: Option<SharedString>,
    /// Whether this step can be skipped
    pub can_skip: bool,
    /// Whether this step is disabled
    pub disabled: bool,
}

impl WizardStep {
    /// Create a new wizard step
    pub fn new(id: impl Into<SharedString>, label: impl Into<SharedString>) -> Self {
        Self {
            id: id.into(),
            label: label.into(),
            description: None,
            icon: None,
            can_skip: false,
            disabled: false,
        }
    }

    /// Add a description
    pub fn description(mut self, description: impl Into<SharedString>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Add an icon
    pub fn icon(mut self, icon: impl Into<SharedString>) -> Self {
        self.icon = Some(icon.into());
        self
    }

    /// Set whether this step can be skipped
    pub fn can_skip(mut self, can_skip: bool) -> Self {
        self.can_skip = can_skip;
        self
    }

    /// Set whether this step is disabled
    pub fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }
}

/// Variant for wizard layout
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum WizardVariant {
    /// Horizontal step indicators (default)
    #[default]
    Horizontal,
    /// Vertical step indicators (sidebar style)
    Vertical,
}

/// A wizard component for multi-step workflows
pub struct Wizard {
    steps: Vec<WizardStep>,
    step_statuses: Vec<StepStatus>,
    current_step: usize,
    variant: WizardVariant,
    theme: Option<WizardTheme>,
    /// Whether an operation is in progress (disables navigation)
    is_busy: bool,
    /// Progress value (0.0 - 1.0) for async operations
    progress: Option<f32>,
    /// Status message to display
    status_message: Option<SharedString>,
    /// Whether the cancel button is shown
    show_cancel: bool,
    /// Custom label for the back button
    back_label: Option<SharedString>,
    /// Custom label for the next button
    next_label: Option<SharedString>,
    /// Custom label for the finish button
    finish_label: Option<SharedString>,
    /// Custom label for the cancel button
    cancel_label: Option<SharedString>,
    /// Callback when step changes
    on_step_change: Option<Box<dyn Fn(usize, &mut Window, &mut App) + 'static>>,
    /// Callback when validation is needed before advancing
    on_validate: Option<Box<dyn Fn(usize) -> bool + 'static>>,
    /// Callback when finish is clicked (last step)
    on_finish: Option<Box<dyn Fn(&mut Window, &mut App) + 'static>>,
    /// Callback when cancel is clicked
    on_cancel: Option<Box<dyn Fn(&mut Window, &mut App) + 'static>>,
    /// Callback when back is clicked
    on_back: Option<Box<dyn Fn(usize, &mut Window, &mut App) + 'static>>,
    /// Callback when next is clicked
    on_next: Option<Box<dyn Fn(usize, &mut Window, &mut App) + 'static>>,
}

impl Wizard {
    /// Create a new wizard
    pub fn new() -> Self {
        Self {
            steps: Vec::new(),
            step_statuses: Vec::new(),
            current_step: 0,
            variant: WizardVariant::default(),
            theme: None,
            is_busy: false,
            progress: None,
            status_message: None,
            show_cancel: true,
            back_label: None,
            next_label: None,
            finish_label: None,
            cancel_label: None,
            on_step_change: None,
            on_validate: None,
            on_finish: None,
            on_cancel: None,
            on_back: None,
            on_next: None,
        }
    }

    /// Set the wizard steps
    pub fn steps(mut self, steps: Vec<WizardStep>) -> Self {
        let count = steps.len();
        self.steps = steps;
        // Initialize step statuses - first step is active, rest are not visited
        self.step_statuses = vec![StepStatus::NotVisited; count];
        if count > 0 {
            self.step_statuses[0] = StepStatus::Active;
        }
        self
    }

    /// Set the step statuses (must match steps length)
    pub fn step_statuses(mut self, statuses: Vec<StepStatus>) -> Self {
        self.step_statuses = statuses;
        self
    }

    /// Set the current step index
    pub fn current_step(mut self, step: usize) -> Self {
        self.current_step = step;
        self
    }

    /// Set the wizard variant
    pub fn variant(mut self, variant: WizardVariant) -> Self {
        self.variant = variant;
        self
    }

    /// Set the theme
    pub fn theme(mut self, theme: WizardTheme) -> Self {
        self.theme = Some(theme);
        self
    }

    /// Set busy state
    pub fn is_busy(mut self, busy: bool) -> Self {
        self.is_busy = busy;
        self
    }

    /// Set progress value (0.0 - 1.0)
    pub fn progress(mut self, progress: f32) -> Self {
        self.progress = Some(progress);
        self
    }

    /// Set status message
    pub fn status_message(mut self, message: impl Into<SharedString>) -> Self {
        self.status_message = Some(message.into());
        self
    }

    /// Show or hide cancel button
    pub fn show_cancel(mut self, show: bool) -> Self {
        self.show_cancel = show;
        self
    }

    /// Set custom back button label
    pub fn back_label(mut self, label: impl Into<SharedString>) -> Self {
        self.back_label = Some(label.into());
        self
    }

    /// Set custom next button label
    pub fn next_label(mut self, label: impl Into<SharedString>) -> Self {
        self.next_label = Some(label.into());
        self
    }

    /// Set custom finish button label
    pub fn finish_label(mut self, label: impl Into<SharedString>) -> Self {
        self.finish_label = Some(label.into());
        self
    }

    /// Set custom cancel button label
    pub fn cancel_label(mut self, label: impl Into<SharedString>) -> Self {
        self.cancel_label = Some(label.into());
        self
    }

    /// Set step change handler
    pub fn on_step_change(
        mut self,
        handler: impl Fn(usize, &mut Window, &mut App) + 'static,
    ) -> Self {
        self.on_step_change = Some(Box::new(handler));
        self
    }

    /// Set validation handler (return true if step is valid)
    pub fn on_validate(mut self, handler: impl Fn(usize) -> bool + 'static) -> Self {
        self.on_validate = Some(Box::new(handler));
        self
    }

    /// Set finish handler
    pub fn on_finish(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
        self.on_finish = Some(Box::new(handler));
        self
    }

    /// Set cancel handler
    pub fn on_cancel(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
        self.on_cancel = Some(Box::new(handler));
        self
    }

    /// Set back button handler
    pub fn on_back(mut self, handler: impl Fn(usize, &mut Window, &mut App) + 'static) -> Self {
        self.on_back = Some(Box::new(handler));
        self
    }

    /// Set next button handler
    pub fn on_next(mut self, handler: impl Fn(usize, &mut Window, &mut App) + 'static) -> Self {
        self.on_next = Some(Box::new(handler));
        self
    }

    /// Build the step indicators
    fn build_step_indicators(&self, theme: &WizardTheme) -> Div {
        let mut container = div().flex().items_center().gap_2();

        for (index, step) in self.steps.iter().enumerate() {
            let status = self
                .step_statuses
                .get(index)
                .copied()
                .unwrap_or(StepStatus::NotVisited);
            let is_current = index == self.current_step;

            // Determine colors based on status
            let (bg_color, text_color, border_color) = match status {
                StepStatus::NotVisited => (theme.step_bg, theme.label_text, theme.step_border),
                StepStatus::Active => (theme.step_active_bg, theme.step_text, theme.step_active_bg),
                StepStatus::Completed => (
                    theme.step_completed_bg,
                    theme.step_text,
                    theme.step_completed_bg,
                ),
                StepStatus::Error => (theme.step_error_bg, theme.step_text, theme.step_error_bg),
                StepStatus::Skipped => (theme.step_bg, theme.label_text, theme.step_border),
            };

            // Step indicator circle
            let step_number = format!("{}", index + 1);
            let step_icon = if status == StepStatus::Completed {
                "✓".to_string()
            } else if status == StepStatus::Error {
                "✗".to_string()
            } else if let Some(icon) = &step.icon {
                icon.to_string()
            } else {
                step_number
            };

            let step_circle = div()
                .w(px(28.0))
                .h(px(28.0))
                .rounded_full()
                .bg(bg_color)
                .border_2()
                .border_color(border_color)
                .flex()
                .items_center()
                .justify_center()
                .child(
                    div()
                        .text_sm()
                        .font_weight(if is_current {
                            FontWeight::BOLD
                        } else {
                            FontWeight::NORMAL
                        })
                        .text_color(text_color)
                        .child(step_icon),
                );

            // Label
            let label_color = if is_current {
                theme.label_active_text
            } else {
                theme.label_text
            };

            let label = div()
                .text_sm()
                .font_weight(if is_current {
                    FontWeight::SEMIBOLD
                } else {
                    FontWeight::NORMAL
                })
                .text_color(label_color)
                .child(step.label.clone());

            // Step item (circle + label)
            let step_item = div()
                .flex()
                .items_center()
                .gap_2()
                .child(step_circle)
                .child(label);

            container = container.child(step_item);

            // Connector line between steps (except after last step)
            if index < self.steps.len() - 1 {
                let connector_color = if status == StepStatus::Completed {
                    theme.connector_completed_color
                } else {
                    theme.connector_color
                };

                let connector = div().w(px(32.0)).h(px(2.0)).bg(connector_color);

                container = container.child(connector);
            }
        }

        container
    }

    /// Build the navigation buttons
    fn build_navigation(&self, _theme: &WizardTheme) -> Div {
        let is_first_step = self.current_step == 0;
        let is_last_step = self.current_step >= self.steps.len().saturating_sub(1);

        let back_label = self.back_label.clone().unwrap_or_else(|| {
            if is_first_step {
                "Close".into()
            } else {
                "Back".into()
            }
        });

        let next_label = if is_last_step {
            self.finish_label.clone().unwrap_or_else(|| "Finish".into())
        } else {
            self.next_label.clone().unwrap_or_else(|| "Next".into())
        };

        let cancel_label = self.cancel_label.clone().unwrap_or_else(|| "Cancel".into());

        // Create button elements
        let mut buttons = div().flex().items_center().gap_3();

        // Cancel button (if shown and we have a handler)
        if self.show_cancel && self.on_cancel.is_some() {
            let on_cancel: Option<*const dyn Fn(&mut Window, &mut App)> =
                self.on_cancel.as_ref().map(|f| f.as_ref() as *const _);

            let mut cancel_btn = Button::new("wizard-cancel", cancel_label)
                .variant(ButtonVariant::Ghost)
                .size(ButtonSize::Md)
                .disabled(self.is_busy);

            if let Some(handler_ptr) = on_cancel {
                cancel_btn = cancel_btn.on_click(move |window, cx| unsafe {
                    (*handler_ptr)(window, cx);
                });
            }

            buttons = buttons.child(cancel_btn);
        }

        // Spacer
        buttons = buttons.child(div().flex_1());

        // Back button
        let on_back: Option<*const dyn Fn(usize, &mut Window, &mut App)> =
            self.on_back.as_ref().map(|f| f.as_ref() as *const _);
        let current_step = self.current_step;

        let mut back_btn = Button::new("wizard-back", back_label)
            .variant(ButtonVariant::Secondary)
            .size(ButtonSize::Md)
            .disabled(self.is_busy);

        if let Some(handler_ptr) = on_back {
            back_btn = back_btn.on_click(move |window, cx| unsafe {
                (*handler_ptr)(current_step, window, cx);
            });
        }

        buttons = buttons.child(back_btn);

        // Next/Finish button
        let on_next: Option<*const dyn Fn(usize, &mut Window, &mut App)> =
            self.on_next.as_ref().map(|f| f.as_ref() as *const _);
        let on_finish: Option<*const dyn Fn(&mut Window, &mut App)> =
            self.on_finish.as_ref().map(|f| f.as_ref() as *const _);

        let mut next_btn = Button::new("wizard-next", next_label)
            .variant(ButtonVariant::Primary)
            .size(ButtonSize::Md)
            .disabled(self.is_busy);

        if is_last_step {
            if let Some(handler_ptr) = on_finish {
                next_btn = next_btn.on_click(move |window, cx| unsafe {
                    (*handler_ptr)(window, cx);
                });
            }
        } else if let Some(handler_ptr) = on_next {
            next_btn = next_btn.on_click(move |window, cx| unsafe {
                (*handler_ptr)(current_step, window, cx);
            });
        }

        buttons = buttons.child(next_btn);

        buttons
    }

    /// Build into element with theme
    pub fn build_with_theme(self, global_theme: &WizardTheme) -> Div {
        let theme = self.theme.as_ref().unwrap_or(global_theme);

        let mut container = div().flex().flex_col().gap_4().w_full();

        // Step indicators
        let indicators = self.build_step_indicators(theme);
        container = container.child(indicators);

        // Progress bar (if progress is set)
        if let Some(progress_value) = self.progress {
            let progress_bar = Progress::new(progress_value)
                .size(ProgressSize::Sm)
                .variant(ProgressVariant::Default);

            container = container.child(progress_bar);
        }

        // Status message (if set)
        if let Some(message) = &self.status_message {
            container = container.child(
                div()
                    .text_sm()
                    .text_color(theme.label_text)
                    .child(message.clone()),
            );
        }

        // Navigation buttons
        let navigation = self.build_navigation(theme);
        container = container.child(navigation);

        container
    }
}

impl Default for Wizard {
    fn default() -> Self {
        Self::new()
    }
}

impl RenderOnce for Wizard {
    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
        let global_theme = cx.theme();
        let wizard_theme = WizardTheme::from(&global_theme);
        self.build_with_theme(&wizard_theme)
    }
}

impl IntoElement for Wizard {
    type Element = gpui::Component<Self>;

    fn into_element(self) -> Self::Element {
        gpui::Component::new(self)
    }
}

/// Header component for wizard screens - renders just the step indicators
/// Use this when you want to place the wizard header and navigation separately
pub struct WizardHeader {
    steps: Vec<WizardStep>,
    step_statuses: Vec<StepStatus>,
    current_step: usize,
    title: Option<SharedString>,
    theme: Option<WizardTheme>,
}

impl WizardHeader {
    /// Create a new wizard header
    pub fn new() -> Self {
        Self {
            steps: Vec::new(),
            step_statuses: Vec::new(),
            current_step: 0,
            title: None,
            theme: None,
        }
    }

    /// Set the wizard steps
    pub fn steps(mut self, steps: Vec<WizardStep>) -> Self {
        let count = steps.len();
        self.steps = steps;
        self.step_statuses = vec![StepStatus::NotVisited; count];
        if count > 0 {
            self.step_statuses[0] = StepStatus::Active;
        }
        self
    }

    /// Set step statuses
    pub fn step_statuses(mut self, statuses: Vec<StepStatus>) -> Self {
        self.step_statuses = statuses;
        self
    }

    /// Set current step
    pub fn current_step(mut self, step: usize) -> Self {
        self.current_step = step;
        self
    }

    /// Set title
    pub fn title(mut self, title: impl Into<SharedString>) -> Self {
        self.title = Some(title.into());
        self
    }

    /// Set theme
    pub fn theme(mut self, theme: WizardTheme) -> Self {
        self.theme = Some(theme);
        self
    }

    /// Build step indicators (reuses Wizard's logic)
    fn build_step_indicators(&self, theme: &WizardTheme) -> Div {
        let mut container = div().flex().items_center().gap_2();

        for (index, step) in self.steps.iter().enumerate() {
            let status = self
                .step_statuses
                .get(index)
                .copied()
                .unwrap_or(StepStatus::NotVisited);
            let is_current = index == self.current_step;

            let (bg_color, text_color, border_color) = match status {
                StepStatus::NotVisited => (theme.step_bg, theme.label_text, theme.step_border),
                StepStatus::Active => (theme.step_active_bg, theme.step_text, theme.step_active_bg),
                StepStatus::Completed => (
                    theme.step_completed_bg,
                    theme.step_text,
                    theme.step_completed_bg,
                ),
                StepStatus::Error => (theme.step_error_bg, theme.step_text, theme.step_error_bg),
                StepStatus::Skipped => (theme.step_bg, theme.label_text, theme.step_border),
            };

            let step_icon = if status == StepStatus::Completed {
                "✓".to_string()
            } else if status == StepStatus::Error {
                "✗".to_string()
            } else if let Some(icon) = &step.icon {
                icon.to_string()
            } else {
                format!("{}", index + 1)
            };

            let step_circle = div()
                .w(px(28.0))
                .h(px(28.0))
                .rounded_full()
                .bg(bg_color)
                .border_2()
                .border_color(border_color)
                .flex()
                .items_center()
                .justify_center()
                .child(
                    div()
                        .text_sm()
                        .font_weight(if is_current {
                            FontWeight::BOLD
                        } else {
                            FontWeight::NORMAL
                        })
                        .text_color(text_color)
                        .child(step_icon),
                );

            let label_color = if is_current {
                theme.label_active_text
            } else {
                theme.label_text
            };

            let label = div()
                .text_sm()
                .font_weight(if is_current {
                    FontWeight::SEMIBOLD
                } else {
                    FontWeight::NORMAL
                })
                .text_color(label_color)
                .child(step.label.clone());

            let step_item = div()
                .flex()
                .items_center()
                .gap_2()
                .child(step_circle)
                .child(label);

            container = container.child(step_item);

            if index < self.steps.len() - 1 {
                let connector_color = if status == StepStatus::Completed {
                    theme.connector_completed_color
                } else {
                    theme.connector_color
                };

                container = container.child(div().w(px(32.0)).h(px(2.0)).bg(connector_color));
            }
        }

        container
    }

    /// Build with theme
    pub fn build_with_theme(self, global_theme: &WizardTheme) -> Div {
        let theme = self.theme.as_ref().unwrap_or(global_theme);

        let mut container = div().flex().items_center().gap_4();

        // Title (if set)
        if let Some(title) = &self.title {
            container = container.child(
                div()
                    .text_xl()
                    .font_weight(FontWeight::BOLD)
                    .text_color(theme.label_active_text)
                    .child(title.clone()),
            );

            // Separator
            container = container.child(div().w(px(1.0)).h(px(24.0)).bg(theme.step_border));
        }

        // Step indicators
        container = container.child(self.build_step_indicators(theme));

        container
    }
}

impl Default for WizardHeader {
    fn default() -> Self {
        Self::new()
    }
}

impl RenderOnce for WizardHeader {
    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
        let global_theme = cx.theme();
        let wizard_theme = WizardTheme::from(&global_theme);
        self.build_with_theme(&wizard_theme)
    }
}

impl IntoElement for WizardHeader {
    type Element = gpui::Component<Self>;

    fn into_element(self) -> Self::Element {
        gpui::Component::new(self)
    }
}

/// Navigation buttons component for wizard screens
/// Use this when you want to place navigation separately from the header
pub struct WizardNavigation {
    current_step: usize,
    total_steps: usize,
    is_busy: bool,
    progress: Option<f32>,
    status_message: Option<SharedString>,
    show_cancel: bool,
    back_label: Option<SharedString>,
    next_label: Option<SharedString>,
    finish_label: Option<SharedString>,
    cancel_label: Option<SharedString>,
    back_disabled: bool,
    next_disabled: bool,
    on_back: Option<Box<dyn Fn(usize, &mut Window, &mut App) + 'static>>,
    on_next: Option<Box<dyn Fn(usize, &mut Window, &mut App) + 'static>>,
    on_finish: Option<Box<dyn Fn(&mut Window, &mut App) + 'static>>,
    on_cancel: Option<Box<dyn Fn(&mut Window, &mut App) + 'static>>,
    theme: Option<WizardTheme>,
}

impl WizardNavigation {
    /// Create new navigation with step info
    pub fn new(current_step: usize, total_steps: usize) -> Self {
        Self {
            current_step,
            total_steps,
            is_busy: false,
            progress: None,
            status_message: None,
            show_cancel: false,
            back_label: None,
            next_label: None,
            finish_label: None,
            cancel_label: None,
            back_disabled: false,
            next_disabled: false,
            on_back: None,
            on_next: None,
            on_finish: None,
            on_cancel: None,
            theme: None,
        }
    }

    /// Set busy state
    pub fn is_busy(mut self, busy: bool) -> Self {
        self.is_busy = busy;
        self
    }

    /// Set progress
    pub fn progress(mut self, progress: f32) -> Self {
        self.progress = Some(progress);
        self
    }

    /// Set status message
    pub fn status_message(mut self, message: impl Into<SharedString>) -> Self {
        self.status_message = Some(message.into());
        self
    }

    /// Show cancel button
    pub fn show_cancel(mut self, show: bool) -> Self {
        self.show_cancel = show;
        self
    }

    /// Set back label
    pub fn back_label(mut self, label: impl Into<SharedString>) -> Self {
        self.back_label = Some(label.into());
        self
    }

    /// Set next label
    pub fn next_label(mut self, label: impl Into<SharedString>) -> Self {
        self.next_label = Some(label.into());
        self
    }

    /// Set finish label
    pub fn finish_label(mut self, label: impl Into<SharedString>) -> Self {
        self.finish_label = Some(label.into());
        self
    }

    /// Set cancel label
    pub fn cancel_label(mut self, label: impl Into<SharedString>) -> Self {
        self.cancel_label = Some(label.into());
        self
    }

    /// Disable back button
    pub fn back_disabled(mut self, disabled: bool) -> Self {
        self.back_disabled = disabled;
        self
    }

    /// Disable next button
    pub fn next_disabled(mut self, disabled: bool) -> Self {
        self.next_disabled = disabled;
        self
    }

    /// Set back handler
    pub fn on_back(mut self, handler: impl Fn(usize, &mut Window, &mut App) + 'static) -> Self {
        self.on_back = Some(Box::new(handler));
        self
    }

    /// Set next handler
    pub fn on_next(mut self, handler: impl Fn(usize, &mut Window, &mut App) + 'static) -> Self {
        self.on_next = Some(Box::new(handler));
        self
    }

    /// Set finish handler
    pub fn on_finish(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
        self.on_finish = Some(Box::new(handler));
        self
    }

    /// Set cancel handler
    pub fn on_cancel(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
        self.on_cancel = Some(Box::new(handler));
        self
    }

    /// Set theme
    pub fn theme(mut self, theme: WizardTheme) -> Self {
        self.theme = Some(theme);
        self
    }

    /// Build with theme
    pub fn build_with_theme(self, global_theme: &WizardTheme) -> Div {
        let theme = self.theme.as_ref().unwrap_or(global_theme);
        let is_first_step = self.current_step == 0;
        let is_last_step = self.current_step >= self.total_steps.saturating_sub(1);

        let back_label = self.back_label.clone().unwrap_or_else(|| {
            if is_first_step {
                "Close".into()
            } else {
                "Back".into()
            }
        });

        let next_label = if is_last_step {
            self.finish_label.clone().unwrap_or_else(|| "Finish".into())
        } else {
            self.next_label.clone().unwrap_or_else(|| "Next".into())
        };

        let cancel_label = self.cancel_label.clone().unwrap_or_else(|| "Cancel".into());

        let mut container = div().flex().flex_col().gap_3().w_full();

        // Progress bar
        if let Some(progress_value) = self.progress {
            container = container.child(
                Progress::new(progress_value)
                    .size(ProgressSize::Sm)
                    .variant(ProgressVariant::Default),
            );
        }

        // Status message
        if let Some(message) = &self.status_message {
            container = container.child(
                div()
                    .text_sm()
                    .text_color(theme.label_text)
                    .child(message.clone()),
            );
        }

        // Buttons row
        let mut buttons = div().flex().items_center().gap_3();

        // Cancel button
        if self.show_cancel {
            let on_cancel: Option<*const dyn Fn(&mut Window, &mut App)> =
                self.on_cancel.as_ref().map(|f| f.as_ref() as *const _);

            let mut cancel_btn = Button::new("wizard-nav-cancel", cancel_label)
                .variant(ButtonVariant::Ghost)
                .size(ButtonSize::Md)
                .disabled(self.is_busy);

            if let Some(handler_ptr) = on_cancel {
                cancel_btn = cancel_btn.on_click(move |window, cx| unsafe {
                    (*handler_ptr)(window, cx);
                });
            }

            buttons = buttons.child(cancel_btn);
        }

        // Spacer
        buttons = buttons.child(div().flex_1());

        // Back button
        let on_back: Option<*const dyn Fn(usize, &mut Window, &mut App)> =
            self.on_back.as_ref().map(|f| f.as_ref() as *const _);
        let current_step = self.current_step;

        let mut back_btn = Button::new("wizard-nav-back", back_label)
            .variant(ButtonVariant::Secondary)
            .size(ButtonSize::Md)
            .disabled(self.is_busy || self.back_disabled);

        if let Some(handler_ptr) = on_back {
            back_btn = back_btn.on_click(move |window, cx| unsafe {
                (*handler_ptr)(current_step, window, cx);
            });
        }

        buttons = buttons.child(back_btn);

        // Next/Finish button
        let on_next: Option<*const dyn Fn(usize, &mut Window, &mut App)> =
            self.on_next.as_ref().map(|f| f.as_ref() as *const _);
        let on_finish: Option<*const dyn Fn(&mut Window, &mut App)> =
            self.on_finish.as_ref().map(|f| f.as_ref() as *const _);

        let mut next_btn = Button::new("wizard-nav-next", next_label)
            .variant(ButtonVariant::Primary)
            .size(ButtonSize::Md)
            .disabled(self.is_busy || self.next_disabled);

        if is_last_step {
            if let Some(handler_ptr) = on_finish {
                next_btn = next_btn.on_click(move |window, cx| unsafe {
                    (*handler_ptr)(window, cx);
                });
            }
        } else if let Some(handler_ptr) = on_next {
            next_btn = next_btn.on_click(move |window, cx| unsafe {
                (*handler_ptr)(current_step, window, cx);
            });
        }

        buttons = buttons.child(next_btn);

        container = container.child(buttons);

        container
    }
}

impl RenderOnce for WizardNavigation {
    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
        let global_theme = cx.theme();
        let wizard_theme = WizardTheme::from(&global_theme);
        self.build_with_theme(&wizard_theme)
    }
}

impl IntoElement for WizardNavigation {
    type Element = gpui::Component<Self>;

    fn into_element(self) -> Self::Element {
        gpui::Component::new(self)
    }
}