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
//! Filter toggle mouse event handling.

use crate::state::AppState;

/// Check if a point is within a rectangle.
///
/// What: Determines if mouse coordinates fall within the bounds of a rectangle.
///
/// Inputs:
/// - `mx`: Mouse X coordinate (column)
/// - `my`: Mouse Y coordinate (row)
/// - `rect`: Optional rectangle (x, y, width, height)
///
/// Output:
/// - `true` if the point is within the rectangle, `false` otherwise.
///
/// Details:
/// - Returns `false` if `rect` is `None`.
/// - Uses inclusive start and exclusive end bounds for width and height.
const fn is_point_in_rect(mx: u16, my: u16, rect: Option<(u16, u16, u16, u16)>) -> bool {
    if let Some((x, y, w, h)) = rect {
        mx >= x && mx < x + w && my >= y && my < y + h
    } else {
        false
    }
}

/// Toggle a simple boolean filter if the mouse click is within its rectangle.
///
/// What: Checks if mouse coordinates are within a filter's rectangle and toggles the filter if so.
///
/// Inputs:
/// - `mx`: Mouse X coordinate (column)
/// - `my`: Mouse Y coordinate (row)
/// - `rect`: Optional rectangle for the filter toggle area
/// - `toggle_fn`: Closure that toggles the filter state
/// - `app`: Mutable application state for applying filters and sort
///
/// Output:
/// - `true` if the filter was toggled, `false` otherwise.
///
/// Details:
/// - If the click is within the rectangle, calls the toggle function and applies filters/sort.
fn try_toggle_simple_filter<F>(
    mx: u16,
    my: u16,
    rect: Option<(u16, u16, u16, u16)>,
    toggle_fn: F,
    app: &mut AppState,
) -> bool
where
    F: FnOnce(&mut AppState),
{
    if is_point_in_rect(mx, my, rect) {
        toggle_fn(app);
        crate::logic::apply_filters_and_sort_preserve_selection(app);
        true
    } else {
        false
    }
}

/// Toggle all Artix repository filters together.
///
/// What: Sets all individual Artix repository filters to the same state (all on or all off).
///
/// Inputs:
/// - `app`: Mutable application state containing Artix filter states
///
/// Output: None (modifies state in place)
///
/// Details:
/// - Checks if all individual Artix filters are currently on.
/// - If all are on, turns all off; otherwise turns all on.
/// - Updates the main Artix filter state to match.
/// - Applies filters and sort after toggling.
fn toggle_all_artix_filters(app: &mut AppState) {
    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 new_state = !all_on;
    app.results_filter_show_artix_omniverse = new_state;
    app.results_filter_show_artix_universe = new_state;
    app.results_filter_show_artix_lib32 = new_state;
    app.results_filter_show_artix_galaxy = new_state;
    app.results_filter_show_artix_world = new_state;
    app.results_filter_show_artix_system = new_state;
    app.results_filter_show_artix = new_state;
    crate::logic::apply_filters_and_sort_preserve_selection(app);
}

/// Check if Artix-specific filters are hidden (dropdown mode).
///
/// What: Determines if individual Artix repository filter rectangles are all hidden.
///
/// Inputs:
/// - `app`: Application state containing Artix filter rectangles
///
/// Output:
/// - `true` if all individual Artix filter rectangles are `None`, `false` otherwise.
///
/// Details:
/// - Returns `true` when in dropdown mode (all individual filters hidden).
const fn has_hidden_artix_filters(app: &AppState) -> bool {
    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()
}

