use gpui_kit::{AbsoluteLength, DefiniteLength, Hsla, Pixels, Rems, Rgba, px, rems};
include!("generated/tokens.rs");
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum CarbonTheme {
#[default]
White,
G10,
G90,
G100,
}
impl CarbonTheme {
pub const ALL: [CarbonTheme; 4] = [Self::White, Self::G10, Self::G90, Self::G100];
pub const fn id(self) -> &'static str {
match self {
Self::White => "white",
Self::G10 => "g10",
Self::G90 => "g90",
Self::G100 => "g100",
}
}
pub const fn label(self) -> &'static str {
match self {
Self::White => "White",
Self::G10 => "Gray 10",
Self::G90 => "Gray 90",
Self::G100 => "Gray 100",
}
}
pub fn from_id(id: &str) -> Option<Self> {
Self::ALL
.into_iter()
.find(|theme| theme.id().eq_ignore_ascii_case(id))
}
pub const fn is_dark(self) -> bool {
matches!(self, Self::G90 | Self::G100)
}
}
impl ColorRole {
pub fn in_theme(self, theme: CarbonTheme) -> Hsla {
self.rgba_in(theme).into()
}
pub const fn rgba_in(self, theme: CarbonTheme) -> Rgba {
COLOR_TABLES[theme as usize][self as usize]
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum FontFamilyKey {
Sans,
Mono,
Serif,
SansCondensed,
}
impl FontFamilyKey {
pub const fn stack(self) -> &'static [&'static str] {
match self {
Self::Sans => FAMILY_SANS,
Self::Mono => FAMILY_MONO,
Self::Serif => FAMILY_SERIF,
Self::SansCondensed => FAMILY_SANS_CONDENSED,
}
}
pub const fn primary(self) -> &'static str {
self.stack()[0]
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum LineHeight {
Multiplier(f32),
Px(f32),
Rem(f32),
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct TypeSpec {
pub name: &'static str,
pub group: &'static str,
pub font_size_rem: f32,
pub line_height: LineHeight,
pub weight: u16,
pub letter_spacing_px: f32,
pub family: FontFamilyKey,
}
impl TypeSpec {
pub fn font_size(&self) -> Rems {
rems(self.font_size_rem)
}
pub fn font_size_px(&self) -> f32 {
self.font_size_rem * REM_PX
}
pub fn line_height(&self) -> DefiniteLength {
match self.line_height {
LineHeight::Multiplier(m) => gpui_kit::relative(m),
LineHeight::Px(v) => px(v).into(),
LineHeight::Rem(v) => rems(v).into(),
}
}
pub fn line_height_px(&self) -> f32 {
let raw = match self.line_height {
LineHeight::Multiplier(m) => m * self.font_size_px(),
LineHeight::Px(v) => v,
LineHeight::Rem(v) => v * REM_PX,
};
(raw * 100.).round() / 100.
}
}
impl TypeStyle {
pub const fn name(self) -> &'static str {
self.spec().name
}
}
pub const REM_PX: f32 = 16.;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum TokenLength {
Rem(f32),
Px(f32),
}
impl TokenLength {
pub const fn px_value(self) -> f32 {
match self {
Self::Rem(v) => v * REM_PX,
Self::Px(v) => v,
}
}
pub fn length(self) -> AbsoluteLength {
match self {
Self::Rem(v) => rems(v).into(),
Self::Px(v) => px(v).into(),
}
}
pub fn rems(self) -> Rems {
match self {
Self::Rem(v) => rems(v),
Self::Px(v) => rems(v / REM_PX),
}
}
pub fn pixels(self) -> Pixels {
px(self.px_value())
}
}
impl From<TokenLength> for AbsoluteLength {
fn from(value: TokenLength) -> Self {
value.length()
}
}
impl From<TokenLength> for DefiniteLength {
fn from(value: TokenLength) -> Self {
DefiniteLength::Absolute(value.length())
}
}
impl From<TokenLength> for gpui_kit::Length {
fn from(value: TokenLength) -> Self {
gpui_kit::Length::Definite(value.into())
}
}
#[cfg(test)]
mod tests {
use super::*;
fn hex(role: ColorRole, theme: CarbonTheme) -> String {
let c = role.rgba_in(theme);
let b = |v: f32| (v * 255.).round() as u8;
if c.a < 1. {
format!("rgba({}, {}, {}, {})", b(c.r), b(c.g), b(c.b), c.a)
} else {
format!("#{:02x}{:02x}{:02x}", b(c.r), b(c.g), b(c.b))
}
}
#[test]
fn spot_values_match_tokens_json() {
use CarbonTheme::*;
use ColorRole::*;
let cases: &[(ColorRole, [&str; 4])] = &[
(Background, ["#ffffff", "#f4f4f4", "#262626", "#161616"]),
(Interactive, ["#0f62fe", "#0f62fe", "#4589ff", "#4589ff"]),
(ButtonPrimary, ["#0f62fe", "#0f62fe", "#0f62fe", "#0f62fe"]),
(Focus, ["#0f62fe", "#0f62fe", "#ffffff", "#ffffff"]),
(Layer01, ["#f4f4f4", "#ffffff", "#393939", "#262626"]),
(Field01, ["#f4f4f4", "#ffffff", "#393939", "#262626"]),
(SupportError, ["#da1e28", "#da1e28", "#ff8389", "#fa4d56"]),
(
BackgroundHover,
[
"rgba(141, 141, 141, 0.12)",
"rgba(141, 141, 141, 0.12)",
"rgba(141, 141, 141, 0.16)",
"rgba(141, 141, 141, 0.16)",
],
),
];
for (role, expected) in cases {
for (theme, want) in [White, G10, G90, G100].into_iter().zip(expected) {
assert_eq!(
&hex(*role, theme),
want,
"{} in {}",
role.name(),
theme.id()
);
}
}
}
#[test]
fn names_round_trip_for_every_role() {
for role in ColorRole::ALL {
assert_eq!(ColorRole::from_name(role.name()), Some(role));
}
}
#[test]
fn aliases_resolve_to_their_target() {
for theme in CarbonTheme::ALL {
assert_eq!(
ColorRole::StateFocus.rgba_in(theme),
ColorRole::Focus.rgba_in(theme)
);
assert_eq!(
ColorRole::StateCurrentIndicator.rgba_in(theme),
ColorRole::BorderInteractive.rgba_in(theme)
);
}
}
#[test]
fn type_and_length_tokens() {
let body = TypeStyle::BodyCompact01.spec();
assert_eq!(body.name, "body-compact-01");
assert_eq!(body.font_size_px(), 14.);
assert_eq!(body.line_height_px().round(), 18.);
assert_eq!(body.letter_spacing_px, 0.16);
assert_eq!(body.weight, 400);
let label = TypeStyle::Label01.spec();
assert_eq!(
(label.font_size_px(), label.line_height_px().round()),
(12., 16.)
);
assert_eq!(TypeStyle::Code02.spec().family, FontFamilyKey::Mono);
assert_eq!(TypeStyle::Heading07.spec().weight, 300);
assert_eq!(SPACING_05.px_value(), 16.);
assert_eq!(SPACING_13.px_value(), 160.);
assert_eq!(SIZE_LG.px_value(), 48.);
assert_eq!(DENSITY_FIELD_MD.px_value(), 32.);
assert_eq!(BORDER_RADIUS_16.px_value(), 16.);
assert_eq!(WINDOW_MIN_WIDTH, TokenLength::Px(800.));
assert_eq!(FontFamilyKey::Sans.primary(), "IBM Plex Sans");
assert_eq!(
FontFamilyKey::Sans.stack(),
[
"IBM Plex Sans",
"PingFang SC",
"Hiragino Sans GB",
"Noto Sans CJK SC"
]
);
}
}