flowcull 0.1.0

Fast photo culling with raw/JPEG pair support
//! Dark theme and the small painted widgets the sidebar and filmstrip use.

use eframe::egui::{self, Color32};

use crate::catalog::Label;

pub const BG: Color32 = Color32::from_rgb(18, 18, 18);
pub const PANEL: Color32 = Color32::from_rgb(30, 30, 30);
const ACCENT: Color32 = Color32::from_rgb(90, 150, 240);

pub fn card(ui: &mut egui::Ui, title: &str, add_contents: impl FnOnce(&mut egui::Ui)) {
    egui::Frame::new()
        .fill(Color32::from_rgb(38, 38, 38))
        .corner_radius(8)
        .inner_margin(egui::Margin::same(12))
        .show(ui, |ui| {
            ui.set_width(ui.available_width());
            ui.spacing_mut().item_spacing.y = 8.0;
            ui.label(
                egui::RichText::new(title.to_ascii_uppercase())
                    .size(11.0)
                    .color(Color32::from_rgb(140, 140, 140)),
            );
            add_contents(ui);
        });
}

pub fn label_chip(ui: &mut egui::Ui, label: Label) {
    let (rect, _) =
        ui.allocate_exact_size(egui::vec2(ui.available_width(), 28.0), egui::Sense::hover());
    paint_label_chip(ui.painter(), rect, label);
}

pub fn paint_label_chip(painter: &egui::Painter, rect: egui::Rect, label: Label) {
    let (text, fill, fg) = match label {
        Label::Pick => ("PICKED", label_color(Label::Pick), Color32::WHITE),
        Label::Reject => ("REJECTED", label_color(Label::Reject), Color32::WHITE),
        Label::Unlabeled => (
            "UNLABELED",
            Color32::from_rgb(50, 50, 50),
            Color32::from_rgb(140, 140, 140),
        ),
    };
    painter.rect_filled(rect, 6, fill);
    painter.text(
        rect.center(),
        egui::Align2::CENTER_CENTER,
        text,
        egui::FontId::proportional(13.0),
        fg,
    );
}

pub fn key_cap(ui: &mut egui::Ui, key: &str) {
    let galley = ui.painter().layout_no_wrap(
        key.to_string(),
        egui::FontId::monospace(11.0),
        Color32::from_rgb(200, 200, 200),
    );
    let size = galley.size() + egui::vec2(12.0, 6.0);
    let (rect, _) = ui.allocate_exact_size(size, egui::Sense::hover());
    ui.painter()
        .rect_filled(rect, 4, Color32::from_rgb(55, 55, 55));
    ui.painter().rect_stroke(
        rect,
        4,
        egui::Stroke::new(1.0, Color32::from_rgb(75, 75, 75)),
        egui::StrokeKind::Inside,
    );
    ui.painter()
        .galley(rect.min + egui::vec2(6.0, 3.0), galley, Color32::WHITE);
}

pub fn count_dot(ui: &mut egui::Ui, color: Color32, n: usize, what: &str) {
    let (rect, _) = ui.allocate_exact_size(egui::vec2(10.0, 10.0), egui::Sense::hover());
    ui.painter().circle_filled(rect.center(), 5.0, color);
    ui.label(format!("{n} {what}"));
}

pub fn label_glyph(label: Label) -> &'static str {
    match label {
        Label::Pick => "P",
        Label::Reject => "X",
        Label::Unlabeled => "",
    }
}

pub fn label_color(label: Label) -> Color32 {
    match label {
        Label::Pick => Color32::from_rgb(98, 168, 110),
        Label::Reject => Color32::from_rgb(208, 96, 92),
        Label::Unlabeled => Color32::GRAY,
    }
}

/// Flat dark theme in the style of photo tools: near-black canvas, muted panels, few borders.
pub fn apply_style(ctx: &egui::Context) {
    ctx.set_theme(egui::Theme::Dark);
    ctx.style_mut_of(egui::Theme::Dark, |style| {
        let v = &mut style.visuals;
        v.panel_fill = PANEL;
        v.window_fill = PANEL;
        v.extreme_bg_color = BG;
        v.faint_bg_color = Color32::from_rgb(38, 38, 38);
        v.window_stroke = egui::Stroke::new(1.0, Color32::from_rgb(50, 50, 50));
        v.window_corner_radius = 8.into();
        v.selection.bg_fill = ACCENT.gamma_multiply(0.6);
        v.hyperlink_color = ACCENT;
        let fg = Color32::from_rgb(220, 220, 220);
        for w in [
            &mut v.widgets.noninteractive,
            &mut v.widgets.inactive,
            &mut v.widgets.hovered,
            &mut v.widgets.active,
            &mut v.widgets.open,
        ] {
            w.corner_radius = 6.into();
            w.bg_stroke = egui::Stroke::NONE;
            w.fg_stroke.color = fg;
        }
        v.widgets.noninteractive.fg_stroke.color = Color32::from_rgb(170, 170, 170);
        v.widgets.inactive.weak_bg_fill = Color32::from_rgb(45, 45, 45);
        v.widgets.hovered.weak_bg_fill = Color32::from_rgb(60, 60, 60);
        v.widgets.active.weak_bg_fill = ACCENT.gamma_multiply(0.5);
        v.widgets.open.weak_bg_fill = Color32::from_rgb(50, 50, 50);
        style.spacing.item_spacing = egui::vec2(10.0, 6.0);
        style.spacing.button_padding = egui::vec2(12.0, 6.0);
        style.spacing.interact_size.y = 28.0;
    });
}