gitwig 2.2.1

a rust based tui, an alternative to sourcetree and gitui
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
use crate::app::{App, Mode};
use crate::config::SortOrder;
use crate::repo::RemoteInfo;
use crate::ui::layout::{centered_rect, centered_rect_fixed};
use crate::ui::style::{
    ACCENT, CARD_BORDER, DANGER, SUCCESS, WARNING, accent_style, muted_style, parse_color,
    primary_style,
};
use crate::ui::{wrap_excludes, wrap_str};
use ratatui::Frame;
use ratatui::layout::{Alignment, Constraint, Direction, Layout, Margin, Position, Rect};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span, Text};
use ratatui::widgets::{Block, BorderType, Borders, Clear, Padding, Paragraph, Wrap};

const GENERAL_SETTING_INDICES: &[usize] = &[0, 7, 9, 12, 13];
const SORTING_SETTING_INDICES: &[usize] = &[1, 2, 6];
const FZF_SETTING_INDICES: &[usize] = &[11, 5, 4, 10, 8];
const THEME_SETTING_INDICES: &[usize] = &[3];

fn get_category_indices(cat: usize) -> &'static [usize] {
    match cat {
        0 => GENERAL_SETTING_INDICES,
        1 => SORTING_SETTING_INDICES,
        2 => FZF_SETTING_INDICES,
        3 => THEME_SETTING_INDICES,
        _ => &[],
    }
}

fn get_category_name(cat: usize) -> &'static str {
    match cat {
        0 => "General Settings",
        1 => "Sorting & Limits",
        2 => "FZF Discovery",
        3 => "Theme & Style",
        _ => "",
    }
}

fn get_category_icon(cat: usize, compat: bool) -> &'static str {
    if compat {
        match cat {
            0 => "* ",
            1 => "# ",
            2 => "? ",
            3 => "@ ",
            _ => "",
        }
    } else {
        match cat {
            0 => "",
            1 => "📶 ",
            2 => "🔍 ",
            3 => "🎨 ",
            _ => "",
        }
    }
}

fn get_active_category(selected_idx: usize) -> usize {
    if GENERAL_SETTING_INDICES.contains(&selected_idx) {
        0
    } else if SORTING_SETTING_INDICES.contains(&selected_idx) {
        1
    } else if FZF_SETTING_INDICES.contains(&selected_idx) {
        2
    } else {
        3
    }
}

fn get_sub_index(selected_idx: usize) -> usize {
    let cat = get_active_category(selected_idx);
    let indices = get_category_indices(cat);
    indices.iter().position(|&x| x == selected_idx).unwrap_or(0)
}

fn get_label(global_idx: usize) -> &'static str {
    match global_idx {
        0 => "Poll Interval (ms)",
        1 => "Sort By",
        2 => "Sort Reverse",
        3 => "Theme Name",
        4 => "FZF Max Depth",
        5 => "FZF Start Dir",
        6 => "Max Commits",
        7 => "Page Size",
        8 => "FZF Exclude Folders",
        9 => "Preferred Git Client",
        10 => "FZF Git Only",
        11 => "Use FZF",
        12 => "Compatibility Mode",
        13 => "Resync on Tab Change",
        _ => "",
    }
}

fn get_desc(global_idx: usize) -> &'static str {
    match global_idx {
        0 => "Event-loop poll interval in milliseconds. Sane range: 16-500.",
        1 => "Initial repository sorting criteria.",
        2 => "Reverse the order of repositories.",
        3 => "Active theme configuration name. Press Enter/Space to select from dropdown.",
        4 => "Maximum directory depth to search for git repositories.",
        5 => "Starting directory for interactive repository discovery via FZF.",
        6 => "Maximum commits to load in workspace view. Set to 0 for unlimited.",
        7 => "Number of lines/items scrolled by Page Up / Page Down.",
        8 => "Comma-separated list of folders/patterns to exclude from FZF search.",
        9 => "External Git application triggered by 'g' key (e.g. gitui or lazygit).",
        10 => "Only scan folders that contain a .git directory.",
        11 => {
            "Whether to use FZF for repository discovery. If disabled, manual text input is used."
        }
        12 => {
            "Use simple ASCII symbols instead of complex Unicode emojis/icons to avoid layout breakage in some terminals."
        }
        13 => {
            "Whether to automatically refresh repository details from disk when switching tabs inside a repository."
        }
        _ => "",
    }
}

