use crate::{
icons::{CarbonIcon, Icon},
t3::{
craft::{mono, rem, sans},
press::inset_frame,
},
theme::color,
tokens::{self, ColorRole},
u0::primitives::{
IconButton,
parts::{focus_ring, focus_visible, keyed_focus},
},
};
use gpui_kit::{
App, CursorStyle, ElementId, Hsla, InteractiveElement as _, IntoElement, ParentElement,
RenderOnce, SharedString, StatefulInteractiveElement as _, Styled, Window, div,
prelude::FluentBuilder as _, px,
};
use std::rc::Rc;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ChipKind {
#[default]
Quote,
TerminalOutput,
ReviewComment,
PullRequest,
Issue,
}
impl ChipKind {
pub const ALL: [Self; 5] = [
Self::Quote,
Self::TerminalOutput,
Self::ReviewComment,
Self::PullRequest,
Self::Issue,
];
pub const fn name(self) -> &'static str {
match self {
Self::Quote => "quote",
Self::TerminalOutput => "terminal-output",
Self::ReviewComment => "review-comment",
Self::PullRequest => "pr",
Self::Issue => "issue",
}
}
pub const fn glyph_and_label(self) -> (CarbonIcon, &'static str) {
match self {
Self::Quote => (CarbonIcon::Quotes, "Quote"),
Self::TerminalOutput => (CarbonIcon::Terminal, "Terminal output"),
Self::ReviewComment => (CarbonIcon::Chat, "Review comment"),
Self::PullRequest => (CarbonIcon::PullRequest, "Pull request"),
Self::Issue => (CarbonIcon::Ticket, "Issue"),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ChipState {
Open,
Merged,
Closed,
Draft,
}
impl ChipState {
pub const ALL: [Self; 4] = [Self::Open, Self::Merged, Self::Closed, Self::Draft];
pub const fn name(self) -> &'static str {
match self {
Self::Open => "open",
Self::Merged => "merged",
Self::Closed => "closed",
Self::Draft => "draft",
}
}
pub fn label(self) -> String {
let name = self.name();
name[..1].to_uppercase() + &name[1..]
}
}
pub fn chip_state_dot(state: ChipState) -> Hsla {
color(match state {
ChipState::Open => ColorRole::SupportSuccess,
ChipState::Merged => ColorRole::SupportInfo,
ChipState::Closed => ColorRole::SupportError,
ChipState::Draft => ColorRole::IconSecondary,
})
}
#[derive(Clone)]
pub struct ComposerChipTexts {
pub kind: Option<SharedString>,
pub remove: Rc<dyn Fn(&str) -> SharedString>,
}
impl Default for ComposerChipTexts {
fn default() -> Self {
Self {
kind: None,
remove: Rc::new(|kind| format!("Remove {}", kind.to_lowercase()).into()),
}
}
}
type Handler = Rc<dyn Fn(&mut Window, &mut App)>;
#[derive(IntoElement)]
pub struct ComposerChip {
id: ElementId,
kind: ChipKind,
title: SharedString,
excerpt: Option<SharedString>,
meta: Option<SharedString>,
state: Option<ChipState>,
on_open: Option<Handler>,
on_remove: Option<Handler>,
texts: ComposerChipTexts,
}
impl ComposerChip {
pub fn new(id: impl Into<ElementId>, kind: ChipKind, title: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
kind,
title: title.into(),
excerpt: None,
meta: None,
state: None,
on_open: None,
on_remove: None,
texts: ComposerChipTexts::default(),
}
}
pub fn excerpt(mut self, excerpt: impl Into<SharedString>) -> Self {
self.excerpt = Some(excerpt.into());
self
}
pub fn meta(mut self, meta: impl Into<SharedString>) -> Self {
self.meta = Some(meta.into());
self
}
pub fn state(mut self, state: ChipState) -> Self {
self.state = Some(state);
self
}
pub fn on_open(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_open = Some(Rc::new(f));
self
}
pub fn on_remove(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_remove = Some(Rc::new(f));
self
}
pub fn texts(mut self, texts: ComposerChipTexts) -> Self {
self.texts = texts;
self
}
}
impl RenderOnce for ComposerChip {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
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);
let (glyph, label) = self.kind.glyph_and_label();
let label: SharedString = self.texts.kind.clone().unwrap_or(label.into());
let secondary = color(ColorRole::TextSecondary);
let on_open = self.on_open.clone();
let excerpt = self.excerpt.clone().map(|ex| {
let base = match self.kind {
ChipKind::TerminalOutput => mono(div(), 12., 400),
_ => sans(div(), 12., 400),
};
base.w_full()
.line_clamp(2)
.overflow_hidden()
.text_color(secondary)
.map(|d| match self.kind {
ChipKind::Quote => d
.border_l_2()
.border_color(color(ColorRole::BorderStrong01))
.pl(tokens::SPACING_03)
.italic(),
ChipKind::TerminalOutput => d
.py(px(2.))
.px(tokens::SPACING_02)
.bg(color(ColorRole::TerminalBackground))
.text_color(color(ColorRole::TerminalText)),
_ => d,
})
.child(ex)
});
let remove = self.on_remove.clone().map(|f| {
IconButton::new(
(id.clone(), "remove"),
CarbonIcon::Close,
(self.texts.remove)(&label),
)
.size_px(28.)
.icon_px(14.)
.on_click(move |_, w, cx| f(w, cx))
});
div()
.id(id.clone())
.flex()
.items_stretch()
.w_full()
.max_w(rem(352.))
.bg(color(ColorRole::Layer02))
.shadow(inset_frame(color(ColorRole::BorderSubtle01)))
.child(
div()
.flex_none()
.pt(tokens::SPACING_03)
.pl(tokens::SPACING_03)
.child(
Icon::carbon(glyph)
.size_length(rem(16.))
.color(color(ColorRole::IconSecondary)),
),
)
.child(
gpui_kit::base::Button::new((id.clone(), "main"))
.track_focus(&focus)
.relative()
.flex()
.flex_col()
.items_start()
.flex_1()
.min_w_0()
.gap(px(2.))
.p(tokens::SPACING_03)
.when(over, |b| b.bg(color(ColorRole::LayerHover01)))
.text_color(color(ColorRole::TextPrimary))
.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| {
if let Some(f) = &on_open {
f(w, cx);
}
})
.child(
sans(div(), 12., 400)
.flex()
.items_center()
.gap(tokens::SPACING_03)
.child(
div()
.text_color(secondary)
.child(SharedString::from(label.to_uppercase())),
)
.when_some(self.state, |d, s| {
d.child(
sans(div(), 12., 600)
.flex()
.items_center()
.gap(px(4.))
.text_color(color(ColorRole::TextPrimary))
.child(
div().size(px(6.)).rounded_full().bg(chip_state_dot(s)),
)
.child(s.label()),
)
})
.when_some(self.meta.clone(), |d, m| {
d.child(div().text_color(secondary).child(m))
}),
)
.child(
sans(div(), 13., 400)
.min_w_0()
.truncate()
.child(self.title.clone()),
)
.children(excerpt)
.when(ring, |b| b.child(focus_ring())),
)
.children(remove)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{CarbonTheme, theme::color_in};
#[test]
fn kinds_and_states() {
assert_eq!(
ChipKind::TerminalOutput.glyph_and_label(),
(CarbonIcon::Terminal, "Terminal output")
);
assert_eq!(ChipKind::PullRequest.name(), "pr");
assert_eq!(ChipState::Closed.label(), "Closed");
crate::theme::tests_set(CarbonTheme::White);
let c = |r| color_in(CarbonTheme::White, r);
assert_eq!(
chip_state_dot(ChipState::Open),
c(ColorRole::SupportSuccess)
);
assert_eq!(chip_state_dot(ChipState::Merged), c(ColorRole::SupportInfo));
assert_eq!(
chip_state_dot(ChipState::Closed),
c(ColorRole::SupportError)
);
assert_eq!(
chip_state_dot(ChipState::Draft),
c(ColorRole::IconSecondary)
);
assert_eq!(
(ComposerChipTexts::default().remove)("Terminal output"),
"Remove terminal output"
);
}
}