use std::collections::{HashMap, 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; use crate::i18n::{tr, tr_args}; use crate::org_emojis::{self, OrgEmojiError}; use crate::remote_image::image_source; use crate::views::image_memory::bounded; use teams_core::OrgEmoji; 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; const ORG_COLUMNS: usize = 8; #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum Tab { Emoji, Org, } enum OrgState { Loading, Ready(Vec), Failed, } fn org_empty_message(query: &str) -> String { if query.is_empty() { tr("reaction_picker.org.empty") } else { let mut args = HashMap::new(); args.insert("query".into(), query.to_owned()); tr_args("reaction_picker.org.empty_query", &args) } } 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, tab: Tab, org: OrgState, org_generation: u64, org_task: Option>, _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, tab: Tab::Emoji, org: OrgState::Loading, org_generation: 0, org_task: None, _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.tab = Tab::Emoji; self.input.update(cx, |state, cx| { state.set_value("", window, cx); state.set_placeholder("Search emoji", window, cx); state.focus(window, cx); }); if !self.customizing { self.load_org_emojis(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 { .. } => { let first = if self.tab == Tab::Org { self.visible_org_emojis().first().map(|emoji| emoji.key()) } else { self.results(cx).first().cloned() }; if let Some(first) = first { 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) })) } fn visible_org_emojis(&self) -> Vec { match &self.org { OrgState::Ready(emojis) => org_emojis::filter(emojis.as_slice(), &self.query) .into_iter() .cloned() .collect(), _ => Vec::new(), } } fn set_tab(&mut self, tab: Tab, window: &mut Window, cx: &mut Context) { if tab == self.tab { return; } self.tab = tab; let placeholder = if tab == Tab::Org { tr("reaction_picker.search.org") } else { "Search emoji".to_owned() }; self.input.update(cx, |state, cx| { state.set_placeholder(placeholder, window, cx); state.focus(window, cx); }); if tab == Tab::Org { self.load_org_emojis(cx); } cx.notify(); } fn load_org_emojis(&mut self, cx: &mut Context) { if matches!(self.org, OrgState::Ready(_)) { return; } self.org_generation += 1; let generation = self.org_generation; self.org = OrgState::Loading; let (demo, engine) = { let state = self.app.read(cx); (state.mode.demo, state.engine.clone()) }; self.org_task = Some(cx.spawn(async move |this, cx| { let outcome = org_emojis::list(engine, demo, false) .await .unwrap_or(Err(OrgEmojiError::Failed)); this.update(cx, |this, cx| { if this.org_generation != generation { return; } this.org = match outcome { Ok(emojis) => OrgState::Ready(emojis), Err(_) => OrgState::Failed, }; cx.notify(); }) .ok(); })); } fn tab_button(&self, tab: Tab, label_key: &str, cx: &mut Context) -> Stateful
{ let selected = tab == self.tab; div() .id(("reaction-picker-tab", tab as usize)) .h(px(26.)) .px(px(8.)) .flex() .items_center() .cursor_pointer() .text_size(px(12.)) .font_weight(FontWeight::SEMIBOLD) .border_b_2() .border_color(if selected { theme::accent() } else { transparent_black() }) .text_color(if selected { theme::accent_text() } else { theme::text_muted() }) .hover(|button| button.text_color(theme::text_strong())) .child(tr(label_key)) .on_click(cx.listener(move |this, _, window, cx| this.set_tab(tab, window, cx))) } fn org_cell(&self, emoji: &OrgEmoji, index: usize, cx: &mut Context) -> AnyElement { let key = emoji.key(); let name = emoji.name.clone(); let selected = self.selected.contains(&key); div() .id(("reaction-picker-org", index)) .size(px(CELL_SIZE)) .flex_none() .p(px(3.)) .rounded(px(6.)) .cursor_pointer() .when(selected, |cell| cell.bg(theme::reaction_on_own())) .hover(|cell| cell.bg(theme::popup_hover())) .child( img(bounded(image_source(&emoji.image_url()))) .size_full() .object_fit(ObjectFit::Contain), ) .tooltip(move |window, cx| Tooltip::new(name.clone()).build(window, cx)) .on_click(cx.listener(move |this, event: &ClickEvent, window, cx| { this.pick(&key, event.modifiers().shift, window, cx) })) .into_any_element() } fn render_org(&self, cx: &mut Context) -> AnyElement { match &self.org { OrgState::Loading => div() .text_size(px(11.)) .text_color(theme::text_muted()) .child("…") .into_any_element(), OrgState::Failed => div() .text_size(px(11.)) .text_color(theme::text_muted()) .child(tr("fun_picker.org.failed")) .into_any_element(), OrgState::Ready(_) => { let visible = self.visible_org_emojis(); if visible.is_empty() { return div() .text_size(px(11.)) .text_color(theme::text_muted()) .child(org_empty_message(&self.query)) .into_any_element(); } let mut rows = Vec::new(); let mut row = Vec::new(); for (index, emoji) in visible.iter().enumerate() { row.push(self.org_cell(emoji, index, cx)); if row.len() == ORG_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().gap(px(2.)).children(rows).into_any_element() } } } } 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)); } let tabs = (!self.customizing).then(|| { h_flex() .flex_none() .gap(px(2.)) .child(self.tab_button(Tab::Emoji, "reaction_picker.tab.emoji", cx)) .child(self.tab_button(Tab::Org, "reaction_picker.tab.org", cx)) }); let body = if self.tab == Tab::Org && !self.customizing { self.render_org(cx) } else { v_flex() .gap(px(2.)) .child( div() .text_size(px(11.)) .text_color(theme::text_muted()) .child(label), ) .child(v_flex().gap(px(2.)).children(rows)) .into_any_element() }; v_flex() .w(px(PICKER_WIDTH)) .gap(px(8.)) .p(px(8.)) .children(tabs) .when(self.tab != Tab::Org || self.customizing, |picker| { picker.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(body) .children(self.tone_menu(cx)) } }