ferrodock 1.13.2

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
//! The menu bar (macOS) / notification area (Windows) icon: how many
//! containers run, and a menu to start, stop and restart them without
//! opening the window.
//!
//! The menu is described here as plain data, built from the store, so it
//! can be tested anywhere; `native` turns it into a real menu on the
//! platforms that have one. Linux has no tray yet (it would need GTK).

#[cfg(any(target_os = "macos", target_os = "windows"))]
mod native;

use crate::app::store::{Connection, Store};
use crate::docker::{Action, ContainerInfo, TEMPLATES, Task};

/// Items listed per section; the rest are a click away in the window.
const MAX_ITEMS: usize = 20;

/// One menu entry.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Entry {
    Item {
        id: String,
        label: String,
        enabled: bool,
    },
    Separator,
    Menu {
        label: String,
        items: Vec<Entry>,
    },
}

/// What a click in the menu asks for.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TrayAction {
    /// Show the window.
    Open,
    /// Show the window with an item selected on a page.
    Show {
        page: &'static str,
        id: String,
    },
    /// Show the window on a page.
    OpenPage(&'static str),
    /// Run the Quick start template with this name, with its defaults.
    QuickStart(&'static str),
    Run(Vec<Task>),
    Quit,
}

fn item(id: impl Into<String>, label: impl Into<String>) -> Entry {
    Entry::Item {
        id: id.into(),
        label: label.into(),
        enabled: true,
    }
}

fn note(label: impl Into<String>) -> Entry {
    Entry::Item {
        id: String::new(),
        label: label.into(),
        enabled: false,
    }
}

/// The text next to the icon (macOS) and in its tooltip.
pub fn title(store: &Store) -> String {
    match store.connection {
        Connection::Connected { .. } => store.running_count().to_string(),
        _ => "–".into(),
    }
}

/// The whole menu, from what the store knows.
pub fn model(store: &Store) -> Vec<Entry> {
    let mut menu = vec![item("open", "Open Ferrodock"), Entry::Separator];
    match &store.connection {
        Connection::Connected { version } => {
            menu.push(note(format!(
                "Docker {version} · {} running",
                store.running_count()
            )));
        }
        _ => menu.push(note("Docker isn't running")),
    }

    // The same sections as the sidebar.
    menu.push(Entry::Separator);
    let mut containers: Vec<&ContainerInfo> = store.containers.iter().collect();
    // Running first, then by name (the store's order).
    containers.sort_by_key(|c| !c.is_active());
    let mut items: Vec<Entry> = containers
        .iter()
        .take(MAX_ITEMS)
        .map(|c| Entry::Menu {
            label: format!("{} {}", dot(c.is_active()), c.name),
            items: container_items(c),
        })
        .collect();
    if containers.len() > MAX_ITEMS {
        items.push(note(format!(
            "{} more in the window",
            containers.len() - MAX_ITEMS
        )));
    }
    menu.push(section("Containers", items));

    if !store.projects.is_empty() {
        let projects = store
            .projects
            .iter()
            .map(|project| {
                let running = project.running();
                let mut items = vec![note(format!(
                    "{running} of {} running",
                    project.containers.len()
                ))];
                if running < project.containers.len() {
                    items.push(item(format!("up:{}", project.name), "Start all"));
                }
                if running > 0 {
                    items.push(item(format!("down:{}", project.name), "Stop all"));
                }
                Entry::Menu {
                    label: format!("{} {}", dot(running > 0), project.name),
                    items,
                }
            })
            .collect();
        menu.push(Entry::Menu {
            label: "Compose".into(),
            items: projects,
        });
    }
    let images = store
        .images
        .iter()
        .map(|i| item(format!("image:{}", i.id), i.name()));
    menu.push(section("Images", listed(images.collect())));
    let volumes = store
        .volumes
        .iter()
        .map(|v| item(format!("volume:{}", v.name), &v.name));
    menu.push(section("Volumes", listed(volumes.collect())));
    let networks = store
        .networks
        .iter()
        .map(|n| item(format!("network:{}", n.id), &n.name));
    menu.push(section("Networks", listed(networks.collect())));

    menu.push(Entry::Separator);
    let mut templates: Vec<Entry> = TEMPLATES
        .iter()
        .map(|t| {
            let label = if crate::app::pages::quick_start::running(store, t).is_some() {
                format!("● {}", t.name)
            } else {
                t.name.to_owned()
            };
            item(format!("quick:{}", t.name), label)
        })
        .collect();
    templates.extend([Entry::Separator, item("quick-start", "Customize…")]);
    menu.push(Entry::Menu {
        label: "Quick start".into(),
        items: templates,
    });

    menu.extend([Entry::Separator, item("quit", "Quit Ferrodock")]);
    menu
}

/// A submenu, or a dimmed line when there's nothing in it.
fn section(label: &str, items: Vec<Entry>) -> Entry {
    if items.is_empty() {
        return note(format!("No {}", label.to_lowercase()));
    }
    Entry::Menu {
        label: label.into(),
        items,
    }
}

/// The first [`MAX_ITEMS`] of `items`, with a note for the rest.
fn listed(mut items: Vec<Entry>) -> Vec<Entry> {
    if items.len() > MAX_ITEMS {
        let rest = items.len() - MAX_ITEMS;
        items.truncate(MAX_ITEMS);
        items.push(note(format!("{rest} more in the window")));
    }
    items
}

fn dot(running: bool) -> &'static str {
    if running { "●" } else { "○" }
}

