ferrodock 1.28.0

A fast, native container manager for Docker, written in Rust.
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
//! User preferences: what they are, how they're applied, and the dialog
//! that changes them. They're saved with the window state between runs.

use eframe::egui::{self, CornerRadius, Frame, Margin, RichText, Stroke, ThemePreference};
use serde::{Deserialize, Serialize};

use super::components::confirm::{self, Choice};
use super::components::{table, widgets};
use super::page::{Confirmation, TypedCheck};
use crate::docker::RESET_WORD;
use crate::theme::Palette;
use crate::update::{Status, Updater};
use crate::vm;

/// Key the settings are stored under.
pub const STORAGE_KEY: &str = "settings";

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum Theme {
    #[default]
    Light,
    Dark,
    /// Follow the operating system.
    System,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum Density {
    #[default]
    Comfortable,
    /// One-line rows, so more fit on screen.
    Compact,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct Settings {
    pub theme: Theme,
    /// An editor theme (its id) used instead of Ferrodock's own colours;
    /// it brings its own light or dark mode.
    pub editor_theme: Option<String>,
    pub density: Density,
    /// Interface size, 0.9 to 1.25.
    pub scale: f32,
    /// Look for a new version when Ferrodock starts.
    pub check_updates: bool,
    /// Closing the window hides it, leaving Ferrodock in the menu bar
    /// (notification area on Windows).
    pub close_to_tray: bool,
    /// The engine picked last (a Docker context's name, or a detected
    /// engine's); None follows the Docker CLI's current context.
    pub engine: Option<String>,
    /// Compose files brought up from Ferrodock, newest first.
    pub recent_compose: Vec<String>,
    /// Show a system notification when a container crashes or runs out
    /// of memory while Ferrodock isn't in front.
    pub notify_crashes: bool,
    /// Start the Docker engine when Ferrodock opens and finds it down.
    pub start_engine: bool,
    /// The app that served the engine when it last ran (macOS), to start
    /// it again.
    pub engine_app: Option<String>,
    /// CPUs, memory and disk size for Ferrodock's engine (macOS); None is
    /// the default.
    pub engine_cpus: Option<u32>,
    pub engine_memory_mib: Option<u64>,
    pub engine_disk_gib: Option<u64>,
    /// Ferrodock has put `fdock` on the PATH (or found that it needs a
    /// password), so it doesn't try again at start.
    pub cli_installed: bool,
    /// Ferrodock opens when the user logs in. Read from the system at
    /// start, never saved: the system's login items are the record.
    #[serde(skip)]
    pub launch_at_login: bool,
}

impl Default for Settings {
    fn default() -> Self {
        Self {
            theme: Theme::default(),
            editor_theme: None,
            density: Density::default(),
            scale: 1.0,
            check_updates: true,
            close_to_tray: true,
            engine: None,
            recent_compose: Vec::new(),
            notify_crashes: true,
            start_engine: true,
            engine_app: None,
            engine_cpus: None,
            engine_memory_mib: None,
            engine_disk_gib: None,
            cli_installed: false,
            launch_at_login: false,
        }
    }
}

impl Settings {
    /// Compose files kept in the recent list.
    const RECENT: usize = 8;

    /// Puts `files` at the top of the recent Compose files.
    pub fn remember_compose_files(&mut self, files: &[std::path::PathBuf]) {
        for file in files.iter().rev() {
            let file = file.display().to_string();
            self.recent_compose.retain(|f| *f != file);
            self.recent_compose.insert(0, file);
        }
        self.recent_compose.truncate(Self::RECENT);
    }

    /// What Ferrodock's engine gets: the chosen CPUs, memory and disk,
    /// within what this computer has.
    pub fn resources(&self) -> vm::Resources {
        let (default, most) = (vm::Resources::default(), vm::Resources::most());
        vm::Resources {
            cpus: self.engine_cpus.unwrap_or(default.cpus).clamp(1, most.cpus),
            memory_mib: self
                .engine_memory_mib
                .unwrap_or(default.memory_mib)
                .clamp(1024, most.memory_mib.max(1024)),
            disk_gib: self
                .engine_disk_gib
                .unwrap_or(default.disk_gib)
                .clamp(vm::DISK_GIB[0], most.disk_gib),
        }
    }

    pub const MIN_SCALE: f32 = 0.9;
    pub const MAX_SCALE: f32 = 1.25;

    /// Makes the settings take effect.
    pub fn apply(&self, ctx: &egui::Context) {
        match self
            .editor_theme
            .as_deref()
            .and_then(crate::theme::editor_theme)
        {
            Some(editor) if editor.dark => {
                crate::theme::use_palettes(ctx, &editor.palette, &crate::theme::LIGHT);
                ctx.set_theme(ThemePreference::Dark);
            }
            Some(editor) => {
                crate::theme::use_palettes(ctx, &crate::theme::DARK, &editor.palette);
                ctx.set_theme(ThemePreference::Light);
            }
            None => {
                crate::theme::use_palettes(ctx, &crate::theme::DARK, &crate::theme::LIGHT);
                ctx.set_theme(match self.theme {
                    Theme::Light => ThemePreference::Light,
                    Theme::Dark => ThemePreference::Dark,
                    Theme::System => ThemePreference::System,
                });
            }
        }
        ctx.set_zoom_factor(self.scale.clamp(Self::MIN_SCALE, Self::MAX_SCALE));
        table::set_compact(ctx, self.density == Density::Compact);
    }
}

/// Draws the Settings dialog. Changes apply immediately. Returns false
/// once it should close.
pub fn show(
    ui: &mut egui::Ui,
    p: &Palette,
    settings: &mut Settings,
    updater: &Updater,
    engine: &vm::Engine,
) -> bool {
    let before = settings.clone();
    let mut close = false;
    let modal = egui::Modal::new(egui::Id::unique("ferrodock-settings"))
        .frame(
            Frame::new()
                .fill(p.sidebar)
                .stroke(Stroke::new(1.0, p.border))
                .corner_radius(CornerRadius::same(10))
                .inner_margin(Margin::same(20)),
        )
        .show(ui.ctx(), |ui| {
            ui.set_width(430.0);
            ui.label(RichText::new("Settings").size(18.0).strong().color(p.text));
            ui.add_space(14.0);

            widgets::dialog_body(ui, |ui| {
                heading(ui, p, "Appearance");
                egui::Grid::new("settings-grid")
                    .num_columns(2)
                    .spacing([18.0, 12.0])
                    .show(ui, |ui| {
                        ui.label(RichText::new("Theme").color(p.muted));
                        ui.horizontal(|ui| {
                            for (theme, label) in [
                                (Theme::Light, "Light"),
                                (Theme::Dark, "Dark"),
                                (Theme::System, "System"),
                            ] {
                                let on = settings.editor_theme.is_none() && settings.theme == theme;
                                if ui.selectable_label(on, label).clicked() {
                                    settings.theme = theme;
                                    settings.editor_theme = None;
                                }
                            }
                        });
                        ui.end_row();

                        ui.label(RichText::new("Density").color(p.muted));
                        ui.horizontal(|ui| {
                            ui.selectable_value(
                                &mut settings.density,
                                Density::Comfortable,
                                "Comfortable",
                            );
                            ui.selectable_value(&mut settings.density, Density::Compact, "Compact");
                        });
                        ui.end_row();

                        ui.label(RichText::new("Interface size").color(p.muted));
                        // The slider shows percent; the setting is a factor.
                        let mut percent = (settings.scale * 100.0).round();
                        let slider = egui::Slider::new(
                            &mut percent,
                            Settings::MIN_SCALE * 100.0..=Settings::MAX_SCALE * 100.0,
                        )
                        .step_by(5.0)
                        .suffix("%");
                        // Apply when the drag ends, so the slider doesn't move
                        // under the pointer while the whole window rescales.
                        let response = ui.add(slider);
                        if response.drag_stopped() || (response.changed() && !response.dragged()) {
                            settings.scale = percent / 100.0;
                        }
                        ui.end_row();
                    });

                ui.add_space(14.0);
                ui.label(RichText::new("Editor themes").color(p.muted));
                ui.add_space(6.0);
                egui::Grid::new("editor-themes")
                    .num_columns(3)
                    .spacing([12.0, 10.0])
                    .show(ui, |ui| {
                        for (i, editor) in crate::theme::EDITOR_THEMES.iter().enumerate() {
                            let on = settings.editor_theme.as_deref() == Some(editor.id);
                            if theme_swatch(ui, p, editor, on).clicked() {
                                settings.editor_theme = Some(editor.id.to_owned());
                            }
                            if i % 3 == 2 {
                                ui.end_row();
                            }
                        }
                    });
                ui.add_space(18.0);
                heading(ui, p, "Notifications");
                ui.checkbox(
                    &mut settings.notify_crashes,
                    "Tell me when a container crashes, even when Ferrodock is in the background",
                );

                ui.add_space(18.0);
                heading(ui, p, "Startup");
                ui.checkbox(
                    &mut settings.start_engine,
                    "Start the Docker engine when Ferrodock opens, if it isn't running",
                );
                let before = settings.launch_at_login;
                ui.checkbox(
                    &mut settings.launch_at_login,
                    "Open Ferrodock when you log in",
                );
                let error_id = egui::Id::unique("launch-at-login-error");
                if settings.launch_at_login != before
                    && let Err(e) = crate::app::autostart::set(settings.launch_at_login)
                {
                    settings.launch_at_login = before;
                    ui.data_mut(|d| d.insert_temp(error_id, e));
                }
                if let Some(error) = ui.data(|d| d.get_temp::<String>(error_id)) {
                    ui.label(RichText::new(format!("Couldn't change it: {error}")).color(p.danger));
                }
                if vm::SUPPORTED && engine.state() != vm::State::NotInstalled {
                    ui.add_space(18.0);
                    heading(ui, p, "Ferrodock's engine");
                    engine_section(ui, p, settings, engine);
                }
                if crate::app::tray::Tray::SUPPORTED {
                    ui.add_space(18.0);
                    let place = if cfg!(target_os = "macos") {
                        "Menu bar"
                    } else {
                        "Notification area"
                    };
                    heading(ui, p, place);
                    ui.checkbox(
                        &mut settings.close_to_tray,
                        "Keep running there when the window is closed",
                    );
                }

                ui.add_space(18.0);
                heading(ui, p, "Updates");
                ui.checkbox(
                    &mut settings.check_updates,
                    "Check for updates when Ferrodock starts",
                );
                ui.horizontal(|ui| {
                    let status = updater.status();
                    let (text, color) = match &status {
                        Status::Idle => (String::new(), p.muted),
                        Status::Checking => ("Checking…".into(), p.muted),
                        Status::UpToDate => ("Ferrodock is up to date.".into(), p.muted),
                        Status::Available(a) => {
                            (format!("Version {} is available.", a.version), p.text)
                        }
                        Status::Downloading { version, .. } => {
                            (format!("Downloading {version}…"), p.muted)
                        }
                        Status::Ready { version, .. } => (
                            format!("Version {version} is installed. Restart to use it."),
                            p.text,
                        ),
                        Status::Failed(e) => (e.clone(), p.danger),
                    };
                    if let Status::Available(available) = status {
                        if widgets::primary_button(ui, "Update now").clicked() {
                            updater.install(available);
                        }
                    } else if ui.button("Check now").clicked() {
                        updater.check();
                    }
                    ui.label(RichText::new(text).color(color));
                });
                if crate::update::installed_with_homebrew() {
                    ui.label(
                        RichText::new(
                            "Installed with Homebrew: `brew upgrade --cask ferrodock` works too.",
                        )
                        .size(12.0)
                        .color(p.muted),
                    );
                }

                ui.add_space(18.0);
                heading(ui, p, "About");
                ui.label(
                    RichText::new(format!("Ferrodock {}", env!("CARGO_PKG_VERSION"))).color(p.text),
                );
                ui.horizontal(|ui| {
                    ui.hyperlink_to("Website", "https://manimovassagh.github.io/Ferrodock/");
                    ui.label(RichText::new("·").color(p.muted));
                    ui.hyperlink_to("GitHub", "https://github.com/manimovassagh/Ferrodock");
                    ui.label(RichText::new("·").color(p.muted));
                    ui.hyperlink_to(
                        "Report a problem",
                        "https://github.com/manimovassagh/Ferrodock/issues/new/choose",
                    );
                });
            });

            ui.add_space(18.0);
            ui.horizontal(|ui| {
                if ui.button("Reset to defaults").clicked() {
                    *settings = Settings::default();
                }
                ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
                    close = ui.button("Done").clicked();
                });
            });
        });
    if *settings != before {
        settings.apply(ui.ctx());
    }
    let open = !(close || modal.should_close());
    reset_dialog(ui, p, settings, engine, open);
    open
}

