fresh-editor 0.1.90

A lightweight, fast terminal-based text editor with LSP support and TypeScript plugins
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
//! Entry detail dialog for editing complex map entries
//!
//! Provides a modal dialog for editing complex map entries using the same
//! SettingItem/SettingControl infrastructure as the main settings UI.

use super::items::{build_item_from_value, control_to_value, SettingControl, SettingItem};
use super::schema::{SettingSchema, SettingType};
use crate::view::controls::{FocusState, TextInputState};
use serde_json::Value;

/// State for the entry detail dialog
#[derive(Debug, Clone)]
pub struct EntryDialogState {
    /// The entry key (e.g., "rust" for language)
    pub entry_key: String,
    /// The map path this entry belongs to (e.g., "/languages", "/lsp")
    pub map_path: String,
    /// Human-readable title for the dialog
    pub title: String,
    /// Whether this is a new entry (vs editing existing)
    pub is_new: bool,
    /// Items in the dialog (using same SettingItem structure as main settings)
    pub items: Vec<SettingItem>,
    /// Currently selected item index
    pub selected_item: usize,
    /// Sub-focus index within the selected item (for TextList/Map navigation)
    pub sub_focus: Option<usize>,
    /// Whether we're in text editing mode
    pub editing_text: bool,
    /// Currently focused button (0=Save, 1=Delete, 2=Cancel for existing; 0=Save, 1=Cancel for new)
    pub focused_button: usize,
    /// Whether focus is on buttons (true) or items (false)
    pub focus_on_buttons: bool,
    /// Whether deletion was requested
    pub delete_requested: bool,
    /// Scroll offset for the items area
    pub scroll_offset: usize,
    /// Last known viewport height (updated during render)
    pub viewport_height: usize,
    /// Hovered item index (for mouse hover feedback)
    pub hover_item: Option<usize>,
    /// Hovered button index (for mouse hover feedback)
    pub hover_button: Option<usize>,
    /// Original value when dialog was opened (for Cancel to restore)
    pub original_value: Value,
    /// Index of first editable item (items before this are read-only)
    /// Used for rendering separator and focus navigation
    pub first_editable_index: usize,
    /// Whether deletion is disabled (for auto-managed entries like plugins)
    pub no_delete: bool,
}

impl EntryDialogState {
    /// Create a dialog from a schema definition
    ///
    /// This is the primary, schema-driven constructor. It builds items
    /// dynamically from the SettingSchema's properties using the same
    /// build logic as the main settings UI.
    pub fn from_schema(
        key: String,
        value: &Value,
        schema: &SettingSchema,
        map_path: &str,
        is_new: bool,
        no_delete: bool,
    ) -> Self {
        let mut items = Vec::new();

        // Add key field as first item (read-only for existing entries)
        let key_item = SettingItem {
            path: "__key__".to_string(),
            name: "Key".to_string(),
            description: Some("unique identifier for this entry".to_string()),
            control: SettingControl::Text(TextInputState::new("Key").with_value(&key)),
            default: None,
            modified: false,
            layer_source: crate::config_io::ConfigLayer::System,
            read_only: !is_new, // Key is editable only for new entries
            is_auto_managed: false,
            section: None,
            is_section_start: false,
        };
        items.push(key_item);

        // Add schema-driven items from object properties
        if let SettingType::Object { properties } = &schema.setting_type {
            for prop in properties {
                let field_name = prop.path.trim_start_matches('/');
                let field_value = value.get(field_name);
                let item = build_item_from_value(prop, field_value);
                items.push(item);
            }
        }

        // Sort items: read-only first, then editable
        items.sort_by_key(|item| !item.read_only);

        // Find the first editable item index
        let first_editable_index = items
            .iter()
            .position(|item| !item.read_only)
            .unwrap_or(items.len());

        // If all items are read-only, start with focus on buttons
        let focus_on_buttons = first_editable_index >= items.len();
        let selected_item = if focus_on_buttons {
            0
        } else {
            first_editable_index
        };

        let title = if is_new {
            format!("Add {}", schema.name)
        } else {
            format!("Edit {}", schema.name)
        };

        Self {
            entry_key: key,
            map_path: map_path.to_string(),
            title,
            is_new,
            items,
            selected_item,
            sub_focus: None,
            editing_text: false,
            focused_button: 0,
            focus_on_buttons,
            delete_requested: false,
            scroll_offset: 0,
            viewport_height: 20, // Default, updated during render
            hover_item: None,
            hover_button: None,
            original_value: value.clone(),
            first_editable_index,
            no_delete,
        }
    }

