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