use std::collections::HashSet; use std::rc::Rc; use gpui_kit::base::PopoverState; use gpui_kit::component::{ h_flex, input::{Input, InputEvent, InputState}, v_flex, }; use gpui_kit::prelude::FluentBuilder as _; use gpui_kit::*; use gpui_kit::assets::IconName; use gpui_kit::component::tooltip::Tooltip; use super::customize_reactions::{CUSTOMIZE_TOOLTIP, open_customize_reactions}; use super::skin_tone_choices::{tone_badge, tone_choices}; use super::widgets::icon; use crate::app_state::AppState; use crate::emoji; use crate::emoticons; use crate::rows::reaction_glyph; use crate::theme; const PICKER_QUICK: [&str; 6] = [ "\u{1F44D}", "\u{2764}", "\u{1F602}", "\u{1F62E}", "\u{1F622}", "\u{1F64F}", ]; const FALLBACK_RECENT: [&str; 16] = [ "\u{1F389}", "\u{1F525}", "\u{1F440}", "\u{2705}", "\u{1F4AF}", "\u{1F680}", "\u{1F605}", "\u{1F914}", "\u{1F44F}", "\u{1F64C}", "\u{1F60A}", "\u{1F4A1}", "\u{1F4CC}", "\u{1F41E}", "\u{1F9EA}", "\u{2615}", ]; const PICKER_WIDTH: f32 = 296.; const CELL_SIZE: f32 = 32.; const GRID_COLUMNS: usize = 8; const GRID_LIMIT: usize = 32; pub type PickHandler = Rc; pub type DoneHandler = Rc; #[derive(Clone)] struct PickTarget { on_pick: PickHandler, on_done: DoneHandler, popover: WeakEntity, } pub struct ReactionPicker { app: Entity, input: Entity, query: String, recent: Entity, selected: HashSet, target: Option, skin_tone: u8, tone_menu: Option<(String, Point)>, customizing: bool, _subscription: Subscription, } impl ReactionPicker { pub fn new( app: Entity, recent: Entity, window: &mut Window, cx: &mut Context, ) -> Self { let input = cx.new(|cx| InputState::new(window, cx).placeholder("Search emoji")); let subscription = cx.subscribe_in(&input, window, Self::on_input_event); ReactionPicker { app, input, query: String::new(), recent, selected: HashSet::new(), target: None, skin_tone: 0, tone_menu: None, customizing: false, _subscription: subscription, } } pub fn for_customizing(mut self) -> Self { self.customizing = true; self } pub fn set_target( &mut self, on_pick: PickHandler, on_done: DoneHandler, popover: WeakEntity, selected: HashSet, ) { self.target = Some(PickTarget { on_pick, on_done, popover, }); self.selected = selected .iter() .map(|glyph| emoticons::without_tone(glyph)) .collect(); } pub fn reset(&mut self, window: &mut Window, cx: &mut Context) { let store = self.app.read(cx).store.clone(); self.skin_tone = teams_core::skin_tone(&store); self.tone_menu = None; self.recent.update(cx, |recent, cx| { recent.reload(&store); cx.notify(); }); self.query.clear(); self.input.update(cx, |state, cx| { state.set_value("", window, cx); state.focus(window, cx); }); cx.notify(); } fn on_input_event( &mut self, input: &Entity, event: &InputEvent, window: &mut Window, cx: &mut Context, ) { match event { InputEvent::Change => { self.query = input.read(cx).value().trim().to_owned(); cx.notify(); } InputEvent::PressEnter { .. } => { if let Some(first) = self.results(cx).first().cloned() { self.pick(&first, false, window, cx); } } _ => {} } } fn results(&self, cx: &App) -> Vec { let recent = self.recent.read(cx).glyphs(); if self.query.is_empty() { let fill = FALLBACK_RECENT .iter() .map(|glyph| (*glyph).to_owned()) .filter(|glyph| !recent.contains(glyph)); return recent .iter() .cloned() .chain(fill) .take(GRID_LIMIT) .collect(); } emoji::search(&self.query, &recent, GRID_LIMIT) .into_iter() .map(|found| found.glyph.to_owned()) .collect() } fn pick(&mut self, glyph: &str, keep_open: bool, window: &mut Window, cx: &mut Context) { let target = if keep_open { self.target.clone() } else { self.target.take() }; let Some(target) = target else { return; }; self.tone_menu = None; let normalized = emoticons::without_tone(&reaction_glyph(glyph)); if !self.selected.remove(&normalized) { self.selected.insert(normalized); } (target.on_pick)(glyph, window, cx); if keep_open { cx.notify(); return; } target .popover .update(cx, |state, cx| state.dismiss(window, cx)) .ok(); (target.on_done)(window, cx); } fn pick_tone(&mut self, glyph: &str, tone: u8, window: &mut Window, cx: &mut Context) { self.skin_tone = tone; if !self.customizing { self.app .update(cx, |state, cx| state.set_skin_tone(tone, cx)); } self.pick(&emoticons::with_tone(glyph, tone), false, window, cx); } fn open_customize(&mut self, window: &mut Window, cx: &mut Context) { if let Some(target) = self.target.take() { target .popover .update(cx, |state, cx| state.dismiss(window, cx)) .ok(); (target.on_done)(window, cx); } open_customize_reactions(self.app.clone(), window, cx); } fn customize_button(&self, cx: &mut Context) -> Option { (!self.customizing).then(|| { div() .id("customize-reactions-trigger") .size(px(CELL_SIZE)) .flex_none() .flex() .items_center() .justify_center() .rounded(px(8.)) .cursor_pointer() .hover(|button| button.bg(theme::popup_hover())) .child(icon(IconName::Settings, 16., theme::text_soft())) .tooltip(|window, cx| Tooltip::new(CUSTOMIZE_TOOLTIP).build(window, cx)) .on_click(cx.listener(|this, _, window, cx| this.open_customize(window, cx))) .into_any_element() }) } fn tone_menu(&self, cx: &mut Context) -> Option { let (base, position) = self.tone_menu.clone()?; let (pick_view, dismiss_view) = (cx.entity().downgrade(), cx.entity().downgrade()); let glyph = base.clone(); Some(tone_choices( "reaction-picker-tones", position, &glyph, self.skin_tone, Rc::new(move |tone, window, cx| { pick_view .update(cx, |this, cx| this.pick_tone(&base, tone, window, cx)) .ok(); }), Rc::new(move |_, cx| { dismiss_view .update(cx, |this, cx| { this.tone_menu = None; cx.notify(); }) .ok(); }), )) } fn cell( &self, glyph: String, index: usize, toned: bool, cx: &mut Context, ) -> Stateful
{ let toneable = toned && emoticons::is_toneable(&glyph); let shown = if toneable { emoticons::with_tone(&glyph, self.skin_tone) } else { glyph.clone() }; let picked = shown.clone(); let selected = self .selected .contains(&emoticons::without_tone(&reaction_glyph(&glyph))); div() .id(("reaction-picker-cell", index)) .relative() .size(px(CELL_SIZE)) .flex() .items_center() .justify_center() .rounded(px(6.)) .text_size(px(20.)) .font_family(theme::EMOJI_FONT_FAMILY) .cursor_pointer() .when(selected, |cell| cell.bg(theme::reaction_on_own())) .hover(|cell| cell.bg(theme::popup_hover())) .child(shown) .when(toneable, |cell| { let base = glyph.clone(); cell.child(tone_badge()).on_mouse_down( MouseButton::Right, cx.listener(move |this, event: &MouseDownEvent, _, cx| { cx.stop_propagation(); this.tone_menu = Some((base.clone(), event.position)); cx.notify(); }), ) }) .on_click(cx.listener(move |this, event: &ClickEvent, window, cx| { this.pick(&picked, event.modifiers().shift, window, cx) })) } } impl Render for ReactionPicker { fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { let results = self.results(cx); let label = if self.query.is_empty() { "Recently used" } else if results.is_empty() { "No emoji found" } else { "Results" }; let quick = PICKER_QUICK .iter() .enumerate() .map(|(index, glyph)| self.cell((*glyph).to_owned(), 1000 + index, false, cx)) .collect::>(); let mut rows = Vec::new(); let mut row = Vec::new(); for (index, glyph) in results.into_iter().enumerate() { row.push(self.cell(glyph, index, true, cx)); if row.len() == GRID_COLUMNS { rows.push(h_flex().gap(px(2.)).children(std::mem::take(&mut row))); } } if !row.is_empty() { rows.push(h_flex().gap(px(2.)).children(row)); } v_flex() .w(px(PICKER_WIDTH)) .gap(px(8.)) .p(px(8.)) .child(h_flex().gap(px(2.)).children(quick)) .child( h_flex() .gap(px(4.)) .items_center() .child( div() .flex_1() .h(px(32.)) .px(px(8.)) .flex() .items_center() .rounded(px(8.)) .bg(theme::popup_hover()) .child(Input::new(&self.input).appearance(false).bordered(false)), ) .children(self.customize_button(cx)), ) .child( div() .text_size(px(11.)) .text_color(theme::text_muted()) .child(label), ) .child(v_flex().gap(px(2.)).children(rows)) .children(self.tone_menu(cx)) } }