BlueGit

ict rusty-teams

Files

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

reaction_pills.rs

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