ferrodock 1.4.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
//! The Ferrodock window.
//!
//! This module only wires things together: it feeds worker responses into
//! the [`Store`], lays out the shell, lets the active [`Page`] draw itself
//! and turns the page's [`Intent`]s into worker requests.

mod commands;
mod components;
mod keys;
mod page;
mod pages;
#[cfg(test)]
mod screenshots;
mod settings;
mod shell;
mod sidebar;
mod store;

use eframe::egui::{self, Frame, Margin};

use crate::docker::{Action, Task};
use crate::theme;
use crate::worker::{Request, Worker};
use commands::Target;
use components::confirm::{self, Choice};
use components::palette::{CommandPalette, Outcome};
use components::toast::Toast;
use components::{shortcuts, table};
use keys::Shortcut;
use page::{CommandRun, Confirmation, Intent, Page, PageCtx};
use store::{Connection, Live, Store};

pub struct FerrodockApp {
    worker: Worker,
    store: Store,
    pages: Vec<Box<dyn Page>>,
    active: usize,
    search: String,
    toast: Option<Toast>,
    /// A destructive action waiting for the user to confirm it.
    confirmation: Option<Confirmation>,
    /// The container being streamed, and whether it was running when the
    /// stream started (a restart needs a fresh stream).
    watching: Option<(String, bool)>,
    /// The ⌘K palette, while it's open.
    palette: Option<CommandPalette>,
    /// Labels of recently used palette entries, most recent first.
    recent: Vec<String>,
    show_help: bool,
    settings: settings::Settings,
    show_settings: bool,
    updater: crate::update::Updater,
}

impl FerrodockApp {
    pub fn new(cc: &eframe::CreationContext<'_>) -> Self {
        theme::install(&cc.egui_ctx);
        let settings: settings::Settings = cc
            .storage
            .and_then(|s| eframe::get_value(s, settings::STORAGE_KEY))
            .unwrap_or_default();
        settings.apply(&cc.egui_ctx);
        let updater = {
            let ctx = cc.egui_ctx.clone();
            crate::update::Updater::new(move || ctx.request_repaint())
        };
        if settings.check_updates {
            updater.check();
        }
        let ctx = cc.egui_ctx.clone();
        let worker = Worker::spawn(move || ctx.request_repaint());
        let pages = pages::all();
        // Load everything once so the sidebar counts are filled in.
        for page in &pages {
            worker.send(Request::Refresh(page.resource()));
        }
        // From now on the worker keeps the first page's data fresh by itself,
        // and the UI only wakes up when something changes.
        worker.send(Request::Poll(pages[0].resource()));
        Self {
            worker,
            store: Store::new(),
            pages,
            active: 0,
            search: String::new(),
            toast: None,
            confirmation: None,
            watching: None,
            palette: None,
            recent: Vec::new(),
            show_help: false,
            settings,
            show_settings: false,
            updater,
        }
    }

    /// Reloads the open page's data (plus the containers) right away.
    fn refresh(&mut self) {
        let resource = self.pages[self.active].resource();
        self.worker.send(Request::Refresh(resource));
    }

    fn open_page(&mut self, index: usize) {
        if index != self.active {
            self.pages[self.active].clear_selection();
            self.active = index;
            self.search.clear();
            // Loads the page's data now and keeps it fresh.
            self.worker
                .send(Request::Poll(self.pages[index].resource()));
        }
    }

    fn handle_responses(&mut self) {
        while let Some(response) = self.worker.try_recv() {
            if let Some(notice) = self.store.apply(response) {
                self.toast = Some(Toast::new(notice.text, notice.is_error));
            }
        }
    }

    /// Starts, switches or stops the live logs/stats stream to match what
    /// the open page wants to watch.
    fn sync_watch(&mut self) {
        let wanted = self.pages[self.active]
            .watched_container(&self.store)
            .map(|id| {
                let running = self.store.container(&id).is_some_and(|c| c.is_running());
                (id, running)
            });
        if wanted == self.watching {
            return;
        }
        let id = wanted.as_ref().map(|(id, _)| id.clone());
        self.store.live = id.clone().map(Live::new);
        self.worker.send(Request::Watch(id));
        self.watching = wanted;
    }

    /// Whether a dialog or overlay has the keyboard.
    fn overlay_open(&self) -> bool {
        self.palette.is_some()
            || self.pages[self.active].has_overlay()
            || self.show_help
            || self.show_settings
            || self.confirmation.is_some()
    }

