fresh-editor 0.1.96

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
//! File browser popup renderer for the Open File dialog
//!
//! Renders a structured popup above the prompt with:
//! - Navigation shortcuts (parent, root, home)
//! - Sortable column headers (name, size, modified)
//! - File list with metadata
//! - Scrollbar for long lists

use super::scrollbar::{render_scrollbar, ScrollbarColors, ScrollbarState};
use super::status_bar::truncate_path;
use crate::app::file_open::{
    format_modified, format_size, FileOpenSection, FileOpenState, SortMode,
};
use crate::primitives::display_width::str_width;
use ratatui::layout::Rect;
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Clear, Paragraph};
use ratatui::Frame;
use rust_i18n::t;

/// Renderer for the file browser popup
pub struct FileBrowserRenderer;

impl FileBrowserRenderer {
    /// Render the file browser popup
    ///
    /// # Arguments
    /// * `frame` - The ratatui frame to render to
    /// * `area` - The rectangular area for the popup (above the prompt)
    /// * `state` - The file open dialog state
    /// * `theme` - The active theme for colors
    /// * `hover_target` - Current mouse hover target (for highlighting)
    /// * `keybindings` - Optional keybinding resolver for displaying shortcuts
    ///
    /// # Returns
    /// Information for mouse hit testing (scrollbar area, thumb positions, etc.)
    pub fn render(
        frame: &mut Frame,
        area: Rect,
        state: &FileOpenState,
        theme: &crate::view::theme::Theme,
        hover_target: &Option<crate::app::HoverTarget>,
        keybindings: Option<&crate::input::keybindings::KeybindingResolver>,
    ) -> Option<FileBrowserLayout> {
        if area.height < 5 || area.width < 20 {
            return None;
        }

        // Clear the area behind the popup
        frame.render_widget(Clear, area);

        // Truncate path for title if needed (leave space for borders and padding)
        let max_title_len = (area.width as usize).saturating_sub(4); // 2 for borders, 2 for padding
        let truncated_path = truncate_path(&state.current_dir, max_title_len);
        let title = format!(" {} ", truncated_path.to_string_plain());

        // Create styled title with highlighted [...] if truncated
        let title_line = if truncated_path.truncated {
            Line::from(vec![
                Span::raw(" "),
                Span::styled(
                    truncated_path.prefix.clone(),
                    Style::default().fg(theme.popup_border_fg),
                ),
                Span::styled("/[...]", Style::default().fg(theme.menu_highlight_fg)),
                Span::styled(
                    truncated_path.suffix.clone(),
                    Style::default().fg(theme.popup_border_fg),
                ),
                Span::raw(" "),
            ])
        } else {
            Line::from(title)
        };

        // Create the popup block with border
        let block = Block::default()
            .borders(Borders::ALL)
            .border_style(Style::default().fg(theme.popup_border_fg))
            .style(Style::default().bg(theme.popup_bg))
            .title(title_line);

        let inner_area = block.inner(area);
        frame.render_widget(block, area);

        if inner_area.height < 3 || inner_area.width < 10 {
            return None;
        }

        // Layout: Navigation (2-3 rows) | Header (1 row) | File list (remaining) | Scrollbar (1 col)
        let nav_height = 2u16; // Navigation shortcuts section
        let header_height = 1u16;
        let scrollbar_width = 1u16;

        let content_width = inner_area.width.saturating_sub(scrollbar_width);
        let list_height = inner_area.height.saturating_sub(nav_height + header_height);

        // Navigation area
        let nav_area = Rect::new(inner_area.x, inner_area.y, content_width, nav_height);

        // Header area
        let header_area = Rect::new(
            inner_area.x,
            inner_area.y + nav_height,
            content_width,
            header_height,
        );

        // File list area
        let list_area = Rect::new(
            inner_area.x,
            inner_area.y + nav_height + header_height,
            content_width,
            list_height,
        );

        // Scrollbar area
        let scrollbar_area = Rect::new(
            inner_area.x + content_width,
            inner_area.y + nav_height + header_height,
            scrollbar_width,
            list_height,
        );

        // Render each section with hover state
        Self::render_navigation(frame, nav_area, state, theme, hover_target, keybindings);
        Self::render_header(frame, header_area, state, theme, hover_target);
        let visible_rows = Self::render_file_list(frame, list_area, state, theme, hover_target);

        // Render scrollbar with theme colors (hover-aware)
        let scrollbar_state =
            ScrollbarState::new(state.entries.len(), visible_rows, state.scroll_offset);
        let is_scrollbar_hovered = matches!(
            hover_target,
            Some(crate::app::HoverTarget::FileBrowserScrollbar)
        );
        let colors = if is_scrollbar_hovered {
            ScrollbarColors::from_theme_hover(theme)
        } else {
            ScrollbarColors::from_theme(theme)
        };
        let (thumb_start, thumb_end) =
            render_scrollbar(frame, scrollbar_area, &scrollbar_state, &colors);

        Some(FileBrowserLayout {
            nav_area,
            header_area,
            list_area,
            scrollbar_area,
            thumb_start,
            thumb_end,
            visible_rows,
            content_width,
        })
    }

