BlueGit

ict rusty-teams

Files

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

reaction_pills.rs

c72b0466 History
reaction_pills.rs 11.3 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 reaction_emoji(glyph: &str, label: &str, size: f32, directory: &Directory) -> AnyElement {
82 let Some(image) = custom_emoji_image(glyph) else {
83 return div()
84 .flex_none()
85 .h(px(size))
86 .text_size(px(size))
87 .font_family(theme::EMOJI_FONT_FAMILY)
88 .line_height(px(size - 2. * (size * EMOJI_LIFT).round()))
89 .child(label.to_owned())
90 .into_any_element();
91 };
92 match directory.image(&image.url) {
93 Some(entry) => img(bounded(entry.path.clone()))
94 .size(px(size))
95 .object_fit(ObjectFit::Contain)
96 .flex_none()
97 .into_any_element(),
98 None if directory.image_failed(&image.url) => div()
99 .child(image.alt.unwrap_or_default())
100 .into_any_element(),
101 None => div().size(px(size)).flex_none().into_any_element(),
102 }
103}
104
105fn pill_content(chip: &ReactionChip, background: Hsla, directory: &Directory) -> Div {
106 let content = h_flex().items_center().child(reaction_emoji(
107 &chip.glyph(),
108 &chip.label,
109 PILL_EMOJI_SIZE,
110 directory,
111 ));
112 if shows_faces(chip.reactors.len()) {
113 content.child(
114 div()
115 .relative()
116 .flex_none()
117 .ml(px(PILL_GAP - FACE_RING))
118 .w(px(faces_width(chip.reactors.len())))
119 .h(px(FACE_SIZE + 2. * FACE_RING))
120 .children(
121 chip.reactors
122 .iter()
123 .enumerate()
124 .map(|(position, reactor)| face(position, reactor, background, directory)),
125 ),
126 )
127 } else {
128 content.gap(px(PILL_GAP)).child(chip.count.to_string())
129 }
130}
131
132fn pill(
133 chip: &ReactionChip,
134 index: usize,
135 own: bool,
136 directory: &Directory,
137 react: Option<PickHandler>,
138 controls: Option<&ReactionControls>,
139) -> Stateful<Div> {
140 let (background, foreground) = if chip.mine {
141 (theme::accent(), theme::white())
142 } else if own {
143 (theme::reaction_on_own(), theme::text_soft())
144 } else {
145 (theme::border_strong(), theme::text_soft())
146 };
147 let glyph = chip.click_glyph.clone();
148 let tooltip_text = chip_tooltip(chip);
149 let toggle = controls.map(|controls| {
150 let is_open = controls
151 .popover
152 .as_ref()
153 .is_some_and(|state| state.anchor_glyph == chip.glyph());
154 (
155 controls.open.clone(),
156 controls.close.clone(),
157 chip.glyph(),
158 is_open,
159 )
160 });
161 h_flex()
162 .id(ElementId::Name(
163 format!("reaction-{index}-{}", chip.reaction_type).into(),
164 ))
165 .h(px(PILL_HEIGHT))
166 .flex_none()
167 .pl(px(5.))
168 .pr(px(if shows_faces(chip.reactors.len()) {
169 2.
170 } else {
171 8.
172 }))
173 .items_center()
174 .rounded(px(PILL_HEIGHT / 2.))
175 .bg(background)
176 .text_size(px(PILL_TEXT_SIZE))
177 .line_height(px(PILL_HEIGHT))
178 .text_color(foreground)
179 .child(pill_content(chip, background, directory))
180 .tooltip(move |window, cx| Tooltip::new(tooltip_text.clone()).build(window, cx))
181 .when_some(toggle, |pill, (open, close, glyph, is_open)| {
182 pill.on_mouse_down(MouseButton::Right, move |_, _, cx| {
183 cx.stop_propagation();
184 if is_open {
185 close(cx);
186 } else {
187 open(&glyph, cx);
188 }
189 })
190 })
191 .when_some(react, |pill, react| {
192 pill.cursor_pointer()
193 .on_mouse_down(MouseButton::Left, |_, _, cx| {
194 GlobalState::suppress_text_selection(cx)
195 })
196 .on_click(move |_, window, cx| {
197 cx.stop_propagation();
198 react(&glyph, window, cx);
199 })
200 })
201}
202
203fn tab_button(
204 tab: &ReactionTab,
205 active: bool,
206 select_tab: SelectTab,
207 directory: &Directory,
208) -> Stateful<Div> {
209 let glyph = tab.glyph.clone();
210 h_flex()
211 .id(ElementId::Name(
212 format!("reaction-tab-{}", glyph.as_deref().unwrap_or("all")).into(),
213 ))
214 .h(px(TAB_HEIGHT))
215 .px(px(12.))
216 .gap(px(4.))
217 .items_center()
218 .rounded(px(7.))
219 .cursor_pointer()
220 .text_size(px(12.5))
221 .text_color(if active {
222 theme::text()
223 } else {
224 theme::text_muted()
225 })
226 .when(active, |button| button.bg(theme::popup_hover()))
227 .child(match &tab.glyph {
228 Some(glyph) => reaction_emoji(glyph, &tab.label, TAB_EMOJI_SIZE, directory),
229 None => tab.label.clone().into_any_element(),
230 })
231 .child(tab.count.to_string())
232 .on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
233 .on_click(move |_, _, cx| {
234 cx.stop_propagation();
235 select_tab(glyph.clone(), cx);
236 })
237}
238
239fn entry_row(entry: &ReactionEntry, directory: &Directory) -> Div {
240 h_flex()
241 .h(px(ENTRY_HEIGHT))
242 .flex_none()
243 .px(px(8.))
244 .gap(px(8.))
245 .items_center()
246 .child(person_avatar(
247 directory,
248 entry.reactor.user_id.as_deref(),
249 &entry.reactor.name,
250 ENTRY_AVATAR_SIZE,
251 ))
252 .child(
253 v_flex()
254 .flex_1()
255 .min_w_0()
256 .child(
257 div()
258 .text_size(px(13.5))
259 .text_color(theme::text())
260 .truncate()
261 .child(entry.reactor.name.clone()),
262 )
263 .children(
264 entry
265 .reactor
266 .created_at
267 .map(|time| reaction_time_label(time, &Local::now()))
268 .map(|time| {
269 div()
270 .text_size(px(11.5))
271 .text_color(theme::text_muted())
272 .child(time)
273 }),
274 ),
275 )
276 .child(reaction_emoji(
277 &entry.glyph,
278 &entry.label,
279 ENTRY_EMOJI_SIZE,
280 directory,
281 ))
282}
283
284fn popover(
285 chips: &[ReactionChip],
286 selected: Option<&str>,
287 directory: &Directory,
288 controls: &ReactionControls,
289) -> AnyElement {
290 let selected = selected.filter(|glyph| chips.iter().any(|chip| chip.glyph() == *glyph));
291 let tabs = reaction_tabs(chips);
292 let entries = reaction_entries(chips, selected);
293 let close = controls.close.clone();
294 let panel = v_flex()
295 .id("reaction-popover")
296 .w(px(POPOVER_WIDTH))
297 .p(px(6.))
298 .gap(px(4.))
299 .rounded(px(10.))
300 .flyout_surface()
301 .occlude()
302 .on_mouse_down_out(move |_, _, cx| close(cx))
303 .on_mouse_down(MouseButton::Left, |_, _, cx| {
304 GlobalState::suppress_text_selection(cx);
305 cx.stop_propagation();
306 })
307 .on_mouse_down(MouseButton::Right, |_, _, cx| cx.stop_propagation())
308 .on_scroll_wheel(|_, _, cx| cx.stop_propagation())
309 .when(!tabs.is_empty(), |panel| {
310 panel.child(h_flex().gap(px(2.)).children(tabs.iter().map(|tab| {
311 tab_button(
312 tab,
313 tab.glyph.as_deref() == selected,
314 controls.select_tab.clone(),
315 directory,
316 )
317 })))
318 })
319 .child(
320 v_flex()
321 .id("reaction-popover-entries")
322 .max_h(px(POPOVER_MAX_HEIGHT))
323 .overflow_y_scrollbar()
324 .children(entries.iter().map(|entry| entry_row(entry, directory))),
325 );
326 deferred(
327 anchored()
328 .anchor(Anchor::TopLeft)
329 .offset(point(px(0.), px(PILL_HEIGHT + POPOVER_GAP)))
330 .snap_to_window_with_margin(px(8.))
331 .child(panel),
332 )
333 .with_priority(2)
334 .into_any_element()
335}
336
337pub fn reaction_pills(
338 chips: &[ReactionChip],
339 index: usize,
340 own: bool,
341 directory: &Directory,
342 react: Option<PickHandler>,
343 controls: Option<&ReactionControls>,
344) -> Div {
345 h_flex()
346 .gap(px(4.))
347 .flex_wrap()
348 .children(chips.iter().map(|chip| {
349 let open_here = controls
350 .and_then(|controls| controls.popover.as_ref().map(|state| (controls, state)))
351 .filter(|(_, state)| state.anchor_glyph == chip.glyph());
352 div()
353 .relative()
354 .flex_none()
355 .child(pill(chip, index, own, directory, react.clone(), controls))
356 .when_some(open_here, |wrapper, (controls, state)| {
357 wrapper.child(popover(chips, state.tab.as_deref(), directory, controls))
358 })
359 }))
360}