cleansys-gui 0.6.4

CleanSys — desktop GUI for system cleanup on Linux, macOS, and Windows (Iced)
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
//! Application state for the CleanSys Iced GUI.

use cleansys_core::{CleanerCategory, Status};

use crate::theme::ThemeColors;

/// Top-level state for the CleanSys GUI application.
pub struct CleanSysGui {
    /// Cleaner categories and items (shared domain model from `cleansys-core`).
    pub categories: Vec<CleanerCategory>,
    /// Rolling log of operation messages shown in the activity panel.
    pub logs: Vec<String>,
    /// Total bytes freed across all completed operations in the current run.
    pub total_bytes_cleaned: u64,
    /// Whether cleaners are currently running.
    pub is_running: bool,
    /// Whether the process is running with root/administrator privileges.
    pub is_root: bool,
    /// Whether the sudo authentication dialog is visible.
    pub needs_password: bool,
    /// The password currently typed into the authentication dialog.
    pub password_input: String,
    /// Error message shown in the authentication dialog, if any.
    pub password_error: Option<String>,
    /// Operations queued to run once sudo authentication succeeds.
    pub pending_root_ops: Vec<(usize, usize)>,
    /// Index of the currently active category tab.
    pub active_tab: usize,
    /// Index of the currently selected UI theme (see `cleansys_core::THEME_NAMES`).
    pub theme_index: usize,
    /// Whether the "confirm this run" dialog is visible.
    pub confirm_run_pending: bool,
    /// Whether a preview (dry-run) is currently being computed.
    pub previewing: bool,
    /// Whether the preview results dialog is visible.
    pub preview_open: bool,
    /// Results of the most recent preview run: `(cleaner_name, result)`.
    pub preview_results: Vec<(String, cleansys_core::CleaningResult)>,
    /// Total number of operations in the current run (for the progress bar).
    pub operations_total: usize,
    /// Number of operations completed so far in the current run.
    pub operations_completed: usize,
    /// Whether the "needs Administrator" notice is visible (Windows only;
    /// Windows has no interactive sudo-password flow, so this replaces the
    /// password dialog when elevation is required there).
    pub needs_admin_notice: bool,
}

impl Default for CleanSysGui {
    fn default() -> Self {
        Self::new()
    }
}

impl CleanSysGui {
    /// Construct a fresh application state with all known cleaners loaded.
    pub fn new() -> Self {
        // Unit tests must never read the real ~/.config/cleansys/settings.json
        // — doing so made tests flaky/order-dependent, since every test in
        // this crate's test binary shares the same real file on disk (writes
        // from one test's `save_selections()`/`save_theme()` would leak into
        // whichever test happened to construct `CleanSysGui::new()` next).
        let settings = if cfg!(test) {
            cleansys_core::Settings::default()
        } else {
            cleansys_core::load_settings().unwrap_or_default()
        };
        let mut categories = cleansys_core::load_categories();
        for category in &mut categories {
            for item in &mut category.items {
                item.selected = settings.is_selected(&category.name, &item.name);
            }
        }
        Self {
            categories,
            logs: Vec::new(),
            total_bytes_cleaned: 0,
            is_running: false,
            is_root: cleansys_core::check_root(),
            needs_password: false,
            password_input: String::new(),
            password_error: None,
            pending_root_ops: Vec::new(),
            active_tab: 0,
            theme_index: settings.theme_index(),
            confirm_run_pending: false,
            previewing: false,
            preview_open: false,
            preview_results: Vec::new(),
            operations_total: 0,
            operations_completed: 0,
            needs_admin_notice: false,
        }
    }

    /// Number of currently selected items across all categories.
    pub fn selected_count(&self) -> usize {
        self.categories
            .iter()
            .flat_map(|c| &c.items)
            .filter(|i| i.selected)
            .count()
    }

    /// Number of currently selected items within a specific category.
    pub fn selected_count_in(&self, cat_idx: usize) -> usize {
        self.categories
            .get(cat_idx)
            .map(|c| c.items.iter().filter(|i| i.selected).count())
            .unwrap_or(0)
    }

    /// True if any selected item requires root and we don't already have it.
    pub fn selection_needs_root(&self) -> bool {
        !self.is_root
            && self
                .categories
                .iter()
                .flat_map(|c| &c.items)
                .any(|i| i.selected && i.requires_root)
    }

    /// `(category_index, item_index)` pairs of every currently-selected item.
    pub fn selected_indices(&self) -> Vec<(usize, usize)> {
        self.categories
            .iter()
            .enumerate()
            .flat_map(|(ci, c)| {
                c.items
                    .iter()
                    .enumerate()
                    .filter(|(_, i)| i.selected)
                    .map(move |(ii, _)| (ci, ii))
            })
            .collect()
    }

