rosier 0.2.0

A fast picture and video viewer for large folders
//! The shortcut sheet (?): keycaps next to what they do, in two groups.

use gpui::{AnyElement, Context, div, prelude::*, px};

use crate::app::Rosier;
use crate::i18n::t;
use crate::theme;
use crate::ui::{glass, glass_sheen};

const BROWSE: &[(&[&str], &str)] = &[
    (&["←", "↑", "↓", "→"], "help.move"),
    (&["PgUp", "PgDn"], "help.page"),
    (&["Enter"], "help.open"),
    (&["F"], "help.fav"),
    (&["Ctrl", "Wheel"], "help.tiles"),
    (&["Ctrl", "B"], "help.sidebar"),
    (&["1", "2", "4"], "help.split"),
];

const PLAYER: &[(&[&str], &str)] = &[
    (&["Space"], "help.play"),
    (&["←", "→"], "help.prevnext"),
    (&["Wheel"], "help.zoom"),
    (&["0"], "help.zoomreset"),
    (&["[", "]"], "help.seek"),
    (&["↑", "↓"], "help.volume"),
    (&["−", "+"], "help.interval"),
    (&["Tab"], "help.nextpane"),
    (&["R"], "help.shuffle"),
    (&["P"], "help.pip"),
    (&["F11"], "help.full"),
    (&["Esc"], "help.back"),
];

fn keycap(k: &str) -> gpui::Div {
    div()
        .flex_none()
        .min_w(px(22.0))
        .h(px(22.0))
        .px_1p5()
        .flex()
        .items_center()
        .justify_center()
        .rounded(px(theme::R_SMALL))
        .bg(theme::raised())
        .border_1()
        .border_color(theme::glass_edge())
        .text_xs()
        .text_color(theme::text())
        .child(key_label(k))
}

fn group(title: &'static str, rows: &[(&[&str], &'static str)]) -> gpui::Div {
    div()
        .flex()
        .flex_col()
        .gap_2()
        .w(px(360.0))
        .child(div().pb_1().text_xs().text_color(theme::text_faint()).child(t(title)))
        .children(rows.iter().map(|(keys, label)| {
            div()
                .flex()
                .flex_row()
                .items_center()
                .gap_3()
                // Long labels (German, Russian) wrap onto a second line
                // instead of running into the other column.
                .min_h(px(28.0))
                .child(
                    div().w(px(132.0)).flex_none().flex().flex_row().gap_1().children(keys.iter().map(|k| keycap(k))),
                )
                .child(div().flex_grow().min_w_0().text_sm().text_color(theme::text_dim()).child(t(label)))
        }))
}

impl Rosier {
    pub(crate) fn render_help(&mut self, cx: &mut Context<Self>) -> AnyElement {
        div()
            .id("help")
            .absolute()
            .top_0()
            .left_0()
            .size_full()
            .flex()
            .items_center()
            .justify_center()
            .bg(gpui::rgba(0x000000a8))
            .on_click(cx.listener(|this, _, _, cx| {
                this.show_help = false;
                cx.notify();
            }))
            .child(
                // A reading surface: denser than the floating panels.
                glass(theme::R_PANEL)
                    .bg(gpui::rgba(0x18181cf0))
                    .p_6()
                    .flex()
                    .flex_col()
                    .gap_5()
                    .child(glass_sheen(theme::R_PANEL))
                    .child(div().text_lg().child(t("help.title")))
                    .child(
                        div()
                            .flex()
                            .flex_row()
                            .gap_8()
                            .child(group("help.browse", BROWSE))
                            .child(group("help.player", PLAYER)),
                    )
                    .child(div().text_xs().text_color(theme::text_faint()).child(t("help.close"))),
            )
            .into_any_element()
    }
}

/// Key names as printed on the reader's keyboard (Strg, Entrée, …), and
/// "Wheel" in their language.
fn key_label(k: &str) -> String {
    use crate::i18n::t;
    match k {
        "Wheel" => t("key.wheel"),
        "Ctrl" => t("key.ctrl"),
        "Space" => t("key.space"),
        "Enter" => t("key.enter"),
        "Esc" => t("key.esc"),
        "PgUp" => t("key.pgup"),
        "PgDn" => t("key.pgdn"),
        other => other,
    }
    .to_string()
}