fc-ui 0.9.7

A comprehensive, professional UI component library for GPUI inspired by shadcn/ui. 85+ polished components for building beautiful desktop applications.
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
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
//! Input component - Advanced text input with validation, masking, and accessibility.

pub use crate::capabilities::controls::input_state::{
    wire_actions, Backspace, Copy, Cut, Delete, End, Enter, Escape, Home, InputEvent, InputMask,
    InputState, InputType, Left, Paste, Right, SelectAll, SelectLeft, SelectRight, SelectWordLeft,
    SelectWordRight, ShiftTab, Tab, ValidationError, ValidationRules, WordLeft, WordRight,
};
use crate::capabilities::controls::validation::{validation_footer, CharacterCount, FooterMessage};
use crate::capabilities::foundation::theme::use_theme;
use crate::capabilities::layout::{HStack, VStack};
use crate::capabilities::motion::animations::{easings, shake_offset};
use crate::capabilities::primitives::icon::Icon;
use gpui::{prelude::FluentBuilder as _, *};
use std::rc::Rc;
use std::sync::Arc;
use std::time::Duration;

type InputValidator = Rc<dyn Fn(&str) -> Result<(), String>>;
type InputTransformer = Rc<dyn Fn(&str) -> String>;
type InputValueCallback = Rc<dyn Fn(SharedString, &mut App)>;
type InputValidationCallback = Rc<dyn Fn(Result<(), ValidationError>, &mut App)>;