    /// Create a dialog for an array item (no key field)
    ///
    /// Used for ObjectArray controls where items are identified by index, not key.
    pub fn for_array_item(
        index: Option<usize>,
        value: &Value,
        schema: &SettingSchema,
        array_path: &str,
        is_new: bool,
    ) -> Self {
        let mut items = Vec::new();

        // Add schema-driven items from object properties (no key field for arrays)
        if let SettingType::Object { properties } = &schema.setting_type {
            for prop in properties {
                let field_name = prop.path.trim_start_matches('/');
                let field_value = value.get(field_name);
                let item = build_item_from_value(prop, field_value);
                items.push(item);
            }
        }

        // Sort items: read-only first, then editable
        items.sort_by_key(|item| !item.read_only);

        // Find the first editable item index
        let first_editable_index = items
            .iter()
            .position(|item| !item.read_only)
            .unwrap_or(items.len());

        // If all items are read-only, start with focus on buttons
        let focus_on_buttons = first_editable_index >= items.len();
        let selected_item = if focus_on_buttons {
            0
        } else {
            first_editable_index
        };

        let title = if is_new {
            format!("Add {}", schema.name)
        } else {
            format!("Edit {}", schema.name)
        };

        Self {
            entry_key: index.map_or(String::new(), |i| i.to_string()),
            map_path: array_path.to_string(),
            title,
            is_new,
            items,
            selected_item,
            sub_focus: None,
            editing_text: false,
            focused_button: 0,
            focus_on_buttons,
            delete_requested: false,
            scroll_offset: 0,
            viewport_height: 20,
            hover_item: None,
            hover_button: None,
            original_value: value.clone(),
            first_editable_index,
            no_delete: false, // Arrays typically allow deletion
        }
    }

    /// Get the current key value from the key item
    pub fn get_key(&self) -> String {
        // Find the key item by path (may not be first after sorting)
        for item in &self.items {
            if item.path == "__key__" {
                if let SettingControl::Text(state) = &item.control {
                    return state.value.clone();
                }
            }
        }
        self.entry_key.clone()
    }

    /// Get button count (3 for existing entries with Delete, 2 for new/no_delete entries)
    pub fn button_count(&self) -> usize {
        if self.is_new || self.no_delete {
            2 // Save, Cancel (no Delete for new entries or when no_delete is set)
        } else {
            3
        }
    }

    /// Convert dialog state back to JSON value (excludes the __key__ item)
    pub fn to_value(&self) -> Value {
        let mut obj = serde_json::Map::new();

        for item in &self.items {
            // Skip the special key item - it's stored separately
            if item.path == "__key__" {
                continue;
            }

            let field_name = item.path.trim_start_matches('/');
            let value = control_to_value(&item.control);
            obj.insert(field_name.to_string(), value);
        }

        Value::Object(obj)
    }

    /// Get currently selected item
    pub fn current_item(&self) -> Option<&SettingItem> {
        if self.focus_on_buttons {
            None
        } else {
            self.items.get(self.selected_item)
        }
    }

    /// Get currently selected item mutably
    pub fn current_item_mut(&mut self) -> Option<&mut SettingItem> {
        if self.focus_on_buttons {
            None
        } else {
            self.items.get_mut(self.selected_item)
        }
    }

