use crate::t3::press::{self, Handler, inset_frame, rem};
use crate::{
icons::CarbonIcon,
theme::color,
tokens::{ColorRole, FontFamilyKey},
typography::font,
};
use gpui_kit::{
AnyElement, 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)]
#[allow(missing_docs)]
pub enum ContextChipKind {
#[default]
File,
Folder,
Image,
Video,
Mention,
Terminal,
Element,
PreviewAnnotation,
ReviewComment,
PullRequest,
PrOpen,
PrDraft,
PrMerged,
PrClosed,
Skill,
Model,
Thread,
Environment,
Citation,
DocPdf,
Issue,
}
impl ContextChipKind {
pub const ALL: [ContextChipKind; 21] = [
Self::File,
Self::Image,
Self::Video,
Self::Mention,
Self::Terminal,
Self::Element,
Self::PreviewAnnotation,
Self::ReviewComment,
Self::PullRequest,
Self::PrOpen,
Self::PrDraft,
Self::PrMerged,
Self::PrClosed,
Self::Skill,
Self::Model,
Self::Thread,
Self::Environment,
Self::Citation,
Self::DocPdf,
Self::Folder,
Self::Issue,
];
pub const fn name(self) -> &'static str {
match self {
Self::File => "file",
Self::Folder => "folder",
Self::Image => "image",
Self::Video => "video",
Self::Mention => "mention",
Self::Terminal => "terminal",
Self::Element => "element",
Self::PreviewAnnotation => "preview-annotation",
Self::ReviewComment => "review-comment",
Self::PullRequest => "pull-request",
Self::PrOpen => "pr-open",
Self::PrDraft => "pr-draft",
Self::PrMerged => "pr-merged",
Self::PrClosed => "pr-closed",
Self::Skill => "skill",
Self::Model => "model",
Self::Thread => "thread",
Self::Environment => "environment",
Self::Citation => "citation",
Self::DocPdf => "doc-pdf",
Self::Issue => "issue",
}
}
pub fn from_name(name: &str) -> Self {
Self::ALL
.into_iter()
.find(|k| k.name() == name)
.unwrap_or_default()
}
pub const fn icon(self) -> CarbonIcon {
match self {
Self::File => CarbonIcon::Document,
Self::Image => CarbonIcon::Image,
Self::Video => CarbonIcon::Video,
Self::Mention => CarbonIcon::User,
Self::Terminal => CarbonIcon::Terminal,
Self::Element => CarbonIcon::Code,
Self::PreviewAnnotation => CarbonIcon::Earth,
Self::ReviewComment | Self::Thread => CarbonIcon::Chat,
Self::PullRequest | Self::PrOpen | Self::PrDraft | Self::PrClosed => {
CarbonIcon::PullRequest
}
Self::PrMerged => CarbonIcon::Merge,
Self::Skill => CarbonIcon::MagicWand,
Self::Model => CarbonIcon::Model,
Self::Environment => CarbonIcon::Laptop,
Self::Citation => CarbonIcon::Quotes,
Self::DocPdf => CarbonIcon::DocumentPdf,
Self::Folder => CarbonIcon::Folder,
Self::Issue => CarbonIcon::Ticket,
}
}
pub const fn ink(self) -> ColorRole {
match self {
Self::File | Self::Folder => ColorRole::TagColorBlue,
Self::Image | Self::Video => ColorRole::TagColorMagenta,
Self::Mention | Self::Thread | Self::PrMerged => ColorRole::TagColorPurple,
Self::Terminal | Self::Environment => ColorRole::TagColorGray,
Self::Element | Self::PreviewAnnotation | Self::Model => ColorRole::TagColorCyan,
Self::ReviewComment | Self::Citation => ColorRole::TagColorWarmGray,
Self::PullRequest | Self::PrOpen => ColorRole::TagColorGreen,
Self::PrDraft => ColorRole::TextSecondary,
Self::PrClosed => ColorRole::TagColorRed,
Self::Skill => ColorRole::TagColorTeal,
Self::DocPdf => ColorRole::TextPrimary,
Self::Issue => ColorRole::LinkPrimary,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ContextChipState {
#[default]
Normal,
Unresolved,
Invalid,
}
impl ContextChipState {
pub const ALL: [ContextChipState; 3] = [Self::Normal, Self::Unresolved, Self::Invalid];
pub const fn name(self) -> &'static str {
match self {
Self::Normal => "",
Self::Unresolved => "unresolved",
Self::Invalid => "invalid",
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ContextChipShape {
#[default]
Inline,
Bubble,
}
impl ContextChipShape {
pub const ALL: [ContextChipShape; 2] = [Self::Inline, Self::Bubble];
pub const fn name(self) -> &'static str {
match self {
Self::Inline => "inline",
Self::Bubble => "bubble",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ChipLook {
pub ink: Hsla,
pub icon: Hsla,
pub label: Hsla,
pub remove: Hsla,
pub fill: Option<Hsla>,
pub frame: Option<Hsla>,
}
impl ChipLook {
pub fn of(
kind: ContextChipKind,
state: ContextChipState,
shape: ContextChipShape,
in_composer: bool,
) -> Self {
let ink = color(match state {
ContextChipState::Unresolved => ColorRole::TextSecondary,
ContextChipState::Invalid => ColorRole::TextError,
ContextChipState::Normal => kind.ink(),
});
let boxed = shape == ContextChipShape::Bubble || in_composer;
match shape {
ContextChipShape::Bubble => Self {
ink,
icon: ink,
label: color(ColorRole::TextPrimary),
remove: color(if in_composer {
ColorRole::IconSecondary
} else {
ColorRole::TextPrimary
}),
fill: Some(color(ColorRole::Layer02)),
frame: Some(color(ColorRole::BorderSubtle01)),
},
ContextChipShape::Inline => Self {
ink,
icon: if kind == ContextChipKind::DocPdf {
color(ColorRole::SupportError)
} else {
ink
},
label: if in_composer {
color(ColorRole::TextPrimary)
} else {
ink
},
remove: if in_composer {
color(ColorRole::IconSecondary)
} else {
ink
},
fill: boxed.then(|| color(ColorRole::Layer02)),
frame: boxed.then(|| color(ColorRole::BorderSubtle01)),
},
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ChipGeometry {
pub width: Option<f32>,
pub max_width: Option<f32>,
pub height: Option<f32>,
pub padding: (f32, f32, f32),
pub gap: f32,
pub remove_padding: f32,
pub remove_radius: f32,
pub weight: u16,
}
impl ChipGeometry {
pub const fn of(shape: ContextChipShape, in_composer: bool) -> Self {
let (remove_padding, remove_radius) = if in_composer { (4., 0.) } else { (2., 2.) };
match (shape, in_composer) {
(ContextChipShape::Bubble, false) => Self {
width: Some(224.),
max_width: None,
height: None,
padding: (8., 12., 12.),
gap: 8.,
remove_padding,
remove_radius,
weight: 400,
},
(ContextChipShape::Bubble, true) => Self {
width: None,
max_width: Some(224.),
height: Some(32.),
padding: (0., 8., 8.),
gap: 8.,
remove_padding,
remove_radius,
weight: 400,
},
(ContextChipShape::Inline, false) => Self {
width: None,
max_width: None,
height: None,
padding: (0., 0., 0.),
gap: 4.,
remove_padding,
remove_radius,
weight: 600,
},
(ContextChipShape::Inline, true) => Self {
width: None,
max_width: None,
height: Some(32.),
padding: (0., 8., 4.),
gap: 4.,
remove_padding,
remove_radius,
weight: 400,
},
}
}
}
#[derive(IntoElement)]
pub struct ContextChip {
id: ElementId,
kind: ContextChipKind,
label: SharedString,
meta: Option<SharedString>,
state: ContextChipState,
shape: ContextChipShape,
in_composer: bool,
remove_label: Option<SharedString>,
on_click: Option<Handler>,
on_remove: Option<Handler>,
}
impl ContextChip {
pub fn new(
id: impl Into<ElementId>,
kind: ContextChipKind,
label: impl Into<SharedString>,
) -> Self {
Self {
id: id.into(),
kind,
label: label.into(),
meta: None,
state: ContextChipState::Normal,
shape: ContextChipShape::Inline,
in_composer: false,
remove_label: None,
on_click: None,
on_remove: None,
}
}
pub fn meta(mut self, meta: impl Into<SharedString>) -> Self {
self.meta = Some(meta.into());
self
}
pub fn state(mut self, state: ContextChipState) -> Self {
self.state = state;
self
}
pub fn shape(mut self, shape: ContextChipShape) -> Self {
self.shape = shape;
self
}
pub fn in_composer(mut self, in_composer: bool) -> Self {
self.in_composer = in_composer;
self
}
pub fn on_click(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_click = 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 remove_label(mut self, label: impl Into<SharedString>) -> Self {
self.remove_label = Some(label.into());
self
}
}
impl RenderOnce for ContextChip {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let look = ChipLook::of(self.kind, self.state, self.shape, self.in_composer);
let geo = ChipGeometry::of(self.shape, self.in_composer);
let bubble = self.shape == ContextChipShape::Bubble;
let id = self.id.clone();
let (main_hovered, main_hover) = press::hover((id.clone(), "main-hover"), window, cx);
let label = div()
.min_w_0()
.overflow_hidden()
.whitespace_nowrap()
.text_ellipsis()
.text_color(look.label)
.when(bubble, |this| this.flex_1())
.when(self.state == ContextChipState::Unresolved, |this| {
this.line_through()
})
.when(self.on_click.is_some() && main_hovered, |this| {
this.underline()
})
.child(self.label.clone());
let mut parts: Vec<AnyElement> = vec![
press::icon(self.kind.icon(), 16., look.icon).into_any_element(),
label.into_any_element(),
];
if let Some(meta) = self.meta.clone() {
parts.push(
div()
.flex_none()
.font_weight(gpui_kit::FontWeight::NORMAL)
.text_size(rem(12.))
.text_color(color(ColorRole::TextSecondary))
.child(meta)
.into_any_element(),
);
}
fn main_box<E: Styled + ParentElement>(this: E, gap: f32, bubble: bool) -> E {
let this = this
.flex()
.flex_row()
.items_center()
.min_w_0()
.gap(rem(gap));
if bubble { this.flex_1() } else { this }
}
let main: AnyElement = match self.on_click.clone() {
Some(handler) => {
let (focus, ring) = press::focus((id.clone(), "main-focus"), window, cx);
main_box(
gpui_kit::base::Button::new((id.clone(), "main"))
.track_focus(&focus)
.accessibility_label(self.label.clone())
.relative()
.cursor(CursorStyle::PointingHand)
.on_hover(press::track_hover(main_hover))
.on_click(move |_, window, cx| handler(window, cx)),
geo.gap,
bubble,
)
.children(parts)
.when(ring, |this| this.child(press::ring()))
.into_any_element()
}
None => main_box(div(), geo.gap, bubble)
.children(parts)
.into_any_element(),
};
let remove = self.on_remove.clone().map(|handler| {
let (focus, ring) = press::focus((id.clone(), "x-focus"), window, cx);
let (hovered, hover) = press::hover((id.clone(), "x-hover"), window, cx);
let name = self
.remove_label
.clone()
.unwrap_or_else(|| format!("Remove {}", self.label).into());
gpui_kit::base::Button::new((id.clone(), "remove"))
.track_focus(&focus)
.accessibility_label(name)
.relative()
.flex()
.flex_none()
.p(rem(geo.remove_padding))
.rounded(px(geo.remove_radius))
.cursor(CursorStyle::PointingHand)
.when(hovered, |this| this.bg(color(ColorRole::BackgroundHover)))
.on_hover(press::track_hover(hover))
.on_click({
let focus = focus.clone();
move |_, window, cx| {
crate::u0::primitives::parts::vacate_focus(&focus, window, cx);
handler(window, cx)
}
})
.child(press::icon(CarbonIcon::Close, 12., look.remove))
.when(ring, |this| this.child(press::ring()))
});
let (py, pl, pr) = geo.padding;
div()
.id(id)
.flex()
.flex_row()
.flex_none()
.items_center()
.max_w_full()
.gap(rem(geo.gap))
.font(font(FontFamilyKey::Sans, geo.weight))
.text_size(rem(14.))
.line_height(rem(20.))
.text_color(look.ink)
.when_some(geo.width, |this, w| this.w(rem(w)))
.when_some(geo.max_width, |this, w| this.max_w(rem(w)))
.when_some(geo.height, |this, h| this.h(rem(h)))
.pt(rem(py))
.pb(rem(py))
.pl(rem(pl))
.pr(rem(pr))
.when_some(look.fill, |this, fill| this.bg(fill))
.when_some(look.frame, |this, frame| this.shadow(inset_frame(frame)))
.child(main)
.children(remove)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tokens::CarbonTheme;
fn hex(c: Hsla) -> String {
let c = c.to_rgb();
let b = |v: f32| (v * 255.).round() as u8;
format!("#{:02x}{:02x}{:02x}", b(c.r), b(c.g), b(c.b))
}
#[test]
fn kinds_take_their_tag_ink() {
crate::theme::tests_set(CarbonTheme::White);
let ink =
|k| hex(ChipLook::of(k, ContextChipState::Normal, ContextChipShape::Inline, false).ink);
assert_eq!(ink(ContextChipKind::File), "#0043ce");
assert_eq!(ink(ContextChipKind::PrClosed), "#a2191f");
assert_eq!(ink(ContextChipKind::PrDraft), "#525252");
assert_eq!(ink(ContextChipKind::Issue), "#0f62fe");
assert_eq!(
ContextChipKind::from_name("pr-merged"),
ContextChipKind::PrMerged
);
assert_eq!(ContextChipKind::from_name("nope"), ContextChipKind::File);
}
#[test]
fn states_override_the_kind_ink() {
crate::theme::tests_set(CarbonTheme::White);
let u = ChipLook::of(
ContextChipKind::File,
ContextChipState::Unresolved,
ContextChipShape::Inline,
false,
);
assert_eq!(hex(u.label), "#525252");
let i = ChipLook::of(
ContextChipKind::File,
ContextChipState::Invalid,
ContextChipShape::Inline,
false,
);
assert_eq!(hex(i.label), "#da1e28");
}
#[test]
fn pdf_icon_is_red_inline_and_ink_in_a_bubble() {
crate::theme::tests_set(CarbonTheme::White);
let inline = ChipLook::of(
ContextChipKind::DocPdf,
ContextChipState::Normal,
ContextChipShape::Inline,
false,
);
assert_eq!(hex(inline.icon), "#da1e28");
let bubble = ChipLook::of(
ContextChipKind::DocPdf,
ContextChipState::Normal,
ContextChipShape::Bubble,
false,
);
assert_eq!(hex(bubble.icon), "#161616");
assert_eq!(hex(bubble.fill.unwrap()), "#ffffff");
}
#[test]
fn composer_chips_are_32px_boxes() {
let inline = ChipGeometry::of(ContextChipShape::Inline, true);
assert_eq!(inline.height, Some(32.));
assert_eq!(inline.padding, (0., 8., 4.));
assert_eq!(inline.remove_padding, 4.);
assert_eq!(inline.weight, 400);
let bubble = ChipGeometry::of(ContextChipShape::Bubble, false);
assert_eq!(bubble.width, Some(224.));
assert_eq!(bubble.padding, (8., 12., 12.));
assert_eq!(
ChipGeometry::of(ContextChipShape::Inline, false).weight,
600
);
}
}