dotstate 0.3.4

A modern, secure, and user-friendly dotfile manager built with Rust
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
//! Settings screen for configuring application options.
//!
//! Provides a two-pane interface:
//! - Left: List of settings
//! - Right: Current value, options, and explanation

use crate::components::footer::Footer;
use crate::components::header::Header;
use crate::config::{Config, RepoMode};
use crate::icons::Icons;
use crate::keymap::{Action, KeymapPreset};
use crate::screens::screen_trait::{RenderContext, Screen, ScreenAction, ScreenContext};
use crate::styles::{init_theme, theme, ThemeType};
use crate::ui::Screen as ScreenId;
use crate::utils::{
    create_split_layout, create_standard_layout, focused_border_style, unfocused_border_style,
    MouseRegions,
};
use anyhow::Result;
use crossterm::event::{Event, KeyEventKind, MouseButton, MouseEventKind};
use ratatui::layout::{Alignment, Constraint, Direction, Layout, Rect};
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span, Text};
use ratatui::widgets::{
    Block, Borders, List, ListItem, ListState, Padding, Paragraph, StatefulWidget, Wrap,
};
use ratatui::Frame;

/// Available settings
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SettingItem {
    Theme,
    IconSet,
    KeymapPreset,
    Backups,
    CheckForUpdates,
    EmbedCredentials,
}

impl SettingItem {
    #[must_use]
    pub fn all(repo_mode: RepoMode) -> Vec<SettingItem> {
        let mut items = vec![
            SettingItem::Theme,
            SettingItem::IconSet,
            SettingItem::KeymapPreset,
            SettingItem::Backups,
            SettingItem::CheckForUpdates,
        ];
        if repo_mode == RepoMode::GitHub {
            items.push(SettingItem::EmbedCredentials);
        }
        items
    }

    #[must_use]
    pub fn name(&self) -> &'static str {
        match self {
            SettingItem::Theme => "Theme",
            SettingItem::IconSet => "Icon Set",
            SettingItem::KeymapPreset => "Keymap Preset",
            SettingItem::Backups => "Backups",
            SettingItem::CheckForUpdates => "Check for Updates",
            SettingItem::EmbedCredentials => "Token in Remote URL",
        }
    }

    #[must_use]
    pub fn from_index(index: usize, repo_mode: RepoMode) -> Option<SettingItem> {
        Self::all(repo_mode).get(index).copied()
    }
}

/// Focus within the settings screen
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SettingsFocus {
    #[default]
    List,
    Options,
}

/// Settings screen state
#[derive(Debug)]
pub struct SettingsState {
    pub list_state: ListState,
    pub focus: SettingsFocus,
    pub option_index: usize, // Selected option within the current setting
}

impl Default for SettingsState {
    fn default() -> Self {
        let mut list_state = ListState::default();
        list_state.select(Some(0));
        Self {
            list_state,
            focus: SettingsFocus::List,
            option_index: 0,
        }
    }
}

/// Settings screen controller
pub struct SettingsScreen {
    state: SettingsState,
    /// Clickable regions for settings list items
    settings_regions: MouseRegions<usize>,
    /// Clickable regions for option items
    option_regions: MouseRegions<usize>,
    /// Area of the settings list pane (for scroll hit-testing)
    list_pane_area: Option<Rect>,
    /// Area of the options pane (for scroll hit-testing)
    options_pane_area: Option<Rect>,
}

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

impl SettingsScreen {
    #[must_use]
    pub fn new() -> Self {
        Self {
            state: SettingsState::default(),
            settings_regions: MouseRegions::new(),
            option_regions: MouseRegions::new(),
            list_pane_area: None,
            options_pane_area: None,
        }
    }

    fn selected_setting(&self, repo_mode: RepoMode) -> Option<SettingItem> {
        self.state
            .list_state
            .selected()
            .and_then(|i| SettingItem::from_index(i, repo_mode))
    }

