BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / reaction_picker.rs

reaction_picker.rs

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