use super::head::{code_head, dim, ghost_icon, grow, head_icon, head_name};
use crate::{
icons::CarbonIcon,
t3::craft::{rem, sans},
theme::{color, color_in},
tokens::{CarbonTheme, ColorRole},
u0::primitives::parts::{focus_ring, focus_visible, keyed_focus},
};
use gpui_kit::{
App, Background, Bounds, CursorStyle, ElementId, Hsla, ImageSource, InteractiveElement as _,
IntoElement, MouseButton, ObjectFit, ParentElement, Pixels, Point, RenderOnce, ScrollHandle,
SharedString, StatefulInteractiveElement as _, Styled, StyledImage as _, Window, canvas, div,
fill, img, point, prelude::FluentBuilder as _, px, relative, size,
};
use std::rc::Rc;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Zoom {
Fit,
Scale(f32),
}
impl Zoom {
pub fn out(self) -> Self {
Self::Scale((self.base() / 1.5).max(0.25))
}
pub fn zoom_in(self) -> Self {
Self::Scale((self.base() * 1.5).min(8.))
}
pub fn toggle(self) -> Self {
match self {
Self::Fit => Self::Scale(1.),
Self::Scale(_) => Self::Fit,
}
}
fn base(self) -> f32 {
match self {
Self::Fit => 1.,
Self::Scale(s) => s,
}
}
pub fn percent(self) -> Option<u32> {
match self {
Self::Fit => None,
Self::Scale(s) => Some((s * 100.).round() as u32),
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum StageBackground {
#[default]
Checker,
Dark,
Light,
}
impl StageBackground {
pub fn next(self) -> Self {
match self {
Self::Checker => Self::Dark,
Self::Dark => Self::Light,
Self::Light => Self::Checker,
}
}
pub fn solid(self) -> Option<Hsla> {
match self {
Self::Checker => None,
Self::Dark => Some(color_in(CarbonTheme::G100, ColorRole::Background)),
Self::Light => Some(color_in(CarbonTheme::White, ColorRole::Background)),
}
}
}
pub fn checker_cell(col: usize, row: usize) -> ColorRole {
if (col + row).is_multiple_of(2) {
ColorRole::LayerAccent01
} else {
ColorRole::Layer01
}
}
pub const STAGE_HEIGHT: f32 = 224.;
pub const STAGE_PAD: f32 = 16.;
pub fn drawn_size(zoom: Zoom, width: f32, height: f32, inner_h: f32) -> (f32, f32) {
let ratio = if height > 0. { width / height } else { 1. };
match zoom {
Zoom::Scale(s) => (width * s, width * s / ratio),
Zoom::Fit => {
let h = height.min(inner_h);
(h * ratio, h)
}
}
}
#[derive(Clone)]
pub struct ImageViewerTexts {
pub zoom_out: SharedString,
pub zoom_in: SharedString,
pub toggle_fit: SharedString,
pub fit: SharedString,
pub background: SharedString,
pub dimensions: Rc<dyn Fn(u32, u32) -> SharedString>,
}
impl Default for ImageViewerTexts {
fn default() -> Self {
Self {
zoom_out: "Zoom out".into(),
zoom_in: "Zoom in".into(),
toggle_fit: "Toggle fit / actual size".into(),
fit: "Fit".into(),
background: "Toggle background".into(),
dimensions: Rc::new(|w, h| format!("{w}×{h}").into()),
}
}
}
#[derive(Default)]
struct Drag {
from: Option<(Point<Pixels>, Point<Pixels>)>,
}
#[derive(IntoElement)]
pub struct ImageViewer {
id: ElementId,
src: Option<ImageSource>,
alt: SharedString,
name: SharedString,
width: u32,
height: u32,
placeholder: Option<Background>,
texts: ImageViewerTexts,
}
impl ImageViewer {
pub fn new(id: impl Into<ElementId>, name: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
src: None,
alt: SharedString::default(),
name: name.into(),
width: 0,
height: 0,
placeholder: None,
texts: ImageViewerTexts::default(),
}
}
pub fn src(mut self, src: impl Into<ImageSource>) -> Self {
self.src = Some(src.into());
self
}
pub fn alt(mut self, alt: impl Into<SharedString>) -> Self {
self.alt = alt.into();
self
}
pub fn dimensions(mut self, width: u32, height: u32) -> Self {
self.width = width;
self.height = height;
self
}
pub fn color(mut self, fill: impl Into<Background>) -> Self {
self.placeholder = Some(fill.into());
self
}
pub fn texts(mut self, texts: ImageViewerTexts) -> Self {
self.texts = texts;
self
}
}
fn checkerboard() -> impl IntoElement {
let a = color(checker_cell(0, 0));
let b = color(checker_cell(1, 0));
canvas(
|_, _, _| (),
move |bounds: Bounds<Pixels>, _, window, _| {
let cell = px(8.);
let cols = (bounds.size.width / cell).ceil() as usize;
let rows = (bounds.size.height / cell).ceil() as usize;
window.paint_quad(fill(bounds, b));
for r in 0..rows {
for c in 0..cols {
if (c + r).is_multiple_of(2) {
let origin = bounds.origin + point(cell * c as f32, cell * r as f32);
window.paint_quad(fill(Bounds::new(origin, size(cell, cell)), a));
}
}
}
},
)
.absolute()
.top_0()
.left_0()
.size_full()
}
fn zoom_value(
id: ElementId,
label: SharedString,
name: SharedString,
on_click: impl Fn(&mut Window, &mut App) + 'static,
window: &mut Window,
cx: &mut App,
) -> impl IntoElement {
let focus = keyed_focus((id.clone(), "focus"), window, cx);
let ring = focus_visible(&focus, window);
let hovered = window.use_keyed_state((id.clone(), "hover"), cx, |_, _| false);
let over = *hovered.read(cx);
sans(gpui_kit::base::Button::new(id), 12., 400)
.track_focus(&focus)
.accessibility_label(name)
.relative()
.flex()
.flex_none()
.items_center()
.justify_center()
.min_w(rem(48.))
.h(rem(32.))
.px(rem(4.))
.text_color(color(ColorRole::TextPrimary))
.when(over, |b| b.bg(color(ColorRole::LayerHover01)))
.cursor(CursorStyle::PointingHand)
.on_hover(move |h, _, cx| {
hovered.update(cx, |v, cx| {
if *v != *h {
*v = *h;
cx.notify();
}
})
})
.on_click(move |_, w, cx| on_click(w, cx))
.child(label)
.when(ring, |b| b.child(focus_ring()))
}
impl RenderOnce for ImageViewer {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let zoom = window.use_keyed_state((id.clone(), "zoom"), cx, |_, _| Zoom::Fit);
let bg = window.use_keyed_state((id.clone(), "bg"), cx, |_, _| StageBackground::Checker);
let drag = window.use_keyed_state((id.clone(), "drag"), cx, |_, _| Drag::default());
let scroll = window
.use_keyed_state((id.clone(), "scroll"), cx, |_, _| ScrollHandle::new())
.read(cx)
.clone();
let z = *zoom.read(cx);
let stage_bg = *bg.read(cx);
let set_zoom = |f: fn(Zoom) -> Zoom| {
let zoom = zoom.clone();
move |_: &mut Window, cx: &mut App| {
zoom.update(cx, |z, cx| {
*z = f(*z);
cx.notify();
})
}
};
let value = match z.percent() {
None => self.texts.fit.clone(),
Some(p) => format!("{p}%").into(),
};
let head = code_head()
.child(head_icon(CarbonIcon::Image))
.child(head_name(self.name.clone(), ColorRole::TextSecondary))
.when(self.width > 0, |h| {
h.child(dim((self.texts.dimensions)(self.width, self.height), 12.))
})
.child(grow())
.child(ghost_icon(
(id.clone(), "out"),
self.texts.zoom_out.clone(),
CarbonIcon::ZoomOut,
false,
Some(Rc::new(set_zoom(Zoom::out))),
))
.child(zoom_value(
(id.clone(), "value").into(),
value,
self.texts.toggle_fit.clone(),
set_zoom(Zoom::toggle),
window,
cx,
))
.child(ghost_icon(
(id.clone(), "in"),
self.texts.zoom_in.clone(),
CarbonIcon::ZoomIn,
false,
Some(Rc::new(set_zoom(Zoom::zoom_in))),
))
.child(ghost_icon(
(id.clone(), "bg"),
self.texts.background.clone(),
CarbonIcon::ColorPalette,
false,
Some(Rc::new({
let bg = bg.clone();
move |_, cx| {
bg.update(cx, |b, cx| {
*b = b.next();
cx.notify();
})
}
})),
));
let (w, h) = drawn_size(
z,
self.width.max(1) as f32,
self.height.max(1) as f32,
STAGE_HEIGHT - 2. * STAGE_PAD,
);
let picture = match &self.src {
Some(src) => match z {
Zoom::Fit => img(src.clone())
.size_full()
.object_fit(ObjectFit::Contain)
.into_any_element(),
Zoom::Scale(_) => img(src.clone())
.flex_none()
.w(rem(w))
.h(rem(h))
.into_any_element(),
},
None => match z {
Zoom::Fit => div()
.flex_none()
.w(relative(0.7))
.aspect_ratio(self.width.max(1) as f32 / self.height.max(1) as f32)
.max_w_full()
.max_h_full(),
Zoom::Scale(_) => div().flex_none().w(rem(w)).h(rem(h)),
}
.when_some(self.placeholder, |d, f| d.bg(f))
.into_any_element(),
};
let zoomed = z != Zoom::Fit;
let down = drag.clone();
let moving = drag.clone();
let up = drag.clone();
let pan = scroll.clone();
let start = scroll.clone();
let stage = div()
.id((id.clone(), "stage"))
.relative()
.h(rem(STAGE_HEIGHT))
.overflow_scroll()
.track_scroll(&scroll)
.when_some(stage_bg.solid(), |s, c| s.bg(c))
.when(stage_bg == StageBackground::Checker, |s| {
s.child(checkerboard())
})
.when(zoomed, |s| s.cursor(CursorStyle::OpenHand))
.on_mouse_down(MouseButton::Left, move |e, _, cx| {
down.update(cx, |d, _| d.from = Some((e.position, start.offset())));
})
.on_mouse_move(move |e, window, cx| {
if let Some((from, offset)) = moving.read(cx).from {
if !e.dragging() {
return;
}
let max = pan.max_offset();
let next = offset + (e.position - from);
pan.set_offset(point(
next.x.clamp(-max.x, px(0.)),
next.y.clamp(-max.y, px(0.)),
));
window.refresh();
}
})
.on_mouse_up(MouseButton::Left, move |_, _, cx| {
up.update(cx, |d, _| d.from = None);
})
.child(
div()
.relative()
.flex()
.items_center()
.justify_center()
.min_w_full()
.min_h_full()
.when(!zoomed, |d| d.size_full())
.p(rem(STAGE_PAD))
.child(picture),
);
div()
.id(id)
.flex()
.flex_col()
.min_w_0()
.bg(color(ColorRole::Layer01))
.child(head)
.child(stage)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn zoom_steps_follow_the_reference() {
assert_eq!(Zoom::Fit.zoom_in(), Zoom::Scale(1.5));
assert_eq!(Zoom::Fit.out(), Zoom::Scale(1. / 1.5));
assert_eq!(Zoom::Scale(0.3).out(), Zoom::Scale(0.25));
assert_eq!(Zoom::Scale(6.).zoom_in(), Zoom::Scale(8.));
assert_eq!(Zoom::Fit.toggle(), Zoom::Scale(1.));
assert_eq!(Zoom::Scale(2.25).toggle(), Zoom::Fit);
assert_eq!(Zoom::Scale(1.5).percent(), Some(150));
assert_eq!(Zoom::Fit.percent(), None);
}
#[test]
fn backgrounds_cycle_and_paint() {
use StageBackground::*;
assert_eq!(Checker.next(), Dark);
assert_eq!(Dark.next(), Light);
assert_eq!(Light.next(), Checker);
assert_eq!(Dark.solid(), Some(gpui_kit::rgb(0x161616).into()));
assert_eq!(Light.solid(), Some(gpui_kit::rgb(0xffffff).into()));
assert_eq!(checker_cell(0, 0), ColorRole::LayerAccent01);
assert_eq!(checker_cell(1, 0), ColorRole::Layer01);
assert_eq!(checker_cell(1, 1), ColorRole::LayerAccent01);
}
#[test]
fn drawn_size_keeps_the_ratio() {
assert_eq!(drawn_size(Zoom::Fit, 1440., 900., 192.), (307.2, 192.));
assert_eq!(
drawn_size(Zoom::Scale(0.5), 1440., 900., 192.),
(720., 450.)
);
assert_eq!(drawn_size(Zoom::Fit, 100., 50., 192.), (100., 50.));
}
}