| 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, Offset}; |
| 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: chrono::FixedOffset, |
| 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 = if wants_messages { |
| 264 | let hits = self |
| 265 | .app |
| 266 | .read(cx) |
| 267 | .store |
| 268 | .search_messages(query, None, MAX_MESSAGE_RESULTS) |
| 269 | .unwrap_or_default(); |
| 270 | let now = Local::now(); |
| 271 | message_results( |
| 272 | hits, |
| 273 | &self.conversations, |
| 274 | now.date_naive(), |
| 275 | now.offset().fix(), |
| 276 | ) |
| 277 | } else { |
| 278 | Vec::new() |
| 279 | }; |
| 280 | self.messages = self.local_messages.clone(); |
| 281 | self.people.clear(); |
| 282 | self.highlighted = 0; |
| 283 | self.generation += 1; |
| 284 | if query.trim().chars().count() >= MIN_SERVER_QUERY_CHARS { |
| 285 | self.schedule_server_search(query.trim().to_owned(), cx); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | fn schedule_server_search(&mut self, query: String, cx: &mut Context<Self>) { |
| 290 | let Some(engine) = self.app.read(cx).engine.clone() else { |
| 291 | return; |
| 292 | }; |
| 293 | let generation = self.generation; |
| 294 | let search_session = self.search_session.clone(); |
| 295 | cx.spawn(async move |this, cx| { |
| 296 | cx.background_executor().timer(SERVER_DEBOUNCE).await; |
| 297 | let still_current = this |
| 298 | .update(cx, |switcher, _| switcher.generation == generation) |
| 299 | .unwrap_or(false); |
| 300 | if !still_current { |
| 301 | return; |
| 302 | } |
| 303 | let receiver = |
| 304 | runtime::spawn( |
| 305 | async move { engine.server_suggestions(&search_session, &query).await }, |
| 306 | ); |
| 307 | let suggestions = match receiver.await { |
| 308 | Ok(Ok(suggestions)) => suggestions, |
| 309 | _ => return, |
| 310 | }; |
| 311 | this.update(cx, |switcher, cx| { |
| 312 | switcher.apply_server(generation, suggestions, cx) |
| 313 | }) |
| 314 | .ok(); |
| 315 | }) |
| 316 | .detach(); |
| 317 | } |
| 318 | |
| 319 | fn apply_server(&mut self, generation: u64, suggestions: Suggestions, cx: &mut Context<Self>) { |
| 320 | if generation != self.generation { |
| 321 | return; |
| 322 | } |
| 323 | let held_message = self |
| 324 | .highlighted |
| 325 | .checked_sub(self.results.len() + self.people.len()) |
| 326 | .and_then(|index| self.messages.get(index)) |
| 327 | .map(|message| message.message_id.clone()); |
| 328 | let shown_titles: HashSet<String> = self |
| 329 | .results |
| 330 | .iter() |
| 331 | .map(|candidate| candidate.title.clone()) |
| 332 | .collect(); |
| 333 | let my_user_id = self |
| 334 | .app |
| 335 | .read(cx) |
| 336 | .directory |
| 337 | .me |
| 338 | .as_ref() |
| 339 | .map(|me| me.user_id.clone()); |
| 340 | self.people = people_results( |
| 341 | &suggestions.people, |
| 342 | &shown_titles, |
| 343 | my_user_id.as_deref(), |
| 344 | MAX_PEOPLE_RESULTS, |
| 345 | ); |
| 346 | let now = Local::now(); |
| 347 | let server = server_message_results( |
| 348 | &suggestions.messages, |
| 349 | &self.conversations, |
| 350 | now.date_naive(), |
| 351 | now.offset().fix(), |
| 352 | ); |
| 353 | self.messages = merge_messages(&self.local_messages, server, MAX_MESSAGE_RESULTS); |
| 354 | let held_position = held_message.and_then(|message_id| { |
| 355 | self.messages |
| 356 | .iter() |
| 357 | .position(|message| message.message_id == message_id) |
| 358 | }); |
| 359 | self.highlighted = match held_position { |
| 360 | Some(position) => self.results.len() + self.people.len() + position, |
| 361 | None => self.highlighted.min(self.result_count().saturating_sub(1)), |
| 362 | }; |
| 363 | cx.notify(); |
| 364 | } |
| 365 | |
| 366 | fn result_count(&self) -> usize { |
| 367 | self.results.len() + self.people.len() + self.messages.len() |
| 368 | } |
| 369 | |
| 370 | fn on_input_event( |
| 371 | &mut self, |
| 372 | input: &Entity<InputState>, |
| 373 | event: &InputEvent, |
| 374 | _: &mut Window, |
| 375 | cx: &mut Context<Self>, |
| 376 | ) { |
| 377 | match event { |
| 378 | InputEvent::Change => { |
| 379 | let query = input.read(cx).value().to_string(); |
| 380 | self.refresh(&query, cx); |
| 381 | cx.notify(); |
| 382 | } |
| 383 | InputEvent::PressEnter { .. } => self.pick(cx), |
| 384 | _ => {} |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | fn pick(&mut self, cx: &mut Context<Self>) { |
| 389 | let person_index = self.highlighted.checked_sub(self.results.len()); |
| 390 | let message_index = person_index.and_then(|index| index.checked_sub(self.people.len())); |
| 391 | if let Some(candidate) = self.results.get(self.highlighted) { |
| 392 | cx.emit(SwitcherEvent::Pick(candidate.selection.clone())); |
| 393 | } else if let Some(person) = person_index.and_then(|index| self.people.get(index)) { |
| 394 | cx.emit(SwitcherEvent::PickPerson { |
| 395 | user_id: person.user_id.clone(), |
| 396 | }); |
| 397 | } else if let Some(message) = message_index.and_then(|index| self.messages.get(index)) { |
| 398 | cx.emit(SwitcherEvent::PickMessage { |
| 399 | selection: message.selection.clone(), |
| 400 | message_id: message.message_id.clone(), |
| 401 | }); |
| 402 | } else { |
| 403 | cx.emit(SwitcherEvent::Close); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | fn on_key_down(&mut self, event: &KeyDownEvent, _: &mut Window, cx: &mut Context<Self>) { |
| 408 | match event.keystroke.key.as_str() { |
| 409 | "down" => self.highlighted = clamp_highlight(self.highlighted, 1, self.result_count()), |
| 410 | "up" => self.highlighted = clamp_highlight(self.highlighted, -1, self.result_count()), |
| 411 | "escape" => cx.emit(SwitcherEvent::Close), |
| 412 | _ => return, |
| 413 | } |
| 414 | self.rows_scroll.scroll_to_item(row_position( |
| 415 | &self.results, |
| 416 | self.people.len(), |
| 417 | self.highlighted, |
| 418 | )); |
| 419 | cx.notify(); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | fn row_position(results: &[Candidate], people: usize, highlighted: usize) -> usize { |
| 424 | let groups = |candidates: &[Candidate]| { |
| 425 | candidates |
| 426 | .iter() |
| 427 | .enumerate() |
| 428 | .filter(|(index, candidate)| { |
| 429 | *index == 0 || candidates[index - 1].group() != candidate.group() |
| 430 | }) |
| 431 | .count() |
| 432 | }; |
| 433 | if highlighted < results.len() { |
| 434 | return groups(&results[..=highlighted]) + highlighted; |
| 435 | } |
| 436 | let before_people = groups(results) + results.len(); |
| 437 | let person = highlighted - results.len(); |
| 438 | if person < people { |
| 439 | return before_people + 1 + person; |
| 440 | } |
| 441 | let before_messages = before_people + if people > 0 { 1 + people } else { 0 }; |
| 442 | before_messages + 1 + (person - people) |
| 443 | } |
| 444 | |
| 445 | fn group_header(label: &'static str) -> AnyElement { |
| 446 | div() |
| 447 | .px(px(12.)) |
| 448 | .pt(px(8.)) |
| 449 | .pb(px(4.)) |
| 450 | .text_size(px(11.)) |
| 451 | .font_weight(FontWeight::SEMIBOLD) |
| 452 | .text_color(theme::text_muted()) |
| 453 | .child(label) |
| 454 | .into_any_element() |
| 455 | } |
| 456 | |
| 457 | impl Render for Switcher { |
| 458 | fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 459 | let highlighted = self.highlighted; |
| 460 | let directory = &self.app.read(cx).directory; |
| 461 | let mut rows: Vec<AnyElement> = Vec::new(); |
| 462 | let mut last_group = ""; |
| 463 | for (index, candidate) in self.results.iter().enumerate() { |
| 464 | if candidate.group() != last_group { |
| 465 | last_group = candidate.group(); |
| 466 | rows.push(group_header(last_group)); |
| 467 | } |
| 468 | let selection = candidate.selection.clone(); |
| 469 | let avatar = candidate_avatar(directory, &candidate.avatar, 28., theme::popup()); |
| 470 | let kind_icon = match candidate.selection { |
| 471 | Selection::Chat(_) => IconName::MessageCircle, |
| 472 | Selection::Channel(_) => IconName::Hash, |
| 473 | }; |
| 474 | rows.push( |
| 475 | h_flex() |
| 476 | .id(ElementId::Name(format!("switch-{index}").into())) |
| 477 | .mx(px(6.)) |
| 478 | .px(px(6.)) |
| 479 | .h(px(44.)) |
| 480 | .gap(px(10.)) |
| 481 | .items_center() |
| 482 | .rounded(px(6.)) |
| 483 | .cursor_pointer() |
| 484 | .when(index == highlighted, |row| row.bg(theme::popup_hover())) |
| 485 | .hover(|row| row.bg(theme::popup_hover())) |
| 486 | .child(avatar) |
| 487 | .child( |
| 488 | v_flex() |
| 489 | .flex_1() |
| 490 | .min_w_0() |
| 491 | .child( |
| 492 | div() |
| 493 | .truncate() |
| 494 | .text_size(px(14.)) |
| 495 | .text_color(theme::text()) |
| 496 | .child(candidate.title.clone()), |
| 497 | ) |
| 498 | .child( |
| 499 | div() |
| 500 | .truncate() |
| 501 | .text_size(px(12.)) |
| 502 | .text_color(theme::text_muted()) |
| 503 | .child(candidate.subtitle.clone()), |
| 504 | ), |
| 505 | ) |
| 506 | .child(icon(kind_icon, 14., theme::text_muted())) |
| 507 | .on_click(cx.listener(move |_, _, _, cx| { |
| 508 | cx.emit(SwitcherEvent::Pick(selection.clone())); |
| 509 | })) |
| 510 | .into_any_element(), |
| 511 | ); |
| 512 | } |
| 513 | if !self.people.is_empty() { |
| 514 | rows.push(group_header(PEOPLE_GROUP)); |
| 515 | } |
| 516 | for (offset, person) in self.people.iter().enumerate() { |
| 517 | let index = self.results.len() + offset; |
| 518 | let user_id = person.user_id.clone(); |
| 519 | rows.push( |
| 520 | h_flex() |
| 521 | .id(ElementId::Name(format!("switch-person-{index}").into())) |
| 522 | .mx(px(6.)) |
| 523 | .px(px(6.)) |
| 524 | .h(px(44.)) |
| 525 | .gap(px(10.)) |
| 526 | .items_center() |
| 527 | .rounded(px(6.)) |
| 528 | .cursor_pointer() |
| 529 | .when(index == highlighted, |row| row.bg(theme::popup_hover())) |
| 530 | .hover(|row| row.bg(theme::popup_hover())) |
| 531 | .child(person_avatar( |
| 532 | directory, |
| 533 | Some(&person.user_id), |
| 534 | &person.name, |
| 535 | 28., |
| 536 | )) |
| 537 | .child( |
| 538 | v_flex() |
| 539 | .flex_1() |
| 540 | .min_w_0() |
| 541 | .child( |
| 542 | div() |
| 543 | .truncate() |
| 544 | .text_size(px(14.)) |
| 545 | .text_color(theme::text()) |
| 546 | .child(person.name.clone()), |
| 547 | ) |
| 548 | .when(!person.subtitle.is_empty(), |column| { |
| 549 | column.child( |
| 550 | div() |
| 551 | .truncate() |
| 552 | .text_size(px(12.)) |
| 553 | .text_color(theme::text_muted()) |
| 554 | .child(person.subtitle.clone()), |
| 555 | ) |
| 556 | }), |
| 557 | ) |
| 558 | .child(icon(IconName::User, 14., theme::text_muted())) |
| 559 | .on_click(cx.listener(move |_, _, _, cx| { |
| 560 | cx.emit(SwitcherEvent::PickPerson { |
| 561 | user_id: user_id.clone(), |
| 562 | }); |
| 563 | })) |
| 564 | .into_any_element(), |
| 565 | ); |
| 566 | } |
| 567 | if !self.messages.is_empty() { |
| 568 | rows.push(group_header(MESSAGE_GROUP)); |
| 569 | } |
| 570 | for (offset, message) in self.messages.iter().enumerate() { |
| 571 | let index = self.results.len() + self.people.len() + offset; |
| 572 | let (selection, message_id) = (message.selection.clone(), message.message_id.clone()); |
| 573 | let marks = message |
| 574 | .matches |
| 575 | .iter() |
| 576 | .map(|range| { |
| 577 | ( |
| 578 | range.clone(), |
| 579 | HighlightStyle { |
| 580 | color: Some(theme::text()), |
| 581 | font_weight: Some(FontWeight::BOLD), |
| 582 | ..Default::default() |
| 583 | }, |
| 584 | ) |
| 585 | }) |
| 586 | .collect::<Vec<_>>(); |
| 587 | rows.push( |
| 588 | h_flex() |
| 589 | .id(ElementId::Name(format!("switch-message-{index}").into())) |
| 590 | .mx(px(6.)) |
| 591 | .px(px(6.)) |
| 592 | .h(px(52.)) |
| 593 | .gap(px(10.)) |
| 594 | .items_center() |
| 595 | .rounded(px(6.)) |
| 596 | .cursor_pointer() |
| 597 | .when(index == highlighted, |row| row.bg(theme::popup_hover())) |
| 598 | .hover(|row| row.bg(theme::popup_hover())) |
| 599 | .child(person_avatar(directory, None, &message.author, 28.)) |
| 600 | .child( |
| 601 | v_flex() |
| 602 | .flex_1() |
| 603 | .min_w_0() |
| 604 | .gap(px(1.)) |
| 605 | .child( |
| 606 | h_flex() |
| 607 | .gap(px(6.)) |
| 608 | .items_baseline() |
| 609 | .child( |
| 610 | div() |
| 611 | .flex_none() |
| 612 | .text_size(px(13.)) |
| 613 | .font_weight(FontWeight::SEMIBOLD) |
| 614 | .text_color(theme::text()) |
| 615 | .child(message.author.clone()), |
| 616 | ) |
| 617 | .child( |
| 618 | div() |
| 619 | .min_w_0() |
| 620 | .truncate() |
| 621 | .text_size(px(12.)) |
| 622 | .text_color(theme::text_muted()) |
| 623 | .child(format!("in {}", message.conversation)), |
| 624 | ) |
| 625 | .child(div().flex_1()) |
| 626 | .child( |
| 627 | div() |
| 628 | .flex_none() |
| 629 | .text_size(px(11.)) |
| 630 | .text_color(theme::text_muted()) |
| 631 | .child(message.date.clone()), |
| 632 | ), |
| 633 | ) |
| 634 | .child( |
| 635 | div() |
| 636 | .truncate() |
| 637 | .text_size(px(12.5)) |
| 638 | .text_color(theme::text_soft()) |
| 639 | .child( |
| 640 | StyledText::new(message.snippet.clone()) |
| 641 | .with_highlights(marks), |
| 642 | ), |
| 643 | ), |
| 644 | ) |
| 645 | .on_click(cx.listener(move |_, _, _, cx| { |
| 646 | cx.emit(SwitcherEvent::PickMessage { |
| 647 | selection: selection.clone(), |
| 648 | message_id: message_id.clone(), |
| 649 | }); |
| 650 | })) |
| 651 | .into_any_element(), |
| 652 | ); |
| 653 | } |
| 654 | let footer = h_flex() |
| 655 | .h(px(32.)) |
| 656 | .px(px(12.)) |
| 657 | .gap(px(16.)) |
| 658 | .items_center() |
| 659 | .text_size(px(11.)) |
| 660 | .text_color(theme::text_muted()) |
| 661 | .child("Enter to open") |
| 662 | .child("Arrows to select") |
| 663 | .child("Esc to close"); |
| 664 | div() |
| 665 | .id("switcher-backdrop") |
| 666 | .absolute() |
| 667 | .size_full() |
| 668 | .top_0() |
| 669 | .left_0() |
| 670 | .flex() |
| 671 | .justify_center() |
| 672 | .items_start() |
| 673 | .pt(px(CARD_TOP)) |
| 674 | .on_click(cx.listener(|_, _, _, cx| cx.emit(SwitcherEvent::Close))) |
| 675 | .child( |
| 676 | v_flex() |
| 677 | .id("switcher-card") |
| 678 | .w(px(CARD_WIDTH)) |
| 679 | .max_h(px(640.)) |
| 680 | .rounded(px(10.)) |
| 681 | .flyout_surface() |
| 682 | .overflow_hidden() |
| 683 | .on_click(|_, _, cx| cx.stop_propagation()) |
| 684 | .on_key_down(cx.listener(Self::on_key_down)) |
| 685 | .child( |
| 686 | h_flex() |
| 687 | .p(px(8.)) |
| 688 | .pl(px(12.)) |
| 689 | .gap(px(8.)) |
| 690 | .items_center() |
| 691 | .child(icon(IconName::Search, 14., theme::text_muted())) |
| 692 | .child( |
| 693 | div().flex_1().min_w_0().child( |
| 694 | Input::new(&self.input).appearance(false).bordered(false), |
| 695 | ), |
| 696 | ), |
| 697 | ) |
| 698 | .child( |
| 699 | v_flex() |
| 700 | .id("switcher-rows") |
| 701 | .track_scroll(&self.rows_scroll) |
| 702 | .w_full() |
| 703 | .flex_1() |
| 704 | .min_h_0() |
| 705 | .overflow_y_scroll() |
| 706 | .py_1() |
| 707 | .children(rows), |
| 708 | ) |
| 709 | .child(footer), |
| 710 | ) |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | #[cfg(test)] |
| 715 | mod tests { |
| 716 | use super::{Candidate, CandidateAvatar, clamp_highlight, parse_snippet, row_position}; |
| 717 | use crate::app_state::Selection; |
| 718 | use teams_core::{HIGHLIGHT_END, HIGHLIGHT_START}; |
| 719 | |
| 720 | #[test] |
| 721 | fn snippet_markers_become_ranges() { |
| 722 | let raw = format!("a {HIGHLIGHT_START}Build{HIGHLIGHT_END} 42\nok"); |
| 723 | let (text, matches) = parse_snippet(&raw); |
| 724 | assert_eq!(text, "a Build 42 ok"); |
| 725 | assert_eq!(matches, vec![2..7]); |
| 726 | } |
| 727 | |
| 728 | #[test] |
| 729 | fn unterminated_or_empty_markers_are_ignored() { |
| 730 | let raw = format!("{HIGHLIGHT_START}{HIGHLIGHT_END}x{HIGHLIGHT_START}y"); |
| 731 | assert_eq!(parse_snippet(&raw), ("xy".to_owned(), Vec::new())); |
| 732 | } |
| 733 | |
| 734 | #[test] |
| 735 | fn highlight_stays_inside_the_list() { |
| 736 | assert_eq!(clamp_highlight(0, -1, 3), 0); |
| 737 | assert_eq!(clamp_highlight(2, 1, 3), 2); |
| 738 | assert_eq!(clamp_highlight(1, 1, 3), 2); |
| 739 | assert_eq!(clamp_highlight(0, 1, 0), 0); |
| 740 | } |
| 741 | |
| 742 | fn candidate(selection: Selection) -> Candidate { |
| 743 | Candidate { |
| 744 | title: String::new(), |
| 745 | subtitle: String::new(), |
| 746 | avatar: CandidateAvatar::Team { |
| 747 | name: String::new(), |
| 748 | key: String::new(), |
| 749 | }, |
| 750 | selection, |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | #[test] |
| 755 | fn row_positions_count_the_group_headers() { |
| 756 | let results = [ |
| 757 | candidate(Selection::Chat("a".into())), |
| 758 | candidate(Selection::Chat("b".into())), |
| 759 | candidate(Selection::Channel("c".into())), |
| 760 | ]; |
| 761 | assert_eq!(row_position(&results, 2, 0), 1); |
| 762 | assert_eq!(row_position(&results, 2, 2), 4); |
| 763 | assert_eq!(row_position(&results, 2, 3), 6); |
| 764 | assert_eq!(row_position(&results, 2, 5), 9); |
| 765 | assert_eq!(row_position(&results, 0, 3), 6); |
| 766 | } |
| 767 | } |