    /// Get available options for the current setting
    fn get_options(&self, config: &Config) -> Vec<(String, bool)> {
        match self.selected_setting(config.repo_mode) {
            Some(SettingItem::Theme) => {
                let current = &config.theme;
                ThemeType::all()
                    .iter()
                    .map(|t| (t.name().to_string(), current == t.to_config_string()))
                    .collect()
            }
            Some(SettingItem::IconSet) => {
                use crate::icons::IconSet;
                let current = &config.icon_set;
                vec![
                    ("auto".to_string(), current == "auto"),
                    (IconSet::NerdFonts.name().to_string(), current == "nerd"),
                    (IconSet::Unicode.name().to_string(), current == "unicode"),
                    (IconSet::Emoji.name().to_string(), current == "emoji"),
                    (IconSet::Ascii.name().to_string(), current == "ascii"),
                ]
            }
            Some(SettingItem::KeymapPreset) => {
                let current = config.keymap.preset;
                vec![
                    ("Standard".to_string(), current == KeymapPreset::Standard),
                    ("Vim".to_string(), current == KeymapPreset::Vim),
                    ("Emacs".to_string(), current == KeymapPreset::Emacs),
                ]
            }
            Some(SettingItem::Backups) => {
                vec![
                    ("Enabled".to_string(), config.backup_enabled),
                    ("Disabled".to_string(), !config.backup_enabled),
                ]
            }
            Some(SettingItem::CheckForUpdates) => {
                vec![
                    ("Enabled".to_string(), config.updates.check_enabled),
                    ("Disabled".to_string(), !config.updates.check_enabled),
                ]
            }
            Some(SettingItem::EmbedCredentials) => {
                vec![
                    ("Enabled".to_string(), config.embed_credentials_in_url),
                    ("Disabled".to_string(), !config.embed_credentials_in_url),
                ]
            }
            None => vec![],
        }
    }

    /// Get explanation text for the current setting
    fn get_explanation(&self, config: &Config) -> Text<'static> {
        let t = theme();
        let icons = Icons::from_config(config);