    /// Render navigation shortcuts section with "Show Hidden" checkbox on separate row
    fn render_navigation(
        frame: &mut Frame,
        area: Rect,
        state: &FileOpenState,
        theme: &crate::view::theme::Theme,
        hover_target: &Option<crate::app::HoverTarget>,
        keybindings: Option<&crate::input::keybindings::KeybindingResolver>,
    ) {
        use crate::app::HoverTarget;

        // Look up the keybinding for toggle hidden action
        let shortcut_hint = keybindings
            .and_then(|kb| {
                kb.get_keybinding_for_action(
                    &crate::input::keybindings::Action::FileBrowserToggleHidden,
                    crate::input::keybindings::KeyContext::Prompt,
                )
            })
            .unwrap_or_default();

        // First line: "Show Hidden" checkbox (on its own row to avoid truncation on Windows)
        let checkbox_icon = if state.show_hidden { "" } else { "" };
        let checkbox_label = format!("{} {}", checkbox_icon, t!("file_browser.show_hidden"));
        let shortcut_text = if shortcut_hint.is_empty() {
            String::new()
        } else {
            format!(" ({})", shortcut_hint)
        };

        let is_checkbox_hovered = matches!(
            hover_target,
            Some(HoverTarget::FileBrowserShowHiddenCheckbox)
        );
        let checkbox_style = if is_checkbox_hovered {
            Style::default()
                .fg(theme.menu_hover_fg)
                .bg(theme.menu_hover_bg)
        } else if state.show_hidden {
            Style::default()
                .fg(theme.menu_highlight_fg)
                .bg(theme.popup_bg)
        } else {
            Style::default().fg(theme.help_key_fg).bg(theme.popup_bg)
        };
        let shortcut_style = if is_checkbox_hovered {
            Style::default()
                .fg(theme.menu_hover_fg)
                .bg(theme.menu_hover_bg)
        } else {
            Style::default()
                .fg(theme.help_separator_fg)
                .bg(theme.popup_bg)
        };

        let mut checkbox_spans = Vec::new();
        checkbox_spans.push(Span::styled(format!(" {}", checkbox_label), checkbox_style));
        if !shortcut_text.is_empty() {
            checkbox_spans.push(Span::styled(shortcut_text, shortcut_style));
        }
        // Fill rest of row with background
        let checkbox_line_width: usize = checkbox_spans.iter().map(|s| str_width(&s.content)).sum();
        let remaining = (area.width as usize).saturating_sub(checkbox_line_width);
        if remaining > 0 {
            checkbox_spans.push(Span::styled(
                " ".repeat(remaining),
                Style::default().bg(theme.popup_bg),
            ));
        }
        let checkbox_line = Line::from(checkbox_spans);

        // Second line: Navigation shortcuts
        let is_nav_active = state.active_section == FileOpenSection::Navigation;

        let mut nav_spans = Vec::new();
        nav_spans.push(Span::styled(
            format!(" {}", t!("file_browser.navigation")),
            Style::default()
                .fg(theme.help_separator_fg)
                .bg(theme.popup_bg),
        ));

        for (idx, shortcut) in state.shortcuts.iter().enumerate() {
            let is_selected = is_nav_active && idx == state.selected_shortcut;
            let is_hovered =
                matches!(hover_target, Some(HoverTarget::FileBrowserNavShortcut(i)) if *i == idx);

            let style = if is_selected {
                Style::default()
                    .fg(theme.popup_text_fg)
                    .bg(theme.suggestion_selected_bg)
                    .add_modifier(Modifier::BOLD)
            } else if is_hovered {
                Style::default()
                    .fg(theme.menu_hover_fg)
                    .bg(theme.menu_hover_bg)
            } else {
                Style::default().fg(theme.help_key_fg).bg(theme.popup_bg)
            };

            nav_spans.push(Span::styled(format!(" {} ", shortcut.label), style));

            if idx < state.shortcuts.len() - 1 {
                nav_spans.push(Span::styled(
                    "",
                    Style::default()
                        .fg(theme.help_separator_fg)
                        .bg(theme.popup_bg),
                ));
            }
        }

        // Fill rest of navigation row with background
        let nav_line_width: usize = nav_spans.iter().map(|s| str_width(&s.content)).sum();
        let nav_remaining = (area.width as usize).saturating_sub(nav_line_width);
        if nav_remaining > 0 {
            nav_spans.push(Span::styled(
                " ".repeat(nav_remaining),
                Style::default().bg(theme.popup_bg),
            ));
        }
        let nav_line = Line::from(nav_spans);

        let paragraph = Paragraph::new(vec![checkbox_line, nav_line]);
        frame.render_widget(paragraph, area);
    }