    /// Move focus to next item or button
    pub fn focus_next(&mut self) {
        if self.editing_text {
            return; // Don't change focus while editing
        }

        if self.focus_on_buttons {
            if self.focused_button + 1 < self.button_count() {
                // Move to next button
                self.focused_button += 1;
            } else {
                // Wrap to first editable item (skip read-only items)
                if self.first_editable_index < self.items.len() {
                    self.focus_on_buttons = false;
                    self.selected_item = self.first_editable_index;
                }
                // If all items are read-only, stay on buttons (don't wrap)
            }
        } else {
            // Check if current item is an ObjectArray that can navigate internally
            let array_nav_result = self.items.get(self.selected_item).and_then(|item| {
                if let SettingControl::ObjectArray(state) = &item.control {
                    // Navigation order: entries -> add-new -> exit
                    match state.focused_index {
                        Some(idx) if idx + 1 < state.bindings.len() => {
                            // On entry, can go to next entry
                            Some(true)
                        }
                        Some(_) => {
                            // On last entry, can go to add-new
                            Some(true)
                        }
                        None => {
                            // On add-new, exit to next dialog item
                            Some(false)
                        }
                    }
                } else {
                    None
                }
            });

            match array_nav_result {
                Some(true) => {
                    // Navigate within the ObjectArray
                    if let Some(item) = self.items.get_mut(self.selected_item) {
                        if let SettingControl::ObjectArray(state) = &mut item.control {
                            state.focus_next();
                        }
                    }
                }
                Some(false) => {
                    // Exit ObjectArray, go to next item
                    if self.selected_item + 1 < self.items.len() {
                        self.selected_item += 1;
                        self.sub_focus = None;
                        // Initialize next item's ObjectArray if it has entries
                        self.init_object_array_focus();
                    } else {
                        self.focus_on_buttons = true;
                        self.focused_button = 0;
                    }
                }
                None => {
                    // Not an ObjectArray, normal navigation
                    // All items after first_editable_index are editable (sorted)
                    if self.selected_item + 1 < self.items.len() {
                        self.selected_item += 1;
                        self.sub_focus = None;
                        // Initialize next item's ObjectArray if it has entries
                        self.init_object_array_focus();
                    } else {
                        self.focus_on_buttons = true;
                        self.focused_button = 0;
                    }
                }
            }
        }

        self.update_focus_states();
        self.ensure_selected_visible(self.viewport_height);
    }

    /// Move focus to previous item or button
    pub fn focus_prev(&mut self) {
        if self.editing_text {
            return; // Don't change focus while editing
        }

        if self.focus_on_buttons {
            if self.focused_button > 0 {
                self.focused_button -= 1;
            } else {
                // Move back to last editable item
                if self.first_editable_index < self.items.len() {
                    self.focus_on_buttons = false;
                    self.selected_item = self.items.len().saturating_sub(1);
                }
                // If all items are read-only, stay on buttons (don't wrap)
            }
        } else {
            // Check if current item is an ObjectArray that can navigate internally
            let array_nav_result = self.items.get(self.selected_item).and_then(|item| {
                if let SettingControl::ObjectArray(state) = &item.control {
                    // Navigation order (reverse): exit <- entries <- add-new
                    match state.focused_index {
                        None => {
                            // On add-new, can go back to last entry (if any)
                            if !state.bindings.is_empty() {
                                Some(true)
                            } else {
                                Some(false) // No entries, exit
                            }
                        }
                        Some(0) => {
                            // On first entry, exit to previous dialog item
                            Some(false)
                        }
                        Some(_) => {
                            // On entry, can go to previous entry
                            Some(true)
                        }
                    }
                } else {
                    None
                }
            });

            match array_nav_result {
                Some(true) => {
                    // Navigate within the ObjectArray
                    if let Some(item) = self.items.get_mut(self.selected_item) {
                        if let SettingControl::ObjectArray(state) = &mut item.control {
                            state.focus_prev();
                        }
                    }
                }
                Some(false) => {
                    // Exit ObjectArray, go to previous editable item (not into read-only)
                    if self.selected_item > self.first_editable_index {
                        self.selected_item -= 1;
                        self.sub_focus = None;
                        // Initialize previous item's ObjectArray to add-new (end)
                        self.init_object_array_focus_end();
                    } else {
                        // At first editable item, go to buttons
                        self.focus_on_buttons = true;
                        self.focused_button = self.button_count().saturating_sub(1);
                    }
                }
                None => {
                    // Not an ObjectArray, normal navigation
                    // Don't go below first_editable_index (read-only items)
                    if self.selected_item > self.first_editable_index {
                        self.selected_item -= 1;
                        self.sub_focus = None;
                        // Initialize previous item's ObjectArray to add-new (end)
                        self.init_object_array_focus_end();
                    } else {
                        // At first editable item, go to buttons
                        self.focus_on_buttons = true;
                        self.focused_button = self.button_count().saturating_sub(1);
                    }
                }
            }
        }

        self.update_focus_states();
        self.ensure_selected_visible(self.viewport_height);
    }

    /// Initialize ObjectArray focus to first entry (when arriving from above)
    fn init_object_array_focus(&mut self) {
        if let Some(item) = self.items.get_mut(self.selected_item) {
            if let SettingControl::ObjectArray(state) = &mut item.control {
                // Start at first entry if there are any, otherwise stay on add-new
                if !state.bindings.is_empty() {
                    state.focused_index = Some(0);
                }
            }
        }
    }

