use crate::Tokens;
use gpui_kit::component::Sizable as _;
use gpui_kit::component::attachment::{
Attachment, AttachmentContent, AttachmentMedia, AttachmentStatus, AttachmentTitle,
};
use gpui_kit::component::v_flex;
use gpui_kit::{
Div, ElementId, InteractiveElement as _, ParentElement as _, SharedString, Stateful,
StatefulInteractiveElement as _, Styled as _, TestSupportExt as _, div, rems,
};
const TILE_WIDTH_REM: f32 = 13.;
const TILE_MIN_HEIGHT_REM: f32 = 8.75;
pub fn tile(
id: impl Into<ElementId>,
label: impl Into<SharedString>,
tokens: &Tokens,
) -> Stateful<Div> {
v_flex()
.id(id)
.aria_label(label)
.relative()
.w(rems(TILE_WIDTH_REM))
.min_h(rems(TILE_MIN_HEIGHT_REM))
.gap_1()
.p_2()
.rounded(tokens.control_radius)
.border_1()
.border_color(tokens.line)
.bg(tokens.pane)
}
pub fn file_chip(label: impl Into<SharedString>, tokens: &Tokens) -> Div {
div()
.px_2()
.py_1()
.rounded(tokens.control_radius)
.bg(tokens.pane)
.border_1()
.border_color(tokens.line)
.child(label.into())
}
pub enum Thumb {
Image(std::sync::Arc<gpui_kit::RenderImage>),
Text,
File,
Loading,
Retry,
}
const THUMB_REM: f32 = 6.;
const KB: f64 = 1000.;
pub fn size_text(bytes: usize) -> String {
let value = bytes as f64;
if value < KB {
format!("{bytes} bytes")
} else if value < KB * KB {
format!("{:.1} KB", value / KB)
} else {
format!("{:.1} MB", value / (KB * KB))
}
}
pub fn thumbnail(
id: impl Into<ElementId>,
thumb: Thumb,
(name, label, tooltip): (SharedString, SharedString, SharedString),
(ring, open): (
bool,
impl Fn(&mut gpui_kit::Window, &mut gpui_kit::App) + 'static,
),
tokens: &Tokens,
) -> impl gpui_kit::IntoElement {
let id = id.into();
let card = attachment_card(
ElementId::from((id.clone(), "attachment")),
thumb,
name,
tooltip,
);
div()
.id(id)
.relative()
.flex_none()
.cursor_pointer()
.role(gpui_kit::accesskit::Role::Button)
.aria_label(label)
.on_click(move |_, window, cx| open(window, cx))
.child(card)
.children(ring.then(|| focus_ring(tokens)))
.test_support()
}
fn attachment_card(
id: ElementId,
thumb: Thumb,
name: SharedString,
tooltip: SharedString,
) -> Attachment {
dress(Attachment::new().id(id).tooltip(tooltip), face(thumb), name)
}
fn face(
thumb: Thumb,
) -> Result<std::sync::Arc<gpui_kit::RenderImage>, (subspace_lucide::IconName, AttachmentStatus)> {
use subspace_lucide::IconName;
match thumb {
Thumb::Image(image) => Ok(image),
Thumb::Text => Err((IconName::FileText, AttachmentStatus::Complete)),
Thumb::Loading => Err((IconName::File, AttachmentStatus::Processing)),
Thumb::Retry => Err((IconName::FileX, AttachmentStatus::Failed)),
Thumb::File => Err((IconName::File, AttachmentStatus::Complete)),
}
}
fn dress(
card: Attachment,
face: Result<
std::sync::Arc<gpui_kit::RenderImage>,
(subspace_lucide::IconName, AttachmentStatus),
>,
name: SharedString,
) -> Attachment {
match face {
Ok(image) => card
.axis(gpui_kit::Axis::Vertical)
.large()
.size(rems(THUMB_REM))
.media(AttachmentMedia::new().src(image)),
Err((icon, status)) => card
.small()
.status(status)
.media(AttachmentMedia::new().child(gpui_kit::component::Icon::from(icon)))
.content(AttachmentContent::new().title(AttachmentTitle::new(name))),
}
}
const RING_OUTSET: f32 = 3.;
pub fn focus_ring(tokens: &Tokens) -> Div {
let outset = gpui_kit::px(-RING_OUTSET);
div()
.absolute()
.top(outset)
.left(outset)
.right(outset)
.bottom(outset)
.rounded(tokens.control_radius)
.border_2()
.border_color(tokens.accent)
}
pub fn unavailable_chip(
id: impl Into<ElementId>,
key: impl Into<SharedString>,
ring: bool,
tokens: &Tokens,
) -> impl gpui_kit::IntoElement {
let key = key.into();
gpui_kit::component::h_flex()
.id(id)
.relative()
.aria_label(SharedString::from(format!("Attachment unavailable: {key}")))
.gap_1p5()
.px_2()
.py_0p5()
.rounded(tokens.control_radius)
.border_1()
.border_color(tokens.line)
.text_xs()
.text_color(tokens.muted)
.child(gpui_kit::component::Icon::from(subspace_lucide::IconName::FileX).xsmall())
.child("Attachment unavailable")
.child(key)
.children(ring.then(|| focus_ring(tokens)))
.test_support()
}
#[cfg(test)]
mod tests {
#[test]
fn sizes_read_in_decimal_bytes_kilobytes_and_megabytes() {
assert_eq!(super::size_text(5), "5 bytes");
assert_eq!(super::size_text(1500), "1.5 KB");
assert_eq!(super::size_text(25_000_000), "25.0 MB");
assert_eq!(super::size_text(999), "999 bytes");
}
}