    /// Render sortable column headers
    fn render_header(
        frame: &mut Frame,
        area: Rect,
        state: &FileOpenState,
        theme: &crate::view::theme::Theme,
        hover_target: &Option<crate::app::HoverTarget>,
    ) {
        use crate::app::HoverTarget;

        let width = area.width as usize;

        // Column widths
        let size_col_width = 10;
        let date_col_width = 14;
        let name_col_width = width.saturating_sub(size_col_width + date_col_width + 4);

        let header_style = Style::default()
            .fg(theme.help_key_fg)
            .bg(theme.menu_dropdown_bg)
            .add_modifier(Modifier::BOLD);

        let active_header_style = Style::default()
            .fg(theme.menu_highlight_fg)
            .bg(theme.menu_dropdown_bg)
            .add_modifier(Modifier::BOLD);

        let hover_header_style = Style::default()
            .fg(theme.menu_hover_fg)
            .bg(theme.menu_hover_bg)
            .add_modifier(Modifier::BOLD);

        // Sort indicator
        let sort_arrow = if state.sort_ascending { "" } else { "" };

        let mut spans = Vec::new();

        // Name column
        let name_header = format!(
            " {}{}",
            t!("file_browser.name"),
            if state.sort_mode == SortMode::Name {
                sort_arrow
            } else {
                " "
            }
        );
        let is_name_hovered = matches!(
            hover_target,
            Some(HoverTarget::FileBrowserHeader(SortMode::Name))
        );
        let name_style = if state.sort_mode == SortMode::Name {
            active_header_style
        } else if is_name_hovered {
            hover_header_style
        } else {
            header_style
        };
        let name_display = if name_header.len() < name_col_width {
            format!("{:<width$}", name_header, width = name_col_width)
        } else {
            name_header[..name_col_width].to_string()
        };
        spans.push(Span::styled(name_display, name_style));

        // Size column
        let size_header = format!(
            "{:>width$}",
            format!(
                "{}{}",
                t!("file_browser.size"),
                if state.sort_mode == SortMode::Size {
                    sort_arrow
                } else {
                    " "
                }
            ),
            width = size_col_width
        );
        let is_size_hovered = matches!(
            hover_target,
            Some(HoverTarget::FileBrowserHeader(SortMode::Size))
        );
        let size_style = if state.sort_mode == SortMode::Size {
            active_header_style
        } else if is_size_hovered {
            hover_header_style
        } else {
            header_style
        };
        spans.push(Span::styled(size_header, size_style));

        // Separator
        spans.push(Span::styled("  ", header_style));

        // Modified column
        let modified_header = format!(
            "{:>width$}",
            format!(
                "{}{}",
                t!("file_browser.modified"),
                if state.sort_mode == SortMode::Modified {
                    sort_arrow
                } else {
                    " "
                }
            ),
            width = date_col_width
        );
        let is_modified_hovered = matches!(
            hover_target,
            Some(HoverTarget::FileBrowserHeader(SortMode::Modified))
        );
        let modified_style = if state.sort_mode == SortMode::Modified {
            active_header_style
        } else if is_modified_hovered {
            hover_header_style
        } else {
            header_style
        };
        spans.push(Span::styled(modified_header, modified_style));

        let line = Line::from(spans);
        let paragraph = Paragraph::new(vec![line]);
        frame.render_widget(paragraph, area);
    }