/// Handle mouse click on the main Artix filter toggle.
///
/// What: Processes clicks on the main Artix filter button with special handling for dropdown mode.
///
/// Inputs:
/// - `mx`: Mouse X coordinate (column)
/// - `my`: Mouse Y coordinate (row)
/// - `app`: Mutable application state
///
/// Output:
/// - `true` if the click was handled, `false` otherwise.
///
/// Details:
/// - In dropdown mode (hidden filters), toggles the dropdown menu.
/// - Otherwise, toggles all Artix filters together.
fn handle_artix_main_filter_click(mx: u16, my: u16, app: &mut AppState) -> bool {
    if !is_point_in_rect(mx, my, app.results_filter_artix_rect) {
        return false;
    }

    app.custom_repos_filter_menu_open = false;
    if has_hidden_artix_filters(app) {
        app.artix_filter_menu_open = !app.artix_filter_menu_open;
    } else {
        toggle_all_artix_filters(app);
    }
    true
}

/// What: Toggle the custom `repos.conf` filter dropdown from the title chip.
///
/// Inputs:
/// - `mx` / `my`: Mouse coordinates.
/// - `app`: Application state.
///
/// Output:
/// - `true` when the chip absorbed the click.
///
/// Details:
/// - Closes the Artix filter menu so only one overflow menu is open at a time.
fn handle_custom_repos_chip_click(mx: u16, my: u16, app: &mut AppState) -> bool {
    if app.results_filter_dynamic.is_empty() {
        return false;
    }
    if !is_point_in_rect(mx, my, app.results_filter_custom_repos_rect) {
        return false;
    }
    app.artix_filter_menu_open = false;
    app.custom_repos_filter_menu_open = !app.custom_repos_filter_menu_open;
    true
}

/// What: Handle checkbox rows inside the custom dynamic filter dropdown.
///
/// Inputs:
/// - `mx` / `my`: Mouse coordinates.
/// - `app`: Application state.
///
/// Output:
/// - `true` when a row toggle was applied.
///
/// Details:
/// - Row 0 flips every dynamic id; other rows map to sorted canonical ids.
fn handle_custom_repos_dropdown_click(mx: u16, my: u16, app: &mut AppState) -> bool {
    if !app.custom_repos_filter_menu_open {
        return false;
    }
    let Some((x, y, w, h)) = app.custom_repos_filter_menu_rect else {
        return false;
    };
    if !is_point_in_rect(mx, my, Some((x, y, w, h))) {
        return false;
    }
    let row = my.saturating_sub(y) as usize;
    let mut keys: Vec<String> = app.results_filter_dynamic.keys().cloned().collect();
    keys.sort();
    if row == 0 {
        let all_on = app.results_filter_dynamic.values().all(|v| *v);
        crate::logic::repos::persist_dynamic_filters_set_all(app, !all_on);
        return true;
    }
    let idx = row.saturating_sub(1);
    let Some(id) = keys.get(idx) else {
        return false;
    };
    let cur = app.results_filter_dynamic.get(id).copied().unwrap_or(true);
    crate::logic::repos::persist_dynamic_filter_toggle_and_refresh(app, id, !cur);
    true
}

/// Update the main Artix filter state based on individual filter states.
///
/// What: Sets the main Artix filter to true if any individual Artix filter is enabled.
///
/// Inputs:
/// - `app`: Mutable application state containing Artix filter states
///
/// Output: None (modifies state in place)
///
/// Details:
/// - The main Artix filter is enabled if at least one individual Artix filter is enabled.
#[allow(clippy::missing_const_for_fn)]
fn update_main_artix_filter_state(app: &mut AppState) {
    app.results_filter_show_artix = 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;
}

