use crate::{
theme::color,
tokens::{ColorRole, SPACING_03, SPACING_04, TypeStyle},
typography::TypeStyled as _,
};
use gpui_kit::{
AnyElement, App, ElementId, Hsla, InteractiveElement as _, IntoElement, ParentElement,
RenderOnce, SharedString, StatefulInteractiveElement as _, StyleRefinement, Styled, Window,
div, prelude::FluentBuilder as _, px,
};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum MarkerVariant {
#[default]
Default,
Border,
Separator,
}
impl MarkerVariant {
pub const ALL: [MarkerVariant; 3] = [Self::Default, Self::Border, Self::Separator];
pub const fn name(self) -> &'static str {
match self {
Self::Default => "default",
Self::Border => "border",
Self::Separator => "separator",
}
}
}
pub fn text_color() -> Hsla {
color(ColorRole::TextSecondary)
}
pub fn line_color() -> Hsla {
color(ColorRole::BorderSubtle01)
}
#[derive(IntoElement, Default)]
pub struct Marker {
variant: MarkerVariant,
status: Option<(ElementId, SharedString)>,
style: StyleRefinement,
children: Vec<AnyElement>,
}
impl Marker {
pub fn new() -> Self {
Self::default()
}
pub fn variant(mut self, variant: MarkerVariant) -> Self {
self.variant = variant;
self
}
pub fn status(mut self, id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
self.status = Some((id.into(), label.into()));
self
}
}
impl Styled for Marker {
fn style(&mut self) -> &mut StyleRefinement {
&mut self.style
}
}
impl ParentElement for Marker {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for Marker {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let rule = || div().flex_1().h(px(1.)).bg(line_color());
let separator = self.variant == MarkerVariant::Separator;
let mut row = div()
.relative()
.flex()
.flex_row()
.items_center()
.gap(SPACING_03)
.type_style(TypeStyle::Label01)
.text_color(text_color())
.when(self.variant == MarkerVariant::Border, |this| {
this.py(SPACING_03).px(SPACING_04).child(
div()
.absolute()
.top_0()
.left_0()
.size_full()
.border_1()
.border_color(line_color()),
)
})
.when(separator, |this| this.child(rule()))
.children(self.children)
.when(separator, |this| this.child(rule()));
gpui_kit::Refineable::refine(row.style(), &self.style);
match self.status {
Some((id, label)) => div().id(id).aria_label(label).child(row).into_any_element(),
None => row.into_any_element(),
}
}
}
#[derive(IntoElement, Default)]
pub struct MarkerIcon {
children: Vec<AnyElement>,
}
impl MarkerIcon {
pub fn new() -> Self {
Self::default()
}
}
impl ParentElement for MarkerIcon {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for MarkerIcon {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
div()
.flex()
.flex_none()
.items_center()
.children(self.children)
}
}
#[derive(IntoElement, Default)]
pub struct MarkerContent {
children: Vec<AnyElement>,
}
impl MarkerContent {
pub fn new() -> Self {
Self::default()
}
}
impl ParentElement for MarkerContent {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for MarkerContent {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
div().flex().flex_row().children(self.children)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tokens::CarbonTheme;
#[test]
fn marker_follows_custom_css() {
crate::theme::tests_set(CarbonTheme::White);
assert_eq!(text_color(), color(ColorRole::TextSecondary));
assert_eq!(line_color(), color(ColorRole::BorderSubtle01));
assert_eq!((SPACING_03.px_value(), SPACING_04.px_value()), (8., 12.));
let label = TypeStyle::Label01.spec();
assert_eq!(label.font_size_px(), 12.);
}
}