pacsea 0.8.2

A fast, friendly TUI for browsing and installing Arch and AUR packages with built-in news and security scanning
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
use ratatui::{
    Frame,
    prelude::Rect,
    style::{Modifier, Style},
    text::{Line, Span},
    widgets::{Block, BorderType, Borders, Clear, Paragraph, Wrap},
};
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};

use crate::i18n;
use crate::state::AppState;
use crate::theme::theme;

/// What: Calculate menu dimensions based on options and available space.
///
/// Inputs:
/// - `opts`: Menu option strings
/// - `results_area`: Available area for positioning
/// - `extra_width`: Additional width needed (e.g., for checkboxes)
///
/// Output:
/// - Tuple of (`width`, `height`, `max_number_width`)
///
/// Details:
/// - Uses Unicode display width for accurate sizing with wide characters.
fn calculate_menu_dimensions(
    opts: &[String],
    results_area: Rect,
    extra_width: u16,
) -> (u16, u16, u16) {
    let widest = opts
        .iter()
        .map(|s| u16::try_from(s.width()).map_or(u16::MAX, |x| x))
        .max()
        .unwrap_or(0);
    let max_num_width = u16::try_from(format!("{}", opts.len()).len()).unwrap_or(u16::MAX);
    let w = widest
        .saturating_add(max_num_width)
        .saturating_add(2) // spacing between text and number
        .saturating_add(extra_width)
        .min(results_area.width.saturating_sub(2));
    let h = u16::try_from(opts.len())
        .unwrap_or(u16::MAX)
        .saturating_add(2); // borders
    (w, h, max_num_width)
}

/// What: Calculate menu rectangle position aligned to a button.
///
/// Inputs:
/// - `button_rect`: Optional button rectangle (x, y, width, height)
/// - `menu_width`: Calculated menu width
/// - `menu_height`: Calculated menu height
/// - `results_area`: Available area for positioning
///
/// Output:
/// - Menu rectangle and inner hit-test rectangle
///
/// Details:
/// - Horizontally aligns with the button's left edge (clamped to `results_area` width).
/// - Vertically opens on the row **below** the button when `button_rect` is `Some`; otherwise falls
///   back to `results_area.y + 1` (legacy title-row placement without a hit rect).
fn calculate_menu_rect(
    button_rect: Option<(u16, u16, u16, u16)>,
    menu_width: u16,
    menu_height: u16,
    results_area: Rect,
) -> (Rect, (u16, u16, u16, u16)) {
    let rect_w = menu_width.saturating_add(2);
    let max_x = results_area.x + results_area.width.saturating_sub(rect_w);
    let button_x = button_rect.map_or(max_x, |(x, _, _, _)| x);
    let menu_x = button_x.min(max_x);
    let menu_y = button_rect.map_or_else(
        || results_area.y.saturating_add(1),
        |(_, by, _, bh)| by.saturating_add(bh),
    );
    let rect = Rect {
        x: menu_x,
        y: menu_y,
        width: rect_w,
        height: menu_height,
    };
    let inner_rect = (
        rect.x + 1,
        rect.y + 1,
        menu_width,
        menu_height.saturating_sub(2),
    );
    (rect, inner_rect)
}

