| 1 | use std::collections::HashMap; |
| 2 | use std::ops::Range; |
| 3 | use std::time::Duration; |
| 4 | |
| 5 | use gpui_kit::assets::IconName; |
| 6 | use gpui_kit::component::{ |
| 7 | h_flex, |
| 8 | input::{ |
| 9 | Backspace, Enter, Escape, IndentInline, InlineToken, InputContent, InputEvent, MoveDown, |
| 10 | MoveUp, Textarea, TextareaState, |
| 11 | }, |
| 12 | v_flex, |
| 13 | }; |
| 14 | use gpui_kit::prelude::FluentBuilder as _; |
| 15 | use gpui_kit::*; |
| 16 | use teams_core::{MentionCandidate, MentionInput}; |
| 17 | |
| 18 | use super::avatar::{person_avatar, square_avatar}; |
| 19 | use super::emoji_popup::{self, EmojiPopup}; |
| 20 | use super::widgets::{icon, symbol}; |
| 21 | use crate::app_state::AppState; |
| 22 | use crate::emoji; |
| 23 | use crate::runtime; |
| 24 | use crate::theme; |
| 25 | |
| 26 | const MIN_ROWS: usize = 1; |
| 27 | const MAX_ROWS: usize = 8; |
| 28 | const MENTION_DEBOUNCE: Duration = Duration::from_millis(150); |
| 29 | const MENTION_LIMIT: usize = 8; |
| 30 | const MENTION_QUERY_MAX_CHARS: usize = 32; |
| 31 | const MENTION_QUERY_MAX_WORDS: usize = 3; |
| 32 | const POPUP_WIDTH: f32 = 380.; |
| 33 | const POPUP_ROW_HEIGHT: f32 = 44.; |
| 34 | |
| 35 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 36 | pub struct ReplyPreview { |
| 37 | pub message_id: String, |
| 38 | pub author: String, |
| 39 | pub excerpt: String, |
| 40 | } |
| 41 | |
| 42 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 43 | pub struct Outgoing { |
| 44 | pub text: String, |
| 45 | pub mentions: Vec<MentionInput>, |
| 46 | pub reply: Option<ReplyPreview>, |
| 47 | } |
| 48 | |
| 49 | pub enum ComposerEvent { |
| 50 | Submit(Outgoing), |
| 51 | } |
| 52 | |
| 53 | struct MentionPopup { |
| 54 | range: Range<usize>, |
| 55 | query: String, |
| 56 | candidates: Vec<MentionCandidate>, |
| 57 | highlighted: usize, |
| 58 | } |
| 59 | |
| 60 | struct Conversion { |
| 61 | value: String, |
| 62 | cursor: usize, |
| 63 | range: Range<usize>, |
| 64 | original: String, |
| 65 | } |
| 66 | |
| 67 | pub struct Composer { |
| 68 | app: Entity<AppState>, |
| 69 | input: Entity<TextareaState>, |
| 70 | conversation_id: Option<String>, |
| 71 | reply: Option<ReplyPreview>, |
| 72 | mention_inputs: HashMap<String, MentionInput>, |
| 73 | mention_counter: usize, |
| 74 | popup: Option<MentionPopup>, |
| 75 | lookup: Option<Task<()>>, |
| 76 | emoji_popup: Option<EmojiPopup>, |
| 77 | emoji_dismissed_at: Option<usize>, |
| 78 | recent_emoji: emoji::Recent, |
| 79 | conversion: Option<Conversion>, |
| 80 | undone_value: Option<String>, |
| 81 | previous_value: String, |
| 82 | _subscription: Subscription, |
| 83 | } |
| 84 | |
| 85 | impl EventEmitter<ComposerEvent> for Composer {} |
| 86 | |
| 87 | /// Enter sends, Shift+Enter is a newline (handled by the input itself). Blank text never sends. |
| 88 | pub fn submitted_text(event: &InputEvent, value: &str) -> Option<String> { |
| 89 | match event { |
| 90 | InputEvent::PressEnter { shift: false, .. } => { |
| 91 | let trimmed = value.trim(); |
| 92 | (!trimmed.is_empty()).then(|| trimmed.to_owned()) |
| 93 | } |
| 94 | _ => None, |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | pub fn active_mention( |
| 99 | text: &str, |
| 100 | cursor: usize, |
| 101 | tokens: &[Range<usize>], |
| 102 | ) -> Option<(Range<usize>, String)> { |
| 103 | if cursor > text.len() || !text.is_char_boundary(cursor) { |
| 104 | return None; |
| 105 | } |
| 106 | let before = &text[..cursor]; |
| 107 | let at = before.rfind('@')?; |
| 108 | if before[..at] |
| 109 | .chars() |
| 110 | .next_back() |
| 111 | .is_some_and(|previous| !previous.is_whitespace()) |
| 112 | { |
| 113 | return None; |
| 114 | } |
| 115 | let query = &before[at + 1..]; |
| 116 | let rejected = query.contains('\n') |
| 117 | || query.starts_with(char::is_whitespace) |
| 118 | || query.ends_with(char::is_whitespace) |
| 119 | || query.chars().count() > MENTION_QUERY_MAX_CHARS |
| 120 | || query.split_whitespace().count() > MENTION_QUERY_MAX_WORDS |
| 121 | || tokens |
| 122 | .iter() |
| 123 | .any(|token| token.start < cursor && at < token.end); |
| 124 | (!rejected).then(|| (at..cursor, query.to_owned())) |
| 125 | } |
| 126 | |
| 127 | fn candidate_subtitle(candidate: &MentionCandidate) -> String { |
| 128 | match candidate { |
| 129 | MentionCandidate::Person(person) => person |
| 130 | .job_title |
| 131 | .clone() |
| 132 | .filter(|title| !title.trim().is_empty()) |
| 133 | .or_else(|| person.mail.clone()) |
| 134 | .unwrap_or_default(), |
| 135 | MentionCandidate::Channel { .. } => "Channel".to_owned(), |
| 136 | MentionCandidate::Team { .. } => "Team".to_owned(), |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | fn candidate_key(candidate: &MentionCandidate) -> &str { |
| 141 | match candidate { |
| 142 | MentionCandidate::Person(person) => &person.user_id, |
| 143 | MentionCandidate::Channel { channel_id, .. } => channel_id, |
| 144 | MentionCandidate::Team { team_id, .. } => team_id, |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | impl Composer { |
| 149 | pub fn new(app: Entity<AppState>, window: &mut Window, cx: &mut Context<Self>) -> Self { |
| 150 | let input = cx.new(|cx| { |
| 151 | TextareaState::new(window, cx) |
| 152 | .auto_grow(MIN_ROWS, MAX_ROWS) |
| 153 | .submit_on_enter(true) |
| 154 | .placeholder("Type a message") |
| 155 | }); |
| 156 | let subscription = cx.subscribe_in(&input, window, Self::on_input_event); |
| 157 | let recent_emoji = emoji::Recent::load(&app.read(cx).store); |
| 158 | Composer { |
| 159 | app, |
| 160 | input, |
| 161 | conversation_id: None, |
| 162 | reply: None, |
| 163 | mention_inputs: HashMap::new(), |
| 164 | mention_counter: 0, |
| 165 | popup: None, |
| 166 | lookup: None, |
| 167 | emoji_popup: None, |
| 168 | emoji_dismissed_at: None, |
| 169 | recent_emoji, |
| 170 | conversion: None, |
| 171 | undone_value: None, |
| 172 | previous_value: String::new(), |
| 173 | _subscription: subscription, |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | fn on_input_event( |
| 178 | &mut self, |
| 179 | input: &Entity<TextareaState>, |
| 180 | event: &InputEvent, |
| 181 | window: &mut Window, |
| 182 | cx: &mut Context<Self>, |
| 183 | ) { |
| 184 | cx.notify(); |
| 185 | if matches!(event, InputEvent::Change) { |
| 186 | self.convert_typed(window, cx); |
| 187 | self.update_emoji(cx); |
| 188 | self.update_mention(cx); |
| 189 | } |
| 190 | let value = input.read(cx).value(); |
| 191 | if submitted_text(event, &value).is_some() { |
| 192 | self.submit_current(window, cx); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | fn update_mention(&mut self, cx: &mut Context<Self>) { |
| 197 | let state = self.input.read(cx); |
| 198 | let tokens: Vec<Range<usize>> = state.tokens().iter().map(|span| span.range()).collect(); |
| 199 | let found = active_mention(&state.value(), state.cursor(), &tokens); |
| 200 | let Some((range, query)) = found else { |
| 201 | self.close_mention_popup(); |
| 202 | return; |
| 203 | }; |
| 204 | let unchanged = self |
| 205 | .popup |
| 206 | .as_ref() |
| 207 | .is_some_and(|popup| popup.query == query && popup.range == range); |
| 208 | if unchanged { |
| 209 | return; |
| 210 | } |
| 211 | let previous = self.popup.take(); |
| 212 | self.popup = Some(MentionPopup { |
| 213 | range, |
| 214 | query: query.clone(), |
| 215 | candidates: previous.map(|popup| popup.candidates).unwrap_or_default(), |
| 216 | highlighted: 0, |
| 217 | }); |
| 218 | self.request_candidates(query, cx); |
| 219 | } |
| 220 | |
| 221 | fn close_popup(&mut self) { |
| 222 | self.close_mention_popup(); |
| 223 | self.emoji_popup = None; |
| 224 | } |
| 225 | |
| 226 | fn close_mention_popup(&mut self) { |
| 227 | self.popup = None; |
| 228 | self.lookup = None; |
| 229 | } |
| 230 | |
| 231 | fn convert_typed(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 232 | let state = self.input.read(cx); |
| 233 | let (value, cursor) = (state.value().to_string(), state.cursor()); |
| 234 | let previous = std::mem::replace(&mut self.previous_value, value.clone()); |
| 235 | let found = match emoji::typed_char(&previous, &value, cursor) { |
| 236 | Some(':') => emoji::closing_code(&value, cursor) |
| 237 | .and_then(|(range, code)| Some((range, emoji::lookup(code)?))), |
| 238 | Some(' ') => emoji::smiley_before_space(&value, cursor), |
| 239 | _ => None, |
| 240 | }; |
| 241 | let Some((range, glyph)) = found else { |
| 242 | return; |
| 243 | }; |
| 244 | let original = value[range.clone()].to_owned(); |
| 245 | let cursor = self.replace_text(range.clone(), glyph, cursor, window, cx); |
| 246 | self.previous_value = self.input.read(cx).value().to_string(); |
| 247 | self.conversion = Some(Conversion { |
| 248 | value: self.previous_value.clone(), |
| 249 | cursor, |
| 250 | range: range.start..range.start + glyph.len(), |
| 251 | original, |
| 252 | }); |
| 253 | self.remember_emoji(glyph, cx); |
| 254 | } |
| 255 | |
| 256 | /// Returns the cursor, kept at the same spot relative to the text after `range`. |
| 257 | fn replace_text( |
| 258 | &mut self, |
| 259 | range: Range<usize>, |
| 260 | text: &str, |
| 261 | cursor: usize, |
| 262 | window: &mut Window, |
| 263 | cx: &mut Context<Self>, |
| 264 | ) -> usize { |
| 265 | let cursor = cursor + text.len() - range.len(); |
| 266 | self.input.update(cx, |state, cx| { |
| 267 | state.set_selected_range(range, cx); |
| 268 | state.replace(text.to_owned(), window, cx); |
| 269 | state.set_selected_range(cursor..cursor, cx); |
| 270 | }); |
| 271 | cursor |
| 272 | } |
| 273 | |
| 274 | fn undo_conversion(&mut self, window: &mut Window, cx: &mut Context<Self>) -> bool { |
| 275 | let state = self.input.read(cx); |
| 276 | let unchanged = self.conversion.as_ref().is_some_and(|conversion| { |
| 277 | conversion.value == state.value().as_ref() && conversion.cursor == state.cursor() |
| 278 | }); |
| 279 | let Some(conversion) = self.conversion.take().filter(|_| unchanged) else { |
| 280 | return false; |
| 281 | }; |
| 282 | self.replace_text( |
| 283 | conversion.range, |
| 284 | &conversion.original, |
| 285 | conversion.cursor, |
| 286 | window, |
| 287 | cx, |
| 288 | ); |
| 289 | self.undone_value = Some(self.input.read(cx).value().to_string()); |
| 290 | self.update_emoji(cx); |
| 291 | true |
| 292 | } |
| 293 | |
| 294 | fn remember_emoji(&mut self, glyph: &str, cx: &App) { |
| 295 | self.recent_emoji.push(glyph); |
| 296 | self.recent_emoji.save(&self.app.read(cx).store); |
| 297 | } |
| 298 | |
| 299 | fn update_emoji(&mut self, cx: &mut Context<Self>) { |
| 300 | let state = self.input.read(cx); |
| 301 | let found = emoji::active_query(&state.value(), state.cursor()); |
| 302 | if found.as_ref().map(|(range, _)| range.start) != self.emoji_dismissed_at { |
| 303 | self.emoji_dismissed_at = None; |
| 304 | } |
| 305 | let found = found.filter(|_| self.emoji_dismissed_at.is_none()); |
| 306 | let Some((range, query)) = found else { |
| 307 | self.emoji_popup = None; |
| 308 | return; |
| 309 | }; |
| 310 | let unchanged = self |
| 311 | .emoji_popup |
| 312 | .as_ref() |
| 313 | .is_some_and(|popup| popup.query == query && popup.range == range); |
| 314 | if unchanged { |
| 315 | return; |
| 316 | } |
| 317 | let matches = emoji::search(&query, self.recent_emoji.glyphs(), emoji_popup::LIMIT); |
| 318 | self.emoji_popup = (!matches.is_empty()).then(|| EmojiPopup::new(range, query, matches)); |
| 319 | } |
| 320 | |
| 321 | fn accept_emoji(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 322 | let Some(popup) = self.emoji_popup.take() else { |
| 323 | return; |
| 324 | }; |
| 325 | let Some(glyph) = popup.selected().map(|found| found.glyph) else { |
| 326 | return; |
| 327 | }; |
| 328 | let cursor = popup.range.end; |
| 329 | self.replace_text(popup.range, glyph, cursor, window, cx); |
| 330 | self.remember_emoji(glyph, cx); |
| 331 | cx.notify(); |
| 332 | } |
| 333 | |
| 334 | fn request_candidates(&mut self, query: String, cx: &mut Context<Self>) { |
| 335 | let Some(conversation_id) = self.conversation_id.clone() else { |
| 336 | return; |
| 337 | }; |
| 338 | let (demo, engine) = { |
| 339 | let state = self.app.read(cx); |
| 340 | (state.mode.demo, state.engine.clone()) |
| 341 | }; |
| 342 | if demo { |
| 343 | let candidates = crate::demo::mention_candidates(&conversation_id, &query); |
| 344 | self.apply_candidates(&query, candidates, cx); |
| 345 | return; |
| 346 | } |
| 347 | let Some(engine) = engine else { |
| 348 | return; |
| 349 | }; |
| 350 | self.lookup = Some(cx.spawn(async move |this, cx| { |
| 351 | cx.background_executor().timer(MENTION_DEBOUNCE).await; |
| 352 | let task_query = query.clone(); |
| 353 | let receiver = runtime::spawn(async move { |
| 354 | engine |
| 355 | .mention_candidates(&conversation_id, &task_query, MENTION_LIMIT) |
| 356 | .await |
| 357 | }); |
| 358 | if let Ok(Ok(candidates)) = receiver.await { |
| 359 | this.update(cx, |this, cx| this.apply_candidates(&query, candidates, cx)) |
| 360 | .ok(); |
| 361 | } |
| 362 | })); |
| 363 | } |
| 364 | |
| 365 | fn apply_candidates( |
| 366 | &mut self, |
| 367 | query: &str, |
| 368 | candidates: Vec<MentionCandidate>, |
| 369 | cx: &mut Context<Self>, |
| 370 | ) { |
| 371 | let Some(popup) = self.popup.as_mut().filter(|popup| popup.query == query) else { |
| 372 | return; |
| 373 | }; |
| 374 | popup.candidates = candidates; |
| 375 | popup.highlighted = 0; |
| 376 | cx.notify(); |
| 377 | } |
| 378 | |
| 379 | fn popup_is_open(&self) -> bool { |
| 380 | self.emoji_popup.is_some() |
| 381 | || self |
| 382 | .popup |
| 383 | .as_ref() |
| 384 | .is_some_and(|popup| !popup.candidates.is_empty()) |
| 385 | } |
| 386 | |
| 387 | fn move_highlight(&mut self, delta: isize, cx: &mut Context<Self>) { |
| 388 | if let Some(popup) = self.emoji_popup.as_mut() { |
| 389 | popup.move_highlight(delta); |
| 390 | cx.notify(); |
| 391 | } else if let Some(popup) = self.popup.as_mut() { |
| 392 | let last = popup.candidates.len().saturating_sub(1) as isize; |
| 393 | popup.highlighted = (popup.highlighted as isize + delta).clamp(0, last) as usize; |
| 394 | cx.notify(); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | fn accept_popup(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 399 | if self.emoji_popup.is_some() { |
| 400 | self.accept_emoji(window, cx); |
| 401 | } else { |
| 402 | self.accept_mention(window, cx); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | fn accept_mention(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 407 | let Some(popup) = self.popup.take() else { |
| 408 | return; |
| 409 | }; |
| 410 | self.lookup = None; |
| 411 | let Some(candidate) = popup.candidates.get(popup.highlighted) else { |
| 412 | return; |
| 413 | }; |
| 414 | self.mention_counter += 1; |
| 415 | let token_id = format!( |
| 416 | "mention-{}-{}", |
| 417 | self.mention_counter, |
| 418 | candidate_key(candidate) |
| 419 | ); |
| 420 | let label = format!("@{}", candidate.display_name()); |
| 421 | self.mention_inputs |
| 422 | .insert(token_id.clone(), candidate.to_mention()); |
| 423 | self.input.update(cx, |state, cx| { |
| 424 | let token = InlineToken::new(token_id, label); |
| 425 | if state |
| 426 | .replace_range_with_token(popup.range, token, window, cx) |
| 427 | .is_ok() |
| 428 | { |
| 429 | state.insert(" ", window, cx); |
| 430 | } |
| 431 | }); |
| 432 | self.close_popup(); |
| 433 | cx.notify(); |
| 434 | } |
| 435 | |
| 436 | pub fn set_conversation( |
| 437 | &mut self, |
| 438 | conversation_id: &str, |
| 439 | name: &str, |
| 440 | window: &mut Window, |
| 441 | cx: &mut Context<Self>, |
| 442 | ) { |
| 443 | let placeholder = format!("Message {name}"); |
| 444 | self.input.update(cx, |state, cx| { |
| 445 | state.set_placeholder(placeholder, window, cx) |
| 446 | }); |
| 447 | if self.conversation_id.as_deref() != Some(conversation_id) { |
| 448 | self.conversation_id = Some(conversation_id.to_owned()); |
| 449 | self.reply = None; |
| 450 | self.mention_inputs.clear(); |
| 451 | self.close_popup(); |
| 452 | cx.notify(); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | pub fn set_reply(&mut self, reply: Option<ReplyPreview>, cx: &mut Context<Self>) { |
| 457 | self.reply = reply; |
| 458 | cx.notify(); |
| 459 | } |
| 460 | |
| 461 | fn outgoing(&self, cx: &App) -> Option<Outgoing> { |
| 462 | let state = self.input.read(cx); |
| 463 | let mut text = state.value().trim().to_owned(); |
| 464 | if text.is_empty() { |
| 465 | return None; |
| 466 | } |
| 467 | if self.undone_value.as_deref() != Some(state.value().as_ref()) |
| 468 | && let Some((range, glyph)) = emoji::trailing_smiley(&text) |
| 469 | { |
| 470 | text.replace_range(range, glyph); |
| 471 | } |
| 472 | let mentions = state |
| 473 | .tokens() |
| 474 | .iter() |
| 475 | .filter_map(|span| self.mention_inputs.get(span.token().id().as_ref())) |
| 476 | .cloned() |
| 477 | .collect(); |
| 478 | Some(Outgoing { |
| 479 | text, |
| 480 | mentions, |
| 481 | reply: self.reply.clone(), |
| 482 | }) |
| 483 | } |
| 484 | |
| 485 | pub fn submit_current(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 486 | let Some(outgoing) = self.outgoing(cx) else { |
| 487 | return; |
| 488 | }; |
| 489 | self.input |
| 490 | .update(cx, |state, cx| state.set_value("", window, cx)); |
| 491 | self.mention_inputs.clear(); |
| 492 | self.reply = None; |
| 493 | self.close_popup(); |
| 494 | cx.emit(ComposerEvent::Submit(outgoing)); |
| 495 | cx.notify(); |
| 496 | } |
| 497 | |
| 498 | pub fn restore(&mut self, outgoing: &Outgoing, window: &mut Window, cx: &mut Context<Self>) { |
| 499 | self.mention_inputs.clear(); |
| 500 | let mut content = InputContent::new(outgoing.text.clone()); |
| 501 | let mut cursor = 0; |
| 502 | for mention in &outgoing.mentions { |
| 503 | let needle = format!("@{}", mention.text); |
| 504 | let Some(offset) = outgoing.text[cursor..].find(&needle) else { |
| 505 | continue; |
| 506 | }; |
| 507 | let range = cursor + offset..cursor + offset + needle.len(); |
| 508 | self.mention_counter += 1; |
| 509 | let token_id = format!("mention-{}", self.mention_counter); |
| 510 | let token = InlineToken::new(token_id.clone(), needle); |
| 511 | if let Ok(next) = content.clone().with_token(range.clone(), token) { |
| 512 | content = next; |
| 513 | self.mention_inputs.insert(token_id, mention.clone()); |
| 514 | cursor = range.end; |
| 515 | } |
| 516 | } |
| 517 | let end = outgoing.text.len(); |
| 518 | self.input.update(cx, |state, cx| { |
| 519 | state.set_value(content, window, cx); |
| 520 | state.set_selected_range(end..end, cx); |
| 521 | }); |
| 522 | self.reply = outgoing.reply.clone(); |
| 523 | self.close_popup(); |
| 524 | cx.notify(); |
| 525 | } |
| 526 | |
| 527 | pub fn set_text(&mut self, text: &str, window: &mut Window, cx: &mut Context<Self>) { |
| 528 | let text = text.to_owned(); |
| 529 | let end = text.len(); |
| 530 | self.input.update(cx, |state, cx| { |
| 531 | state.set_value(text, window, cx); |
| 532 | state.set_selected_range(end..end, cx); |
| 533 | }); |
| 534 | self.mention_inputs.clear(); |
| 535 | self.update_emoji(cx); |
| 536 | self.update_mention(cx); |
| 537 | cx.notify(); |
| 538 | } |
| 539 | |
| 540 | pub fn is_empty(&self, cx: &App) -> bool { |
| 541 | self.input.read(cx).value().trim().is_empty() |
| 542 | } |
| 543 | |
| 544 | pub fn focus(&self, window: &mut Window, cx: &mut Context<Self>) { |
| 545 | self.input.update(cx, |state, cx| state.focus(window, cx)); |
| 546 | } |
| 547 | |
| 548 | fn render_popup(&self, cx: &mut Context<Self>) -> Option<AnyElement> { |
| 549 | let popup = self |
| 550 | .popup |
| 551 | .as_ref() |
| 552 | .filter(|popup| !popup.candidates.is_empty())?; |
| 553 | let directory = &self.app.read(cx).directory; |
| 554 | let rows = popup |
| 555 | .candidates |
| 556 | .iter() |
| 557 | .enumerate() |
| 558 | .map(|(index, candidate)| { |
| 559 | let name = candidate.display_name().to_owned(); |
| 560 | let avatar = match candidate { |
| 561 | MentionCandidate::Person(person) => { |
| 562 | person_avatar(directory, Some(&person.user_id), &name, 28.) |
| 563 | } |
| 564 | other => square_avatar(&name, candidate_key(other), 28., 7.).into_any_element(), |
| 565 | }; |
| 566 | let subtitle = candidate_subtitle(candidate); |
| 567 | h_flex() |
| 568 | .id(ElementId::Name(format!("mention-row-{index}").into())) |
| 569 | .mx(px(6.)) |
| 570 | .px(px(6.)) |
| 571 | .h(px(POPUP_ROW_HEIGHT)) |
| 572 | .gap(px(10.)) |
| 573 | .items_center() |
| 574 | .rounded(px(6.)) |
| 575 | .cursor_pointer() |
| 576 | .when(index == popup.highlighted, |row| { |
| 577 | row.bg(theme::surface_raised()) |
| 578 | }) |
| 579 | .hover(|row| row.bg(theme::row_hover())) |
| 580 | .child(avatar) |
| 581 | .child( |
| 582 | v_flex() |
| 583 | .flex_1() |
| 584 | .min_w_0() |
| 585 | .child( |
| 586 | div() |
| 587 | .truncate() |
| 588 | .text_size(px(13.5)) |
| 589 | .text_color(theme::text()) |
| 590 | .child(name), |
| 591 | ) |
| 592 | .when(!subtitle.is_empty(), |column| { |
| 593 | column.child( |
| 594 | div() |
| 595 | .truncate() |
| 596 | .text_size(px(12.)) |
| 597 | .text_color(theme::text_muted()) |
| 598 | .child(subtitle), |
| 599 | ) |
| 600 | }), |
| 601 | ) |
| 602 | .on_click(cx.listener(move |this, _, window, cx| { |
| 603 | if let Some(popup) = this.popup.as_mut() { |
| 604 | popup.highlighted = index; |
| 605 | } |
| 606 | this.accept_mention(window, cx); |
| 607 | })) |
| 608 | }); |
| 609 | Some( |
| 610 | v_flex() |
| 611 | .id("mention-popup") |
| 612 | .absolute() |
| 613 | .bottom(relative(1.)) |
| 614 | .left_0() |
| 615 | .mb(px(6.)) |
| 616 | .w(px(POPUP_WIDTH)) |
| 617 | .py(px(6.)) |
| 618 | .rounded(px(10.)) |
| 619 | .border_1() |
| 620 | .border_color(theme::border_strong()) |
| 621 | .bg(theme::surface()) |
| 622 | .shadow_lg() |
| 623 | .occlude() |
| 624 | .children(rows) |
| 625 | .into_any_element(), |
| 626 | ) |
| 627 | } |
| 628 | |
| 629 | fn render_emoji_popup( |
| 630 | &self, |
| 631 | window: &mut Window, |
| 632 | cx: &mut Context<Self>, |
| 633 | ) -> Option<AnyElement> { |
| 634 | let popup = self.emoji_popup.as_ref()?; |
| 635 | let colon = popup.range.start..popup.range.start + 1; |
| 636 | let anchor = self |
| 637 | .input |
| 638 | .read(cx) |
| 639 | .range_to_bounds(&colon) |
| 640 | .map(|bounds| bounds.origin); |
| 641 | if anchor.is_none() { |
| 642 | window.request_animation_frame(); |
| 643 | } |
| 644 | let composer = cx.entity().downgrade(); |
| 645 | Some(emoji_popup::render( |
| 646 | popup, |
| 647 | anchor, |
| 648 | move |index, window, cx| { |
| 649 | composer |
| 650 | .update(cx, |this, cx| { |
| 651 | if let Some(popup) = this.emoji_popup.as_mut() { |
| 652 | popup.highlighted = index; |
| 653 | } |
| 654 | this.accept_emoji(window, cx); |
| 655 | }) |
| 656 | .ok(); |
| 657 | }, |
| 658 | )) |
| 659 | } |
| 660 | |
| 661 | fn render_reply_strip(&self, cx: &mut Context<Self>) -> Option<Div> { |
| 662 | let reply = self.reply.as_ref()?; |
| 663 | Some( |
| 664 | h_flex() |
| 665 | .w_full() |
| 666 | .mb(px(6.)) |
| 667 | .gap(px(10.)) |
| 668 | .items_center() |
| 669 | .pl(px(10.)) |
| 670 | .pr(px(4.)) |
| 671 | .py(px(6.)) |
| 672 | .rounded(px(8.)) |
| 673 | .bg(theme::surface()) |
| 674 | .border_1() |
| 675 | .border_color(theme::border()) |
| 676 | .child(icon(IconName::Undo2, 14., theme::accent_text())) |
| 677 | .child( |
| 678 | v_flex() |
| 679 | .flex_1() |
| 680 | .min_w_0() |
| 681 | .child( |
| 682 | div() |
| 683 | .truncate() |
| 684 | .text_size(px(12.)) |
| 685 | .font_weight(FontWeight::SEMIBOLD) |
| 686 | .text_color(theme::accent_text()) |
| 687 | .child(format!("Replying to {}", reply.author)), |
| 688 | ) |
| 689 | .child( |
| 690 | div() |
| 691 | .truncate() |
| 692 | .text_size(px(12.)) |
| 693 | .text_color(theme::text_muted()) |
| 694 | .child(reply.excerpt.clone()), |
| 695 | ), |
| 696 | ) |
| 697 | .child( |
| 698 | div() |
| 699 | .id("reply-close") |
| 700 | .size(px(24.)) |
| 701 | .flex() |
| 702 | .items_center() |
| 703 | .justify_center() |
| 704 | .rounded(px(6.)) |
| 705 | .cursor_pointer() |
| 706 | .hover(|button| button.bg(theme::row_hover())) |
| 707 | .child(icon(IconName::Close, 14., theme::text_muted())) |
| 708 | .on_click(cx.listener(|this, _, _, cx| this.set_reply(None, cx))), |
| 709 | ), |
| 710 | ) |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | impl Render for Composer { |
| 715 | fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 716 | let focused = self.input.focus_handle(cx).contains_focused(window, cx); |
| 717 | let empty = self.is_empty(cx); |
| 718 | let send = div() |
| 719 | .id("composer-send") |
| 720 | .size(px(32.)) |
| 721 | .flex_none() |
| 722 | .flex() |
| 723 | .items_center() |
| 724 | .justify_center() |
| 725 | .rounded(px(8.)) |
| 726 | .child(symbol("keyboard_return", 20., white())) |
| 727 | .when(empty, |button| button.opacity(0.4)) |
| 728 | .when(!empty, |button| { |
| 729 | button |
| 730 | .cursor_pointer() |
| 731 | .hover(|button| button.bg(white().opacity(0.1))) |
| 732 | .on_click(cx.listener(|this, _, window, cx| this.submit_current(window, cx))) |
| 733 | }); |
| 734 | let input = Textarea::new(&self.input) |
| 735 | .appearance(false) |
| 736 | .bordered(false) |
| 737 | .token(|context, _, _| { |
| 738 | div() |
| 739 | .h(context.line_height()) |
| 740 | .px(px(5.)) |
| 741 | .rounded(px(5.)) |
| 742 | .bg(if context.is_selected() { |
| 743 | theme::accent().opacity(0.4) |
| 744 | } else { |
| 745 | theme::mention_background(false) |
| 746 | }) |
| 747 | .text_color(theme::mention_text()) |
| 748 | .font_weight(FontWeight::SEMIBOLD) |
| 749 | .child(context.token().label().clone()) |
| 750 | }); |
| 751 | v_flex() |
| 752 | .w_full() |
| 753 | .flex_none() |
| 754 | .px(px(24.)) |
| 755 | .pt(px(8.)) |
| 756 | .pb(px(12.)) |
| 757 | .capture_action(cx.listener(|this, _: &MoveUp, _, cx| { |
| 758 | if this.popup_is_open() { |
| 759 | this.move_highlight(-1, cx); |
| 760 | cx.stop_propagation(); |
| 761 | } |
| 762 | })) |
| 763 | .capture_action(cx.listener(|this, _: &MoveDown, _, cx| { |
| 764 | if this.popup_is_open() { |
| 765 | this.move_highlight(1, cx); |
| 766 | cx.stop_propagation(); |
| 767 | } |
| 768 | })) |
| 769 | .capture_action(cx.listener(|this, _: &Enter, window, cx| { |
| 770 | if this.popup_is_open() { |
| 771 | this.accept_popup(window, cx); |
| 772 | cx.stop_propagation(); |
| 773 | } |
| 774 | })) |
| 775 | .capture_action(cx.listener(|this, _: &IndentInline, window, cx| { |
| 776 | if this.popup_is_open() { |
| 777 | this.accept_popup(window, cx); |
| 778 | cx.stop_propagation(); |
| 779 | } |
| 780 | })) |
| 781 | .capture_action(cx.listener(|this, _: &Backspace, window, cx| { |
| 782 | if this.undo_conversion(window, cx) { |
| 783 | cx.stop_propagation(); |
| 784 | } |
| 785 | })) |
| 786 | .capture_action(cx.listener(|this, _: &Escape, _, cx| { |
| 787 | if let Some(popup) = &this.emoji_popup { |
| 788 | this.emoji_dismissed_at = Some(popup.range.start); |
| 789 | } |
| 790 | if this.popup.is_some() || this.emoji_popup.is_some() { |
| 791 | this.close_popup(); |
| 792 | cx.notify(); |
| 793 | cx.stop_propagation(); |
| 794 | } |
| 795 | })) |
| 796 | .children(self.render_reply_strip(cx)) |
| 797 | .child( |
| 798 | div() |
| 799 | .relative() |
| 800 | .w_full() |
| 801 | .children(self.render_popup(cx)) |
| 802 | .children(self.render_emoji_popup(window, cx)) |
| 803 | .child( |
| 804 | h_flex() |
| 805 | .w_full() |
| 806 | .items_end() |
| 807 | .gap(px(6.)) |
| 808 | .py(px(6.)) |
| 809 | .pl(px(12.)) |
| 810 | .pr(px(6.)) |
| 811 | .rounded(px(10.)) |
| 812 | .bg(theme::surface()) |
| 813 | .border_1() |
| 814 | .border_color(if focused { |
| 815 | theme::accent() |
| 816 | } else { |
| 817 | theme::border_strong() |
| 818 | }) |
| 819 | .child(div().flex_1().min_w_0().child(input)) |
| 820 | .child(send), |
| 821 | ), |
| 822 | ) |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | #[cfg(test)] |
| 827 | mod tests { |
| 828 | use super::{active_mention, submitted_text}; |
| 829 | use gpui_kit::component::input::InputEvent; |
| 830 | |
| 831 | fn press(shift: bool) -> InputEvent { |
| 832 | InputEvent::PressEnter { |
| 833 | secondary: false, |
| 834 | shift, |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | #[test] |
| 839 | fn enter_sends_trimmed_text() { |
| 840 | assert_eq!( |
| 841 | submitted_text(&press(false), " hello \n"), |
| 842 | Some("hello".into()) |
| 843 | ); |
| 844 | } |
| 845 | |
| 846 | #[test] |
| 847 | fn shift_enter_never_sends() { |
| 848 | assert_eq!(submitted_text(&press(true), "hello"), None); |
| 849 | } |
| 850 | |
| 851 | #[test] |
| 852 | fn blank_text_never_sends() { |
| 853 | assert_eq!(submitted_text(&press(false), " \n "), None); |
| 854 | } |
| 855 | |
| 856 | #[test] |
| 857 | fn other_events_never_send() { |
| 858 | assert_eq!(submitted_text(&InputEvent::Change, "hello"), None); |
| 859 | } |
| 860 | |
| 861 | #[test] |
| 862 | fn at_sign_opens_a_mention_query() { |
| 863 | assert_eq!(active_mention("hi @Ma", 6, &[]), Some((3..6, "Ma".into()))); |
| 864 | assert_eq!(active_mention("@", 1, &[]), Some((0..1, String::new()))); |
| 865 | } |
| 866 | |
| 867 | #[test] |
| 868 | fn names_with_a_space_keep_the_query_open() { |
| 869 | assert_eq!( |
| 870 | active_mention("@Mara Lin", 9, &[]), |
| 871 | Some((0..9, "Mara Lin".into())) |
| 872 | ); |
| 873 | } |
| 874 | |
| 875 | #[test] |
| 876 | fn mail_addresses_and_closed_queries_do_not_open() { |
| 877 | assert_eq!(active_mention("a@b", 3, &[]), None); |
| 878 | assert_eq!(active_mention("@Mara ", 6, &[]), None); |
| 879 | assert_eq!(active_mention("@ x", 3, &[]), None); |
| 880 | assert_eq!(active_mention("@Ma\nrest", 8, &[]), None); |
| 881 | } |
| 882 | |
| 883 | #[test] |
| 884 | fn text_inside_a_token_does_not_reopen() { |
| 885 | let token = 0..15; |
| 886 | assert_eq!( |
| 887 | active_mention("@Mara Lindqvist", 15, std::slice::from_ref(&token)), |
| 888 | None |
| 889 | ); |
| 890 | assert_eq!( |
| 891 | active_mention("@Mara Lindqvist @Pr", 19, std::slice::from_ref(&token)), |
| 892 | Some((16..19, "Pr".into())) |
| 893 | ); |
| 894 | } |
| 895 | } |