Skip to main content

fc_ui/capabilities/primitives/display/
badge.rs

1//! Badge component - Status labels and tags.
2
3use crate::capabilities::foundation::theme::use_theme;
4use gpui::{prelude::FluentBuilder as _, *};
5
6#[derive(Copy, Clone, Debug, PartialEq, Eq)]
7pub enum BadgeVariant {
8    Default,
9    Secondary,
10    Destructive,
11    Outline,
12    Warning,
13}
14
15#[derive(IntoElement)]
16pub struct Badge {
17    label: SharedString,
18    variant: BadgeVariant,
19    style: StyleRefinement,
20}
21
22impl Badge {
23    pub fn new<T: Into<SharedString>>(label: T) -> Self {
24        Self {
25            label: label.into(),
26            variant: BadgeVariant::Default,
27            style: StyleRefinement::default(),
28        }
29    }
30
31    pub fn variant(mut self, variant: BadgeVariant) -> Self {
32        self.variant = variant;
33        self
34    }
35}
36
37impl Styled for Badge {
38    fn style(&mut self) -> &mut StyleRefinement {
39        &mut self.style
40    }
41}
42
43impl RenderOnce for Badge {
44    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
45        let theme = use_theme(cx);
46        let user_style = self.style;
47
48        let (bg_color, fg_color, border_color) = match self.variant {
49            BadgeVariant::Default => (
50                theme.tokens.primary,
51                theme.tokens.primary_foreground,
52                gpui::transparent_black(),
53            ),
54            BadgeVariant::Secondary => (
55                theme.tokens.secondary,
56                theme.tokens.secondary_foreground,
57                gpui::transparent_black(),
58            ),
59            BadgeVariant::Destructive => (
60                theme.tokens.destructive,
61                theme.tokens.destructive_foreground,
62                gpui::transparent_black(),
63            ),
64            BadgeVariant::Outline => (
65                gpui::transparent_black(),
66                theme.tokens.foreground,
67                theme.tokens.border,
68            ),
69            BadgeVariant::Warning => (
70                gpui::hsla(38.0 / 360.0, 0.92, 0.55, 1.0),
71                gpui::hsla(0.0, 0.0, 0.0, 1.0),
72                gpui::transparent_black(),
73            ),
74        };
75
76        div()
77            .flex()
78            .items_center()
79            .px(px(10.0))
80            .py(px(2.0))
81            .rounded_full()
82            .text_size(px(12.0))
83            .font_family(theme.tokens.font_family.clone())
84            .font_weight(FontWeight::MEDIUM)
85            .bg(bg_color)
86            .text_color(fg_color)
87            .when(self.variant == BadgeVariant::Outline, |el| {
88                el.border_1().border_color(border_color)
89            })
90            .map(|this| {
91                let mut div = this;
92                div.style().refine(&user_style);
93                div
94            })
95            .child(self.label)
96    }
97}