        match self.selected_setting(config.repo_mode) {
            Some(SettingItem::Theme) => {
                let lines = vec![
                    Line::from(Span::styled("Color Theme", t.title_style())),
                    Line::from(""),
                    Line::from(Span::styled(
                        "Choose how DotState looks. The theme affects all colors in the UI.",
                        t.text_style(),
                    )),
                    Line::from(""),
                    Line::from(vec![
                        Span::styled(icons.lightbulb(), Style::default().fg(t.secondary)),
                        Span::styled(" Current: ", t.muted_style()),
                        Span::styled(config.theme.clone(), t.emphasis_style()),
                    ]),
                ];
                Text::from(lines)
            }
            Some(SettingItem::IconSet) => {
                let icons_preview = Icons::from_config(config);
                let lines = vec![
                    Line::from(Span::styled("Icon Set", t.title_style())),
                    Line::from(""),
                    Line::from(Span::styled(
                        "Choose which icon set to use in the interface.",
                        t.text_style(),
                    )),
                    Line::from(""),
                    Line::from(Span::styled("Preview:", t.muted_style())),
                    Line::from(vec![
                        Span::styled(
                            format!("  {} Folder  ", icons_preview.folder()),
                            t.text_style(),
                        ),
                        Span::styled(format!("{} File  ", icons_preview.file()), t.text_style()),
                        Span::styled(format!("{} Sync", icons_preview.sync()), t.text_style()),
                    ]),
                    Line::from(""),
                    Line::from(vec![
                        Span::styled(icons.lightbulb(), Style::default().fg(t.secondary)),
                        Span::styled(" Tip: ", Style::default().fg(t.secondary)),
                        Span::styled(
                            "Use 'nerd' if you have a NerdFont installed",
                            t.text_style(),
                        ),
                    ]),
                ];
                Text::from(lines)
            }
            Some(SettingItem::KeymapPreset) => {
                let lines = vec![
                    Line::from(Span::styled("Keymap Preset", t.title_style())),
                    Line::from(""),
                    Line::from(Span::styled(
                        "Choose keyboard bindings that feel natural to you.",
                        t.text_style(),
                    )),
                    Line::from(""),
                    Line::from(vec![
                        Span::styled("  • ", t.muted_style()),
                        Span::styled("Standard", t.emphasis_style()),
                        Span::styled(": Arrow keys, Enter, Escape", t.text_style()),
                    ]),
                    Line::from(vec![
                        Span::styled("  • ", t.muted_style()),
                        Span::styled("Vim", t.emphasis_style()),
                        Span::styled(": hjkl navigation, Esc to cancel", t.text_style()),
                    ]),
                    Line::from(vec![
                        Span::styled("  • ", t.muted_style()),
                        Span::styled("Emacs", t.emphasis_style()),
                        Span::styled(": Ctrl+n/p/f/b navigation", t.text_style()),
                    ]),
                    Line::from(""),
                    Line::from(vec![
                        Span::styled(icons.lightbulb(), Style::default().fg(t.secondary)),
                        Span::styled(" Override bindings in config:", t.muted_style()),
                    ]),
                    Line::from(Span::styled("  [keymap.overrides]", t.emphasis_style())),
                    Line::from(Span::styled("  confirm = \"ctrl+s\"", t.emphasis_style())),
                ];
                Text::from(lines)
            }
            Some(SettingItem::Backups) => {
                let lines = vec![
                    Line::from(Span::styled("Automatic Backups", t.title_style())),
                    Line::from(""),
                    Line::from(Span::styled(
                        "When enabled, DotState creates .bak files before overwriting existing files during sync operations.",
                        t.text_style(),
                    )),
                    Line::from(""),
                    Line::from(vec![
                        Span::styled(icons.lightbulb(), Style::default().fg(t.secondary)),
                        Span::styled(" Current: ", t.muted_style()),
                        Span::styled(
                            if config.backup_enabled { "Enabled" } else { "Disabled" },
                            t.emphasis_style(),
                        ),
                    ]),
                ];
                Text::from(lines)
            }
            Some(SettingItem::CheckForUpdates) => {
                let lines = vec![
                    Line::from(Span::styled("Update Checks", t.title_style())),
                    Line::from(""),
                    Line::from(Span::styled(
                        "When enabled, DotState periodically checks for new versions and shows a notification in the main menu.",
                        t.text_style(),
                    )),
                    Line::from(""),
                    Line::from(Span::styled(
                        "You can always manually check for updates using:",
                        t.text_style(),
                    )),
                    Line::from(Span::styled("  dotstate upgrade", t.emphasis_style())),
                    Line::from(""),
                    Line::from(vec![
                        Span::styled(icons.lightbulb(), Style::default().fg(t.secondary)),
                        Span::styled(" Current: ", t.muted_style()),
                        Span::styled(
                            if config.updates.check_enabled { "Enabled" } else { "Disabled" },
                            t.emphasis_style(),
                        ),
                    ]),
                ];
                Text::from(lines)
            }
            Some(SettingItem::EmbedCredentials) => {
                let lines = vec![
                    Line::from(Span::styled("Token in Remote URL", t.title_style())),
                    Line::from(""),
                    Line::from(Span::styled(
                        "Controls how DotState authenticates with GitHub when syncing your dotfiles.",
                        t.text_style(),
                    )),
                    Line::from(""),
                    Line::from(vec![
                        Span::styled("  • ", t.muted_style()),
                        Span::styled("Enabled", t.emphasis_style()),
                        Span::styled(
                            ": Token is stored in the remote URL",
                            t.text_style(),
                        ),
                    ]),
                    Line::from(vec![
                        Span::styled("  • ", t.muted_style()),
                        Span::styled("Disabled", t.emphasis_style()),
                        Span::styled(
                            ": Uses your system's git credential manager",
                            t.text_style(),
                        ),
                    ]),
                    Line::from(""),
                    Line::from(vec![
                        Span::styled(icons.lightbulb(), Style::default().fg(t.secondary)),
                        Span::styled(
                            " Disable if your environment blocks URLs with embedded tokens.",
                            t.muted_style(),
                        ),
                    ]),
                    Line::from(""),
                    Line::from(vec![
                        Span::styled(icons.lightbulb(), Style::default().fg(t.secondary)),
                        Span::styled(" Current: ", t.muted_style()),
                        Span::styled(
                            if config.embed_credentials_in_url { "Enabled" } else { "Disabled" },
                            t.emphasis_style(),
                        ),
                    ]),
                ];
                Text::from(lines)
            }
            None => Text::from(""),
        }
    }

    /// Apply a setting change by setting name (public, for use by App)
    pub fn apply_setting_to_config(
        &self,
        config: &mut Config,
        setting_name: &str,
        option_index: usize,
    ) -> bool {
        Self::apply_setting_by_name(config, setting_name, option_index)
    }

    /// Apply a setting by name and option index
    fn apply_setting_by_name(config: &mut Config, setting_name: &str, option_index: usize) -> bool {
        match setting_name {
            "Theme" => {
                let themes = ThemeType::all();
                if option_index < themes.len() {
                    let selected_theme = themes[option_index];
                    config.theme = selected_theme.to_config_string().to_string();
                    // Apply theme immediately
                    init_theme(selected_theme);
                    return true;
                }
            }
            "Icon Set" => {
                let sets = ["auto", "nerd", "unicode", "emoji", "ascii"];
                if option_index < sets.len() {
                    config.icon_set = sets[option_index].to_string();
                    return true;
                }
            }
            "Keymap Preset" => {
                let presets = [
                    KeymapPreset::Standard,
                    KeymapPreset::Vim,
                    KeymapPreset::Emacs,
                ];
                if option_index < presets.len() {
                    config.keymap.preset = presets[option_index];
                    // Clear overrides when changing preset to ensure clean bindings
                    config.keymap.overrides.clear();
                    return true;
                }
            }
            "Backups" => {
                config.backup_enabled = option_index == 0;
                return true;
            }
            "Check for Updates" => {
                config.updates.check_enabled = option_index == 0;
                return true;
            }
            "Token in Remote URL" => {
                config.embed_credentials_in_url = option_index == 0;
                return true;
            }
            _ => {}
        }
        false
    }

    /// Find the current option index for the selected setting
    fn current_option_index(&self, config: &Config) -> usize {
        let options = self.get_options(config);
        options
            .iter()
            .position(|(_, selected)| *selected)
            .unwrap_or(0)
    }

    fn render_settings_list(&mut self, frame: &mut Frame, area: Rect, config: &Config) {
        let t = theme();
        let icons = Icons::from_config(config);
        let is_focused = self.state.focus == SettingsFocus::List;

        // Store pane area and populate mouse regions
        self.list_pane_area = Some(area);
        self.settings_regions.clear();
        let inner = Block::default().borders(Borders::ALL).inner(area);
        let item_count = SettingItem::all(config.repo_mode).len();
        let scroll_offset = self.state.list_state.offset();
        for i in 0..item_count {
            let visible_idx = i.saturating_sub(scroll_offset);
            if i >= scroll_offset && (visible_idx as u16) < inner.height {
                let row = Rect::new(inner.x, inner.y + visible_idx as u16, inner.width, 1);
                self.settings_regions.add(row, i);
            }
        }

        let items: Vec<ListItem> = SettingItem::all(config.repo_mode)
            .iter()
            .map(|item| {
                let current_value = match item {
                    SettingItem::Theme => config.theme.clone(),
                    SettingItem::IconSet => config.icon_set.clone(),
                    SettingItem::KeymapPreset => format!("{:?}", config.keymap.preset),
                    SettingItem::Backups => {
                        if config.backup_enabled {
                            "On".to_string()
                        } else {
                            "Off".to_string()
                        }
                    }
                    SettingItem::CheckForUpdates => {
                        if config.updates.check_enabled {
                            "On".to_string()
                        } else {
                            "Off".to_string()
                        }
                    }
                    SettingItem::EmbedCredentials => {
                        if config.embed_credentials_in_url {
                            "On".to_string()
                        } else {
                            "Off".to_string()
                        }
                    }
                };

                let line = Line::from(vec![
                    Span::styled(
                        format!("{} ", icons.cog()),
                        Style::default().fg(t.secondary),
                    ),
                    Span::styled(item.name(), t.text_style()),
                    Span::styled(format!(" ({current_value})"), t.muted_style()),
                ]);
                ListItem::new(line)
            })
            .collect();

        let border_style = if is_focused {
            focused_border_style()
        } else {
            unfocused_border_style()
        };

        let list = List::new(items)
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .title(" Settings ")
                    .title_alignment(Alignment::Center)
                    .border_type(t.border_type(is_focused))
                    .border_style(border_style)
                    .style(t.background_style()),
            )
            .highlight_style(t.highlight_style())
            .highlight_symbol(crate::styles::LIST_HIGHLIGHT_SYMBOL);

        StatefulWidget::render(list, area, frame.buffer_mut(), &mut self.state.list_state);
    }

    fn render_options_pane(&mut self, frame: &mut Frame, area: Rect, config: &Config) {
        let t = theme();
        let is_focused = self.state.focus == SettingsFocus::Options;

        // Split into options (top) and explanation (bottom)
        let chunks = Layout::default()
            .direction(Direction::Vertical)
            .constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
            .split(area);

        // Store pane area and populate option regions
        self.options_pane_area = Some(chunks[0]);
        self.option_regions.clear();

        // Render options
        let options = self.get_options(config);
        let icons = Icons::from_config(config);

        // Populate option click regions (each option is 1 line inside the block)
        let options_inner = Block::default().borders(Borders::ALL).inner(chunks[0]);
        for i in 0..options.len() {
            if (i as u16) < options_inner.height {
                let row = Rect::new(
                    options_inner.x,
                    options_inner.y + i as u16,
                    options_inner.width,
                    1,
                );
                self.option_regions.add(row, i);
            }
        }

        let option_lines: Vec<Line> = options
            .iter()
            .enumerate()
            .map(|(i, (name, selected))| {
                let marker = if *selected {
                    icons.circle_filled()
                } else {
                    icons.circle_empty()
                };
                let style = if *selected {
                    Style::default().fg(t.success).add_modifier(Modifier::BOLD)
                } else if is_focused && i == self.state.option_index {
                    t.highlight_style()
                } else {
                    t.text_style()
                };
                Line::from(vec![
                    Span::styled(format!("  {marker} "), style),
                    Span::styled(name.clone(), style),
                ])
            })
            .collect();

        let border_style = if is_focused {
            focused_border_style()
        } else {
            unfocused_border_style()
        };

        let options_block = Paragraph::new(option_lines)
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .title(" Options ")
                    .title_alignment(Alignment::Center)
                    .border_type(t.border_type(is_focused))
                    .border_style(border_style)
                    .style(t.background_style()),
            )
            .wrap(Wrap { trim: false });
        frame.render_widget(options_block, chunks[0]);

        // Render explanation
        let explanation = self.get_explanation(config);
        let explanation_block = Paragraph::new(explanation)
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .title(" Details ")
                    .title_alignment(Alignment::Center)
                    .border_type(t.border_type(false))
                    .border_style(unfocused_border_style())
                    .padding(Padding::proportional(1))
                    .style(t.background_style()),
            )
            .wrap(Wrap { trim: false });
        frame.render_widget(explanation_block, chunks[1]);
    }
}