    /// Initialize ObjectArray focus to add-new (when arriving from below)
    fn init_object_array_focus_end(&mut self) {
        if let Some(item) = self.items.get_mut(self.selected_item) {
            if let SettingControl::ObjectArray(state) = &mut item.control {
                // Start at add-new row (None)
                state.focused_index = None;
            }
        }
    }

    /// Move to next sub-item within current control (for TextList, Map)
    pub fn sub_focus_next(&mut self) {
        if let Some(item) = self.items.get(self.selected_item) {
            let max_sub = match &item.control {
                SettingControl::TextList(state) => state.items.len(), // +1 for add-new
                SettingControl::Map(state) => state.entries.len(),    // +1 for add-new
                _ => 0,
            };

            if max_sub > 0 {
                let current = self.sub_focus.unwrap_or(0);
                if current < max_sub {
                    self.sub_focus = Some(current + 1);
                } else {
                    // Move to next item
                    self.sub_focus = None;
                    self.focus_next();
                }
            } else {
                self.focus_next();
            }
        } else {
            self.focus_next();
        }
    }

    /// Move to previous sub-item within current control
    pub fn sub_focus_prev(&mut self) {
        if let Some(sub) = self.sub_focus {
            if sub > 0 {
                self.sub_focus = Some(sub - 1);
            } else {
                self.sub_focus = None;
            }
        } else {
            self.focus_prev();
        }
    }

    /// Update focus states for all items
    pub fn update_focus_states(&mut self) {
        for (idx, item) in self.items.iter_mut().enumerate() {
            let state = if !self.focus_on_buttons && idx == self.selected_item {
                FocusState::Focused
            } else {
                FocusState::Normal
            };

            match &mut item.control {
                SettingControl::Toggle(s) => s.focus = state,
                SettingControl::Number(s) => s.focus = state,
                SettingControl::Dropdown(s) => s.focus = state,
                SettingControl::Text(s) => s.focus = state,
                SettingControl::TextList(s) => s.focus = state,
                SettingControl::Map(s) => s.focus = state,
                SettingControl::ObjectArray(s) => s.focus = state,
                SettingControl::Json(s) => s.focus = state,
                SettingControl::Complex { .. } => {}
            }
        }
    }

    /// Calculate total content height for all items (including separator)
    pub fn total_content_height(&self) -> usize {
        let items_height: usize = self
            .items
            .iter()
            .map(|item| item.control.control_height() as usize)
            .sum();
        // Add 1 for separator if we have both read-only and editable items
        let separator_height =
            if self.first_editable_index > 0 && self.first_editable_index < self.items.len() {
                1
            } else {
                0
            };
        items_height + separator_height
    }

    /// Calculate the Y offset of the selected item (including separator)
    pub fn selected_item_offset(&self) -> usize {
        let items_offset: usize = self
            .items
            .iter()
            .take(self.selected_item)
            .map(|item| item.control.control_height() as usize)
            .sum();
        // Add 1 for separator if selected item is after it
        let separator_offset = if self.first_editable_index > 0
            && self.first_editable_index < self.items.len()
            && self.selected_item >= self.first_editable_index
        {
            1
        } else {
            0
        };
        items_offset + separator_offset
    }

    /// Calculate the height of the selected item
    pub fn selected_item_height(&self) -> usize {
        self.items
            .get(self.selected_item)
            .map(|item| item.control.control_height() as usize)
            .unwrap_or(1)
    }

    /// Ensure the selected item is visible within the viewport
    pub fn ensure_selected_visible(&mut self, viewport_height: usize) {
        if self.focus_on_buttons {
            // Scroll to bottom when buttons are focused
            let total = self.total_content_height();
            if total > viewport_height {
                self.scroll_offset = total.saturating_sub(viewport_height);
            }
            return;
        }

        let item_start = self.selected_item_offset();
        let item_end = item_start + self.selected_item_height();

        // If item starts before viewport, scroll up
        if item_start < self.scroll_offset {
            self.scroll_offset = item_start;
        }
        // If item ends after viewport, scroll down
        else if item_end > self.scroll_offset + viewport_height {
            self.scroll_offset = item_end.saturating_sub(viewport_height);
        }
    }