/// Where the open "Reset engine?" dialog keeps its state.
fn reset_id() -> egui::Id {
    egui::Id::unique("ferrodock-engine-reset")
}

/// Asks before resetting the engine, over the Settings dialog, and
/// resets it on a yes. Closing Settings drops the question.
fn reset_dialog(
    ui: &mut egui::Ui,
    p: &Palette,
    settings: &Settings,
    engine: &vm::Engine,
    open: bool,
) {
    let Some(mut confirmation) = ui.data(|d| d.get_temp::<Confirmation>(reset_id())) else {
        return;
    };
    let choice = if open {
        confirm::show(ui, p, &mut confirmation)
    } else {
        Some(Choice::Cancel)
    };
    match choice {
        Some(Choice::Confirm) => {
            ui.data_mut(|d| d.remove::<Confirmation>(reset_id()));
            engine.reset(settings.resources(), repaint(ui));
        }
        Some(Choice::Cancel) => ui.data_mut(|d| d.remove::<Confirmation>(reset_id())),
        // Keep what was typed so far.
        None => {
            ui.data_mut(|d| d.insert_temp(reset_id(), confirmation));
        }
    }
}

/// The "Reset engine?" question: what's deleted, said plainly.
fn reset_confirmation() -> Confirmation {
    let how = if cfg!(windows) {
        "Its WSL distro, ferrodock, is removed and set up again, which downloads the engine \
         again."
    } else if cfg!(target_os = "linux") {
        "Its data folder, /var/lib/ferrodock, is deleted. This asks for your admin password."
    } else {
        "Its disk, ~/.ferrodock/data.img, is deleted and made again, empty, at the size \
         chosen above."
    };
    Confirmation {
        title: "Reset Ferrodock's engine?".into(),
        message: format!(
            "This stops the engine and deletes everything in it: all containers, images, \
             volumes (and the data in them), networks and build cache. Then it starts again, \
             empty, as on first use.\n\n{how}\n\nThis can't be undone."
        ),
        action_label: "Reset engine".into(),
        typed: Some(TypedCheck::new(RESET_WORD)),
        ..Default::default()
    }
}

