BlueGit

ict rusty-teams

Files

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

reaction_pills.rs

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