    fn handle_shortcut(
        &mut self,
        ctx: &egui::Context,
        p: &'static theme::Palette,
        shortcut: Shortcut,
    ) {
        match shortcut {
            Shortcut::Palette => self.palette = Some(CommandPalette::default()),
            Shortcut::Help => self.show_help = true,
            Shortcut::Settings => self.show_settings = true,
            Shortcut::SelectAll => {
                let mut intents = Vec::new();
                let page_ctx = PageCtx::new(&self.store, p, &self.search, &mut intents);
                self.pages[self.active].select_all(&page_ctx);
            }
            Shortcut::Page(index) if index < self.pages.len() => self.open_page(index),
            Shortcut::Page(_) => {}
            Shortcut::Step(delta) => table::request_step(ctx, delta),
            Shortcut::CloseDetails => self.pages[self.active].clear_selection(),
            Shortcut::FocusSearch => ctx.memory_mut(|m| m.request_focus(shell::search_id())),
            Shortcut::Refresh => self.refresh(),
            Shortcut::PageKey(key) => {
                let mut intents = Vec::new();
                let mut page_ctx = PageCtx::new(&self.store, p, &self.search, &mut intents);
                self.pages[self.active].shortcut(key, &mut page_ctx);
                intents.into_iter().for_each(|i| self.handle_intent(i));
            }
        }
    }

    fn show_palette(&mut self, ui: &mut egui::Ui, p: &theme::Palette) {
        let Some(palette) = &mut self.palette else {
            return;
        };
        let (entries, mut targets): (Vec<_>, Vec<_>) =
            commands::build(&self.pages, &self.store, &self.recent)
                .into_iter()
                .map(|item| (item.entry, item.target))
                .unzip();
        match palette.show(ui, p, &entries) {
            Some(Outcome::Chosen(index)) => {
                self.palette = None;
                commands::remember(&mut self.recent, &entries[index].label);
                self.run_target(ui.ctx(), targets.swap_remove(index));
            }
            Some(Outcome::Closed) => self.palette = None,
            None => {}
        }
    }

    fn run_target(&mut self, ctx: &egui::Context, target: Target) {
        match target {
            Target::OpenPage(index) => self.open_page(index),
            Target::Page(index, run) => {
                self.open_page(index);
                match run {
                    CommandRun::Select(id) => {
                        self.pages[index].select(&id);
                        table::request_step(ctx, 0);
                    }
                    CommandRun::Intents(intents) => {
                        intents.into_iter().for_each(|i| self.handle_intent(i));
                    }
                }
            }
            Target::Refresh => self.refresh(),
            Target::Help => self.show_help = true,
            Target::Settings => self.show_settings = true,
        }
    }

    fn handle_intent(&mut self, intent: Intent) {
        match intent {
            Intent::Run(task) => self.run(task),
            Intent::Confirm(confirmation) => self.confirmation = Some(confirmation),
            Intent::Pull(image) => {
                self.store.pending.insert(image.clone(), Action::Pull);
                self.worker.send(Request::Pull(image));
            }
            Intent::RunImage(spec) => self.worker.send(Request::RunImage(spec)),
            Intent::Files(op) => {
                self.store.files_requested(&op);
                self.worker.send(Request::Files(op));
            }
            Intent::Change(change) => {
                let task = change.task();
                self.store.pending.insert(task.id, task.action);
                self.worker.send(Request::Change(change));
            }
            Intent::Processes(id) => {
                // Keep the old table on screen while the new one loads.
                let previous = self
                    .store
                    .processes
                    .take()
                    .filter(|(asked, _, _)| *asked == id)
                    .and_then(|(_, _, table)| table);
                self.store.processes = Some((id.clone(), std::time::Instant::now(), previous));
                self.worker.send(Request::Processes(id));
            }
            Intent::History(image) => {
                self.store.history = Some((image.clone(), None));
                self.worker.send(Request::History(image));
            }
            Intent::Notify { text, is_error } => self.toast = Some(Toast::new(text, is_error)),
            Intent::Open { page, id } => {
                if let Some(index) = self.pages.iter().position(|p| p.title() == page) {
                    self.open_page(index);
                    self.pages[index].select(&id);
                }
            }
            Intent::OpenPage(title) => {
                if let Some(index) = self.pages.iter().position(|p| p.title() == title) {
                    self.open_page(index);
                }
            }
            Intent::ClearSearch => self.search.clear(),
            Intent::Inspect(resource, id) => {
                self.store.inspection = None;
                self.store.inspecting = Some(id.clone());
                self.worker.send(Request::Inspect(resource, id));
            }
        }
    }

    fn run(&mut self, task: Task) {
        self.store.pending.insert(task.id.clone(), task.action);
        self.worker.send(Request::Run(task));
    }

