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
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
//! Input component
//!
//! Text input field with optional label, placeholder, and validation.
//!
//! Features:
//! - Full keyboard text editing support (self-contained)
//! - Click to focus and start editing
//! - Enter to confirm, Escape to cancel
//! - Cursor navigation and text selection
//! - Mouse drag to select text, double-click to select all
//! - Clipboard support: Cmd+C (copy), Cmd+X (cut), Cmd+V (paste), Cmd+A (select all)
//! - Emacs-style keybindings (Ctrl+A/E/K/U/W/H/D/F/B)
//! - Disabled and readonly states
//!
//! # Simple Usage
//!
//! The Input component handles all focus and keyboard events internally.
//! Just provide callbacks for changes:
//!
//! ```ignore
//! Input::new("my-input")
//!     .value(current_value)
//!     .placeholder("Enter text...")
//!     .on_change(|new_value, _window, _cx| {
//!         // Called when user confirms with Enter
//!         println!("Value changed to: {}", new_value);
//!     })
//!     .on_text_change(|text, _window, _cx| {
//!         // Called on every keystroke (optional, for live updates)
//!         println!("Current text: {}", text);
//!     })
//! ```
//!
//! # Thread-Local State Pattern
//!
//! This component uses `thread_local!` storage to persist focus handles and
//! edit state across renders. This is necessary because GPUI's `RenderOnce`
//! components are recreated on each render, but we need state to persist:
//!
//! - **Focus handles**: Must be the same instance across renders or focus is lost
//! - **Edit state**: Cursor position, text, and selection must persist during editing
//!
//! ## Memory Considerations
//!
//! The thread-local `HashMap` entries grow as new element IDs are used and are
//! never automatically cleaned up. For most applications this is fine because:
//! - Element IDs are typically static or part of a bounded set
//! - The stored data is small (FocusHandle, EditState)
//!
//! If you have dynamic element IDs (e.g., from a virtualized list), consider:
//! 1. Using a stable ID scheme that reuses IDs
//! 2. Calling `cleanup_input_state(id)` when components are removed
//!
//! ## Cleanup Function
//!
//! To manually clean up state for a removed element:
//! ```rust,ignore
//! cleanup_input_state(&element_id);
//! ```

use crate::ComponentTheme;
use crate::theme::ThemeExt;
use gpui::prelude::*;
use gpui::*;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;

// Maximum number of input states to retain in thread-local storage.
// Excess states will be automatically evicted (oldest first).
// This prevents memory leaks when cleanup functions are not called.
const MAX_THREAD_LOCAL_INPUT_STATES: usize = 1000;

// Thread-local registry for focus handles, keyed by element ID.
// This ensures the same focus handle is reused across renders for Input components
// that don't provide their own focus handle. Without this, focus would be lost
// on every re-render since Input is a RenderOnce component.
thread_local! {
    static FOCUS_HANDLES: RefCell<HashMap<ElementId, FocusHandle>> = RefCell::new(HashMap::new());
}

// Thread-local registry for edit state, keyed by element ID.
// This ensures edit state (cursor position, current text, selection) persists
// across renders. Without this, every re-render would reset the editing state.
//
// Note: State is automatically evicted when exceeding MAX_THREAD_LOCAL_INPUT_STATES.
// For applications with many dynamic inputs, call cleanup_input_state() or
// cleanup_stale_input_states() periodically to manage memory explicitly.
thread_local! {
    static EDIT_STATES: RefCell<HashMap<ElementId, Rc<RefCell<EditState>>>> = RefCell::new(HashMap::new());
}

/// Evict oldest entries if thread-local storage exceeds maximum size.
/// This prevents unbounded memory growth when cleanup functions are not called.
/// Returns the number of entries evicted from each map.
fn trim_thread_local_storage() -> (usize, usize) {
    let mut focus_evicted = 0;
    let mut edit_evicted = 0;

    FOCUS_HANDLES.with(|handles| {
        let mut handles = handles.borrow_mut();
        while handles.len() > MAX_THREAD_LOCAL_INPUT_STATES {
            if let Some(key) = handles.keys().next().cloned() {
                handles.remove(&key);
                focus_evicted += 1;
            }
        }
    });

    EDIT_STATES.with(|states| {
        let mut states = states.borrow_mut();
        while states.len() > MAX_THREAD_LOCAL_INPUT_STATES {
            if let Some(key) = states.keys().next().cloned() {
                states.remove(&key);
                edit_evicted += 1;
            }
        }
    });

    (focus_evicted, edit_evicted)
}