impl Screen for SettingsScreen {
    fn render(&mut self, frame: &mut Frame, area: Rect, ctx: &RenderContext) -> Result<()> {
        // Standard layout (header=5, footer=2)
        let (header_chunk, content_chunk, footer_chunk) = create_standard_layout(area, 5, 3);

        // Header
        Header::render(
            frame,
            header_chunk,
            "DotState - Settings",
            "Configure your preferences. Changes are applied instantly.",
        )?;

        // Content: two-pane layout
        let panes = create_split_layout(content_chunk, &[40, 60]);

        // Left: settings list
        self.render_settings_list(frame, panes[0], ctx.config);

        // Right: options and explanation
        self.render_options_pane(frame, panes[1], ctx.config);

        // Footer
        let k = |a| ctx.config.keymap.get_key_display_for_action(a);
        let footer_text = format!(
            "{}: Navigate | {}: Switch Focus | {}: Select | {}: Back",
            ctx.config.keymap.navigation_display(),
            k(Action::NextTab),
            k(Action::Confirm),
            k(Action::Cancel),
        );
        Footer::render(frame, footer_chunk, &footer_text)?;

        Ok(())
    }

    fn handle_event(&mut self, event: Event, ctx: &ScreenContext) -> Result<ScreenAction> {
        match event {
            Event::Key(key) if key.kind == KeyEventKind::Press => {
                let action = ctx.config.keymap.get_action(key.code, key.modifiers);

                if let Some(action) = action {
                    match self.state.focus {
                        SettingsFocus::List => match action {
                            Action::MoveUp => {
                                self.state.list_state.select_previous();
                                self.state.option_index = self.current_option_index(ctx.config);
                            }
                            Action::MoveDown => {
                                self.state.list_state.select_next();
                                self.state.option_index = self.current_option_index(ctx.config);
                            }
                            Action::Confirm | Action::NextTab | Action::MoveRight => {
                                self.state.focus = SettingsFocus::Options;
                                self.state.option_index = self.current_option_index(ctx.config);
                            }
                            Action::Cancel | Action::Quit => {
                                return Ok(ScreenAction::Navigate(ScreenId::MainMenu));
                            }
                            _ => {}
                        },
                        SettingsFocus::Options => {
                            let options = self.get_options(ctx.config);
                            match action {
                                Action::MoveUp if self.state.option_index > 0 => {
                                    self.state.option_index -= 1;
                                }
                                Action::MoveDown
                                    if self.state.option_index
                                        < options.len().saturating_sub(1) =>
                                {
                                    self.state.option_index += 1;
                                }
                                Action::Confirm => {
                                    return Ok(ScreenAction::UpdateSetting {
                                        setting: self
                                            .selected_setting(ctx.config.repo_mode)
                                            .map(|s| s.name().to_string())
                                            .unwrap_or_default(),
                                        option_index: self.state.option_index,
                                    });
                                }
                                Action::NextTab | Action::MoveLeft | Action::Cancel => {
                                    self.state.focus = SettingsFocus::List;
                                }
                                Action::Quit => {
                                    return Ok(ScreenAction::Navigate(ScreenId::MainMenu));
                                }
                                _ => {}
                            }
                        }
                    }
                }
            }
            Event::Mouse(mouse) => {
                return self.handle_mouse_event(mouse, ctx);
            }
            _ => {}
        }

        Ok(ScreenAction::None)
    }