    fn show_confirmation(&mut self, ui: &mut egui::Ui, p: &theme::Palette) {
        let Some(confirmation) = &self.confirmation else {
            return;
        };
        match confirm::show(ui, p, confirmation) {
            Some(Choice::Confirm) => {
                if let Some(confirmation) = self.confirmation.take() {
                    confirmation
                        .tasks
                        .into_iter()
                        .for_each(|task| self.run(task));
                }
            }
            Some(Choice::Cancel) => self.confirmation = None,
            None => {}
        }
    }
}

impl eframe::App for FerrodockApp {
    fn save(&mut self, storage: &mut dyn eframe::Storage) {
        eframe::set_value(storage, settings::STORAGE_KEY, &self.settings);
    }

    fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
        self.handle_responses();
        let p = theme::palette(ui);
        if !self.overlay_open() {
            for shortcut in keys::read(ui.ctx()) {
                self.handle_shortcut(ui.ctx(), p, shortcut);
            }
        }

        let clicked_page = egui::Panel::left("sidebar")
            .resizable(false)
            .exact_size(210.0)
            .frame(Frame::new().fill(p.sidebar).inner_margin(Margin::same(14)))
            .show(ui, |ui| {
                sidebar::show(
                    ui,
                    p,
                    &self.pages,
                    self.active,
                    &self.store,
                    &self.updater.status(),
                )
            })
            .inner;
        match clicked_page {
            Some(sidebar::Click::Page(index)) => self.open_page(index),
            Some(sidebar::Click::Settings) => self.show_settings = true,
            Some(sidebar::Click::Update) => {
                if let crate::update::Status::Available(available) = self.updater.status() {
                    self.updater.install(available);
                }
            }
            Some(sidebar::Click::Restart) => {
                if let crate::update::Status::Ready { relaunch, .. } = self.updater.status() {
                    match relaunch.start() {
                        Ok(()) => ui.ctx().send_viewport_cmd(egui::ViewportCommand::Close),
                        Err(e) => {
                            self.toast = Some(Toast::new(format!("Couldn't restart: {e}"), true));
                        }
                    }
                }
            }
            None => {}
        }

        let mut intents = Vec::new();
        let mut refresh = false;
        let page = &mut self.pages[self.active];

        if page.has_selection(&self.store) {
            egui::Panel::right("details")
                .default_size(340.0)
                .min_size(280.0)
                .frame(Frame::new().fill(p.sidebar).inner_margin(Margin::same(18)))
                .show(ui, |ui| {
                    let mut ctx = PageCtx::new(&self.store, p, &self.search, &mut intents);
                    page.show_details(ui, &mut ctx);
                });
        }

        egui::CentralPanel::default_margins()
            .frame(
                Frame::new()
                    .fill(p.bg)
                    .inner_margin(Margin::symmetric(22, 18)),
            )
            .show(ui, |ui| {
                let summary = page.summary(&self.store);
                // The toolbar sees this frame's search text; the box edits a copy.
                let mut search = self.search.clone();
                refresh = shell::header(
                    ui,
                    p,
                    page.title(),
                    &summary,
                    page.search_hint(),
                    &mut search,
                    |ui| {
                        if !self.store.is_disconnected() {
                            let mut ctx = PageCtx::new(&self.store, p, &self.search, &mut intents);
                            page.toolbar(ui, &mut ctx);
                        }
                    },
                );
                self.search = search;
                ui.add_space(12.0);
                if let Connection::Disconnected { reason } = &self.store.connection {
                    refresh |= shell::disconnected(ui, p, reason);
                } else if matches!(self.store.connection, Connection::Connecting) {
                    shell::connecting(ui, p);
                } else {
                    let mut ctx = PageCtx::new(&self.store, p, &self.search, &mut intents);
                    page.show(ui, &mut ctx);
                }
            });

        for intent in intents {
            self.handle_intent(intent);
        }
        self.sync_watch();
        if refresh {
            self.refresh();
        }
        table::clear_step(ui.ctx());
        self.show_confirmation(ui, p);
        self.show_palette(ui, p);
        if self.show_settings {
            self.show_settings = settings::show(ui, p, &mut self.settings, &self.updater);
        }
        if self.show_help {
            let titles: Vec<&'static str> = self.pages.iter().map(|page| page.title()).collect();
            self.show_help = shortcuts::show(ui, p, &keys::help(&titles));
        }
        if let Some(toast) = &self.toast
            && !toast.show(ui, p)
        {
            self.toast = None;
        }
    }
}