use super::chip::{ContextChip, ContextChipKind, ContextChipShape};
use crate::t3::base::Spin;
use crate::t3::press::{self, rem};
use crate::{icons::CarbonIcon, theme::color, tokens::ColorRole};
use gpui_kit::{
AnyElement, App, BoxShadow, CursorStyle, ElementId, Hsla, ImageSource, InteractiveElement as _,
IntoElement, ObjectFit, ParentElement, RenderOnce, SharedString,
StatefulInteractiveElement as _, Styled, StyledImage as _, Window, div, img,
prelude::FluentBuilder as _, px, rgba,
};
use std::rc::Rc;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum AttachmentImageState {
#[default]
Done,
Uploading,
Error,
}
#[derive(Clone)]
pub struct AttachmentImage {
pub name: SharedString,
pub source: Option<ImageSource>,
pub color: Option<Hsla>,
pub state: AttachmentImageState,
pub error: Option<SharedString>,
}
impl AttachmentImage {
pub fn new(name: impl Into<SharedString>) -> Self {
Self {
name: name.into(),
source: None,
color: None,
state: AttachmentImageState::Done,
error: None,
}
}
pub fn source(mut self, source: impl Into<ImageSource>) -> Self {
self.source = Some(source.into());
self
}
pub fn color(mut self, color: impl Into<Hsla>) -> Self {
self.color = Some(color.into());
self
}
pub fn state(mut self, state: AttachmentImageState) -> Self {
self.state = state;
self
}
pub fn error(mut self, error: impl Into<SharedString>) -> Self {
self.error = Some(error.into());
self
}
}
#[derive(Clone)]
pub struct AttachmentFile {
pub name: SharedString,
pub kind: ContextChipKind,
pub size: Option<SharedString>,
}
impl AttachmentFile {
pub fn new(name: impl Into<SharedString>) -> Self {
Self {
name: name.into(),
kind: ContextChipKind::File,
size: None,
}
}
pub fn kind(mut self, kind: ContextChipKind) -> Self {
self.kind = kind;
self
}
pub fn size(mut self, size: impl Into<SharedString>) -> Self {
self.size = Some(size.into());
self
}
}
#[derive(Clone)]
pub struct AttachmentTexts {
pub upload_failed: SharedString,
pub retry: SharedString,
pub uploading: SharedString,
pub remove: Rc<dyn Fn(&str) -> SharedString>,
pub open: Rc<dyn Fn(&str) -> SharedString>,
}
impl Default for AttachmentTexts {
fn default() -> Self {
Self {
upload_failed: "Upload failed".into(),
retry: "Retry".into(),
uploading: "Uploading".into(),
remove: Rc::new(|name| format!("Remove {name}").into()),
open: Rc::new(|name| format!("Open {name}").into()),
}
}
}
pub const fn thumb_geometry(in_composer: bool) -> (f32, f32, f32) {
if in_composer {
(32., -6., 1.)
} else {
(64., 2., 2.)
}
}
pub fn upload_scrim(in_composer: bool) -> Hsla {
rgba(if in_composer { 0x1616168c } else { 0x16161680 }).into()
}
type NameHandler = Rc<dyn Fn(SharedString, &mut Window, &mut App)>;
#[derive(IntoElement)]
pub struct ComposerAttachmentStrip {
id: ElementId,
images: Vec<AttachmentImage>,
files: Vec<AttachmentFile>,
in_composer: bool,
texts: AttachmentTexts,
on_remove: Option<NameHandler>,
on_retry: Option<NameHandler>,
on_open: Option<NameHandler>,
}
impl ComposerAttachmentStrip {
pub fn new(
id: impl Into<ElementId>,
images: Vec<AttachmentImage>,
files: Vec<AttachmentFile>,
) -> Self {
Self {
id: id.into(),
images,
files,
in_composer: false,
texts: AttachmentTexts::default(),
on_remove: None,
on_retry: None,
on_open: None,
}
}
pub fn in_composer(mut self, in_composer: bool) -> Self {
self.in_composer = in_composer;
self
}
pub fn texts(mut self, texts: AttachmentTexts) -> Self {
self.texts = texts;
self
}
pub fn on_remove(mut self, f: impl Fn(SharedString, &mut Window, &mut App) + 'static) -> Self {
self.on_remove = Some(Rc::new(f));
self
}
pub fn on_open(mut self, f: impl Fn(SharedString, &mut Window, &mut App) + 'static) -> Self {
self.on_open = Some(Rc::new(f));
self
}
pub fn on_retry(mut self, f: impl Fn(SharedString, &mut Window, &mut App) + 'static) -> Self {
self.on_retry = Some(Rc::new(f));
self
}
}
#[derive(Clone, Copy)]
struct Fill {
hover: Option<Hsla>,
focus: Option<Hsla>,
}
fn bare_button(
id: ElementId,
label: SharedString,
fill: Fill,
content: impl FnOnce(bool) -> AnyElement,
on_click: Option<press::Handler>,
window: &mut Window,
cx: &mut App,
) -> gpui_kit::base::Button {
let (hovered, hover) = press::hover((id.clone(), "hover"), window, cx);
let (focus, ring) = press::focus((id.clone(), "focus"), window, cx);
let bg = if ring {
fill.focus.or(fill.hover.filter(|_| hovered))
} else {
fill.hover.filter(|_| hovered)
};
gpui_kit::base::Button::new(id)
.track_focus(&focus)
.accessibility_label(label)
.relative()
.flex()
.flex_none()
.items_center()
.justify_center()
.cursor(CursorStyle::PointingHand)
.on_hover(press::track_hover(hover))
.when_some(bg, |this, bg| this.bg(bg))
.when_some(on_click, |this, f| {
let focus = focus.clone();
this.on_click(move |_, w, cx| {
crate::u0::primitives::parts::vacate_focus(&focus, w, cx);
f(w, cx)
})
})
.child(content(hovered))
.when(ring, |this| this.child(press::ring()))
}
impl RenderOnce for ComposerAttachmentStrip {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let texts = self.texts.clone();
let (side, x_at, x_pad) = thumb_geometry(self.in_composer);
let mut children: Vec<AnyElement> = Vec::new();
for image in self.images {
let name = image.name.clone();
let key = |part: &str| -> ElementId {
(id.clone(), SharedString::from(format!("{part}-{name}"))).into()
};
let remove_name = (texts.remove)(&image.name);
let remove = self.on_remove.clone().map(|f| {
let name = image.name.clone();
Rc::new(move |w: &mut Window, cx: &mut App| f(name.clone(), w, cx))
as Rc<dyn Fn(&mut Window, &mut App)>
});
if image.state == AttachmentImageState::Error {
let retry = self.on_retry.clone().map(|f| {
let name = image.name.clone();
Rc::new(move |w: &mut Window, cx: &mut App| f(name.clone(), w, cx))
as Rc<dyn Fn(&mut Window, &mut App)>
});
children.push(
div()
.flex()
.flex_row()
.flex_none()
.items_center()
.gap(rem(8.))
.h(rem(32.))
.pl(rem(8.))
.pr(rem(4.))
.bg(color(ColorRole::NotificationBackgroundError))
.shadow(press::inset_frame(color(ColorRole::SupportError)))
.text_size(rem(14.))
.line_height(rem(18.))
.whitespace_nowrap()
.child(press::icon(
CarbonIcon::ErrorFilled,
16.,
color(ColorRole::SupportError),
))
.child(
div()
.text_color(color(ColorRole::TextPrimary))
.child(image.name.clone()),
)
.child(
div()
.text_size(rem(12.))
.text_color(color(ColorRole::TextError))
.child(image.error.clone().unwrap_or(texts.upload_failed.clone())),
)
.child(
bare_button(
key("retry"),
texts.retry.clone(),
Fill {
hover: None,
focus: Some(color(ColorRole::LayerHover01)),
},
|hovered| {
div()
.text_color(color(ColorRole::LinkPrimary))
.when(hovered, |this| this.underline())
.child(texts.retry.clone())
.into_any_element()
},
retry,
window,
cx,
)
.h(rem(24.))
.px(rem(4.)),
)
.child(
bare_button(
key("x"),
remove_name,
Fill {
hover: Some(color(ColorRole::LayerHover01)),
focus: Some(color(ColorRole::LayerHover01)),
},
|_| {
press::icon(
CarbonIcon::Close,
12.,
color(ColorRole::IconPrimary),
)
.into_any_element()
},
remove,
window,
cx,
)
.size(rem(24.)),
)
.into_any_element(),
);
continue;
}
let uploading = image.state == AttachmentImageState::Uploading;
let open = self.on_open.clone().filter(|_| !uploading).map(|f| {
let name = image.name.clone();
Rc::new(move |w: &mut Window, cx: &mut App| f(name.clone(), w, cx))
as Rc<dyn Fn(&mut Window, &mut App)>
});
let picture: AnyElement = match image.source.clone() {
Some(source) => img(source)
.size_full()
.object_fit(ObjectFit::Cover)
.into_any_element(),
None => div()
.size_full()
.flex()
.items_center()
.justify_center()
.when_some(image.color, |this, c| this.bg(c))
.child(press::icon(
CarbonIcon::Image,
20.,
color(ColorRole::TextOnColor).opacity(0.8),
))
.into_any_element(),
};
let x = bare_button(
key("thumb-x"),
remove_name,
Fill {
hover: None,
focus: None,
},
|_| {
press::icon(CarbonIcon::Close, 12., color(ColorRole::TextInverse))
.into_any_element()
},
remove,
window,
cx,
)
.p(px(x_pad))
.bg(color(ColorRole::BackgroundInverse))
.when(self.in_composer, |this| {
this.shadow(vec![
BoxShadow::new(px(0.), px(0.), color(ColorRole::Layer01)).spread_radius(px(1.)),
])
});
children.push(
div()
.relative()
.flex_none()
.size(rem(side))
.bg(color(ColorRole::LayerAccent01))
.child(
div()
.absolute()
.inset_0()
.overflow_hidden()
.child(match open {
Some(f) => bare_button(
key("open"),
(texts.open)(&image.name),
Fill {
hover: None,
focus: None,
},
|_| picture,
Some(f),
window,
cx,
)
.size_full()
.into_any_element(),
None => picture,
})
.when(uploading, |this| {
this.child(
div()
.absolute()
.inset_0()
.flex()
.items_center()
.justify_center()
.bg(upload_scrim(self.in_composer))
.child(
Spin::new(key("spin"))
.label(texts.uploading.clone())
.color(gpui_kit::white())
.track(gpui_kit::white().opacity(0.3)),
),
)
}),
)
.when(self.in_composer, |this| {
this.child(
div()
.absolute()
.inset_0()
.border_1()
.border_color(color(ColorRole::BorderSubtle01)),
)
})
.child(
div()
.absolute()
.top(px(x_at))
.right(px(x_at))
.occlude()
.child(x),
)
.into_any_element(),
);
}
for file in self.files {
let mut chip = ContextChip::new(
(
id.clone(),
SharedString::from(format!("file-{}", file.name)),
),
file.kind,
file.name.clone(),
)
.shape(ContextChipShape::Bubble)
.in_composer(self.in_composer)
.remove_label((texts.remove)(&file.name));
if let Some(size) = file.size {
chip = chip.meta(size);
}
let remove = self.on_remove.clone();
let name = file.name.clone();
chip = chip.on_remove(move |w, cx| {
if let Some(f) = remove.as_ref() {
f(name.clone(), w, cx)
}
});
children.push(chip.into_any_element());
}
div()
.flex()
.flex_row()
.flex_wrap()
.gap(rem(8.))
.when(self.in_composer, |this| this.items_center())
.children(children)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn composer_thumbs_are_32px_with_a_hanging_x() {
assert_eq!(thumb_geometry(false), (64., 2., 2.));
assert_eq!(thumb_geometry(true), (32., -6., 1.));
}
struct Strip {
heard: Rc<std::cell::RefCell<Vec<String>>>,
}
impl gpui_kit::Render for Strip {
fn render(&mut self, _: &mut Window, _: &mut gpui_kit::Context<Self>) -> impl IntoElement {
let (opened, removed) = (self.heard.clone(), self.heard.clone());
div().size_full().child(
ComposerAttachmentStrip::new("strip", vec![AttachmentImage::new("a.png")], vec![])
.on_open(move |name, _, _| opened.borrow_mut().push(format!("open {name}")))
.on_remove(move |name, _, _| {
removed.borrow_mut().push(format!("remove {name}"));
}),
)
}
}
#[gpui_kit::test]
fn a_thumbnail_opens_and_its_x_only_removes(cx: &mut gpui_kit::TestAppContext) {
let heard = Rc::new(std::cell::RefCell::new(Vec::new()));
let (_, window) = cx.add_window_view({
let heard = heard.clone();
|_, _| Strip { heard }
});
window.simulate_click(
gpui_kit::point(px(16.), px(48.)),
gpui_kit::Modifiers::none(),
);
window.simulate_click(
gpui_kit::point(px(56.), px(8.)),
gpui_kit::Modifiers::none(),
);
assert_eq!(*heard.borrow(), ["open a.png", "remove a.png"]);
}
#[test]
fn upload_scrims_follow_the_css() {
assert!((upload_scrim(false).a - 0.5).abs() < 0.01);
assert!((upload_scrim(true).a - 0.55).abs() < 0.01);
}
}