use super::base::{EASE, EASE_IN_OUT, Status, Tone, pulse};
use super::craft::rem;
use crate::{
components::{
button::{Button, ButtonSize, ButtonVariant},
menu::{self, MenuItem, MenuSize},
overlay,
},
icons::{
CarbonIcon, Icon,
brands::{self, BrandMark},
},
motion,
theme::color,
tokens::{ColorRole, TypeStyle},
typography::TypeStyled as _,
};
use gpui_kit::{
App, ElementId, FontWeight, Hsla, ImageSource, InteractiveElement, IntoElement, ObjectFit,
ParentElement, RenderOnce, SharedString, StatefulInteractiveElement as _, Styled,
StyledImage as _, TestSupportExt as _, Window, canvas, div, img, px,
};
use std::rc::Rc;
use std::time::Duration;
pub mod app_shell;
pub mod chrome;
pub mod palette;
pub mod sidebar;
pub use app_shell::AppShell;
pub use chrome::{
Crumb, LayoutPanel, PanelLayoutControls, RightPanelTabs, SurfaceKind, SurfaceTab,
WorkspaceBreadcrumb, WorkspacePageHeader,
};
pub use palette::{CommandPalette, ListFilter, ListToolbar, PaletteGroup, PaletteItem};
pub use sidebar::{
FolderCounts, ProjectFolderHeader, PullRequestMiniBadge, SidebarBrandHeader,
SidebarShelfHeader, ThreadRow, UpdatePill, UpdateState,
};
fn title_tooltip(text: SharedString) -> impl Fn(&mut Window, &mut App) -> gpui_kit::AnyView {
move |window, cx| gpui_kit::component::tooltip::Tooltip::new(text.clone()).build(window, cx)
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum IdentitySize {
Xs,
Sm,
#[default]
Md,
Lg,
}
impl IdentitySize {
pub const ALL: [IdentitySize; 4] = [Self::Xs, Self::Sm, Self::Md, Self::Lg];
pub const fn name(self) -> &'static str {
match self {
Self::Xs => "xs",
Self::Sm => "sm",
Self::Md => "md",
Self::Lg => "lg",
}
}
pub const fn px(self) -> f32 {
match self {
Self::Xs => 16.,
Self::Sm => 20.,
Self::Md => 24.,
Self::Lg => 40.,
}
}
pub const fn monogram_font(self) -> f32 {
match self {
Self::Xs => 8.,
Self::Sm => 10.,
Self::Md => 11.,
Self::Lg => 16.,
}
}
}
pub const TILE_RADIUS: f32 = 4.;
pub const PROVIDER_GLYPHS: [(&str, &str); 2] = [
(
"Codex",
"M8 .8 14.4 4.4v7.2L8 15.2 1.6 11.6V4.4z M8 3 3.6 5.5v5L8 13l4.4-2.5v-5z M5.2 6.4 7.6 8l-2.4 1.6-.8-1.1L5.3 8l-1-.5z M8.4 9.4h3.2v1.2H8.4z",
),
("Antigravity", "M1.5 14.5 8 1.5l6.5 13h-2.9L8 7.3 4.4 14.5z"),
];
pub(crate) const ASSET_PREFIX: &str = "u0ui/t3/";
pub(crate) fn asset_svg(name: &str) -> Option<String> {
let key = name.strip_prefix("provider-")?;
let (_, d) = PROVIDER_GLYPHS.iter().find(|(k, _)| *k == key)?;
Some(format!(
"<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 16 16\" width=\"16\" \
height=\"16\"><path fill-rule=\"evenodd\" d=\"{d}\"/></svg>"
))
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ProviderMark {
Brand(&'static BrandMark),
Glyph(&'static str),
Generic,
}
impl ProviderMark {
pub fn of(name: &str) -> Self {
if let Some(mark) = brands::brand_mark(name) {
return Self::Brand(mark);
}
PROVIDER_GLYPHS
.iter()
.find(|(k, _)| name.starts_with(k))
.map_or(Self::Generic, |(k, _)| Self::Glyph(k))
}
pub fn ink(self) -> Hsla {
match self {
Self::Brand(mark) => mark.ink(),
Self::Glyph(_) => color(ColorRole::TextPrimary),
Self::Generic => color(ColorRole::IconSecondary),
}
}
fn icon(self) -> Icon {
match self {
Self::Brand(mark) => Icon::from_path(mark.path()),
Self::Glyph(key) => Icon::from_path(format!("{ASSET_PREFIX}provider-{key}.svg")),
Self::Generic => Icon::named(GENERIC_PROVIDER_SYMBOL),
}
.color(self.ink())
}
}
pub const GENERIC_PROVIDER_SYMBOL: &str = "terminal";
pub const PROVIDER_SPIN_PERIOD: Duration = Duration::from_millis(800);
const ACCENTS: [ColorRole; 8] = [
ColorRole::AgentAccent1,
ColorRole::AgentAccent2,
ColorRole::AgentAccent3,
ColorRole::AgentAccent4,
ColorRole::AgentAccent5,
ColorRole::AgentAccent6,
ColorRole::AgentAccent7,
ColorRole::AgentAccent8,
];
#[derive(IntoElement)]
pub struct ProviderIcon {
id: Option<ElementId>,
name: SharedString,
src: Option<ImageSource>,
starting: bool,
size: IdentitySize,
accent: Option<u8>,
starting_label: Option<SharedString>,
}
impl ProviderIcon {
pub fn new(name: impl Into<SharedString>) -> Self {
Self {
id: None,
name: name.into(),
src: None,
starting: false,
size: IdentitySize::Md,
accent: None,
starting_label: None,
}
}
pub fn id(mut self, id: impl Into<ElementId>) -> Self {
self.id = Some(id.into());
self
}
pub fn src(mut self, src: impl Into<ImageSource>) -> Self {
self.src = Some(src.into());
self
}
pub fn starting(mut self, starting: bool) -> Self {
self.starting = starting;
self
}
pub fn size(mut self, size: IdentitySize) -> Self {
self.size = size;
self
}
pub fn accent(mut self, accent: Option<u8>) -> Self {
self.accent = accent;
self
}
pub fn starting_label(mut self, label: impl Into<SharedString>) -> Self {
self.starting_label = Some(label.into());
self
}
}
fn paint_provider_spin(bounds: gpui_kit::Bounds<gpui_kit::Pixels>, turn: f32, window: &mut Window) {
use std::f32::consts::{FRAC_PI_2, FRAC_PI_4, TAU};
let center = bounds.center();
let edge = f32::from(bounds.size.width);
let border = edge / 5.;
window.paint_quad(
gpui_kit::fill(bounds, color(ColorRole::Background)).corner_radii(px(edge / 2.)),
);
let r = (edge - border) / 2.;
super::base::stroke_arc(
window,
center,
r,
border,
0.,
TAU,
color(ColorRole::LayerAccent01),
);
let start = -FRAC_PI_2 - FRAC_PI_4 + turn * TAU;
super::base::stroke_arc(
window,
center,
r,
border,
start,
FRAC_PI_2,
color(ColorRole::Interactive),
);
}
impl RenderOnce for ProviderIcon {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self
.id
.unwrap_or_else(|| ElementId::Name(format!("t3-provider-{}", self.name).into()));
let edge = self.size.px();
let title: SharedString = if self.starting {
self.starting_label
.unwrap_or_else(|| format!("{} is starting", self.name).into())
} else {
self.name.clone()
};
let has_src = self.src.is_some();
let reduced = motion::reduced_motion(cx);
let mark = match self.src {
Some(src) => img(src)
.size_full()
.object_fit(ObjectFit::Contain)
.into_any_element(),
None => {
let glyph = rem(edge * 0.84);
ProviderMark::of(&self.name)
.icon()
.size_length(glyph)
.into_any_element()
}
};
let ground = if has_src {
match self.accent.filter(|a| (1..=8).contains(a)) {
Some(a) => color(ACCENTS[a as usize - 1]),
None => color(ColorRole::LayerAccent01),
}
} else {
gpui_kit::transparent_black()
};
let spin = self.starting.then(|| {
let frame = super::base::spin_frame_now(PROVIDER_SPIN_PERIOD, reduced, window);
let ring = div().size_full().opacity(frame.opacity).child(
canvas(
|_, _, _| {},
move |b, _, window, _| paint_provider_spin(b, frame.turn, window),
)
.size_full(),
);
div()
.absolute()
.right(rem(-4.))
.bottom(rem(-4.))
.size(rem(10.))
.child(ring)
});
div()
.id(id)
.relative()
.flex()
.flex_none()
.items_center()
.justify_center()
.size(rem(edge))
.rounded(rem(TILE_RADIUS))
.bg(ground)
.tooltip(title_tooltip(title))
.child(mark)
.children(spin)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum MonoTone {
Blue,
Teal,
Purple,
Magenta,
Cyan,
Green,
Red,
WarmGray,
}
impl MonoTone {
pub const ALL: [MonoTone; 8] = [
Self::Blue,
Self::Teal,
Self::Purple,
Self::Magenta,
Self::Cyan,
Self::Green,
Self::Red,
Self::WarmGray,
];
pub const fn name(self) -> &'static str {
match self {
Self::Blue => "blue",
Self::Teal => "teal",
Self::Purple => "purple",
Self::Magenta => "magenta",
Self::Cyan => "cyan",
Self::Green => "green",
Self::Red => "red",
Self::WarmGray => "warm-gray",
}
}
pub fn for_name(name: &str) -> Self {
Self::ALL[(monogram_hash(name) % 8) as usize]
}
}
pub fn monogram_hash(name: &str) -> u32 {
name.chars().fold(7u32, |a, c| {
let mut units = [0u16; 2];
let unit = c.encode_utf16(&mut units)[0];
a.wrapping_mul(31).wrapping_add(unit as u32)
})
}
pub fn monogram_letters(name: &str) -> String {
let cleaned: String = name
.chars()
.map(|c| if c.is_ascii_alphanumeric() { c } else { ' ' })
.collect();
let letters: String = cleaned
.split_whitespace()
.filter_map(|w| w.chars().next())
.take(2)
.collect::<String>()
.to_uppercase();
if letters.is_empty() {
"?".into()
} else {
letters
}
}
#[derive(IntoElement, Clone)]
pub struct ProjectMonogram {
name: SharedString,
size: IdentitySize,
tone: Option<MonoTone>,
}
impl ProjectMonogram {
pub fn new(name: impl Into<SharedString>) -> Self {
Self {
name: name.into(),
size: IdentitySize::Md,
tone: None,
}
}
pub fn size(mut self, size: IdentitySize) -> Self {
self.size = size;
self
}
pub fn tone(mut self, tone: MonoTone) -> Self {
self.tone = Some(tone);
self
}
pub fn resolved_tone(&self) -> MonoTone {
self.tone.unwrap_or_else(|| MonoTone::for_name(&self.name))
}
pub fn colors() -> (Hsla, Hsla) {
(
color(ColorRole::LayerAccent01),
color(ColorRole::TextSecondary),
)
}
}
fn tile(size: IdentitySize) -> gpui_kit::Div {
div()
.flex()
.flex_none()
.items_center()
.justify_center()
.size(rem(size.px()))
.rounded(rem(TILE_RADIUS))
}
impl RenderOnce for ProjectMonogram {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let _tone = self.resolved_tone();
let (bg, fg) = Self::colors();
let font = self.size.monogram_font();
tile(self.size)
.bg(bg)
.text_color(fg)
.font_family(crate::typography::font(crate::tokens::FontFamilyKey::Sans, 600).family)
.font_weight(FontWeight::SEMIBOLD)
.text_size(rem(font))
.line_height(rem(font))
.child(monogram_letters(&self.name))
}
}
#[derive(IntoElement)]
pub struct ProjectFavicon {
name: SharedString,
src: Option<ImageSource>,
icon: Option<Icon>,
size: IdentitySize,
}
impl ProjectFavicon {
pub fn new(name: impl Into<SharedString>) -> Self {
Self {
name: name.into(),
src: None,
icon: None,
size: IdentitySize::Md,
}
}
pub fn src(mut self, src: impl Into<ImageSource>) -> Self {
self.src = Some(src.into());
self
}
pub fn icon(mut self, icon: impl Into<Icon>) -> Self {
self.icon = Some(icon.into());
self
}
pub fn size(mut self, size: IdentitySize) -> Self {
self.size = size;
self
}
}
impl RenderOnce for ProjectFavicon {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let monogram = ProjectMonogram::new(self.name.clone()).size(self.size);
let edge = rem(self.size.px());
match (self.src, self.icon) {
(Some(src), icon) => {
let fallback_icon = icon.clone();
let size = self.size;
img(src)
.flex_none()
.size(edge)
.rounded(rem(TILE_RADIUS))
.object_fit(ObjectFit::Cover)
.with_fallback(move || match &fallback_icon {
Some(icon) => icon_tile(icon.clone(), size).into_any_element(),
None => monogram.clone().into_any_element(),
})
.into_any_element()
}
(None, Some(icon)) => icon_tile(icon, self.size).into_any_element(),
(None, None) => monogram.into_any_element(),
}
}
}
fn icon_tile(icon: Icon, size: IdentitySize) -> gpui_kit::Div {
tile(size).bg(color(ColorRole::LayerAccent01)).child(
icon.size_length(rem(16.))
.color(color(ColorRole::IconPrimary)),
)
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum MachineKind {
#[default]
Laptop,
Desktop,
Server,
Cloud,
Phone,
}
impl MachineKind {
pub const ALL: [MachineKind; 5] = [
Self::Laptop,
Self::Desktop,
Self::Server,
Self::Cloud,
Self::Phone,
];
pub const fn name(self) -> &'static str {
match self {
Self::Laptop => "laptop",
Self::Desktop => "desktop",
Self::Server => "server",
Self::Cloud => "cloud",
Self::Phone => "phone",
}
}
pub fn icon(self) -> Icon {
match self {
Self::Laptop => Icon::carbon(CarbonIcon::Laptop),
Self::Desktop => Icon::carbon(CarbonIcon::Screen),
Self::Server => Icon::named("dns"),
Self::Cloud => Icon::carbon(CarbonIcon::Cloud),
Self::Phone => Icon::named("phone_iphone"),
}
}
}
#[derive(IntoElement)]
pub struct EnvironmentMachineIcon {
kind: MachineKind,
size: f32,
}
impl EnvironmentMachineIcon {
pub fn new(kind: MachineKind) -> Self {
Self { kind, size: 16. }
}
pub fn size(mut self, size: f32) -> Self {
self.size = size;
self
}
}
impl RenderOnce for EnvironmentMachineIcon {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
self.kind
.icon()
.size_length(rem(self.size))
.color(color(ColorRole::IconSecondary))
.flex_none()
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ConnectionStatus {
#[default]
Connected,
Connecting,
Offline,
Error,
Sleeping,
}
impl ConnectionStatus {
pub const ALL: [ConnectionStatus; 5] = [
Self::Connected,
Self::Connecting,
Self::Offline,
Self::Error,
Self::Sleeping,
];
pub const fn name(self) -> &'static str {
match self {
Self::Connected => "connected",
Self::Connecting => "connecting",
Self::Offline => "offline",
Self::Error => "error",
Self::Sleeping => "sleeping",
}
}
pub const fn label(self) -> &'static str {
match self {
Self::Connected => "Connected",
Self::Connecting => "Connectingโฆ",
Self::Offline => "Offline",
Self::Error => "Connection error",
Self::Sleeping => "Computer is off",
}
}
pub fn colors(self) -> (Hsla, Option<Hsla>) {
match self {
Self::Connected => (color(ColorRole::SupportSuccess), None),
Self::Connecting => (color(ColorRole::SupportWarning), None),
Self::Error => (color(ColorRole::SupportError), None),
Self::Offline | Self::Sleeping => (
gpui_kit::transparent_black(),
Some(color(ColorRole::IconSecondary)),
),
}
}
}
pub const DOT_SIZE: f32 = 8.;
pub const DOT_RING: f32 = 1.5;
#[derive(IntoElement)]
pub struct ConnectionStatusDot {
id: Option<ElementId>,
status: ConnectionStatus,
label: Option<SharedString>,
ring: Option<Hsla>,
}
impl ConnectionStatusDot {
pub fn new(status: ConnectionStatus) -> Self {
Self {
id: None,
status,
label: None,
ring: None,
}
}
pub fn id(mut self, id: impl Into<ElementId>) -> Self {
self.id = Some(id.into());
self
}
pub fn label(mut self, label: impl Into<SharedString>) -> Self {
self.label = Some(label.into());
self
}
pub fn cutout(mut self, surface: Hsla) -> Self {
self.ring = Some(surface);
self
}
}
impl RenderOnce for ConnectionStatusDot {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let id = self
.id
.unwrap_or_else(|| ElementId::Name(format!("t3-dot-{}", self.status.name()).into()));
let label = self.label.unwrap_or_else(|| self.status.label().into());
let (fill, inset) = self.status.colors();
let ring = |color: Hsla, spread: f32, inset: bool| gpui_kit::BoxShadow {
color,
offset: gpui_kit::point(px(0.), px(0.)),
blur_radius: px(0.),
spread_radius: px(spread),
inset,
};
let mut shadows = Vec::new();
if let Some(c) = inset {
shadows.push(ring(c, DOT_RING, true));
}
if let Some(surface) = self.ring {
shadows.push(ring(surface, 2., false));
}
let dot = div()
.flex_none()
.size(rem(DOT_SIZE))
.rounded_full()
.bg(fill)
.shadow(shadows);
let dot = if self.status == ConnectionStatus::Connecting {
pulse(dot, (id.clone(), "pulse"), EASE_IN_OUT).into_any_element()
} else {
dot.into_any_element()
};
div()
.id(id)
.flex()
.flex_none()
.tooltip(title_tooltip(label))
.child(dot)
.test_support()
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum BadgeSize {
Sm,
#[default]
Md,
}
impl BadgeSize {
pub const ALL: [BadgeSize; 2] = [Self::Sm, Self::Md];
pub const fn name(self) -> &'static str {
match self {
Self::Sm => "sm",
Self::Md => "md",
}
}
pub const fn geometry(self) -> (f32, f32) {
match self {
Self::Sm => (20., 4.),
Self::Md => (24., 8.),
}
}
}
#[derive(IntoElement)]
pub struct EnvironmentBadge {
name: SharedString,
kind: MachineKind,
status: ConnectionStatus,
size: BadgeSize,
id: Option<ElementId>,
}
impl EnvironmentBadge {
pub fn new(name: impl Into<SharedString>) -> Self {
Self {
name: name.into(),
kind: MachineKind::Laptop,
status: ConnectionStatus::Connected,
size: BadgeSize::Md,
id: None,
}
}
pub fn kind(mut self, kind: MachineKind) -> Self {
self.kind = kind;
self
}
pub fn status(mut self, status: ConnectionStatus) -> Self {
self.status = status;
self
}
pub fn size(mut self, size: BadgeSize) -> Self {
self.size = size;
self
}
pub fn id(mut self, id: impl Into<ElementId>) -> Self {
self.id = Some(id.into());
self
}
}
impl RenderOnce for EnvironmentBadge {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let (height, pad) = self.size.geometry();
let id = self
.id
.unwrap_or_else(|| ElementId::Name(format!("t3-envbadge-{}", self.name).into()));
div()
.flex()
.flex_none()
.items_center()
.gap(rem(4.))
.h(rem(height))
.px(rem(pad))
.min_w_0()
.overflow_hidden()
.bg(color(ColorRole::LayerAccent01))
.text_size(rem(12.))
.line_height(rem(16.))
.text_color(color(ColorRole::TextPrimary))
.whitespace_nowrap()
.child(EnvironmentMachineIcon::new(self.kind).size(14.))
.child(div().flex_none().child(self.name))
.child(ConnectionStatusDot::new(self.status).id(id))
}
}
pub fn pill_colors(tone: Tone) -> (Hsla, Hsla, Hsla) {
let (bg, fg) = match tone {
Tone::Interactive => (ColorRole::TagBackgroundBlue, ColorRole::TagColorBlue),
Tone::Success => (ColorRole::TagBackgroundGreen, ColorRole::TagColorGreen),
Tone::Error => (ColorRole::TagBackgroundRed, ColorRole::TagColorRed),
Tone::Neutral => (ColorRole::TagBackgroundGray, ColorRole::TagColorGray),
Tone::Warning => {
let ink = super::base::ink();
return (color(ColorRole::SupportWarning), ink, ink);
}
};
let fg = color(fg);
(color(bg), fg, fg)
}
pub const PILL_HEIGHT: f32 = 20.;
pub const PILL_DOT: f32 = 6.;
#[derive(IntoElement)]
pub struct StatusPill {
id: Option<ElementId>,
status: Status,
label: Option<SharedString>,
}
impl StatusPill {
pub fn new(status: Status) -> Self {
Self {
id: None,
status,
label: None,
}
}
pub fn label(mut self, label: impl Into<SharedString>) -> Self {
self.label = Some(label.into());
self
}
pub fn id(mut self, id: impl Into<ElementId>) -> Self {
self.id = Some(id.into());
self
}
}
impl RenderOnce for StatusPill {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let tone = self.status.tone();
let (bg, fg, dot_color) = pill_colors(tone);
let id = self
.id
.unwrap_or_else(|| ElementId::Name(format!("t3-pill-{}", self.status.name()).into()));
let dot = div()
.flex_none()
.size(rem(PILL_DOT))
.rounded_full()
.bg(dot_color);
let dot = if tone == Tone::Interactive {
pulse(dot, (id, "pulse"), EASE).into_any_element()
} else {
dot.into_any_element()
};
div()
.flex()
.flex_none()
.items_center()
.gap(rem(4.))
.h(rem(PILL_HEIGHT))
.px(rem(8.))
.rounded(rem(16.))
.bg(bg)
.text_color(fg)
.type_style(TypeStyle::Label01)
.whitespace_nowrap()
.child(dot)
.child(self.label.unwrap_or_else(|| self.status.label().into()))
}
}
#[derive(IntoElement)]
pub struct IconMenu {
id: ElementId,
label: SharedString,
icon: Icon,
variant: ButtonVariant,
size: ButtonSize,
disabled: bool,
icon_color: Option<Hsla>,
fill: Option<Hsla>,
items: Vec<MenuItem>,
}
impl IconMenu {
pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
label: label.into(),
icon: Icon::carbon(CarbonIcon::ChevronDown),
variant: ButtonVariant::Secondary,
size: ButtonSize::Md,
disabled: false,
icon_color: None,
fill: None,
items: Vec::new(),
}
}
pub fn icon_color(mut self, ink: Hsla) -> Self {
self.icon_color = Some(ink);
self
}
pub fn fill(mut self, fill: Hsla) -> Self {
self.fill = Some(fill);
self
}
pub fn icon(mut self, icon: impl Into<Icon>) -> Self {
self.icon = icon.into();
self
}
pub fn variant(mut self, variant: ButtonVariant) -> Self {
self.variant = variant;
self
}
pub fn size(mut self, size: ButtonSize) -> Self {
self.size = size;
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
pub fn item(mut self, item: MenuItem) -> Self {
self.items.push(item);
self
}
pub fn items(mut self, items: impl IntoIterator<Item = MenuItem>) -> Self {
self.items.extend(items);
self
}
pub const fn menu_size(size: ButtonSize) -> MenuSize {
match size {
ButtonSize::Sm => MenuSize::Sm,
_ => MenuSize::Md,
}
}
}
impl RenderOnce for IconMenu {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let openness = overlay::openness(&id, None, false, None, window, cx);
let open = openness.open && !self.disabled;
let set_open = openness.setter();
let mut button = Button::new((id.clone(), "button"), self.label.clone())
.variant(self.variant)
.size(self.size)
.icon(self.icon)
.icon_only(true)
.disabled(self.disabled)
.tooltip(!open);
if let Some(ink) = self.icon_color {
button = button.icon_color(ink);
}
if let Some(fill) = self.fill {
button = button.fill(fill);
}
let button = button.into_any_element();
let disabled = self.disabled;
let end =
match overlay::drop_anchor((id.clone(), "drop"), window, cx).hangs_from_end(window) {
Some(end) => end,
None => {
if open && !self.items.is_empty() {
window.request_animation_frame();
}
false
}
};
let trigger = overlay::trigger_wrapper((id.clone(), "trigger"), button, {
let set_open = set_open.clone();
Rc::new(move |window, cx| {
if !disabled {
set_open(!open, window, cx)
}
})
})
.test_support()
.into_any_element();
menu::anchored_menu_aligned(
&id,
trigger,
menu::MenuSpec {
items: self.items,
size: Self::menu_size(self.size),
label: self.label,
open,
set_open,
min_width: None,
on_unhandled_key: None,
},
end,
window,
cx,
)
}
}
#[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 monogram_letters_and_hash_follow_shell_jsx() {
assert_eq!(monogram_letters("carbon-ui"), "CU");
assert_eq!(monogram_letters("Marketing Site"), "MS");
assert_eq!(monogram_letters("docs"), "D");
assert_eq!(monogram_letters("my big project"), "MB");
assert_eq!(monogram_letters("โ"), "?");
assert_eq!(monogram_hash("ab"), 9832);
assert_eq!(monogram_hash(""), 7);
assert_eq!(MonoTone::for_name("ab"), MonoTone::ALL[9832 % 8]);
let long = "x".repeat(40);
let mut a: u64 = 7;
for _ in 0..40 {
a = (a * 31 + 120) % (1 << 32);
}
assert_eq!(monogram_hash(&long) as u64, a);
}
#[test]
fn identity_sizes_follow_t3_css() {
assert_eq!(IdentitySize::ALL.map(|s| s.px()), [16., 20., 24., 40.]);
assert_eq!(
IdentitySize::ALL.map(|s| s.monogram_font()),
[8., 10., 11., 16.]
);
assert_eq!(BadgeSize::Md.geometry(), (24., 8.));
assert_eq!(BadgeSize::Sm.geometry(), (20., 4.));
}
#[test]
fn provider_marks_prefer_the_brand_then_the_glyph_then_generic() {
crate::theme::tests_set(CarbonTheme::White);
let brand = |n: &str| match ProviderMark::of(n) {
ProviderMark::Brand(m) => Some(m.slug),
_ => None,
};
assert_eq!(brand("Claude Code"), Some("claude"));
assert_eq!(brand("gemini"), Some("googlegemini"));
assert_eq!(ProviderMark::of("Codex"), ProviderMark::Glyph("Codex"));
assert_eq!(
ProviderMark::of("Antigravity"),
ProviderMark::Glyph("Antigravity")
);
assert_eq!(ProviderMark::of("Kiro"), ProviderMark::Generic);
assert_eq!(hex(ProviderMark::of("Claude").ink()), "#c15f3c");
assert_eq!(hex(ProviderMark::of("Gemini").ink()), "#1a73e8");
assert_eq!(hex(ProviderMark::of("Cursor").ink()), "#161616");
assert_eq!(hex(ProviderMark::of("Codex").ink()), "#161616");
assert_eq!(hex(ProviderMark::of("Kiro").ink()), "#525252");
assert!(asset_svg("provider-Codex").unwrap().contains("evenodd"));
assert!(asset_svg("provider-Kiro").is_none());
assert_eq!(GENERIC_PROVIDER_SYMBOL, "terminal");
assert!(gpui_material_symbols::Symbol::named(GENERIC_PROVIDER_SYMBOL).is_some());
assert!(gpui_material_symbols::Symbol::named("dns").is_some());
assert!(gpui_material_symbols::Symbol::named("phone_iphone").is_some());
}
#[test]
fn dots_and_pills_follow_t3_css() {
crate::theme::tests_set(CarbonTheme::White);
let (fill, ring) = ConnectionStatus::Connected.colors();
assert_eq!((hex(fill), ring), ("#24a148".into(), None));
let (fill, ring) = ConnectionStatus::Offline.colors();
assert_eq!((fill.a, ring.map(hex)), (0., Some("#525252".into())));
assert_eq!(hex(ConnectionStatus::Error.colors().0), "#da1e28");
assert_eq!(ConnectionStatus::Sleeping.label(), "Computer is off");
let (bg, fg, dot) = pill_colors(Tone::Warning);
assert_eq!(
(hex(bg), hex(fg), hex(dot)),
("#f1c21b".into(), "#161616".into(), "#161616".into())
);
let (bg, fg, _) = pill_colors(Tone::Interactive);
assert_eq!((hex(bg), hex(fg)), ("#d0e2ff".into(), "#0043ce".into()));
crate::theme::tests_set(CarbonTheme::G100);
assert_eq!(hex(pill_colors(Tone::Warning).1), "#161616");
crate::theme::tests_set(CarbonTheme::White);
}
#[test]
fn monograms_are_neutral_and_menus_follow_trigger_size() {
crate::theme::tests_set(CarbonTheme::White);
let (bg, fg) = ProjectMonogram::colors();
assert_eq!((hex(bg), hex(fg)), ("#e0e0e0".into(), "#525252".into()));
assert_eq!(IconMenu::menu_size(ButtonSize::Sm), MenuSize::Sm);
assert_eq!(IconMenu::menu_size(ButtonSize::Lg), MenuSize::Md);
}
}