/// Handle mouse clicks inside the Artix filter dropdown menu.
///
/// What: Processes clicks on menu items to toggle individual Artix filters or all at once.
///
/// Inputs:
/// - `mx`: Mouse X coordinate (column)
/// - `my`: Mouse Y coordinate (row)
/// - `app`: Mutable application state
///
/// Output:
/// - `true` if the click was handled, `false` otherwise.
///
/// Details:
/// - Row 0 toggles all Artix filters together.
/// - Rows 1-6 toggle individual Artix repository filters.
/// - Updates the main Artix filter state after any toggle.
fn handle_artix_dropdown_click(mx: u16, my: u16, app: &mut AppState) -> bool {
    if !app.artix_filter_menu_open {
        return false;
    }

    let Some((x, y, w, h)) = app.artix_filter_menu_rect else {
        return false;
    };

    if !is_point_in_rect(mx, my, Some((x, y, w, h))) {
        return false;
    }

    let row = my.saturating_sub(y) as usize;
    match row {
        0 => toggle_all_artix_filters(app),
        1 => {
            app.results_filter_show_artix_omniverse = !app.results_filter_show_artix_omniverse;
            crate::logic::apply_filters_and_sort_preserve_selection(app);
        }
        2 => {
            app.results_filter_show_artix_universe = !app.results_filter_show_artix_universe;
            crate::logic::apply_filters_and_sort_preserve_selection(app);
        }
        3 => {
            app.results_filter_show_artix_lib32 = !app.results_filter_show_artix_lib32;
            crate::logic::apply_filters_and_sort_preserve_selection(app);
        }
        4 => {
            app.results_filter_show_artix_galaxy = !app.results_filter_show_artix_galaxy;
            crate::logic::apply_filters_and_sort_preserve_selection(app);
        }
        5 => {
            app.results_filter_show_artix_world = !app.results_filter_show_artix_world;
            crate::logic::apply_filters_and_sort_preserve_selection(app);
        }
        6 => {
            app.results_filter_show_artix_system = !app.results_filter_show_artix_system;
            crate::logic::apply_filters_and_sort_preserve_selection(app);
        }
        _ => return false,
    }

    update_main_artix_filter_state(app);
    true
}

/// Handle mouse events for filter toggles.
///
/// What: Process mouse clicks on filter toggle labels in the Results title bar to enable/disable
/// repository filters (`AUR`, `Core`, `Extra`, `Multilib`, `EOS`, `CachyOS`, `Artix`, `Manjaro`).
///
/// Inputs:
/// - `mx`: Mouse X coordinate (column)
/// - `my`: Mouse Y coordinate (row)
/// - `app`: Mutable application state containing filter state and UI rectangles
///
/// Output:
/// - `Some(bool)` if the event was handled (consumed by a filter toggle), `None` if not handled.
///   The boolean value indicates whether the application should exit (always `false` here).
///
/// Details:
/// - Individual filters: Clicking a filter label toggles that filter and applies filters/sort.
/// - Artix main filter: When individual Artix repo filters are visible, toggles all Artix filters
///   together (all on -> all off, otherwise all on). When hidden (dropdown mode), toggles the dropdown menu.
/// - Artix dropdown menu: Handles clicks on menu items to toggle individual Artix repo filters or all at once.
///   Updates the main Artix filter state based on individual filter states.
pub(super) fn handle_filters_mouse(mx: u16, my: u16, app: &mut AppState) -> Option<bool> {
    if matches!(app.app_mode, crate::state::types::AppMode::News) {
        let mut handled = false;
        if is_point_in_rect(mx, my, app.news_filter_arch_rect) {
            app.news_filter_show_arch_news = !app.news_filter_show_arch_news;
            handled = true;
        } else if is_point_in_rect(mx, my, app.news_filter_advisory_rect) {
            match (
                app.news_filter_show_advisories,
                app.news_filter_installed_only,
            ) {
                (true, false) => {
                    app.news_filter_show_advisories = true;
                    app.news_filter_installed_only = true;
                }
                (true, true) => {
                    app.news_filter_show_advisories = false;
                    app.news_filter_installed_only = false;
                }
                (false, _) => {
                    app.news_filter_show_advisories = true;
                    app.news_filter_installed_only = false;
                }
            }
            handled = true;
        } else if is_point_in_rect(mx, my, app.news_filter_updates_rect) {
            app.news_filter_show_pkg_updates = !app.news_filter_show_pkg_updates;
            handled = true;
        } else if is_point_in_rect(mx, my, app.news_filter_aur_updates_rect) {
            app.news_filter_show_aur_updates = !app.news_filter_show_aur_updates;
            handled = true;
        } else if is_point_in_rect(mx, my, app.news_filter_aur_comments_rect) {
            app.news_filter_show_aur_comments = !app.news_filter_show_aur_comments;
            handled = true;
        } else if is_point_in_rect(mx, my, app.news_filter_read_rect) {
            app.news_filter_read_status = match app.news_filter_read_status {
                crate::state::types::NewsReadFilter::All => {
                    crate::state::types::NewsReadFilter::Unread
                }
                crate::state::types::NewsReadFilter::Unread => {
                    crate::state::types::NewsReadFilter::Read
                }
                crate::state::types::NewsReadFilter::Read => {
                    crate::state::types::NewsReadFilter::All
                }
            };
            handled = true;
        }
        if handled {
            crate::theme::save_news_filter_show_arch_news(app.news_filter_show_arch_news);
            crate::theme::save_news_filter_show_advisories(app.news_filter_show_advisories);
            crate::theme::save_news_filter_show_pkg_updates(app.news_filter_show_pkg_updates);
            crate::theme::save_news_filter_show_aur_updates(app.news_filter_show_aur_updates);
            crate::theme::save_news_filter_show_aur_comments(app.news_filter_show_aur_comments);
            crate::theme::save_news_filter_installed_only(app.news_filter_installed_only);
            app.refresh_news_results();
            return Some(false);
        }
        return None;
    }

    // Custom repos.conf filter dropdown (same layer as Artix overflow)
    if handle_custom_repos_dropdown_click(mx, my, app) {
        return Some(false);
    }

    // Handle Artix dropdown menu first (higher priority)
    if handle_artix_dropdown_click(mx, my, app) {
        return Some(false);
    }

    if handle_custom_repos_chip_click(mx, my, app) {
        return Some(false);
    }

    // Handle main Artix filter
    if handle_artix_main_filter_click(mx, my, app) {
        return Some(false);
    }

    // Handle simple filters
    if handle_simple_filter_toggles(mx, my, app) {
        return Some(false);
    }
    None
}