/// Clean up thread-local state for an Input element.
///
/// Call this when removing an Input with a dynamic element ID to prevent
/// memory leaks. For static element IDs, cleanup is not necessary.
///
/// # Example
/// ```rust,ignore
/// // When removing a dynamically-created Input
/// cleanup_input_state(&ElementId::Name(format!("input-{}", item_id).into()));
/// ```
pub fn cleanup_input_state(id: &ElementId) {
    FOCUS_HANDLES.with(|handles| {
        handles.borrow_mut().remove(id);
    });
    EDIT_STATES.with(|states| {
        states.borrow_mut().remove(id);
    });
}

/// Clean up thread-local state for Input elements whose IDs are not in the retained set.
///
/// This is useful for virtualized lists where you want to keep state only for
/// currently visible items. Call this periodically or when the visible set changes.
///
/// # Example
/// ```rust,ignore
/// // Keep only the inputs currently in view
/// let visible_ids: HashSet<ElementId> = visible_items
///     .iter()
///     .map(|item| ElementId::Name(format!("input-{}", item.id).into()))
///     .collect();
/// cleanup_stale_input_states(&visible_ids);
/// ```
pub fn cleanup_stale_input_states(retained_ids: &std::collections::HashSet<ElementId>) {
    FOCUS_HANDLES.with(|handles| {
        handles
            .borrow_mut()
            .retain(|id, _| retained_ids.contains(id));
    });
    EDIT_STATES.with(|states| {
        states
            .borrow_mut()
            .retain(|id, _| retained_ids.contains(id));
    });
}

/// Get the current count of stored input states.
///
/// Useful for debugging memory leaks. If this number grows beyond
/// MAX_THREAD_LOCAL_INPUT_STATES, older entries will be automatically evicted.
///
/// # Returns
/// A tuple of (focus_handle_count, edit_state_count)
pub fn input_state_count() -> (usize, usize) {
    let _ = trim_thread_local_storage();
    let focus_count = FOCUS_HANDLES.with(|handles| handles.borrow().len());
    let edit_count = EDIT_STATES.with(|states| states.borrow().len());
    (focus_count, edit_count)
}

/// Clear all input states.
///
/// This removes all thread-local state for all Input components.
/// Use with caution - this will reset all input editing state.
pub fn clear_all_input_states() {
    FOCUS_HANDLES.with(|handles| {
        handles.borrow_mut().clear();
    });
    EDIT_STATES.with(|states| {
        states.borrow_mut().clear();
    });
}

/// Theme colors for input styling
#[derive(Debug, Clone, ComponentTheme)]
pub struct InputTheme {
    /// Background color
    #[theme(default = 0x1e1e1e, from = background)]
    pub background: Rgba,
    /// Filled variant background
    #[theme(default = 0x2a2a2a, from = surface)]
    pub filled_bg: Rgba,
    /// Text color
    #[theme(default = 0xffffff, from = text_primary)]
    pub text: Rgba,
    /// Placeholder color
    #[theme(default = 0x666666, from = text_muted)]
    pub placeholder: Rgba,
    /// Label color
    #[theme(default = 0xcccccc, from = text_secondary)]
    pub label: Rgba,
    /// Border color
    #[theme(default = 0x3a3a3a, from = border)]
    pub border: Rgba,
    /// Border hover color
    #[theme(default = 0x007acc, from = accent)]
    pub border_hover: Rgba,
    /// Border focus color
    #[theme(default = 0x007acc, from = accent)]
    pub border_focus: Rgba,
    /// Error color
    #[theme(default = 0xcc3333, from = error)]
    pub error: Rgba,
    /// Cursor color
    #[theme(default = 0x007acc, from = accent)]
    pub cursor: Rgba,
    /// Selection background
    #[theme(
        default = 0x007acc44,
        from_expr = "Rgba { r: theme.accent.r, g: theme.accent.g, b: theme.accent.b, a: 0.3 }"
    )]
    pub selection_bg: Rgba,
    /// Transparent color (for invisible borders/backgrounds)
    #[theme(default = 0x00000000, from = transparent)]
    pub transparent: Rgba,
}

/// Input size variants
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum InputSize {
    /// Small input
    Sm,
    /// Medium input (default)
    #[default]
    Md,
    /// Large input
    Lg,
}