/// Wakes the UI as the engine's state changes.
fn repaint(ui: &egui::Ui) -> impl Fn() + Send + Sync + 'static {
    let ctx = ui.ctx().clone();
    move || ctx.request_repaint()
}

/// Ferrodock's own engine: how it's doing, what it gets (macOS), start,
/// stop, restart and reset, and its log.
fn engine_section(ui: &mut egui::Ui, p: &Palette, settings: &mut Settings, engine: &vm::Engine) {
    let state = engine.state();
    let (text, color) = match &state {
        vm::State::Running => ("Running".to_owned(), p.text),
        vm::State::Stopped | vm::State::NotInstalled => ("Stopped".to_owned(), p.muted),
        vm::State::InstallingWsl => ("Installing WSL…".to_owned(), p.muted),
        vm::State::NeedsRestart => ("Restart Windows to finish setting up".to_owned(), p.muted),
        vm::State::Starting => ("Starting…".to_owned(), p.muted),
        vm::State::Stopping => ("Stopping…".to_owned(), p.muted),
        vm::State::Downloading { update: false, .. } => ("Downloading…".to_owned(), p.muted),
        vm::State::Downloading { update: true, .. } => ("Updating…".to_owned(), p.muted),
        vm::State::Failed(e) => (e.clone(), p.danger),
    };
    ui.label(RichText::new(text).color(color));
    if cfg!(target_os = "macos") {
        let (default, most) = (vm::Resources::default(), vm::Resources::most());
        let chosen = settings.resources();
        egui::Grid::new("engine-grid")
            .num_columns(2)
            .spacing([18.0, 12.0])
            .show(ui, |ui| {
                ui.label(RichText::new("CPUs").color(p.muted));
                let mut cpus = chosen.cpus;
                if ui
                    .add(egui::Slider::new(&mut cpus, 1..=most.cpus))
                    .changed()
                {
                    settings.engine_cpus = (cpus != default.cpus).then_some(cpus);
                }
                ui.end_row();

                ui.label(RichText::new("Memory").color(p.muted));
                let mut gib = chosen.memory_mib / 1024;
                let slider =
                    egui::Slider::new(&mut gib, 1..=(most.memory_mib / 1024).max(1)).suffix(" GB");
                if ui.add(slider).changed() {
                    let mib = gib * 1024;
                    settings.engine_memory_mib = (mib != default.memory_mib).then_some(mib);
                }
                ui.end_row();

                ui.label(RichText::new("Disk").color(p.muted));
                ui.horizontal(|ui| {
                    for gib in vm::DISK_GIB {
                        if ui
                            .selectable_label(chosen.disk_gib == gib, format!("{gib} GB"))
                            .clicked()
                        {
                            settings.engine_disk_gib = (gib != default.disk_gib).then_some(gib);
                        }
                    }
                });
                ui.end_row();
            });
        // The disk never shrinks: that would lose what's on it.
        let note = match engine.disk_gib() {
            Some(now) if chosen.disk_gib < now => Some(format!(
                "The disk is {now} GB and only grows: reset the engine to start over with a \
                 smaller one."
            )),
            _ if engine.resources().is_some_and(|given| given != chosen) => {
                Some("Restart the engine to use the new settings.".to_owned())
            }
            _ => None,
        };
        if let Some(note) = note {
            ui.label(RichText::new(note).size(12.0).color(p.muted));
        }
    }
    ui.horizontal(|ui| {
        match state {
            vm::State::Running => {
                if ui.button("Stop engine").clicked() {
                    engine.shut_down(repaint(ui));
                }
                if vm::Engine::RESTARTS && ui.button("Restart engine").clicked() {
                    engine.restart(settings.resources(), repaint(ui));
                }
            }
            vm::State::Stopped | vm::State::NotInstalled | vm::State::Failed(_) => {
                if ui.button("Start engine").clicked() {
                    engine.start(settings.resources(), repaint(ui));
                }
            }
            // Busy, or waiting for Windows to restart: nothing to click.
            vm::State::InstallingWsl
            | vm::State::NeedsRestart
            | vm::State::Starting
            | vm::State::Stopping
            | vm::State::Downloading { .. } => {}
        }
        if !state.busy() && ui.button("Reset engine…").clicked() {
            ui.data_mut(|d| d.insert_temp(reset_id(), reset_confirmation()));
        }
        if let Some(log) = engine.log()
            && ui.button("Open log").clicked()
        {
            open_file(&log);
        }
    });
    if cfg!(target_os = "linux") {
        ui.label(
            RichText::new(
                "It runs as the ferrodock-engine service. Its log: journalctl -u ferrodock-engine",
            )
            .size(12.0)
            .color(p.muted),
        );
    }
}