    /// Ensure the cursor within a JSON editor is visible
    ///
    /// When editing a multiline JSON control, this adjusts scroll_offset
    /// to keep the cursor row visible within the viewport.
    pub fn ensure_cursor_visible(&mut self) {
        if !self.editing_text || self.focus_on_buttons {
            return;
        }

        // Get cursor row from current item (if it's a JSON editor)
        let cursor_row = if let Some(item) = self.items.get(self.selected_item) {
            if let SettingControl::Json(state) = &item.control {
                state.cursor_pos().0
            } else {
                return; // Not a JSON editor
            }
        } else {
            return;
        };

        // Calculate absolute position of cursor row in content:
        // item_offset + 1 (for label row) + cursor_row
        let item_offset = self.selected_item_offset();
        let cursor_content_row = item_offset + 1 + cursor_row;

        let viewport_height = self.viewport_height;

        // If cursor is above viewport, scroll up
        if cursor_content_row < self.scroll_offset {
            self.scroll_offset = cursor_content_row;
        }
        // If cursor is below viewport, scroll down
        else if cursor_content_row >= self.scroll_offset + viewport_height {
            self.scroll_offset = cursor_content_row.saturating_sub(viewport_height) + 1;
        }
    }

    /// Scroll up by one line
    pub fn scroll_up(&mut self) {
        self.scroll_offset = self.scroll_offset.saturating_sub(1);
    }

    /// Scroll down by one line
    pub fn scroll_down(&mut self, viewport_height: usize) {
        let max_scroll = self.total_content_height().saturating_sub(viewport_height);
        if self.scroll_offset < max_scroll {
            self.scroll_offset += 1;
        }
    }

    /// Scroll to a position based on ratio (0.0 = top, 1.0 = bottom)
    ///
    /// Used for scrollbar drag operations.
    pub fn scroll_to_ratio(&mut self, ratio: f32) {
        let max_scroll = self
            .total_content_height()
            .saturating_sub(self.viewport_height);
        let new_offset = (ratio * max_scroll as f32).round() as usize;
        self.scroll_offset = new_offset.min(max_scroll);
    }

    /// Start text editing mode for the current control
    pub fn start_editing(&mut self) {
        if let Some(item) = self.current_item_mut() {
            // Don't allow editing read-only fields
            if item.read_only {
                return;
            }
            match &mut item.control {
                SettingControl::Text(state) => {
                    // TextInputState uses focus state, cursor is already at end from with_value
                    state.cursor = state.value.len();
                    self.editing_text = true;
                }
                SettingControl::TextList(state) => {
                    // Focus on the new item input by default
                    state.focus_new_item();
                    self.editing_text = true;
                }
                SettingControl::Number(state) => {
                    state.start_editing();
                    self.editing_text = true;
                }
                SettingControl::Json(_) => {
                    // JSON editor is always ready to edit, just set the flag
                    self.editing_text = true;
                }
                _ => {}
            }
        }
    }

    /// Stop text editing mode
    pub fn stop_editing(&mut self) {
        if let Some(item) = self.current_item_mut() {
            if let SettingControl::Number(state) = &mut item.control {
                state.cancel_editing();
            }
        }
        self.editing_text = false;
    }

    /// Handle character input
    pub fn insert_char(&mut self, c: char) {
        if !self.editing_text {
            return;
        }
        if let Some(item) = self.current_item_mut() {
            match &mut item.control {
                SettingControl::Text(state) => {
                    state.insert(c);
                }
                SettingControl::TextList(state) => {
                    state.insert(c);
                }
                SettingControl::Number(state) => {
                    state.insert_char(c);
                }
                SettingControl::Json(state) => {
                    state.insert(c);
                }
                _ => {}
            }
        }
    }

    pub fn insert_str(&mut self, s: &str) {
        if !self.editing_text {
            return;
        }
        if let Some(item) = self.current_item_mut() {
            match &mut item.control {
                SettingControl::Text(state) => {
                    state.insert_str(s);
                }
                SettingControl::TextList(state) => {
                    state.insert_str(s);
                }
                SettingControl::Number(state) => {
                    for c in s.chars() {
                        state.insert_char(c);
                    }
                }
                SettingControl::Json(state) => {
                    state.insert_str(s);
                }
                _ => {}
            }
        }
    }

    /// Handle backspace
    pub fn backspace(&mut self) {
        if !self.editing_text {
            return;
        }
        if let Some(item) = self.current_item_mut() {
            match &mut item.control {
                SettingControl::Text(state) => {
                    state.backspace();
                }
                SettingControl::TextList(state) => {
                    state.backspace();
                }
                SettingControl::Number(state) => {
                    state.backspace();
                }
                SettingControl::Json(state) => {
                    state.backspace();
                }
                _ => {}
            }
        }
    }

