use gpui::{
AnyElement, Context, MouseButton, MouseDownEvent, MouseMoveEvent, SharedString, Window, div, img, prelude::*, px,
};
use crate::app::Rosier;
use crate::assets::Icon;
use crate::i18n::{t, tf};
use crate::pane;
use crate::ui::{button, icon, primary_button, segment, segmented};
use crate::ui_controls::counter;
use crate::{grid, theme};
impl Rosier {
fn info_panel(&self, ix: usize, w: f32) -> Option<AnyElement> {
let pane = &self.panes[ix];
let i = pane.current().filter(|_| self.show_info && pane.video.is_none())?;
let e = self.lib.entries.get(i)?;
let exif = pane.info.as_ref().filter(|(at, _)| *at == i).map(|(_, info)| info.clone()).unwrap_or_default();
let name = e.path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
let folder = e.path.parent().map(|p| p.display().to_string());
let (fw, fh) = pane.full;
let size = if fw > 0 {
format!("{fw} ร {fh} ยท {}", crate::i18n::human_size(e.size))
} else {
crate::i18n::human_size(e.size)
};
let rows = [
(t("info.camera"), exif.camera),
(t("info.lens"), exif.lens),
(t("info.exposure"), exif.exposure),
(t("info.taken"), exif.taken),
];
let line = |label: SharedString, value: String| {
div()
.flex()
.flex_col()
.gap_0p5()
.child(div().text_xs().text_color(gpui::rgba(0xffffff90)).child(label))
.child(div().text_sm().text_color(gpui::white()).child(value))
};
Some(
crate::ui::glass(theme::R_PANEL)
.absolute()
.top(px(10.0))
.right(px(10.0))
.w(px(260.0f32.min(w - 20.0).max(120.0)))
.p_3()
.flex()
.flex_col()
.gap_2()
.child(crate::ui::glass_sheen(theme::R_PANEL))
.child(div().text_sm().text_color(gpui::white()).child(name))
.children(folder.map(|f| div().text_xs().text_color(gpui::rgba(0xffffff90)).child(f)))
.child(div().text_xs().text_color(gpui::rgba(0xffffffc0)).child(size))
.children(rows.into_iter().filter_map(|(label, v)| v.map(|v| line(label.into(), v))))
.into_any_element(),
)
}
pub(crate) fn render_split(&mut self, window: &mut Window, cx: &mut Context<Self>) -> AnyElement {
if self.panes.iter().any(|p| p.fade_from.as_ref().is_some_and(|(_, at)| at.elapsed() < crate::app::FADE)) {
window.request_animation_frame();
}
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);
let rect = (col as f32 * pane_w, row as f32 * pane_h, pane_w, pane_h);
let resized = (p.rect.2, p.rect.3, p.px_size) != (rect.2, rect.3, phys);
p.rect = rect;
p.px_size = phys;
if resized && p.wants_detail() {
cx.defer_in(window, move |this, _, cx| this.update_detail(ix, cx));
}
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)
.tooltip(crate::ui::tip(t([
"tip.split1",
"tip.split2stacked",
"tip.split2side",
"tip.split4",
][k])))
.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);
}))
}),
),
)
.when(self.split > 1, |d| {
d.child(
button("split-sync")
.tooltip(crate::ui::tip(t("help.syncview")))
.child(icon(Icon::Link, 16.0, if self.sync_view { theme::accent() } else { theme::text_dim() }))
.on_click(cx.listener(|this, _, _, cx| this.toggle_sync_view(cx))),
)
})
.child(div().w(px(1.0)).h(px(20.0)).bg(theme::line()))
.child(
button("split-pip")
.tooltip(crate::ui::tip(t("help.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")
.tooltip(crate::ui::tip(t("help.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())
.on_drop(
cx.listener(move |this, paths: &gpui::ExternalPaths, _, cx| this.dropped(paths.paths(), Some(ix), cx)),
)
.drag_over::<gpui::ExternalPaths>(|style, _, _, _| style.border_2().border_color(theme::accent()))
.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| {
if ev.modifiers.control || ev.modifiers.platform {
let dy = f32::from(ev.delta.pixel_delta(px(20.0)).y);
if dy == 0.0 || this.panes[ix].video.is_some() {
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);
return;
}
if this.strip_at(ix).is_some() {
let dy = match ev.delta {
gpui::ScrollDelta::Lines(l) => -l.y * STRIP_LINE_PX,
gpui::ScrollDelta::Pixels(d) => -f32::from(d.y),
};
this.scroll_strip(ix, dy, cx);
return;
}
let n = notches(&mut this.wheel_rest, &ev.delta);
if n != 0 {
this.advance(ix, -n as isize, 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));
this.update_detail(ix, cx);
this.mirror_view(ix, cx);
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((cw, pages)) = self.strip_visible(ix) {
let x = (w - cw) / 2.0;
for (y, ph, image) in pages {
root = root.child(match image {
Some(image) => crate::ui::media(&image).left(px(x)).top(px(y)).w(px(cw)).h(px(ph)),
None => div().absolute().left(px(x)).top(px(y)).w(px(cw)).h(px(ph)).bg(theme::stage()),
});
}
} else if let Some(frame) = &pane.frame {
let fade = pane
.fade_from
.as_ref()
.map(|(old, at)| (old, at.elapsed().as_secs_f32() / crate::app::FADE.as_secs_f32()))
.filter(|(_, t)| *t < 1.0);
if let Some((old, _)) = fade {
let sz = old.size(0);
let (ox, oy, dw, dh) = pane.base(i32::from(sz.width) as f32, i32::from(sz.height) as f32);
root = root.child(crate::ui::media(old).left(px(ox)).top(px(oy)).w(px(dw)).h(px(dh)));
}
let sz = frame.size(0);
let fit = pane.base(i32::from(sz.width) as f32, i32::from(sz.height) as f32);
let (ox, oy, dw, dh) = pane::zoomed(fit, w, h, pane.zoom, pane.pan);
root = root.child(
crate::ui::media(frame)
.left(px(ox))
.top(px(oy))
.w(px(dw))
.h(px(dh))
.when_some(fade, |d, (_, t)| d.opacity(t * t * (3.0 - 2.0 * t))),
);
if let Some((detail, n)) = pane.detail.as_ref().filter(|_| pane.wants_detail()) {
root = root.child(
img(detail.clone())
.absolute()
.left(px(ox + n[0] * dw))
.top(px(oy + n[1] * dh))
.w(px((n[2] - n[0]) * dw))
.h(px((n[3] - n[1]) * dh)),
);
}
if let Some(m) = pane.motion_frame.as_ref().filter(|_| pane.zoom <= 1.0) {
let sz = m.size(0);
let (mx, my, mw, mh) = pane.base(i32::from(sz.width) as f32, i32::from(sz.height) as f32);
root = root.child(crate::ui::media(m).left(px(mx)).top(px(my)).w(px(mw)).h(px(mh)));
}
if pane.has_motion {
root = root.child(self.live_chip(ix, cx));
}
} 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}", counter(pane)))),
);
if let Some(panel) = self.info_panel(ix, w) {
root = root.child(panel);
}
root = root.child(self.glass_controls(ix, w, h, cx));
root.children(focused.then(focus_ring)).into_any_element()
}
}
pub(crate) fn notches(rest: &mut f32, delta: &gpui::ScrollDelta) -> i32 {
match delta {
gpui::ScrollDelta::Lines(p) => {
*rest = 0.0;
if p.y > 0.0 {
1
} else if p.y < 0.0 {
-1
} else {
0
}
}
gpui::ScrollDelta::Pixels(p) => {
*rest += f32::from(p.y);
let n = (*rest / WHEEL_NOTCH_PX).trunc();
*rest -= n * WHEEL_NOTCH_PX;
n as i32
}
}
}
const STRIP_LINE_PX: f32 = 120.0;
const WHEEL_NOTCH_PX: f32 = 60.0;
fn focus_ring() -> gpui::Div {
div().absolute().top_0().left_0().size_full().border_2().border_color(theme::accent())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_wheel_notch_is_one_step_and_touchpad_travel_adds_up() {
let mut rest = 0.0;
let lines = |y: f32| gpui::ScrollDelta::Lines(gpui::point(0.0, y));
let pixels = |y: f32| gpui::ScrollDelta::Pixels(gpui::point(px(0.0), px(y)));
assert_eq!(notches(&mut rest, &lines(3.0)), 1, "a notch is one step however many lines");
assert_eq!(notches(&mut rest, &lines(-1.0)), -1);
assert_eq!(notches(&mut rest, &lines(0.0)), 0);
assert_eq!(notches(&mut rest, &pixels(-25.0)), 0);
assert_eq!(notches(&mut rest, &pixels(-25.0)), 0);
assert_eq!(notches(&mut rest, &pixels(-25.0)), -1, "75 px make one notch");
assert_eq!(notches(&mut rest, &pixels(130.0)), 1, "-15 + 130 = 115: one notch, 55 left");
assert_eq!(notches(&mut rest, &pixels(70.0)), 2, "55 + 70 = 125: two");
}
}