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
//! Preflight modal tab-specific mouse event handling.

use crate::state::AppState;
use crate::state::modal::ServiceRestartDecision;
use crossterm::event::{MouseEvent, MouseEventKind};
use std::collections::{HashMap, HashSet};

use super::preflight_helpers::build_deps_display_items;

/// Handle click events in the Deps tab of the Preflight modal.
///
/// What: Process clicks on package group headers to toggle expansion state.
///
/// Inputs:
/// - `mx`: Mouse X coordinate (column)
/// - `my`: Mouse Y coordinate (row)
/// - `app`: Mutable application state containing modal state and UI rectangles
///
/// Output:
/// - `true` if the click was handled, `false` otherwise.
///
/// Details:
/// - Builds display items list to find which package header was clicked.
/// - Calculates clicked index accounting for scroll position.
/// - Toggles package expansion state when clicking on a header.
pub(super) fn handle_deps_tab_click(mx: u16, my: u16, app: &mut AppState) -> bool {
    if let crate::state::Modal::Preflight {
        items,
        dependency_info,
        dep_selected,
        dep_tree_expanded,
        ..
    } = &mut app.modal
    {
        if dependency_info.is_empty() {
            return false;
        }

        let Some((content_x, content_y, content_w, content_h)) = app.preflight_content_rect else {
            return false;
        };

        if mx < content_x
            || mx >= content_x + content_w
            || my < content_y
            || my >= content_y + content_h
        {
            return false;
        }

        // Calculate which row was clicked relative to content area
        let clicked_row = (my - content_y) as usize;

        // Build display items list to find which package header was clicked
        let display_items = build_deps_display_items(items, dependency_info, dep_tree_expanded);

        // Calculate offset for summary line before the list
        // Deps tab has: summary line (1) + empty line (1) = 2 lines
        let list_start_offset = 2;

        // Only process clicks that are on or after the list starts
        if clicked_row < list_start_offset {
            return false;
        }

        let list_clicked_row = clicked_row - list_start_offset;

        // Calculate scroll position to find actual index
        let available_height =
            content_h.saturating_sub(u16::try_from(list_start_offset).unwrap_or(u16::MAX)) as usize;
        let start_idx = (*dep_selected)
            .saturating_sub(available_height / 2)
            .min(display_items.len().saturating_sub(available_height));

        let actual_idx = start_idx + list_clicked_row;
        if let Some((is_header, pkg_name)) = display_items.get(actual_idx)
            && *is_header
            && actual_idx < display_items.len()
        {
            // Toggle this package's expanded state
            if dep_tree_expanded.contains(pkg_name) {
                dep_tree_expanded.remove(pkg_name);
            } else {
                dep_tree_expanded.insert(pkg_name.clone());
            }
            return true;
        }
    }
    false
}

/// Handle click events in the Files tab of the Preflight modal.
///
/// What: Process clicks on package group headers to toggle expansion state.
///
/// Inputs:
/// - `mx`: Mouse X coordinate (column)
/// - `my`: Mouse Y coordinate (row)
/// - `app`: Mutable application state containing modal state and UI rectangles
///
/// Output:
/// - `true` if the click was handled, `false` otherwise.
///
/// Details:
/// - Uses existing helper to build display items list.
/// - Calculates clicked index accounting for scroll position and sync timestamp offset.
/// - Toggles package expansion state when clicking on a header.
pub(super) fn handle_files_tab_click(mx: u16, my: u16, app: &mut AppState) -> bool {
    if let crate::state::Modal::Preflight {
        items,
        file_info,
        file_selected,
        file_tree_expanded,
        ..
    } = &mut app.modal
    {
        let Some((content_x, content_y, content_w, content_h)) = app.preflight_content_rect else {
            return false;
        };

        if mx < content_x
            || mx >= content_x + content_w
            || my < content_y
            || my >= content_y + content_h
        {
            return false;
        }

        // Calculate which row was clicked relative to content area
        let clicked_row = (my - content_y) as usize;

        // Build display items list to find which package header was clicked
        // Always show all packages, even if they have no files
        let display_items = crate::events::preflight::build_file_display_items(
            items,
            file_info,
            file_tree_expanded,
        );

        // Calculate offset for summary lines before the list
        // Files tab has: summary line (1) + empty line (1) + optional sync timestamp (0-2) + empty line (0-1)
        // Minimum offset is 2 lines (summary + empty)
        let sync_timestamp_lines = if crate::logic::files::get_file_db_sync_info().is_some() {
            2 // timestamp line + empty line
        } else {
            0
        };
        let list_start_offset = 2 + sync_timestamp_lines; // summary + empty + sync timestamp lines

        // Only process clicks that are on or after the list starts
        if clicked_row < list_start_offset {
            return false;
        }

        let list_clicked_row = clicked_row - list_start_offset;

        // Calculate scroll position to find actual index
        let total_items = display_items.len();
        let file_selected_clamped = (*file_selected).min(total_items.saturating_sub(1));
        let available_height =
            content_h.saturating_sub(u16::try_from(list_start_offset).unwrap_or(u16::MAX)) as usize;
        let (start_idx, _end_idx) = if total_items <= available_height {
            (0, total_items)
        } else {
            let start = file_selected_clamped
                .saturating_sub(available_height / 2)
                .min(total_items.saturating_sub(available_height));
            let end = (start + available_height).min(total_items);
            (start, end)
        };

        let actual_idx = start_idx + list_clicked_row;
        if let Some((is_header, pkg_name)) = display_items.get(actual_idx)
            && *is_header
            && actual_idx < display_items.len()
        {
            // Toggle this package's expanded state
            if file_tree_expanded.contains(pkg_name) {
                file_tree_expanded.remove(pkg_name);
            } else {
                file_tree_expanded.insert(pkg_name.clone());
            }
            return true;
        }
    }
    false
}