fn container_items(c: &ContainerInfo) -> Vec<Entry> {
    let mut items = Vec::new();
    if c.is_active() {
        items.push(item(format!("stop:{}", c.id), "Stop"));
        items.push(item(format!("restart:{}", c.id), "Restart"));
    } else {
        items.push(item(format!("start:{}", c.id), "Start"));
    }
    items.push(Entry::Separator);
    items.push(item(format!("show:{}", c.id), "Show in Ferrodock"));
    items
}

/// What the menu item `id` asks for, or None if what it refers to is gone.
pub fn action(store: &Store, id: &str) -> Option<TrayAction> {
    let task = |action: Action, c: &ContainerInfo| Task {
        action,
        id: c.id.clone(),
        name: c.name.clone(),
    };
    match id.split_once(':') {
        None => match id {
            "open" => Some(TrayAction::Open),
            "quit" => Some(TrayAction::Quit),
            "quick-start" => Some(TrayAction::OpenPage("Quick start")),
            _ => None,
        },
        Some(("quick", name)) => {
            crate::docker::template(name).map(|t| TrayAction::QuickStart(t.name))
        }
        Some((kind @ ("image" | "volume" | "network"), id)) => {
            let page = match kind {
                "image" => "Images",
                "volume" => "Volumes",
                _ => "Networks",
            };
            Some(TrayAction::Show {
                page,
                id: id.to_owned(),
            })
        }
        Some((verb @ ("up" | "down"), name)) => {
            let project = store.projects.iter().find(|p| p.name == name)?;
            let (action, wanted) = if verb == "up" {
                (Action::Start, false)
            } else {
                (Action::Stop, true)
            };
            let tasks = project
                .containers
                .iter()
                .filter(|c| c.is_active() == wanted)
                .map(|c| task(action, c))
                .collect();
            Some(TrayAction::Run(tasks))
        }
        Some((verb, container)) => {
            let c = store.container(container)?;
            let action = match verb {
                "start" => Action::Start,
                "stop" => Action::Stop,
                "restart" => Action::Restart,
                "show" => {
                    return Some(TrayAction::Show {
                        page: "Containers",
                        id: c.id.clone(),
                    });
                }
                _ => return None,
            };
            Some(TrayAction::Run(vec![task(action, c)]))
        }
    }
}

