| 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 crate::app_state::AppState; |
| 14 | use crate::emoji; |
| 15 | use crate::rows::reaction_glyph; |
| 16 | use crate::theme; |
| 17 | |
| 18 | const PICKER_QUICK: [&str; 6] = [ |
| 19 | "\u{1F44D}", |
| 20 | "\u{2764}", |
| 21 | "\u{1F602}", |
| 22 | "\u{1F62E}", |
| 23 | "\u{1F622}", |
| 24 | "\u{1F64F}", |
| 25 | ]; |
| 26 | const FALLBACK_RECENT: [&str; 16] = [ |
| 27 | "\u{1F389}", |
| 28 | "\u{1F525}", |
| 29 | "\u{1F440}", |
| 30 | "\u{2705}", |
| 31 | "\u{1F4AF}", |
| 32 | "\u{1F680}", |
| 33 | "\u{1F605}", |
| 34 | "\u{1F914}", |
| 35 | "\u{1F44F}", |
| 36 | "\u{1F64C}", |
| 37 | "\u{1F60A}", |
| 38 | "\u{1F4A1}", |
| 39 | "\u{1F4CC}", |
| 40 | "\u{1F41E}", |
| 41 | "\u{1F9EA}", |
| 42 | "\u{2615}", |
| 43 | ]; |
| 44 | const PICKER_WIDTH: f32 = 296.; |
| 45 | const CELL_SIZE: f32 = 32.; |
| 46 | const GRID_COLUMNS: usize = 8; |
| 47 | const GRID_LIMIT: usize = 32; |
| 48 | |
| 49 | pub type PickHandler = Rc<dyn Fn(&str, &mut Window, &mut App)>; |
| 50 | pub type DoneHandler = Rc<dyn Fn(&mut Window, &mut App)>; |
| 51 | |
| 52 | #[derive(Clone)] |
| 53 | struct PickTarget { |
| 54 | on_pick: PickHandler, |
| 55 | on_done: DoneHandler, |
| 56 | popover: WeakEntity<PopoverState>, |
| 57 | } |
| 58 | |
| 59 | pub struct ReactionPicker { |
| 60 | app: Entity<AppState>, |
| 61 | input: Entity<InputState>, |
| 62 | query: String, |
| 63 | recent: Entity<emoji::Recent>, |
| 64 | selected: HashSet<String>, |
| 65 | target: Option<PickTarget>, |
| 66 | _subscription: Subscription, |
| 67 | } |
| 68 | |
| 69 | impl ReactionPicker { |
| 70 | pub fn new( |
| 71 | app: Entity<AppState>, |
| 72 | recent: Entity<emoji::Recent>, |
| 73 | window: &mut Window, |
| 74 | cx: &mut Context<Self>, |
| 75 | ) -> Self { |
| 76 | let input = cx.new(|cx| InputState::new(window, cx).placeholder("Search emoji")); |
| 77 | let subscription = cx.subscribe_in(&input, window, Self::on_input_event); |
| 78 | ReactionPicker { |
| 79 | app, |
| 80 | input, |
| 81 | query: String::new(), |
| 82 | recent, |
| 83 | selected: HashSet::new(), |
| 84 | target: None, |
| 85 | _subscription: subscription, |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | pub fn set_target( |
| 90 | &mut self, |
| 91 | on_pick: PickHandler, |
| 92 | on_done: DoneHandler, |
| 93 | popover: WeakEntity<PopoverState>, |
| 94 | selected: HashSet<String>, |
| 95 | ) { |
| 96 | self.target = Some(PickTarget { |
| 97 | on_pick, |
| 98 | on_done, |
| 99 | popover, |
| 100 | }); |
| 101 | self.selected = selected; |
| 102 | } |
| 103 | |
| 104 | pub fn reset(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 105 | let store = self.app.read(cx).store.clone(); |
| 106 | self.recent.update(cx, |recent, cx| { |
| 107 | recent.reload(&store); |
| 108 | cx.notify(); |
| 109 | }); |
| 110 | self.query.clear(); |
| 111 | self.input.update(cx, |state, cx| { |
| 112 | state.set_value("", window, cx); |
| 113 | state.focus(window, cx); |
| 114 | }); |
| 115 | cx.notify(); |
| 116 | } |
| 117 | |
| 118 | fn on_input_event( |
| 119 | &mut self, |
| 120 | input: &Entity<InputState>, |
| 121 | event: &InputEvent, |
| 122 | window: &mut Window, |
| 123 | cx: &mut Context<Self>, |
| 124 | ) { |
| 125 | match event { |
| 126 | InputEvent::Change => { |
| 127 | self.query = input.read(cx).value().trim().to_owned(); |
| 128 | cx.notify(); |
| 129 | } |
| 130 | InputEvent::PressEnter { .. } => { |
| 131 | if let Some(first) = self.results(cx).first().cloned() { |
| 132 | self.pick(&first, false, window, cx); |
| 133 | } |
| 134 | } |
| 135 | _ => {} |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | fn results(&self, cx: &App) -> Vec<String> { |
| 140 | let recent = self.recent.read(cx).glyphs(); |
| 141 | if self.query.is_empty() { |
| 142 | let fill = FALLBACK_RECENT |
| 143 | .iter() |
| 144 | .map(|glyph| (*glyph).to_owned()) |
| 145 | .filter(|glyph| !recent.contains(glyph)); |
| 146 | return recent |
| 147 | .iter() |
| 148 | .cloned() |
| 149 | .chain(fill) |
| 150 | .take(GRID_LIMIT) |
| 151 | .collect(); |
| 152 | } |
| 153 | emoji::search(&self.query, &recent, GRID_LIMIT) |
| 154 | .into_iter() |
| 155 | .map(|found| found.glyph.to_owned()) |
| 156 | .collect() |
| 157 | } |
| 158 | |
| 159 | fn pick(&mut self, glyph: &str, keep_open: bool, window: &mut Window, cx: &mut Context<Self>) { |
| 160 | let target = if keep_open { |
| 161 | self.target.clone() |
| 162 | } else { |
| 163 | self.target.take() |
| 164 | }; |
| 165 | let Some(target) = target else { |
| 166 | return; |
| 167 | }; |
| 168 | let normalized = reaction_glyph(glyph); |
| 169 | if !self.selected.remove(&normalized) { |
| 170 | self.selected.insert(normalized); |
| 171 | } |
| 172 | (target.on_pick)(glyph, window, cx); |
| 173 | if keep_open { |
| 174 | cx.notify(); |
| 175 | return; |
| 176 | } |
| 177 | target |
| 178 | .popover |
| 179 | .update(cx, |state, cx| state.dismiss(window, cx)) |
| 180 | .ok(); |
| 181 | (target.on_done)(window, cx); |
| 182 | } |
| 183 | |
| 184 | fn cell(&self, glyph: String, index: usize, cx: &mut Context<Self>) -> Stateful<Div> { |
| 185 | let picked = glyph.clone(); |
| 186 | let selected = self.selected.contains(&reaction_glyph(&glyph)); |
| 187 | div() |
| 188 | .id(("reaction-picker-cell", index)) |
| 189 | .size(px(CELL_SIZE)) |
| 190 | .flex() |
| 191 | .items_center() |
| 192 | .justify_center() |
| 193 | .rounded(px(6.)) |
| 194 | .text_size(px(20.)) |
| 195 | .cursor_pointer() |
| 196 | .when(selected, |cell| cell.bg(theme::reaction_on_own())) |
| 197 | .hover(|cell| cell.bg(theme::border_strong())) |
| 198 | .child(glyph) |
| 199 | .on_click(cx.listener(move |this, event: &ClickEvent, window, cx| { |
| 200 | this.pick(&picked, event.modifiers().shift, window, cx) |
| 201 | })) |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | impl Render for ReactionPicker { |
| 206 | fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 207 | let results = self.results(cx); |
| 208 | let label = if self.query.is_empty() { |
| 209 | "Recently used" |
| 210 | } else if results.is_empty() { |
| 211 | "No emoji found" |
| 212 | } else { |
| 213 | "Results" |
| 214 | }; |
| 215 | let quick = PICKER_QUICK |
| 216 | .iter() |
| 217 | .enumerate() |
| 218 | .map(|(index, glyph)| self.cell((*glyph).to_owned(), 1000 + index, cx)) |
| 219 | .collect::<Vec<_>>(); |
| 220 | let mut rows = Vec::new(); |
| 221 | let mut row = Vec::new(); |
| 222 | for (index, glyph) in results.into_iter().enumerate() { |
| 223 | row.push(self.cell(glyph, index, cx)); |
| 224 | if row.len() == GRID_COLUMNS { |
| 225 | rows.push(h_flex().gap(px(2.)).children(std::mem::take(&mut row))); |
| 226 | } |
| 227 | } |
| 228 | if !row.is_empty() { |
| 229 | rows.push(h_flex().gap(px(2.)).children(row)); |
| 230 | } |
| 231 | v_flex() |
| 232 | .w(px(PICKER_WIDTH)) |
| 233 | .gap(px(8.)) |
| 234 | .p(px(8.)) |
| 235 | .child(h_flex().gap(px(2.)).children(quick)) |
| 236 | .child( |
| 237 | div() |
| 238 | .h(px(32.)) |
| 239 | .px(px(8.)) |
| 240 | .flex() |
| 241 | .items_center() |
| 242 | .rounded(px(8.)) |
| 243 | .bg(theme::surface()) |
| 244 | .border_1() |
| 245 | .border_color(theme::border_strong()) |
| 246 | .child(Input::new(&self.input).appearance(false).bordered(false)), |
| 247 | ) |
| 248 | .child( |
| 249 | div() |
| 250 | .text_size(px(11.)) |
| 251 | .text_color(theme::text_muted()) |
| 252 | .child(label), |
| 253 | ) |
| 254 | .child(v_flex().gap(px(2.)).children(rows)) |
| 255 | } |
| 256 | } |