use std::rc::Rc;
use gpui::{
div, prelude::*, px, AnyElement, App, ClickEvent, ElementId, SharedString, StyleRefinement,
Window,
};
use crate::sx::SxStyled;
use crate::{
hooks::EventHandler,
icon::{Icon, IconName},
styles,
styles::ApplyStyleOverrides,
};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ItemVariant {
#[default]
Default,
Outline,
Muted,
}
#[derive(IntoElement)]
pub struct Item {
id: ElementId,
variant: ItemVariant,
small: bool,
icon: Option<IconName>,
media: Option<AnyElement>,
title: Option<SharedString>,
description: Option<SharedString>,
actions: Vec<AnyElement>,
children: Vec<AnyElement>,
on_click: Option<EventHandler<ClickEvent>>,
sx: crate::sx::Sx,
style_overrides: StyleRefinement,
}
crate::implement_style_overrides!(Item);
impl Item {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
variant: ItemVariant::Default,
small: false,
icon: None,
media: None,
title: None,
description: None,
actions: Vec::new(),
children: Vec::new(),
on_click: None,
sx: crate::sx::Sx::new(),
style_overrides: StyleRefinement::default(),
}
}
#[must_use]
pub fn variant(mut self, variant: ItemVariant) -> Self {
self.variant = variant;
self
}
#[must_use]
pub fn outline(self) -> Self {
self.variant(ItemVariant::Outline)
}
#[must_use]
pub fn muted(self) -> Self {
self.variant(ItemVariant::Muted)
}
#[must_use]
pub fn small(mut self) -> Self {
self.small = true;
self
}
#[must_use]
pub fn icon(mut self, icon: IconName) -> Self {
self.icon = Some(icon);
self
}
#[must_use]
pub fn media(mut self, media: impl IntoElement) -> Self {
self.media = Some(media.into_any_element());
self
}
#[must_use]
pub fn title(mut self, title: impl Into<SharedString>) -> Self {
self.title = Some(title.into());
self
}
#[must_use]
pub fn description(mut self, description: impl Into<SharedString>) -> Self {
self.description = Some(description.into());
self
}
#[must_use]
pub fn action(mut self, action: impl IntoElement) -> Self {
self.actions.push(action.into_any_element());
self
}
#[must_use]
pub fn on_click(
mut self,
handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
) -> Self {
self.on_click = Some(Rc::new(handler));
self
}
}
impl ParentElement for Item {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
styles! {
ITEM = {
root: {
display: flex,
align: center,
gap: 4,
width: full,
padding_x: 4,
padding_y: 4,
radius: md,
border: 1,
border_color: transparent,
text: sm,
},
small: { gap: 2.5, padding_y: 3 },
variant(ItemVariant): {
Default: {},
Outline: { border_color: border },
Muted: { background: muted/50 },
},
clickable: { cursor: pointer, hover: { background: accent/50 } },
icon: {
flex: none,
size: 8,
display: flex,
align: center,
justify: center,
radius: sm,
border: 1,
border_color: border,
background: muted,
},
media: { flex: none },
content: { display: flex, direction: column, flex: 1, gap: 1, min_width: 0 },
title: { font: medium, line_height: 5 },
description: { color: muted_foreground, line_clamp: 2 },
actions: { display: flex, align: center, gap: 2 },
group: { display: flex, direction: column, width: full },
divider: { height: 0.25, margin_x: 4, background: border },
}
}
impl RenderOnce for Item {
fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
div()
.id(self.id)
.sx((
&ITEM.root,
self.small.then_some(&ITEM.small),
ITEM.variant(self.variant),
self.on_click.is_some().then_some(&ITEM.clickable),
&self.sx,
))
.when_some(self.on_click, |item, handler| {
item.on_click(move |event, window, cx| handler(event, window, cx))
})
.when_some(self.icon, |item, icon| {
item.child(div().sx(&ITEM.icon).child(Icon::new(icon).size(px(16.))))
})
.when_some(self.media, |item, media| {
item.child(div().sx(&ITEM.media).child(media))
})
.child(
div()
.sx(&ITEM.content)
.when_some(self.title, |content, title| {
content.child(
div()
.sx(&ITEM.title)
.child(crate::components::bidi_text::text(title)),
)
})
.when_some(self.description, |content, description| {
content.child(
div()
.sx(&ITEM.description)
.child(crate::components::bidi_text::text(description)),
)
})
.children(self.children),
)
.when(!self.actions.is_empty(), |item| {
item.child(div().sx(&ITEM.actions).children(self.actions))
})
.apply_style_overrides(&self.style_overrides)
}
}
#[derive(IntoElement)]
pub struct ItemGroup {
items: Vec<AnyElement>,
sx: crate::sx::Sx,
style_overrides: StyleRefinement,
}
crate::implement_style_overrides!(ItemGroup);
impl ItemGroup {
#[must_use]
pub fn new() -> Self {
Self {
items: Vec::new(),
sx: crate::sx::Sx::new(),
style_overrides: StyleRefinement::default(),
}
}
}
impl Default for ItemGroup {
fn default() -> Self {
Self::new()
}
}
impl ParentElement for ItemGroup {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.items.extend(elements);
}
}
impl RenderOnce for ItemGroup {
fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
let count = self.items.len();
let mut children = Vec::with_capacity(count * 2);
for (index, item) in self.items.into_iter().enumerate() {
children.push(item);
if index + 1 < count {
children.push(div().sx(&ITEM.divider).into_any_element());
}
}
div()
.sx((&ITEM.group, &self.sx))
.children(children)
.apply_style_overrides(&self.style_overrides)
}
}