use eframe::egui::{self, Color32, RichText};
use crate::app::components::table;
use crate::app::components::widgets::{self, Mark};
use crate::docker::{Action, ContainerInfo};
use crate::theme::Palette;
pub(super) fn pill(ui: &mut egui::Ui, p: &Palette, c: &ContainerInfo, pending: Option<Action>) {
if let Some(label) = pending.and_then(transition) {
widgets::pill_with(ui, label, p.warning, Mark::Dot);
return;
}
widgets::pill_with(
ui,
&capitalize(&c.state),
color(p, &c.state),
mark(&c.state),
);
}
fn transition(action: Action) -> Option<&'static str> {
Some(match action {
Action::Start => "Starting…",
Action::Stop => "Stopping…",
Action::Restart => "Restarting…",
Action::Pause => "Pausing…",
Action::Unpause => "Resuming…",
Action::Remove(_) => "Removing…",
_ => return None,
})
}
pub(super) fn cell(ui: &mut egui::Ui, p: &Palette, c: &ContainerInfo, pending: Option<Action>) {
if table::is_compact(ui) {
pill(ui, p, c, pending);
return;
}
ui.vertical(|ui| {
ui.add_space(6.0);
pill(ui, p, c, pending);
if !c.status.eq_ignore_ascii_case(&c.state) {
ui.add(
egui::Label::new(RichText::new(&c.status).size(12.0).color(p.muted))
.truncate()
.selectable(false),
);
}
});
}
fn mark(state: &str) -> Mark {
match state {
"running" | "restarting" => Mark::Dot,
"paused" => Mark::Square,
"dead" => Mark::Triangle,
_ => Mark::Ring,
}
}
pub(super) fn color(p: &Palette, state: &str) -> Color32 {
match state {
"running" => p.success,
"paused" | "restarting" => p.warning,
"dead" => p.danger,
_ => p.muted,
}
}
fn capitalize(s: &str) -> String {
let mut chars = s.chars();
match chars.next() {
Some(first) => first.to_uppercase().chain(chars).collect(),
None => String::new(),
}
}