    /// Handle cursor left
    pub fn cursor_left(&mut self) {
        if !self.editing_text {
            return;
        }
        if let Some(item) = self.current_item_mut() {
            match &mut item.control {
                SettingControl::Text(state) => {
                    state.move_left();
                }
                SettingControl::TextList(state) => {
                    state.move_left();
                }
                SettingControl::Json(state) => {
                    state.move_left();
                }
                _ => {}
            }
        }
    }

    /// Handle cursor left with selection (Shift+Left)
    pub fn cursor_left_selecting(&mut self) {
        if !self.editing_text {
            return;
        }
        if let Some(item) = self.current_item_mut() {
            if let SettingControl::Json(state) = &mut item.control {
                state.editor.move_left_selecting();
            }
        }
    }

    /// Handle cursor right
    pub fn cursor_right(&mut self) {
        if !self.editing_text {
            return;
        }
        if let Some(item) = self.current_item_mut() {
            match &mut item.control {
                SettingControl::Text(state) => {
                    state.move_right();
                }
                SettingControl::TextList(state) => {
                    state.move_right();
                }
                SettingControl::Json(state) => {
                    state.move_right();
                }
                _ => {}
            }
        }
    }

    /// Handle cursor right with selection (Shift+Right)
    pub fn cursor_right_selecting(&mut self) {
        if !self.editing_text {
            return;
        }
        if let Some(item) = self.current_item_mut() {
            if let SettingControl::Json(state) = &mut item.control {
                state.editor.move_right_selecting();
            }
        }
    }

    /// Handle cursor up (for multiline controls)
    pub fn cursor_up(&mut self) {
        if !self.editing_text {
            return;
        }
        if let Some(item) = self.current_item_mut() {
            if let SettingControl::Json(state) = &mut item.control {
                state.move_up();
            }
        }
        self.ensure_cursor_visible();
    }

    /// Handle cursor up with selection (Shift+Up)
    pub fn cursor_up_selecting(&mut self) {
        if !self.editing_text {
            return;
        }
        if let Some(item) = self.current_item_mut() {
            if let SettingControl::Json(state) = &mut item.control {
                state.editor.move_up_selecting();
            }
        }
        self.ensure_cursor_visible();
    }

    /// Handle cursor down (for multiline controls)
    pub fn cursor_down(&mut self) {
        if !self.editing_text {
            return;
        }
        if let Some(item) = self.current_item_mut() {
            if let SettingControl::Json(state) = &mut item.control {
                state.move_down();
            }
        }
        self.ensure_cursor_visible();
    }

    /// Handle cursor down with selection (Shift+Down)
    pub fn cursor_down_selecting(&mut self) {
        if !self.editing_text {
            return;
        }
        if let Some(item) = self.current_item_mut() {
            if let SettingControl::Json(state) = &mut item.control {
                state.editor.move_down_selecting();
            }
        }
        self.ensure_cursor_visible();
    }

    /// Insert newline in JSON editor
    pub fn insert_newline(&mut self) {
        if !self.editing_text {
            return;
        }
        if let Some(item) = self.current_item_mut() {
            if let SettingControl::Json(state) = &mut item.control {
                state.insert('\n');
            }
        }
    }

    /// Revert JSON changes to original and stop editing
    pub fn revert_json_and_stop(&mut self) {
        if let Some(item) = self.current_item_mut() {
            if let SettingControl::Json(state) = &mut item.control {
                state.revert();
            }
        }
        self.editing_text = false;
    }

    /// Check if current control is a JSON editor
    pub fn is_editing_json(&self) -> bool {
        if !self.editing_text {
            return false;
        }
        self.current_item()
            .map(|item| matches!(&item.control, SettingControl::Json(_)))
            .unwrap_or(false)
    }

    /// Toggle boolean value
    pub fn toggle_bool(&mut self) {
        if let Some(item) = self.current_item_mut() {
            // Don't allow toggling read-only fields
            if item.read_only {
                return;
            }
            if let SettingControl::Toggle(state) = &mut item.control {
                state.checked = !state.checked;
            }
        }
    }

