BlueGit

ict rusty-teams

Files

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

transfer_picker.rs

transfer_picker.rs 11.5 KiB Download Raw
1use gpui_kit::assets::IconName;
2use gpui_kit::component::{
3 button::{Button, ButtonVariants as _},
4 h_flex,
5 input::{Input, InputEvent, InputState},
6 v_flex,
7};
8use gpui_kit::prelude::FluentBuilder as _;
9use gpui_kit::*;
10
11use super::avatar::person_avatar;
12use super::switcher::clamp_highlight;
13use super::widgets::icon;
14use crate::app_state::AppState;
15use crate::call::TransferCandidate;
16use crate::fuzzy;
17use crate::theme;
18
19const MAX_RESULTS: usize = 6;
20const CARD_WIDTH: f32 = 440.;
21const CARD_RADIUS: f32 = 12.;
22const CARD_PADDING: f32 = 16.;
23const SECTION_GAP: f32 = 12.;
24const BACKDROP_OPACITY: f32 = 0.55;
25const ROW_HEIGHT: f32 = 44.;
26const AVATAR_SIZE: f32 = 28.;
27const FIELD_HEIGHT: f32 = 36.;
28const CLOSE_SIZE: f32 = 28.;
29const DISABLED_OPACITY: f32 = 0.4;
30
31pub fn visible_candidates(
32 candidates: &[(String, TransferCandidate)],
33 query: &str,
34) -> Vec<TransferCandidate> {
35 fuzzy::rank(query, candidates, MAX_RESULTS)
36}
37
38pub struct TransferPicker {
39 app: Entity<AppState>,
40 candidates: Vec<(String, TransferCandidate)>,
41 search: Entity<InputState>,
42 results: Vec<TransferCandidate>,
43 highlighted: usize,
44 target: Option<TransferCandidate>,
45 _subscription: Subscription,
46}
47
48impl TransferPicker {
49 pub fn new(
50 app: Entity<AppState>,
51 candidates: Vec<TransferCandidate>,
52 window: &mut Window,
53 cx: &mut Context<Self>,
54 ) -> Self {
55 let candidates: Vec<(String, TransferCandidate)> = candidates
56 .into_iter()
57 .map(|candidate| (candidate.name.clone(), candidate))
58 .collect();
59 let search = cx.new(|cx| InputState::new(window, cx).placeholder("Search people"));
60 let subscription = cx.subscribe_in(&search, window, Self::on_search_event);
61 search.update(cx, |input, cx| input.focus(window, cx));
62 let results = visible_candidates(&candidates, "");
63 TransferPicker {
64 app,
65 candidates,
66 search,
67 results,
68 highlighted: 0,
69 target: None,
70 _subscription: subscription,
71 }
72 }
73
74 pub fn choose(&mut self, candidate: TransferCandidate) {
75 self.target = Some(candidate);
76 }
77
78 fn on_search_event(
79 &mut self,
80 input: &Entity<InputState>,
81 event: &InputEvent,
82 _: &mut Window,
83 cx: &mut Context<Self>,
84 ) {
85 match event {
86 InputEvent::Change => {
87 let query = input.read(cx).value().to_string();
88 self.results = visible_candidates(&self.candidates, &query);
89 self.highlighted = 0;
90 cx.notify();
91 }
92 InputEvent::PressEnter { .. } => {
93 if let Some(candidate) = self.results.get(self.highlighted).cloned() {
94 self.target = Some(candidate);
95 cx.notify();
96 }
97 }
98 _ => {}
99 }
100 }
101
102 fn on_key_down(&mut self, event: &KeyDownEvent, _: &mut Window, cx: &mut Context<Self>) {
103 match event.keystroke.key.as_str() {
104 "escape" => self.close(cx),
105 "down" => {
106 self.highlighted = clamp_highlight(self.highlighted, 1, self.results.len());
107 cx.notify();
108 }
109 "up" => {
110 self.highlighted = clamp_highlight(self.highlighted, -1, self.results.len());
111 cx.notify();
112 }
113 _ => {}
114 }
115 }
116
117 fn close(&self, cx: &mut Context<Self>) {
118 self.app
119 .update(cx, |state, cx| state.close_transfer_picker(cx));
120 }
121
122 fn list(&self, cx: &mut Context<Self>) -> Stateful<Div> {
123 let directory = &self.app.read(cx).directory;
124 let rows: Vec<AnyElement> = self
125 .results
126 .iter()
127 .enumerate()
128 .map(|(index, candidate)| {
129 let chosen = self.target.as_ref() == Some(candidate);
130 let picked = candidate.clone();
131 h_flex()
132 .id(ElementId::Name(format!("transfer-target-{index}").into()))
133 .h(px(ROW_HEIGHT))
134 .flex_none()
135 .px(px(8.))
136 .gap(px(10.))
137 .items_center()
138 .rounded(px(6.))
139 .cursor_pointer()
140 .when(
141 chosen || (self.target.is_none() && index == self.highlighted),
142 |row| row.bg(theme::surface_raised()),
143 )
144 .hover(|row| row.bg(theme::row_hover()))
145 .child(person_avatar(
146 directory,
147 candidate.user_id.as_deref(),
148 &candidate.name,
149 AVATAR_SIZE,
150 ))
151 .child(
152 div()
153 .flex_1()
154 .min_w_0()
155 .truncate()
156 .text_size(px(14.))
157 .child(candidate.name.clone()),
158 )
159 .when(chosen, |row| {
160 row.child(icon(IconName::Check, 16., theme::accent_text()))
161 })
162 .on_click(cx.listener(move |this, _, _, cx| {
163 this.choose(picked.clone());
164 cx.notify();
165 }))
166 .into_any_element()
167 })
168 .collect();
169 v_flex()
170 .id("transfer-targets")
171 .w_full()
172 .max_h(px(ROW_HEIGHT * MAX_RESULTS as f32))
173 .overflow_y_scroll()
174 .children(rows)
175 }
176
177 fn action(
178 &self,
179 id: &'static str,
180 label: &'static str,
181 primary: bool,
182 run: fn(&mut AppState, TransferCandidate, &mut Context<AppState>),
183 ) -> AnyElement {
184 let button = Button::new(id).label(label);
185 let button = if primary { button } else { button.ghost() };
186 let Some(target) = self.target.clone() else {
187 return button.opacity(DISABLED_OPACITY).into_any_element();
188 };
189 let app = self.app.clone();
190 button
191 .on_click(move |_, _, cx| app.update(cx, |state, cx| run(state, target.clone(), cx)))
192 .into_any_element()
193 }
194}
195
196impl Render for TransferPicker {
197 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
198 let field = h_flex()
199 .w_full()
200 .h(px(FIELD_HEIGHT))
201 .px(px(10.))
202 .gap(px(8.))
203 .items_center()
204 .rounded(px(6.))
205 .bg(theme::surface())
206 .border_1()
207 .border_color(theme::border())
208 .child(icon(IconName::Search, 14., theme::text_muted()))
209 .child(
210 div()
211 .flex_1()
212 .child(Input::new(&self.search).appearance(false).bordered(false)),
213 );
214 let actions = h_flex()
215 .w_full()
216 .justify_end()
217 .gap(px(8.))
218 .child(
219 Button::new("transfer-cancel")
220 .ghost()
221 .label("Cancel")
222 .on_click(cx.listener(|this, _, _, cx| this.close(cx))),
223 )
224 .child(self.action(
225 "transfer-consult",
226 "Consult first",
227 false,
228 AppState::start_consult,
229 ))
230 .child(self.action(
231 "transfer-now",
232 "Transfer now",
233 true,
234 AppState::transfer_call_blind,
235 ));
236 div()
237 .id("transfer-layer")
238 .absolute()
239 .top_0()
240 .left_0()
241 .size_full()
242 .flex()
243 .items_center()
244 .justify_center()
245 .child(
246 div()
247 .id("transfer-backdrop")
248 .absolute()
249 .top_0()
250 .left_0()
251 .size_full()
252 .bg(black().opacity(BACKDROP_OPACITY))
253 .occlude()
254 .on_click(cx.listener(|this, _, _, cx| this.close(cx))),
255 )
256 .child(
257 v_flex()
258 .id("transfer-card")
259 .w(px(CARD_WIDTH))
260 .max_w(relative(1.))
261 .p(px(CARD_PADDING))
262 .gap(px(SECTION_GAP))
263 .rounded(px(CARD_RADIUS))
264 .bg(theme::background())
265 .border_1()
266 .border_color(theme::border_strong())
267 .text_color(theme::text())
268 .shadow_lg()
269 .occlude()
270 .on_key_down(cx.listener(Self::on_key_down))
271 .child(
272 h_flex()
273 .w_full()
274 .items_center()
275 .gap(px(SECTION_GAP))
276 .child(
277 div()
278 .flex_1()
279 .text_size(px(15.))
280 .font_weight(FontWeight::SEMIBOLD)
281 .child("Transfer call"),
282 )
283 .child(
284 div()
285 .id("transfer-close")
286 .size(px(CLOSE_SIZE))
287 .flex_none()
288 .flex()
289 .items_center()
290 .justify_center()
291 .rounded_full()
292 .cursor_pointer()
293 .hover(|button| button.bg(theme::row_hover()))
294 .on_click(cx.listener(|this, _, _, cx| this.close(cx)))
295 .child(icon(IconName::X, 16., theme::text_muted())),
296 ),
297 )
298 .child(field)
299 .child(self.list(cx))
300 .child(actions),
301 )
302 }
303}
304
305#[cfg(test)]
306mod tests {
307 use super::{MAX_RESULTS, visible_candidates};
308 use crate::call::TransferCandidate;
309
310 fn candidate(name: &str) -> (String, TransferCandidate) {
311 (
312 name.to_owned(),
313 TransferCandidate {
314 mri: format!("8:orgid:{name}"),
315 name: name.to_owned(),
316 user_id: Some(name.to_owned()),
317 chat_id: format!("19:{name}"),
318 },
319 )
320 }
321
322 #[test]
323 fn the_search_ranks_people_by_name() {
324 let people = vec![
325 candidate("Ana Weiss"),
326 candidate("Bo Brandt"),
327 candidate("Anja Vogel"),
328 ];
329 let found: Vec<String> = visible_candidates(&people, "anj")
330 .into_iter()
331 .map(|person| person.name)
332 .collect();
333 assert_eq!(found.first().map(String::as_str), Some("Anja Vogel"));
334 assert_eq!(visible_candidates(&people, "").len(), 3);
335 assert!(visible_candidates(&people, "zzz").is_empty());
336 }
337
338 #[test]
339 fn the_list_is_capped() {
340 let people: Vec<(String, TransferCandidate)> = (0..20)
341 .map(|index| candidate(&format!("Person {index}")))
342 .collect();
343 assert_eq!(visible_candidates(&people, "").len(), MAX_RESULTS);
344 }
345}