BlueGit

ict rusty-teams

Files

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

reaction_pills.rs

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