impl From<crate::ComponentSize> for InputSize {
    fn from(size: crate::ComponentSize) -> Self {
        match size {
            crate::ComponentSize::Xs | crate::ComponentSize::Sm => Self::Sm,
            crate::ComponentSize::Md => Self::Md,
            crate::ComponentSize::Lg | crate::ComponentSize::Xl => Self::Lg,
        }
    }
}

/// Input visual variant
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum InputVariant {
    /// Default input style
    #[default]
    Default,
    /// Filled background
    Filled,
    /// Flushed (bottom border only)
    Flushed,
}

/// Internal editing state for the input
#[derive(Clone, Default)]
struct EditState {
    /// Whether currently editing
    editing: bool,
    /// Current edit text
    text: String,
    /// Cursor position (character index)
    cursor: usize,
    /// Selection anchor (where selection started). If Some, selection is from anchor to cursor.
    selection_anchor: Option<usize>,
    /// Whether currently dragging to select
    is_dragging: bool,
}

impl EditState {
    fn new(value: &str) -> Self {
        let len = value.chars().count();
        Self {
            editing: true,
            text: value.to_string(),
            cursor: len,
            selection_anchor: Some(0), // Select all by default
            is_dragging: false,
        }
    }

    /// Check if there's any selection
    #[allow(dead_code)]
    fn has_selection(&self) -> bool {
        if let Some(anchor) = self.selection_anchor {
            anchor != self.cursor
        } else {
            false
        }
    }

    /// Get selection range (start, end) where start <= end
    fn selection_range(&self) -> Option<(usize, usize)> {
        self.selection_anchor.map(|anchor| {
            let start = anchor.min(self.cursor);
            let end = anchor.max(self.cursor);
            (start, end)
        })
    }

    /// Check if all text is selected
    #[allow(dead_code)]
    fn is_all_selected(&self) -> bool {
        if let Some((start, end)) = self.selection_range() {
            start == 0 && end == self.text.chars().count()
        } else {
            false
        }
    }

    /// Get the currently selected text
    fn get_selected_text(&self) -> Option<String> {
        if let Some((start, end)) = self.selection_range()
            && start != end
        {
            let chars: Vec<char> = self.text.chars().collect();
            return Some(chars[start..end].iter().collect());
        }
        None
    }

    fn clear_selection(&mut self) {
        self.selection_anchor = None;
    }

    fn move_to_start(&mut self) {
        self.cursor = 0;
        self.clear_selection();
    }

    fn move_to_end(&mut self) {
        self.cursor = self.text.chars().count();
        self.clear_selection();
    }

    fn move_forward(&mut self) {
        let len = self.text.chars().count();
        if self.cursor < len {
            self.cursor += 1;
        }
        self.clear_selection();
    }

    fn move_backward(&mut self) {
        if self.cursor > 0 {
            self.cursor -= 1;
        }
        self.clear_selection();
    }

    fn select_all(&mut self) {
        self.selection_anchor = Some(0);
        self.cursor = self.text.chars().count();
    }

    fn kill_to_end(&mut self) {
        let chars: Vec<char> = self.text.chars().collect();
        self.text = chars[..self.cursor].iter().collect();
        self.clear_selection();
    }

    fn kill_to_start(&mut self) {
        let chars: Vec<char> = self.text.chars().collect();
        self.text = chars[self.cursor..].iter().collect();
        self.cursor = 0;
        self.clear_selection();
    }

    fn kill_word_backward(&mut self) {
        if self.cursor == 0 {
            return;
        }
        let chars: Vec<char> = self.text.chars().collect();
        let mut new_pos = self.cursor;
        // Skip trailing spaces
        while new_pos > 0 && chars[new_pos - 1].is_whitespace() {
            new_pos -= 1;
        }
        // Skip word characters
        while new_pos > 0 && !chars[new_pos - 1].is_whitespace() {
            new_pos -= 1;
        }
        let mut new_chars = chars[..new_pos].to_vec();
        new_chars.extend_from_slice(&chars[self.cursor..]);
        self.text = new_chars.into_iter().collect();
        self.cursor = new_pos;
        self.clear_selection();
    }

    /// Delete selected text, returning true if something was deleted
    fn delete_selection(&mut self) -> bool {
        if let Some((start, end)) = self.selection_range()
            && start != end
        {
            let chars: Vec<char> = self.text.chars().collect();
            let mut new_chars = chars[..start].to_vec();
            new_chars.extend_from_slice(&chars[end..]);
            self.text = new_chars.into_iter().collect();
            self.cursor = start;
            self.clear_selection();
            return true;
        }
        false
    }

