| 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 | const FACE_SIZE: f32 = 18.; |
| 28 | const FACE_RING: f32 = 1.5; |
| 29 | const FACE_OVERLAP: f32 = 6.; |
| 30 | const POPOVER_WIDTH: f32 = 290.; |
| 31 | const POPOVER_MAX_HEIGHT: f32 = 320.; |
| 32 | const POPOVER_GAP: f32 = 4.; |
| 33 | const TAB_HEIGHT: f32 = 26.; |
| 34 | const ENTRY_HEIGHT: f32 = 48.; |
| 35 | const ENTRY_AVATAR_SIZE: f32 = 32.; |
| 36 | |
| 37 | type OpenReaction = Rc<dyn Fn(&str, &mut App)>; |
| 38 | type SelectTab = Rc<dyn Fn(Option<String>, &mut App)>; |
| 39 | |
| 40 | #[derive(Clone)] |
| 41 | pub struct ReactionPopover { |
| 42 | pub anchor_glyph: String, |
| 43 | pub tab: Option<String>, |
| 44 | } |
| 45 | |
| 46 | #[derive(Clone)] |
| 47 | pub 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 | |
| 54 | fn 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 | |
| 72 | fn 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 | |
| 77 | fn 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 | .font_family(theme::EMOJI_FONT_FAMILY) |
| 84 | .line_height(px(size)) |
| 85 | .child(label.to_owned()) |
| 86 | .into_any_element(); |
| 87 | }; |
| 88 | match directory.image(&image.url) { |
| 89 | Some(entry) => img(bounded(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 | |
| 101 | fn pill_content(chip: &ReactionChip, 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 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.gap(px(PILL_GAP)).child(chip.count.to_string()) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | fn pill( |
| 129 | chip: &ReactionChip, |
| 130 | index: usize, |
| 131 | own: bool, |
| 132 | directory: &Directory, |
| 133 | react: Option<PickHandler>, |
| 134 | controls: Option<&ReactionControls>, |
| 135 | ) -> Stateful<Div> { |
| 136 | let (background, foreground) = if chip.mine { |
| 137 | (theme::accent(), theme::white()) |
| 138 | } else if own { |
| 139 | (theme::reaction_on_own(), theme::text_soft()) |
| 140 | } else { |
| 141 | (theme::border_strong(), theme::text_soft()) |
| 142 | }; |
| 143 | let glyph = chip.click_glyph.clone(); |
| 144 | let tooltip_text = chip_tooltip(chip); |
| 145 | let toggle = controls.map(|controls| { |
| 146 | let is_open = controls |
| 147 | .popover |
| 148 | .as_ref() |
| 149 | .is_some_and(|state| state.anchor_glyph == chip.glyph()); |
| 150 | ( |
| 151 | controls.open.clone(), |
| 152 | controls.close.clone(), |
| 153 | chip.glyph(), |
| 154 | is_open, |
| 155 | ) |
| 156 | }); |
| 157 | h_flex() |
| 158 | .id(ElementId::Name( |
| 159 | format!("reaction-{index}-{}", chip.reaction_type).into(), |
| 160 | )) |
| 161 | .h(px(PILL_HEIGHT)) |
| 162 | .flex_none() |
| 163 | .pl(px(5.)) |
| 164 | .pr(px(if shows_faces(chip.reactors.len()) { |
| 165 | 2. |
| 166 | } else { |
| 167 | 8. |
| 168 | })) |
| 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, 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 | |
| 199 | fn 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 | |
| 235 | fn 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 | |
| 280 | fn 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_scrollbar() |
| 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 | |
| 333 | pub fn reaction_pills( |
| 334 | chips: &[ReactionChip], |
| 335 | index: usize, |
| 336 | own: bool, |
| 337 | directory: &Directory, |
| 338 | react: Option<PickHandler>, |
| 339 | controls: Option<&ReactionControls>, |
| 340 | ) -> Div { |
| 341 | h_flex() |
| 342 | .gap(px(4.)) |
| 343 | .flex_wrap() |
| 344 | .children(chips.iter().map(|chip| { |
| 345 | let open_here = controls |
| 346 | .and_then(|controls| controls.popover.as_ref().map(|state| (controls, state))) |
| 347 | .filter(|(_, state)| state.anchor_glyph == chip.glyph()); |
| 348 | div() |
| 349 | .relative() |
| 350 | .flex_none() |
| 351 | .child(pill(chip, index, own, directory, react.clone(), controls)) |
| 352 | .when_some(open_here, |wrapper, (controls, state)| { |
| 353 | wrapper.child(popover(chips, state.tab.as_deref(), directory, controls)) |
| 354 | }) |
| 355 | })) |
| 356 | } |