    fn is_input_focused(&self) -> bool {
        false
    }

    fn on_enter(&mut self, _ctx: &ScreenContext) -> Result<()> {
        // Reset to first setting
        self.state.list_state.select(Some(0));
        self.state.focus = SettingsFocus::List;
        self.state.option_index = 0;
        Ok(())
    }
}

impl SettingsScreen {
    fn handle_mouse_event(
        &mut self,
        mouse: crossterm::event::MouseEvent,
        ctx: &ScreenContext,
    ) -> Result<ScreenAction> {
        match mouse.kind {
            MouseEventKind::Down(MouseButton::Left) => {
                // Check settings list click
                if let Some(&idx) = self.settings_regions.hit_test(mouse.column, mouse.row) {
                    self.state.list_state.select(Some(idx));
                    self.state.focus = SettingsFocus::List;
                    self.state.option_index = self.current_option_index(ctx.config);
                    return Ok(ScreenAction::Refresh);
                }
                // Check options click
                if let Some(&idx) = self.option_regions.hit_test(mouse.column, mouse.row) {
                    self.state.focus = SettingsFocus::Options;
                    self.state.option_index = idx;
                    // Apply immediately on click
                    return Ok(ScreenAction::UpdateSetting {
                        setting: self
                            .selected_setting(ctx.config.repo_mode)
                            .map(|s| s.name().to_string())
                            .unwrap_or_default(),
                        option_index: idx,
                    });
                }
            }
            MouseEventKind::ScrollUp => {
                if let Some(area) = self.list_pane_area {
                    if area.contains(ratatui::layout::Position::new(mouse.column, mouse.row)) {
                        for _ in 0..3 {
                            self.state.list_state.select_previous();
                        }
                        self.state.option_index = self.current_option_index(ctx.config);
                        return Ok(ScreenAction::Refresh);
                    }
                }
                if let Some(area) = self.options_pane_area {
                    if area.contains(ratatui::layout::Position::new(mouse.column, mouse.row)) {
                        self.state.focus = SettingsFocus::Options;
                        self.state.option_index = self.state.option_index.saturating_sub(3);
                        return Ok(ScreenAction::Refresh);
                    }
                }
            }
            MouseEventKind::ScrollDown => {
                if let Some(area) = self.list_pane_area {
                    if area.contains(ratatui::layout::Position::new(mouse.column, mouse.row)) {
                        for _ in 0..3 {
                            self.state.list_state.select_next();
                        }
                        self.state.option_index = self.current_option_index(ctx.config);
                        return Ok(ScreenAction::Refresh);
                    }
                }
                if let Some(area) = self.options_pane_area {
                    if area.contains(ratatui::layout::Position::new(mouse.column, mouse.row)) {
                        self.state.focus = SettingsFocus::Options;
                        let max = self.get_options(ctx.config).len().saturating_sub(1);
                        self.state.option_index = (self.state.option_index + 3).min(max);
                        return Ok(ScreenAction::Refresh);
                    }
                }
            }
            _ => {}
        }
        Ok(ScreenAction::None)
    }
}