    fn do_backspace(&mut self) {
        if self.delete_selection() {
            return;
        }
        if self.cursor > 0 {
            // Find byte positions for character before cursor
            let byte_pos = self
                .text
                .char_indices()
                .nth(self.cursor - 1)
                .map(|(i, _)| i)
                .unwrap_or(0);
            let next_byte = self
                .text
                .char_indices()
                .nth(self.cursor)
                .map(|(i, _)| i)
                .unwrap_or(self.text.len());
            self.text.replace_range(byte_pos..next_byte, "");
            self.cursor -= 1;
        }
    }

    fn do_delete(&mut self) {
        if self.delete_selection() {
            return;
        }
        let len = self.text.chars().count();
        if self.cursor < len {
            // Find byte positions for character at cursor
            let byte_pos = self
                .text
                .char_indices()
                .nth(self.cursor)
                .map(|(i, _)| i)
                .unwrap_or(self.text.len());
            let next_byte = self
                .text
                .char_indices()
                .nth(self.cursor + 1)
                .map(|(i, _)| i)
                .unwrap_or(self.text.len());
            self.text.replace_range(byte_pos..next_byte, "");
        }
    }

    fn insert_text(&mut self, char_text: &str) {
        self.delete_selection();
        // Find byte position for insertion
        let byte_pos = self
            .text
            .char_indices()
            .nth(self.cursor)
            .map(|(i, _)| i)
            .unwrap_or(self.text.len());
        self.text.insert_str(byte_pos, char_text);
        self.cursor += char_text.chars().count();
    }

    /// Start a selection at the given position
    fn start_selection(&mut self, pos: usize) {
        self.cursor = pos;
        self.selection_anchor = Some(pos);
        self.is_dragging = true;
    }

    /// Select word at the given position
    fn select_word_at(&mut self, pos: usize) {
        let text = &self.text;
        let len = text.chars().count();
        if len == 0 {
            return;
        }
        let pos = pos.min(len);
        let chars: Vec<char> = text.chars().collect();

        // Helper to check if char is part of a word (alphanumeric or underscore)
        let is_word_char = |c: char| c.is_alphanumeric() || c == '_';

        // Find start of word
        let mut start = pos;
        if start < len && !is_word_char(chars[start]) && start > 0 && is_word_char(chars[start - 1])
        {
            // Clicked just after a word, select that word
            start -= 1;
        }

        // If we are on a non-word char (like whitespace), select the run of whitespace/symbols?
        // Standard behavior: double click on whitespace selects whitespace run.
        let target_is_word = start < len && is_word_char(chars[start]);

        while start > 0 {
            let prev = chars[start - 1];
            if is_word_char(prev) != target_is_word {
                break;
            }
            start -= 1;
        }

        // Find end of word
        let mut end = pos;
        // Ensure we start searching from at least 'start'
        if end < start {
            end = start;
        }

        while end < len {
            let curr = chars[end];
            if is_word_char(curr) != target_is_word {
                break;
            }
            end += 1;
        }

        self.selection_anchor = Some(start);
        self.cursor = end;
    }

    /// Update selection during drag
    fn update_selection(&mut self, pos: usize) {
        self.cursor = pos;
    }

    /// End selection drag
    fn end_selection(&mut self) {
        self.is_dragging = false;
        // If no actual selection (anchor == cursor), clear the anchor
        if let Some(anchor) = self.selection_anchor
            && anchor == self.cursor
        {
            self.selection_anchor = None;
        }
    }
}

/// A text input component with full keyboard editing support
///
/// The Input handles all focus and keyboard events internally.
/// Parent components only need to provide callbacks for value changes.
pub struct Input {
    id: ElementId,
    value: SharedString,
    placeholder: Option<SharedString>,
    label: Option<SharedString>,
    size: InputSize,
    variant: InputVariant,
    disabled: bool,
    readonly: bool,
    error: Option<SharedString>,
    icon_left: Option<SharedString>,
    icon_right: Option<SharedString>,
    bg_color: Option<Rgba>,
    text_color: Option<Rgba>,
    border_color: Option<Rgba>,
    placeholder_color: Option<Rgba>,
    /// Called when value is confirmed (Enter pressed)
    on_change: Option<Box<dyn Fn(&str, &mut Window, &mut App) + 'static>>,
    /// Called when editing starts (click on input)
    on_edit_start: Option<Box<dyn Fn(&mut Window, &mut App) + 'static>>,
    /// Called when editing ends (Enter = Some(value), Escape = None)
    on_edit_end: Option<Box<dyn Fn(Option<String>, &mut Window, &mut App) + 'static>>,
    /// Called on every text change during editing (for live updates)
    on_text_change: Option<Box<dyn Fn(String, &mut Window, &mut App) + 'static>>,
    /// Focus handle for this input
    focus_handle: Option<FocusHandle>,
}