    /// Render the file list with metadata columns
    ///
    /// Returns the number of visible rows
    fn render_file_list(
        frame: &mut Frame,
        area: Rect,
        state: &FileOpenState,
        theme: &crate::view::theme::Theme,
        hover_target: &Option<crate::app::HoverTarget>,
    ) -> usize {
        use crate::app::HoverTarget;

        let visible_rows = area.height as usize;
        let width = area.width as usize;

        // Column widths (matching header)
        let size_col_width = 10;
        let date_col_width = 14;
        let name_col_width = width.saturating_sub(size_col_width + date_col_width + 4);

        let is_files_active = state.active_section == FileOpenSection::Files;

        // Loading state
        if state.loading {
            let loading_line = Line::from(Span::styled(
                t!("file_browser.loading").to_string(),
                Style::default()
                    .fg(theme.help_separator_fg)
                    .bg(theme.popup_bg),
            ));
            let paragraph = Paragraph::new(vec![loading_line]);
            frame.render_widget(paragraph, area);
            return visible_rows;
        }

        // Error state
        if let Some(error) = &state.error {
            let error_line = Line::from(Span::styled(
                t!("file_browser.error", error = error).to_string(),
                Style::default()
                    .fg(theme.diagnostic_error_fg)
                    .bg(theme.popup_bg),
            ));
            let paragraph = Paragraph::new(vec![error_line]);
            frame.render_widget(paragraph, area);
            return visible_rows;
        }

        // Empty state
        if state.entries.is_empty() {
            let empty_line = Line::from(Span::styled(
                format!(" {}", t!("file_browser.empty")),
                Style::default()
                    .fg(theme.help_separator_fg)
                    .bg(theme.popup_bg),
            ));
            let paragraph = Paragraph::new(vec![empty_line]);
            frame.render_widget(paragraph, area);
            return visible_rows;
        }

        let mut lines = Vec::new();
        let visible_entries = state.visible_entries(visible_rows);

        for (view_idx, entry) in visible_entries.iter().enumerate() {
            let actual_idx = state.scroll_offset + view_idx;
            let is_selected = is_files_active && state.selected_index == Some(actual_idx);
            let is_hovered =
                matches!(hover_target, Some(HoverTarget::FileBrowserEntry(i)) if *i == actual_idx);

            // Base style based on selection, hover, and filter match
            let base_style = if is_selected {
                Style::default()
                    .fg(theme.popup_text_fg)
                    .bg(theme.suggestion_selected_bg)
            } else if is_hovered && entry.matches_filter {
                Style::default()
                    .fg(theme.menu_hover_fg)
                    .bg(theme.menu_hover_bg)
            } else if !entry.matches_filter {
                // Non-matching items are dimmed using the separator color
                Style::default()
                    .fg(theme.help_separator_fg)
                    .bg(theme.popup_bg)
                    .add_modifier(Modifier::DIM)
            } else {
                Style::default().fg(theme.popup_text_fg).bg(theme.popup_bg)
            };

            let mut spans = Vec::new();

            // Name column with trailing type indicator (dirs get /, symlinks get @)
            let name_with_indicator = if entry.fs_entry.is_dir() {
                format!("{}/", entry.fs_entry.name)
            } else if entry.fs_entry.is_symlink() {
                format!("{}@", entry.fs_entry.name)
            } else {
                entry.fs_entry.name.clone()
            };
            let name_display = if name_with_indicator.len() < name_col_width {
                format!("{:<width$}", name_with_indicator, width = name_col_width)
            } else {
                // Truncate with ellipsis
                let truncated: String = name_with_indicator
                    .chars()
                    .take(name_col_width - 3)
                    .collect();
                format!("{}...", truncated)
            };

            // Color directories differently
            let name_style = if entry.fs_entry.is_dir() && !is_selected {
                base_style.fg(theme.help_key_fg)
            } else {
                base_style
            };
            spans.push(Span::styled(name_display, name_style));

            // Size column
            let size_display = if entry.fs_entry.is_dir() {
                format!("{:>width$}", "--", width = size_col_width)
            } else {
                let size = entry
                    .fs_entry
                    .metadata
                    .as_ref()
                    .map(|m| format_size(m.size))
                    .unwrap_or_else(|| "--".to_string());
                format!("{:>width$}", size, width = size_col_width)
            };
            spans.push(Span::styled(size_display, base_style));

            // Separator
            spans.push(Span::styled("  ", base_style));

            // Modified column
            let modified_display = entry
                .fs_entry
                .metadata
                .as_ref()
                .and_then(|m| m.modified)
                .map(format_modified)
                .unwrap_or_else(|| "--".to_string());
            let modified_formatted =
                format!("{:>width$}", modified_display, width = date_col_width);
            spans.push(Span::styled(modified_formatted, base_style));

            lines.push(Line::from(spans));
        }

        // Fill remaining rows with empty lines
        while lines.len() < visible_rows {
            lines.push(Line::from(Span::styled(
                " ".repeat(width),
                Style::default().bg(theme.popup_bg),
            )));
        }

        let paragraph = Paragraph::new(lines);
        frame.render_widget(paragraph, area);

        visible_rows
    }
}

