use super::button::{Button, ButtonSize, ButtonVariant};
use super::form::rem;
use super::spinner::{Spinner, SpinnerSize};
use crate::{
icons::{CarbonIcon, Icon, IconSize},
theme::color,
tokens::{ColorRole, SIZE_LG, SIZE_MD, SIZE_SM, SPACING_02, SPACING_03, SPACING_04, TypeStyle},
typography::TypeStyled as _,
};
use gpui_kit::{
AnyElement, App, ElementId, Hsla, ImageSource, IntoElement, ObjectFit, ParentElement,
PathPromptOptions, RenderOnce, SharedString, Styled, StyledImage as _, Window, div, img,
prelude::FluentBuilder as _, px,
};
use std::{path::PathBuf, rc::Rc};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum AttachmentState {
Idle,
Uploading,
Processing,
Error,
#[default]
Done,
}
impl AttachmentState {
pub const ALL: [AttachmentState; 5] = [
Self::Idle,
Self::Uploading,
Self::Processing,
Self::Error,
Self::Done,
];
pub const fn name(self) -> &'static str {
match self {
Self::Idle => "idle",
Self::Uploading => "uploading",
Self::Processing => "processing",
Self::Error => "error",
Self::Done => "done",
}
}
pub const fn is_busy(self) -> bool {
matches!(self, Self::Uploading | Self::Processing)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum AttachmentSize {
#[default]
Default,
Sm,
Xs,
}
impl AttachmentSize {
pub const ALL: [AttachmentSize; 3] = [Self::Default, Self::Sm, Self::Xs];
pub const fn name(self) -> &'static str {
match self {
Self::Default => "default",
Self::Sm => "sm",
Self::Xs => "xs",
}
}
pub const fn geometry(self) -> (f32, f32) {
match self {
Self::Default => (SIZE_LG.px_value(), SPACING_04.px_value()),
Self::Sm => (SIZE_MD.px_value(), SPACING_04.px_value()),
Self::Xs => (SIZE_SM.px_value(), SPACING_03.px_value()),
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum AttachmentOrientation {
#[default]
Horizontal,
Vertical,
}
impl AttachmentOrientation {
pub const ALL: [AttachmentOrientation; 2] = [Self::Horizontal, Self::Vertical];
pub const fn name(self) -> &'static str {
match self {
Self::Horizontal => "horizontal",
Self::Vertical => "vertical",
}
}
}
pub const MAX_WIDTH: f32 = 320.;
pub const VERTICAL_WIDTH: f32 = 160.;
pub const VERTICAL_IMAGE: f32 = 96.;
pub const IMAGE: f32 = 32.;
pub const ERROR_OUTLINE: f32 = 2.;
pub fn colors(state: AttachmentState) -> (Hsla, Option<Hsla>) {
(
color(ColorRole::Layer01),
(state == AttachmentState::Error).then(|| color(ColorRole::SupportError)),
)
}
pub struct AttachmentMedia {
icon: Icon,
image: Option<ImageSource>,
}
impl Default for AttachmentMedia {
fn default() -> Self {
Self::new()
}
}
impl AttachmentMedia {
pub fn new() -> Self {
Self {
icon: Icon::carbon(CarbonIcon::Document),
image: None,
}
}
pub fn icon(mut self, icon: impl Into<Icon>) -> Self {
self.icon = icon.into();
self
}
pub fn image(mut self, source: impl Into<ImageSource>) -> Self {
self.image = Some(source.into());
self
}
fn render(self, vertical: bool) -> AnyElement {
let base = div().flex().flex_none().items_center().justify_center();
match self.image {
Some(source) => base
.overflow_hidden()
.map(|this| {
if vertical {
this.w_full().h(rem(VERTICAL_IMAGE))
} else {
this.size(rem(IMAGE))
}
})
.child(img(source).size_full().object_fit(ObjectFit::Cover))
.into_any_element(),
None => base
.child(
self.icon
.size(IconSize::S20)
.color(color(ColorRole::IconPrimary)),
)
.into_any_element(),
}
}
}
#[derive(IntoElement, Default)]
pub struct AttachmentContent {
children: Vec<AnyElement>,
}
impl AttachmentContent {
pub fn new() -> Self {
Self::default()
}
}
impl ParentElement for AttachmentContent {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for AttachmentContent {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
div()
.flex()
.flex_1()
.flex_col()
.min_w_0()
.gap(px(2.))
.children(self.children)
}
}
#[derive(IntoElement)]
pub struct AttachmentTitle(SharedString);
impl AttachmentTitle {
pub fn new(text: impl Into<SharedString>) -> Self {
Self(text.into())
}
}
impl RenderOnce for AttachmentTitle {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
div()
.w_full()
.overflow_hidden()
.whitespace_nowrap()
.text_ellipsis()
.type_style(TypeStyle::BodyCompact01)
.text_color(color(ColorRole::TextPrimary))
.child(self.0)
}
}
#[derive(IntoElement)]
pub struct AttachmentDescription(SharedString);
impl AttachmentDescription {
pub fn new(text: impl Into<SharedString>) -> Self {
Self(text.into())
}
}
impl RenderOnce for AttachmentDescription {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
div()
.type_style(TypeStyle::Label01)
.text_color(color(ColorRole::TextSecondary))
.child(self.0)
}
}
#[derive(IntoElement)]
pub struct AttachmentStatus {
id: ElementId,
state: AttachmentState,
label: Option<SharedString>,
}
impl AttachmentStatus {
pub fn new(id: impl Into<ElementId>, state: AttachmentState) -> Self {
Self {
id: id.into(),
state,
label: None,
}
}
pub fn label(mut self, label: impl Into<SharedString>) -> Self {
self.label = Some(label.into());
self
}
}
impl RenderOnce for AttachmentStatus {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
match self.state {
s if s.is_busy() => div()
.flex()
.flex_row()
.items_center()
.text_color(color(ColorRole::TextSecondary))
.child(
div()
.flex()
.flex_none()
.mr(SPACING_03)
.child(Spinner::new(self.id).size(SpinnerSize::Sm)),
)
.when_some(self.label, |this, text| {
this.child(div().type_style(TypeStyle::Label02).child(text))
})
.into_any_element(),
AttachmentState::Error => div()
.flex()
.flex_row()
.items_center()
.gap(SPACING_02)
.text_size(rem(12.))
.text_color(color(ColorRole::TextError))
.child(
Icon::carbon(CarbonIcon::ErrorFilled)
.size(IconSize::S16)
.color(color(ColorRole::SupportError)),
)
.children(self.label)
.into_any_element(),
_ => gpui_kit::Empty.into_any_element(),
}
}
}
type ClickHandler = Rc<dyn Fn(&mut Window, &mut App)>;
pub struct AttachmentAction {
id: ElementId,
label: SharedString,
icon: Icon,
on_click: Option<ClickHandler>,
}
impl AttachmentAction {
pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
label: label.into(),
icon: Icon::carbon(CarbonIcon::Close),
on_click: None,
}
}
pub fn icon(mut self, icon: impl Into<Icon>) -> Self {
self.icon = icon.into();
self
}
pub fn on_click(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_click = Some(Rc::new(f));
self
}
}
impl IntoElement for AttachmentAction {
type Element = <Button as IntoElement>::Element;
fn into_element(self) -> Self::Element {
let on_click = self.on_click;
Button::new(self.id, self.label)
.variant(ButtonVariant::Ghost)
.size(ButtonSize::Sm)
.icon(self.icon)
.icon_only(true)
.when_some(on_click, |b, f| {
b.on_click(move |_, window, cx| f(window, cx))
})
.into_element()
}
}
#[derive(Default)]
pub struct AttachmentActions {
children: Vec<AnyElement>,
}
impl AttachmentActions {
pub fn new() -> Self {
Self::default()
}
}
impl ParentElement for AttachmentActions {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
#[derive(IntoElement, Default)]
pub struct Attachment {
state: AttachmentState,
size: AttachmentSize,
orientation: AttachmentOrientation,
media: Option<AttachmentMedia>,
content: Option<AttachmentContent>,
actions: Option<AttachmentActions>,
}
impl Attachment {
pub fn new() -> Self {
Self::default()
}
pub fn state(mut self, state: AttachmentState) -> Self {
self.state = state;
self
}
pub fn size(mut self, size: AttachmentSize) -> Self {
self.size = size;
self
}
pub fn orientation(mut self, orientation: AttachmentOrientation) -> Self {
self.orientation = orientation;
self
}
pub fn media(mut self, media: AttachmentMedia) -> Self {
self.media = Some(media);
self
}
pub fn content(mut self, content: AttachmentContent) -> Self {
self.content = Some(content);
self
}
pub fn actions(mut self, actions: AttachmentActions) -> Self {
self.actions = Some(actions);
self
}
}
impl RenderOnce for Attachment {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let vertical = self.orientation == AttachmentOrientation::Vertical;
let (fill, outline) = colors(self.state);
let (min_height, gap) = self.size.geometry();
let inset = |e: gpui_kit::Div| if vertical { e.px(SPACING_04) } else { e };
div()
.relative()
.flex()
.bg(fill)
.map(|this| {
if vertical {
this.flex_col()
.w(rem(VERTICAL_WIDTH))
.pb(SPACING_03)
.gap(SPACING_03)
} else {
this.flex_row()
.items_center()
.max_w(rem(MAX_WIDTH))
.gap(rem(gap))
.pl(SPACING_04)
.pr(SPACING_02)
}
})
.min_h(rem(min_height))
.children(self.media.map(|m| m.render(vertical)))
.children(
self.content
.map(|c| inset(div().flex().min_w_0().flex_1().child(c))),
)
.children(
self.actions
.map(|a| inset(div().flex().flex_none().children(a.children))),
)
.when_some(outline, |this, line| {
this.child(
div()
.absolute()
.top_0()
.left_0()
.size_full()
.border(px(ERROR_OUTLINE))
.border_color(line),
)
})
}
}
#[derive(IntoElement, Default)]
pub struct AttachmentGroup {
children: Vec<AnyElement>,
}
impl AttachmentGroup {
pub fn new() -> Self {
Self::default()
}
}
impl ParentElement for AttachmentGroup {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for AttachmentGroup {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
div()
.flex()
.flex_row()
.flex_wrap()
.items_start()
.gap(SPACING_03)
.children(self.children)
}
}
type FilesHandler = Rc<dyn Fn(Vec<PathBuf>, &mut App)>;
pub struct AttachmentTrigger {
id: ElementId,
label: SharedString,
multiple: bool,
on_files: Option<FilesHandler>,
}
impl AttachmentTrigger {
pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
label: label.into(),
multiple: false,
on_files: None,
}
}
pub fn multiple(mut self, multiple: bool) -> Self {
self.multiple = multiple;
self
}
pub fn on_files(mut self, f: impl Fn(Vec<PathBuf>, &mut App) + 'static) -> Self {
self.on_files = Some(Rc::new(f));
self
}
}
impl IntoElement for AttachmentTrigger {
type Element = <Button as IntoElement>::Element;
fn into_element(self) -> Self::Element {
let on_files = self.on_files;
let multiple = self.multiple;
let prompt = self.label.clone();
Button::new(self.id, self.label)
.variant(ButtonVariant::Ghost)
.size(ButtonSize::Sm)
.on_click(move |_, _, cx| {
let receiver = cx.prompt_for_paths(PathPromptOptions {
files: true,
directories: false,
multiple,
prompt: Some(prompt.clone()),
});
let on_files = on_files.clone();
cx.spawn(async move |cx| {
if let Ok(Ok(Some(paths))) = receiver.await
&& let Some(f) = on_files
{
cx.update(|cx| f(paths, cx));
}
})
.detach();
})
.into_element()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tokens::CarbonTheme;
#[test]
fn geometry_follows_custom_css() {
assert_eq!(AttachmentSize::Default.geometry(), (48., 12.));
assert_eq!(AttachmentSize::Sm.geometry(), (40., 12.));
assert_eq!(AttachmentSize::Xs.geometry(), (32., 8.));
assert_eq!(
(MAX_WIDTH, VERTICAL_WIDTH, VERTICAL_IMAGE, IMAGE),
(320., 160., 96., 32.)
);
assert_eq!(ERROR_OUTLINE, 2.);
}
#[test]
fn colors_follow_custom_css() {
crate::theme::tests_set(CarbonTheme::White);
assert_eq!(
colors(AttachmentState::Done),
(color(ColorRole::Layer01), None)
);
assert_eq!(
colors(AttachmentState::Error).1,
Some(color(ColorRole::SupportError))
);
assert!(AttachmentState::Uploading.is_busy());
assert!(!AttachmentState::Error.is_busy());
}
}