impl Input {
    /// Create a new input
    pub fn new(id: impl Into<ElementId>) -> Self {
        Self {
            id: id.into(),
            value: "".into(),
            placeholder: None,
            label: None,
            size: InputSize::default(),
            variant: InputVariant::default(),
            disabled: false,
            readonly: false,
            error: None,
            icon_left: None,
            icon_right: None,
            bg_color: None,
            text_color: None,
            border_color: None,
            placeholder_color: None,
            on_change: None,
            on_edit_start: None,
            on_edit_end: None,
            on_text_change: None,
            focus_handle: None,
        }
    }

    /// Set the focus handle (optional - one is created internally if not provided)
    pub fn focus_handle(mut self, handle: FocusHandle) -> Self {
        self.focus_handle = Some(handle);
        self
    }

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

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

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

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

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

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

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

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

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

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

    /// Set background color
    pub fn bg_color(mut self, color: impl Into<Rgba>) -> Self {
        self.bg_color = Some(color.into());
        self
    }

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

    /// Set border color
    pub fn border_color(mut self, color: impl Into<Rgba>) -> Self {
        self.border_color = Some(color.into());
        self
    }

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

    /// Set change handler (called when input value is confirmed with Enter)
    pub fn on_change(mut self, handler: impl Fn(&str, &mut Window, &mut App) + 'static) -> Self {
        self.on_change = Some(Box::new(handler));
        self
    }

    /// Set edit start handler (called when user clicks on input to edit)
    pub fn on_edit_start(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
        self.on_edit_start = Some(Box::new(handler));
        self
    }

    /// Set edit end handler (called when user confirms or cancels edit)
    /// The Option<String> is Some(value) if confirmed, None if cancelled
    pub fn on_edit_end(
        mut self,
        handler: impl Fn(Option<String>, &mut Window, &mut App) + 'static,
    ) -> Self {
        self.on_edit_end = Some(Box::new(handler));
        self
    }

    /// Set text change handler (called on every keystroke during editing)
    pub fn on_text_change(
        mut self,
        handler: impl Fn(String, &mut Window, &mut App) + 'static,
    ) -> Self {
        self.on_text_change = Some(Box::new(handler));
        self
    }
}

impl RenderOnce for Input {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let global_theme = cx.theme();
        let theme = InputTheme::from(&global_theme);

        let (py, _text_size_class) = match self.size {
            InputSize::Sm => (px(4.0), "text_xs"),
            InputSize::Md => (px(8.0), "text_sm"),
            InputSize::Lg => (px(12.0), "text_base"),
        };

        let has_error = self.error.is_some();
        let disabled = self.disabled;
        let readonly = self.readonly;
        let current_value = self.value.clone();

        // Use provided focus handle, or get/create one from the registry.
        // The registry ensures the same focus handle is reused across renders,
        // which is critical since Input is a RenderOnce component.
        let focus_handle = self.focus_handle.unwrap_or_else(|| {
            FOCUS_HANDLES.with(|handles| {
                let mut handles = handles.borrow_mut();
                handles
                    .entry(self.id.clone())
                    .or_insert_with(|| cx.focus_handle())
                    .clone()
            })
        });

        // Determine editing state from focus
        // The input is "editing" when it has focus
        let is_focused = focus_handle.is_focused(window);

        // When focused, we're always in editing mode
        let editing = is_focused && !disabled && !readonly;

        // Get or create edit state from registry (persists across renders)
        let edit_state = EDIT_STATES.with(|states| {
            let mut states = states.borrow_mut();
            states
                .entry(self.id.clone())
                .or_insert_with(|| Rc::new(RefCell::new(EditState::default())))
                .clone()
        });

        // Get display state from edit_state
        let state = edit_state.borrow();
        let selection_anchor = if editing {
            state.selection_anchor
        } else {
            None
        };
        let cursor_pos = state.cursor;
        let _is_dragging = state.is_dragging;
        // When editing, display the internal state.text; otherwise display props value
        let edit_text = if editing && state.editing {
            state.text.clone()
        } else {
            current_value.to_string()
        };
        drop(state);

