| 1 | use gpui_kit::assets::IconName; |
| 2 | use gpui_kit::component::{ |
| 3 | h_flex, |
| 4 | input::{Input, InputEvent, InputState}, |
| 5 | v_flex, |
| 6 | }; |
| 7 | use std::collections::{HashMap, HashSet}; |
| 8 | use std::ops::Range; |
| 9 | use std::time::Duration; |
| 10 | |
| 11 | use chrono::Local; |
| 12 | use gpui_kit::prelude::FluentBuilder as _; |
| 13 | use gpui_kit::*; |
| 14 | use store::SearchHit; |
| 15 | use teams_core::substrate_search::{SearchSession, Suggestions}; |
| 16 | use teams_core::{HIGHLIGHT_END, HIGHLIGHT_START}; |
| 17 | |
| 18 | use super::avatar::{person_avatar, spec_avatar, team_avatar}; |
| 19 | use super::switcher_server::{ |
| 20 | PersonResult, merge_messages, people_results, server_message_results, |
| 21 | }; |
| 22 | use super::widgets::icon; |
| 23 | use crate::app_state::{AppState, Selection, chat_title}; |
| 24 | use crate::data::{Directory, is_one_on_one}; |
| 25 | use crate::format; |
| 26 | use crate::fuzzy; |
| 27 | use crate::runtime; |
| 28 | use crate::sidebar_model::{AvatarSpec, avatar_for}; |
| 29 | use crate::theme::{self, ElevatedSurface as _}; |
| 30 | |
| 31 | const MAX_RESULTS: usize = 9; |
| 32 | const MAX_RESULTS_WITH_MESSAGES: usize = 5; |
| 33 | const MAX_MESSAGE_RESULTS: usize = 5; |
| 34 | const MIN_MESSAGE_QUERY_CHARS: usize = 2; |
| 35 | const MAX_PEOPLE_RESULTS: usize = 4; |
| 36 | const MIN_SERVER_QUERY_CHARS: usize = 2; |
| 37 | const SERVER_DEBOUNCE: Duration = Duration::from_millis(200); |
| 38 | const MESSAGE_GROUP: &str = "Messages"; |
| 39 | const PEOPLE_GROUP: &str = "People"; |
| 40 | const CARD_WIDTH: f32 = 520.; |
| 41 | const CARD_TOP: f32 = 6.; |
| 42 | |
| 43 | pub enum SwitcherEvent { |
| 44 | Pick(Selection), |
| 45 | PickMessage { |
| 46 | selection: Selection, |
| 47 | message_id: String, |
| 48 | }, |
| 49 | PickPerson { |
| 50 | user_id: String, |
| 51 | }, |
| 52 | Close, |
| 53 | } |
| 54 | |
| 55 | #[derive(Clone)] |
| 56 | pub struct MessageResult { |
| 57 | pub selection: Selection, |
| 58 | pub message_id: String, |
| 59 | pub author: String, |
| 60 | pub conversation: String, |
| 61 | pub date: String, |
| 62 | pub snippet: String, |
| 63 | pub matches: Vec<Range<usize>>, |
| 64 | } |
| 65 | |
| 66 | pub fn parse_snippet(snippet: &str) -> (String, Vec<Range<usize>>) { |
| 67 | let mut text = String::with_capacity(snippet.len()); |
| 68 | let mut matches = Vec::new(); |
| 69 | let mut start: Option<usize> = None; |
| 70 | for character in snippet.chars() { |
| 71 | match character { |
| 72 | HIGHLIGHT_START => start = Some(text.len()), |
| 73 | HIGHLIGHT_END => { |
| 74 | if let Some(begin) = start.take().filter(|begin| *begin < text.len()) { |
| 75 | matches.push(begin..text.len()); |
| 76 | } |
| 77 | } |
| 78 | '\n' | '\r' => text.push(' '), |
| 79 | other => text.push(other), |
| 80 | } |
| 81 | } |
| 82 | (text, matches) |
| 83 | } |
| 84 | |
| 85 | pub fn message_results( |
| 86 | hits: Vec<SearchHit>, |
| 87 | conversations: &HashMap<String, (String, Selection)>, |
| 88 | today: chrono::NaiveDate, |
| 89 | offset: app_base::format::Zone, |
| 90 | ) -> Vec<MessageResult> { |
| 91 | hits.into_iter() |
| 92 | .filter_map(|hit| { |
| 93 | let (conversation, selection) = conversations.get(&hit.conversation_id)?.clone(); |
| 94 | let (snippet, matches) = parse_snippet(&hit.snippet); |
| 95 | Some(MessageResult { |
| 96 | selection, |
| 97 | message_id: hit.message_id, |
| 98 | author: hit.sender_name.unwrap_or_else(|| "Unknown".to_owned()), |
| 99 | conversation, |
| 100 | date: format::list_time_label(hit.created_at, today, offset), |
| 101 | snippet, |
| 102 | matches, |
| 103 | }) |
| 104 | }) |
| 105 | .collect() |
| 106 | } |
| 107 | |
| 108 | #[derive(Clone)] |
| 109 | pub enum CandidateAvatar { |
| 110 | Chat(AvatarSpec), |
| 111 | Team { name: String, key: String }, |
| 112 | } |
| 113 | |
| 114 | #[derive(Clone)] |
| 115 | pub struct Candidate { |
| 116 | pub title: String, |
| 117 | pub subtitle: String, |
| 118 | pub avatar: CandidateAvatar, |
| 119 | pub selection: Selection, |
| 120 | } |
| 121 | |
| 122 | impl Candidate { |
| 123 | fn group(&self) -> &'static str { |
| 124 | match self.selection { |
| 125 | Selection::Chat(_) => "Chats", |
| 126 | Selection::Channel(_) => "Channels", |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | pub struct Switcher { |
| 132 | app: Entity<AppState>, |
| 133 | input: Entity<InputState>, |
| 134 | candidates: Vec<(String, Candidate)>, |
| 135 | conversations: HashMap<String, (String, Selection)>, |
| 136 | results: Vec<Candidate>, |
| 137 | people: Vec<PersonResult>, |
| 138 | local_messages: Vec<MessageResult>, |
| 139 | messages: Vec<MessageResult>, |
| 140 | search_session: SearchSession, |
| 141 | generation: u64, |
| 142 | rows_scroll: ScrollHandle, |
| 143 | highlighted: usize, |
| 144 | _subscription: Subscription, |
| 145 | } |
| 146 | |
| 147 | impl EventEmitter<SwitcherEvent> for Switcher {} |
| 148 | |
| 149 | pub fn candidates_from(state: &AppState) -> Vec<(String, Candidate)> { |
| 150 | let me = state.directory.me.as_ref(); |
| 151 | let chats = state.sidebar.chats.iter().map(|chat| { |
| 152 | let title = chat_title(chat); |
| 153 | ( |
| 154 | title.clone(), |
| 155 | Candidate { |
| 156 | title, |
| 157 | subtitle: if is_one_on_one(chat) { |
| 158 | "Chat".to_owned() |
| 159 | } else { |
| 160 | format!("Group chat, {} participants", chat.members.len()) |
| 161 | }, |
| 162 | avatar: CandidateAvatar::Chat(avatar_for(chat, me)), |
| 163 | selection: Selection::Chat(chat.id.clone()), |
| 164 | }, |
| 165 | ) |
| 166 | }); |
| 167 | let channels = state.sidebar.teams.iter().flat_map(|team| { |
| 168 | team.channels.iter().map(|channel| { |
| 169 | ( |
| 170 | format!("{} / {}", team.team.name, channel.name), |
| 171 | Candidate { |
| 172 | title: channel.name.clone(), |
| 173 | subtitle: team.team.name.clone(), |
| 174 | avatar: CandidateAvatar::Team { |
| 175 | name: team.team.name.clone(), |
| 176 | key: team.team.id.clone(), |
| 177 | }, |
| 178 | selection: Selection::Channel(channel.id.clone()), |
| 179 | }, |
| 180 | ) |
| 181 | }) |
| 182 | }); |
| 183 | chats.chain(channels).collect() |
| 184 | } |
| 185 | |
| 186 | pub fn candidate_avatar( |
| 187 | directory: &Directory, |
| 188 | avatar: &CandidateAvatar, |
| 189 | size: f32, |
| 190 | ring: Hsla, |
| 191 | ) -> AnyElement { |
| 192 | match avatar { |
| 193 | CandidateAvatar::Chat(spec) => spec_avatar(directory, spec, size, ring), |
| 194 | CandidateAvatar::Team { name, key } => { |
| 195 | let photo = directory.avatar(&teams_core::team_avatar_key(key)); |
| 196 | team_avatar(photo, name, key, size, size / 4.) |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | pub fn clamp_highlight(current: usize, delta: isize, length: usize) -> usize { |
| 202 | if length == 0 { |
| 203 | return 0; |
| 204 | } |
| 205 | (current as isize + delta).clamp(0, length as isize - 1) as usize |
| 206 | } |
| 207 | |
| 208 | impl Switcher { |
| 209 | pub fn new( |
| 210 | app: Entity<AppState>, |
| 211 | candidates: Vec<(String, Candidate)>, |
| 212 | initial_query: &str, |
| 213 | window: &mut Window, |
| 214 | cx: &mut Context<Self>, |
| 215 | ) -> Self { |
| 216 | let input = cx.new(|cx| { |
| 217 | InputState::new(window, cx).placeholder("Jump to a chat, channel, person or message") |
| 218 | }); |
| 219 | if !initial_query.is_empty() { |
| 220 | let query = initial_query.to_owned(); |
| 221 | input.update(cx, |input, cx| input.set_value(query, window, cx)); |
| 222 | } |
| 223 | let subscription = cx.subscribe_in(&input, window, Self::on_input_event); |
| 224 | input.update(cx, |input, cx| input.focus(window, cx)); |
| 225 | let conversations = candidates |
| 226 | .iter() |
| 227 | .map(|(title, candidate)| { |
| 228 | ( |
| 229 | candidate.selection.conversation_id().to_owned(), |
| 230 | (title.clone(), candidate.selection.clone()), |
| 231 | ) |
| 232 | }) |
| 233 | .collect(); |
| 234 | let mut switcher = Switcher { |
| 235 | app, |
| 236 | input, |
| 237 | candidates, |
| 238 | conversations, |
| 239 | results: Vec::new(), |
| 240 | people: Vec::new(), |
| 241 | local_messages: Vec::new(), |
| 242 | messages: Vec::new(), |
| 243 | search_session: SearchSession::new(), |
| 244 | generation: 0, |
| 245 | rows_scroll: ScrollHandle::new(), |
| 246 | highlighted: 0, |
| 247 | _subscription: subscription, |
| 248 | }; |
| 249 | switcher.refresh(initial_query, cx); |
| 250 | switcher |
| 251 | } |
| 252 | |
| 253 | fn refresh(&mut self, query: &str, cx: &mut Context<Self>) { |
| 254 | let wants_messages = query.trim().chars().count() >= MIN_MESSAGE_QUERY_CHARS; |
| 255 | let limit = if wants_messages { |
| 256 | MAX_RESULTS_WITH_MESSAGES |
| 257 | } else { |
| 258 | MAX_RESULTS |
| 259 | }; |
| 260 | self.results = fuzzy::rank(query, &self.candidates, limit); |
| 261 | self.results |
| 262 | .sort_by_key(|candidate| candidate.group() != "Chats"); |
| 263 | self.local_messages.clear(); |
| 264 | self.messages.clear(); |
| 265 | self.people.clear(); |
| 266 | self.highlighted = 0; |
| 267 | self.generation += 1; |
| 268 | if wants_messages { |
| 269 | self.schedule_local_search(query.to_owned(), cx); |
| 270 | } |
| 271 | if query.trim().chars().count() >= MIN_SERVER_QUERY_CHARS { |
| 272 | self.schedule_server_search(query.trim().to_owned(), cx); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | fn schedule_local_search(&mut self, query: String, cx: &mut Context<Self>) { |
| 277 | let store = self.app.read(cx).store.clone(); |
| 278 | let generation = self.generation; |
| 279 | cx.spawn(async move |this, cx| { |
| 280 | let hits = cx |
| 281 | .background_spawn(async move { |
| 282 | store |
| 283 | .search_messages(&query, None, MAX_MESSAGE_RESULTS) |
| 284 | .unwrap_or_default() |
| 285 | }) |
| 286 | .await; |
| 287 | this.update(cx, |switcher, cx| { |
| 288 | switcher.apply_local(generation, hits, cx) |
| 289 | }) |
| 290 | .ok(); |
| 291 | }) |
| 292 | .detach(); |
| 293 | } |
| 294 | |
| 295 | fn apply_local(&mut self, generation: u64, hits: Vec<SearchHit>, cx: &mut Context<Self>) { |
| 296 | if generation != self.generation { |
| 297 | return; |
| 298 | } |
| 299 | self.local_messages = message_results( |
| 300 | hits, |
| 301 | &self.conversations, |
| 302 | Local::now().date_naive(), |
| 303 | app_base::format::Zone::Local, |
| 304 | ); |
| 305 | self.messages = merge_messages( |
| 306 | &self.local_messages, |
| 307 | std::mem::take(&mut self.messages), |
| 308 | MAX_MESSAGE_RESULTS, |
| 309 | ); |
| 310 | cx.notify(); |
| 311 | } |
| 312 | |
| 313 | fn schedule_server_search(&mut self, query: String, cx: &mut Context<Self>) { |
| 314 | let Some(engine) = self.app.read(cx).engine.clone() else { |
| 315 | return; |
| 316 | }; |
| 317 | let generation = self.generation; |
| 318 | let search_session = self.search_session.clone(); |
| 319 | cx.spawn(async move |this, cx| { |
| 320 | cx.background_executor().timer(SERVER_DEBOUNCE).await; |
| 321 | let still_current = this |
| 322 | .update(cx, |switcher, _| switcher.generation == generation) |
| 323 | .unwrap_or(false); |
| 324 | if !still_current { |
| 325 | return; |
| 326 | } |
| 327 | let receiver = |
| 328 | runtime::spawn( |
| 329 | async move { engine.server_suggestions(&search_session, &query).await }, |
| 330 | ); |
| 331 | let suggestions = match receiver.await { |
| 332 | Ok(Ok(suggestions)) => suggestions, |
| 333 | _ => return, |
| 334 | }; |
| 335 | this.update(cx, |switcher, cx| { |
| 336 | switcher.apply_server(generation, suggestions, cx) |
| 337 | }) |
| 338 | .ok(); |
| 339 | }) |
| 340 | .detach(); |
| 341 | } |
| 342 | |
| 343 | fn apply_server(&mut self, generation: u64, suggestions: Suggestions, cx: &mut Context<Self>) { |
| 344 | if generation != self.generation { |
| 345 | return; |
| 346 | } |
| 347 | let held_message = self |
| 348 | .highlighted |
| 349 | .checked_sub(self.results.len() + self.people.len()) |
| 350 | .and_then(|index| self.messages.get(index)) |
| 351 | .map(|message| message.message_id.clone()); |
| 352 | let shown_titles: HashSet<String> = self |
| 353 | .results |
| 354 | .iter() |
| 355 | .map(|candidate| candidate.title.clone()) |
| 356 | .collect(); |
| 357 | let my_user_id = self |
| 358 | .app |
| 359 | .read(cx) |
| 360 | .directory |
| 361 | .me |
| 362 | .as_ref() |
| 363 | .map(|me| me.user_id.clone()); |
| 364 | self.people = people_results( |
| 365 | &suggestions.people, |
| 366 | &shown_titles, |
| 367 | my_user_id.as_deref(), |
| 368 | MAX_PEOPLE_RESULTS, |
| 369 | ); |
| 370 | let now = Local::now(); |
| 371 | let server = server_message_results( |
| 372 | &suggestions.messages, |
| 373 | &self.conversations, |
| 374 | now.date_naive(), |
| 375 | app_base::format::Zone::Local, |
| 376 | ); |
| 377 | self.messages = merge_messages(&self.local_messages, server, MAX_MESSAGE_RESULTS); |
| 378 | let held_position = held_message.and_then(|message_id| { |
| 379 | self.messages |
| 380 | .iter() |
| 381 | .position(|message| message.message_id == message_id) |
| 382 | }); |
| 383 | self.highlighted = match held_position { |
| 384 | Some(position) => self.results.len() + self.people.len() + position, |
| 385 | None => self.highlighted.min(self.result_count().saturating_sub(1)), |
| 386 | }; |
| 387 | cx.notify(); |
| 388 | } |
| 389 | |
| 390 | fn result_count(&self) -> usize { |
| 391 | self.results.len() + self.people.len() + self.messages.len() |
| 392 | } |
| 393 | |
| 394 | fn on_input_event( |
| 395 | &mut self, |
| 396 | input: &Entity<InputState>, |
| 397 | event: &InputEvent, |
| 398 | _: &mut Window, |
| 399 | cx: &mut Context<Self>, |
| 400 | ) { |
| 401 | match event { |
| 402 | InputEvent::Change => { |
| 403 | let query = input.read(cx).value().to_string(); |
| 404 | self.refresh(&query, cx); |
| 405 | cx.notify(); |
| 406 | } |
| 407 | InputEvent::PressEnter { .. } => self.pick(cx), |
| 408 | _ => {} |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | fn pick(&mut self, cx: &mut Context<Self>) { |
| 413 | let person_index = self.highlighted.checked_sub(self.results.len()); |
| 414 | let message_index = person_index.and_then(|index| index.checked_sub(self.people.len())); |
| 415 | if let Some(candidate) = self.results.get(self.highlighted) { |
| 416 | cx.emit(SwitcherEvent::Pick(candidate.selection.clone())); |
| 417 | } else if let Some(person) = person_index.and_then(|index| self.people.get(index)) { |
| 418 | cx.emit(SwitcherEvent::PickPerson { |
| 419 | user_id: person.user_id.clone(), |
| 420 | }); |
| 421 | } else if let Some(message) = message_index.and_then(|index| self.messages.get(index)) { |
| 422 | cx.emit(SwitcherEvent::PickMessage { |
| 423 | selection: message.selection.clone(), |
| 424 | message_id: message.message_id.clone(), |
| 425 | }); |
| 426 | } else { |
| 427 | cx.emit(SwitcherEvent::Close); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | fn on_key_down(&mut self, event: &KeyDownEvent, _: &mut Window, cx: &mut Context<Self>) { |
| 432 | match event.keystroke.key.as_str() { |
| 433 | "down" => self.highlighted = clamp_highlight(self.highlighted, 1, self.result_count()), |
| 434 | "up" => self.highlighted = clamp_highlight(self.highlighted, -1, self.result_count()), |
| 435 | "escape" => cx.emit(SwitcherEvent::Close), |
| 436 | _ => return, |
| 437 | } |
| 438 | self.rows_scroll.scroll_to_item(row_position( |
| 439 | &self.results, |
| 440 | self.people.len(), |
| 441 | self.highlighted, |
| 442 | )); |
| 443 | cx.notify(); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | fn row_position(results: &[Candidate], people: usize, highlighted: usize) -> usize { |
| 448 | let groups = |candidates: &[Candidate]| { |
| 449 | candidates |
| 450 | .iter() |
| 451 | .enumerate() |
| 452 | .filter(|(index, candidate)| { |
| 453 | *index == 0 || candidates[index - 1].group() != candidate.group() |
| 454 | }) |
| 455 | .count() |
| 456 | }; |
| 457 | if highlighted < results.len() { |
| 458 | return groups(&results[..=highlighted]) + highlighted; |
| 459 | } |
| 460 | let before_people = groups(results) + results.len(); |
| 461 | let person = highlighted - results.len(); |
| 462 | if person < people { |
| 463 | return before_people + 1 + person; |
| 464 | } |
| 465 | let before_messages = before_people + if people > 0 { 1 + people } else { 0 }; |
| 466 | before_messages + 1 + (person - people) |
| 467 | } |
| 468 | |
| 469 | fn group_header(label: &'static str) -> AnyElement { |
| 470 | div() |
| 471 | .px(px(12.)) |
| 472 | .pt(px(8.)) |
| 473 | .pb(px(4.)) |
| 474 | .text_size(px(11.)) |
| 475 | .font_weight(FontWeight::SEMIBOLD) |
| 476 | .text_color(theme::text_muted()) |
| 477 | .child(label) |
| 478 | .into_any_element() |
| 479 | } |
| 480 | |
| 481 | impl Render for Switcher { |
| 482 | fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 483 | let highlighted = self.highlighted; |
| 484 | let directory = &self.app.read(cx).directory; |
| 485 | let mut rows: Vec<AnyElement> = Vec::new(); |
| 486 | let mut last_group = ""; |
| 487 | for (index, candidate) in self.results.iter().enumerate() { |
| 488 | if candidate.group() != last_group { |
| 489 | last_group = candidate.group(); |
| 490 | rows.push(group_header(last_group)); |
| 491 | } |
| 492 | let selection = candidate.selection.clone(); |
| 493 | let avatar = candidate_avatar(directory, &candidate.avatar, 28., theme::popup()); |
| 494 | let kind_icon = match candidate.selection { |
| 495 | Selection::Chat(_) => IconName::MessageCircle, |
| 496 | Selection::Channel(_) => IconName::Hash, |
| 497 | }; |
| 498 | rows.push( |
| 499 | h_flex() |
| 500 | .id(ElementId::Name(format!("switch-{index}").into())) |
| 501 | .mx(px(6.)) |
| 502 | .px(px(6.)) |
| 503 | .h(px(44.)) |
| 504 | .gap(px(10.)) |
| 505 | .items_center() |
| 506 | .rounded(px(6.)) |
| 507 | .cursor_pointer() |
| 508 | .when(index == highlighted, |row| row.bg(theme::popup_hover())) |
| 509 | .hover(|row| row.bg(theme::popup_hover())) |
| 510 | .child(avatar) |
| 511 | .child( |
| 512 | v_flex() |
| 513 | .flex_1() |
| 514 | .min_w_0() |
| 515 | .child( |
| 516 | div() |
| 517 | .truncate() |
| 518 | .text_size(px(14.)) |
| 519 | .text_color(theme::text()) |
| 520 | .child(candidate.title.clone()), |
| 521 | ) |
| 522 | .child( |
| 523 | div() |
| 524 | .truncate() |
| 525 | .text_size(px(12.)) |
| 526 | .text_color(theme::text_muted()) |
| 527 | .child(candidate.subtitle.clone()), |
| 528 | ), |
| 529 | ) |
| 530 | .child(icon(kind_icon, 14., theme::text_muted())) |
| 531 | .on_click(cx.listener(move |_, _, _, cx| { |
| 532 | cx.emit(SwitcherEvent::Pick(selection.clone())); |
| 533 | })) |
| 534 | .into_any_element(), |
| 535 | ); |
| 536 | } |
| 537 | if !self.people.is_empty() { |
| 538 | rows.push(group_header(PEOPLE_GROUP)); |
| 539 | } |
| 540 | for (offset, person) in self.people.iter().enumerate() { |
| 541 | let index = self.results.len() + offset; |
| 542 | let user_id = person.user_id.clone(); |
| 543 | rows.push( |
| 544 | h_flex() |
| 545 | .id(ElementId::Name(format!("switch-person-{index}").into())) |
| 546 | .mx(px(6.)) |
| 547 | .px(px(6.)) |
| 548 | .h(px(44.)) |
| 549 | .gap(px(10.)) |
| 550 | .items_center() |
| 551 | .rounded(px(6.)) |
| 552 | .cursor_pointer() |
| 553 | .when(index == highlighted, |row| row.bg(theme::popup_hover())) |
| 554 | .hover(|row| row.bg(theme::popup_hover())) |
| 555 | .child(person_avatar( |
| 556 | directory, |
| 557 | Some(&person.user_id), |
| 558 | &person.name, |
| 559 | 28., |
| 560 | )) |
| 561 | .child( |
| 562 | v_flex() |
| 563 | .flex_1() |
| 564 | .min_w_0() |
| 565 | .child( |
| 566 | div() |
| 567 | .truncate() |
| 568 | .text_size(px(14.)) |
| 569 | .text_color(theme::text()) |
| 570 | .child(person.name.clone()), |
| 571 | ) |
| 572 | .when(!person.subtitle.is_empty(), |column| { |
| 573 | column.child( |
| 574 | div() |
| 575 | .truncate() |
| 576 | .text_size(px(12.)) |
| 577 | .text_color(theme::text_muted()) |
| 578 | .child(person.subtitle.clone()), |
| 579 | ) |
| 580 | }), |
| 581 | ) |
| 582 | .child(icon(IconName::User, 14., theme::text_muted())) |
| 583 | .on_click(cx.listener(move |_, _, _, cx| { |
| 584 | cx.emit(SwitcherEvent::PickPerson { |
| 585 | user_id: user_id.clone(), |
| 586 | }); |
| 587 | })) |
| 588 | .into_any_element(), |
| 589 | ); |
| 590 | } |
| 591 | if !self.messages.is_empty() { |
| 592 | rows.push(group_header(MESSAGE_GROUP)); |
| 593 | } |
| 594 | for (offset, message) in self.messages.iter().enumerate() { |
| 595 | let index = self.results.len() + self.people.len() + offset; |
| 596 | let (selection, message_id) = (message.selection.clone(), message.message_id.clone()); |
| 597 | let marks = message |
| 598 | .matches |
| 599 | .iter() |
| 600 | .map(|range| { |
| 601 | ( |
| 602 | range.clone(), |
| 603 | HighlightStyle { |
| 604 | color: Some(theme::text()), |
| 605 | font_weight: Some(FontWeight::BOLD), |
| 606 | ..Default::default() |
| 607 | }, |
| 608 | ) |
| 609 | }) |
| 610 | .collect::<Vec<_>>(); |
| 611 | rows.push( |
| 612 | h_flex() |
| 613 | .id(ElementId::Name(format!("switch-message-{index}").into())) |
| 614 | .mx(px(6.)) |
| 615 | .px(px(6.)) |
| 616 | .h(px(52.)) |
| 617 | .gap(px(10.)) |
| 618 | .items_center() |
| 619 | .rounded(px(6.)) |
| 620 | .cursor_pointer() |
| 621 | .when(index == highlighted, |row| row.bg(theme::popup_hover())) |
| 622 | .hover(|row| row.bg(theme::popup_hover())) |
| 623 | .child(person_avatar(directory, None, &message.author, 28.)) |
| 624 | .child( |
| 625 | v_flex() |
| 626 | .flex_1() |
| 627 | .min_w_0() |
| 628 | .gap(px(1.)) |
| 629 | .child( |
| 630 | h_flex() |
| 631 | .gap(px(6.)) |
| 632 | .items_baseline() |
| 633 | .child( |
| 634 | div() |
| 635 | .flex_none() |
| 636 | .text_size(px(13.)) |
| 637 | .font_weight(FontWeight::SEMIBOLD) |
| 638 | .text_color(theme::text()) |
| 639 | .child(message.author.clone()), |
| 640 | ) |
| 641 | .child( |
| 642 | div() |
| 643 | .min_w_0() |
| 644 | .truncate() |
| 645 | .text_size(px(12.)) |
| 646 | .text_color(theme::text_muted()) |
| 647 | .child(format!("in {}", message.conversation)), |
| 648 | ) |
| 649 | .child(div().flex_1()) |
| 650 | .child( |
| 651 | div() |
| 652 | .flex_none() |
| 653 | .text_size(px(11.)) |
| 654 | .text_color(theme::text_muted()) |
| 655 | .child(message.date.clone()), |
| 656 | ), |
| 657 | ) |
| 658 | .child( |
| 659 | div() |
| 660 | .truncate() |
| 661 | .text_size(px(12.5)) |
| 662 | .text_color(theme::text_soft()) |
| 663 | .child( |
| 664 | StyledText::new(message.snippet.clone()) |
| 665 | .with_highlights(marks), |
| 666 | ), |
| 667 | ), |
| 668 | ) |
| 669 | .on_click(cx.listener(move |_, _, _, cx| { |
| 670 | cx.emit(SwitcherEvent::PickMessage { |
| 671 | selection: selection.clone(), |
| 672 | message_id: message_id.clone(), |
| 673 | }); |
| 674 | })) |
| 675 | .into_any_element(), |
| 676 | ); |
| 677 | } |
| 678 | let footer = h_flex() |
| 679 | .h(px(32.)) |
| 680 | .px(px(12.)) |
| 681 | .gap(px(16.)) |
| 682 | .items_center() |
| 683 | .text_size(px(11.)) |
| 684 | .text_color(theme::text_muted()) |
| 685 | .child("Enter to open") |
| 686 | .child("Arrows to select") |
| 687 | .child("Esc to close"); |
| 688 | div() |
| 689 | .id("switcher-backdrop") |
| 690 | .absolute() |
| 691 | .size_full() |
| 692 | .top_0() |
| 693 | .left_0() |
| 694 | .flex() |
| 695 | .justify_center() |
| 696 | .items_start() |
| 697 | .pt(px(CARD_TOP)) |
| 698 | .on_click(cx.listener(|_, _, _, cx| cx.emit(SwitcherEvent::Close))) |
| 699 | .child( |
| 700 | v_flex() |
| 701 | .id("switcher-card") |
| 702 | .w(px(CARD_WIDTH)) |
| 703 | .max_h(px(640.)) |
| 704 | .rounded(px(10.)) |
| 705 | .flyout_surface() |
| 706 | .overflow_hidden() |
| 707 | .on_click(|_, _, cx| cx.stop_propagation()) |
| 708 | .on_key_down(cx.listener(Self::on_key_down)) |
| 709 | .child( |
| 710 | h_flex() |
| 711 | .p(px(8.)) |
| 712 | .pl(px(12.)) |
| 713 | .gap(px(8.)) |
| 714 | .items_center() |
| 715 | .child(icon(IconName::Search, 14., theme::text_muted())) |
| 716 | .child( |
| 717 | div().flex_1().min_w_0().child( |
| 718 | Input::new(&self.input).appearance(false).bordered(false), |
| 719 | ), |
| 720 | ), |
| 721 | ) |
| 722 | .child( |
| 723 | v_flex() |
| 724 | .id("switcher-rows") |
| 725 | .track_scroll(&self.rows_scroll) |
| 726 | .w_full() |
| 727 | .flex_1() |
| 728 | .min_h_0() |
| 729 | .overflow_y_scroll() |
| 730 | .py_1() |
| 731 | .children(rows), |
| 732 | ) |
| 733 | .child(footer), |
| 734 | ) |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | #[cfg(test)] |
| 739 | mod tests { |
| 740 | use super::{Candidate, CandidateAvatar, clamp_highlight, parse_snippet, row_position}; |
| 741 | use crate::app_state::Selection; |
| 742 | use teams_core::{HIGHLIGHT_END, HIGHLIGHT_START}; |
| 743 | |
| 744 | #[test] |
| 745 | fn snippet_markers_become_ranges() { |
| 746 | let raw = format!("a {HIGHLIGHT_START}Build{HIGHLIGHT_END} 42\nok"); |
| 747 | let (text, matches) = parse_snippet(&raw); |
| 748 | assert_eq!(text, "a Build 42 ok"); |
| 749 | assert_eq!(matches, vec![2..7]); |
| 750 | } |
| 751 | |
| 752 | #[test] |
| 753 | fn unterminated_or_empty_markers_are_ignored() { |
| 754 | let raw = format!("{HIGHLIGHT_START}{HIGHLIGHT_END}x{HIGHLIGHT_START}y"); |
| 755 | assert_eq!(parse_snippet(&raw), ("xy".to_owned(), Vec::new())); |
| 756 | } |
| 757 | |
| 758 | #[test] |
| 759 | fn highlight_stays_inside_the_list() { |
| 760 | assert_eq!(clamp_highlight(0, -1, 3), 0); |
| 761 | assert_eq!(clamp_highlight(2, 1, 3), 2); |
| 762 | assert_eq!(clamp_highlight(1, 1, 3), 2); |
| 763 | assert_eq!(clamp_highlight(0, 1, 0), 0); |
| 764 | } |
| 765 | |
| 766 | fn candidate(selection: Selection) -> Candidate { |
| 767 | Candidate { |
| 768 | title: String::new(), |
| 769 | subtitle: String::new(), |
| 770 | avatar: CandidateAvatar::Team { |
| 771 | name: String::new(), |
| 772 | key: String::new(), |
| 773 | }, |
| 774 | selection, |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | #[test] |
| 779 | fn row_positions_count_the_group_headers() { |
| 780 | let results = [ |
| 781 | candidate(Selection::Chat("a".into())), |
| 782 | candidate(Selection::Chat("b".into())), |
| 783 | candidate(Selection::Channel("c".into())), |
| 784 | ]; |
| 785 | assert_eq!(row_position(&results, 2, 0), 1); |
| 786 | assert_eq!(row_position(&results, 2, 2), 4); |
| 787 | assert_eq!(row_position(&results, 2, 3), 6); |
| 788 | assert_eq!(row_position(&results, 2, 5), 9); |
| 789 | assert_eq!(row_position(&results, 0, 3), 6); |
| 790 | } |
| 791 | } |