BlueGit

ict rusty-teams

Files

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

reaction_picker.rs

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