        let border_color = if has_error {
            theme.error
        } else if editing {
            theme.border_focus
        } else {
            self.border_color.unwrap_or(theme.border)
        };

        let mut container = div().flex().flex_col().gap_1();

        // Label
        if let Some(label) = &self.label {
            container = container.child(
                div()
                    .text_sm()
                    .text_color(theme.label)
                    .font_weight(FontWeight::MEDIUM)
                    .child(label.clone()),
            );
        }

        // Create a unique ID for the input field
        let field_id = ElementId::Name(SharedString::from(format!("{:?}-field", self.id)));

        // Input wrapper
        let mut input_wrapper = div()
            .id(self.id.clone())
            .track_focus(&focus_handle)
            .flex()
            .items_center()
            .gap_2()
            .px_3()
            .py(py)
            .rounded_md()
            .border_1()
            .border_color(border_color)
            .focusable();

        // Apply variant styling
        match self.variant {
            InputVariant::Default => {
                input_wrapper = input_wrapper.bg(self.bg_color.unwrap_or(theme.background));
            }
            InputVariant::Filled => {
                input_wrapper = input_wrapper
                    .bg(self.bg_color.unwrap_or(theme.filled_bg))
                    .border_color(theme.transparent);
            }
            InputVariant::Flushed => {
                input_wrapper = input_wrapper
                    .bg(theme.transparent)
                    .border_0()
                    .border_b_1()
                    .border_color(border_color)
                    .rounded_none();
            }
        }

        let border_hover = theme.border_hover;
        if disabled {
            input_wrapper = input_wrapper.opacity(0.5).cursor_not_allowed();
        } else if !readonly {
            input_wrapper = input_wrapper
                .cursor_text()
                .hover(move |s| s.border_color(border_hover));
        }

        let placeholder_color = self.placeholder_color.unwrap_or(theme.placeholder);
        let text_color = self.text_color.unwrap_or(theme.text);
        let selection_bg = theme.selection_bg;
        let cursor_color = theme.cursor;

        // Wrap handlers in Rc for sharing
        let on_change_rc = self.on_change.map(Rc::new);
        let on_edit_start_rc = self.on_edit_start.map(Rc::new);
        let on_edit_end_rc = self.on_edit_end.map(Rc::new);
        let on_text_change_rc = self.on_text_change.map(Rc::new);

        // Add click handler - focus and start editing
        // Double-click selects word
        // Single click positions cursor, drag selects text
        if !disabled && !readonly {
            let focus_handle_for_click = focus_handle.clone();
            let edit_state_for_click = edit_state.clone();
            let value_for_click = current_value.to_string();
            let on_edit_start_click = on_edit_start_rc.clone();
            let edit_text_for_click = edit_text.clone();

            input_wrapper =
                input_wrapper.on_mouse_down(MouseButton::Left, move |event, window, cx| {
                    // Focus the input
                    window.focus(&focus_handle_for_click);

                    let mut state = edit_state_for_click.borrow_mut();

                    // Calculate cursor position from click
                    // Use a simple heuristic: assume monospace ~8px per character
                    // TODO: Replace with proper text layout measurement when available in GPUI
                    let text_len = edit_text_for_click.chars().count();
                    let char_width = 8.0_f32; // Approximate width per character
                    let click_x: f32 = event.position.x.into();
                    let char_pos = ((click_x / char_width).round() as usize).min(text_len);

                    // Double-click: select word at cursor
                    if event.click_count == 2 {
                        if !state.editing {
                            *state = EditState::new(&value_for_click);
                        }
                        state.select_word_at(char_pos);
                        drop(state);
                        window.refresh();
                        return;
                    }

                    // Single click: start editing if not already, position cursor
                    if !state.editing {
                        *state = EditState::new(&value_for_click);
                        state.cursor = char_pos;
                        state.clear_selection();
                        drop(state);

                        // Call on_edit_start callback
                        if let Some(ref handler) = on_edit_start_click {
                            handler(window, cx);
                        }
                    } else {
                        state.start_selection(char_pos);
                        drop(state);
                    }
                    window.refresh();
                });

            // Mouse move handler for drag selection
            let edit_state_for_move = edit_state.clone();
            let edit_text_for_move = edit_text.clone();

            input_wrapper = input_wrapper.on_mouse_move(move |event, window, _cx| {
                let mut state = edit_state_for_move.borrow_mut();
                if state.is_dragging && state.editing {
                    let text_len = edit_text_for_move.chars().count();
                    let char_width = 8.0_f32;
                    let move_x: f32 = event.position.x.into();
                    let char_pos = ((move_x / char_width).round() as usize).min(text_len);
                    state.update_selection(char_pos);
                    drop(state);
                    window.refresh();
                }
            });

            // Mouse up handler to end drag selection
            let edit_state_for_up = edit_state.clone();

            input_wrapper =
                input_wrapper.on_mouse_up(MouseButton::Left, move |_event, window, _cx| {
                    let mut state = edit_state_for_up.borrow_mut();
                    if state.is_dragging {
                        state.end_selection();
                        drop(state);
                        window.refresh();
                    }
                });
        }