/// What: Build menu lines with right-aligned row numbers.
///
/// Inputs:
/// - `opts`: Menu option strings
/// - `widest`: Width of widest option
/// - `max_num_width`: Maximum width needed for numbers
/// - `total_line_width`: Target display width for each line
/// - `spacing`: Spacing between text and numbers
/// - `th`: Theme colors
///
/// Output:
/// - Vector of styled lines ready for rendering
///
/// Details:
/// - Handles Unicode display width for accurate alignment with wide characters.
fn build_numbered_menu_lines(
    opts: &[String],
    widest: u16,
    max_num_width: u16,
    total_line_width: u16,
    spacing: u16,
    th: crate::theme::Theme,
) -> Vec<Line<'static>> {
    let num_start_col = widest + spacing;
    let mut lines: Vec<Line> = Vec::new();
    for (i, text) in opts.iter().enumerate() {
        let num_str = format!("{}", i + 1);
        let num_width = u16::try_from(num_str.len()).unwrap_or(u16::MAX);
        let num_padding = max_num_width.saturating_sub(num_width);
        let padded_num = format!("{}{}", " ".repeat(num_padding as usize), num_str);

        let text_display_width = u16::try_from(text.width()).unwrap_or(u16::MAX);
        let text_padding = widest.saturating_sub(text_display_width);

        let mut complete_line = format!(
            "{}{}{}{}",
            text,
            " ".repeat(text_padding as usize),
            " ".repeat(spacing as usize),
            padded_num
        );

        let current_width = u16::try_from(complete_line.width()).unwrap_or(u16::MAX);
        if current_width < total_line_width {
            complete_line.push_str(&" ".repeat((total_line_width - current_width) as usize));
        } else if current_width > total_line_width {
            let mut truncated = String::new();
            let mut width_so_far = 0u16;
            for ch in complete_line.chars() {
                let ch_width = u16::try_from(ch.width().unwrap_or(0)).unwrap_or(u16::MAX);
                if width_so_far + ch_width > total_line_width {
                    break;
                }
                truncated.push(ch);
                width_so_far += ch_width;
            }
            complete_line = truncated;
        }

        let mut text_part = String::new();
        let mut width_so_far = 0u16;
        for ch in complete_line.chars() {
            let ch_width = u16::try_from(ch.width().unwrap_or(0)).unwrap_or(u16::MAX);
            if width_so_far + ch_width > num_start_col {
                break;
            }
            text_part.push(ch);
            width_so_far += ch_width;
        }
        let num_part = complete_line
            .chars()
            .skip(text_part.chars().count())
            .collect::<String>();

        lines.push(Line::from(vec![
            Span::styled(text_part, Style::default().fg(th.text)),
            Span::styled(num_part, Style::default().fg(th.overlay1)),
        ]));
    }
    lines
}

/// What: Build menu lines with checkbox indicators.
///
/// Inputs:
/// - `opts`: Menu options with enabled state
/// - `menu_width`: Target width for each line
/// - `th`: Theme colors
///
/// Output:
/// - Vector of styled lines ready for rendering
///
/// Details:
/// - Padding uses [`UnicodeWidthStr::width`] so row width matches terminal columns for non-ASCII labels.
fn build_checkbox_menu_lines(
    opts: &[(String, bool)],
    menu_width: u16,
    th: crate::theme::Theme,
) -> Vec<Line<'static>> {
    let mut lines: Vec<Line> = Vec::new();
    for (text, enabled) in opts {
        let indicator = if *enabled { "✓ " } else { "  " };
        let text_w = u16::try_from(text.width()).unwrap_or(u16::MAX);
        let indicator_w = u16::try_from(indicator.width()).unwrap_or(u16::MAX);
        let pad = menu_width
            .saturating_sub(text_w)
            .saturating_sub(indicator_w);
        let padding = " ".repeat(pad as usize);
        lines.push(Line::from(vec![
            Span::styled(
                indicator.to_string(),
                Style::default().fg(if *enabled { th.green } else { th.overlay1 }),
            ),
            Span::styled(text.clone(), Style::default().fg(th.text)),
            Span::raw(padding),
        ]));
    }
    lines
}

