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