/// Layout information for mouse hit testing
#[derive(Debug, Clone)]
pub struct FileBrowserLayout {
    /// Navigation shortcuts area
    pub nav_area: Rect,
    /// Column headers area
    pub header_area: Rect,
    /// File list area
    pub list_area: Rect,
    /// Scrollbar area
    pub scrollbar_area: Rect,
    /// Scrollbar thumb start position
    pub thumb_start: usize,
    /// Scrollbar thumb end position
    pub thumb_end: usize,
    /// Number of visible rows in the file list
    pub visible_rows: usize,
    /// Width of the content area (for checkbox position calculation)
    pub content_width: u16,
}

impl FileBrowserLayout {
    /// Check if a position is within the file list area
    pub fn is_in_list(&self, x: u16, y: u16) -> bool {
        x >= self.list_area.x
            && x < self.list_area.x + self.list_area.width
            && y >= self.list_area.y
            && y < self.list_area.y + self.list_area.height
    }

    /// Convert a click in the list area to an entry index
    pub fn click_to_index(&self, y: u16, scroll_offset: usize) -> Option<usize> {
        if y < self.list_area.y || y >= self.list_area.y + self.list_area.height {
            return None;
        }
        let row = (y - self.list_area.y) as usize;
        Some(scroll_offset + row)
    }