/// What: Create a styled menu block with title.
///
/// Inputs:
/// - `lines`: Menu lines to display
/// - `title_first_letter_key`: i18n key for first letter of title
/// - `title_suffix_key`: i18n key for suffix of title
/// - `app`: Application state for i18n
/// - `th`: Theme colors
///
/// Output:
/// - Styled Paragraph widget ready for rendering
fn create_menu_block(
    lines: Vec<Line<'static>>,
    title_first_letter_key: &str,
    title_suffix_key: &str,
    app: &AppState,
    th: crate::theme::Theme,
) -> Paragraph<'static> {
    let first_letter = i18n::t(app, title_first_letter_key);
    let suffix = i18n::t(app, title_suffix_key);
    Paragraph::new(lines)
        .style(Style::default().fg(th.text).bg(th.base))
        .wrap(Wrap { trim: false })
        .block(
            Block::default()
                .style(Style::default().bg(th.base))
                .title(Line::from(vec![
                    Span::styled(" ", Style::default().fg(th.overlay1)),
                    Span::styled(
                        first_letter,
                        Style::default()
                            .fg(th.overlay1)
                            .add_modifier(Modifier::UNDERLINED),
                    ),
                    Span::styled(suffix, Style::default().fg(th.overlay1)),
                ]))
                .borders(Borders::ALL)
                .border_type(BorderType::Rounded)
                .border_style(Style::default().fg(th.mauve)),
        )
}

/// What: Render Config/Lists dropdown menu.
///
/// Inputs:
/// - `f`: Frame to render into
/// - `app`: Mutable application state
/// - `results_area`: Available area for positioning
/// - `th`: Theme colors
///
/// Output:
/// - Updates `app.config_menu_rect` if menu is rendered
fn render_config_menu(
    f: &mut Frame,
    app: &mut AppState,
    results_area: Rect,
    th: crate::theme::Theme,
) {
    app.config_menu_rect = None;
    if !app.config_menu_open {
        return;
    }

    let opts: Vec<String> = vec![
        i18n::t(app, "app.results.config_menu.options.settings"),
        i18n::t(app, "app.results.config_menu.options.theme"),
        i18n::t(app, "app.results.config_menu.options.keybindings"),
        i18n::t(app, "app.results.config_menu.options.repos"),
    ];

    let widest = opts
        .iter()
        .map(|s| u16::try_from(s.width()).map_or(u16::MAX, |x| x))
        .max()
        .unwrap_or(0);
    let (w, h, max_num_width) = calculate_menu_dimensions(&opts, results_area, 0);
    let (rect, inner_rect) = calculate_menu_rect(app.config_button_rect, w, h, results_area);
    app.config_menu_rect = Some(inner_rect);

    let spacing = 2u16;
    let lines = build_numbered_menu_lines(&opts, widest, max_num_width, w, spacing, th);
    let menu = create_menu_block(
        lines,
        "app.results.menus.config_lists.first_letter",
        "app.results.menus.config_lists.suffix",
        app,
        th,
    );
    f.render_widget(Clear, rect);
    f.render_widget(menu, rect);
}

