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