    /// Toggle dropdown open state
    pub fn toggle_dropdown(&mut self) {
        if let Some(item) = self.current_item_mut() {
            // Don't allow editing read-only fields
            if item.read_only {
                return;
            }
            if let SettingControl::Dropdown(state) = &mut item.control {
                state.open = !state.open;
            }
        }
    }

    /// Move dropdown selection up
    pub fn dropdown_prev(&mut self) {
        if let Some(item) = self.current_item_mut() {
            if let SettingControl::Dropdown(state) = &mut item.control {
                if state.open {
                    state.select_prev();
                }
            }
        }
    }

    /// Move dropdown selection down
    pub fn dropdown_next(&mut self) {
        if let Some(item) = self.current_item_mut() {
            if let SettingControl::Dropdown(state) = &mut item.control {
                if state.open {
                    state.select_next();
                }
            }
        }
    }

    /// Confirm dropdown selection
    pub fn dropdown_confirm(&mut self) {
        if let Some(item) = self.current_item_mut() {
            if let SettingControl::Dropdown(state) = &mut item.control {
                state.open = false;
            }
        }
    }

    /// Increment number value
    pub fn increment_number(&mut self) {
        if let Some(item) = self.current_item_mut() {
            // Don't allow editing read-only fields
            if item.read_only {
                return;
            }
            if let SettingControl::Number(state) = &mut item.control {
                state.increment();
            }
        }
    }

    /// Decrement number value
    pub fn decrement_number(&mut self) {
        if let Some(item) = self.current_item_mut() {
            // Don't allow editing read-only fields
            if item.read_only {
                return;
            }
            if let SettingControl::Number(state) = &mut item.control {
                state.decrement();
            }
        }
    }

    /// Delete the currently focused item from a TextList control
    pub fn delete_list_item(&mut self) {
        if let Some(item) = self.current_item_mut() {
            if let SettingControl::TextList(state) = &mut item.control {
                // Remove the currently focused item if any
                if let Some(idx) = state.focused_item {
                    state.remove_item(idx);
                }
            }
        }
    }

    /// Delete character at cursor (forward delete)
    pub fn delete(&mut self) {
        if !self.editing_text {
            return;
        }
        if let Some(item) = self.current_item_mut() {
            match &mut item.control {
                SettingControl::Text(state) => {
                    state.delete();
                }
                SettingControl::TextList(state) => {
                    state.delete();
                }
                SettingControl::Json(state) => {
                    state.delete();
                }
                _ => {}
            }
        }
    }

    /// Move cursor to beginning of line
    pub fn cursor_home(&mut self) {
        if !self.editing_text {
            return;
        }
        if let Some(item) = self.current_item_mut() {
            match &mut item.control {
                SettingControl::Text(state) => {
                    state.move_home();
                }
                SettingControl::TextList(state) => {
                    state.move_home();
                }
                SettingControl::Json(state) => {
                    state.move_home();
                }
                _ => {}
            }
        }
    }

    /// Move cursor to end of line
    pub fn cursor_end(&mut self) {
        if !self.editing_text {
            return;
        }
        if let Some(item) = self.current_item_mut() {
            match &mut item.control {
                SettingControl::Text(state) => {
                    state.move_end();
                }
                SettingControl::TextList(state) => {
                    state.move_end();
                }
                SettingControl::Json(state) => {
                    state.move_end();
                }
                _ => {}
            }
        }
    }

    /// Select all text in current control
    pub fn select_all(&mut self) {
        if !self.editing_text {
            return;
        }
        if let Some(item) = self.current_item_mut() {
            if let SettingControl::Json(state) = &mut item.control {
                state.select_all();
            }
            // Note: Text and TextList don't have select_all implemented
        }
    }