/// Handle click events in the Services tab of the Preflight modal.
///
/// What: Process clicks on service items to select them or toggle restart decision.
///
/// Inputs:
/// - `mx`: Mouse X coordinate (column)
/// - `my`: Mouse Y coordinate (row)
/// - `app`: Mutable application state containing modal state and UI rectangles
///
/// Output:
/// - `true` if the click was handled, `false` otherwise.
///
/// Details:
/// - Calculates clicked index accounting for scroll position.
/// - Selects service when clicking on a different item.
/// - Toggles restart decision when clicking on the currently selected item.
pub(super) fn handle_services_tab_click(mx: u16, my: u16, app: &mut AppState) -> bool {
    if let crate::state::Modal::Preflight {
        service_info,
        service_selected,
        ..
    } = &mut app.modal
    {
        if service_info.is_empty() {
            return false;
        }

        let Some((content_x, content_y, content_w, content_h)) = app.preflight_content_rect else {
            return false;
        };

        if mx < content_x
            || mx >= content_x + content_w
            || my < content_y
            || my >= content_y + content_h
        {
            return false;
        }

        let list_start_offset = 2;
        let clicked_row_offset = (my - content_y) as usize;
        if clicked_row_offset < list_start_offset {
            return false;
        }

        let list_clicked_row = clicked_row_offset - list_start_offset;
        let available_height =
            content_h.saturating_sub(u16::try_from(list_start_offset).unwrap_or(u16::MAX)) as usize;
        let total_items = service_info.len();
        if total_items == 0 {
            return false;
        }

        let selected_clamped = (*service_selected).min(total_items.saturating_sub(1));
        let start_idx = if total_items <= available_height {
            0
        } else {
            selected_clamped
                .saturating_sub(available_height / 2)
                .min(total_items.saturating_sub(available_height))
        };
        let end_idx = (start_idx + available_height).min(total_items);
        let actual_idx = start_idx + list_clicked_row;
        if actual_idx >= end_idx {
            return false;
        }

        let actual_idx = actual_idx.min(total_items.saturating_sub(1));
        if actual_idx == *service_selected {
            if let Some(service) = service_info.get_mut(actual_idx) {
                service.restart_decision = match service.restart_decision {
                    ServiceRestartDecision::Restart => ServiceRestartDecision::Defer,
                    ServiceRestartDecision::Defer => ServiceRestartDecision::Restart,
                };
            }
        } else {
            *service_selected = actual_idx;
        }
        return true;
    }
    false
}

