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