fn get_val_str(app: &App, global_idx: usize) -> String {
    let is_selected = app.settings_selected_index == global_idx;
    match global_idx {
        0 => {
            if is_selected && app.settings_editing {
                format!("{}", app.input_buffer)
            } else {
                app.config.poll_interval_ms.to_string()
            }
        }
        1 => {
            let s = match app.config.sort_by {
                SortOrder::Alphabetical => "Alphabetical",
                SortOrder::RecentVisit => "Recent Visit",
                SortOrder::LatestChanges => "Latest Changes",
                SortOrder::Custom => "Custom",
            };
            s.to_string()
        }
        2 => app.config.sort_reverse.to_string(),
        3 => {
            if is_selected && app.settings_editing {
                if app.settings_theme_index < app.settings_theme_list.len() {
                    app.settings_theme_list[app.settings_theme_index].clone()
                } else {
                    app.config.theme_name.clone()
                }
            } else {
                app.config.theme_name.clone()
            }
        }
        4 => {
            if is_selected && app.settings_editing {
                format!("{}", app.input_buffer)
            } else {
                app.config.fzf.max_depth.to_string()
            }
        }
        5 => {
            if is_selected && app.settings_editing {
                format!("{}", app.input_buffer)
            } else {
                app.config.fzf.start_dir.clone()
            }
        }
        6 => {
            if is_selected && app.settings_editing {
                format!("{}", app.input_buffer)
            } else {
                app.config.max_commits.to_string()
            }
        }
        7 => {
            if is_selected && app.settings_editing {
                format!("{}", app.input_buffer)
            } else {
                app.config.page_size.to_string()
            }
        }
        8 => {
            if is_selected && app.settings_editing {
                format!("{}", app.input_buffer)
            } else {
                app.config.fzf.excludes.join(",")
            }
        }
        9 => {
            if is_selected && app.settings_editing {
                format!("{}", app.input_buffer)
            } else {
                app.config.git_app.clone()
            }
        }
        10 => app.config.fzf.git_only.to_string(),
        11 => app.config.fzf.enabled.to_string(),
        12 => app.config.compatibility_mode.to_string(),
        13 => app.config.resync_on_tab_change.to_string(),
        _ => String::new(),
    }
}