    /// Check if a position is in the navigation area
    pub fn is_in_nav(&self, x: u16, y: u16) -> bool {
        x >= self.nav_area.x
            && x < self.nav_area.x + self.nav_area.width
            && y >= self.nav_area.y
            && y < self.nav_area.y + self.nav_area.height
    }

    /// Determine which navigation shortcut was clicked based on x position
    /// The layout is: " Navigation: " (13 chars) then for each shortcut: " {label} " + " │ " separator
    /// Navigation shortcuts are on the second row (y == nav_area.y + 1)
    pub fn nav_shortcut_at(&self, x: u16, y: u16, shortcut_labels: &[&str]) -> Option<usize> {
        // Navigation shortcuts are on the second row of the nav area
        if y != self.nav_area.y + 1 {
            return None;
        }

        let rel_x = x.saturating_sub(self.nav_area.x) as usize;

        // Skip " Navigation: " prefix
        let prefix_len = 13;
        if rel_x < prefix_len {
            return None;
        }

        let mut current_x = prefix_len;
        for (idx, label) in shortcut_labels.iter().enumerate() {
            // Each shortcut: " {label} " = visual width + 2 spaces
            let shortcut_width = str_width(label) + 2;

            if rel_x >= current_x && rel_x < current_x + shortcut_width {
                return Some(idx);
            }
            current_x += shortcut_width;

            // Separator: " │ " = 3 chars
            if idx < shortcut_labels.len() - 1 {
                current_x += 3;
            }
        }

        None
    }

    /// Check if a position is in the header area (for sorting)
    pub fn is_in_header(&self, x: u16, y: u16) -> bool {
        x >= self.header_area.x
            && x < self.header_area.x + self.header_area.width
            && y >= self.header_area.y
            && y < self.header_area.y + self.header_area.height
    }

    /// Determine which column header was clicked
    pub fn header_column_at(&self, x: u16) -> Option<SortMode> {
        let rel_x = x.saturating_sub(self.header_area.x) as usize;
        let width = self.header_area.width as usize;

        let size_col_width = 10;
        let date_col_width = 14;
        let name_col_width = width.saturating_sub(size_col_width + date_col_width + 4);

        if rel_x < name_col_width {
            Some(SortMode::Name)
        } else if rel_x < name_col_width + size_col_width {
            Some(SortMode::Size)
        } else {
            Some(SortMode::Modified)
        }
    }

    /// Check if a position is in the scrollbar area
    pub fn is_in_scrollbar(&self, x: u16, y: u16) -> bool {
        x >= self.scrollbar_area.x
            && x < self.scrollbar_area.x + self.scrollbar_area.width
            && y >= self.scrollbar_area.y
            && y < self.scrollbar_area.y + self.scrollbar_area.height
    }

    /// Check if a position is in the scrollbar thumb
    pub fn is_in_thumb(&self, y: u16) -> bool {
        let rel_y = y.saturating_sub(self.scrollbar_area.y) as usize;
        rel_y >= self.thumb_start && rel_y < self.thumb_end
    }

    /// Check if a position is on the "Show Hidden" checkbox
    /// The checkbox is on its own row (first row of navigation area)
    /// Format: " ☐ Show Hidden (Alt+.)" (includes keyboard shortcut hint)
    pub fn is_on_show_hidden_checkbox(&self, x: u16, y: u16) -> bool {
        // Must be on the first row of navigation area (checkbox row)
        if y != self.nav_area.y {
            return false;
        }

        // Must be within the x bounds of the navigation area
        if x < self.nav_area.x || x >= self.nav_area.x + self.nav_area.width {
            return false;
        }

        // Checkbox spans the left portion of the row
        // " ☐ Show Hidden (Alt+.)" is approximately 24 characters
        let checkbox_width = 24u16;
        x < self.nav_area.x + checkbox_width
    }
}