pub fn init(cx: &mut App) {
    if !crate::capabilities::foundation::initialization::begin(cx, "input") {
        return;
    }
    cx.bind_keys([
        KeyBinding::new("backspace", Backspace, Some("Input")),
        KeyBinding::new("delete", Delete, Some("Input")),
        KeyBinding::new("left", Left, Some("Input")),
        KeyBinding::new("right", Right, Some("Input")),
        KeyBinding::new("shift-left", SelectLeft, Some("Input")),
        KeyBinding::new("shift-right", SelectRight, Some("Input")),
        KeyBinding::new("home", Home, Some("Input")),
        KeyBinding::new("end", End, Some("Input")),
        KeyBinding::new("enter", Enter, Some("Input")),
        KeyBinding::new("tab", Tab, Some("Input")),
        KeyBinding::new("shift-tab", ShiftTab, Some("Input")),
        #[cfg(target_os = "macos")]
        KeyBinding::new("cmd-a", SelectAll, Some("Input")),
        #[cfg(not(target_os = "macos"))]
        KeyBinding::new("ctrl-a", SelectAll, Some("Input")),
        #[cfg(target_os = "macos")]
        KeyBinding::new("cmd-c", Copy, Some("Input")),
        #[cfg(not(target_os = "macos"))]
        KeyBinding::new("ctrl-c", Copy, Some("Input")),
        #[cfg(target_os = "macos")]
        KeyBinding::new("cmd-x", Cut, Some("Input")),
        #[cfg(not(target_os = "macos"))]
        KeyBinding::new("ctrl-x", Cut, Some("Input")),
        #[cfg(target_os = "macos")]
        KeyBinding::new("cmd-v", Paste, Some("Input")),
        #[cfg(not(target_os = "macos"))]
        KeyBinding::new("ctrl-v", Paste, Some("Input")),
        KeyBinding::new("escape", Escape, Some("Input")),
        // Word navigation
        #[cfg(target_os = "macos")]
        KeyBinding::new("alt-left", WordLeft, Some("Input")),
        #[cfg(target_os = "macos")]
        KeyBinding::new("alt-right", WordRight, Some("Input")),
        #[cfg(not(target_os = "macos"))]
        KeyBinding::new("ctrl-left", WordLeft, Some("Input")),
        #[cfg(not(target_os = "macos"))]
        KeyBinding::new("ctrl-right", WordRight, Some("Input")),
        #[cfg(target_os = "macos")]
        KeyBinding::new("alt-shift-left", SelectWordLeft, Some("Input")),
        #[cfg(target_os = "macos")]
        KeyBinding::new("alt-shift-right", SelectWordRight, Some("Input")),
        #[cfg(not(target_os = "macos"))]
        KeyBinding::new("ctrl-shift-left", SelectWordLeft, Some("Input")),
        #[cfg(not(target_os = "macos"))]
        KeyBinding::new("ctrl-shift-right", SelectWordRight, Some("Input")),
    ]);
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InputVariant {
    Default,
    Outline,
    Ghost,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum InputSize {
    Sm,
    #[default]
    Md,
    Lg,
}

#[derive(IntoElement)]
pub struct Input {
    state: Entity<InputState>,
    placeholder: Option<SharedString>,
    variant: InputVariant,
    size: InputSize,
    disabled: bool,
    error: bool,
    password: bool,
    clearable: bool,
    prefix: Option<AnyElement>,
    suffix: Option<AnyElement>,
    initial_value: Option<SharedString>,
    value: Option<SharedString>,

    // Enhanced features
    input_type: Option<InputType>,
    validation_rules: Option<ValidationRules>,
    helper_text: Option<SharedString>,
    show_character_count: bool,
    aria_label: Option<SharedString>,
    aria_description: Option<SharedString>,
    autocomplete: Option<SharedString>,
    required: bool,

    // Custom functions for extensibility
    custom_validator: Option<InputValidator>,
    custom_filter: Option<InputTransformer>,
    custom_formatter: Option<InputTransformer>,

    // Callbacks
    on_change: Option<InputValueCallback>,
    on_enter: Option<InputValueCallback>,
    on_focus: Option<InputValueCallback>,
    on_blur: Option<InputValueCallback>,
    on_validate: Option<InputValidationCallback>,

    // Style refinement for Styled trait
    style: StyleRefinement,
}

impl Input {
    /// Create a new input with an InputState entity
    pub fn new(state: &Entity<InputState>) -> Self {
        Self {
            state: state.clone(),
            placeholder: None,
            variant: InputVariant::Default,
            size: InputSize::default(),
            disabled: false,
            error: false,
            password: false,
            clearable: false,
            prefix: None,
            suffix: None,
            initial_value: None,
            value: None,

            // Enhanced features
            input_type: None,
            validation_rules: None,
            helper_text: None,
            show_character_count: false,
            aria_label: None,
            aria_description: None,
            autocomplete: None,
            required: false,

            // Custom functions
            custom_validator: None,
            custom_filter: None,
            custom_formatter: None,

            // Callbacks
            on_change: None,
            on_enter: None,
            on_focus: None,
            on_blur: None,
            on_validate: None,

            // Style refinement
            style: StyleRefinement::default(),
        }
    }

    /// Set the value applied when this input state is first rendered.
    ///
    /// Later renders leave the state unchanged. To replace an existing value,
    /// update the associated [`InputState`] with [`InputState::set_value`].
    pub fn initial_value(mut self, value: impl Into<SharedString>) -> Self {
        self.initial_value = Some(value.into());
        self.value = None;
        self
    }

    /// Set a value controlled by the parent view.
    ///
    /// The input updates when the supplied value changes. Use [`Input::initial_value`]
    /// when the value should only be applied once.
    pub fn value(mut self, value: impl Into<SharedString>) -> Self {
        self.value = Some(value.into());
        self.initial_value = None;
        self
    }

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

    /// Enable the clear button when the input has a value.
    ///
    /// This spelling is kept as a compatibility alias for older callers.
    #[deprecated(note = "use clearable instead")]
    pub fn cleanable(self) -> Self {
        self.clearable(true)
    }

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

    /// Set the input size
    pub fn size(mut self, size: InputSize) -> Self {
        self.size = size;
        self
    }

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

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

    /// Enable password masking
    pub fn password(mut self, password: bool) -> Self {
        self.password = password;
        self
    }

    /// Enable the clear button when the input has a value.
    pub fn clearable(mut self, clearable: bool) -> Self {
        self.clearable = clearable;
        self
    }

    /// Add a prefix element (icon, label, etc.)
    pub fn prefix(mut self, prefix: impl IntoElement) -> Self {
        self.prefix = Some(prefix.into_any_element());
        self
    }

    /// Add a suffix element (icon, label, etc.)
    pub fn suffix(mut self, suffix: impl IntoElement) -> Self {
        self.suffix = Some(suffix.into_any_element());
        self
    }

    /// Set callback when value changes.
    ///
    /// The callback receives the canonical value before any custom display
    /// formatter is applied.
    pub fn on_change<F>(mut self, callback: F) -> Self
    where
        F: Fn(SharedString, &mut App) + 'static,
    {
        self.on_change = Some(Rc::new(callback));
        self
    }

    /// Set callback when Enter key is pressed
    pub fn on_enter<F>(mut self, callback: F) -> Self
    where
        F: Fn(SharedString, &mut App) + 'static,
    {
        self.on_enter = Some(Rc::new(callback));
        self
    }

    /// Set callback when input gains focus
    pub fn on_focus<F>(mut self, callback: F) -> Self
    where
        F: Fn(SharedString, &mut App) + 'static,
    {
        self.on_focus = Some(Rc::new(callback));
        self
    }

    /// Set callback when input loses focus
    pub fn on_blur<F>(mut self, callback: F) -> Self
    where
        F: Fn(SharedString, &mut App) + 'static,
    {
        self.on_blur = Some(Rc::new(callback));
        self
    }

    /// Set the input type (email, number, tel, etc.)
    pub fn input_type(mut self, input_type: InputType) -> Self {
        self.input_type = Some(input_type);
        self
    }

    /// Set validation rules
    pub fn validation_rules(mut self, rules: ValidationRules) -> Self {
        self.validation_rules = Some(rules);
        self
    }

    /// Set minimum length requirement
    pub fn min_length(mut self, min: usize) -> Self {
        if self.validation_rules.is_none() {
            self.validation_rules = Some(ValidationRules::default());
        }
        if let Some(ref mut rules) = self.validation_rules {
            rules.min_length = Some(min);
        }
        self
    }

    /// Set maximum length requirement
    pub fn max_length(mut self, max: usize) -> Self {
        if self.validation_rules.is_none() {
            self.validation_rules = Some(ValidationRules::default());
        }
        if let Some(ref mut rules) = self.validation_rules {
            rules.max_length = Some(max);
        }
        self
    }

    /// Mark field as required
    pub fn required(mut self, required: bool) -> Self {
        self.required = required;
        if self.validation_rules.is_none() {
            self.validation_rules = Some(ValidationRules::default());
        }
        if let Some(ref mut rules) = self.validation_rules {
            rules.required = required;
        }
        self
    }

    /// Set helper text displayed below the input
    pub fn helper_text(mut self, text: impl Into<SharedString>) -> Self {
        self.helper_text = Some(text.into());
        self
    }

    /// Show character count indicator
    pub fn show_character_count(mut self, show: bool) -> Self {
        self.show_character_count = show;
        self
    }

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

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

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

    /// Set callback for validation events
    pub fn on_validate<F>(mut self, callback: F) -> Self
    where
        F: Fn(Result<(), ValidationError>, &mut App) + 'static,
    {
        self.on_validate = Some(Rc::new(callback));
        self
    }

    /// Set a custom validation function
    ///
    /// # Example
    /// ```rust,ignore
    /// Input::new(&state)
    ///     .custom_validator(|value| {
    ///         if value.contains("@company.com") {
    ///             Ok(())
    ///         } else {
    ///             Err("Must be a company email".to_string())
    ///         }
    ///     })
    /// ```
    pub fn custom_validator<F>(mut self, validator: F) -> Self
    where
        F: Fn(&str) -> Result<(), String> + 'static,
    {
        self.custom_validator = Some(Rc::new(validator));
        self
    }

    /// Set a custom filter function to control which characters are allowed
    ///
    /// # Example
    /// ```rust,ignore
    /// Input::new(&state)
    ///     .custom_filter(|input| {
    ///         // Only allow alphanumeric and underscores
    ///         input.chars()
    ///             .filter(|c| c.is_alphanumeric() || *c == '_')
    ///             .collect()
    ///     })
    /// ```
    pub fn custom_filter<F>(mut self, filter: F) -> Self
    where
        F: Fn(&str) -> String + 'static,
    {
        self.custom_filter = Some(Rc::new(filter));
        self
    }

    /// Set a custom formatter function to format the input value
    ///
    /// # Example
    /// ```rust,ignore
    /// Input::new(&state)
    ///     .custom_formatter(|input| {
    ///         // Format as currency
    ///         format!("${:.2}", input.parse::<f64>().unwrap_or(0.0))
    ///     })
    /// ```
    pub fn custom_formatter<F>(mut self, formatter: F) -> Self
    where
        F: Fn(&str) -> String + 'static,
    {
        self.custom_formatter = Some(Rc::new(formatter));
        self
    }

    fn install_event_subscription(&self, cx: &mut App) {
        let on_change = self.on_change.clone();
        let on_enter = self.on_enter.clone();
        let on_focus = self.on_focus.clone();
        let on_blur = self.on_blur.clone();
        let on_validate = self.on_validate.clone();

        let has_callbacks = on_change.is_some()
            || on_enter.is_some()
            || on_focus.is_some()
            || on_blur.is_some()
            || on_validate.is_some();
        let subscription = has_callbacks.then(|| {
            cx.subscribe(&self.state, move |emitter, event: &InputEvent, cx| {
                // Custom formatters only change the rendered representation.
                // Propagate the canonical value so a controlled input can feed
                // the callback back into `value` without formatting it twice.
                let callback_value = || emitter.read(cx).raw_value().into();
                match event {
                    InputEvent::Change => {
                        if let Some(callback) = on_change.as_ref() {
                            callback(callback_value(), cx);
                        }
                    }
                    InputEvent::Enter => {
                        if let Some(callback) = on_enter.as_ref() {
                            callback(callback_value(), cx);
                        }
                    }
                    InputEvent::Focus => {
                        if let Some(callback) = on_focus.as_ref() {
                            callback(callback_value(), cx);
                        }
                    }
                    InputEvent::Blur => {
                        if let Some(callback) = on_blur.as_ref() {
                            callback(callback_value(), cx);
                        }
                    }
                    InputEvent::Validate(result) => {
                        if let Some(callback) = on_validate.as_ref() {
                            callback(result.clone(), cx);
                        }
                    }
                    InputEvent::Tab | InputEvent::ShiftTab => {}
                }
            })
        });

        self.state.update(cx, |state, _| {
            state.replace_event_subscription(subscription);
        });
    }

    /// Get height based on size
    fn height(&self) -> Pixels {
        match self.size {
            InputSize::Sm => px(32.0),
            InputSize::Md => px(40.0),
            InputSize::Lg => px(48.0),
        }
    }

    /// Get horizontal padding based on size
    fn padding_x(&self) -> Pixels {
        match self.size {
            InputSize::Sm => px(8.0),
            InputSize::Md => px(12.0),
            InputSize::Lg => px(16.0),
        }
    }

    /// Get font size based on size
    fn font_size(&self) -> Pixels {
        match self.size {
            InputSize::Sm => px(13.0),
            InputSize::Md => px(14.0),
            InputSize::Lg => px(16.0),
        }
    }

    /// Get gap between elements based on size
    fn element_gap(&self) -> Pixels {
        match self.size {
            InputSize::Sm => px(6.0),
            InputSize::Md => px(8.0),
            InputSize::Lg => px(10.0),
        }
    }

    /// Merge builder callbacks into the public ValidationRules representation.
    ///
    /// ValidationRules keeps Arc-based callback fields for API compatibility,
    /// while Input builders own UI-thread Rc callbacks. The conversion is
    /// intentionally kept in this one boundary rather than duplicated during
    /// every render branch.
    #[allow(clippy::arc_with_non_send_sync)]
    fn validation_rules_for_state(&self) -> Option<ValidationRules> {
        let has_builder_callbacks = self.custom_validator.is_some()
            || self.custom_filter.is_some()
            || self.custom_formatter.is_some();
        let mut rules = self
            .validation_rules
            .clone()
            .or_else(|| has_builder_callbacks.then(ValidationRules::default))?;

        if let Some(custom_validator) = self.custom_validator.as_ref() {
            let validator = custom_validator.clone();
            rules.custom_validator = Some(Arc::new(move |value| match validator(value) {
                Ok(()) => Ok(()),
                Err(message) => Err(ValidationError {
                    message: message.into(),
                    field_name: None,
                }),
            }));
        }

        if let Some(custom_filter) = self.custom_filter.as_ref() {
            let filter = custom_filter.clone();
            rules.custom_filter = Some(Arc::new(move |input| filter(input)));
        }

        if let Some(custom_formatter) = self.custom_formatter.as_ref() {
            let formatter = custom_formatter.clone();
            rules.custom_formatter = Some(Arc::new(move |input| formatter(input)));
        }

        Some(rules)
    }
}

impl Styled for Input {
    fn style(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

impl RenderOnce for Input {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let theme = use_theme(cx);
        let height = self.height();
        let padding_x = self.padding_x();
        let font_size = self.font_size();
        let gap = self.element_gap();

        self.state.update(cx, |state, cx| {
            state.disabled = self.disabled;
            if let Some(placeholder) = self.placeholder.clone() {
                if state.placeholder != placeholder {
                    state.placeholder = placeholder;
                    if state.content().is_empty() {
                        state.invalidate_layout_cache();
                    }
                }
            }

            // Enable masking when this builder first transitions the state to a password input.
            // Later renders preserve user-driven visibility toggles.
            if self.password && state.input_type != InputType::Password {
                state.input_type = InputType::Password;
                state.masked = true;
            }

            if let Some(input_type) = self.input_type {
                let input_type_changed = state.input_type != input_type;
                state.input_type = input_type;
                if input_type_changed && input_type == InputType::Password {
                    state.masked = true;
                }
                match input_type {
                    InputType::Tel => state.input_mask = InputMask::Phone,
                    InputType::CreditCard => state.input_mask = InputMask::CreditCard,
                    InputType::Date => state.input_mask = InputMask::Date,
                    InputType::Time => state.input_mask = InputMask::Time,
                    _ => {}
                }
            }

            let builder_formatter_active = self.custom_formatter.is_some()
                || self
                    .validation_rules
                    .as_ref()
                    .is_some_and(|rules| rules.custom_formatter.is_some());

            if let Some(rules) = self.validation_rules_for_state() {
                state.validation_rules = rules;
            }
            state.sync_builder_formatter(builder_formatter_active);

            // Keep the raw value when formatter configuration changes, then
            // refresh the display representation before applying controlled
            // value synchronization.
            state.refresh_formatted_content();

            state.aria_label = self.aria_label.clone();
            state.aria_description = self.aria_description.clone();
            state.autocomplete = self.autocomplete.clone();
            state.helper_text = self.helper_text.clone();

            if let Some(value) = self.value.clone() {
                if !state.value_matches(value.as_ref()) {
                    state.set_value(value, window, cx);
                }
            } else if let Some(value) = self.initial_value.clone() {
                state.apply_initial_value(value, window, cx);
            }
        });

        self.install_event_subscription(cx);

        // A failed validation (e.g. on blur) shows the destructive border just
        // like an explicit `.error(true)`, matching what `aria_invalid`
        // already announces.
        let has_error = self.error || self.state.read(cx).validation_error.is_some();

        let (bg_color, border_color, text_color) =
            crate::capabilities::controls::input_shell_colors(
                self.variant,
                self.disabled,
                has_error,
                &theme.tokens,
            );

        let has_value = !self.state.read(cx).content.is_empty();
        let show_clear = self.clearable && has_value && !self.disabled;
        let state_for_clear = self.state.clone();
        let state_for_password = self.state.clone();

        let input_state = self.state.read(cx);
        let validation_error = input_state.validation_error.clone();
        let success_message = input_state.success_message.clone();
        let composing = input_state.composition_char_count();
        let content_length = input_state.content.chars().count() - composing;
        let max_length = input_state.validation_rules.max_length;
        let is_focused = input_state.focus_handle(cx).is_focused(window);
        let is_masked = input_state.masked;
        let aria_role = if input_state.input_type == InputType::Password {
            Role::PasswordInput
        } else {
            Role::TextInput
        };
        let aria_label = input_state.aria_label.clone();
        let aria_description = input_state.aria_description.clone();
        let aria_placeholder = input_state.placeholder.clone();
        let aria_value: SharedString = if is_masked {
            "•".repeat(input_state.content.chars().count()).into()
        } else {
            input_state.content.clone()
        };
        let aria_required = input_state.validation_rules.required;
        let aria_invalid = has_error;
        let shake_triggered = input_state.shake_triggered;

        if shake_triggered {
            self.state.update(cx, |state, _cx| {
                state.shake_triggered = false;
                state.shake_count = state.shake_count.wrapping_add(1);
            });
        }
        let shake_count = self.state.read(cx).shake_count;

        let shadow_xs = BoxShadow {
            offset: theme.tokens.shadow_xs.offset,
            blur_radius: theme.tokens.shadow_xs.blur_radius,
            spread_radius: theme.tokens.shadow_xs.spread_radius,
            inset: false,
            color: theme.tokens.shadow_xs.color,
        };
        let focus_ring = theme.tokens.focus_ring_light();
        let error_ring_focused = theme.tokens.error_ring();
        let error_ring_unfocused = theme.tokens.error_ring();
        let ring_color = theme.tokens.ring;
        let destructive_color = theme.tokens.destructive;

        let user_style = self.style;

        VStack::new()
            .w_full()
            .gap(px(4.0))
            .child({
                let input_container = wire_actions(&self.state, window, cx)
                    .role(aria_role)
                    .when_some(aria_label, |this, label| this.aria_label(label))
                    .when_some(aria_description, |this, description| {
                        this.aria_description(description)
                    })
                    .aria_value(aria_value)
                    .aria_placeholder(aria_placeholder)
                    .aria_disabled(self.disabled)
                    .aria_required(aria_required)
                    .when(aria_invalid, |this| {
                        this.aria_invalid(gpui::accesskit::Invalid::True)
                    })
                    .child(
                        HStack::new()
                            .h(height)
                            .w_full()
                            .px(padding_x)
                            .gap(gap)
                            .bg(bg_color)
                            .border_1()
                            .border_color(border_color)
                            .rounded(theme.tokens.radius_md)
                            .items_center()
                            .text_size(font_size)
                            .font_family(theme.tokens.font_mono.clone())
                            .text_color(text_color)
                            .shadow(smallvec::smallvec![shadow_xs])
                            .when(!self.disabled, |h| h.cursor(gpui::CursorStyle::IBeam))
                            .when(!self.disabled, |h| {
                                h.hover(move |style| {
                                    style.border_color(if has_error {
                                        destructive_color
                                    } else {
                                        ring_color
                                    })
                                })
                            })
                            .when(is_focused && !self.disabled, |h| {
                                if has_error {
                                    h.border_color(destructive_color)
                                        .shadow(smallvec::smallvec![error_ring_focused])
                                } else {
                                    h.border_color(ring_color)
                                        .shadow(smallvec::smallvec![focus_ring])
                                }
                            })
                            .when(has_error && !is_focused, |h| {
                                h.shadow(smallvec::smallvec![error_ring_unfocused])
                            })
                            .children(self.prefix)
                            .child(div().flex_1().overflow_hidden().child(self.state.clone()))
                            .when(show_clear, |h| {
                                h.child(
                                    div()
                                        .px(px(4.0))
                                        .py(px(4.0))
                                        .rounded(px(4.0))
                                        .cursor_pointer()
                                        .hover(|style| style.bg(theme.tokens.muted))
                                        .on_mouse_down(MouseButton::Left, move |_, window, cx| {
                                            state_for_clear.update(cx, |state, cx| {
                                                state.set_value("", window, cx);
                                            })
                                        })
                                        .child("×")
                                        .text_color(theme.tokens.muted_foreground),
                                )
                            })
                            .when(self.password, |h| {
                                h.child(
                                    div()
                                        .px(px(4.0))
                                        .py(px(4.0))
                                        .rounded(px(4.0))
                                        .cursor_pointer()
                                        .hover(|style| style.bg(theme.tokens.muted))
                                        .on_mouse_down(MouseButton::Left, {
                                            let state = state_for_password.clone();
                                            move |_, window, cx| {
                                                state.update(cx, |state, cx| {
                                                    state.set_masked(!state.masked, cx);
                                                });
                                                window.refresh();
                                            }
                                        })
                                        .child(
                                            Icon::new(if is_masked { "eye" } else { "eye-off" })
                                                .size(px(16.0))
                                                .color(theme.tokens.muted_foreground),
                                        ),
                                )
                            })
                            .children(self.suffix),
                    );

                if shake_count > 0 {
                    input_container
                        .with_animation(
                            ElementId::Name(format!("input-shake-{}", shake_count).into()),
                            Animation::new(Duration::from_millis(300))
                                .with_easing(easings::ease_out_quad),
                            move |el, delta| el.ml(px(shake_offset(delta, 6.0))),
                        )
                        .into_any_element()
                } else {
                    input_container.into_any_element()
                }
            })
            .when(
                self.helper_text.is_some()
                    || validation_error.is_some()
                    || success_message.is_some()
                    || self.show_character_count,
                |v| {
                    let message = if let Some(error) = validation_error.as_ref() {
                        FooterMessage::Error(error)
                    } else if let Some(success) = success_message.as_ref() {
                        if has_value {
                            FooterMessage::Success(success)
                        } else {
                            FooterMessage::Blank
                        }
                    } else if let Some(helper) = self.helper_text.as_ref() {
                        FooterMessage::Helper(helper)
                    } else {
                        FooterMessage::Blank
                    };
                    let character_count = self.show_character_count.then_some(CharacterCount {
                        committed: content_length,
                        composing,
                        max: max_length,
                    });
                    v.child(validation_footer(&theme.tokens, message, character_count))
                },
            )
            .map(|this| {
                let mut vstack = this;
                vstack.style().refine(&user_style);
                vstack
            })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::capabilities::foundation::theme::{install_theme, Theme};
    use std::{
        cell::{Cell, RefCell},
        rc::Rc,
    };

    struct InputRenderTestView {
        state: Entity<InputState>,
        callback_calls: Option<Rc<Cell<usize>>>,
        initial_value: Option<SharedString>,
        value: Option<SharedString>,
    }

    struct FormattedInputRenderTestView {
        state: Entity<InputState>,
        value: SharedString,
        callback_calls: Rc<Cell<usize>>,
        suffix: bool,
    }

    struct FormattedCallbackTestView {
        state: Entity<InputState>,
        values: Rc<RefCell<Vec<SharedString>>>,
    }

    struct ToggleFormatterRenderTestView {
        state: Entity<InputState>,
        formatter_enabled: bool,
    }

    impl Render for FormattedCallbackTestView {
        fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
            let values = self.values.clone();
            Input::new(&self.state)
                .custom_formatter(|input| format!("${:.2}", input.parse::<f64>().unwrap_or(0.0)))
                .on_change(move |value, _| values.borrow_mut().push(value))
        }
    }

    impl Render for FormattedInputRenderTestView {
        fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
            let callback_calls = self.callback_calls.clone();
            let suffix = self.suffix;
            Input::new(&self.state)
                .value(self.value.clone())
                .custom_formatter(move |input| {
                    if suffix {
                        format!("{input} USD")
                    } else {
                        format!("${:.2}", input.parse::<f64>().unwrap_or(0.0))
                    }
                })
                .on_change(move |_, _| callback_calls.set(callback_calls.get() + 1))
        }
    }

    impl Render for ToggleFormatterRenderTestView {
        fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
            let input = Input::new(&self.state).value("12");
            if self.formatter_enabled {
                input.custom_formatter(|value| {
                    format!("${:.2}", value.parse::<f64>().unwrap_or(0.0))
                })
            } else {
                input
            }
        }
    }

    impl Render for InputRenderTestView {
        fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
            Input::new(&self.state)
                .when_some(self.initial_value.clone(), Input::initial_value)
                .when_some(self.value.clone(), Input::value)
                .when_some(self.callback_calls.clone(), |input, calls| {
                    input.on_change(move |_, _| calls.set(calls.get() + 1))
                })
        }
    }

    #[gpui::test]
    fn controlled_value_changes_replace_existing_content() {
        let mut app = TestApp::new();
        app.update(|cx| install_theme(cx, Theme::light()));
        let mut window = app.open_window(|_, cx| InputRenderTestView {
            state: cx.new(InputState::new),
            callback_calls: None,
            initial_value: None,
            value: Some("first".into()),
        });

        window.draw();
        window.update(|view, _, cx| {
            view.value = Some("replacement".into());
            cx.notify();
        });
        window.draw();

        assert_eq!(
            window.read(|view, cx| view.state.read(cx).content().to_string()),
            "replacement"
        );
    }

    #[gpui::test]
    fn unchanged_controlled_value_does_not_emit_change_on_rerender() {
        let mut app = TestApp::new();
        app.update(|cx| install_theme(cx, Theme::light()));
        let calls = Rc::new(Cell::new(0));
        let mut window = app.open_window(|_, cx| InputRenderTestView {
            state: cx.new(|cx| {
                let mut state = InputState::new(cx);
                state.content = "first".into();
                state
            }),
            callback_calls: Some(calls.clone()),
            initial_value: None,
            value: Some("first".into()),
        });

        window.draw();
        window.update(|_, _, cx| cx.notify());
        window.draw();

        assert_eq!(calls.get(), 0);
    }

    #[gpui::test]
    fn formatted_controlled_value_does_not_emit_on_unchanged_rerender() {
        let mut app = TestApp::new();
        app.update(|cx| install_theme(cx, Theme::light()));
        let calls = Rc::new(Cell::new(0));
        let mut window = app.open_window(|_, cx| FormattedInputRenderTestView {
            state: cx.new(InputState::new),
            value: "12".into(),
            callback_calls: calls.clone(),
            suffix: false,
        });

        window.draw();
        let calls_after_initial_render = calls.get();
        assert_eq!(
            window.read(|view, cx| view.state.read(cx).content().to_string()),
            "$12.00"
        );

        window.update(|_, _, cx| cx.notify());
        window.draw();

        assert_eq!(calls.get(), calls_after_initial_render);
    }

    #[gpui::test]
    fn changing_formatter_reformats_the_controlled_value() {
        let mut app = TestApp::new();
        app.update(|cx| install_theme(cx, Theme::light()));
        let calls = Rc::new(Cell::new(0));
        let mut window = app.open_window(|_, cx| FormattedInputRenderTestView {
            state: cx.new(InputState::new),
            value: "12".into(),
            callback_calls: calls.clone(),
            suffix: false,
        });

        window.draw();
        assert_eq!(
            window.read(|view, cx| view.state.read(cx).content().to_string()),
            "$12.00"
        );

        window.update(|view, _, cx| {
            view.suffix = true;
            cx.notify();
        });
        window.draw();

        assert_eq!(
            window.read(|view, cx| view.state.read(cx).content().to_string()),
            "12 USD"
        );
    }

    #[gpui::test]
    fn removing_builder_formatter_restores_raw_display_value() {
        let mut app = TestApp::new();
        app.update(|cx| install_theme(cx, Theme::light()));
        let mut window = app.open_window(|_, cx| ToggleFormatterRenderTestView {
            state: cx.new(InputState::new),
            formatter_enabled: true,
        });

        window.draw();
        assert_eq!(
            window.read(|view, cx| view.state.read(cx).content().to_string()),
            "$12.00"
        );

        window.update(|view, _, cx| {
            view.formatter_enabled = false;
            cx.notify();
        });
        window.draw();

        assert_eq!(
            window.read(|view, cx| view.state.read(cx).content().to_string()),
            "12"
        );
    }

    #[gpui::test]
    fn formatted_input_change_callback_reports_raw_value() {
        let mut app = TestApp::new();
        app.update(|cx| install_theme(cx, Theme::light()));
        let state = app.update(|cx| cx.new(InputState::new));
        let values = Rc::new(RefCell::new(Vec::new()));
        let values_for_view = values.clone();
        let mut window = app.open_window(|_, _| FormattedCallbackTestView {
            state: state.clone(),
            values: values_for_view,
        });

        window.draw();
        window.update(|view, window, cx| {
            view.state.update(cx, |state, cx| {
                state.set_value("123", window, cx);
            });
        });

        assert_eq!(
            values.borrow().last().map(SharedString::as_ref),
            Some("123")
        );
    }

    #[gpui::test]
    fn clearable_builder_enables_clear_button() {
        let mut app = TestApp::new();
        app.update(|cx| install_theme(cx, Theme::light()));
        let state = app.update(|cx| cx.new(InputState::new));

        let input = Input::new(&state).clearable(true);

        assert!(input.clearable);
    }

    #[gpui::test]
    fn repeated_render_replaces_input_event_subscription() {
        let mut app = TestApp::new();
        app.update(|cx| install_theme(cx, Theme::light()));
        let stale_calls = Rc::new(Cell::new(0));
        let current_calls = Rc::new(Cell::new(0));
        let mut window = app.open_window(|_, cx| InputRenderTestView {
            state: cx.new(InputState::new),
            callback_calls: Some(stale_calls.clone()),
            initial_value: None,
            value: None,
        });

        window.draw();
        window.update(|view, _, cx| {
            view.callback_calls = Some(current_calls.clone());
            cx.notify();
        });
        window.draw();
        window.update(|view, _, cx| {
            view.state.update(cx, |_, cx| cx.emit(InputEvent::Change));
        });
        assert_eq!(stale_calls.get(), 0);
        assert_eq!(current_calls.get(), 1);

        window.update(|view, _, cx| {
            view.callback_calls = None;
            cx.notify();
        });
        window.draw();
        window.update(|view, _, cx| {
            view.state.update(cx, |_, cx| cx.emit(InputEvent::Change));
        });
        assert_eq!(current_calls.get(), 1);
    }

    #[gpui::test]
    fn initial_value_does_not_overwrite_user_edits_on_rerender() {
        let mut app = TestApp::new();
        app.update(|cx| install_theme(cx, Theme::light()));
        let mut window = app.open_window(|_, cx| InputRenderTestView {
            state: cx.new(InputState::new),
            callback_calls: None,
            initial_value: Some("initial".into()),
            value: None,
        });

        window.draw();
        window.update(|view, _, cx| {
            view.state.update(cx, |state, cx| {
                state.content = "edited".into();
                cx.notify();
            });
            cx.notify();
        });
        window.draw();

        assert_eq!(
            window.read(|view, cx| view.state.read(cx).content().to_string()),
            "edited"
        );
    }

    #[gpui::test]
    fn builder_initial_value_changes_are_ignored_after_first_render() {
        let mut app = TestApp::new();
        app.update(|cx| install_theme(cx, Theme::light()));
        let mut window = app.open_window(|_, cx| InputRenderTestView {
            state: cx.new(InputState::new),
            callback_calls: None,
            initial_value: Some("initial".into()),
            value: None,
        });

        window.draw();
        window.update(|view, _, cx| {
            view.initial_value = Some("replacement".into());
            cx.notify();
        });
        window.draw();

        assert_eq!(
            window.read(|view, cx| view.state.read(cx).content().to_string()),
            "initial"
        );
    }

    #[gpui::test]
    fn set_value_emits_one_change_event() {
        let mut app = TestApp::new();
        app.update(|cx| install_theme(cx, Theme::light()));
        let calls = Rc::new(Cell::new(0));
        let mut window = app.open_window(|_, cx| InputRenderTestView {
            state: cx.new(InputState::new),
            callback_calls: Some(calls.clone()),
            initial_value: None,
            value: None,
        });

        window.draw();
        window.update(|view, window, cx| {
            view.state
                .update(cx, |state, cx| state.set_value("edited", window, cx));
        });

        assert_eq!(calls.get(), 1);
    }

    #[gpui::test]
    fn set_value_replaces_existing_content() {
        let mut app = TestApp::new();
        app.update(|cx| install_theme(cx, Theme::light()));
        let mut window = app.open_window(|_, cx| InputRenderTestView {
            state: cx.new(InputState::new),
            callback_calls: None,
            initial_value: Some("initial".into()),
            value: None,
        });

        window.draw();
        window.update(|view, window, cx| {
            view.state
                .update(cx, |state, cx| state.set_value("edited", window, cx));
        });

        assert_eq!(
            window.read(|view, cx| view.state.read(cx).content().to_string()),
            "edited"
        );
    }

    struct BareInputTestView {
        state: Entity<InputState>,
    }

    impl Render for BareInputTestView {
        fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
            wire_actions(&self.state, window, cx)
                .h(px(36.0))
                .px(px(12.0))
                .rounded(px(8.0))
                .child(self.state.clone())
        }
    }

    #[gpui::test]
    fn wire_actions_gives_custom_input_containers_full_keyboard_editing() {
        let mut app = TestApp::new();
        app.update(|cx| {
            install_theme(cx, Theme::light());
            crate::capabilities::controls::input::init(cx);
        });
        let state = app.update(|cx| cx.new(|cx| InputState::new(cx).value("hello")));
        let mut window = app.open_window(|_, _| BareInputTestView {
            state: state.clone(),
        });

        window.draw();
        window.update(|_, window, cx| state.read(cx).focus_handle(cx).focus(window));
        window.draw();
        let handle: AnyWindowHandle = window.handle().into();
        app.raw_context_mut()
            .simulate_keystrokes(handle, "backspace");
        app.flush();

        app.update(|cx| {
            assert_eq!(state.read(cx).content(), "hell");
        });
    }
}