    /// Check if any field is currently in edit mode
    pub fn is_editing(&self) -> bool {
        self.editing_text
            || self
                .current_item()
                .map(|item| {
                    matches!(
                        &item.control,
                        SettingControl::Dropdown(s) if s.open
                    )
                })
                .unwrap_or(false)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn create_test_schema() -> SettingSchema {
        SettingSchema {
            path: "/test".to_string(),
            name: "Test".to_string(),
            description: Some("Test schema".to_string()),
            setting_type: SettingType::Object {
                properties: vec![
                    SettingSchema {
                        path: "/enabled".to_string(),
                        name: "Enabled".to_string(),
                        description: Some("Enable this".to_string()),
                        setting_type: SettingType::Boolean,
                        default: Some(serde_json::json!(true)),
                        read_only: false,
                        section: None,
                    },
                    SettingSchema {
                        path: "/command".to_string(),
                        name: "Command".to_string(),
                        description: Some("Command to run".to_string()),
                        setting_type: SettingType::String,
                        default: Some(serde_json::json!("")),
                        read_only: false,
                        section: None,
                    },
                ],
            },
            default: None,
            read_only: false,
            section: None,
        }
    }

    #[test]
    fn from_schema_creates_key_item_first() {
        let schema = create_test_schema();
        let dialog = EntryDialogState::from_schema(
            "test".to_string(),
            &serde_json::json!({}),
            &schema,
            "/test",
            false,
            false,
        );

        assert!(!dialog.items.is_empty());
        assert_eq!(dialog.items[0].path, "__key__");
        assert_eq!(dialog.items[0].name, "Key");
    }

    #[test]
    fn from_schema_creates_items_from_properties() {
        let schema = create_test_schema();
        let dialog = EntryDialogState::from_schema(
            "test".to_string(),
            &serde_json::json!({"enabled": true, "command": "test-cmd"}),
            &schema,
            "/test",
            false,
            false,
        );

        // Key + 2 properties = 3 items
        assert_eq!(dialog.items.len(), 3);
        assert_eq!(dialog.items[1].name, "Enabled");
        assert_eq!(dialog.items[2].name, "Command");
    }

    #[test]
    fn get_key_returns_key_value() {
        let schema = create_test_schema();
        let dialog = EntryDialogState::from_schema(
            "mykey".to_string(),
            &serde_json::json!({}),
            &schema,
            "/test",
            false,
            false,
        );

        assert_eq!(dialog.get_key(), "mykey");
    }

    #[test]
    fn to_value_excludes_key() {
        let schema = create_test_schema();
        let dialog = EntryDialogState::from_schema(
            "test".to_string(),
            &serde_json::json!({"enabled": true, "command": "cmd"}),
            &schema,
            "/test",
            false,
            false,
        );

        let value = dialog.to_value();
        assert!(value.get("__key__").is_none());
        assert!(value.get("enabled").is_some());
    }

    #[test]
    fn focus_navigation_works() {
        let schema = create_test_schema();
        let mut dialog = EntryDialogState::from_schema(
            "test".to_string(),
            &serde_json::json!({}),
            &schema,
            "/test",
            false, // existing entry - Key is read-only
            false, // allow delete
        );

        // With is_new=false, Key is read-only and sorted first
        // Items: [Key (read-only), Enabled, Command]
        // Focus starts at first editable item (index 1)
        assert_eq!(dialog.first_editable_index, 1);
        assert_eq!(dialog.selected_item, 1); // First editable (Enabled)
        assert!(!dialog.focus_on_buttons);

        dialog.focus_next();
        assert_eq!(dialog.selected_item, 2); // Command

        dialog.focus_next();
        assert!(dialog.focus_on_buttons); // No more editable items
        assert_eq!(dialog.focused_button, 0);

        // Going back should skip read-only Key
        dialog.focus_prev();
        assert!(!dialog.focus_on_buttons);
        assert_eq!(dialog.selected_item, 2); // Last editable (Command)

        dialog.focus_prev();
        assert_eq!(dialog.selected_item, 1); // First editable (Enabled)

        dialog.focus_prev();
        assert!(dialog.focus_on_buttons); // Wraps to buttons, not to read-only Key
    }

    #[test]
    fn button_count_differs_for_new_vs_existing() {
        let schema = create_test_schema();

        let new_dialog = EntryDialogState::from_schema(
            "test".to_string(),
            &serde_json::json!({}),
            &schema,
            "/test",
            true,
            false,
        );
        assert_eq!(new_dialog.button_count(), 2); // Save, Cancel

        let existing_dialog = EntryDialogState::from_schema(
            "test".to_string(),
            &serde_json::json!({}),
            &schema,
            "/test",
            false,
            false, // allow delete
        );
        assert_eq!(existing_dialog.button_count(), 3); // Save, Delete, Cancel

        // no_delete hides the Delete button even for existing entries
        let no_delete_dialog = EntryDialogState::from_schema(
            "test".to_string(),
            &serde_json::json!({}),
            &schema,
            "/test",
            false,
            true, // no delete (auto-managed entries like plugins)
        );
        assert_eq!(no_delete_dialog.button_count(), 2); // Save, Cancel (no Delete)
    }
}