/// What: Render Panels dropdown menu.
///
/// Inputs:
/// - `f`: Frame to render into
/// - `app`: Mutable application state
/// - `results_area`: Available area for positioning
/// - `th`: Theme colors
///
/// Output:
/// - Updates `app.panels_menu_rect` if menu is rendered
fn render_panels_menu(
    f: &mut Frame,
    app: &mut AppState,
    results_area: Rect,
    th: crate::theme::Theme,
) {
    app.panels_menu_rect = None;
    if !app.panels_menu_open {
        return;
    }

    let news_mode = matches!(app.app_mode, crate::state::types::AppMode::News);
    let opts: Vec<String> = if news_mode {
        let label_history = if app.show_news_history_pane {
            i18n::t(app, "app.results.panels_menu.hide_history")
        } else {
            i18n::t(app, "app.results.panels_menu.show_history")
        };
        let label_bookmarks = if app.show_news_bookmarks_pane {
            i18n::t(app, "app.results.panels_menu.hide_bookmarks")
        } else {
            i18n::t(app, "app.results.panels_menu.show_bookmarks")
        };
        let label_keybinds = if app.show_keybinds_footer {
            i18n::t(app, "app.results.panels_menu.hide_keybinds")
        } else {
            i18n::t(app, "app.results.panels_menu.show_keybinds")
        };
        vec![label_history, label_bookmarks, label_keybinds]
    } else {
        let label_recent = if app.show_recent_pane {
            i18n::t(app, "app.results.panels_menu.hide_recent")
        } else {
            i18n::t(app, "app.results.panels_menu.show_recent")
        };
        let label_install = if app.show_install_pane {
            i18n::t(app, "app.results.panels_menu.hide_install_list")
        } else {
            i18n::t(app, "app.results.panels_menu.show_install_list")
        };
        let label_keybinds = if app.show_keybinds_footer {
            i18n::t(app, "app.results.panels_menu.hide_keybinds")
        } else {
            i18n::t(app, "app.results.panels_menu.show_keybinds")
        };
        vec![label_recent, label_install, label_keybinds]
    };

    let widest = opts
        .iter()
        .map(|s| u16::try_from(s.width()).map_or(u16::MAX, |x| x))
        .max()
        .unwrap_or(0);
    let (w, h, max_num_width) = calculate_menu_dimensions(&opts, results_area, 0);
    let (rect, inner_rect) = calculate_menu_rect(app.panels_button_rect, w, h, results_area);
    app.panels_menu_rect = Some(inner_rect);

    let spacing = 2u16;
    let lines = build_numbered_menu_lines(&opts, widest, max_num_width, w, spacing, th);
    let menu = create_menu_block(
        lines,
        "app.results.menus.panels.first_letter",
        "app.results.menus.panels.suffix",
        app,
        th,
    );
    f.render_widget(Clear, rect);
    f.render_widget(menu, rect);
}

/// What: Render Options dropdown menu.
///
/// Inputs:
/// - `f`: Frame to render into
/// - `app`: Mutable application state
/// - `results_area`: Available area for positioning
/// - `th`: Theme colors
///
/// Output:
/// - Updates `app.options_menu_rect` if menu is rendered
fn render_options_menu(
    f: &mut Frame,
    app: &mut AppState,
    results_area: Rect,
    th: crate::theme::Theme,
) {
    app.options_menu_rect = None;
    if !app.options_menu_open {
        return;
    }

    let news_mode = matches!(app.app_mode, crate::state::types::AppMode::News);
    let mode_toggle_label = if news_mode {
        i18n::t(app, "app.results.options_menu.package_mode")
    } else {
        i18n::t(app, "app.results.options_menu.news_management")
    };
    let mut opts: Vec<String> = Vec::new();
    if !news_mode {
        let label_toggle = if app.installed_only_mode {
            i18n::t(app, "app.results.options_menu.list_all_packages")
        } else {
            i18n::t(app, "app.results.options_menu.list_installed_packages")
        };
        opts.push(label_toggle);
    }
    opts.push(i18n::t(app, "app.results.options_menu.update_system"));
    opts.push(i18n::t(app, "app.results.options_menu.tui_optional_deps"));
    opts.push(i18n::t(app, "app.results.options_menu.repositories"));
    opts.push(mode_toggle_label);
    let widest = opts
        .iter()
        .map(|s| u16::try_from(s.width()).map_or(u16::MAX, |x| x))
        .max()
        .unwrap_or(0);
    let (w, h, max_num_width) = calculate_menu_dimensions(&opts, results_area, 0);
    let (rect, inner_rect) = calculate_menu_rect(app.options_button_rect, w, h, results_area);
    app.options_menu_rect = Some(inner_rect);

    let spacing = 2u16;
    let lines = build_numbered_menu_lines(&opts, widest, max_num_width, w, spacing, th);
    let menu = create_menu_block(
        lines,
        "app.results.menus.options.first_letter",
        "app.results.menus.options.suffix",
        app,
        th,
    );
    f.render_widget(Clear, rect);
    f.render_widget(menu, rect);
}