pub fn draw_settings_page(f: &mut Frame, app: &App, area: Rect) {
    let popup_area = centered_rect(75, 75, area);

    // Draw background block
    let block = Block::default()
        .borders(Borders::ALL)
        .border_type(CARD_BORDER())
        .border_style(accent_style())
        .title(
            Line::from(vec![
                Span::raw(" "),
                Span::styled("Settings", primary_style()),
                Span::raw(" "),
            ])
            .alignment(Alignment::Center),
        );

    f.render_widget(Clear, popup_area);
    f.render_widget(block.clone(), popup_area);

    let inner_rect = block.inner(popup_area);

    // Split inner_rect horizontally: sidebar vs content
    let chunks = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Percentage(28), // Sidebar
            Constraint::Length(1),      // Separator
            Constraint::Percentage(71), // Right content pane
        ])
        .split(inner_rect);

    // Draw vertical separator
    let sep_block = Block::default().borders(Borders::LEFT).border_style(muted_style());
    f.render_widget(sep_block, chunks[1]);

    let active_cat = get_active_category(app.settings_selected_index);
    let is_compat = app.config.compatibility_mode;

    // 1. Sidebar Categories Rendering
    let mut sidebar_items = Vec::new();
    for idx in 0..4 {
        let is_selected = idx == active_cat;
        let is_focused = is_selected && app.settings_focus_sidebar;

        let prefix = if is_focused {
            if is_compat { "> " } else { "" }
        } else if is_selected {
            if is_compat { "o " } else { "" }
        } else {
            "  "
        };

        let icon = get_category_icon(idx, is_compat);
        let name = get_category_name(idx);
        let style = if is_focused {
            accent_style().add_modifier(Modifier::BOLD)
        } else if is_selected {
            primary_style().add_modifier(Modifier::BOLD)
        } else {
            muted_style()
        };

        sidebar_items.push(Line::from(vec![
            Span::styled(prefix, if is_focused { accent_style() } else { muted_style() }),
            Span::styled(icon, style),
            Span::styled(name, style),
        ]));
        sidebar_items.push(Line::from("")); // spacer
    }

    let sidebar_block = Block::default()
        .borders(Borders::ALL)
        .border_type(CARD_BORDER())
        .border_style(if app.settings_focus_sidebar { accent_style() } else { muted_style() })
        .title(Span::styled(" Categories ", primary_style()))
        .padding(Padding::horizontal(1));

    let sidebar_inner = sidebar_block.inner(chunks[0]);
    f.render_widget(sidebar_block, chunks[0]);

    let sidebar_paragraph = Paragraph::new(sidebar_items);
    f.render_widget(sidebar_paragraph, sidebar_inner);

    // 2. Settings Content Pane Rendering
    let indices = get_category_indices(active_cat);
    let right_inner_rect = chunks[2].inner(Margin { horizontal: 1, vertical: 1 });
    let available_text_width = (right_inner_rect.width as usize).saturating_sub(6);

    let mut right_items = Vec::new();
    let mut current_line = 0;
    let mut item_starts = vec![0; indices.len()];

    let mut selected_val_chunks_len = 1;
    let mut selected_last_chunk_char_count = 0;
    let mut selected_val_offset = 11;

    let active_sub_idx = get_sub_index(app.settings_selected_index);

    for (sub_idx, &global_idx) in indices.iter().enumerate() {
        let is_selected = sub_idx == active_sub_idx && !app.settings_focus_sidebar;

        let label = get_label(global_idx);
        let desc = get_desc(global_idx);
        let val_str = get_val_str(app, global_idx);

        let val_offset = if is_selected && app.settings_editing { 11 } else { 5 };
        let val_width = available_text_width.saturating_sub(val_offset).max(10);
        let val_chunks = if global_idx == 8 {
            wrap_excludes(&val_str, val_width)
        } else {
            wrap_str(&val_str, val_width)
        };

        let desc_offset = 5;
        let desc_width = available_text_width.saturating_sub(desc_offset).max(10);
        let desc_chunks = wrap_str(desc, desc_width);

        item_starts[sub_idx] = current_line;
        let item_height = 1 + val_chunks.len() + desc_chunks.len() + 1; // label + value + desc + spacer
        current_line += item_height;

        if is_selected {
            selected_val_chunks_len = val_chunks.len();
            selected_last_chunk_char_count =
                val_chunks.last().map(|c| c.chars().count()).unwrap_or(0);
            selected_val_offset = val_offset;
        }

        let prefix = if is_selected { "> " } else { "  " };

        // Line 1: Label line
        right_items.push(Line::from(vec![
            Span::styled(prefix, if is_selected { accent_style() } else { muted_style() }),
            Span::styled(
                label,
                if is_selected {
                    accent_style().add_modifier(Modifier::BOLD)
                } else {
                    primary_style()
                },
            ),
        ]));

        // Line 2: First line of value (indented by val_offset)
        let mut val_line_spans = Vec::new();
        if is_selected && app.settings_editing {
            let label_edit = if global_idx == 3 { "   [Select]: " } else { "   [Edit]: " };
            val_line_spans.push(Span::styled(label_edit, muted_style()));
            val_line_spans.push(Span::styled(
                val_chunks[0].clone(),
                Style::default().fg(ACCENT()).add_modifier(Modifier::UNDERLINED),
            ));
        } else {
            val_line_spans.push(Span::styled("   : ", muted_style()));
            val_line_spans.push(Span::styled(
                val_chunks[0].clone(),
                if is_selected { accent_style() } else { Style::default() },
            ));
        }
        right_items.push(Line::from(val_line_spans));

        // Subsequent lines of the value (indented by val_offset spaces)
        for chunk in val_chunks.iter().skip(1) {
            let spaces = " ".repeat(val_offset);
            let span = Span::styled(
                chunk.clone(),
                if is_selected && app.settings_editing {
                    Style::default().fg(ACCENT()).add_modifier(Modifier::UNDERLINED)
                } else if is_selected {
                    accent_style()
                } else {
                    Style::default()
                },
            );
            right_items.push(Line::from(vec![Span::raw(spaces), span]));
        }

        // Description lines (indented by 5 spaces)
        for chunk in desc_chunks {
            right_items
                .push(Line::from(vec![Span::raw("     "), Span::styled(chunk, muted_style())]));
        }

        // Spacer
        right_items.push(Line::from(""));
    }

    let viewport_height = right_inner_rect.height as usize;
    let total_height = current_line;
    let mut scroll_y = if viewport_height >= total_height {
        0
    } else {
        let sel_start = item_starts[active_sub_idx];
        let sel_height = if active_sub_idx + 1 < indices.len() {
            item_starts[active_sub_idx + 1] - sel_start
        } else {
            total_height - sel_start
        };
        let item_center = sel_start + sel_height / 2;
        let target_scroll = item_center.saturating_sub(viewport_height / 2);
        let max_scroll = total_height.saturating_sub(viewport_height);
        target_scroll.min(max_scroll)
    };

    if app.settings_editing && app.settings_selected_index != 3 && !app.settings_focus_sidebar {
        let cursor_line = item_starts[active_sub_idx] + 1 + (selected_val_chunks_len - 1);
        if cursor_line < scroll_y {
            scroll_y = cursor_line;
        } else if cursor_line >= scroll_y + viewport_height {
            scroll_y = cursor_line.saturating_sub(viewport_height).saturating_add(1);
        }
    }

    let right_title = format!(" {} ", get_category_name(active_cat));
    let right_block = Block::default()
        .borders(Borders::ALL)
        .border_type(CARD_BORDER())
        .border_style(if !app.settings_focus_sidebar { accent_style() } else { muted_style() })
        .title(Span::styled(right_title, primary_style()))
        .padding(Padding::horizontal(1));

    let content_inner = right_block.inner(chunks[2]);
    f.render_widget(right_block, chunks[2]);

    let paragraph =
        Paragraph::new(right_items).alignment(Alignment::Left).scroll((scroll_y as u16, 0));
    f.render_widget(paragraph, content_inner);

    // Dropdown rendering for theme name (index 3)
    if app.settings_editing && app.settings_selected_index == 3 && !app.settings_focus_sidebar {
        let dropdown_width = 30;
        let dropdown_height = (app.settings_theme_list.len() + 2) as u16;

        let theme_row_y = item_starts[active_sub_idx] as u16;
        let dropdown_x = content_inner.x + 15;
        let dropdown_y = (content_inner.y + theme_row_y + 2).saturating_sub(scroll_y as u16);

        let dropdown_area = Rect::new(
            dropdown_x.min(area.right().saturating_sub(dropdown_width)),
            dropdown_y.min(area.bottom().saturating_sub(dropdown_height)),
            dropdown_width.min(area.width),
            dropdown_height.min(area.height),
        );

        let dropdown_block = Block::default()
            .borders(Borders::ALL)
            .border_type(CARD_BORDER())
            .border_style(accent_style())
            .title(Span::styled(" Select Theme ", accent_style()));

        f.render_widget(Clear, dropdown_area);
        f.render_widget(dropdown_block.clone(), dropdown_area);

        let dropdown_inner = dropdown_block.inner(dropdown_area);

        let mut theme_spans = Vec::new();
        for (idx, theme_name) in app.settings_theme_list.iter().enumerate() {
            let is_active = idx == app.settings_theme_index;
            let prefix = if is_active { if is_compat { "> " } else { "" } } else { "  " };
            let style = if is_active {
                accent_style().add_modifier(Modifier::BOLD)
            } else {
                Style::default()
            };
            theme_spans.push(Line::from(Span::styled(format!("{}{}", prefix, theme_name), style)));
        }

        let list = Paragraph::new(theme_spans);
        f.render_widget(list, dropdown_inner);
    }

    // Cursor position rendering
    if app.settings_editing && app.settings_selected_index != 3 && !app.settings_focus_sidebar {
        let cursor_line = item_starts[active_sub_idx] + 1 + (selected_val_chunks_len - 1);

        if cursor_line >= scroll_y && cursor_line < scroll_y + viewport_height {
            let cursor_y = (content_inner.y + cursor_line as u16).saturating_sub(scroll_y as u16);
            let cursor_x = content_inner.x
                + selected_val_offset as u16
                + selected_last_chunk_char_count.saturating_sub(1) as u16;
            f.set_cursor_position(Position::new(cursor_x, cursor_y));
        }
    }
}

