gpui_material_components/chip/
render.rs1use std::{cell::Cell, rc::Rc};
2
3use gpui::{
4 AnyElement, App, Div, ElementId, Hsla, InteractiveElement, IntoElement, ParentElement, Pixels,
5 RenderOnce, Styled, Window, div, prelude::FluentBuilder, px,
6};
7use gpui_material_foundation::{InteractionSource, current_material_theme};
8use gpui_material_theme::{Direction, LogicalEdges, TypeRole, TypographyEmphasis};
9
10use crate::action::{Action, ActionControl, InvokeAction, icon_slot};
11use crate::{Icon, IconName};
12
13use super::{
14 Chip, ChipVariant,
15 palette::{build_chip_layout, build_chip_style, resolve_palette},
16 split::{measure_height, render_split_chip, split_action_style},
17};
18
19#[allow(clippy::too_many_arguments)]
20fn build_leading_slot(
21 avatar: Option<AnyElement>,
22 leading: Option<AnyElement>,
23 filter_mark: f32,
24 spacing: Pixels,
25 leading_color: Hsla,
26 direction: Direction,
27) -> Option<AnyElement> {
28 avatar
29 .map(|avatar| {
30 div()
31 .size(px(24.))
32 .flex_none()
33 .rounded_full()
34 .overflow_hidden()
35 .text_color(leading_color)
36 .child(icon_slot(avatar, 24.))
37 .into_any_element()
38 })
39 .or_else(|| {
40 leading.map(|leading| {
41 div()
42 .flex_none()
43 .text_color(leading_color)
44 .child(icon_slot(leading, 18.))
45 .into_any_element()
46 })
47 })
48 .or_else(|| {
49 (filter_mark > 0.).then(|| {
50 div()
51 .w((px(18.) + spacing) * filter_mark)
52 .flex_none()
53 .overflow_hidden()
54 .opacity(filter_mark)
55 .text_color(leading_color)
56 .when(direction == Direction::Rtl, |slot| {
57 slot.flex().justify_end()
58 })
59 .child(icon_slot(
60 Icon::new(IconName::Check).into_any_element(),
61 18.,
62 ))
63 .into_any_element()
64 })
65 })
66}
67
68#[allow(clippy::too_many_arguments)]
69fn build_primary_content(
70 leading: Option<AnyElement>,
71 trailing: Option<AnyElement>,
72 label: AnyElement,
73 padding: LogicalEdges,
74 spacing: Pixels,
75 filter_mark: f32,
76 split: bool,
77 direction: Direction,
78 visual_height: &Rc<Cell<Pixels>>,
79) -> AnyElement {
80 let content = div()
81 .relative()
82 .flex()
83 .items_center()
84 .min_w_0()
85 .text_size(px(14.))
86 .when(direction == Direction::Rtl, Styled::flex_row_reverse)
87 .pt(px(padding.top))
88 .pb(px(padding.bottom));
89 let mut content = logical_edges(
90 content,
91 px(padding.start),
92 if split { spacing } else { px(padding.end) },
93 direction,
94 )
95 .children(leading)
96 .when(filter_mark == 0., |row| row.gap(spacing))
97 .child(label)
98 .children(trailing.map(|trailing| {
99 if filter_mark > 0. {
100 logical_edges(div(), spacing, px(0.), direction)
101 .child(trailing)
102 .into_any_element()
103 } else {
104 trailing
105 }
106 }));
107 if split {
108 content = content.child(measure_height(visual_height.clone()));
109 }
110 content.into_any_element()
111}
112
113pub(super) fn logical_edges(element: Div, start: Pixels, end: Pixels, direction: Direction) -> Div {
114 if direction == Direction::Rtl {
115 element.pl(end).pr(start)
116 } else {
117 element.pl(start).pr(end)
118 }
119}
120
121impl RenderOnce for Chip {
122 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
123 let selectable = matches!(self.variant, ChipVariant::Filter | ChipVariant::Input);
124 let selected = selectable && self.selected;
125 let tonal = selectable && self.tonal;
126 let elevated = self.elevated && self.variant != ChipVariant::Input;
127 let split = selectable && self.remove.is_some();
128 let (direction, density) = {
129 let theme = current_material_theme(window, cx);
130 (theme.layout.direction, theme.layout.density)
131 };
132 let palette = resolve_palette(
133 &self.id,
134 self.variant,
135 selectable,
136 selected,
137 tonal,
138 self.tone,
139 elevated,
140 self.disabled,
141 self.colors,
142 self.border,
143 window,
144 cx,
145 );
146 let style = build_chip_style(
147 self.variant,
148 selectable,
149 selected,
150 tonal,
151 elevated,
152 self.shapes,
153 palette.foreground,
154 );
155 let layout = build_chip_layout(
156 split,
157 self.tab_stop,
158 self.dragged,
159 self.shapes,
160 self.elevations,
161 palette,
162 );
163
164 let avatar = (self.variant == ChipVariant::Input)
165 .then_some(self.avatar)
166 .flatten();
167 let has_avatar = avatar.is_some();
168 let filter_mark = if self.variant == ChipVariant::Filter && self.leading.is_none() {
169 palette.selection
170 } else {
171 0.
172 };
173 let leading = build_leading_slot(
174 avatar,
175 self.leading,
176 filter_mark,
177 self.spacing,
178 palette.leading,
179 direction,
180 );
181 let trailing = self
182 .trailing
183 .filter(|_| self.variant != ChipVariant::Suggestion)
184 .map(|content| {
185 div()
186 .flex_none()
187 .text_color(palette.trailing)
188 .text_size(px(18.))
189 .child(content)
190 .into_any_element()
191 });
192 let padding = self.padding.unwrap_or_else(|| {
193 let start = if has_avatar {
194 4.
195 } else if filter_mark > 0. {
196 16. - 8. * filter_mark
197 } else if leading.is_some() {
198 8.
199 } else {
200 16.
201 };
202 let end = if split {
203 f32::from(self.spacing)
204 } else if trailing.is_some() {
205 8.
206 } else {
207 16.
208 };
209 LogicalEdges::new(0., end, 0., start)
210 });
211 let label = self.content.unwrap_or_else(|| {
212 crate::text::single_line(
213 self.label.clone(),
214 TypeRole::LabelLarge.style(TypographyEmphasis::Baseline),
215 1.,
216 palette.label,
217 (None, 1., 1.),
218 window,
219 cx,
220 )
221 .into_any_element()
222 });
223 let row = div()
224 .id(self.id.clone())
225 .relative()
226 .flex()
227 .min_w_0()
228 .items_center()
229 .when(direction == Direction::Rtl, Styled::flex_row_reverse);
230 let (trailing, remove_content) = if split {
231 (None, trailing)
232 } else {
233 (trailing, None)
234 };
235 let visual_height = Rc::new(Cell::new(px(density.visual_size(style.height))));
236 let content = build_primary_content(
237 leading,
238 trailing,
239 label,
240 padding,
241 self.spacing,
242 filter_mark,
243 split,
244 direction,
245 &visual_height,
246 );
247 let primary = ActionControl {
248 id: ElementId::from((self.id.clone(), "activate")),
249 label: self.label,
250 focus: self.focus,
251 disabled: self.disabled,
252 action: if selectable {
253 Action::Toggle {
254 selected,
255 on_change: self.on_change,
256 }
257 } else {
258 Action::Invoke(self.on_activate.map(InvokeAction::Full))
259 },
260 style: if split {
261 split_action_style(style, 0.)
262 } else {
263 style
264 },
265 content,
266 };
267 let tracker = self
268 .interaction_source
269 .as_ref()
270 .map(|source| InteractionSource::track(source, &self.id, window, cx));
271 if let Some((label, on_remove)) = self.remove.filter(|_| selectable) {
272 render_split_chip(
273 self.id,
274 row,
275 primary,
276 label,
277 on_remove,
278 remove_content,
279 self.disabled,
280 self.dragged,
281 selected,
282 direction,
283 padding,
284 self.padding,
285 style,
286 layout,
287 palette,
288 self.elevations,
289 visual_height,
290 tracker,
291 window,
292 cx,
293 )
294 } else {
295 row.child(primary.render_with_tracker(1., layout, tracker, window, cx))
296 }
297 }
298}