/// Opens a text file in the system's editor.
fn open_file(path: &std::path::Path) {
    let mut command = if cfg!(target_os = "macos") {
        let mut c = std::process::Command::new("open");
        c.arg("-t");
        c
    } else if cfg!(windows) {
        std::process::Command::new("notepad.exe")
    } else {
        std::process::Command::new("xdg-open")
    };
    let _ = command.arg(path).spawn();
}

/// An editor theme to pick: a small preview of its colours (background,
/// text lines in its text and accent colours) and its name under it.
fn theme_swatch(
    ui: &mut egui::Ui,
    p: &Palette,
    editor: &crate::theme::EditorTheme,
    selected: bool,
) -> egui::Response {
    let size = egui::vec2(132.0, 70.0);
    let (rect, response) = ui.allocate_exact_size(size, egui::Sense::click());
    let painter = ui.painter();
    let theme = &editor.palette;
    let preview = egui::Rect::from_min_size(rect.min, egui::vec2(size.x, 48.0));
    painter.rect_filled(preview, CornerRadius::same(6), theme.bg);
    let side = egui::Rect::from_min_size(preview.min, egui::vec2(26.0, preview.height()));
    painter.rect_filled(
        side,
        CornerRadius {
            nw: 6,
            sw: 6,
            ne: 0,
            se: 0,
        },
        theme.sidebar,
    );
    for (row, (color, width)) in [
        (theme.accent, 34.0),
        (theme.text, 50.0),
        (theme.muted, 40.0),
    ]
    .into_iter()
    .enumerate()
    {
        let y = preview.top() + 12.0 + row as f32 * 11.0;
        let line =
            egui::Rect::from_min_size(egui::pos2(side.right() + 8.0, y), egui::vec2(width, 4.0));
        painter.rect_filled(line, CornerRadius::same(2), color);
    }
    painter.circle_filled(side.center_top() + egui::vec2(0.0, 12.0), 4.0, theme.accent);
    let ring = if selected {
        Stroke::new(2.0, p.accent)
    } else if response.hovered() {
        Stroke::new(1.0, p.muted)
    } else {
        Stroke::new(1.0, p.border)
    };
    painter.rect_stroke(
        preview,
        CornerRadius::same(6),
        ring,
        egui::StrokeKind::Outside,
    );
    painter.text(
        egui::pos2(rect.center().x, preview.bottom() + 12.0),
        egui::Align2::CENTER_CENTER,
        editor.name,
        egui::FontId::proportional(11.5),
        if selected { p.text } else { p.muted },
    );
    response.widget_info(|| {
        egui::WidgetInfo::selected(egui::Role::RadioButton, true, selected, editor.name)
    });
    response.on_hover_cursor(egui::CursorIcon::PointingHand)
}