    /// Fraction of the current run's operations completed so far, in
    /// `0.0..=1.0`. `0.0` when no run is in progress.
    pub fn progress_fraction(&self) -> f32 {
        if self.operations_total == 0 {
            0.0
        } else {
            (self.operations_completed as f32 / self.operations_total as f32).clamp(0.0, 1.0)
        }
    }

    /// Whether this platform supports the interactive sudo-password
    /// elevation flow (Unix). Windows uses UAC/Administrator tokens instead.
    pub fn supports_sudo_prompt(&self) -> bool {
        cleansys_core::utils::supports_sudo_prompt()
    }

    /// Push a line to the activity log, keeping only the most recent entries.
    pub fn push_log(&mut self, line: impl Into<String>) {
        self.logs.push(line.into());
        if self.logs.len() > 500 {
            self.logs.remove(0);
        }
    }

    /// Mark every selected item as `Status::Pending` in preparation for a run.
    pub fn mark_selected_pending(&mut self) {
        for category in &mut self.categories {
            for item in &mut category.items {
                if item.selected {
                    item.status = Some(Status::Pending);
                }
            }
        }
    }

    /// Derive the full [`ThemeColors`] from the currently active core theme.
    ///
    /// Call this at the top of view functions: `let c = state.colors();`
    pub fn colors(&self) -> ThemeColors {
        ThemeColors::from_core(&cleansys_core::theme_by_index(self.theme_index))
    }

    /// Return a custom `iced::Theme` derived from the active core theme, for
    /// the top-level `iced::application(...).theme(...)` callback.
    pub fn iced_theme(&self) -> iced::Theme {
        crate::theme::iced_theme_for(self.theme_index)
    }