        // Add keyboard event handling... [unchanged]
        // ... (Keyboard handling logic remains same, skipping for brevity in replacement if possible, but I must replace contiguous block)
        // Since I need to output valid Rust code, I will include the keyboard logic.
        if !disabled && !readonly {
            let edit_state_for_key = edit_state.clone();
            let on_edit_end_key = on_edit_end_rc.clone();
            let on_text_change_key = on_text_change_rc.clone();
            let on_change_key = on_change_rc.clone();
            let focus_handle_for_key = focus_handle.clone();
            let current_value_for_key = current_value.to_string();

            input_wrapper = input_wrapper.on_key_down(move |event, window, cx| {
                if !focus_handle_for_key.is_focused(window) {
                    return;
                }
                cx.stop_propagation();

                let key = event.keystroke.key.as_str();
                let ctrl = event.keystroke.modifiers.control;
                let cmd = event.keystroke.modifiers.platform;

                let mut state = edit_state_for_key.borrow_mut();
                if !state.editing {
                    state.text = current_value_for_key.clone();
                    state.editing = true;
                    state.cursor = state.text.chars().count();
                    state.selection_anchor = Some(0);
                }

                if cmd {
                    match key {
                        "c" => {
                            if let Some(selected) = state.get_selected_text() {
                                drop(state);
                                cx.write_to_clipboard(ClipboardItem::new_string(selected));
                            }
                            return;
                        }
                        "x" => {
                            if let Some(selected) = state.get_selected_text() {
                                cx.write_to_clipboard(ClipboardItem::new_string(selected));
                                state.delete_selection();
                                let text = state.text.clone();
                                drop(state);
                                if let Some(ref handler) = on_text_change_key {
                                    handler(text, window, cx);
                                }
                                window.refresh();
                            }
                            return;
                        }
                        "v" => {
                            if let Some(clipboard) = cx.read_from_clipboard()
                                && let Some(paste_text) = clipboard.text()
                            {
                                state.insert_text(&paste_text);
                                let text = state.text.clone();
                                drop(state);
                                if let Some(ref handler) = on_text_change_key {
                                    handler(text, window, cx);
                                }
                                window.refresh();
                            }
                            return;
                        }
                        "a" => {
                            state.select_all();
                            drop(state);
                            window.refresh();
                            return;
                        }
                        _ => {}
                    }
                }

                if ctrl {
                    match key {
                        "a" => state.move_to_start(),
                        "e" => state.move_to_end(),
                        "k" => state.kill_to_end(),
                        "u" => state.kill_to_start(),
                        "w" => state.kill_word_backward(),
                        "h" => state.do_backspace(),
                        "d" => state.do_delete(),
                        "f" => state.move_forward(),
                        "b" => state.move_backward(),
                        _ => {}
                    }
                    let text = state.text.clone();
                    drop(state);
                    if let Some(ref handler) = on_text_change_key {
                        handler(text, window, cx);
                    }
                    window.refresh();
                    return;
                }

                match key {
                    "enter" => {
                        let text = state.text.clone();
                        state.editing = false;
                        state.clear_selection();
                        drop(state);
                        window.blur();
                        if let Some(ref handler) = on_change_key {
                            handler(&text, window, cx);
                        }
                        if let Some(ref handler) = on_edit_end_key {
                            handler(Some(text), window, cx);
                        }
                    }
                    "escape" => {
                        state.editing = false;
                        state.clear_selection();
                        drop(state);
                        window.blur();
                        if let Some(ref handler) = on_edit_end_key {
                            handler(None, window, cx);
                        }
                    }
                    "backspace" => {
                        state.do_backspace();
                        let text = state.text.clone();
                        drop(state);
                        if let Some(ref handler) = on_text_change_key {
                            handler(text, window, cx);
                        }
                        window.refresh();
                    }
                    "delete" => {
                        state.do_delete();
                        let text = state.text.clone();
                        drop(state);
                        if let Some(ref handler) = on_text_change_key {
                            handler(text, window, cx);
                        }
                        window.refresh();
                    }
                    "left" => {
                        state.move_backward();
                        drop(state);
                        window.refresh();
                    }
                    "right" => {
                        state.move_forward();
                        drop(state);
                        window.refresh();
                    }
                    "home" => {
                        state.move_to_start();
                        drop(state);
                        window.refresh();
                    }
                    "end" => {
                        state.move_to_end();
                        drop(state);
                        window.refresh();
                    }
                    _ => {
                        if let Some(char_text) = event.keystroke.key_char.as_ref() {
                            state.insert_text(char_text);
                            let text = state.text.clone();
                            drop(state);
                            if let Some(ref handler) = on_text_change_key {
                                handler(text, window, cx);
                            }
                            window.refresh();
                        }
                    }
                }
            });
        }

