use std::cell::Cell;
use std::rc::Rc;
use gpui::{
AnyElement, Bounds, Context, MouseButton, MouseDownEvent, MouseMoveEvent, Pixels, SharedString, canvas, div, img,
prelude::*, px,
};
use crate::app::Rosier;
use crate::assets::Icon;
use crate::i18n::{t, tf};
use crate::mpv::TrackKind;
use crate::pane::{self, clock};
use crate::theme;
use crate::ui::icon;
use crate::ui_split::notches;
pub(crate) const PILL_H: f32 = 48.0;
#[derive(Clone, Copy)]
pub(crate) struct SliderStyle {
pub track: gpui::Rgba,
pub fill: gpui::Rgba,
pub knob: gpui::Rgba,
}
impl SliderStyle {
pub fn on_glass() -> Self {
SliderStyle { track: gpui::rgba(0xffffff40), fill: gpui::rgba(0xffffffff), knob: gpui::rgba(0xffffffff) }
}
pub fn on_chrome() -> Self {
SliderStyle { track: theme::raised_hover(), fill: theme::accent(), knob: gpui::rgba(0xf4f4f8ff) }
}
}
fn pill_geometry(w: f32, h: f32) -> (f32, f32, f32) {
let pill_w = (w - 24.0).clamp(120.0, 840.0);
let left = (w - pill_w) / 2.0;
let top = (h - PILL_H - 16.0).max(8.0);
(left, top, pill_w)
}
fn glass_tint() -> gpui::Rgba {
gpui::rgba(0x0c0c1038)
}
fn glass_edge() -> gpui::Rgba {
gpui::rgba(0xffffff3a)
}
fn glass_fallback() -> gpui::Rgba {
gpui::rgba(0x1c1c22c0)
}
pub(crate) fn counter(pane: &pane::Pane) -> String {
let (start, n) = match &pane.book {
Some((_, pages)) => (pane.page, pages.len()),
None => (pane.pos, pane.items.len()),
};
match pane.layout.map(|l| l.view(start, n)) {
Some(v) if v.len() == 2 => format!("{}-{} / {n}", v[0] + 1, v[1] + 1),
_ => format!("{} / {n}", start + 1),
}
}
const PREVIEW_W: f32 = 168.0;
fn seek_marks(profile: Option<Vec<f32>>, chapters: &[f64], dur: f64) -> AnyElement {
let ridge = profile.map(|p| {
div()
.absolute()
.left_0()
.bottom(px(14.0))
.w_full()
.h(px(9.0))
.flex()
.flex_row()
.items_end()
.children(p.into_iter().map(|v| div().flex_1().h(px(9.0 * v.max(0.08))).bg(gpui::rgba(0xffffff73))))
});
let notches = chapters.iter().filter(|&&t| dur > 0.0 && t > 0.0 && t < dur).map(|&t| {
div()
.absolute()
.top(px(8.0))
.left(gpui::relative((t / dur) as f32))
.w(px(2.0))
.h(px(8.0))
.rounded_full()
.bg(gpui::rgba(0x000000d0))
});
div().absolute().top_0().left_0().size_full().children(ridge).children(notches).into_any_element()
}
impl Rosier {
pub(crate) fn star_row(&self, ix: usize, stars: u8, cx: &mut Context<Self>) -> AnyElement {
div()
.flex_none()
.flex()
.flex_row()
.items_center()
.px_1()
.children((1..=5u8).map(|n| {
let lit = n <= stars;
div()
.id(SharedString::from(format!("star-{ix}-{n}")))
.size(px(22.0))
.flex()
.items_center()
.justify_center()
.rounded_full()
.cursor_pointer()
.hover(|s| s.bg(gpui::rgba(0xffffff30)))
.child(icon(
if lit { Icon::StarFill } else { Icon::Star },
15.0,
if lit { theme::star() } else { gpui::rgba(0xffffffa0) },
))
.on_click(cx.listener(move |this, _, _, cx| {
if let Some(i) = this.panes[ix].current() {
let now = this.lib.rating(i);
this.set_rating(i, if now == n { 0 } else { n }, cx);
}
}))
}))
.into_any_element()
}
pub(crate) fn glass_controls(&self, ix: usize, w: f32, h: f32, cx: &mut Context<Self>) -> AnyElement {
let pane = &self.panes[ix];
let (pill_left, pill_top, pill_w) = pill_geometry(w, h);
let is_video = pane.video.is_some();
let blur = pane.glass.as_ref().map(|g| {
img(g.clone()).absolute().top_0().left_0().size_full().rounded_full().object_fit(gpui::ObjectFit::Fill)
});
let shown = self.controls_visible(ix);
let round_btn = |id: SharedString, ic: Icon, size: f32, tip: &'static str| {
crate::ui::round_button(id, 32.0)
.when(shown, |d| d.tooltip(crate::ui::tip(t(tip))))
.hover(|s| s.bg(gpui::rgba(0xffffff24)))
.child(icon(ic, size, gpui::white()))
};
let toggle_btn = |id: SharedString, ic: Icon, on: bool, tip: &'static str| {
crate::ui::round_button(id, 32.0)
.when(shown, |d| d.tooltip(crate::ui::tip(t(tip))))
.when(on, |d| d.bg(gpui::rgba(0xffffff26)))
.hover(|s| s.bg(gpui::rgba(0xffffff30)))
.child(icon(ic, 17.0, if on { theme::accent_hover() } else { gpui::rgba(0xffffffd0) }))
};
let playing = pane.playing();
let mut row = div()
.relative()
.size_full()
.flex()
.flex_row()
.items_center()
.gap_1()
.px_2()
.text_xs()
.text_color(gpui::white())
.child(
round_btn(format!("prev-{ix}").into(), Icon::Prev, 16.0, "tip.prev")
.on_click(cx.listener(move |this, _, _, cx| this.advance(ix, -1, cx))),
)
.child(
round_btn(
format!("play-{ix}").into(),
if playing { Icon::Pause } else { Icon::Play },
18.0,
"help.play",
)
.on_click(cx.listener(move |this, _, _, cx| {
this.panes[ix].play_pause();
cx.notify();
})),
)
.child(
round_btn(format!("next-{ix}").into(), Icon::Next, 16.0, "tip.next")
.on_click(cx.listener(move |this, _, _, cx| this.advance(ix, 1, cx))),
);
if is_video {
let (pos, dur) = pane.time;
row = row
.child(div().flex_none().w(px(52.0)).text_right().child(clock(pos)))
.child(self.seek_bar(ix, cx))
.child(div().flex_none().w(px(52.0)).text_color(gpui::rgba(0xffffffb0)).child(clock(dur)))
.children((pane.speed != 1.0).then(|| div().flex_none().text_sm().child(format!("{}×", pane.speed))));
} else {
let secs = pane.interval.as_secs();
let step_btn = |id: SharedString, ic: Icon, tip: &'static str| {
div()
.id(id)
.when(shown, |d| d.tooltip(crate::ui::tip(t(tip))))
.size(px(24.0))
.flex_none()
.flex()
.items_center()
.justify_center()
.rounded_full()
.cursor_pointer()
.hover(|s| s.bg(gpui::rgba(0xffffff24)))
.child(icon(ic, 14.0, gpui::rgba(0xffffffd0)))
};
row = row
.child(
div()
.flex_grow()
.min_w_0()
.px_2()
.truncate()
.text_color(gpui::rgba(0xffffffd0))
.child(counter(pane)),
)
.child(
div()
.flex()
.flex_row()
.flex_none()
.items_center()
.gap_1()
.px_1()
.h(px(30.0))
.rounded_full()
.bg(gpui::rgba(0xffffff12))
.child(step_btn(format!("slower-{ix}").into(), Icon::Minus, "help.interval").on_click(
cx.listener(move |this, _, _, cx| {
this.panes[ix].step_interval(-1);
cx.notify();
}),
))
.child(icon(Icon::Timer, 14.0, gpui::rgba(0xffffffb0)))
.child(div().w(px(36.0)).text_center().child(tf("pane.secs", &[("n", secs.to_string())])))
.child(step_btn(format!("faster-{ix}").into(), Icon::Plus, "help.interval").on_click(
cx.listener(move |this, _, _, cx| {
this.panes[ix].step_interval(1);
cx.notify();
}),
)),
);
}
let fav = pane.current().is_some_and(|i| self.lib.is_favorite(i));
row = row
.child(
toggle_btn(
format!("fav-{ix}").into(),
if fav { Icon::HeartFill } else { Icon::Heart },
fav,
"help.fav",
)
.on_click(cx.listener(move |this, _, _, cx| {
if let Some(i) = this.panes[ix].current() {
this.toggle_favorite(i, cx);
}
})),
)
.children(pane.current().map(|i| self.star_row(ix, self.lib.rating(i), cx)))
.children((!is_video).then(|| {
toggle_btn(format!("lock-{ix}").into(), Icon::Lock, pane.lock_view, "help.lockview")
.on_click(cx.listener(move |this, _, _, cx| this.toggle_lock_view(ix, cx)))
}))
.children((!is_video).then(|| {
toggle_btn(format!("info-{ix}").into(), Icon::Info, self.show_info, "help.info")
.on_click(cx.listener(|this, _, _, cx| this.toggle_info(cx)))
}))
.child(toggle_btn(format!("fit-{ix}").into(), Icon::Fit, self.fit_menu == Some(ix), "tip.fit").on_click(
cx.listener(move |this, ev: &gpui::ClickEvent, _, cx| {
this.toggle_menu(ev, ix, |r| &mut r.fit_menu, cx);
}),
))
.children((!is_video).then(|| {
let on = pane.layout.is_some() || self.book_menu == Some(ix);
toggle_btn(format!("book-{ix}").into(), Icon::Book, on, "tip.book").on_click(cx.listener(
move |this, ev: &gpui::ClickEvent, _, cx| {
this.toggle_menu(ev, ix, |r| &mut r.book_menu, cx);
},
))
}))
.child(toggle_btn(format!("shuffle-{ix}").into(), Icon::Shuffle, pane.shuffle, "help.shuffle").on_click(
cx.listener(move |this, _, _, cx| {
this.panes[ix].shuffle ^= true;
cx.notify();
}),
))
.child(
toggle_btn(format!("slides-{ix}").into(), Icon::Slideshow, pane.slideshow, "tip.slideshow").on_click(
cx.listener(move |this, _, _, cx| {
this.panes[ix].toggle_slideshow();
cx.notify();
}),
),
);
if !is_video {
row = row.child(toggle_btn(format!("fade-{ix}").into(), Icon::Fade, self.fade, "tip.fade").on_click(
cx.listener(|this, _, _, cx| {
this.fade ^= true;
cx.notify();
}),
));
}
let has_tracks = pane.tracks.iter().filter(|t| t.kind == TrackKind::Audio).count() > 1
|| pane.tracks.iter().any(|t| t.kind == TrackKind::Sub);
if is_video && has_tracks {
let open = self.track_menu == Some(ix);
row = row.child(toggle_btn(format!("tracks-{ix}").into(), Icon::Captions, open, "tip.tracks").on_click(
cx.listener(move |this, ev: &gpui::ClickEvent, _, cx| {
this.toggle_menu(ev, ix, |r| &mut r.track_menu, cx);
}),
));
}
if is_video {
let muted = pane.muted || pane.volume <= 0.0;
row = row
.child(
toggle_btn(
format!("mute-{ix}").into(),
if muted { Icon::Mute } else { Icon::Volume },
false,
"help.mute",
)
.on_click(cx.listener(move |this, _, _, cx| this.toggle_mute(ix, cx))),
)
.child(
div()
.id(("vol-wrap", ix))
.flex_none()
.w(px(72.0))
.child(self.slider(
format!("vol-{ix}").into(),
pane.volume / 100.0,
SliderStyle::on_glass(),
None,
cx,
move |this, f| this.panes[ix].set_volume(f * 100.0),
))
.on_scroll_wheel(cx.listener(move |this, ev: &gpui::ScrollWheelEvent, _, cx| {
cx.stop_propagation();
let n = notches(&mut this.wheel_rest, &ev.delta);
if n != 0 {
let p = &mut this.panes[ix];
p.set_volume(p.volume + 5.0 * n as f64);
cx.notify();
}
})),
);
}
div()
.absolute()
.left(px(pill_left))
.top(px(pill_top))
.w(px(pill_w))
.h(px(PILL_H))
.rounded_full()
.bg(glass_fallback())
.border_1()
.border_color(glass_edge())
.shadow(vec![gpui::BoxShadow {
color: gpui::rgba(0x00000066).into(),
offset: gpui::point(px(0.0), px(8.0)),
blur_radius: px(24.0),
spread_radius: px(0.0),
}])
.when(!self.controls_visible(ix), |d| d.opacity(0.0))
.children(blur)
.child(div().absolute().top_0().left_0().size_full().rounded_full().bg(glass_tint()))
.child(div().absolute().top_0().left_0().size_full().rounded_full().bg(gpui::linear_gradient(
180.0,
gpui::linear_color_stop(gpui::rgba(0xffffff2a), 0.0),
gpui::linear_color_stop(gpui::rgba(0xffffff00), 0.55),
)))
.child(row)
.children((self.track_menu == Some(ix) && is_video).then(|| self.track_menu_el(ix, cx)))
.children((self.fit_menu == Some(ix)).then(|| self.fit_menu_el(ix, cx)))
.children((self.book_menu == Some(ix) && !is_video).then(|| self.book_menu_el(ix, cx)))
.into_any_element()
}
pub(crate) fn seek_bar(&self, ix: usize, cx: &mut Context<Self>) -> AnyElement {
let pane = &self.panes[ix];
let (pos, dur) = pane.time;
let bounds: Rc<Cell<Bounds<Pixels>>> = Rc::new(Cell::new(Bounds::default()));
let b1 = bounds.clone();
let at = move |x: Pixels| {
let b = bounds.get();
crate::pane::fraction_at(f32::from(x), f32::from(b.origin.x), f32::from(b.size.width))
};
let preview = pane.seek_hover.filter(|_| dur > 0.0).map(|f| {
div()
.absolute()
.bottom(px(30.0))
.left(gpui::relative(f))
.ml(px(-PREVIEW_W / 2.0 - 4.0))
.p_1()
.flex()
.flex_col()
.items_center()
.gap_1()
.rounded(px(theme::RADIUS))
.bg(gpui::rgba(0x1c1c21f2))
.border_1()
.border_color(theme::glass_edge())
.shadow_lg()
.children(pane.preview.as_ref().map(|(_, image)| {
let sz = image.size(0);
let (w, h) = (i32::from(sz.width).max(1) as f32, i32::from(sz.height).max(1) as f32);
img(image.clone()).w(px(PREVIEW_W)).h(px(PREVIEW_W * h / w)).rounded(px(theme::R_SMALL))
}))
.child(div().text_xs().text_color(gpui::white()).child(clock(f as f64 * dur)))
});
div()
.id(("seek-wrap", ix))
.relative()
.flex_grow()
.min_w(px(40.0))
.mx_1()
.child(canvas(move |b, _, _| b1.set(b), |_, _, _, _| {}).absolute().size_full())
.child(self.slider(
format!("seek-{ix}").into(),
if dur > 0.0 { pos / dur } else { 0.0 },
SliderStyle::on_glass(),
Some(seek_marks(pane.current().and_then(|i| self.lib.heat_profile(i)), &pane.chapters, dur)),
cx,
move |this, f| this.seek_pane(ix, f),
))
.children(preview)
.on_mouse_move(cx.listener(move |this, ev: &MouseMoveEvent, _, cx| {
this.hover_seek(ix, Some(at(ev.position.x)), cx);
}))
.on_hover(cx.listener(move |this, hovered: &bool, _, cx| {
if !*hovered {
this.hover_seek(ix, None, cx);
}
}))
.on_scroll_wheel(cx.listener(move |this, ev: &gpui::ScrollWheelEvent, _, cx| {
cx.stop_propagation();
let n = notches(&mut this.wheel_rest, &ev.delta);
if n != 0 {
this.seek_pane_by(ix, 5.0 * n as f64);
cx.notify();
}
}))
.into_any_element()
}
pub(crate) fn live_chip(&self, ix: usize, cx: &mut Context<Self>) -> AnyElement {
let playing = self.panes[ix].motion.is_some();
div()
.id(("live", ix))
.absolute()
.top(px(14.0))
.right(px(14.0))
.px_2()
.py_1()
.flex()
.flex_row()
.items_center()
.gap_1()
.rounded_full()
.bg(theme::scrim())
.text_xs()
.text_color(if playing { theme::accent().into() } else { gpui::white() })
.cursor_pointer()
.child(icon(Icon::Play, 10.0, if playing { theme::accent().into() } else { gpui::white() }))
.child(t("motion.live"))
.on_click(cx.listener(move |this, _, _, cx| {
if this.panes[ix].motion.is_none() {
this.start_motion(ix, cx);
}
}))
.into_any_element()
}
pub(crate) fn slider(
&self,
id: SharedString,
value: f64,
style: SliderStyle,
under: Option<AnyElement>,
cx: &mut Context<Self>,
on_set: impl Fn(&mut Self, f64) + Clone + 'static,
) -> AnyElement {
let value = value.clamp(0.0, 1.0) as f32;
let bounds: Rc<Cell<Bounds<Pixels>>> = Rc::new(Cell::new(Bounds::default()));
let b1 = bounds.clone();
let b2 = bounds.clone();
let b3 = bounds.clone();
let set_down = on_set.clone();
let frac = move |b: &Rc<Cell<Bounds<Pixels>>>, x: Pixels| -> f64 {
let b = b.get();
crate::pane::fraction_at(f32::from(x), f32::from(b.origin.x), f32::from(b.size.width)) as f64
};
div()
.id(id)
.relative()
.h(px(24.0))
.w_full()
.flex()
.items_center()
.cursor_pointer()
.child(canvas(move |b, _, _| b1.set(b), |_, _, _, _| {}).absolute().size_full())
.children(under)
.child(div().relative().w_full().h(px(4.0)).rounded_full().bg(style.track).child(
div().absolute().left_0().top_0().h_full().rounded_full().bg(style.fill).w(gpui::relative(value)),
))
.child(
div()
.absolute()
.left(gpui::relative(value))
.ml(px(-6.0))
.size(px(12.0))
.rounded_full()
.bg(style.knob)
.shadow_sm(),
)
.on_mouse_down(
MouseButton::Left,
cx.listener(move |this, ev: &MouseDownEvent, _, cx| {
set_down(this, frac(&b2, ev.position.x));
cx.stop_propagation();
cx.notify();
}),
)
.on_mouse_move(cx.listener(move |this, ev: &MouseMoveEvent, _, cx| {
if ev.pressed_button == Some(MouseButton::Left) {
on_set(this, frac(&b3, ev.position.x));
cx.notify();
}
}))
.into_any_element()
}
pub(crate) fn seek_pane_by(&mut self, ix: usize, secs: f64) {
let p = &mut self.panes[ix];
let (pos, dur) = p.time;
if let Some(v) = &p.video {
v.seek(secs);
p.time.0 = (pos + secs).clamp(0.0, dur.max(0.0));
}
}
pub(crate) fn seek_pane(&mut self, ix: usize, f: f64) {
let p = &mut self.panes[ix];
let dur = p.time.1;
if let Some(v) = &p.video
&& dur > 0.0
{
v.seek_to(f * dur);
p.time.0 = f * dur;
}
}
}