use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
pub struct SettingsPopup;
impl SettingsPopup {
    pub fn handle_event(app: &mut crate::app::App, key: KeyEvent) -> bool {
        let code = key.code;
        if app.settings_editing && app.settings_selected_index == 3 {
            match code {
                KeyCode::Esc => app.cancel_settings_edit(),
                KeyCode::Enter => app.commit_settings_edit(),
                KeyCode::Down | KeyCode::Char('j') | KeyCode::Char('J')
                    if app.settings_theme_index + 1 < app.settings_theme_list.len() =>
                {
                    app.settings_theme_index += 1;
                }
                KeyCode::Up | KeyCode::Char('k') | KeyCode::Char('K')
                    if app.settings_theme_index > 0 =>
                {
                    app.settings_theme_index -= 1;
                }
                KeyCode::PageUp if app.settings_theme_index > 0 => {
                    app.settings_theme_index =
                        app.settings_theme_index.saturating_sub(app.config.page_size);
                }
                KeyCode::PageDown
                    if app.settings_theme_index + 1 < app.settings_theme_list.len() =>
                {
                    app.settings_theme_index = (app.settings_theme_index + app.config.page_size)
                        .min(app.settings_theme_list.len().saturating_sub(1));
                }
                KeyCode::Home => {
                    app.settings_theme_index = 0;
                }
                KeyCode::End if !app.settings_theme_list.is_empty() => {
                    app.settings_theme_index = app.settings_theme_list.len() - 1;
                }
                _ => {}
            }
        } else if app.settings_editing {
            match code {
                KeyCode::Esc => app.cancel_settings_edit(),
                KeyCode::Enter => app.commit_settings_edit(),
                KeyCode::Backspace => app.input_backspace(),
                KeyCode::Char(c) => app.input_char(c),
                _ => {}
            }
        } else {
            // Non-editing mode
            match code {
                KeyCode::Esc => {
                    if !app.settings_focus_sidebar {
                        app.settings_focus_sidebar = true;
                    } else {
                        app.mode = Mode::Normal;
                    }
                }
                KeyCode::Char('q') | KeyCode::Char('Q') => {
                    app.mode = Mode::Normal;
                }
                KeyCode::Left | KeyCode::Char('h') | KeyCode::Char('H') => {
                    app.settings_focus_sidebar = true;
                }
                KeyCode::Right | KeyCode::Char('l') | KeyCode::Char('L') => {
                    app.settings_focus_sidebar = false;
                }
                KeyCode::Char('w') | KeyCode::Char('W') => {
                    app.settings_focus_sidebar = false;
                }
                KeyCode::Char('1') => {
                    app.settings_selected_index = GENERAL_SETTING_INDICES[0];
                    app.settings_focus_sidebar = false;
                }
                KeyCode::Char('2') => {
                    app.settings_selected_index = SORTING_SETTING_INDICES[0];
                    app.settings_focus_sidebar = false;
                }
                KeyCode::Char('3') => {
                    app.settings_selected_index = FZF_SETTING_INDICES[0];
                    app.settings_focus_sidebar = false;
                }
                KeyCode::Char('4') => {
                    app.settings_selected_index = THEME_SETTING_INDICES[0];
                    app.settings_focus_sidebar = false;
                }
                KeyCode::Down | KeyCode::Char('j') | KeyCode::Char('J') => {
                    if app.settings_focus_sidebar {
                        let cat = get_active_category(app.settings_selected_index);
                        if cat + 1 < 4 {
                            app.settings_selected_index = get_category_indices(cat + 1)[0];
                        }
                    } else {
                        let cat = get_active_category(app.settings_selected_index);
                        let indices = get_category_indices(cat);
                        let sub = get_sub_index(app.settings_selected_index);
                        if sub + 1 < indices.len() {
                            app.settings_selected_index = indices[sub + 1];
                        }
                    }
                }
                KeyCode::Up | KeyCode::Char('k') | KeyCode::Char('K') => {
                    if app.settings_focus_sidebar {
                        let cat = get_active_category(app.settings_selected_index);
                        if cat > 0 {
                            app.settings_selected_index = get_category_indices(cat - 1)[0];
                        }
                    } else {
                        let cat = get_active_category(app.settings_selected_index);
                        let indices = get_category_indices(cat);
                        let sub = get_sub_index(app.settings_selected_index);
                        if sub > 0 {
                            app.settings_selected_index = indices[sub - 1];
                        }
                    }
                }
                KeyCode::PageUp => {
                    if app.settings_focus_sidebar {
                        app.settings_selected_index = GENERAL_SETTING_INDICES[0];
                    } else {
                        let cat = get_active_category(app.settings_selected_index);
                        app.settings_selected_index = get_category_indices(cat)[0];
                    }
                }
                KeyCode::PageDown => {
                    if app.settings_focus_sidebar {
                        app.settings_selected_index = THEME_SETTING_INDICES[0];
                    } else {
                        let cat = get_active_category(app.settings_selected_index);
                        let indices = get_category_indices(cat);
                        app.settings_selected_index = indices[indices.len() - 1];
                    }
                }
                KeyCode::Home => {
                    if app.settings_focus_sidebar {
                        app.settings_selected_index = GENERAL_SETTING_INDICES[0];
                    } else {
                        let cat = get_active_category(app.settings_selected_index);
                        app.settings_selected_index = get_category_indices(cat)[0];
                    }
                }
                KeyCode::End => {
                    if app.settings_focus_sidebar {
                        app.settings_selected_index = THEME_SETTING_INDICES[0];
                    } else {
                        let cat = get_active_category(app.settings_selected_index);
                        let indices = get_category_indices(cat);
                        app.settings_selected_index = indices[indices.len() - 1];
                    }
                }
                KeyCode::Enter | KeyCode::Char(' ') => {
                    if app.settings_focus_sidebar {
                        app.settings_focus_sidebar = false;
                    } else {
                        app.toggle_or_edit_setting();
                    }
                }
                _ => {}
            }
        }
        true
    }
}