        // Left icon
        if let Some(icon) = &self.icon_left {
            input_wrapper =
                input_wrapper.child(div().text_color(placeholder_color).child(icon.clone()));
        }

        // Determine display text
        let display_text = if editing {
            edit_text
        } else if current_value.is_empty() {
            self.placeholder
                .as_ref()
                .map(|s| s.to_string())
                .unwrap_or_default()
        } else {
            current_value.to_string()
        };

        // Build the text element with partial selection support
        let mut text_el = div().id(field_id).flex_1().flex().items_center();

        // Apply text size
        text_el = match self.size {
            InputSize::Sm => text_el.text_xs(),
            InputSize::Md => text_el.text_sm(),
            InputSize::Lg => text_el,
        };

        // Cursor element builder
        let cursor_el = || {
            div()
                .w(px(1.5))
                .h(px(14.0)) // Approximate height matching text
                .bg(cursor_color)
        };

        // Render text with selection highlighting and cursor
        if editing {
            let chars: Vec<char> = display_text.chars().collect();
            let len = chars.len();

            // Normalize selection range (if any)
            let (sel_start, sel_end) = if let Some(anchor) = selection_anchor {
                (cursor_pos.min(anchor), cursor_pos.max(anchor))
            } else {
                (cursor_pos, cursor_pos)
            };

            // Split text into 3 parts: 0..min, min..max, max..len
            // Insert cursor at `cursor_pos`

            let part1_end = sel_start;
            let part2_end = sel_end;

            let part1: String = chars[0..part1_end].iter().collect();
            let part2: String = chars[part1_end..part2_end].iter().collect();
            let part3: String = chars[part2_end..len].iter().collect();

            // Part 1 (Pre-selection/Pre-cursor)
            if !part1.is_empty() {
                text_el = text_el.child(div().text_color(text_color).child(part1));
            }

            // If cursor is at start of selection (dragged backwards)
            if cursor_pos == sel_start {
                text_el = text_el.child(cursor_el());
            }

            // Part 2 (Selection)
            if !part2.is_empty() {
                text_el = text_el.child(div().bg(selection_bg).text_color(text_color).child(part2));
            }

            // If cursor is at end of selection (dragged forwards or no selection)
            // Note: if selection is empty, sel_start == sel_end == cursor_pos, so this handles "no selection" case too
            if cursor_pos == sel_end && cursor_pos != sel_start {
                text_el = text_el.child(cursor_el());
            } else if cursor_pos == sel_end && sel_start == sel_end {
                // No selection case (sel_start == sel_end)
                // We already added cursor at sel_start above?
                // Wait: if cursor_pos == sel_start == sel_end, we added it above.
                // So we don't need to add it here.
            }

            // Part 3 (Post-selection/Post-cursor)
            if !part3.is_empty() {
                text_el = text_el.child(div().text_color(text_color).child(part3));
            }
        } else if !editing && current_value.is_empty() {
            // Placeholder text
            text_el = text_el.text_color(placeholder_color).child(display_text);
        } else {
            // Normal text (not editing)
            text_el = text_el.text_color(text_color).child(display_text);
        }

        input_wrapper = input_wrapper.child(text_el);

        // Right icon
        if let Some(icon) = &self.icon_right {
            input_wrapper =
                input_wrapper.child(div().text_color(placeholder_color).child(icon.clone()));
        }

        container = container.child(input_wrapper);

        // Error message
        if let Some(error) = &self.error {
            container =
                container.child(div().text_xs().text_color(theme.error).child(error.clone()));
        }

        container
    }
}

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

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