gpui_material_components/
display.rs1use gpui::{
2 AnyElement, App, Axis, ElementId, Hsla, InteractiveElement, IntoElement, ParentElement,
3 RenderOnce, Role, SharedString, StatefulInteractiveElement, Styled, Window, div,
4 prelude::FluentBuilder, px,
5};
6use gpui_material_foundation::{color, text_style, window_material_theme};
7use gpui_material_theme::{ColorRole, Direction, TypeRole, TypographyEmphasis};
8
9#[derive(IntoElement)]
11pub struct Divider {
12 axis: Axis,
13 thickness: gpui::Pixels,
14 color_role: ColorRole,
15 color_override: Option<gpui::Hsla>,
16 start: gpui::Pixels,
17 end: gpui::Pixels,
18}
19
20impl Divider {
21 const DEFAULT_THICKNESS: gpui::Pixels = px(1.);
22
23 pub fn horizontal() -> Self {
24 Self {
25 axis: Axis::Horizontal,
26 thickness: Self::DEFAULT_THICKNESS,
27 color_role: ColorRole::OutlineVariant,
28 color_override: None,
29 start: px(0.),
30 end: px(0.),
31 }
32 }
33
34 pub fn vertical() -> Self {
35 Self {
36 axis: Axis::Vertical,
37 ..Self::horizontal()
38 }
39 }
40
41 pub fn thickness(mut self, thickness: gpui::Pixels) -> Self {
44 self.thickness = if f32::from(thickness).is_finite() && thickness >= px(0.) {
45 thickness
46 } else {
47 Self::DEFAULT_THICKNESS
48 };
49 self
50 }
51
52 pub fn color(mut self, role: ColorRole) -> Self {
54 self.color_role = role;
55 self.color_override = None;
56 self
57 }
58
59 pub fn color_override(mut self, color: impl Into<gpui::Hsla>) -> Self {
61 self.color_override = Some(color.into());
62 self
63 }
64
65 pub fn inset(mut self, start: gpui::Pixels, end: gpui::Pixels) -> Self {
66 self.start = start.max(px(0.));
67 self.end = end.max(px(0.));
68 self
69 }
70}
71
72impl RenderOnce for Divider {
73 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
74 let fill = self
75 .color_override
76 .unwrap_or_else(|| tone(self.color_role, window, cx));
77 let thickness = if self.thickness == px(0.) {
78 px(window.scale_factor().recip())
79 } else {
80 self.thickness
81 };
82 let (left, right) = window_material_theme(window, cx)
83 .layout
84 .horizontal(self.start, self.end);
85 if self.axis == Axis::Vertical {
86 div()
87 .w(thickness)
88 .h_full()
89 .pt(self.start)
90 .pb(self.end)
91 .child(div().size_full().bg(fill))
92 } else {
93 div()
94 .w_full()
95 .h(thickness)
96 .pl(left)
97 .pr(right)
98 .child(div().size_full().bg(fill))
99 }
100 }
101}
102
103#[derive(IntoElement)]
106pub struct Badge {
107 host: Option<AnyElement>,
108 content: BadgeContent,
109 container_color: Option<Hsla>,
110 content_color: Option<Hsla>,
111 accessible_label: Option<(ElementId, SharedString)>,
112}
113
114enum BadgeContent {
115 Dot,
116 Text(SharedString),
117 Custom(ElementId, AnyElement),
118}
119
120impl Badge {
121 pub fn standalone() -> Self {
123 Self {
124 host: None,
125 content: BadgeContent::Dot,
126 container_color: None,
127 content_color: None,
128 accessible_label: None,
129 }
130 }
131
132 pub fn dot(host: impl IntoElement) -> Self {
134 Self {
135 host: Some(host.into_any_element()),
136 ..Self::standalone()
137 }
138 }
139
140 pub fn label(host: impl IntoElement, text: impl Into<SharedString>) -> Self {
141 Self {
142 content: BadgeContent::Text(text.into()),
143 ..Self::dot(host)
144 }
145 }
146
147 pub fn count(host: impl IntoElement, count: usize, maximum: usize) -> Self {
148 Self::label(
149 host,
150 if count > maximum {
151 format!("{maximum}+")
152 } else {
153 count.to_string()
154 },
155 )
156 }
157
158 pub fn content(mut self, id: impl Into<ElementId>, content: impl IntoElement) -> Self {
163 self.content = BadgeContent::Custom(id.into(), content.into_any_element());
164 self
165 }
166
167 pub fn container_color(mut self, color: impl Into<Hsla>) -> Self {
169 self.container_color = Some(color.into());
170 self
171 }
172
173 pub fn content_color(mut self, color: impl Into<Hsla>) -> Self {
175 self.content_color = Some(color.into());
176 self
177 }
178
179 pub fn accessible_label(
182 mut self,
183 id: impl Into<ElementId>,
184 label: impl Into<SharedString>,
185 ) -> Self {
186 self.accessible_label = Some((id.into(), label.into()));
187 self
188 }
189
190 pub(crate) fn take_accessible_label(&mut self) -> Option<SharedString> {
191 self.accessible_label.take().map(|(_, label)| label)
192 }
193
194 pub(crate) fn attach_to(mut self, host: impl IntoElement) -> Self {
195 self.host = Some(host.into_any_element());
196 self
197 }
198}
199
200impl RenderOnce for Badge {
201 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
202 let large = !matches!(self.content, BadgeContent::Dot);
203 let side = px(if large { 16. } else { 6. });
204 let container_color = self
205 .container_color
206 .unwrap_or_else(|| tone(ColorRole::Error, window, cx));
207 let content_color = self
208 .content_color
209 .unwrap_or_else(|| tone(ColorRole::OnError, window, cx));
210 let typography = TypeRole::LabelSmall.style(TypographyEmphasis::Baseline);
211 let overlap = px(if large { 12. } else { 6. });
212 let mark = div()
213 .min_w(side)
214 .rounded_full()
215 .when(large, |badge| badge.px(px(4.)))
216 .when(self.host.is_some(), |badge| {
217 badge
218 .absolute()
219 .top(if large { px(-2.) } else { px(0.) })
220 .when_else(
221 window_material_theme(window, cx).layout.direction == Direction::Rtl,
222 |badge| badge.right(gpui::relative(1.)).mr(-overlap),
223 |badge| badge.left(gpui::relative(1.)).ml(-overlap),
224 )
225 })
226 .bg(container_color)
227 .text_color(content_color)
228 .flex()
229 .items_center()
230 .justify_center();
231 let mark = match self.content {
232 BadgeContent::Dot => mark.h(side).w(side),
233 BadgeContent::Text(text) => mark.h(side).child(crate::text::single_line(
234 text,
235 typography,
236 1.,
237 content_color,
238 (None, 1., 1.),
239 window,
240 cx,
241 )),
242 BadgeContent::Custom(id, content) => {
243 text_style(mark.min_h(side), typography, 1., window, cx).child(
244 div()
245 .id(id)
246 .role(Role::Group)
247 .a11y_synthetic_children(|tree| tree.parent_node().set_hidden())
248 .child(content),
249 )
250 }
251 };
252 let mark = if let Some((id, label)) = self.accessible_label {
253 mark.id(id)
254 .role(Role::Label)
255 .aria_label(label)
256 .into_any_element()
257 } else {
258 mark.into_any_element()
259 };
260 if let Some(host) = self.host {
261 div().relative().child(host).child(mark)
262 } else {
263 div().flex().items_center().child(mark)
264 }
265 }
266}
267
268fn tone(role: ColorRole, window: &Window, cx: &App) -> gpui::Hsla {
269 color(window_material_theme(window, cx).colors.color(role))
270}