use std::cell::Cell;
use std::rc::Rc;
use gpui::{
AnyElement, Bounds, Context, MouseButton, MouseDownEvent, MouseMoveEvent, Pixels, SharedString, Window, canvas,
div, img, prelude::*, px,
};
use crate::app::Rosier;
use crate::assets::Icon;
use crate::i18n::{t, tf};
use crate::pane::{self, clock};
use crate::ui::{button, icon, primary_button, segment, segmented};
use crate::{grid, theme};
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)
}
impl Rosier {
pub(crate) fn render_split(&mut self, window: &mut Window, cx: &mut Context<Self>) -> AnyElement {
let vp = window.viewport_size();
let scale = window.scale_factor();
let (cols, rows) = pane::layout(self.split, self.split_side);
let pane_w = f32::from(vp.width) / cols as f32;
let pane_h = (f32::from(vp.height) - self.titlebar_h) / rows as f32;
let phys = ((pane_w * scale) as u32, (pane_h * scale) as u32);
for ix in 0..self.panes.len() {
let p = &mut self.panes[ix];
let (col, row) = (ix % cols, ix / cols);
p.rect = (col as f32 * pane_w, row as f32 * pane_h, pane_w, pane_h);
p.px_size = phys;
if let Some(v) = &p.video {
v.set_target(phys.0, phys.1);
}
}
self.controls_shown = (0..self.panes.len()).any(|i| self.controls_visible(i));
let mut grid_rows = Vec::new();
for r in 0..rows {
let mut row = Vec::new();
for c in 0..cols {
let ix = r * cols + c;
if ix < self.panes.len() {
row.push(self.render_pane(ix, pane_w, pane_h, cx));
}
}
grid_rows.push(div().flex().flex_row().children(row));
}
let toolbar = crate::ui::glass(theme::R_PANEL)
.flex()
.flex_row()
.items_center()
.gap_2()
.h(px(theme::TOOLBAR_H - 8.0))
.px_2()
.child(crate::ui::glass_sheen(theme::R_PANEL))
.child(
button("split-back")
.child(icon(Icon::Grid, 16.0, theme::text_dim()))
.child(t("tool.view.grid"))
.on_click(cx.listener(|this, _, _, cx| this.go_browse(None, cx))),
)
.child(div().w(px(1.0)).h(px(20.0)).bg(theme::line()))
.child(
segmented().children(
[
(1usize, false, Icon::Split1),
(2, false, Icon::Split2v),
(2, true, Icon::Split2),
(4, false, Icon::Split4),
]
.into_iter()
.enumerate()
.map(|(k, (n, side, ic))| {
let active = self.split == n && (n != 2 || self.split_side == side);
segment(("split-n", k), active)
.child(icon(ic, 16.0, if active { theme::text() } else { theme::text_dim() }))
.on_click(cx.listener(move |this, _, _, cx| {
this.split_side = side;
this.set_split(n, cx);
}))
}),
),
)
.child(div().w(px(1.0)).h(px(20.0)).bg(theme::line()))
.child(
button("split-pip")
.child(icon(Icon::Pip, 16.0, if self.pip.is_some() { theme::accent() } else { theme::text_dim() }))
.on_click(cx.listener(|this, _, _, cx| this.toggle_pip(cx))),
)
.child(button("split-full").child(icon(Icon::Fullscreen, 16.0, theme::text_dim())).on_click(cx.listener(
|_, _, window, cx| {
window.toggle_fullscreen();
cx.notify();
},
)));
let top_zone = div()
.absolute()
.top_0()
.left_0()
.w_full()
.h(px(72.0))
.group("topbar")
.flex()
.justify_center()
.pt(px(theme::GUTTER))
.child(
div()
.when(!self.controls_shown, |d| d.opacity(0.0).group_hover("topbar", |s| s.opacity(1.0)))
.child(toolbar),
);
div()
.relative()
.size_full()
.flex()
.flex_col()
.bg(gpui::black())
.children(grid_rows)
.child(top_zone)
.into_any_element()
}
fn render_pane(&self, ix: usize, w: f32, h: f32, cx: &mut Context<Self>) -> AnyElement {
let pane = &self.panes[ix];
let group: SharedString = format!("pane-{ix}").into();
let focused = ix == self.focused && self.panes.len() > 1;
let mut root = div()
.id(("pane", ix))
.group(group.clone())
.relative()
.flex_none()
.w(px(w))
.h(px(h))
.overflow_hidden()
.bg(gpui::black())
.when(pane.zoom > 1.0, |d| d.cursor_grab())
.when(self.hover_pane == Some(ix) && !self.controls_visible(ix), |d| d.cursor(gpui::CursorStyle::None))
.when(self.split > 1, |d| d.border_1().border_color(theme::stage()))
.on_mouse_down(
MouseButton::Left,
cx.listener(move |this, _, _, cx| {
if this.focused != ix {
this.set_focus(ix);
cx.notify();
}
}),
)
.on_click(cx.listener(|_, ev: &gpui::ClickEvent, window, cx| {
if ev.click_count() == 2 {
window.toggle_fullscreen();
cx.notify();
}
}))
.on_scroll_wheel(cx.listener(move |this, ev: &gpui::ScrollWheelEvent, _, cx| {
let dy = f32::from(ev.delta.pixel_delta(px(20.0)).y);
if dy == 0.0 {
return;
}
let (x, y, w, h) = this.panes[ix].rect;
let at = (f32::from(ev.position.x) - (x + w / 2.0), f32::from(ev.position.y) - (y + h / 2.0));
this.zoom_pane(ix, if dy > 0.0 { 1.15 } else { 1.0 / 1.15 }, at, cx);
}))
.on_mouse_down(
MouseButton::Left,
cx.listener(move |this, ev: &MouseDownEvent, _, _| {
let p = &mut this.panes[ix];
if p.zoom > 1.0 {
p.drag = Some((f32::from(ev.position.x), f32::from(ev.position.y)));
}
}),
)
.on_hover(cx.listener(move |this, hovered: &bool, _, cx| {
if !*hovered && this.hover_pane == Some(ix) {
this.hover_pane = None;
cx.notify();
}
}))
.on_mouse_move(cx.listener(move |this, _: &MouseMoveEvent, _, cx| this.pointer_moved(Some(ix), cx)))
.on_mouse_move(cx.listener(move |this, ev: &MouseMoveEvent, _, cx| {
let p = &mut this.panes[ix];
match (p.drag, ev.pressed_button) {
(Some((lx, ly)), Some(MouseButton::Left)) => {
let (x, y) = (f32::from(ev.position.x), f32::from(ev.position.y));
p.pan.0 += x - lx;
p.pan.1 += y - ly;
p.drag = Some((x, y));
cx.notify();
}
(Some(_), _) => p.drag = None,
_ => {}
}
}))
.on_mouse_up(
MouseButton::Left,
cx.listener(move |this, _, _, _| {
this.panes[ix].drag = None;
}),
);
if pane.source.is_none() {
return root
.flex()
.items_center()
.justify_center()
.child(
div()
.flex()
.flex_col()
.items_center()
.gap_3()
.child(icon(Icon::Split4, 36.0, theme::text_faint()))
.child(
div()
.text_color(theme::text_dim())
.child(tf("pane.empty.title", &[("n", (ix + 1).to_string())])),
)
.child(
primary_button(("pane-pick", ix), t("pane.empty.pick"))
.on_click(cx.listener(move |this, _, _, cx| this.go_browse(Some(ix), cx))),
),
)
.children(focused.then(focus_ring))
.into_any_element();
}
if let Some(frame) = &pane.frame {
let sz = frame.size(0);
let fit = pane::fit_rect(i32::from(sz.width) as f32, i32::from(sz.height) as f32, w, h);
let (ox, oy, dw, dh) = pane::zoomed(fit, w, h, pane.zoom, pane.pan);
root = root.child(img(frame.clone()).absolute().left(px(ox)).top(px(oy)).w(px(dw)).h(px(dh)));
} else {
let msg = if pane.error.is_some() { t("pane.error") } else { t("pane.loading") };
root = root.child(
div()
.absolute()
.size_full()
.flex()
.items_center()
.justify_center()
.text_color(if pane.error.is_some() { theme::danger() } else { theme::text_faint() })
.child(msg),
);
}
let title = match &pane.source {
Some(crate::pane::PaneSource::Favorites) => t("side.favorites").to_string(),
Some(crate::pane::PaneSource::Folder(p)) => grid::folder_title(p, &self.lib.sources),
None => String::new(),
};
let name = pane
.current()
.and_then(|i| self.lib.path(i))
.and_then(|p| p.file_name())
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_default();
root = root.child(
div()
.absolute()
.top(px(10.0))
.left(px(10.0))
.max_w(px((w - 20.0).max(40.0)))
.px_2p5()
.py_1()
.rounded(px(theme::RADIUS))
.bg(theme::scrim())
.text_xs()
.when(!self.controls_visible(ix), |d| d.opacity(0.0))
.child(div().truncate().text_color(gpui::rgba(0xffffffc0)).child(title))
.child(div().truncate().text_color(gpui::white()).child(format!(
"{} / {} · {name}",
pane.pos + 1,
pane.items.len()
))),
);
root = root.child(self.glass_controls(ix, w, h, cx));
root.children(focused.then(focus_ring)).into_any_element()
}
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 round_btn = |id: SharedString, ic: Icon, size: f32| {
div()
.id(id)
.size(px(32.0))
.flex_none()
.flex()
.items_center()
.justify_center()
.rounded_full()
.cursor_pointer()
.hover(|s| s.bg(gpui::rgba(0xffffff24)))
.child(icon(ic, size, gpui::white()))
};
let toggle_btn = |id: SharedString, ic: Icon, on: bool| {
div()
.id(id)
.size(px(32.0))
.flex_none()
.flex()
.items_center()
.justify_center()
.rounded_full()
.cursor_pointer()
.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)
.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).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)
.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.slider(
format!("seek-{ix}").into(),
if dur > 0.0 { pos / dur } else { 0.0 },
true,
SliderStyle::on_glass(),
cx,
move |this, f| this.seek_pane(ix, f),
))
.child(div().flex_none().w(px(52.0)).text_color(gpui::rgba(0xffffffb0)).child(clock(dur)));
} else {
let secs = pane.interval.as_secs();
let step_btn = |id: SharedString, ic: Icon| {
div()
.id(id)
.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(format!(
"{} / {}",
pane.pos + 1,
pane.items.len()
)))
.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).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).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).on_click(
cx.listener(move |this, _, _, cx| {
if let Some(i) = this.panes[ix].current() {
this.toggle_favorite(i, cx);
}
}),
),
)
.child(toggle_btn(format!("shuffle-{ix}").into(), Icon::Shuffle, pane.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).on_click(cx.listener(
move |this, _, _, cx| {
this.panes[ix].toggle_slideshow();
cx.notify();
},
)));
if is_video {
let muted = ix != self.focused || pane.volume <= 0.0;
row = row
.child(div().flex_none().pl_1().child(icon(
if muted { Icon::Mute } else { Icon::Volume },
17.0,
gpui::rgba(0xffffffd0),
)))
.child(div().flex_none().w(px(72.0)).child(self.slider(
format!("vol-{ix}").into(),
pane.volume / 100.0,
false,
SliderStyle::on_glass(),
cx,
move |this, f| this.panes[ix].set_volume(f * 100.0),
)));
}
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)
.into_any_element()
}
pub(crate) fn slider(
&self,
id: SharedString,
value: f64,
grow: bool,
style: SliderStyle,
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();
let w = f32::from(b.size.width).max(1.0);
(f32::from(x - b.origin.x) / w).clamp(0.0, 1.0) as f64
};
div()
.id(id)
.relative()
.h(px(24.0))
.when(grow, |d| d.flex_grow().min_w(px(40.0)).mx_1())
.when(!grow, |d| d.w_full())
.flex()
.items_center()
.cursor_pointer()
.child(canvas(move |b, _, _| b1.set(b), |_, _, _, _| {}).absolute().size_full())
.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(&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;
}
}
}
fn focus_ring() -> gpui::Div {
div().absolute().top_0().left_0().size_full().border_2().border_color(theme::accent())
}