1use crate::{ColorName, Sizable, Size, StyledExt, theme::ActiveTheme as _};
2use gpui::{
3 AbsoluteLength, AnyElement, App, Hsla, IntoElement, ParentElement, RenderOnce, StyleRefinement,
4 Styled, Window, div, prelude::FluentBuilder as _, relative, rems, transparent_white,
5};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
9pub enum TagVariant {
10 Primary,
11 #[default]
12 Secondary,
13 Danger,
14 Success,
15 Warning,
16 Info,
17 Color(ColorName),
18 Custom {
19 color: Hsla,
20 foreground: Hsla,
21 border: Hsla,
22 },
23}
24
25impl TagVariant {
26 fn bg(&self, cx: &App) -> Hsla {
27 match self {
28 Self::Primary => cx.theme().primary,
29 Self::Secondary => cx.theme().secondary,
30 Self::Danger => cx.theme().danger,
31 Self::Success => cx.theme().success,
32 Self::Warning => cx.theme().warning,
33 Self::Info => cx.theme().info,
34 Self::Color(color) => {
35 if cx.theme().is_dark() {
36 color.scale(950).opacity(0.5)
37 } else {
38 color.scale(50)
39 }
40 }
41 Self::Custom { color, .. } => *color,
42 }
43 }
44
45 fn border(&self, cx: &App) -> Hsla {
46 match self {
47 Self::Primary => cx.theme().primary,
48 Self::Secondary => cx.theme().border,
49 Self::Danger => cx.theme().danger,
50 Self::Success => cx.theme().success,
51 Self::Warning => cx.theme().warning,
52 Self::Info => cx.theme().info,
53 Self::Color(color) => {
54 if cx.theme().is_dark() {
55 color.scale(800).opacity(0.5)
56 } else {
57 color.scale(200)
58 }
59 }
60 Self::Custom { border, .. } => *border,
61 }
62 }
63
64 fn fg(&self, outline: bool, cx: &App) -> Hsla {
65 match self {
66 Self::Primary => {
67 if outline {
68 cx.theme().primary
69 } else {
70 cx.theme().primary_foreground
71 }
72 }
73 Self::Secondary => {
74 if outline {
75 cx.theme().muted_foreground
76 } else {
77 cx.theme().secondary_foreground
78 }
79 }
80 Self::Danger => {
81 if outline {
82 cx.theme().danger
83 } else {
84 cx.theme().danger_foreground
85 }
86 }
87 Self::Success => {
88 if outline {
89 cx.theme().success
90 } else {
91 cx.theme().success_foreground
92 }
93 }
94 Self::Warning => {
95 if outline {
96 cx.theme().warning
97 } else {
98 cx.theme().warning_foreground
99 }
100 }
101 Self::Info => {
102 if outline {
103 cx.theme().info
104 } else {
105 cx.theme().info_foreground
106 }
107 }
108 Self::Color(color) => {
109 if cx.theme().is_dark() {
110 color.scale(300)
111 } else {
112 color.scale(600)
113 }
114 }
115 Self::Custom { foreground, .. } => *foreground,
116 }
117 }
118}
119
120#[derive(IntoElement)]
124pub struct Tag {
125 style: StyleRefinement,
126 variant: TagVariant,
127 outline: bool,
128 size: Size,
129 rounded: Option<AbsoluteLength>,
130 children: Vec<AnyElement>,
131}
132impl Tag {
133 pub fn new() -> Self {
135 Self {
136 style: StyleRefinement::default(),
137 variant: TagVariant::default(),
138 outline: false,
139 size: Size::default(),
140 rounded: None,
141 children: Vec::new(),
142 }
143 }
144
145 pub fn primary() -> Self {
147 Self::new().with_variant(TagVariant::Primary)
148 }
149
150 pub fn secondary() -> Self {
152 Self::new().with_variant(TagVariant::Secondary)
153 }
154
155 pub fn danger() -> Self {
157 Self::new().with_variant(TagVariant::Danger)
158 }
159
160 pub fn success() -> Self {
162 Self::new().with_variant(TagVariant::Success)
163 }
164
165 pub fn warning() -> Self {
167 Self::new().with_variant(TagVariant::Warning)
168 }
169
170 pub fn info() -> Self {
172 Self::new().with_variant(TagVariant::Info)
173 }
174
175 pub fn custom(color: Hsla, foreground: Hsla, border: Hsla) -> Self {
177 Self::new().with_variant(TagVariant::Custom {
178 color,
179 foreground,
180 border,
181 })
182 }
183
184 pub fn color(color: impl Into<ColorName>) -> Self {
186 Self::new().with_variant(TagVariant::Color(color.into()))
187 }
188
189 pub fn with_variant(mut self, variant: TagVariant) -> Self {
191 self.variant = variant;
192 self
193 }
194
195 pub fn outline(mut self) -> Self {
197 self.outline = true;
198 self
199 }
200
201 pub fn rounded(mut self, radius: impl Into<AbsoluteLength>) -> Self {
203 self.rounded = Some(radius.into());
204 self
205 }
206
207 pub fn rounded_full(mut self) -> Self {
209 self.rounded = Some(rems(1.).into());
210 self
211 }
212}
213
214impl Sizable for Tag {
215 fn with_size(mut self, size: impl Into<Size>) -> Self {
216 self.size = size.into();
217 self
218 }
219}
220
221impl ParentElement for Tag {
222 fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
223 self.children.extend(elements);
224 }
225}
226
227impl Styled for Tag {
228 fn style(&mut self) -> &mut StyleRefinement {
229 &mut self.style
230 }
231}
232
233impl RenderOnce for Tag {
234 fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
235 let bg = if self.outline {
236 transparent_white()
237 } else {
238 self.variant.bg(cx)
239 };
240 let fg = self.variant.fg(self.outline, cx);
241 let border = self.variant.border(cx);
242 let rounded = self.rounded.unwrap_or(
243 match self.size {
244 Size::XSmall | Size::Small => cx.theme().radius / 2.,
245 _ => cx.theme().radius,
246 }
247 .into(),
248 );
249
250 div()
251 .flex()
252 .items_center()
253 .border_1()
254 .line_height(relative(1.25))
255 .map(|this| match self.size {
256 Size::XSmall | Size::Small => this.text_size(rems(0.625)),
257 Size::Large => this.text_sm(),
258 _ => this.text_xs(),
259 })
260 .map(|this| match self.size {
261 Size::XSmall | Size::Small => this.px_1p5().py_0p5(),
262 _ => this.px_2p5().py_1(),
263 })
264 .bg(bg)
265 .text_color(fg)
266 .border_color(border)
267 .rounded(rounded)
268 .refine_style(&self.style)
269 .children(self.children)
270 }
271}