| 1 | use std::collections::HashSet; |
| 2 | use std::rc::Rc; |
| 3 | |
| 4 | use gpui_kit::base::PopoverState; |
| 5 | use gpui_kit::component::{ |
| 6 | h_flex, |
| 7 | input::{Input, InputEvent, InputState}, |
| 8 | v_flex, |
| 9 | }; |
| 10 | use gpui_kit::prelude::FluentBuilder as _; |
| 11 | use gpui_kit::*; |
| 12 | |
| 13 | use super::skin_tone_choices::{tone_badge, tone_choices}; |
| 14 | use crate::app_state::AppState; |
| 15 | use crate::emoji; |
| 16 | use crate::emoticons; |
| 17 | use crate::rows::reaction_glyph; |
| 18 | use crate::theme; |
| 19 | |
| 20 | const PICKER_QUICK: [&str; 6] = [ |
| 21 | "\u{1F44D}", |
| 22 | "\u{2764}", |
| 23 | "\u{1F602}", |
| 24 | "\u{1F62E}", |
| 25 | "\u{1F622}", |
| 26 | "\u{1F64F}", |
| 27 | ]; |
| 28 | const FALLBACK_RECENT: [&str; 16] = [ |
| 29 | "\u{1F389}", |
| 30 | "\u{1F525}", |
| 31 | "\u{1F440}", |
| 32 | "\u{2705}", |
| 33 | "\u{1F4AF}", |
| 34 | "\u{1F680}", |
| 35 | "\u{1F605}", |
| 36 | "\u{1F914}", |
| 37 | "\u{1F44F}", |
| 38 | "\u{1F64C}", |
| 39 | "\u{1F60A}", |
| 40 | "\u{1F4A1}", |
| 41 | "\u{1F4CC}", |
| 42 | "\u{1F41E}", |
| 43 | "\u{1F9EA}", |
| 44 | "\u{2615}", |
| 45 | ]; |
| 46 | const PICKER_WIDTH: f32 = 296.; |
| 47 | const CELL_SIZE: f32 = 32.; |
| 48 | const GRID_COLUMNS: usize = 8; |
| 49 | const GRID_LIMIT: usize = 32; |
| 50 | |
| 51 | pub type PickHandler = Rc<dyn Fn(&str, &mut Window, &mut App)>; |
| 52 | pub type DoneHandler = Rc<dyn Fn(&mut Window, &mut App)>; |
| 53 | |
| 54 | #[derive(Clone)] |
| 55 | struct PickTarget { |
| 56 | on_pick: PickHandler, |
| 57 | on_done: DoneHandler, |
| 58 | popover: WeakEntity<PopoverState>, |
| 59 | } |
| 60 | |
| 61 | pub struct ReactionPicker { |
| 62 | app: Entity<AppState>, |
| 63 | input: Entity<InputState>, |
| 64 | query: String, |
| 65 | recent: Entity<emoji::Recent>, |
| 66 | selected: HashSet<String>, |
| 67 | target: Option<PickTarget>, |
| 68 | skin_tone: u8, |
| 69 | tone_menu: Option<(String, Point<Pixels>)>, |
| 70 | _subscription: Subscription, |
| 71 | } |
| 72 | |
| 73 | impl ReactionPicker { |
| 74 | pub fn new( |
| 75 | app: Entity<AppState>, |
| 76 | recent: Entity<emoji::Recent>, |
| 77 | window: &mut Window, |
| 78 | cx: &mut Context<Self>, |
| 79 | ) -> Self { |
| 80 | let input = cx.new(|cx| InputState::new(window, cx).placeholder("Search emoji")); |
| 81 | let subscription = cx.subscribe_in(&input, window, Self::on_input_event); |
| 82 | ReactionPicker { |
| 83 | app, |
| 84 | input, |
| 85 | query: String::new(), |
| 86 | recent, |
| 87 | selected: HashSet::new(), |
| 88 | target: None, |
| 89 | skin_tone: 0, |
| 90 | tone_menu: None, |
| 91 | _subscription: subscription, |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | pub fn set_target( |
| 96 | &mut self, |
| 97 | on_pick: PickHandler, |
| 98 | on_done: DoneHandler, |
| 99 | popover: WeakEntity<PopoverState>, |
| 100 | selected: HashSet<String>, |
| 101 | ) { |
| 102 | self.target = Some(PickTarget { |
| 103 | on_pick, |
| 104 | on_done, |
| 105 | popover, |
| 106 | }); |
| 107 | self.selected = selected |
| 108 | .iter() |
| 109 | .map(|glyph| emoticons::without_tone(glyph)) |
| 110 | .collect(); |
| 111 | } |
| 112 | |
| 113 | pub fn reset(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 114 | let store = self.app.read(cx).store.clone(); |
| 115 | self.skin_tone = teams_core::skin_tone(&store); |
| 116 | self.tone_menu = None; |
| 117 | self.recent.update(cx, |recent, cx| { |
| 118 | recent.reload(&store); |
| 119 | cx.notify(); |
| 120 | }); |
| 121 | self.query.clear(); |
| 122 | self.input.update(cx, |state, cx| { |
| 123 | state.set_value("", window, cx); |
| 124 | state.focus(window, cx); |
| 125 | }); |
| 126 | cx.notify(); |
| 127 | } |
| 128 | |
| 129 | fn on_input_event( |
| 130 | &mut self, |
| 131 | input: &Entity<InputState>, |
| 132 | event: &InputEvent, |
| 133 | window: &mut Window, |
| 134 | cx: &mut Context<Self>, |
| 135 | ) { |
| 136 | match event { |
| 137 | InputEvent::Change => { |
| 138 | self.query = input.read(cx).value().trim().to_owned(); |
| 139 | cx.notify(); |
| 140 | } |
| 141 | InputEvent::PressEnter { .. } => { |
| 142 | if let Some(first) = self.results(cx).first().cloned() { |
| 143 | self.pick(&first, false, window, cx); |
| 144 | } |
| 145 | } |
| 146 | _ => {} |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | fn results(&self, cx: &App) -> Vec<String> { |
| 151 | let recent = self.recent.read(cx).glyphs(); |
| 152 | if self.query.is_empty() { |
| 153 | let fill = FALLBACK_RECENT |
| 154 | .iter() |
| 155 | .map(|glyph| (*glyph).to_owned()) |
| 156 | .filter(|glyph| !recent.contains(glyph)); |
| 157 | return recent |
| 158 | .iter() |
| 159 | .cloned() |
| 160 | .chain(fill) |
| 161 | .take(GRID_LIMIT) |
| 162 | .collect(); |
| 163 | } |
| 164 | emoji::search(&self.query, &recent, GRID_LIMIT) |
| 165 | .into_iter() |
| 166 | .map(|found| found.glyph.to_owned()) |
| 167 | .collect() |
| 168 | } |
| 169 | |
| 170 | fn pick(&mut self, glyph: &str, keep_open: bool, window: &mut Window, cx: &mut Context<Self>) { |
| 171 | let target = if keep_open { |
| 172 | self.target.clone() |
| 173 | } else { |
| 174 | self.target.take() |
| 175 | }; |
| 176 | let Some(target) = target else { |
| 177 | return; |
| 178 | }; |
| 179 | self.tone_menu = None; |
| 180 | let normalized = emoticons::without_tone(&reaction_glyph(glyph)); |
| 181 | if !self.selected.remove(&normalized) { |
| 182 | self.selected.insert(normalized); |
| 183 | } |
| 184 | (target.on_pick)(glyph, window, cx); |
| 185 | if keep_open { |
| 186 | cx.notify(); |
| 187 | return; |
| 188 | } |
| 189 | target |
| 190 | .popover |
| 191 | .update(cx, |state, cx| state.dismiss(window, cx)) |
| 192 | .ok(); |
| 193 | (target.on_done)(window, cx); |
| 194 | } |
| 195 | |
| 196 | fn pick_tone(&mut self, glyph: &str, tone: u8, window: &mut Window, cx: &mut Context<Self>) { |
| 197 | self.skin_tone = tone; |
| 198 | self.app.update(cx, |state, cx| state.set_skin_tone(tone, cx)); |
| 199 | self.pick(&emoticons::with_tone(glyph, tone), false, window, cx); |
| 200 | } |
| 201 | |
| 202 | fn tone_menu(&self, cx: &mut Context<Self>) -> Option<AnyElement> { |
| 203 | let (base, position) = self.tone_menu.clone()?; |
| 204 | let (pick_view, dismiss_view) = (cx.entity().downgrade(), cx.entity().downgrade()); |
| 205 | let glyph = base.clone(); |
| 206 | Some(tone_choices( |
| 207 | "reaction-picker-tones", |
| 208 | position, |
| 209 | &glyph, |
| 210 | self.skin_tone, |
| 211 | Rc::new(move |tone, window, cx| { |
| 212 | pick_view |
| 213 | .update(cx, |this, cx| this.pick_tone(&base, tone, window, cx)) |
| 214 | .ok(); |
| 215 | }), |
| 216 | Rc::new(move |_, cx| { |
| 217 | dismiss_view |
| 218 | .update(cx, |this, cx| { |
| 219 | this.tone_menu = None; |
| 220 | cx.notify(); |
| 221 | }) |
| 222 | .ok(); |
| 223 | }), |
| 224 | )) |
| 225 | } |
| 226 | |
| 227 | fn cell( |
| 228 | &self, |
| 229 | glyph: String, |
| 230 | index: usize, |
| 231 | toned: bool, |
| 232 | cx: &mut Context<Self>, |
| 233 | ) -> Stateful<Div> { |
| 234 | let toneable = toned && emoticons::is_toneable(&glyph); |
| 235 | let shown = if toneable { |
| 236 | emoticons::with_tone(&glyph, self.skin_tone) |
| 237 | } else { |
| 238 | glyph.clone() |
| 239 | }; |
| 240 | let picked = shown.clone(); |
| 241 | let selected = self |
| 242 | .selected |
| 243 | .contains(&emoticons::without_tone(&reaction_glyph(&glyph))); |
| 244 | div() |
| 245 | .id(("reaction-picker-cell", index)) |
| 246 | .relative() |
| 247 | .size(px(CELL_SIZE)) |
| 248 | .flex() |
| 249 | .items_center() |
| 250 | .justify_center() |
| 251 | .rounded(px(6.)) |
| 252 | .text_size(px(20.)) |
| 253 | .cursor_pointer() |
| 254 | .when(selected, |cell| cell.bg(theme::reaction_on_own())) |
| 255 | .hover(|cell| cell.bg(theme::popup_hover())) |
| 256 | .child(shown) |
| 257 | .when(toneable, |cell| { |
| 258 | let base = glyph.clone(); |
| 259 | cell.child(tone_badge()).on_mouse_down( |
| 260 | MouseButton::Right, |
| 261 | cx.listener(move |this, event: &MouseDownEvent, _, cx| { |
| 262 | cx.stop_propagation(); |
| 263 | this.tone_menu = Some((base.clone(), event.position)); |
| 264 | cx.notify(); |
| 265 | }), |
| 266 | ) |
| 267 | }) |
| 268 | .on_click(cx.listener(move |this, event: &ClickEvent, window, cx| { |
| 269 | this.pick(&picked, event.modifiers().shift, window, cx) |
| 270 | })) |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | impl Render for ReactionPicker { |
| 275 | fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 276 | let results = self.results(cx); |
| 277 | let label = if self.query.is_empty() { |
| 278 | "Recently used" |
| 279 | } else if results.is_empty() { |
| 280 | "No emoji found" |
| 281 | } else { |
| 282 | "Results" |
| 283 | }; |
| 284 | let quick = PICKER_QUICK |
| 285 | .iter() |
| 286 | .enumerate() |
| 287 | .map(|(index, glyph)| self.cell((*glyph).to_owned(), 1000 + index, false, cx)) |
| 288 | .collect::<Vec<_>>(); |
| 289 | let mut rows = Vec::new(); |
| 290 | let mut row = Vec::new(); |
| 291 | for (index, glyph) in results.into_iter().enumerate() { |
| 292 | row.push(self.cell(glyph, index, true, cx)); |
| 293 | if row.len() == GRID_COLUMNS { |
| 294 | rows.push(h_flex().gap(px(2.)).children(std::mem::take(&mut row))); |
| 295 | } |
| 296 | } |
| 297 | if !row.is_empty() { |
| 298 | rows.push(h_flex().gap(px(2.)).children(row)); |
| 299 | } |
| 300 | v_flex() |
| 301 | .w(px(PICKER_WIDTH)) |
| 302 | .gap(px(8.)) |
| 303 | .p(px(8.)) |
| 304 | .child(h_flex().gap(px(2.)).children(quick)) |
| 305 | .child( |
| 306 | div() |
| 307 | .h(px(32.)) |
| 308 | .px(px(8.)) |
| 309 | .flex() |
| 310 | .items_center() |
| 311 | .rounded(px(8.)) |
| 312 | .bg(theme::popup_hover()) |
| 313 | .child(Input::new(&self.input).appearance(false).bordered(false)), |
| 314 | ) |
| 315 | .child( |
| 316 | div() |
| 317 | .text_size(px(11.)) |
| 318 | .text_color(theme::text_muted()) |
| 319 | .child(label), |
| 320 | ) |
| 321 | .child(v_flex().gap(px(2.)).children(rows)) |
| 322 | .children(self.tone_menu(cx)) |
| 323 | } |
| 324 | } |