/// What: Render Artix filter dropdown menu.
///
/// Inputs:
/// - `f`: Frame to render into
/// - `app`: Mutable application state
/// - `results_area`: Available area for positioning
/// - `th`: Theme colors
///
/// Output:
/// - Updates `app.artix_filter_menu_rect` if menu is rendered
fn render_artix_filter_menu(
    f: &mut Frame,
    app: &mut AppState,
    results_area: Rect,
    th: crate::theme::Theme,
) {
    app.artix_filter_menu_rect = None;
    if !app.artix_filter_menu_open {
        return;
    }

    let has_hidden_filters = app.results_filter_artix_omniverse_rect.is_none()
        && app.results_filter_artix_universe_rect.is_none()
        && app.results_filter_artix_lib32_rect.is_none()
        && app.results_filter_artix_galaxy_rect.is_none()
        && app.results_filter_artix_world_rect.is_none()
        && app.results_filter_artix_system_rect.is_none();

    if !has_hidden_filters {
        return;
    }

    let all_on = app.results_filter_show_artix_omniverse
        && app.results_filter_show_artix_universe
        && app.results_filter_show_artix_lib32
        && app.results_filter_show_artix_galaxy
        && app.results_filter_show_artix_world
        && app.results_filter_show_artix_system;

    let opts: Vec<(String, bool)> = vec![
        (i18n::t(app, "app.results.filters.artix"), all_on),
        (
            i18n::t(app, "app.results.filters.artix_omniverse"),
            app.results_filter_show_artix_omniverse,
        ),
        (
            i18n::t(app, "app.results.filters.artix_universe"),
            app.results_filter_show_artix_universe,
        ),
        (
            i18n::t(app, "app.results.filters.artix_lib32"),
            app.results_filter_show_artix_lib32,
        ),
        (
            i18n::t(app, "app.results.filters.artix_galaxy"),
            app.results_filter_show_artix_galaxy,
        ),
        (
            i18n::t(app, "app.results.filters.artix_world"),
            app.results_filter_show_artix_world,
        ),
        (
            i18n::t(app, "app.results.filters.artix_system"),
            app.results_filter_show_artix_system,
        ),
    ];

    let widest = opts
        .iter()
        .map(|(s, _)| u16::try_from(s.width()).map_or(u16::MAX, |x| x))
        .max()
        .unwrap_or(0);
    let w = widest
        .saturating_add(4) // space for checkbox indicator
        .saturating_add(2)
        .min(results_area.width.saturating_sub(2));
    let h = u16::try_from(opts.len())
        .unwrap_or(u16::MAX)
        .saturating_add(2);
    let (rect, inner_rect) = calculate_menu_rect(app.results_filter_artix_rect, w, h, results_area);
    app.artix_filter_menu_rect = Some(inner_rect);

    let lines = build_checkbox_menu_lines(&opts, w, th);
    let menu = Paragraph::new(lines)
        .style(Style::default().fg(th.text).bg(th.base))
        .wrap(Wrap { trim: true })
        .block(
            Block::default()
                .style(Style::default().bg(th.base))
                .title(Line::from(vec![Span::styled(
                    "Artix Filters",
                    Style::default().fg(th.overlay1),
                )]))
                .borders(Borders::ALL)
                .border_type(BorderType::Rounded)
                .border_style(Style::default().fg(th.mauve)),
        );
    f.render_widget(Clear, rect);
    f.render_widget(menu, rect);
}