fn heading(ui: &mut egui::Ui, p: &Palette, text: &str) {
    ui.label(RichText::new(text.to_uppercase()).size(11.0).color(p.muted));
    ui.add_space(6.0);
}

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

    #[test]
    fn light_is_the_default() {
        let s = Settings::default();
        assert_eq!(s.theme, Theme::Light);
        assert_eq!(s.density, Density::Comfortable);
        assert_eq!(s.scale, 1.0);
    }

    #[test]
    fn missing_fields_fall_back_to_defaults() {
        // Settings saved by an older version may lack newer fields.
        let s: Settings = serde_json::from_str(r#"{"theme":"Dark"}"#).unwrap();
        assert_eq!(s.theme, Theme::Dark);
        assert_eq!(s.scale, 1.0);
    }

    #[test]
    fn engine_gets_what_was_chosen_within_limits() {
        let mut s = Settings::default();
        assert_eq!(s.resources(), vm::Resources::default());
        s.engine_cpus = Some(10_000);
        s.engine_memory_mib = Some(1);
        s.engine_disk_gib = Some(1);
        let r = s.resources();
        assert_eq!(r.cpus, vm::Resources::most().cpus);
        assert_eq!(r.memory_mib, 1024);
        assert_eq!(r.disk_gib, vm::DISK_GIB[0]);
        let old: Settings = serde_json::from_str("{}").unwrap();
        assert_eq!(old.engine_cpus, None);
        assert_eq!(old.resources().disk_gib, 128);
    }
}