/// The icon and its menu, where the platform has one.
pub struct Tray {
    #[cfg(any(target_os = "macos", target_os = "windows"))]
    native: native::NativeTray,
    /// The menu last shown, to rebuild it only when it changes.
    shown: Vec<Entry>,
    title: String,
}

impl Tray {
    /// Whether this platform has a tray.
    pub const SUPPORTED: bool = cfg!(any(target_os = "macos", target_os = "windows"));

    /// Adds the icon. None where there's no tray, or if it can't be made.
    pub fn new(ctx: &eframe::egui::Context) -> Option<Self> {
        #[cfg(any(target_os = "macos", target_os = "windows"))]
        {
            match native::NativeTray::new(ctx) {
                Ok(native) => Some(Self {
                    native,
                    shown: Vec::new(),
                    title: String::new(),
                }),
                Err(e) => {
                    eprintln!("ferrodock: no tray icon: {e}");
                    None
                }
            }
        }
        #[cfg(not(any(target_os = "macos", target_os = "windows")))]
        {
            let _ = ctx;
            None
        }
    }

    /// Brings the icon and menu up to date, and returns what was clicked.
    pub fn update(&mut self, store: &Store) -> Vec<TrayAction> {
        let menu = model(store);
        let title = title(store);
        #[cfg(any(target_os = "macos", target_os = "windows"))]
        {
            if menu != self.shown {
                self.native.set_menu(&menu);
            }
            if title != self.title {
                self.native.set_title(&title);
            }
        }
        self.shown = menu;
        self.title = title;
        #[cfg(any(target_os = "macos", target_os = "windows"))]
        let clicked = self.native.clicked();
        #[cfg(not(any(target_os = "macos", target_os = "windows")))]
        let clicked: Vec<String> = Vec::new();
        clicked.iter().filter_map(|id| action(store, id)).collect()
    }
}

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

    fn container(id: &str, name: &str, running: bool) -> ContainerInfo {
        ContainerInfo {
            id: id.into(),
            name: name.into(),
            state: if running { "running" } else { "exited" }.into(),
            ..Default::default()
        }
    }

    fn store() -> Store {
        let mut store = Store::new();
        store.connection = Connection::Connected {
            version: "29.0".into(),
        };
        store.containers = vec![container("a1", "api", false), container("w1", "web", true)];
        store
    }

    #[test]
    fn lists_running_containers_first() {
        let menu = model(&store());
        let labels = |entries: &[Entry]| -> Vec<String> {
            entries
                .iter()
                .filter_map(|e| match e {
                    Entry::Menu { label, .. } => Some(label.clone()),
                    _ => None,
                })
                .collect()
        };
        // The sidebar's sections; empty ones are a dimmed line.
        assert_eq!(labels(&menu), ["Containers", "Quick start"]);
        assert!(menu.contains(&note("No images")));
        let Some(Entry::Menu { items, .. }) = menu
            .iter()
            .find(|e| matches!(e, Entry::Menu { label, .. } if label == "Containers"))
        else {
            panic!("expected a Containers submenu");
        };
        assert_eq!(labels(items), ["● web", "○ api"]);
        assert!(menu.contains(&note("Docker 29.0 · 1 running")));
        assert_eq!(title(&store()), "1");
        assert_eq!(
            action(&store(), "quick:Postgres"),
            Some(TrayAction::QuickStart("Postgres"))
        );
    }

    #[test]
    fn clicks_become_actions() {
        let store = store();
        assert_eq!(action(&store, "open"), Some(TrayAction::Open));
        let Some(TrayAction::Run(tasks)) = action(&store, "stop:w1") else {
            panic!("stop should run a task");
        };
        assert_eq!(
            (tasks[0].action, tasks[0].name.as_str()),
            (Action::Stop, "web")
        );
        assert_eq!(
            action(&store, "show:a1"),
            Some(TrayAction::Show {
                page: "Containers",
                id: "a1".into()
            })
        );
        assert_eq!(action(&store, "stop:gone"), None);
    }
}