/// What: Render collapsed menu dropdown (contains Config/Lists, Panels, Options).
///
/// Inputs:
/// - `f`: Frame to render into
/// - `app`: Mutable application state
/// - `results_area`: Available area for positioning
/// - `th`: Theme colors
///
/// Output:
/// - Updates `app.collapsed_menu_rect` if menu is rendered
///
/// Details:
/// - Menu is right-aligned to the collapsed **Menu** button on the updates row (`collapsed_menu_button_rect`).
/// - Falls back to the old results-title geometry only if the button rect is missing.
fn render_collapsed_menu(
    f: &mut Frame,
    app: &mut AppState,
    results_area: Rect,
    th: crate::theme::Theme,
) {
    app.collapsed_menu_rect = None;
    if !app.collapsed_menu_open {
        return;
    }

    let opts: Vec<String> = vec![
        i18n::t(app, "app.results.collapsed_menu.options.config_lists"),
        i18n::t(app, "app.results.collapsed_menu.options.panels"),
        i18n::t(app, "app.results.collapsed_menu.options.options"),
    ];

    let widest = opts
        .iter()
        .map(|s| u16::try_from(s.width()).map_or(u16::MAX, |x| x))
        .max()
        .unwrap_or(0);
    let (w, h, max_num_width) = calculate_menu_dimensions(&opts, results_area, 0);

    let rect_w = w.saturating_add(2);
    let min_x = results_area.x.saturating_add(1);
    let max_x = results_area
        .x
        .saturating_add(results_area.width)
        .saturating_sub(rect_w);

    let (menu_x, menu_y) = if let Some((bx, by, bw, bh)) = app.collapsed_menu_button_rect {
        let btn_right = bx.saturating_add(bw);
        let mx = btn_right.saturating_sub(rect_w).max(min_x).min(max_x);
        let my = by.saturating_add(bh);
        (mx, my)
    } else {
        let options_button_label = format!("{} v", i18n::t(app, "app.results.buttons.options"));
        let options_w = u16::try_from(options_button_label.width()).unwrap_or(u16::MAX);
        let inner_width = results_area.width.saturating_sub(2);
        let opt_x = results_area
            .x
            .saturating_add(1)
            .saturating_add(inner_width.saturating_sub(options_w));
        let opt_right = opt_x.saturating_add(options_w);
        let mx = opt_right.saturating_sub(rect_w).max(min_x).min(max_x);
        let my = results_area.y.saturating_add(1);
        (mx, my)
    };

    let rect = Rect {
        x: menu_x,
        y: menu_y,
        width: rect_w,
        height: h,
    };
    let inner_rect = (rect.x + 1, rect.y + 1, w, h.saturating_sub(2));
    app.collapsed_menu_rect = Some(inner_rect);

    let spacing = 2u16;
    let lines = build_numbered_menu_lines(&opts, widest, max_num_width, w, spacing, th);
    let menu = create_menu_block(
        lines,
        "app.results.menus.menu.first_letter",
        "app.results.menus.menu.suffix",
        app,
        th,
    );
    f.render_widget(Clear, rect);
    f.render_widget(menu, rect);
}

/// What: Render dropdown menus (Config/Lists, Panels, Options) on the overlay layer.
///
/// Inputs:
/// - `f`: Frame to render into
/// - `app`: Mutable application state (tracks menu open flags and rects)
/// - `results_area`: Rect of the results pane (full terminal width; used for horizontal clamp and sizing)
///
/// Output:
/// - Draws any open dropdowns and records their inner rectangles for hit-testing.
///
/// Details:
/// - Vertical placement for button-backed menus uses each button rect’s `y` (e.g. top-bar triggers),
///   not the top edge of the results band.
/// - Clamps width to the results band, clears background, and numbers rows for keyboard shortcuts
///   while ensuring menus render above other content.
pub fn render_dropdowns(f: &mut Frame, app: &mut AppState, results_area: Rect) {
    let th = theme();
    render_config_menu(f, app, results_area, th);
    render_panels_menu(f, app, results_area, th);
    render_options_menu(f, app, results_area, th);
    render_artix_filter_menu(f, app, results_area, th);
    render_custom_repos_filter_menu(f, app, results_area, th);
    render_collapsed_menu(f, app, results_area, th);
}