    /// The display name of the currently active theme.
    pub fn current_theme_name(&self) -> &'static str {
        cleansys_core::THEME_NAMES
            .get(self.theme_index)
            .copied()
            .unwrap_or("Default")
    }

    /// Build the full [`cleansys_core::Settings`] snapshot for the current
    /// state (theme + selected cleaners), for persistence.
    pub fn current_settings(&self) -> cleansys_core::Settings {
        let selected_cleaners = self
            .categories
            .iter()
            .flat_map(|c| {
                let cat_name = c.name.clone();
                c.items
                    .iter()
                    .filter(|i| i.selected)
                    .map(move |i| cleansys_core::Settings::selection_key(&cat_name, &i.name))
            })
            .collect();

        cleansys_core::Settings {
            theme_name: Some(self.current_theme_name().to_string()),
            selected_cleaners,
        }
    }

    /// Persist the current theme and cleaner selections to `settings.json`
    /// (best-effort; failures are logged but never surfaced to the UI).
    ///
    /// A no-op under `cfg(test)` so unit tests never touch the real
    /// `~/.config/cleansys/settings.json` (see the comment in [`Self::new`]).
    pub fn save_theme(&self) {
        if cfg!(test) {
            return;
        }
        if let Err(e) = cleansys_core::save_settings(&self.current_settings()) {
            log::warn!("failed to save theme preference: {e}");
        }
    }

    /// Persist the current cleaner selections (and theme) to `settings.json`.
    /// A no-op under `cfg(test)` — see [`Self::save_theme`].
    pub fn save_selections(&self) {
        if cfg!(test) {
            return;
        }
        if let Err(e) = cleansys_core::save_settings(&self.current_settings()) {
            log::warn!("failed to save cleaner selections: {e}");
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn new_loads_categories_and_defaults() {
        let state = CleanSysGui::new();
        assert_eq!(state.categories.len(), 2);
        assert_eq!(state.active_tab, 0);
        assert_eq!(state.selected_count(), 0);
        assert!(state.logs.is_empty());
        assert_eq!(state.total_bytes_cleaned, 0);
        assert!(!state.is_running);
        assert!(!state.needs_password);
    }

    #[test]
    fn selected_count_tracks_selections() {
        let mut state = CleanSysGui::new();
        assert_eq!(state.selected_count(), 0);
        state.categories[0].items[0].selected = true;
        assert_eq!(state.selected_count(), 1);
        assert_eq!(state.selected_count_in(0), 1);
        assert_eq!(state.selected_count_in(1), 0);
        state.categories[1].items[0].selected = true;
        assert_eq!(state.selected_count(), 2);
    }

    #[test]
    fn selected_count_in_out_of_range_is_zero() {
        let state = CleanSysGui::new();
        assert_eq!(state.selected_count_in(99), 0);
    }

    /// Find `(cat_idx, item_idx)` of a system item that actually requires
    /// root on this platform (not every "System Cleaners" entry does —
    /// e.g. Homebrew on macOS must not run as root).
    fn first_root_required_item(state: &CleanSysGui) -> (usize, usize) {
        for (ci, category) in state.categories.iter().enumerate() {
            for (ii, item) in category.items.iter().enumerate() {
                if item.requires_root {
                    return (ci, ii);
                }
            }
        }
        panic!("expected at least one root-requiring cleaner on this platform");
    }

    #[test]
    fn selection_needs_root_when_not_root_and_system_item_selected() {
        let mut state = CleanSysGui::new();
        state.is_root = false;
        assert!(!state.selection_needs_root());

        let (ci, ii) = first_root_required_item(&state);
        state.categories[ci].items[ii].selected = true;
        assert!(state.selection_needs_root());
    }

    #[test]
    fn selection_needs_root_false_when_already_root() {
        let mut state = CleanSysGui::new();
        state.is_root = true;
        let (ci, ii) = first_root_required_item(&state);
        state.categories[ci].items[ii].selected = true;
        assert!(!state.selection_needs_root());
    }

    #[test]
    fn selection_needs_root_false_for_user_only_selection() {
        let mut state = CleanSysGui::new();
        state.is_root = false;
        state.categories[0].items[0].selected = true;
        assert!(!state.selection_needs_root());
    }

    #[test]
    fn push_log_caps_at_500_entries() {
        let mut state = CleanSysGui::new();
        for i in 0..600 {
            state.push_log(format!("line {i}"));
        }
        assert_eq!(state.logs.len(), 500);
        // Oldest entries should have been evicted; the log should end with the
        // most recent line.
        assert_eq!(state.logs.last().unwrap(), "line 599");
    }

    #[test]
    fn mark_selected_pending_only_affects_selected_items() {
        let mut state = CleanSysGui::new();
        state.categories[0].items[0].selected = true;
        state.mark_selected_pending();

        assert!(matches!(
            state.categories[0].items[0].status,
            Some(Status::Pending)
        ));
        assert!(state.categories[0].items[1].status.is_none());
    }

    #[test]
    fn theme_index_defaults_in_range() {
        let state = CleanSysGui::new();
        assert!(state.theme_index < cleansys_core::THEME_COUNT);
    }

    #[test]
    fn current_theme_name_matches_index() {
        let mut state = CleanSysGui::new();
        state.theme_index = cleansys_core::theme_index_by_name("Dracula");
        assert_eq!(state.current_theme_name(), "Dracula");
    }

    #[test]
    fn current_theme_name_falls_back_for_out_of_range_index() {
        let mut state = CleanSysGui::new();
        state.theme_index = 9999;
        assert_eq!(state.current_theme_name(), "Default");
    }

    #[test]
    fn colors_does_not_panic_for_any_theme() {
        let mut state = CleanSysGui::new();
        for i in 0..cleansys_core::THEME_COUNT {
            state.theme_index = i;
            let _ = state.colors();
            let _ = state.iced_theme();
        }
    }

    #[test]
    fn selected_indices_returns_selected_pairs() {
        let mut state = CleanSysGui::new();
        state.categories[0].items[0].selected = true;
        state.categories[0].items[2].selected = true;
        let indices = state.selected_indices();
        assert_eq!(indices, vec![(0, 0), (0, 2)]);
    }

    #[test]
    fn progress_fraction_is_zero_with_no_operations() {
        let state = CleanSysGui::new();
        assert_eq!(state.progress_fraction(), 0.0);
    }

    #[test]
    fn progress_fraction_computes_ratio() {
        let mut state = CleanSysGui::new();
        state.operations_total = 4;
        state.operations_completed = 1;
        assert_eq!(state.progress_fraction(), 0.25);
        state.operations_completed = 4;
        assert_eq!(state.progress_fraction(), 1.0);
    }

    #[test]
    fn current_settings_includes_selected_cleaners() {
        let mut state = CleanSysGui::new();
        state.categories[0].items[0].selected = true;
        let name = state.categories[0].items[0].name.clone();
        let cat_name = state.categories[0].name.clone();
        let settings = state.current_settings();
        assert!(settings.is_selected(&cat_name, &name));
    }

    #[test]
    fn new_restores_selection_from_settings() {
        // Can't easily isolate the real config dir in a unit test, but we can
        // at least confirm current_settings() round-trips through is_selected
        // the same way new() consults it.
        let mut state = CleanSysGui::new();
        state.categories[0].items[1].selected = true;
        let settings = state.current_settings();
        let cat_name = state.categories[0].name.clone();
        let item_name = state.categories[0].items[1].name.clone();
        assert!(settings.is_selected(&cat_name, &item_name));
        assert!(!settings.is_selected(&cat_name, "Definitely Not A Real Cleaner"));
    }
}