use gpui::{AnyElement, Context, SharedString, div, prelude::*, px};
use crate::app::Rosier;
use crate::assets::Icon;
use crate::i18n::{t, tf};
use crate::mpv::{Track, TrackKind};
use crate::pane;
use crate::theme;
use crate::ui::icon;
use crate::ui_controls::PILL_H;
impl Rosier {
pub(crate) fn fit_menu_el(&self, ix: usize, cx: &mut Context<Self>) -> AnyElement {
let current = self.panes[ix].fit;
self.menu_panel("fit-menu", ix, 220.0, cx, |this| this.fit_menu = None)
.children(pane::Fit::ALL.into_iter().map(|f| {
let label = t(match f {
pane::Fit::Contain => "fit.contain",
pane::Fit::Cover => "fit.cover",
pane::Fit::Stretch => "fit.stretch",
pane::Fit::Actual => "fit.actual",
});
menu_row(format!("fit-{ix}-{}", f.key()).into(), label.to_string(), f == current)
.on_click(cx.listener(move |this, _, _, cx| this.set_fit(ix, f, cx)))
}))
.into_any_element()
}
fn menu_panel(
&self,
id: &'static str,
ix: usize,
w: f32,
cx: &mut Context<Self>,
close: fn(&mut Self),
) -> gpui::Stateful<gpui::Div> {
div()
.id((id, ix))
.occlude()
.absolute()
.right(px(48.0))
.bottom(px(PILL_H + 8.0))
.w(px(w))
.p_1()
.flex()
.flex_col()
.rounded(px(theme::RADIUS + 4.0))
.bg(gpui::rgba(0x1c1c21f2))
.border_1()
.border_color(theme::glass_edge())
.shadow_lg()
.on_mouse_down_out(cx.listener(move |this, ev, _, cx| {
this.menu_closed_by(ev);
close(this);
cx.notify();
}))
}
pub(crate) fn book_menu_el(&self, ix: usize, cx: &mut Context<Self>) -> AnyElement {
use crate::book::Layout;
let pane = &self.panes[ix];
let cur = pane.layout;
let base = cur.unwrap_or_default();
let mut rows: Vec<(&str, bool, Option<Layout>)> = Vec::new();
if pane.book.is_none() {
rows.push(("book.off", cur.is_none(), None));
}
rows.extend([
(
"book.single",
cur.is_some_and(|l| !l.spread && !l.vertical),
Some(Layout { spread: false, vertical: false, ..base }),
),
(
"book.spread",
cur.is_some_and(|l| l.spread && !l.vertical),
Some(Layout { spread: true, vertical: false, ..base }),
),
("book.vertical", cur.is_some_and(|l| l.vertical), Some(Layout { vertical: true, ..base })),
("book.rtl", cur.is_some_and(|l| l.rtl), Some(Layout { rtl: true, ..base })),
("book.ltr", cur.is_some_and(|l| !l.rtl), Some(Layout { rtl: false, ..base })),
]);
self.menu_panel("book-menu", ix, 220.0, cx, |this| this.book_menu = None)
.children(rows.into_iter().map(|(key, on, layout)| {
menu_row(format!("{key}-{ix}").into(), t(key).to_string(), on).on_click(cx.listener(
move |this, _, _, cx| {
this.book_menu = None;
this.set_layout(ix, layout, cx);
},
))
}))
.into_any_element()
}
pub(crate) fn track_menu_el(&self, ix: usize, cx: &mut Context<Self>) -> AnyElement {
let tracks = &self.panes[ix].tracks;
let row = menu_row;
let heading =
|k: &'static str| div().px_2().pt_1().pb_0p5().text_xs().text_color(theme::text_faint()).child(t(k));
let mut menu = self
.menu_panel("track-menu", ix, 240.0, cx, |this| this.track_menu = None)
.max_h(px(320.0))
.overflow_y_scroll();
let audio: Vec<&Track> = tracks.iter().filter(|t| t.kind == TrackKind::Audio).collect();
if audio.len() > 1 {
menu = menu.child(heading("tracks.audio"));
for t in audio {
let id = t.id;
menu =
menu.child(row(format!("aid-{ix}-{id}").into(), track_label(t), t.selected).on_click(
cx.listener(move |this, _, _, cx| this.set_track(ix, TrackKind::Audio, Some(id), cx)),
));
}
}
let subs: Vec<&Track> = tracks.iter().filter(|t| t.kind == TrackKind::Sub).collect();
if !subs.is_empty() {
let none_on = !subs.iter().any(|t| t.selected);
menu = menu.child(heading("tracks.subs")).child(
row(format!("sid-{ix}-off").into(), t("tracks.off").to_string(), none_on)
.on_click(cx.listener(move |this, _, _, cx| this.set_track(ix, TrackKind::Sub, None, cx))),
);
for t in subs {
let id = t.id;
menu = menu.child(
row(format!("sid-{ix}-{id}").into(), track_label(t), t.selected)
.on_click(cx.listener(move |this, _, _, cx| this.set_track(ix, TrackKind::Sub, Some(id), cx))),
);
}
}
menu.into_any_element()
}
}
fn menu_row(id: SharedString, label: String, on: bool) -> gpui::Stateful<gpui::Div> {
div()
.id(id)
.h(px(theme::ROW_H))
.px_2()
.flex()
.flex_row()
.items_center()
.gap_2()
.rounded(px(theme::R_SMALL))
.text_sm()
.cursor_pointer()
.hover(|s| s.bg(theme::raised_hover()))
.child(div().w(px(16.0)).flex_none().when(on, |d| d.child(icon(Icon::Check, 14.0, theme::accent()))))
.child(
div()
.flex_grow()
.min_w_0()
.truncate()
.text_color(if on { theme::text() } else { theme::text_dim() })
.child(label),
)
}
fn track_label(tr: &Track) -> String {
let lang = tr.lang.as_deref().map(language_name);
match (&tr.title, lang) {
(Some(title), Some(lang)) => format!("{title} ({lang})"),
(Some(title), None) => title.clone(),
(None, Some(lang)) => lang,
(None, None) => tf("tracks.n", &[("n", tr.id.to_string())]),
}
}
fn language_name(code: &str) -> String {
let name = match code.to_ascii_lowercase().as_str() {
"en" | "eng" => "English",
"ja" | "jpn" => "日本語",
"de" | "ger" | "deu" => "Deutsch",
"es" | "spa" => "Español",
"fr" | "fre" | "fra" => "Français",
"it" | "ita" => "Italiano",
"ko" | "kor" => "한국어",
"pt" | "por" => "Português",
"ru" | "rus" => "Русский",
"zh" | "chi" | "zho" => "中文",
"nl" | "dut" | "nld" => "Nederlands",
"sv" | "swe" => "Svenska",
"pl" | "pol" => "Polski",
"tr" | "tur" => "Türkçe",
"ar" | "ara" => "العربية",
"hi" | "hin" => "हिन्दी",
"th" | "tha" => "ไทย",
"und" => return t("tracks.unknown").to_string(),
_ => code,
};
name.to_string()
}
#[cfg(test)]
mod tests {
use super::*;
fn track(id: i64, lang: Option<&str>, title: Option<&str>) -> Track {
Track {
kind: TrackKind::Audio,
id,
lang: lang.map(String::from),
title: title.map(String::from),
selected: false,
}
}
#[test]
fn tracks_are_named_by_title_and_language() {
assert_eq!(track_label(&track(1, Some("jpn"), None)), "日本語");
assert_eq!(track_label(&track(2, Some("en"), Some("Commentary"))), "Commentary (English)");
assert_eq!(track_label(&track(3, Some("xyz"), None)), "xyz");
assert_eq!(track_label(&track(4, None, Some("Director"))), "Director");
}
}