/// Handle scroll events in the Deps tab of the Preflight modal.
///
/// What: Process mouse scroll to navigate the dependency list.
///
/// Inputs:
/// - `m`: Mouse event including scroll direction
/// - `app`: Mutable application state containing modal state
///
/// Output:
/// - `true` if the scroll was handled, `false` otherwise.
///
/// Details:
/// - Computes display length (headers + dependencies, accounting for folded groups).
/// - Handles scroll up/down to move selection.
pub(super) fn handle_deps_tab_scroll(m: MouseEvent, app: &mut AppState) -> bool {
    if let crate::state::Modal::Preflight {
        items,
        dependency_info,
        dep_selected,
        dep_tree_expanded,
        ..
    } = &mut app.modal
    {
        // Compute display_items length (headers + dependencies, accounting for folded groups)
        let mut grouped: HashMap<String, Vec<&crate::state::modal::DependencyInfo>> =
            HashMap::new();
        for dep in dependency_info.iter() {
            for req_by in &dep.required_by {
                grouped.entry(req_by.clone()).or_default().push(dep);
            }
        }
        let mut display_len: usize = 0;
        for pkg_name in items.iter().map(|p| &p.name) {
            if let Some(pkg_deps) = grouped.get(pkg_name) {
                display_len += 1; // Header
                // Count dependencies only if expanded
                if dep_tree_expanded.contains(pkg_name) {
                    let mut seen_deps = HashSet::new();
                    for dep in pkg_deps {
                        if seen_deps.insert(dep.name.as_str()) {
                            display_len += 1;
                        }
                    }
                }
            }
        }

        // Handle mouse scroll to navigate dependency list
        if display_len > 0 {
            match m.kind {
                MouseEventKind::ScrollUp => {
                    if *dep_selected > 0 {
                        *dep_selected -= 1;
                    }
                    return true;
                }
                MouseEventKind::ScrollDown => {
                    if *dep_selected < display_len.saturating_sub(1) {
                        *dep_selected += 1;
                    }
                    return true;
                }
                _ => {}
            }
        }
    }
    false
}

/// Handle scroll events in the Files tab of the Preflight modal.
///
/// What: Process mouse scroll to navigate the file list.
///
/// Inputs:
/// - `m`: Mouse event including scroll direction
/// - `app`: Mutable application state containing modal state
///
/// Output:
/// - `true` if the scroll was handled, `false` otherwise.
///
/// Details:
/// - Uses existing helper to compute display length.
/// - Handles scroll up/down to move selection.
pub(super) fn handle_files_tab_scroll(m: MouseEvent, app: &mut AppState) -> bool {
    if let crate::state::Modal::Preflight {
        items,
        file_info,
        file_selected,
        file_tree_expanded,
        ..
    } = &mut app.modal
    {
        match m.kind {
            MouseEventKind::ScrollUp => {
                if *file_selected > 0 {
                    *file_selected -= 1;
                }
                return true;
            }
            MouseEventKind::ScrollDown => {
                let display_len = crate::events::preflight::compute_file_display_items_len(
                    items,
                    file_info,
                    file_tree_expanded,
                );
                if *file_selected < display_len.saturating_sub(1) {
                    *file_selected += 1;
                }
                return true;
            }
            _ => {}
        }
    }
    false
}

/// Handle scroll events in the Summary tab of the Preflight modal.
///
/// What: Process mouse scroll to scroll the entire Summary tab content.
///
/// Inputs:
/// - `m`: Mouse event including scroll direction
/// - `app`: Mutable application state containing modal state
///
/// Output:
/// - `true` if the scroll was handled, `false` otherwise.
///
/// Details:
/// - Increments/decrements scroll offset for the entire Summary tab content.
#[allow(clippy::missing_const_for_fn)]
pub(super) fn handle_summary_tab_scroll(m: MouseEvent, app: &mut AppState) -> bool {
    if let crate::state::Modal::Preflight { summary_scroll, .. } = &mut app.modal {
        match m.kind {
            MouseEventKind::ScrollUp => {
                if *summary_scroll > 0 {
                    *summary_scroll = summary_scroll.saturating_sub(1);
                }
                return true;
            }
            MouseEventKind::ScrollDown => {
                *summary_scroll = summary_scroll.saturating_add(1);
                return true;
            }
            _ => {}
        }
    }
    false
}

/// Handle scroll events in the Services tab of the Preflight modal.
///
/// What: Process mouse scroll to navigate the service list.
///
/// Inputs:
/// - `m`: Mouse event including scroll direction
/// - `app`: Mutable application state containing modal state
///
/// Output:
/// - `true` if the scroll was handled, `false` otherwise.
///
/// Details:
/// - Handles scroll up/down to move selection.
#[allow(clippy::missing_const_for_fn)]
pub(super) fn handle_services_tab_scroll(m: MouseEvent, app: &mut AppState) -> bool {
    if let crate::state::Modal::Preflight {
        service_info,
        service_selected,
        ..
    } = &mut app.modal
    {
        if service_info.is_empty() {
            return false;
        }

        match m.kind {
            MouseEventKind::ScrollUp => {
                if *service_selected > 0 {
                    *service_selected -= 1;
                }
                return true;
            }
            MouseEventKind::ScrollDown => {
                if *service_selected + 1 < service_info.len() {
                    *service_selected += 1;
                }
                return true;
            }
            _ => {}
        }
    }
    false
}