/// What: Render checkbox dropdown for `repos.conf` dynamic `results_filter` ids.
///
/// Inputs:
/// - `f`: Frame to render into.
/// - `app`: Application state (menu open flag, dynamic map, button rect).
/// - `results_area`: Results pane area for clamping.
/// - `th`: Theme colors.
///
/// Output:
/// - Updates `custom_repos_filter_menu_rect` when the menu is visible.
///
/// Details:
/// - Row 0 toggles every dynamic id; following rows toggle one canonical id each. Uses the same
///   overflow positioning helper as the Artix filter menu (`calculate_menu_rect`).
fn render_custom_repos_filter_menu(
    f: &mut Frame,
    app: &mut AppState,
    results_area: Rect,
    th: crate::theme::Theme,
) {
    app.custom_repos_filter_menu_rect = None;
    if !app.custom_repos_filter_menu_open || app.results_filter_dynamic.is_empty() {
        return;
    }

    let mut keys: Vec<String> = app.results_filter_dynamic.keys().cloned().collect();
    keys.sort();
    let all_label = i18n::t(app, "app.results.filters.custom_repos_all");
    let all_on = app.results_filter_dynamic.values().all(|v| *v);
    let mut opts: Vec<(String, bool)> = vec![(all_label, all_on)];
    for k in &keys {
        let on = app.results_filter_dynamic.get(k).copied().unwrap_or(true);
        opts.push((k.clone(), on));
    }

    let widest = opts
        .iter()
        .map(|(s, _)| u16::try_from(s.width()).map_or(u16::MAX, |x| x))
        .max()
        .unwrap_or(0);
    let w = widest
        .saturating_add(4)
        .saturating_add(2)
        .min(results_area.width.saturating_sub(2));
    let h = u16::try_from(opts.len())
        .unwrap_or(u16::MAX)
        .saturating_add(2);
    let (rect, inner_rect) =
        calculate_menu_rect(app.results_filter_custom_repos_rect, w, h, results_area);
    app.custom_repos_filter_menu_rect = Some(inner_rect);

    let lines = build_checkbox_menu_lines(&opts, w, th);
    let menu = Paragraph::new(lines)
        .style(Style::default().fg(th.text).bg(th.base))
        .wrap(Wrap { trim: true })
        .block(
            Block::default()
                .style(Style::default().bg(th.base))
                .title(Line::from(vec![Span::styled(
                    i18n::t(app, "app.results.filters.custom_repos_menu_title"),
                    Style::default().fg(th.overlay1),
                )]))
                .borders(Borders::ALL)
                .border_type(BorderType::Rounded)
                .border_style(Style::default().fg(th.mauve)),
        );
    f.render_widget(Clear, rect);
    f.render_widget(menu, rect);
}

#[cfg(test)]
mod calculate_menu_rect_tests {
    use ratatui::layout::Rect;

    use super::calculate_menu_rect;

    /// What: Verify dropdown Y uses the trigger button, not the results pane top.
    ///
    /// Inputs:
    /// - A results `Rect` placed low on the screen (non-default pane order) and a button on row 0.
    ///
    /// Output:
    /// - Menu `y` is directly under the button.
    ///
    /// Details:
    /// - Regression guard for top-bar Config/Panels/Options when `Results` is not the upper band.
    #[test]
    fn menu_opens_below_button_when_results_band_is_lower_on_screen() {
        let results_area = Rect::new(0, 18, 100, 12);
        let button = Some((70u16, 0u16, 8u16, 1u16));
        let (rect, _) = calculate_menu_rect(button, 24, 7, results_area);
        assert_eq!(
            rect.y, 1,
            "expected menu directly under top bar, not results_area.y + 1"
        );
    }

    /// What: Verify fallback vertical placement when no button rect exists.
    ///
    /// Inputs:
    /// - `button_rect: None` and a sample `results_area`.
    ///
    /// Output:
    /// - Menu `y` is `results_area.y + 1`.
    ///
    /// Details:
    /// - Preserves legacy behavior for callers without hit geometry.
    #[test]
    fn menu_y_fallback_without_button_rect() {
        let results_area = Rect::new(0, 5, 80, 10);
        let (rect, _) = calculate_menu_rect(None, 20, 5, results_area);
        assert_eq!(rect.y, 6);
    }
}