BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / reaction_pills.rs

reaction_pills.rs

reaction_pills.rs 10.8 KiB Download Raw
1use std::rc::Rc;
2
3use chrono::Local;
4use gpui_kit::component::scroll::ScrollableElement as _;
5use gpui_kit::base::GlobalState;
6use gpui_kit::component::{h_flex, tooltip::Tooltip, v_flex};
7use gpui_kit::prelude::FluentBuilder as _;
8use gpui_kit::*;
9
10use super::avatar::person_avatar;
11use super::reaction_picker::PickHandler;
12use crate::data::Directory;
13use crate::reaction_model::{
14 ReactionEntry, ReactionTab, Reactor, chip_tooltip, reaction_entries, reaction_tabs,
15 reaction_time_label, shows_faces,
16};
17use crate::rows::{ReactionChip, custom_emoji_image};
18use crate::theme::{self, ElevatedSurface as _};
19
20pub const PILL_HEIGHT: f32 = 24.;
21const PILL_EMOJI_SIZE: f32 = 14.;
22const TAB_EMOJI_SIZE: f32 = 12.5;
23const ENTRY_EMOJI_SIZE: f32 = 17.;
24const PILL_TEXT_SIZE: f32 = 12.;
25const PILL_GAP: f32 = 3.;
26const FACE_SIZE: f32 = 18.;
27const FACE_RING: f32 = 1.5;
28const FACE_OVERLAP: f32 = 6.;
29const POPOVER_WIDTH: f32 = 290.;
30const POPOVER_MAX_HEIGHT: f32 = 320.;
31const POPOVER_GAP: f32 = 4.;
32const TAB_HEIGHT: f32 = 26.;
33const ENTRY_HEIGHT: f32 = 48.;
34const ENTRY_AVATAR_SIZE: f32 = 32.;
35
36type OpenReaction = Rc<dyn Fn(&str, &mut App)>;
37type SelectTab = Rc<dyn Fn(Option<String>, &mut App)>;
38
39#[derive(Clone)]
40pub struct ReactionPopover {
41 pub anchor_glyph: String,
42 pub tab: Option<String>,
43}
44
45#[derive(Clone)]
46pub struct ReactionControls {
47 pub open: OpenReaction,
48 pub select_tab: SelectTab,
49 pub close: Rc<dyn Fn(&mut App)>,
50 pub popover: Option<ReactionPopover>,
51}
52
53fn face(position: usize, reactor: &Reactor, ring: Hsla, directory: &Directory) -> Div {
54 let step = FACE_SIZE + 2. * FACE_RING - FACE_OVERLAP;
55 div()
56 .absolute()
57 .top_0()
58 .left(px(position as f32 * step))
59 .size(px(FACE_SIZE + 2. * FACE_RING))
60 .p(px(FACE_RING))
61 .rounded_full()
62 .bg(ring)
63 .child(person_avatar(
64 directory,
65 reactor.user_id.as_deref(),
66 &reactor.name,
67 FACE_SIZE,
68 ))
69}
70
71fn faces_width(count: usize) -> f32 {
72 let diameter = FACE_SIZE + 2. * FACE_RING;
73 diameter + count.saturating_sub(1) as f32 * (diameter - FACE_OVERLAP)
74}
75
76fn reaction_emoji(glyph: &str, label: &str, size: f32, directory: &Directory) -> AnyElement {
77 let Some(image) = custom_emoji_image(glyph) else {
78 return div()
79 .text_size(px(size))
80 .child(label.to_owned())
81 .into_any_element();
82 };
83 match directory.image(&image.url) {
84 Some(entry) => img(entry.path.clone())
85 .size(px(size))
86 .object_fit(ObjectFit::Contain)
87 .flex_none()
88 .into_any_element(),
89 None if directory.image_failed(&image.url) => div()
90 .child(image.alt.unwrap_or_default())
91 .into_any_element(),
92 None => div().size(px(size)).flex_none().into_any_element(),
93 }
94}
95
96fn pill_content(chip: &ReactionChip, background: Hsla, directory: &Directory) -> Div {
97 let content = h_flex().items_center().child(reaction_emoji(
98 &chip.glyph(),
99 &chip.label,
100 PILL_EMOJI_SIZE,
101 directory,
102 ));
103 if shows_faces(chip.reactors.len()) {
104 content.child(
105 div()
106 .relative()
107 .flex_none()
108 .ml(px(PILL_GAP - FACE_RING))
109 .w(px(faces_width(chip.reactors.len())))
110 .h(px(FACE_SIZE + 2. * FACE_RING))
111 .children(
112 chip.reactors
113 .iter()
114 .enumerate()
115 .map(|(position, reactor)| face(position, reactor, background, directory)),
116 ),
117 )
118 } else {
119 content
120 .gap(px(PILL_GAP))
121 .child(chip.count.to_string())
122 }
123}
124
125fn pill(
126 chip: &ReactionChip,
127 index: usize,
128 own: bool,
129 directory: &Directory,
130 react: Option<PickHandler>,
131 controls: Option<&ReactionControls>,
132) -> Stateful<Div> {
133 let (background, foreground) = if chip.mine {
134 (theme::accent(), theme::white())
135 } else if own {
136 (theme::reaction_on_own(), theme::text_soft())
137 } else {
138 (theme::border_strong(), theme::text_soft())
139 };
140 let glyph = chip.click_glyph.clone();
141 let tooltip_text = chip_tooltip(chip);
142 let toggle = controls.map(|controls| {
143 let is_open = controls
144 .popover
145 .as_ref()
146 .is_some_and(|state| state.anchor_glyph == chip.glyph());
147 (
148 controls.open.clone(),
149 controls.close.clone(),
150 chip.glyph(),
151 is_open,
152 )
153 });
154 h_flex()
155 .id(ElementId::Name(
156 format!("reaction-{index}-{}", chip.reaction_type).into(),
157 ))
158 .h(px(PILL_HEIGHT))
159 .flex_none()
160 .pl(px(5.))
161 .pr(px(if shows_faces(chip.reactors.len()) { 2. } else { 8. }))
162 .items_center()
163 .rounded(px(PILL_HEIGHT / 2.))
164 .bg(background)
165 .text_size(px(PILL_TEXT_SIZE))
166 .line_height(px(PILL_HEIGHT))
167 .text_color(foreground)
168 .child(pill_content(chip, background, directory))
169 .tooltip(move |window, cx| Tooltip::new(tooltip_text.clone()).build(window, cx))
170 .when_some(toggle, |pill, (open, close, glyph, is_open)| {
171 pill.on_mouse_down(MouseButton::Right, move |_, _, cx| {
172 cx.stop_propagation();
173 if is_open {
174 close(cx);
175 } else {
176 open(&glyph, cx);
177 }
178 })
179 })
180 .when_some(react, |pill, react| {
181 pill.cursor_pointer()
182 .on_mouse_down(MouseButton::Left, |_, _, cx| {
183 GlobalState::suppress_text_selection(cx)
184 })
185 .on_click(move |_, window, cx| {
186 cx.stop_propagation();
187 react(&glyph, window, cx);
188 })
189 })
190}
191
192fn tab_button(
193 tab: &ReactionTab,
194 active: bool,
195 select_tab: SelectTab,
196 directory: &Directory,
197) -> Stateful<Div> {
198 let glyph = tab.glyph.clone();
199 h_flex()
200 .id(ElementId::Name(
201 format!("reaction-tab-{}", glyph.as_deref().unwrap_or("all")).into(),
202 ))
203 .h(px(TAB_HEIGHT))
204 .px(px(12.))
205 .gap(px(4.))
206 .items_center()
207 .rounded(px(7.))
208 .cursor_pointer()
209 .text_size(px(12.5))
210 .text_color(if active {
211 theme::text()
212 } else {
213 theme::text_muted()
214 })
215 .when(active, |button| button.bg(theme::popup_hover()))
216 .child(match &tab.glyph {
217 Some(glyph) => reaction_emoji(glyph, &tab.label, TAB_EMOJI_SIZE, directory),
218 None => tab.label.clone().into_any_element(),
219 })
220 .child(tab.count.to_string())
221 .on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
222 .on_click(move |_, _, cx| {
223 cx.stop_propagation();
224 select_tab(glyph.clone(), cx);
225 })
226}
227
228fn entry_row(entry: &ReactionEntry, directory: &Directory) -> Div {
229 h_flex()
230 .h(px(ENTRY_HEIGHT))
231 .flex_none()
232 .px(px(8.))
233 .gap(px(8.))
234 .items_center()
235 .child(person_avatar(
236 directory,
237 entry.reactor.user_id.as_deref(),
238 &entry.reactor.name,
239 ENTRY_AVATAR_SIZE,
240 ))
241 .child(
242 v_flex()
243 .flex_1()
244 .min_w_0()
245 .child(
246 div()
247 .text_size(px(13.5))
248 .text_color(theme::text())
249 .truncate()
250 .child(entry.reactor.name.clone()),
251 )
252 .children(
253 entry
254 .reactor
255 .created_at
256 .map(|time| reaction_time_label(time, &Local::now()))
257 .map(|time| {
258 div()
259 .text_size(px(11.5))
260 .text_color(theme::text_muted())
261 .child(time)
262 }),
263 ),
264 )
265 .child(reaction_emoji(
266 &entry.glyph,
267 &entry.label,
268 ENTRY_EMOJI_SIZE,
269 directory,
270 ))
271}
272
273fn popover(
274 chips: &[ReactionChip],
275 selected: Option<&str>,
276 directory: &Directory,
277 controls: &ReactionControls,
278) -> AnyElement {
279 let selected = selected.filter(|glyph| chips.iter().any(|chip| chip.glyph() == *glyph));
280 let tabs = reaction_tabs(chips);
281 let entries = reaction_entries(chips, selected);
282 let close = controls.close.clone();
283 let panel = v_flex()
284 .id("reaction-popover")
285 .w(px(POPOVER_WIDTH))
286 .p(px(6.))
287 .gap(px(4.))
288 .rounded(px(10.))
289 .flyout_surface()
290 .occlude()
291 .on_mouse_down_out(move |_, _, cx| close(cx))
292 .on_mouse_down(MouseButton::Left, |_, _, cx| {
293 GlobalState::suppress_text_selection(cx);
294 cx.stop_propagation();
295 })
296 .on_mouse_down(MouseButton::Right, |_, _, cx| cx.stop_propagation())
297 .on_scroll_wheel(|_, _, cx| cx.stop_propagation())
298 .when(!tabs.is_empty(), |panel| {
299 panel.child(h_flex().gap(px(2.)).children(tabs.iter().map(|tab| {
300 tab_button(
301 tab,
302 tab.glyph.as_deref() == selected,
303 controls.select_tab.clone(),
304 directory,
305 )
306 })))
307 })
308 .child(
309 v_flex()
310 .id("reaction-popover-entries")
311 .max_h(px(POPOVER_MAX_HEIGHT))
312 .overflow_y_scrollbar()
313 .children(entries.iter().map(|entry| entry_row(entry, directory))),
314 );
315 deferred(
316 anchored()
317 .anchor(Anchor::TopLeft)
318 .offset(point(px(0.), px(PILL_HEIGHT + POPOVER_GAP)))
319 .snap_to_window_with_margin(px(8.))
320 .child(panel),
321 )
322 .with_priority(2)
323 .into_any_element()
324}
325
326pub fn reaction_pills(
327 chips: &[ReactionChip],
328 index: usize,
329 own: bool,
330 directory: &Directory,
331 react: Option<PickHandler>,
332 controls: Option<&ReactionControls>,
333) -> Div {
334 h_flex()
335 .gap(px(4.))
336 .flex_wrap()
337 .children(chips.iter().map(|chip| {
338 let open_here = controls
339 .and_then(|controls| controls.popover.as_ref().map(|state| (controls, state)))
340 .filter(|(_, state)| state.anchor_glyph == chip.glyph());
341 div()
342 .relative()
343 .flex_none()
344 .child(pill(chip, index, own, directory, react.clone(), controls))
345 .when_some(open_here, |wrapper, (controls, state)| {
346 wrapper.child(popover(chips, state.tab.as_deref(), directory, controls))
347 })
348 }))
349}