use gpui::{
px, relative, AbsoluteLength, DefiniteLength, Hsla, Length, Pixels, Rgba, SharedString,
};
use crate::theme::{Theme, ThemeColors};
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum SxLength {
Units(f32),
Px(Pixels),
Relative(f32),
Full,
Auto,
}
impl SxLength {
pub(super) fn length(self) -> Length {
match self {
SxLength::Units(units) => px(units * 4.).into(),
SxLength::Px(pixels) => pixels.into(),
SxLength::Relative(fraction) => relative(fraction).into(),
SxLength::Full => relative(1.).into(),
SxLength::Auto => Length::Auto,
}
}
pub(super) fn definite(self) -> DefiniteLength {
match self {
SxLength::Units(units) => px(units * 4.).into(),
SxLength::Px(pixels) => pixels.into(),
SxLength::Relative(fraction) => relative(fraction),
SxLength::Full => relative(1.),
SxLength::Auto => px(0.).into(),
}
}
pub(super) fn absolute(self) -> AbsoluteLength {
match self {
SxLength::Units(units) => px(units * 4.).into(),
SxLength::Px(pixels) => pixels.into(),
_ => px(0.).into(),
}
}
}
impl From<f32> for SxLength {
fn from(units: f32) -> Self {
SxLength::Units(units)
}
}
impl From<i32> for SxLength {
fn from(units: i32) -> Self {
SxLength::Units(units as f32)
}
}
impl From<Pixels> for SxLength {
fn from(pixels: Pixels) -> Self {
SxLength::Px(pixels)
}
}
impl From<DefiniteLength> for SxLength {
fn from(length: DefiniteLength) -> Self {
match length {
DefiniteLength::Fraction(fraction) => SxLength::Relative(fraction),
DefiniteLength::Absolute(AbsoluteLength::Pixels(pixels)) => SxLength::Px(pixels),
DefiniteLength::Absolute(AbsoluteLength::Rems(rems)) => SxLength::Px(px(rems.0 * 16.)),
}
}
}
macro_rules! color_tokens {
($($variant:ident => $field:ident),* $(,)?) => {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ColorToken {
$(
#[doc = concat!("The theme's `", stringify!($field), "` color.")]
$variant,
)*
}
impl ColorToken {
pub fn resolve(self, colors: &ThemeColors) -> Hsla {
match self {
$(ColorToken::$variant => colors.$field,)*
}
}
}
};
}
color_tokens! {
Background => background,
Foreground => foreground,
Card => card,
CardForeground => card_foreground,
Popover => popover,
PopoverForeground => popover_foreground,
Primary => primary,
PrimaryForeground => primary_foreground,
Secondary => secondary,
SecondaryForeground => secondary_foreground,
Muted => muted,
MutedForeground => muted_foreground,
Accent => accent,
AccentForeground => accent_foreground,
Destructive => destructive,
DestructiveForeground => destructive_foreground,
DestructiveText => destructive_text,
Border => border,
Input => input,
Ring => ring,
Overlay => overlay,
}
impl ColorToken {
#[must_use]
pub fn alpha(self, alpha: f32) -> SxColor {
SxColor::Token(self, alpha)
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum SxColor {
Token(ColorToken, f32),
Value(Hsla),
}
impl SxColor {
pub const TRANSPARENT: SxColor = SxColor::Value(Hsla {
h: 0.,
s: 0.,
l: 0.,
a: 0.,
});
pub(super) fn resolve(self, colors: &ThemeColors) -> Hsla {
match self {
SxColor::Token(token, alpha) => {
let color = token.resolve(colors);
color.opacity(alpha)
}
SxColor::Value(color) => color,
}
}
}
impl From<ColorToken> for SxColor {
fn from(token: ColorToken) -> Self {
SxColor::Token(token, 1.)
}
}
impl From<Hsla> for SxColor {
fn from(color: Hsla) -> Self {
SxColor::Value(color)
}
}
impl From<Rgba> for SxColor {
fn from(color: Rgba) -> Self {
SxColor::Value(color.into())
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum SxRadius {
None,
Sm,
Md,
Lg,
Xl,
Full,
Length(SxLength),
}
impl SxRadius {
#[doc(hidden)]
#[must_use]
pub fn resolve(self, theme: &Theme) -> AbsoluteLength {
match self {
SxRadius::None => px(0.).into(),
SxRadius::Sm => theme.radius_small().into(),
SxRadius::Md => theme.radius_medium().into(),
SxRadius::Lg => theme.radius_large().into(),
SxRadius::Xl => theme.radius_extra_large().into(),
SxRadius::Full => px(9999.).into(),
SxRadius::Length(length) => length.absolute(),
}
}
}
impl<T: Into<SxLength>> From<T> for SxRadius {
fn from(length: T) -> Self {
SxRadius::Length(length.into())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum SxShadow {
None,
Xs,
Sm,
Md,
Lg,
Xl,
Ring,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum SxText {
Theme,
Xs,
Sm,
Base,
Lg,
Xl,
Xl2,
Xl3,
Px(Pixels),
}
#[derive(Clone, Debug, PartialEq)]
pub enum SxFont {
Sans,
Mono,
Named(SharedString),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Edges {
All,
X,
Y,
Top,
Right,
Bottom,
Left,
Start,
End,
}
impl Edges {
pub(super) fn physical(self) -> Edges {
let rtl = crate::components::direction::is_rtl();
match (self, rtl) {
(Edges::Start, false) | (Edges::End, true) => Edges::Left,
(Edges::Start, true) | (Edges::End, false) => Edges::Right,
(other, _) => other,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Corners {
All,
Top,
Bottom,
Left,
Right,
TopLeft,
TopRight,
BottomLeft,
BottomRight,
Start,
End,
TopStart,
TopEnd,
BottomStart,
BottomEnd,
}
impl Corners {
pub(super) fn physical(self) -> Corners {
let rtl = crate::components::direction::is_rtl();
let (start, end) = if rtl {
(Corners::Right, Corners::Left)
} else {
(Corners::Left, Corners::Right)
};
let (top_start, top_end, bottom_start, bottom_end) = if rtl {
(
Corners::TopRight,
Corners::TopLeft,
Corners::BottomRight,
Corners::BottomLeft,
)
} else {
(
Corners::TopLeft,
Corners::TopRight,
Corners::BottomLeft,
Corners::BottomRight,
)
};
match self {
Corners::Start => start,
Corners::End => end,
Corners::TopStart => top_start,
Corners::TopEnd => top_end,
Corners::BottomStart => bottom_start,
Corners::BottomEnd => bottom_end,
other => other,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum SxDisplay {
Flex,
Block,
Hidden,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum SxDirection {
Row,
Column,
RowReverse,
ColumnReverse,
RowLtr,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum SxAlign {
Start,
Center,
End,
Stretch,
Baseline,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum SxJustify {
Start,
Center,
End,
Between,
Around,
Evenly,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum SxFlex {
One,
Auto,
Initial,
None,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum SxTextAlign {
Left,
Center,
Right,
Start,
End,
}