/// What: Try toggling all simple filters in sequence.
///
/// Inputs:
/// - `mx`: Mouse X coordinate (column)
/// - `my`: Mouse Y coordinate (row)
/// - `app`: Mutable application state
///
/// Output:
/// - `true` if any filter was toggled, `false` otherwise.
///
/// Details:
/// - Tries each filter in order and returns immediately if one is toggled.
fn handle_simple_filter_toggles(mx: u16, my: u16, app: &mut AppState) -> bool {
    try_toggle_simple_filter(
        mx,
        my,
        app.results_filter_aur_rect,
        |a| a.results_filter_show_aur = !a.results_filter_show_aur,
        app,
    ) || try_toggle_simple_filter(
        mx,
        my,
        app.results_filter_core_rect,
        |a| a.results_filter_show_core = !a.results_filter_show_core,
        app,
    ) || try_toggle_simple_filter(
        mx,
        my,
        app.results_filter_extra_rect,
        |a| a.results_filter_show_extra = !a.results_filter_show_extra,
        app,
    ) || try_toggle_simple_filter(
        mx,
        my,
        app.results_filter_multilib_rect,
        |a| a.results_filter_show_multilib = !a.results_filter_show_multilib,
        app,
    ) || try_toggle_simple_filter(
        mx,
        my,
        app.results_filter_eos_rect,
        |a| a.results_filter_show_eos = !a.results_filter_show_eos,
        app,
    ) || try_toggle_simple_filter(
        mx,
        my,
        app.results_filter_cachyos_rect,
        |a| a.results_filter_show_cachyos = !a.results_filter_show_cachyos,
        app,
    ) || try_toggle_simple_filter(
        mx,
        my,
        app.results_filter_artix_omniverse_rect,
        |a| a.results_filter_show_artix_omniverse = !a.results_filter_show_artix_omniverse,
        app,
    ) || try_toggle_simple_filter(
        mx,
        my,
        app.results_filter_artix_universe_rect,
        |a| a.results_filter_show_artix_universe = !a.results_filter_show_artix_universe,
        app,
    ) || try_toggle_simple_filter(
        mx,
        my,
        app.results_filter_artix_lib32_rect,
        |a| a.results_filter_show_artix_lib32 = !a.results_filter_show_artix_lib32,
        app,
    ) || try_toggle_simple_filter(
        mx,
        my,
        app.results_filter_artix_galaxy_rect,
        |a| a.results_filter_show_artix_galaxy = !a.results_filter_show_artix_galaxy,
        app,
    ) || try_toggle_simple_filter(
        mx,
        my,
        app.results_filter_artix_world_rect,
        |a| a.results_filter_show_artix_world = !a.results_filter_show_artix_world,
        app,
    ) || try_toggle_simple_filter(
        mx,
        my,
        app.results_filter_artix_system_rect,
        |a| a.results_filter_show_artix_system = !a.results_filter_show_artix_system,
        app,
    ) || try_toggle_simple_filter(
        mx,
        my,
        app.results_filter_blackarch_rect,
        |a| a.results_filter_show_blackarch = !a.results_filter_show_blackarch,
        app,
    ) || try_toggle_simple_filter(
        mx,
        my,
        app.results_filter_manjaro_rect,
        |a| a.results_filter_show_manjaro = !a.results_filter_show_manjaro,
        app,
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::state::types::{NewsFeedSource, NewsReadFilter, NewsSortMode};

    fn sample_news_items() -> Vec<crate::state::types::NewsFeedItem> {
        vec![
            crate::state::types::NewsFeedItem {
                id: "arch-news-1".to_string(),
                date: "2025-01-01".to_string(),
                title: "Arch item".to_string(),
                summary: None,
                url: None,
                source: NewsFeedSource::ArchNews,
                severity: None,
                packages: Vec::new(),
            },
            crate::state::types::NewsFeedItem {
                id: "adv-1".to_string(),
                date: "2025-01-02".to_string(),
                title: "Advisory item".to_string(),
                summary: None,
                url: None,
                source: NewsFeedSource::SecurityAdvisory,
                severity: None,
                packages: vec!["example".to_string()],
            },
        ]
    }

    #[test]
    fn news_filter_click_toggles_flags_and_refreshes() {
        let mut app = crate::state::AppState {
            app_mode: crate::state::types::AppMode::News,
            news_items: sample_news_items(),
            news_filter_show_arch_news: true,
            news_filter_show_advisories: true,
            news_filter_installed_only: false,
            news_sort_mode: NewsSortMode::DateDesc,
            news_filter_arch_rect: Some((0, 0, 5, 1)),
            ..crate::state::AppState::default()
        };
        app.refresh_news_results();

        let handled = handle_filters_mouse(0, 0, &mut app);

        assert_eq!(handled, Some(false));
        assert!(!app.news_filter_show_arch_news);
        assert!(app.news_results.len() <= app.news_items.len());
        assert!(
            app.news_results
                .iter()
                .all(|i| matches!(i.source, NewsFeedSource::SecurityAdvisory))
        );
    }

    #[test]
    fn news_read_filter_click_cycles_status() {
        let mut app = crate::state::AppState {
            app_mode: crate::state::types::AppMode::News,
            news_items: sample_news_items(),
            news_filter_read_status: NewsReadFilter::All,
            news_filter_read_rect: Some((0, 0, 6, 1)),
            ..crate::state::AppState::default()
        };
        app.refresh_news_results();

        let handled1 = handle_filters_mouse(0, 0, &mut app);
        assert_eq!(handled1, Some(false));
        assert_eq!(app.news_filter_read_status, NewsReadFilter::Unread);

        let handled2 = handle_filters_mouse(0, 0, &mut app);
        assert_eq!(handled2, Some(false));
        assert_eq!(app.news_filter_read_status, NewsReadFilter::Read);

        let handled3 = handle_filters_mouse(0, 0, &mut app);
        assert_eq!(handled3, Some(false));
        assert_eq!(app.news_filter_read_status, NewsReadFilter::All);
    }
}