| 1 | use std::cell::Cell; |
| 2 | use std::collections::{HashMap, HashSet}; |
| 3 | use std::ops::Range; |
| 4 | use std::path::PathBuf; |
| 5 | use std::rc::Rc; |
| 6 | use std::time::{Duration, Instant}; |
| 7 | |
| 8 | use chrono::{DateTime, Utc}; |
| 9 | use gpui_kit::assets::IconName; |
| 10 | use gpui_kit::component::{ |
| 11 | ActiveTheme as _, Sizable as _, |
| 12 | button::{Button, ButtonCustomVariant, ButtonVariants as _}, |
| 13 | h_flex, |
| 14 | input::{ |
| 15 | Backspace, Copy, Cut, Delete, Enter, Escape, IndentInline, InlineToken, InputContent, |
| 16 | InputEvent, InputState, MoveDown, MoveRight, MoveUp, OutdentInline, |
| 17 | RangeDecorationCollection, Redo, TextDecorationCollection, Textarea, TextareaMode, |
| 18 | TextareaState, Undo, |
| 19 | }, |
| 20 | popover::Popover, |
| 21 | tooltip::Tooltip, |
| 22 | v_flex, |
| 23 | }; |
| 24 | use gpui_kit::prelude::FluentBuilder as _; |
| 25 | use gpui_kit::*; |
| 26 | use teams_core::{ |
| 27 | Draft, DraftLine, Edit, FileReference, FormatState, LineKind, LinkPreview, MarkKind, |
| 28 | MentionCandidate, MentionInput, OBJECT_MARK, SizeStep, TABLE_MARK, TableEdit, TypingStyle, |
| 29 | UploadedFile, changed_span, has_markdown, link_url, map_offset, reverse_edits, |
| 30 | }; |
| 31 | |
| 32 | pub(crate) mod delivery; |
| 33 | mod drafts; |
| 34 | mod schedule; |
| 35 | mod table; |
| 36 | |
| 37 | use super::actions::{ |
| 38 | COMPOSER_KEY_CONTEXT, DeleteTable, DeleteTableColumn, DeleteTableRow, EditLink, |
| 39 | InsertTableColumnLeft, InsertTableColumnRight, InsertTableLine, InsertTableRowAbove, |
| 40 | InsertTableRowBelow, OpenSwitcher, PastePlainText, RemoveTableLine, ScheduleSend, ToggleBold, |
| 41 | ToggleCode, ToggleFormatBar, ToggleItalic, ToggleStrike, ToggleSubscript, ToggleSuperscript, |
| 42 | ToggleUnderline, |
| 43 | }; |
| 44 | use super::attachment_tray::{ |
| 45 | AttachmentTray, DoneFile, JobKind, LoadedFile, MAX_ATTACHMENTS, PasteAction, UploadJob, |
| 46 | UploadResult, discard_uploaded, inline_preview, names_of, paste_action, pasted_image_name, |
| 47 | prepare_pasted_image, read_attachment, render_tray, |
| 48 | }; |
| 49 | use super::avatar::{person_avatar, square_avatar}; |
| 50 | use super::draft_style::draft_style; |
| 51 | use super::emoji_popup::{self, EmojiPopup}; |
| 52 | use super::format_toolbar::{self, ButtonState, FormatButton}; |
| 53 | use super::fun_picker::{FunPicker, FunPickerEvent}; |
| 54 | use super::link_preview::{ComposeLink, draft_link, link_preview_card, meeting_join_chip}; |
| 55 | use super::widgets::{icon, symbol}; |
| 56 | use crate::app_state::AppState; |
| 57 | use crate::emoji; |
| 58 | pub use crate::outgoing::{EditPreview, Outgoing, ReplyPreview}; |
| 59 | use crate::remote_image::RemoteImage; |
| 60 | use crate::runtime; |
| 61 | use crate::scheduled_rows::scheduled_label; |
| 62 | use crate::theme::{self, ElevatedSurface as _}; |
| 63 | use crate::typing::OutgoingTyping; |
| 64 | use table::{TableKey, TableLineMenu, TableMenu, TablePicker, TableViews, is_table_token}; |
| 65 | |
| 66 | const MIN_ROWS: usize = 1; |
| 67 | const MAX_ROWS: usize = 8; |
| 68 | const MENTION_DEBOUNCE: Duration = Duration::from_millis(150); |
| 69 | const MENTION_LIMIT: usize = 8; |
| 70 | const LINK_PREVIEW_DEBOUNCE: Duration = Duration::from_millis(600); |
| 71 | const MENTION_QUERY_MAX_CHARS: usize = 32; |
| 72 | const MENTION_QUERY_MAX_WORDS: usize = 3; |
| 73 | const POPUP_WIDTH: f32 = 380.; |
| 74 | const POPUP_ROW_HEIGHT: f32 = 44.; |
| 75 | const DEMO_UPLOAD_STEPS: u8 = 10; |
| 76 | const COMPOSER_ICON_SIZE: f32 = 20.; |
| 77 | const COMPOSER_BUTTON_SIZE: f32 = 32.; |
| 78 | const COMPOSER_BUTTON_INSET: f32 = 6.; |
| 79 | pub const COMPOSER_BOX_TAG: &str = "composer"; |
| 80 | pub const COMPOSER_LEADING_TAG: &str = "composer/leading"; |
| 81 | pub const COMPOSER_SEND_TAG: &str = "composer/send"; |
| 82 | pub const COMPOSER_FORMAT_TAG: &str = "composer/leading/format"; |
| 83 | pub const COMPOSER_FORMAT_BAR_TAG: &str = "composer/format-bar"; |
| 84 | pub const COMPOSER_FORMAT_DIVIDER_TAG: &str = "composer/format-bar/divider"; |
| 85 | /// gpui-kit draws a `Size::Size` button's icon at this fraction of the button size. |
| 86 | const BUTTON_ICON_SCALE: f32 = 0.75; |
| 87 | const DEMO_UPLOAD_STEP: Duration = Duration::from_millis(120); |
| 88 | const DEMO_FAILURE_STEP: u8 = 6; |
| 89 | const HISTORY_LIMIT: usize = 200; |
| 90 | const PASTE_HINT_DURATION: Duration = Duration::from_secs(4); |
| 91 | const IMAGE_TOKEN_PREFIX: &str = "image-"; |
| 92 | |
| 93 | pub fn bind_keys(cx: &mut App) { |
| 94 | super::shortcuts::bind_scopes(cx, &[super::shortcuts::Scope::Composer]); |
| 95 | } |
| 96 | |
| 97 | enum UploadEvent { |
| 98 | Progress(u8), |
| 99 | Uploaded(UploadedFile), |
| 100 | Finished(UploadResult), |
| 101 | } |
| 102 | |
| 103 | enum UploadHandle { |
| 104 | Network(tokio::task::AbortHandle), |
| 105 | Demo(Task<()>), |
| 106 | } |
| 107 | |
| 108 | impl UploadHandle { |
| 109 | fn cancel(self) { |
| 110 | match self { |
| 111 | UploadHandle::Network(handle) => handle.abort(), |
| 112 | UploadHandle::Demo(task) => drop(task), |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | pub enum ComposerEvent { |
| 118 | Submit(Box<Outgoing>), |
| 119 | Schedule { |
| 120 | outgoing: Box<Outgoing>, |
| 121 | send_at: DateTime<Utc>, |
| 122 | }, |
| 123 | EditLast, |
| 124 | Typing(bool), |
| 125 | } |
| 126 | |
| 127 | struct MentionPopup { |
| 128 | range: Range<usize>, |
| 129 | query: String, |
| 130 | candidates: Vec<MentionCandidate>, |
| 131 | highlighted: usize, |
| 132 | } |
| 133 | |
| 134 | /// An automatic edit Backspace takes back while nothing changed since. |
| 135 | struct Conversion { |
| 136 | value: String, |
| 137 | cursor: usize, |
| 138 | before: Draft, |
| 139 | restore: Vec<Edit>, |
| 140 | } |
| 141 | |
| 142 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 143 | enum StepKind { |
| 144 | Typing { |
| 145 | at: usize, |
| 146 | after_space: bool, |
| 147 | }, |
| 148 | Deleting { |
| 149 | start: usize, |
| 150 | end: usize, |
| 151 | }, |
| 152 | CellTyping { |
| 153 | table: usize, |
| 154 | row: usize, |
| 155 | column: usize, |
| 156 | }, |
| 157 | Other, |
| 158 | } |
| 159 | |
| 160 | /// One state of the composer's own undo history: text, tokens, formatting and selection. |
| 161 | struct HistoryStep { |
| 162 | draft: Draft, |
| 163 | tokens: Vec<(Range<usize>, InlineToken)>, |
| 164 | selection: Range<usize>, |
| 165 | kind: StepKind, |
| 166 | } |
| 167 | |
| 168 | struct LinkEditor { |
| 169 | range: Range<usize>, |
| 170 | field: Entity<InputState>, |
| 171 | refused: bool, |
| 172 | _edits: Subscription, |
| 173 | } |
| 174 | |
| 175 | type DraftDecorations = ( |
| 176 | TextDecorationCollection<TextareaMode>, |
| 177 | RangeDecorationCollection<TextareaMode>, |
| 178 | ); |
| 179 | |
| 180 | pub struct Composer { |
| 181 | app: Entity<AppState>, |
| 182 | input: Entity<TextareaState>, |
| 183 | conversation_id: Option<String>, |
| 184 | inline: bool, |
| 185 | reply: Option<ReplyPreview>, |
| 186 | editing: Option<EditPreview>, |
| 187 | mention_inputs: HashMap<String, MentionInput>, |
| 188 | mention_counter: usize, |
| 189 | popup: Option<MentionPopup>, |
| 190 | lookup: Option<Task<()>>, |
| 191 | emoji_popup: Option<EmojiPopup>, |
| 192 | emoji_dismissed_at: Option<usize>, |
| 193 | recent_emoji: emoji::Recent, |
| 194 | conversion: Option<Conversion>, |
| 195 | undone_value: Option<String>, |
| 196 | previous_value: String, |
| 197 | typing: OutgoingTyping, |
| 198 | draft: Draft, |
| 199 | decorations: DraftDecorations, |
| 200 | pending_style: Option<TypingStyle>, |
| 201 | history: Vec<HistoryStep>, |
| 202 | history_index: usize, |
| 203 | pasted_markdown: Option<String>, |
| 204 | paste_hint: Option<Task<()>>, |
| 205 | copied: Option<(String, Vec<DraftLine>)>, |
| 206 | toolbar_dismissed: Option<Range<usize>>, |
| 207 | mouse_selecting: bool, |
| 208 | link_editor: Option<LinkEditor>, |
| 209 | composer_bounds: Rc<Cell<Option<Bounds<Pixels>>>>, |
| 210 | tray: AttachmentTray, |
| 211 | uploads: HashMap<u64, UploadHandle>, |
| 212 | demo_failed: HashSet<u64>, |
| 213 | carry_images_into: Option<String>, |
| 214 | draft_save: Option<Task<()>>, |
| 215 | saved_images: Option<drafts::ImageFingerprint>, |
| 216 | schedule: Option<schedule::SchedulePopover>, |
| 217 | scheduling: Option<String>, |
| 218 | pending_anchors: HashMap<u64, usize>, |
| 219 | link: ComposeLink, |
| 220 | fun_picker: Entity<FunPicker>, |
| 221 | tables: TableViews, |
| 222 | table_counter: usize, |
| 223 | table_picker: Option<TablePicker>, |
| 224 | table_menu: Option<TableMenu>, |
| 225 | format_bar: HashSet<String>, |
| 226 | active_cell: Option<(String, teams_core::Cell)>, |
| 227 | caret_before_table: usize, |
| 228 | caret_text: String, |
| 229 | importance: store::MessageImportance, |
| 230 | delivery_menu: bool, |
| 231 | _subscriptions: [Subscription; 4], |
| 232 | } |
| 233 | |
| 234 | impl EventEmitter<ComposerEvent> for Composer {} |
| 235 | |
| 236 | pub fn is_plain_enter(event: &InputEvent) -> bool { |
| 237 | matches!(event, InputEvent::PressEnter { shift: false, .. }) |
| 238 | } |
| 239 | |
| 240 | /// Enter sends, Shift+Enter is a newline (handled by the input itself). Blank text never sends. |
| 241 | pub fn submitted_text(event: &InputEvent, value: &str) -> Option<String> { |
| 242 | match event { |
| 243 | InputEvent::PressEnter { shift: false, .. } => { |
| 244 | let trimmed = value.trim(); |
| 245 | (!trimmed.is_empty()).then(|| trimmed.to_owned()) |
| 246 | } |
| 247 | _ => None, |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | pub fn active_mention( |
| 252 | text: &str, |
| 253 | cursor: usize, |
| 254 | tokens: &[Range<usize>], |
| 255 | ) -> Option<(Range<usize>, String)> { |
| 256 | if cursor > text.len() || !text.is_char_boundary(cursor) { |
| 257 | return None; |
| 258 | } |
| 259 | let before = &text[..cursor]; |
| 260 | let at = before.rfind('@')?; |
| 261 | if before[..at] |
| 262 | .chars() |
| 263 | .next_back() |
| 264 | .is_some_and(|previous| !previous.is_whitespace()) |
| 265 | { |
| 266 | return None; |
| 267 | } |
| 268 | let query = &before[at + 1..]; |
| 269 | let rejected = query.contains('\n') |
| 270 | || query.starts_with(char::is_whitespace) |
| 271 | || query.ends_with(char::is_whitespace) |
| 272 | || query.chars().count() > MENTION_QUERY_MAX_CHARS |
| 273 | || query.split_whitespace().count() > MENTION_QUERY_MAX_WORDS |
| 274 | || tokens |
| 275 | .iter() |
| 276 | .any(|token| token.start < cursor && at < token.end); |
| 277 | (!rejected).then(|| (at..cursor, query.to_owned())) |
| 278 | } |
| 279 | |
| 280 | fn candidate_subtitle(candidate: &MentionCandidate) -> String { |
| 281 | match candidate { |
| 282 | MentionCandidate::Person(person) => person |
| 283 | .job_title |
| 284 | .clone() |
| 285 | .filter(|title| !title.trim().is_empty()) |
| 286 | .or_else(|| person.mail.clone()) |
| 287 | .unwrap_or_default(), |
| 288 | MentionCandidate::Channel { .. } => "Channel".to_owned(), |
| 289 | MentionCandidate::Team { .. } => "Team".to_owned(), |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | fn step_kind(previous: &str, value: &str, cursor: usize) -> StepKind { |
| 294 | if let Some(typed) = emoji::typed_char(previous, value, cursor).filter(|typed| *typed != '\n') { |
| 295 | return StepKind::Typing { |
| 296 | at: cursor - typed.len_utf8(), |
| 297 | after_space: typed.is_whitespace(), |
| 298 | }; |
| 299 | } |
| 300 | let removed = previous.len().saturating_sub(value.len()); |
| 301 | let one_char_removed = removed > 0 |
| 302 | && previous.is_char_boundary(cursor) |
| 303 | && previous |
| 304 | .get(cursor..cursor + removed) |
| 305 | .is_some_and(|gone| gone.chars().count() == 1 && gone != "\n") |
| 306 | && previous.get(..cursor) == value.get(..cursor) |
| 307 | && previous.get(cursor + removed..) == value.get(cursor..); |
| 308 | if one_char_removed { |
| 309 | StepKind::Deleting { |
| 310 | start: cursor, |
| 311 | end: cursor + removed, |
| 312 | } |
| 313 | } else { |
| 314 | StepKind::Other |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | fn continues(last: &HistoryStep, kind: StepKind) -> bool { |
| 319 | let caret = &last.selection; |
| 320 | match (last.kind, kind) { |
| 321 | ( |
| 322 | StepKind::Typing { after_space, .. }, |
| 323 | StepKind::Typing { |
| 324 | at, |
| 325 | after_space: space, |
| 326 | }, |
| 327 | ) => (!after_space || space) && caret.is_empty() && caret.start == at, |
| 328 | (StepKind::Deleting { .. }, StepKind::Deleting { start, end }) => { |
| 329 | caret.is_empty() && (caret.start == end || caret.start == start) |
| 330 | } |
| 331 | (previous @ StepKind::CellTyping { .. }, next @ StepKind::CellTyping { .. }) => { |
| 332 | previous == next |
| 333 | } |
| 334 | _ => false, |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | fn looks_like_url(text: &str) -> bool { |
| 339 | ["https://", "http://", "www."] |
| 340 | .iter() |
| 341 | .any(|prefix| text.starts_with(prefix)) |
| 342 | && !text.contains(char::is_whitespace) |
| 343 | } |
| 344 | |
| 345 | fn image_token(id: u64) -> InlineToken { |
| 346 | InlineToken::new(format!("{IMAGE_TOKEN_PREFIX}{id}"), OBJECT_MARK.to_string()).block() |
| 347 | } |
| 348 | |
| 349 | fn image_id(token: &InlineToken) -> Option<u64> { |
| 350 | token.id().strip_prefix(IMAGE_TOKEN_PREFIX)?.parse().ok() |
| 351 | } |
| 352 | |
| 353 | fn candidate_key(candidate: &MentionCandidate) -> &str { |
| 354 | match candidate { |
| 355 | MentionCandidate::Person(person) => &person.user_id, |
| 356 | MentionCandidate::Channel { channel_id, .. } => channel_id, |
| 357 | MentionCandidate::Team { team_id, .. } => team_id, |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | impl Composer { |
| 362 | pub fn new(app: Entity<AppState>, window: &mut Window, cx: &mut Context<Self>) -> Self { |
| 363 | let input = cx.new(|cx| { |
| 364 | TextareaState::new(window, cx) |
| 365 | .auto_grow(MIN_ROWS, MAX_ROWS) |
| 366 | .submit_on_enter(true) |
| 367 | .placeholder("Type a message") |
| 368 | }); |
| 369 | let subscription = cx.subscribe_in(&input, window, Self::on_input_event); |
| 370 | let observer = cx.observe_in(&input, window, |this, _, window, cx| { |
| 371 | this.redirect_caret_from_table(window, cx); |
| 372 | cx.notify(); |
| 373 | }); |
| 374 | let image_observer = cx.observe(&app, |this, _, cx| { |
| 375 | if this.link_image_pending(cx) { |
| 376 | cx.notify(); |
| 377 | } |
| 378 | }); |
| 379 | let fun_picker = cx.new(|cx| FunPicker::new(app.clone(), window, cx)); |
| 380 | let picker_subscription = cx.subscribe_in(&fun_picker, window, Self::on_fun_pick); |
| 381 | let decorations = input.update(cx, |state, cx| { |
| 382 | ( |
| 383 | state.create_decorations_collection(Vec::new(), cx), |
| 384 | state.create_range_decorations_collection(Vec::new(), cx), |
| 385 | ) |
| 386 | }); |
| 387 | let recent_emoji = emoji::Recent::load(&app.read(cx).store); |
| 388 | Composer { |
| 389 | app, |
| 390 | input, |
| 391 | conversation_id: None, |
| 392 | inline: false, |
| 393 | reply: None, |
| 394 | editing: None, |
| 395 | mention_inputs: HashMap::new(), |
| 396 | mention_counter: 0, |
| 397 | popup: None, |
| 398 | lookup: None, |
| 399 | emoji_popup: None, |
| 400 | emoji_dismissed_at: None, |
| 401 | recent_emoji, |
| 402 | conversion: None, |
| 403 | undone_value: None, |
| 404 | previous_value: String::new(), |
| 405 | typing: OutgoingTyping::default(), |
| 406 | draft: Draft::default(), |
| 407 | decorations, |
| 408 | pending_style: None, |
| 409 | history: vec![HistoryStep { |
| 410 | draft: Draft::default(), |
| 411 | tokens: Vec::new(), |
| 412 | selection: 0..0, |
| 413 | kind: StepKind::Other, |
| 414 | }], |
| 415 | history_index: 0, |
| 416 | pasted_markdown: None, |
| 417 | paste_hint: None, |
| 418 | copied: None, |
| 419 | toolbar_dismissed: None, |
| 420 | mouse_selecting: false, |
| 421 | link_editor: None, |
| 422 | composer_bounds: Rc::new(Cell::new(None)), |
| 423 | tray: AttachmentTray::default(), |
| 424 | uploads: HashMap::new(), |
| 425 | demo_failed: HashSet::new(), |
| 426 | carry_images_into: None, |
| 427 | draft_save: None, |
| 428 | saved_images: None, |
| 429 | schedule: None, |
| 430 | scheduling: None, |
| 431 | pending_anchors: HashMap::new(), |
| 432 | link: ComposeLink::default(), |
| 433 | fun_picker, |
| 434 | tables: TableViews::default(), |
| 435 | table_counter: 0, |
| 436 | table_picker: None, |
| 437 | table_menu: None, |
| 438 | format_bar: HashSet::new(), |
| 439 | active_cell: None, |
| 440 | caret_before_table: 0, |
| 441 | caret_text: String::new(), |
| 442 | importance: store::MessageImportance::Normal, |
| 443 | delivery_menu: false, |
| 444 | _subscriptions: [subscription, observer, image_observer, picker_subscription], |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | fn on_input_event( |
| 449 | &mut self, |
| 450 | input: &Entity<TextareaState>, |
| 451 | event: &InputEvent, |
| 452 | window: &mut Window, |
| 453 | cx: &mut Context<Self>, |
| 454 | ) { |
| 455 | cx.notify(); |
| 456 | if matches!(event, InputEvent::Change) { |
| 457 | self.on_change(window, cx); |
| 458 | self.update_emoji(cx); |
| 459 | self.update_mention(cx); |
| 460 | self.update_link_preview(cx); |
| 461 | self.schedule_draft_save(cx); |
| 462 | } |
| 463 | let value = input.read(cx).value(); |
| 464 | if submitted_text(event, &value).is_some() |
| 465 | || (is_plain_enter(event) && self.has_attachments(cx)) |
| 466 | { |
| 467 | self.submit_current(window, cx); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | fn update_mention(&mut self, cx: &mut Context<Self>) { |
| 472 | let state = self.input.read(cx); |
| 473 | let tokens: Vec<Range<usize>> = state.tokens().iter().map(|span| span.range()).collect(); |
| 474 | let found = active_mention(&state.value(), state.cursor(), &tokens); |
| 475 | let Some((range, query)) = found else { |
| 476 | self.close_mention_popup(); |
| 477 | return; |
| 478 | }; |
| 479 | let unchanged = self |
| 480 | .popup |
| 481 | .as_ref() |
| 482 | .is_some_and(|popup| popup.query == query && popup.range == range); |
| 483 | if unchanged { |
| 484 | return; |
| 485 | } |
| 486 | let previous = self.popup.take(); |
| 487 | self.popup = Some(MentionPopup { |
| 488 | range, |
| 489 | query: query.clone(), |
| 490 | candidates: previous.map(|popup| popup.candidates).unwrap_or_default(), |
| 491 | highlighted: 0, |
| 492 | }); |
| 493 | self.request_candidates(query, cx); |
| 494 | } |
| 495 | |
| 496 | fn close_popup(&mut self) { |
| 497 | self.close_mention_popup(); |
| 498 | self.emoji_popup = None; |
| 499 | } |
| 500 | |
| 501 | fn close_mention_popup(&mut self) { |
| 502 | self.popup = None; |
| 503 | self.lookup = None; |
| 504 | } |
| 505 | |
| 506 | fn report_typing(&mut self, has_text: bool, cx: &mut Context<Self>) { |
| 507 | if self.editing.is_some() { |
| 508 | return; |
| 509 | } |
| 510 | if let Some(active) = self.typing.on_text(has_text, Instant::now()) { |
| 511 | cx.emit(ComposerEvent::Typing(active)); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | pub fn stop_typing(&mut self) -> bool { |
| 516 | self.typing.stop() |
| 517 | } |
| 518 | |
| 519 | fn on_change(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 520 | let state = self.input.read(cx); |
| 521 | let (value, input_cursor) = (state.value().to_string(), state.cursor()); |
| 522 | self.report_typing(!value.trim().is_empty(), cx); |
| 523 | let previous = std::mem::replace(&mut self.previous_value, value.clone()); |
| 524 | if previous != value { |
| 525 | let (start, old_end, new_end) = changed_span(&previous, &value); |
| 526 | self.shift_anchors(start, old_end, new_end); |
| 527 | } |
| 528 | if value == self.draft.text() { |
| 529 | self.refresh_style(cx); |
| 530 | return; |
| 531 | } |
| 532 | let kind = step_kind(&previous, &value, input_cursor); |
| 533 | let typing = self.pending_style.take(); |
| 534 | let cursor = self.draft.apply_edit(&value, input_cursor, typing.as_ref()); |
| 535 | let edits = self.draft.take_edits(); |
| 536 | self.apply_to_input(edits, cursor..cursor, window, cx); |
| 537 | self.record(kind, cx); |
| 538 | if let Some(pasted) = self.pasted_markdown.take() |
| 539 | && self.convert_paste(&pasted, cursor, window, cx) |
| 540 | { |
| 541 | return; |
| 542 | } |
| 543 | let typed = emoji::typed_char(&previous, self.draft.text(), cursor); |
| 544 | if typed.is_some() && self.convert_markdown(cursor, window, cx) { |
| 545 | return; |
| 546 | } |
| 547 | if matches!(typed, Some(' ' | '\n')) && self.convert_link(cursor, window, cx) { |
| 548 | return; |
| 549 | } |
| 550 | self.convert_emoji(typed, cursor, window, cx); |
| 551 | } |
| 552 | |
| 553 | fn convert_link(&mut self, cursor: usize, window: &mut Window, cx: &mut Context<Self>) -> bool { |
| 554 | let before = self.draft.clone(); |
| 555 | if !self.draft.autolink_typed(cursor) { |
| 556 | return false; |
| 557 | } |
| 558 | self.finish_conversion(before, cursor, window, cx); |
| 559 | true |
| 560 | } |
| 561 | |
| 562 | fn convert_markdown( |
| 563 | &mut self, |
| 564 | cursor: usize, |
| 565 | window: &mut Window, |
| 566 | cx: &mut Context<Self>, |
| 567 | ) -> bool { |
| 568 | let before = self.draft.clone(); |
| 569 | let Some((cursor, style)) = self.draft.convert_typed(cursor) else { |
| 570 | self.draft.take_edits(); |
| 571 | return false; |
| 572 | }; |
| 573 | self.finish_conversion(before, cursor, window, cx); |
| 574 | self.pending_style = (!style.is_empty()).then_some(style); |
| 575 | true |
| 576 | } |
| 577 | |
| 578 | fn convert_emoji( |
| 579 | &mut self, |
| 580 | typed: Option<char>, |
| 581 | cursor: usize, |
| 582 | window: &mut Window, |
| 583 | cx: &mut Context<Self>, |
| 584 | ) { |
| 585 | if self.draft.in_code(cursor) { |
| 586 | return; |
| 587 | } |
| 588 | let value = self.draft.text(); |
| 589 | let found = match typed { |
| 590 | Some(':') => emoji::closing_code(value, cursor) |
| 591 | .and_then(|(range, code)| Some((range, emoji::lookup(code)?))), |
| 592 | Some(' ') => emoji::smiley_before_space(value, cursor), |
| 593 | _ => None, |
| 594 | }; |
| 595 | let Some((range, glyph)) = found else { |
| 596 | return; |
| 597 | }; |
| 598 | let before = self.draft.clone(); |
| 599 | self.draft.replace(range.clone(), glyph); |
| 600 | self.finish_conversion(before, cursor + glyph.len() - range.len(), window, cx); |
| 601 | self.remember_emoji(glyph, cx); |
| 602 | } |
| 603 | |
| 604 | fn finish_conversion( |
| 605 | &mut self, |
| 606 | before: Draft, |
| 607 | cursor: usize, |
| 608 | window: &mut Window, |
| 609 | cx: &mut Context<Self>, |
| 610 | ) { |
| 611 | let edits = self.draft.take_edits(); |
| 612 | let restore = reverse_edits(&edits, before.text()); |
| 613 | self.apply_to_input(edits, cursor..cursor, window, cx); |
| 614 | self.record(StepKind::Other, cx); |
| 615 | self.conversion = Some(Conversion { |
| 616 | value: self.draft.text().to_owned(), |
| 617 | cursor, |
| 618 | before, |
| 619 | restore, |
| 620 | }); |
| 621 | } |
| 622 | |
| 623 | fn convert_paste( |
| 624 | &mut self, |
| 625 | pasted: &str, |
| 626 | cursor: usize, |
| 627 | window: &mut Window, |
| 628 | cx: &mut Context<Self>, |
| 629 | ) -> bool { |
| 630 | let pasted = pasted.replace("\r\n", "\n"); |
| 631 | let Some(start) = cursor |
| 632 | .checked_sub(pasted.len()) |
| 633 | .filter(|start| self.draft.text().get(*start..cursor) == Some(pasted.as_str())) |
| 634 | else { |
| 635 | return false; |
| 636 | }; |
| 637 | let lines = Draft::from_markdown(&pasted).slice(0..usize::MAX); |
| 638 | let cursor = self.draft.insert_lines(start..cursor, &lines); |
| 639 | let changed_text = self.draft.text().get(start..cursor) != Some(pasted.as_str()); |
| 640 | self.commit(Some(cursor), window, cx); |
| 641 | if !changed_text { |
| 642 | return true; |
| 643 | } |
| 644 | self.paste_hint = Some(cx.spawn(async move |this, cx| { |
| 645 | cx.background_executor().timer(PASTE_HINT_DURATION).await; |
| 646 | this.update(cx, |this, cx| { |
| 647 | this.paste_hint = None; |
| 648 | cx.notify(); |
| 649 | }) |
| 650 | .ok(); |
| 651 | })); |
| 652 | true |
| 653 | } |
| 654 | |
| 655 | /// Puts the draft's pending edits into the input, then places the selection. |
| 656 | fn commit(&mut self, cursor: Option<usize>, window: &mut Window, cx: &mut Context<Self>) { |
| 657 | let edits = self.draft.take_edits(); |
| 658 | let selection = self.input.read(cx).selected_range(); |
| 659 | let selection = match cursor { |
| 660 | Some(cursor) => cursor..cursor, |
| 661 | None if edits.is_empty() => selection, |
| 662 | None => map_offset(&edits, selection.start)..map_offset(&edits, selection.end), |
| 663 | }; |
| 664 | self.apply_to_input(edits, selection, window, cx); |
| 665 | self.record(StepKind::Other, cx); |
| 666 | self.schedule_draft_save(cx); |
| 667 | } |
| 668 | |
| 669 | fn apply_to_input( |
| 670 | &mut self, |
| 671 | edits: Vec<Edit>, |
| 672 | selection: Range<usize>, |
| 673 | window: &mut Window, |
| 674 | cx: &mut Context<Self>, |
| 675 | ) { |
| 676 | self.input.update(cx, |state, cx| { |
| 677 | let edited = !edits.is_empty(); |
| 678 | if edited { |
| 679 | state.apply_edits(&edits, window, cx); |
| 680 | } |
| 681 | if edited || state.selected_range() != selection { |
| 682 | state.set_selected_range(selection.clone(), cx); |
| 683 | } |
| 684 | }); |
| 685 | for anchor in self.pending_anchors.values_mut() { |
| 686 | *anchor = map_offset(&edits, *anchor); |
| 687 | } |
| 688 | let value = self.input.read(cx).value().to_string(); |
| 689 | if value != self.draft.text() { |
| 690 | self.draft.apply_edit(&value, selection.end, None); |
| 691 | self.draft.take_edits(); |
| 692 | } |
| 693 | self.previous_value = value; |
| 694 | self.refresh_style(cx); |
| 695 | self.sync_tables(window, cx); |
| 696 | } |
| 697 | |
| 698 | fn shift_anchors(&mut self, start: usize, old_end: usize, new_end: usize) { |
| 699 | for anchor in self.pending_anchors.values_mut() { |
| 700 | *anchor = if *anchor <= start { |
| 701 | *anchor |
| 702 | } else if *anchor >= old_end { |
| 703 | *anchor - old_end + new_end |
| 704 | } else { |
| 705 | new_end |
| 706 | }; |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | /// Adds the current state as an undo step. Typing and deleting runs merge into one step; |
| 711 | /// a typed space ends a typing step. |
| 712 | fn record(&mut self, kind: StepKind, cx: &App) { |
| 713 | let state = self.input.read(cx); |
| 714 | let step = HistoryStep { |
| 715 | draft: self.draft.clone(), |
| 716 | tokens: state |
| 717 | .tokens() |
| 718 | .iter() |
| 719 | .map(|span| (span.range(), span.token().clone())) |
| 720 | .collect(), |
| 721 | selection: state.selected_range(), |
| 722 | kind, |
| 723 | }; |
| 724 | let at_end = self.history_index + 1 == self.history.len(); |
| 725 | let merges = at_end |
| 726 | && self |
| 727 | .history |
| 728 | .last() |
| 729 | .is_some_and(|last| continues(last, kind)); |
| 730 | if merges { |
| 731 | if let Some(last) = self.history.last_mut() { |
| 732 | *last = step; |
| 733 | } |
| 734 | return; |
| 735 | } |
| 736 | self.history.truncate(self.history_index + 1); |
| 737 | self.history.push(step); |
| 738 | if self.history.len() > HISTORY_LIMIT { |
| 739 | self.history.remove(0); |
| 740 | } |
| 741 | self.history_index = self.history.len() - 1; |
| 742 | } |
| 743 | |
| 744 | /// Ctrl+Z / Ctrl+Y move through the composer's own history; the input's is never used. |
| 745 | fn step_history(&mut self, back: bool, window: &mut Window, cx: &mut Context<Self>) { |
| 746 | let target = if back { |
| 747 | self.history_index.checked_sub(1) |
| 748 | } else { |
| 749 | Some(self.history_index + 1).filter(|index| *index < self.history.len()) |
| 750 | }; |
| 751 | let Some(target) = target else { |
| 752 | return; |
| 753 | }; |
| 754 | let source_selection = self.history[self.history_index].selection.clone(); |
| 755 | self.history_index = target; |
| 756 | let step = &self.history[target]; |
| 757 | let (draft, tokens) = (step.draft.clone(), step.tokens.clone()); |
| 758 | let current = self.input.read(cx).value().to_string(); |
| 759 | let (start, old_end, new_end) = changed_span(¤t, draft.text()); |
| 760 | let selection = match (start == old_end && start == new_end, back) { |
| 761 | (false, _) => new_end..new_end, |
| 762 | (true, true) => source_selection, |
| 763 | (true, false) => step.selection.clone(), |
| 764 | }; |
| 765 | if start != old_end || start != new_end { |
| 766 | self.shift_anchors(start, old_end, new_end); |
| 767 | } |
| 768 | self.draft = draft.clone(); |
| 769 | self.previous_value = draft.text().to_owned(); |
| 770 | self.input.update(cx, |state, cx| { |
| 771 | if start != old_end || start != new_end { |
| 772 | state.apply_edits( |
| 773 | &[(start..old_end, draft.text()[start..new_end].to_owned())], |
| 774 | window, |
| 775 | cx, |
| 776 | ); |
| 777 | } |
| 778 | for (range, token) in tokens { |
| 779 | let present = state.tokens().iter().any(|span| span.range() == range); |
| 780 | if !present && range.start >= start && range.end <= new_end { |
| 781 | state |
| 782 | .replace_range_with_token(range, token, window, cx) |
| 783 | .ok(); |
| 784 | } |
| 785 | } |
| 786 | state.set_selected_range(selection, cx); |
| 787 | }); |
| 788 | self.undone_value = Some(self.input.read(cx).value().to_string()); |
| 789 | self.pending_style = None; |
| 790 | self.conversion = None; |
| 791 | self.paste_hint = None; |
| 792 | self.refresh_style(cx); |
| 793 | self.sync_tables(window, cx); |
| 794 | self.update_emoji(cx); |
| 795 | self.update_mention(cx); |
| 796 | self.schedule_draft_save(cx); |
| 797 | cx.notify(); |
| 798 | } |
| 799 | |
| 800 | fn refresh_style(&mut self, cx: &mut Context<Self>) { |
| 801 | let style = draft_style(&self.draft, cx.theme().mono_font_family.clone()); |
| 802 | let (text, ranges) = &self.decorations; |
| 803 | text.set(style.text, cx); |
| 804 | ranges.set(style.ranges, cx); |
| 805 | self.input.update(cx, |state, cx| { |
| 806 | state.set_line_indents(style.indents, cx); |
| 807 | state.set_hanging_indents(style.markers, cx); |
| 808 | }); |
| 809 | } |
| 810 | |
| 811 | fn load_draft( |
| 812 | &mut self, |
| 813 | draft: Draft, |
| 814 | content: InputContent, |
| 815 | window: &mut Window, |
| 816 | cx: &mut Context<Self>, |
| 817 | ) { |
| 818 | let end = draft.text().len(); |
| 819 | self.input.update(cx, |state, cx| { |
| 820 | state.set_value(content, window, cx); |
| 821 | state.set_selected_range(end..end, cx); |
| 822 | }); |
| 823 | self.previous_value = draft.text().to_owned(); |
| 824 | self.draft = draft; |
| 825 | self.pending_style = None; |
| 826 | self.conversion = None; |
| 827 | self.pasted_markdown = None; |
| 828 | self.paste_hint = None; |
| 829 | self.link_editor = None; |
| 830 | self.toolbar_dismissed = None; |
| 831 | self.pending_anchors.clear(); |
| 832 | self.active_cell = None; |
| 833 | self.table_picker = None; |
| 834 | self.sync_tables(window, cx); |
| 835 | self.history.clear(); |
| 836 | self.history_index = 0; |
| 837 | self.record(StepKind::Other, cx); |
| 838 | self.refresh_style(cx); |
| 839 | } |
| 840 | |
| 841 | /// Returns the cursor, kept at the same spot relative to the text after `range`. |
| 842 | fn replace_text( |
| 843 | &mut self, |
| 844 | range: Range<usize>, |
| 845 | text: &str, |
| 846 | cursor: usize, |
| 847 | window: &mut Window, |
| 848 | cx: &mut Context<Self>, |
| 849 | ) -> usize { |
| 850 | let cursor = cursor + text.len() - range.len(); |
| 851 | self.input.update(cx, |state, cx| { |
| 852 | state.set_selected_range(range, cx); |
| 853 | state.replace(text.to_owned(), window, cx); |
| 854 | state.set_selected_range(cursor..cursor, cx); |
| 855 | }); |
| 856 | cursor |
| 857 | } |
| 858 | |
| 859 | fn undo_conversion(&mut self, window: &mut Window, cx: &mut Context<Self>) -> bool { |
| 860 | let state = self.input.read(cx); |
| 861 | let unchanged = self.conversion.as_ref().is_some_and(|conversion| { |
| 862 | conversion.value == state.value().as_ref() && conversion.cursor == state.cursor() |
| 863 | }); |
| 864 | let Some(conversion) = self.conversion.take().filter(|_| unchanged) else { |
| 865 | return false; |
| 866 | }; |
| 867 | let cursor = map_offset(&conversion.restore, conversion.cursor); |
| 868 | self.draft = conversion.before; |
| 869 | self.pending_style = None; |
| 870 | self.apply_to_input(conversion.restore, cursor..cursor, window, cx); |
| 871 | self.record(StepKind::Other, cx); |
| 872 | self.undone_value = Some(self.input.read(cx).value().to_string()); |
| 873 | self.update_emoji(cx); |
| 874 | true |
| 875 | } |
| 876 | |
| 877 | fn selection(&self, cx: &App) -> Range<usize> { |
| 878 | self.input.read(cx).selected_range() |
| 879 | } |
| 880 | |
| 881 | fn toggle_mark(&mut self, kind: MarkKind, cx: &mut Context<Self>) { |
| 882 | let selection = self.selection(cx); |
| 883 | if selection.is_empty() { |
| 884 | let cursor = selection.start; |
| 885 | let mut pending = self |
| 886 | .pending_style |
| 887 | .take() |
| 888 | .filter(|pending| pending.at == cursor) |
| 889 | .unwrap_or(TypingStyle { |
| 890 | at: cursor, |
| 891 | ..TypingStyle::default() |
| 892 | }); |
| 893 | pending.toggle(kind, &self.draft.style_at(cursor)); |
| 894 | self.pending_style = (!pending.is_empty()).then_some(pending); |
| 895 | } else { |
| 896 | self.draft.toggle(selection, kind); |
| 897 | self.draft.take_edits(); |
| 898 | self.record(StepKind::Other, cx); |
| 899 | self.refresh_style(cx); |
| 900 | self.schedule_draft_save(cx); |
| 901 | } |
| 902 | cx.notify(); |
| 903 | } |
| 904 | |
| 905 | fn set_size(&mut self, size: Option<SizeStep>, cx: &mut Context<Self>) { |
| 906 | let selection = self.selection(cx); |
| 907 | if selection.is_empty() { |
| 908 | let cursor = selection.start; |
| 909 | let mut pending = self |
| 910 | .pending_style |
| 911 | .take() |
| 912 | .filter(|pending| pending.at == cursor) |
| 913 | .unwrap_or(TypingStyle { |
| 914 | at: cursor, |
| 915 | ..TypingStyle::default() |
| 916 | }); |
| 917 | pending.set_size(size, &self.draft.style_at(cursor)); |
| 918 | self.pending_style = (!pending.is_empty()).then_some(pending); |
| 919 | } else { |
| 920 | self.draft.set_size(selection, size); |
| 921 | self.draft.take_edits(); |
| 922 | self.record(StepKind::Other, cx); |
| 923 | self.refresh_style(cx); |
| 924 | self.schedule_draft_save(cx); |
| 925 | } |
| 926 | cx.notify(); |
| 927 | } |
| 928 | |
| 929 | fn normal_size_state(&self, range: Range<usize>) -> FormatState { |
| 930 | let sized = [SizeStep::Small, SizeStep::Large] |
| 931 | .into_iter() |
| 932 | .any(|step| self.draft.state(range.clone(), &MarkKind::Size(step)) != FormatState::Off); |
| 933 | if sized { |
| 934 | FormatState::Off |
| 935 | } else { |
| 936 | FormatState::On |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | fn toggle_lines(&mut self, kind: LineKind, window: &mut Window, cx: &mut Context<Self>) { |
| 941 | let selection = self.selection(cx); |
| 942 | self.draft.toggle_lines(selection, kind); |
| 943 | self.commit(None, window, cx); |
| 944 | cx.notify(); |
| 945 | } |
| 946 | |
| 947 | fn press_format(&mut self, button: FormatButton, window: &mut Window, cx: &mut Context<Self>) { |
| 948 | match button { |
| 949 | FormatButton::Bold => self.toggle_mark(MarkKind::Bold, cx), |
| 950 | FormatButton::Italic => self.toggle_mark(MarkKind::Italic, cx), |
| 951 | FormatButton::Underline => self.toggle_mark(MarkKind::Underline, cx), |
| 952 | FormatButton::Strike => self.toggle_mark(MarkKind::Strike, cx), |
| 953 | FormatButton::Superscript => self.toggle_mark(MarkKind::Superscript, cx), |
| 954 | FormatButton::Subscript => self.toggle_mark(MarkKind::Subscript, cx), |
| 955 | FormatButton::SizeSmall => self.set_size(Some(SizeStep::Small), cx), |
| 956 | FormatButton::SizeNormal => self.set_size(None, cx), |
| 957 | FormatButton::SizeLarge => self.set_size(Some(SizeStep::Large), cx), |
| 958 | FormatButton::Code => self.toggle_mark(MarkKind::Code, cx), |
| 959 | FormatButton::Link => self.open_link_editor(window, cx), |
| 960 | FormatButton::Bulleted => self.toggle_lines(LineKind::Bullet(0), window, cx), |
| 961 | FormatButton::Numbered => self.toggle_lines(LineKind::Numbered(0), window, cx), |
| 962 | FormatButton::Quote => self.toggle_lines(LineKind::Quote, window, cx), |
| 963 | FormatButton::Table => { |
| 964 | self.table_menu = None; |
| 965 | self.toolbar_dismissed = Some(self.selection(cx)); |
| 966 | self.open_table_picker(cx); |
| 967 | } |
| 968 | FormatButton::TableInsertLine => { |
| 969 | self.open_table_menu(TableLineMenu::Insert, window, cx); |
| 970 | } |
| 971 | FormatButton::TableRemoveLine => { |
| 972 | self.open_table_menu(TableLineMenu::Remove, window, cx); |
| 973 | } |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | fn typing_state(&self, cursor: usize, kind: &MarkKind) -> FormatState { |
| 978 | let style = self.draft.style_at(cursor); |
| 979 | let style = match self.pending_style.as_ref() { |
| 980 | Some(pending) if pending.at == cursor => pending.apply(style), |
| 981 | _ => style, |
| 982 | }; |
| 983 | if style.iter().any(|active| active.same_format(kind)) { |
| 984 | FormatState::On |
| 985 | } else { |
| 986 | FormatState::Off |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | fn format_state(&self, button: FormatButton, selection: &Range<usize>) -> FormatState { |
| 991 | let range = selection.clone(); |
| 992 | let mark = match button { |
| 993 | FormatButton::Bold => Some(MarkKind::Bold), |
| 994 | FormatButton::Italic => Some(MarkKind::Italic), |
| 995 | FormatButton::Underline => Some(MarkKind::Underline), |
| 996 | FormatButton::Strike => Some(MarkKind::Strike), |
| 997 | FormatButton::Superscript => Some(MarkKind::Superscript), |
| 998 | FormatButton::Subscript => Some(MarkKind::Subscript), |
| 999 | FormatButton::SizeSmall => Some(MarkKind::Size(SizeStep::Small)), |
| 1000 | FormatButton::SizeLarge => Some(MarkKind::Size(SizeStep::Large)), |
| 1001 | FormatButton::Code => Some(MarkKind::Code), |
| 1002 | FormatButton::Link => Some(MarkKind::Link(String::new())), |
| 1003 | _ => None, |
| 1004 | }; |
| 1005 | if range.is_empty() { |
| 1006 | if let Some(kind) = &mark { |
| 1007 | return self.typing_state(range.start, kind); |
| 1008 | } |
| 1009 | if button == FormatButton::SizeNormal { |
| 1010 | let sized = [SizeStep::Small, SizeStep::Large].into_iter().any(|step| { |
| 1011 | self.typing_state(range.start, &MarkKind::Size(step)) == FormatState::On |
| 1012 | }); |
| 1013 | return if sized { |
| 1014 | FormatState::Off |
| 1015 | } else { |
| 1016 | FormatState::On |
| 1017 | }; |
| 1018 | } |
| 1019 | } |
| 1020 | match button { |
| 1021 | FormatButton::Bold => self.draft.state(range, &MarkKind::Bold), |
| 1022 | FormatButton::Italic => self.draft.state(range, &MarkKind::Italic), |
| 1023 | FormatButton::Underline => self.draft.state(range, &MarkKind::Underline), |
| 1024 | FormatButton::Strike => self.draft.state(range, &MarkKind::Strike), |
| 1025 | FormatButton::Superscript => self.draft.state(range, &MarkKind::Superscript), |
| 1026 | FormatButton::Subscript => self.draft.state(range, &MarkKind::Subscript), |
| 1027 | FormatButton::SizeSmall => self.draft.state(range, &MarkKind::Size(SizeStep::Small)), |
| 1028 | FormatButton::SizeLarge => self.draft.state(range, &MarkKind::Size(SizeStep::Large)), |
| 1029 | FormatButton::SizeNormal => self.normal_size_state(range), |
| 1030 | FormatButton::Code => self.draft.state(range, &MarkKind::Code), |
| 1031 | FormatButton::Link => self.draft.state(range, &MarkKind::Link(String::new())), |
| 1032 | FormatButton::Bulleted => self.draft.line_state(range, &LineKind::Bullet(0)), |
| 1033 | FormatButton::Numbered => self.draft.line_state(range, &LineKind::Numbered(0)), |
| 1034 | FormatButton::Quote => self.draft.line_state(range, &LineKind::Quote), |
| 1035 | FormatButton::Table if self.table_picker.is_some() => FormatState::On, |
| 1036 | FormatButton::TableInsertLine |
| 1037 | if self |
| 1038 | .table_menu |
| 1039 | .is_some_and(|menu| menu.kind == TableLineMenu::Insert) => |
| 1040 | { |
| 1041 | FormatState::On |
| 1042 | } |
| 1043 | FormatButton::TableRemoveLine |
| 1044 | if self |
| 1045 | .table_menu |
| 1046 | .is_some_and(|menu| menu.kind == TableLineMenu::Remove) => |
| 1047 | { |
| 1048 | FormatState::On |
| 1049 | } |
| 1050 | FormatButton::Table | FormatButton::TableInsertLine | FormatButton::TableRemoveLine => { |
| 1051 | FormatState::Off |
| 1052 | } |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | /// Text formats do not apply inside a table cell; the row and column menus only there. |
| 1057 | fn button_state( |
| 1058 | &self, |
| 1059 | button: FormatButton, |
| 1060 | selection: &Range<usize>, |
| 1061 | in_table: bool, |
| 1062 | ) -> ButtonState { |
| 1063 | let enabled = match button { |
| 1064 | FormatButton::TableInsertLine | FormatButton::TableRemoveLine => in_table, |
| 1065 | FormatButton::Table => true, |
| 1066 | _ => !in_table, |
| 1067 | }; |
| 1068 | if enabled { |
| 1069 | ButtonState::enabled(self.format_state(button, selection)) |
| 1070 | } else { |
| 1071 | ButtonState::disabled() |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | pub(super) fn format_bar_open(&self) -> bool { |
| 1076 | self.format_bar |
| 1077 | .contains(self.conversation_id.as_deref().unwrap_or_default()) |
| 1078 | } |
| 1079 | |
| 1080 | pub(super) fn set_format_bar(&mut self, open: bool, cx: &mut Context<Self>) { |
| 1081 | let key = self.conversation_id.clone().unwrap_or_default(); |
| 1082 | if open { |
| 1083 | self.format_bar.insert(key); |
| 1084 | } else { |
| 1085 | self.format_bar.remove(&key); |
| 1086 | self.table_picker = None; |
| 1087 | self.table_menu = None; |
| 1088 | } |
| 1089 | cx.notify(); |
| 1090 | } |
| 1091 | |
| 1092 | fn toggle_format_bar(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 1093 | let open = !self.format_bar_open(); |
| 1094 | self.set_format_bar(open, cx); |
| 1095 | if !self.cell_has_focus(window, cx) { |
| 1096 | self.focus(window, cx); |
| 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | fn edit_link(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 1101 | if self.link_editor.is_some() { |
| 1102 | return; |
| 1103 | } |
| 1104 | if self.draft.is_blank() && self.selection(cx).is_empty() { |
| 1105 | window.dispatch_action(Box::new(OpenSwitcher), cx); |
| 1106 | return; |
| 1107 | } |
| 1108 | self.open_link_editor(window, cx); |
| 1109 | } |
| 1110 | |
| 1111 | fn open_link_editor(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 1112 | let range = self.selection(cx); |
| 1113 | let prefill = self |
| 1114 | .draft |
| 1115 | .link_in(range.clone()) |
| 1116 | .or_else(|| { |
| 1117 | cx.read_from_clipboard() |
| 1118 | .and_then(|item| item.text()) |
| 1119 | .map(|text| text.trim().to_owned()) |
| 1120 | .filter(|text| looks_like_url(text)) |
| 1121 | }) |
| 1122 | .unwrap_or_default(); |
| 1123 | let field = cx.new(|cx| { |
| 1124 | InputState::new(window, cx) |
| 1125 | .placeholder("Paste or type a link") |
| 1126 | .default_value(prefill) |
| 1127 | }); |
| 1128 | field.update(cx, |state, cx| { |
| 1129 | state.focus(window, cx); |
| 1130 | state.select_all(window, cx); |
| 1131 | }); |
| 1132 | let edits = cx.subscribe(&field, |this, _, event: &InputEvent, cx| { |
| 1133 | if matches!(event, InputEvent::Change) |
| 1134 | && let Some(editor) = this.link_editor.as_mut() |
| 1135 | { |
| 1136 | editor.refused = false; |
| 1137 | cx.notify(); |
| 1138 | } |
| 1139 | }); |
| 1140 | self.link_editor = Some(LinkEditor { |
| 1141 | range, |
| 1142 | field, |
| 1143 | refused: false, |
| 1144 | _edits: edits, |
| 1145 | }); |
| 1146 | cx.notify(); |
| 1147 | } |
| 1148 | |
| 1149 | fn apply_link(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 1150 | let Some(editor) = self.link_editor.take() else { |
| 1151 | return; |
| 1152 | }; |
| 1153 | let typed = editor.field.read(cx).value().trim().to_owned(); |
| 1154 | let url = if typed.is_empty() { |
| 1155 | String::new() |
| 1156 | } else if let Some(url) = link_url(&typed) { |
| 1157 | url |
| 1158 | } else { |
| 1159 | self.link_editor = Some(LinkEditor { |
| 1160 | refused: true, |
| 1161 | ..editor |
| 1162 | }); |
| 1163 | cx.notify(); |
| 1164 | return; |
| 1165 | }; |
| 1166 | self.focus(window, cx); |
| 1167 | if editor.range.is_empty() { |
| 1168 | if !url.is_empty() { |
| 1169 | let end = self.draft.insert_link(editor.range.start, &url, &url); |
| 1170 | self.commit(Some(end), window, cx); |
| 1171 | } |
| 1172 | } else { |
| 1173 | self.draft.set_link(editor.range.clone(), &url); |
| 1174 | self.draft.take_edits(); |
| 1175 | self.refresh_style(cx); |
| 1176 | self.input.update(cx, |state, cx| { |
| 1177 | state.set_selected_range(editor.range.clone(), cx) |
| 1178 | }); |
| 1179 | self.record(StepKind::Other, cx); |
| 1180 | self.toolbar_dismissed = Some(editor.range); |
| 1181 | } |
| 1182 | cx.notify(); |
| 1183 | } |
| 1184 | |
| 1185 | fn close_link_editor(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 1186 | let Some(editor) = self.link_editor.take() else { |
| 1187 | return; |
| 1188 | }; |
| 1189 | self.focus(window, cx); |
| 1190 | self.input |
| 1191 | .update(cx, |state, cx| state.set_selected_range(editor.range, cx)); |
| 1192 | cx.notify(); |
| 1193 | } |
| 1194 | |
| 1195 | fn break_line(&mut self, shift: bool, window: &mut Window, cx: &mut Context<Self>) -> bool { |
| 1196 | let selection = self.selection(cx); |
| 1197 | if !selection.is_empty() { |
| 1198 | return false; |
| 1199 | } |
| 1200 | match self.draft.break_line(selection.start, shift) { |
| 1201 | Some(cursor) => { |
| 1202 | self.draft.link_word_ending_at(selection.start); |
| 1203 | self.commit(Some(cursor), window, cx); |
| 1204 | true |
| 1205 | } |
| 1206 | None => false, |
| 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | fn backspace_at_start(&mut self, window: &mut Window, cx: &mut Context<Self>) -> bool { |
| 1211 | let selection = self.selection(cx); |
| 1212 | if !selection.is_empty() { |
| 1213 | return false; |
| 1214 | } |
| 1215 | match self.draft.backspace_at_start(selection.start) { |
| 1216 | Some(cursor) => { |
| 1217 | self.commit(Some(cursor), window, cx); |
| 1218 | true |
| 1219 | } |
| 1220 | None => false, |
| 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | fn delete_forward(&mut self, window: &mut Window, cx: &mut Context<Self>) -> bool { |
| 1225 | let selection = self.selection(cx); |
| 1226 | if !selection.is_empty() { |
| 1227 | return false; |
| 1228 | } |
| 1229 | match self.draft.delete_forward(selection.start) { |
| 1230 | Some(cursor) => { |
| 1231 | self.commit(Some(cursor), window, cx); |
| 1232 | true |
| 1233 | } |
| 1234 | None => false, |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | fn leave_quote(&mut self, window: &mut Window, cx: &mut Context<Self>) -> bool { |
| 1239 | let selection = self.selection(cx); |
| 1240 | if !selection.is_empty() || self.input.read(cx).value().as_ref() != self.draft.text() { |
| 1241 | return false; |
| 1242 | } |
| 1243 | match self.draft.leave_quote_at_end(selection.start) { |
| 1244 | Some(cursor) => { |
| 1245 | self.commit(Some(cursor), window, cx); |
| 1246 | true |
| 1247 | } |
| 1248 | None => false, |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | /// Backspace right after a table or Delete right before one goes into it instead of |
| 1253 | /// joining the line with the table's. |
| 1254 | fn enter_table_beside( |
| 1255 | &mut self, |
| 1256 | after: bool, |
| 1257 | window: &mut Window, |
| 1258 | cx: &mut Context<Self>, |
| 1259 | ) -> bool { |
| 1260 | let selection = self.selection(cx); |
| 1261 | if !selection.is_empty() || self.input.read(cx).value().as_ref() != self.draft.text() { |
| 1262 | return false; |
| 1263 | } |
| 1264 | let found = if after { |
| 1265 | self.draft.table_before(selection.start) |
| 1266 | } else { |
| 1267 | self.draft.table_after(selection.start) |
| 1268 | }; |
| 1269 | let Some(index) = found else { |
| 1270 | return false; |
| 1271 | }; |
| 1272 | self.focus_table_line(index, after, window, cx); |
| 1273 | true |
| 1274 | } |
| 1275 | |
| 1276 | fn cell_has_focus(&self, window: &Window, cx: &App) -> bool { |
| 1277 | self.focused_cell(window, cx).is_some() |
| 1278 | } |
| 1279 | |
| 1280 | fn paste_plain(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 1281 | let Some(text) = cx.read_from_clipboard().and_then(|item| item.text()) else { |
| 1282 | return; |
| 1283 | }; |
| 1284 | let text = text |
| 1285 | .replace("\r\n", "\n") |
| 1286 | .replace([OBJECT_MARK, TABLE_MARK], ""); |
| 1287 | if let Some(input) = self.focused_cell_input(window, cx) { |
| 1288 | input.update(cx, |state, cx| state.replace(text, window, cx)); |
| 1289 | return; |
| 1290 | } |
| 1291 | self.pasted_markdown = None; |
| 1292 | self.input |
| 1293 | .update(cx, |state, cx| state.replace(text, window, cx)); |
| 1294 | } |
| 1295 | |
| 1296 | fn indent(&mut self, outdent: bool, window: &mut Window, cx: &mut Context<Self>) -> bool { |
| 1297 | let selection = self.selection(cx); |
| 1298 | if !self.draft.indent(selection, outdent) { |
| 1299 | return false; |
| 1300 | } |
| 1301 | self.commit(None, window, cx); |
| 1302 | true |
| 1303 | } |
| 1304 | |
| 1305 | fn copy_selection(&mut self, cx: &mut Context<Self>) -> bool { |
| 1306 | let selection = self.selection(cx); |
| 1307 | if selection.is_empty() || self.draft.text().get(selection.clone()).is_none() { |
| 1308 | return false; |
| 1309 | } |
| 1310 | let selected = &self.draft.text()[selection.clone()]; |
| 1311 | let lines = if selected.contains(OBJECT_MARK) { |
| 1312 | self.draft.slice_without_objects(selection) |
| 1313 | } else { |
| 1314 | self.draft.slice(selection) |
| 1315 | }; |
| 1316 | let text = if selected.contains(TABLE_MARK) { |
| 1317 | Draft::from_lines(&lines).plain_text() |
| 1318 | } else { |
| 1319 | selected.replace(OBJECT_MARK, "") |
| 1320 | }; |
| 1321 | if text.is_empty() { |
| 1322 | return true; |
| 1323 | } |
| 1324 | cx.write_to_clipboard(ClipboardItem::new_string_with_json_metadata( |
| 1325 | text.clone(), |
| 1326 | lines.clone(), |
| 1327 | )); |
| 1328 | self.copied = Some((text, lines)); |
| 1329 | true |
| 1330 | } |
| 1331 | |
| 1332 | fn copied_lines(&self, item: &ClipboardItem, text: &str) -> Option<Vec<DraftLine>> { |
| 1333 | item.entries() |
| 1334 | .iter() |
| 1335 | .find_map(|entry| match entry { |
| 1336 | ClipboardEntry::String(string) if string.text() == text => { |
| 1337 | string.metadata_json::<Vec<DraftLine>>() |
| 1338 | } |
| 1339 | _ => None, |
| 1340 | }) |
| 1341 | .or_else(|| { |
| 1342 | let normalized = text.replace("\r\n", "\n"); |
| 1343 | self.copied |
| 1344 | .as_ref() |
| 1345 | .filter(|(copied, _)| *copied == normalized) |
| 1346 | .map(|(_, lines)| lines.clone()) |
| 1347 | }) |
| 1348 | } |
| 1349 | |
| 1350 | fn toolbar_requested(&self, cx: &App) -> bool { |
| 1351 | let selection = self.selection(cx); |
| 1352 | !selection.is_empty() |
| 1353 | && !self.mouse_selecting |
| 1354 | && self.toolbar_dismissed.as_ref() != Some(&selection) |
| 1355 | && !self.popup_is_open() |
| 1356 | } |
| 1357 | |
| 1358 | fn remember_emoji(&mut self, glyph: &str, cx: &App) { |
| 1359 | let store = &self.app.read(cx).store; |
| 1360 | self.recent_emoji.reload(store); |
| 1361 | self.recent_emoji.push(glyph); |
| 1362 | self.recent_emoji.save(store); |
| 1363 | } |
| 1364 | |
| 1365 | fn update_emoji(&mut self, cx: &mut Context<Self>) { |
| 1366 | let state = self.input.read(cx); |
| 1367 | let found = emoji::active_query(&state.value(), state.cursor()) |
| 1368 | .filter(|_| !self.draft.in_code(state.cursor())); |
| 1369 | if found.as_ref().map(|(range, _)| range.start) != self.emoji_dismissed_at { |
| 1370 | self.emoji_dismissed_at = None; |
| 1371 | } |
| 1372 | let found = found.filter(|_| self.emoji_dismissed_at.is_none()); |
| 1373 | let Some((range, query)) = found else { |
| 1374 | self.emoji_popup = None; |
| 1375 | return; |
| 1376 | }; |
| 1377 | let unchanged = self |
| 1378 | .emoji_popup |
| 1379 | .as_ref() |
| 1380 | .is_some_and(|popup| popup.query == query && popup.range == range); |
| 1381 | if unchanged { |
| 1382 | return; |
| 1383 | } |
| 1384 | let matches = emoji::search(&query, &self.recent_emoji.glyphs(), emoji_popup::LIMIT); |
| 1385 | let skin_tone = teams_core::skin_tone(&self.app.read(cx).store); |
| 1386 | self.emoji_popup = |
| 1387 | (!matches.is_empty()).then(|| EmojiPopup::new(range, query, matches, skin_tone)); |
| 1388 | } |
| 1389 | |
| 1390 | fn accept_emoji(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 1391 | let Some(popup) = self.emoji_popup.take() else { |
| 1392 | return; |
| 1393 | }; |
| 1394 | let Some(glyph) = popup.selected_glyph() else { |
| 1395 | return; |
| 1396 | }; |
| 1397 | let cursor = popup.range.end; |
| 1398 | self.replace_text(popup.range, &glyph, cursor, window, cx); |
| 1399 | self.remember_emoji(&crate::emoticons::without_tone(&glyph), cx); |
| 1400 | cx.notify(); |
| 1401 | } |
| 1402 | |
| 1403 | fn request_candidates(&mut self, query: String, cx: &mut Context<Self>) { |
| 1404 | let Some(conversation_id) = self.conversation_id.clone() else { |
| 1405 | return; |
| 1406 | }; |
| 1407 | let (demo, engine) = { |
| 1408 | let state = self.app.read(cx); |
| 1409 | (state.mode.demo, state.engine.clone()) |
| 1410 | }; |
| 1411 | if demo { |
| 1412 | let candidates = crate::demo::mention_candidates(&conversation_id, &query); |
| 1413 | self.apply_candidates(&query, candidates, cx); |
| 1414 | return; |
| 1415 | } |
| 1416 | let Some(engine) = engine else { |
| 1417 | return; |
| 1418 | }; |
| 1419 | self.lookup = Some(cx.spawn(async move |this, cx| { |
| 1420 | cx.background_executor().timer(MENTION_DEBOUNCE).await; |
| 1421 | let task_query = query.clone(); |
| 1422 | let receiver = runtime::spawn(async move { |
| 1423 | engine |
| 1424 | .mention_candidates(&conversation_id, &task_query, MENTION_LIMIT) |
| 1425 | .await |
| 1426 | }); |
| 1427 | if let Ok(Ok(candidates)) = receiver.await { |
| 1428 | this.update(cx, |this, cx| this.apply_candidates(&query, candidates, cx)) |
| 1429 | .ok(); |
| 1430 | } |
| 1431 | })); |
| 1432 | } |
| 1433 | |
| 1434 | fn apply_candidates( |
| 1435 | &mut self, |
| 1436 | query: &str, |
| 1437 | candidates: Vec<MentionCandidate>, |
| 1438 | cx: &mut Context<Self>, |
| 1439 | ) { |
| 1440 | let Some(popup) = self.popup.as_mut().filter(|popup| popup.query == query) else { |
| 1441 | return; |
| 1442 | }; |
| 1443 | popup.candidates = candidates; |
| 1444 | popup.highlighted = 0; |
| 1445 | cx.notify(); |
| 1446 | } |
| 1447 | |
| 1448 | fn update_link_preview(&mut self, cx: &mut Context<Self>) { |
| 1449 | if self.editing.is_some() { |
| 1450 | self.link.reset(); |
| 1451 | return; |
| 1452 | } |
| 1453 | let draft = self.current_draft(cx); |
| 1454 | let meeting = teams_core::meeting_link_in_html(&draft.to_html()); |
| 1455 | if let Some(url) = self.link.observe(draft_link(&draft), draft.is_blank()) { |
| 1456 | self.fetch_link_preview(url, cx); |
| 1457 | } |
| 1458 | self.link.meeting = meeting; |
| 1459 | cx.notify(); |
| 1460 | } |
| 1461 | |
| 1462 | fn fetch_link_preview(&mut self, url: String, cx: &mut Context<Self>) { |
| 1463 | let (demo, engine) = { |
| 1464 | let state = self.app.read(cx); |
| 1465 | (state.mode.demo, state.engine.clone()) |
| 1466 | }; |
| 1467 | if demo { |
| 1468 | let preview = crate::demo::link_preview(&url); |
| 1469 | self.finish_link_preview(&url, preview, cx); |
| 1470 | return; |
| 1471 | } |
| 1472 | let Some(engine) = engine else { |
| 1473 | return; |
| 1474 | }; |
| 1475 | self.link.lookup = Some(cx.spawn(async move |this, cx| { |
| 1476 | cx.background_executor().timer(LINK_PREVIEW_DEBOUNCE).await; |
| 1477 | let task_url = url.clone(); |
| 1478 | let receiver = runtime::spawn(async move { engine.link_preview_for(&task_url).await }); |
| 1479 | if let Ok(Ok(preview)) = receiver.await { |
| 1480 | this.update(cx, |this, cx| this.finish_link_preview(&url, preview, cx)) |
| 1481 | .ok(); |
| 1482 | } |
| 1483 | })); |
| 1484 | } |
| 1485 | |
| 1486 | fn finish_link_preview( |
| 1487 | &mut self, |
| 1488 | url: &str, |
| 1489 | preview: Option<LinkPreview>, |
| 1490 | cx: &mut Context<Self>, |
| 1491 | ) { |
| 1492 | let image = preview.as_ref().and_then(LinkPreview::image); |
| 1493 | if !self.link.apply(url, preview) { |
| 1494 | return; |
| 1495 | } |
| 1496 | if let Some(image) = image { |
| 1497 | self.app |
| 1498 | .update(cx, |state, cx| state.request_images(vec![image], cx)); |
| 1499 | } |
| 1500 | cx.notify(); |
| 1501 | } |
| 1502 | |
| 1503 | fn link_image_pending(&self, cx: &App) -> bool { |
| 1504 | self.link |
| 1505 | .shown() |
| 1506 | .and_then(LinkPreview::image) |
| 1507 | .is_some_and(|image| self.app.read(cx).directory.image(&image.url).is_none()) |
| 1508 | } |
| 1509 | |
| 1510 | fn render_meeting_chip(&self) -> Option<Div> { |
| 1511 | let url = self.link.meeting.clone()?; |
| 1512 | Some( |
| 1513 | h_flex() |
| 1514 | .w_full() |
| 1515 | .mb(px(6.)) |
| 1516 | .child(meeting_join_chip("composer-meeting-join".to_owned(), url)), |
| 1517 | ) |
| 1518 | } |
| 1519 | |
| 1520 | fn render_link_preview(&self, cx: &mut Context<Self>) -> Option<Div> { |
| 1521 | let preview = self.link.shown()?; |
| 1522 | let composer = cx.weak_entity(); |
| 1523 | let close: super::link_preview::CloseHandler = Rc::new(move |_, cx| { |
| 1524 | composer |
| 1525 | .update(cx, |this, cx| { |
| 1526 | this.link.dismiss(); |
| 1527 | cx.notify(); |
| 1528 | }) |
| 1529 | .ok(); |
| 1530 | }); |
| 1531 | Some(div().w_full().mb(px(6.)).child(link_preview_card( |
| 1532 | preview, |
| 1533 | "composer-link-preview".to_owned(), |
| 1534 | &self.app.read(cx).directory, |
| 1535 | true, |
| 1536 | Some(close), |
| 1537 | ))) |
| 1538 | } |
| 1539 | |
| 1540 | fn popup_is_open(&self) -> bool { |
| 1541 | self.emoji_popup.is_some() |
| 1542 | || self |
| 1543 | .popup |
| 1544 | .as_ref() |
| 1545 | .is_some_and(|popup| !popup.candidates.is_empty()) |
| 1546 | } |
| 1547 | |
| 1548 | fn move_highlight(&mut self, delta: isize, cx: &mut Context<Self>) { |
| 1549 | if let Some(popup) = self.emoji_popup.as_mut() { |
| 1550 | popup.move_highlight(delta); |
| 1551 | cx.notify(); |
| 1552 | } else if let Some(popup) = self.popup.as_mut() { |
| 1553 | let last = popup.candidates.len().saturating_sub(1) as isize; |
| 1554 | popup.highlighted = (popup.highlighted as isize + delta).clamp(0, last) as usize; |
| 1555 | cx.notify(); |
| 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | fn accept_popup(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 1560 | if self.emoji_popup.is_some() { |
| 1561 | self.accept_emoji(window, cx); |
| 1562 | } else { |
| 1563 | self.accept_mention(window, cx); |
| 1564 | } |
| 1565 | } |
| 1566 | |
| 1567 | fn accept_mention(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 1568 | let Some(popup) = self.popup.take() else { |
| 1569 | return; |
| 1570 | }; |
| 1571 | self.lookup = None; |
| 1572 | let Some(candidate) = popup.candidates.get(popup.highlighted) else { |
| 1573 | return; |
| 1574 | }; |
| 1575 | self.mention_counter += 1; |
| 1576 | let token_id = format!( |
| 1577 | "mention-{}-{}", |
| 1578 | self.mention_counter, |
| 1579 | candidate_key(candidate) |
| 1580 | ); |
| 1581 | let label = format!("@{}", candidate.display_name()); |
| 1582 | self.mention_inputs |
| 1583 | .insert(token_id.clone(), candidate.to_mention()); |
| 1584 | self.input.update(cx, |state, cx| { |
| 1585 | let token = InlineToken::new(token_id, label); |
| 1586 | if state |
| 1587 | .replace_range_with_token(popup.range, token, window, cx) |
| 1588 | .is_ok() |
| 1589 | { |
| 1590 | state.insert(" ", window, cx); |
| 1591 | } |
| 1592 | }); |
| 1593 | self.close_popup(); |
| 1594 | cx.notify(); |
| 1595 | } |
| 1596 | |
| 1597 | pub fn inline(mut self) -> Self { |
| 1598 | self.inline = true; |
| 1599 | self |
| 1600 | } |
| 1601 | |
| 1602 | pub fn set_placeholder(&mut self, text: &str, window: &mut Window, cx: &mut Context<Self>) { |
| 1603 | let text = text.to_owned(); |
| 1604 | self.input |
| 1605 | .update(cx, |state, cx| state.set_placeholder(text, window, cx)); |
| 1606 | } |
| 1607 | |
| 1608 | pub fn set_conversation( |
| 1609 | &mut self, |
| 1610 | conversation_id: &str, |
| 1611 | name: &str, |
| 1612 | window: &mut Window, |
| 1613 | cx: &mut Context<Self>, |
| 1614 | ) { |
| 1615 | let placeholder = if name.is_empty() { |
| 1616 | "Type a message".to_owned() |
| 1617 | } else { |
| 1618 | format!("Message {name}") |
| 1619 | }; |
| 1620 | self.set_placeholder(&placeholder, window, cx); |
| 1621 | if self.conversation_id.as_deref() != Some(conversation_id) { |
| 1622 | self.save_draft_now(cx); |
| 1623 | self.draft_save = None; |
| 1624 | self.saved_images = None; |
| 1625 | let carry = self.carry_images_into.take().as_deref() == Some(conversation_id); |
| 1626 | self.conversation_id = Some(conversation_id.to_owned()); |
| 1627 | self.cancel_uploads(); |
| 1628 | let dropped = if carry { |
| 1629 | self.tray.keep_images_only() |
| 1630 | } else { |
| 1631 | self.tray.clear() |
| 1632 | }; |
| 1633 | self.discard_uploaded(dropped, cx); |
| 1634 | self.remove_orphan_image_tokens(window, cx); |
| 1635 | self.reply = None; |
| 1636 | self.link.reset(); |
| 1637 | if self.editing.take().is_some() { |
| 1638 | self.load_draft(Draft::default(), InputContent::new(""), window, cx); |
| 1639 | } |
| 1640 | self.mention_inputs.clear(); |
| 1641 | self.close_popup(); |
| 1642 | self.schedule = None; |
| 1643 | self.table_picker = None; |
| 1644 | self.table_menu = None; |
| 1645 | self.reset_importance(); |
| 1646 | if !carry { |
| 1647 | self.load_stored_draft(conversation_id, window, cx); |
| 1648 | } |
| 1649 | cx.notify(); |
| 1650 | } |
| 1651 | } |
| 1652 | |
| 1653 | /// The draft's images follow the user into this chat; any other switch clears the tray. |
| 1654 | pub fn carry_images_into(&mut self, conversation_id: String) { |
| 1655 | self.carry_images_into = Some(conversation_id); |
| 1656 | } |
| 1657 | |
| 1658 | fn discard_uploaded(&self, files: Vec<UploadedFile>, cx: &App) { |
| 1659 | discard_uploaded(self.app.read(cx).engine.clone(), files); |
| 1660 | } |
| 1661 | |
| 1662 | pub fn set_reply(&mut self, reply: Option<ReplyPreview>, cx: &mut Context<Self>) { |
| 1663 | self.reply = reply; |
| 1664 | if self.reply.is_some() { |
| 1665 | self.editing = None; |
| 1666 | } |
| 1667 | self.schedule_draft_save(cx); |
| 1668 | cx.notify(); |
| 1669 | } |
| 1670 | |
| 1671 | pub fn begin_edit(&mut self, draft: Outgoing, window: &mut Window, cx: &mut Context<Self>) { |
| 1672 | self.restore(&draft, window, cx); |
| 1673 | self.focus(window, cx); |
| 1674 | } |
| 1675 | |
| 1676 | pub fn cancel_edit(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 1677 | self.editing = None; |
| 1678 | self.set_text("", window, cx); |
| 1679 | } |
| 1680 | |
| 1681 | fn outgoing(&self, cx: &App) -> Option<Outgoing> { |
| 1682 | self.can_send(cx).then(|| self.compose(cx)) |
| 1683 | } |
| 1684 | |
| 1685 | fn compose(&self, cx: &App) -> Outgoing { |
| 1686 | let state = self.input.read(cx); |
| 1687 | let mut draft = self.current_draft(cx); |
| 1688 | let image_spans: Vec<(Range<usize>, u64)> = state |
| 1689 | .tokens() |
| 1690 | .iter() |
| 1691 | .filter_map(|span| Some((span.range(), image_id(span.token())?))) |
| 1692 | .collect(); |
| 1693 | let image_ids: Vec<u64> = image_spans |
| 1694 | .iter() |
| 1695 | .filter(|(_, id)| self.tray.image(*id).is_some()) |
| 1696 | .map(|(_, id)| *id) |
| 1697 | .collect(); |
| 1698 | for (range, _) in image_spans |
| 1699 | .iter() |
| 1700 | .rev() |
| 1701 | .filter(|(_, id)| self.tray.image(*id).is_none()) |
| 1702 | { |
| 1703 | draft.replace(range.clone(), ""); |
| 1704 | } |
| 1705 | draft.take_edits(); |
| 1706 | let mut draft = draft.trimmed(); |
| 1707 | let attachments_apply = self.editing.is_none(); |
| 1708 | if self.undone_value.as_deref() != Some(state.value().as_ref()) |
| 1709 | && let Some((range, glyph)) = emoji::trailing_smiley(draft.text()) |
| 1710 | && !draft.in_code(range.end - 1) |
| 1711 | { |
| 1712 | draft.replace(range, glyph); |
| 1713 | draft.take_edits(); |
| 1714 | } |
| 1715 | let mentions = state |
| 1716 | .tokens() |
| 1717 | .iter() |
| 1718 | .filter_map(|span| self.mention_inputs.get(span.token().id().as_ref())) |
| 1719 | .cloned() |
| 1720 | .collect(); |
| 1721 | Outgoing { |
| 1722 | draft, |
| 1723 | subject: None, |
| 1724 | mentions, |
| 1725 | reply: self.reply.clone(), |
| 1726 | edit: self.editing.clone(), |
| 1727 | link_preview: self.link.shown().filter(|_| attachments_apply).cloned(), |
| 1728 | images: if attachments_apply { |
| 1729 | self.tray.images_in(&image_ids) |
| 1730 | } else { |
| 1731 | Vec::new() |
| 1732 | }, |
| 1733 | files: if attachments_apply { |
| 1734 | self.tray.outgoing_files() |
| 1735 | } else { |
| 1736 | Vec::new() |
| 1737 | }, |
| 1738 | importance: self.sendable_importance(cx), |
| 1739 | } |
| 1740 | } |
| 1741 | |
| 1742 | /// The draft, or the plain input text while a change is still on its way to the draft. |
| 1743 | fn current_draft(&self, cx: &App) -> Draft { |
| 1744 | let value = self.input.read(cx).value(); |
| 1745 | if self.draft.text() == value.as_ref() { |
| 1746 | self.draft.clone() |
| 1747 | } else { |
| 1748 | Draft::plain(&value) |
| 1749 | } |
| 1750 | } |
| 1751 | |
| 1752 | fn can_send(&self, cx: &App) -> bool { |
| 1753 | let has_text = !self.current_draft(cx).is_blank(); |
| 1754 | if self.editing.is_some() { |
| 1755 | return has_text; |
| 1756 | } |
| 1757 | (has_text || self.has_attachments(cx)) && !self.tray.blocks_send() |
| 1758 | } |
| 1759 | |
| 1760 | fn has_attachments(&self, cx: &App) -> bool { |
| 1761 | self.tray.has_chips() |
| 1762 | || self |
| 1763 | .input |
| 1764 | .read(cx) |
| 1765 | .tokens() |
| 1766 | .iter() |
| 1767 | .any(|span| image_id(span.token()).is_some_and(|id| self.tray.image(id).is_some())) |
| 1768 | } |
| 1769 | |
| 1770 | fn image_token_range(&self, id: u64, cx: &App) -> Option<Range<usize>> { |
| 1771 | self.input |
| 1772 | .read(cx) |
| 1773 | .tokens() |
| 1774 | .iter() |
| 1775 | .find(|span| image_id(span.token()) == Some(id)) |
| 1776 | .map(|span| span.range()) |
| 1777 | } |
| 1778 | |
| 1779 | fn remove_orphan_image_tokens(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 1780 | let edits: Vec<Edit> = self |
| 1781 | .input |
| 1782 | .read(cx) |
| 1783 | .tokens() |
| 1784 | .iter() |
| 1785 | .filter(|span| image_id(span.token()).is_some_and(|id| self.tray.image(id).is_none())) |
| 1786 | .map(|span| (span.range(), String::new())) |
| 1787 | .collect(); |
| 1788 | if !edits.is_empty() { |
| 1789 | self.input |
| 1790 | .update(cx, |state, cx| state.apply_edits(&edits, window, cx)); |
| 1791 | } |
| 1792 | } |
| 1793 | |
| 1794 | fn remove_image_token(&mut self, id: u64, window: &mut Window, cx: &mut Context<Self>) { |
| 1795 | let Some(range) = self.image_token_range(id, cx) else { |
| 1796 | return; |
| 1797 | }; |
| 1798 | self.input.update(cx, |state, cx| { |
| 1799 | state.set_selected_range(range, cx); |
| 1800 | state.replace("", window, cx); |
| 1801 | }); |
| 1802 | } |
| 1803 | |
| 1804 | fn insert_image_token(&mut self, id: u64, window: &mut Window, cx: &mut Context<Self>) { |
| 1805 | let state = self.input.read(cx); |
| 1806 | let value = state.value().to_string(); |
| 1807 | let selection = state.selected_range(); |
| 1808 | let mut offset = self |
| 1809 | .pending_anchors |
| 1810 | .remove(&id) |
| 1811 | .unwrap_or(selection.end) |
| 1812 | .min(value.len()); |
| 1813 | while !value.is_char_boundary(offset) { |
| 1814 | offset -= 1; |
| 1815 | } |
| 1816 | if value == self.draft.text() && self.draft.in_code(offset) { |
| 1817 | offset = value.len(); |
| 1818 | } |
| 1819 | if let Some(span) = state |
| 1820 | .tokens() |
| 1821 | .iter() |
| 1822 | .find(|span| span.range().start < offset && offset < span.range().end) |
| 1823 | { |
| 1824 | offset = span.range().end; |
| 1825 | } |
| 1826 | let length = OBJECT_MARK.len_utf8(); |
| 1827 | let after = |position: usize| { |
| 1828 | if position >= offset { |
| 1829 | position + length |
| 1830 | } else { |
| 1831 | position |
| 1832 | } |
| 1833 | }; |
| 1834 | let inserted = self.input.update(cx, |state, cx| { |
| 1835 | let inserted = state |
| 1836 | .replace_range_with_token(offset..offset, image_token(id), window, cx) |
| 1837 | .is_ok(); |
| 1838 | if inserted { |
| 1839 | state.set_selected_range(after(selection.start)..after(selection.end), cx); |
| 1840 | } |
| 1841 | inserted |
| 1842 | }); |
| 1843 | if !inserted { |
| 1844 | return; |
| 1845 | } |
| 1846 | for (other, anchor) in self.pending_anchors.iter_mut() { |
| 1847 | if *anchor > offset || (*anchor == offset && *other > id) { |
| 1848 | *anchor += length; |
| 1849 | } |
| 1850 | } |
| 1851 | self.previous_value = self.input.read(cx).value().to_string(); |
| 1852 | } |
| 1853 | |
| 1854 | pub fn is_editing(&self) -> bool { |
| 1855 | self.editing.is_some() |
| 1856 | } |
| 1857 | |
| 1858 | fn files_allowed(&self) -> bool { |
| 1859 | self.conversation_id |
| 1860 | .as_deref() |
| 1861 | .is_some_and(|conversation_id| !conversation_id.is_empty()) |
| 1862 | } |
| 1863 | |
| 1864 | fn cancel_uploads(&mut self) { |
| 1865 | for (_, handle) in self.uploads.drain() { |
| 1866 | handle.cancel(); |
| 1867 | } |
| 1868 | } |
| 1869 | |
| 1870 | pub fn open_picker(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 1871 | if self.editing.is_some() { |
| 1872 | return; |
| 1873 | } |
| 1874 | let receiver = cx.prompt_for_paths(PathPromptOptions { |
| 1875 | files: true, |
| 1876 | directories: false, |
| 1877 | multiple: true, |
| 1878 | prompt: None, |
| 1879 | }); |
| 1880 | cx.spawn_in(window, async move |this, cx| { |
| 1881 | if let Ok(Ok(Some(paths))) = receiver.await { |
| 1882 | this.update_in(cx, |this, window, cx| this.add_paths(paths, window, cx)) |
| 1883 | .ok(); |
| 1884 | } |
| 1885 | }) |
| 1886 | .detach(); |
| 1887 | } |
| 1888 | |
| 1889 | pub fn add_paths(&mut self, paths: Vec<PathBuf>, window: &mut Window, cx: &mut Context<Self>) { |
| 1890 | if self.editing.is_some() || paths.is_empty() { |
| 1891 | return; |
| 1892 | } |
| 1893 | let ids = self.add_pending(&names_of(&paths), cx); |
| 1894 | for (id, path) in ids.into_iter().zip(paths) { |
| 1895 | cx.spawn_in(window, async move |this, cx| { |
| 1896 | let loaded = cx |
| 1897 | .background_executor() |
| 1898 | .spawn(async move { read_attachment(&path) }) |
| 1899 | .await; |
| 1900 | this.update_in(cx, |this, window, cx| { |
| 1901 | this.finish_reading(id, loaded, window, cx) |
| 1902 | }) |
| 1903 | .ok(); |
| 1904 | }) |
| 1905 | .detach(); |
| 1906 | } |
| 1907 | cx.notify(); |
| 1908 | } |
| 1909 | |
| 1910 | /// Images that no longer sit in the text give way to new attachments. |
| 1911 | fn make_room(&mut self, needed: usize, cx: &App) { |
| 1912 | if self.tray.items().len() + needed > MAX_ATTACHMENTS { |
| 1913 | let referenced: Vec<u64> = self |
| 1914 | .input |
| 1915 | .read(cx) |
| 1916 | .tokens() |
| 1917 | .iter() |
| 1918 | .filter_map(|span| image_id(span.token())) |
| 1919 | .collect(); |
| 1920 | self.tray.discard_images_except(&referenced); |
| 1921 | } |
| 1922 | } |
| 1923 | |
| 1924 | fn add_pending(&mut self, names: &[String], cx: &App) -> Vec<u64> { |
| 1925 | self.make_room(names.len(), cx); |
| 1926 | let ids = self.tray.add_pending(names); |
| 1927 | let anchor = self.selection(cx).end; |
| 1928 | self.pending_anchors |
| 1929 | .extend(ids.iter().map(|id| (*id, anchor))); |
| 1930 | ids |
| 1931 | } |
| 1932 | |
| 1933 | fn add_pasted_image(&mut self, pasted: Image, window: &mut Window, cx: &mut Context<Self>) { |
| 1934 | let ids = self.add_pending(&[pasted_image_name()], cx); |
| 1935 | cx.notify(); |
| 1936 | let Some(&id) = ids.first() else { |
| 1937 | return; |
| 1938 | }; |
| 1939 | cx.spawn_in(window, async move |this, cx| { |
| 1940 | let loaded = cx |
| 1941 | .background_executor() |
| 1942 | .spawn(async move { prepare_pasted_image(pasted) }) |
| 1943 | .await; |
| 1944 | this.update_in(cx, |this, window, cx| { |
| 1945 | this.finish_reading(id, loaded, window, cx) |
| 1946 | }) |
| 1947 | .ok(); |
| 1948 | }) |
| 1949 | .detach(); |
| 1950 | } |
| 1951 | |
| 1952 | fn on_fun_pick( |
| 1953 | &mut self, |
| 1954 | _: &Entity<FunPicker>, |
| 1955 | event: &FunPickerEvent, |
| 1956 | window: &mut Window, |
| 1957 | cx: &mut Context<Self>, |
| 1958 | ) { |
| 1959 | match event { |
| 1960 | FunPickerEvent::Emoji(glyph) => self.insert_emoji(glyph, window, cx), |
| 1961 | FunPickerEvent::Image(image) => self.insert_remote_image(image.clone(), window, cx), |
| 1962 | } |
| 1963 | } |
| 1964 | |
| 1965 | fn insert_emoji(&mut self, glyph: &str, window: &mut Window, cx: &mut Context<Self>) { |
| 1966 | let range = self.selection(cx); |
| 1967 | let cursor = range.end; |
| 1968 | self.replace_text(range, glyph, cursor, window, cx); |
| 1969 | self.remember_emoji(glyph, cx); |
| 1970 | cx.notify(); |
| 1971 | } |
| 1972 | |
| 1973 | fn insert_remote_image( |
| 1974 | &mut self, |
| 1975 | image: RemoteImage, |
| 1976 | window: &mut Window, |
| 1977 | cx: &mut Context<Self>, |
| 1978 | ) { |
| 1979 | self.make_room(1, cx); |
| 1980 | if let Some(id) = self.tray.add_remote(image) { |
| 1981 | self.insert_image_token(id, window, cx); |
| 1982 | } |
| 1983 | self.focus(window, cx); |
| 1984 | cx.notify(); |
| 1985 | } |
| 1986 | |
| 1987 | fn paste(&mut self, item: &ClipboardItem, window: &mut Window, cx: &mut Context<Self>) -> bool { |
| 1988 | if self.editing.is_none() { |
| 1989 | match paste_action(item) { |
| 1990 | PasteAction::Text => {} |
| 1991 | PasteAction::Files(paths) => { |
| 1992 | self.add_paths(paths, window, cx); |
| 1993 | return true; |
| 1994 | } |
| 1995 | PasteAction::Image(pasted) => { |
| 1996 | self.add_pasted_image(pasted, window, cx); |
| 1997 | return true; |
| 1998 | } |
| 1999 | } |
| 2000 | } |
| 2001 | let Some(text) = item.text() else { |
| 2002 | return false; |
| 2003 | }; |
| 2004 | if text.contains([OBJECT_MARK, TABLE_MARK]) { |
| 2005 | let cleaned = text.replace([OBJECT_MARK, TABLE_MARK], ""); |
| 2006 | self.input |
| 2007 | .update(cx, |state, cx| state.replace(cleaned, window, cx)); |
| 2008 | return true; |
| 2009 | } |
| 2010 | if self.draft.in_code(self.selection(cx).start) { |
| 2011 | return false; |
| 2012 | } |
| 2013 | if let Some(lines) = self.copied_lines(item, &text) { |
| 2014 | let cursor = self.draft.insert_lines(self.selection(cx), &lines); |
| 2015 | self.commit(Some(cursor), window, cx); |
| 2016 | return true; |
| 2017 | } |
| 2018 | self.pasted_markdown = has_markdown(&text).then_some(text); |
| 2019 | false |
| 2020 | } |
| 2021 | |
| 2022 | fn finish_reading( |
| 2023 | &mut self, |
| 2024 | id: u64, |
| 2025 | loaded: Result<LoadedFile, String>, |
| 2026 | window: &mut Window, |
| 2027 | cx: &mut Context<Self>, |
| 2028 | ) { |
| 2029 | match loaded { |
| 2030 | Ok(loaded) => { |
| 2031 | let files_allowed = self.files_allowed(); |
| 2032 | let job = self.tray.finish_reading(id, loaded, files_allowed); |
| 2033 | if self.tray.image(id).is_some() { |
| 2034 | self.insert_image_token(id, window, cx); |
| 2035 | } else { |
| 2036 | self.pending_anchors.remove(&id); |
| 2037 | } |
| 2038 | if let Some(job) = job { |
| 2039 | self.start_upload(job, cx); |
| 2040 | } |
| 2041 | } |
| 2042 | Err(message) => { |
| 2043 | self.pending_anchors.remove(&id); |
| 2044 | self.tray.fail_reading(id, message); |
| 2045 | } |
| 2046 | } |
| 2047 | cx.notify(); |
| 2048 | } |
| 2049 | |
| 2050 | fn start_upload(&mut self, job: UploadJob, cx: &mut Context<Self>) { |
| 2051 | let Some(conversation_id) = self.conversation_id.clone().filter(|id| !id.is_empty()) else { |
| 2052 | self.tray.finish_upload(job.id, UploadResult::Failed); |
| 2053 | return; |
| 2054 | }; |
| 2055 | let state = self.app.read(cx); |
| 2056 | let (demo, engine) = (state.mode.demo, state.engine.clone()); |
| 2057 | if demo { |
| 2058 | self.simulate_upload(job, cx); |
| 2059 | return; |
| 2060 | } |
| 2061 | let Some(engine) = engine else { |
| 2062 | self.tray.finish_upload(job.id, UploadResult::Failed); |
| 2063 | return; |
| 2064 | }; |
| 2065 | let id = job.id; |
| 2066 | let (event_sender, mut event_receiver) = |
| 2067 | tokio::sync::mpsc::unbounded_channel::<UploadEvent>(); |
| 2068 | let finished_sender = event_sender.clone(); |
| 2069 | let (_, handle) = runtime::spawn_abortable(async move { |
| 2070 | let result = async { |
| 2071 | let uploaded = match job.kind { |
| 2072 | JobKind::Upload => { |
| 2073 | let progress_sender = event_sender.clone(); |
| 2074 | let uploaded = engine |
| 2075 | .upload_attachment_file( |
| 2076 | &conversation_id, |
| 2077 | &job.name, |
| 2078 | &job.bytes, |
| 2079 | move |percent| { |
| 2080 | let _ = progress_sender.send(UploadEvent::Progress(percent)); |
| 2081 | }, |
| 2082 | ) |
| 2083 | .await; |
| 2084 | match uploaded { |
| 2085 | Ok(uploaded) => { |
| 2086 | let _ = event_sender.send(UploadEvent::Uploaded(uploaded.clone())); |
| 2087 | uploaded |
| 2088 | } |
| 2089 | Err(_) => return UploadResult::Failed, |
| 2090 | } |
| 2091 | } |
| 2092 | JobKind::Share(uploaded) => uploaded, |
| 2093 | }; |
| 2094 | let Some(reference) = uploaded.reference() else { |
| 2095 | return UploadResult::Failed; |
| 2096 | }; |
| 2097 | match engine.share_attachment(&conversation_id, &uploaded).await { |
| 2098 | Ok(()) => UploadResult::Done(DoneFile { |
| 2099 | reference, |
| 2100 | uploaded: Some(uploaded), |
| 2101 | }), |
| 2102 | Err(_) => UploadResult::ShareFailed(uploaded), |
| 2103 | } |
| 2104 | } |
| 2105 | .await; |
| 2106 | let _ = finished_sender.send(UploadEvent::Finished(result)); |
| 2107 | }); |
| 2108 | self.uploads.insert(id, UploadHandle::Network(handle)); |
| 2109 | cx.spawn(async move |this, cx| { |
| 2110 | while let Some(event) = event_receiver.recv().await { |
| 2111 | let updated = this.update(cx, |this, cx| { |
| 2112 | let orphan = match event { |
| 2113 | UploadEvent::Progress(percent) => { |
| 2114 | this.tray.set_progress(id, percent); |
| 2115 | None |
| 2116 | } |
| 2117 | UploadEvent::Uploaded(uploaded) => this.tray.mark_uploaded(id, uploaded), |
| 2118 | UploadEvent::Finished(result) => this.finish_upload(id, result), |
| 2119 | }; |
| 2120 | this.discard_uploaded(orphan.into_iter().collect(), cx); |
| 2121 | cx.notify(); |
| 2122 | }); |
| 2123 | if updated.is_err() { |
| 2124 | break; |
| 2125 | } |
| 2126 | } |
| 2127 | }) |
| 2128 | .detach(); |
| 2129 | } |
| 2130 | |
| 2131 | fn simulate_upload(&mut self, job: UploadJob, cx: &mut Context<Self>) { |
| 2132 | let id = job.id; |
| 2133 | let fails = job.name.to_lowercase().contains("fail") && self.demo_failed.insert(id); |
| 2134 | let task = cx.spawn(async move |this, cx| { |
| 2135 | for step in 1..=DEMO_UPLOAD_STEPS { |
| 2136 | cx.background_executor().timer(DEMO_UPLOAD_STEP).await; |
| 2137 | if fails && step == DEMO_FAILURE_STEP { |
| 2138 | this.update(cx, |this, cx| { |
| 2139 | this.finish_upload_in_demo(id, UploadResult::Failed, cx) |
| 2140 | }) |
| 2141 | .ok(); |
| 2142 | return; |
| 2143 | } |
| 2144 | let percent = (u32::from(step) * 100 / u32::from(DEMO_UPLOAD_STEPS)) as u8; |
| 2145 | this.update(cx, |this, cx| this.set_progress(id, percent, cx)) |
| 2146 | .ok(); |
| 2147 | } |
| 2148 | let reference = FileReference { |
| 2149 | attachment_id: format!("demo-attachment-{id}"), |
| 2150 | content_url: format!("https://demo.invalid/files/{}", job.name), |
| 2151 | name: job.name, |
| 2152 | }; |
| 2153 | let done = DoneFile { |
| 2154 | reference, |
| 2155 | uploaded: None, |
| 2156 | }; |
| 2157 | this.update(cx, |this, cx| { |
| 2158 | this.finish_upload_in_demo(id, UploadResult::Done(done), cx) |
| 2159 | }) |
| 2160 | .ok(); |
| 2161 | }); |
| 2162 | self.uploads.insert(id, UploadHandle::Demo(task)); |
| 2163 | } |
| 2164 | |
| 2165 | fn set_progress(&mut self, id: u64, percent: u8, cx: &mut Context<Self>) { |
| 2166 | self.tray.set_progress(id, percent); |
| 2167 | cx.notify(); |
| 2168 | } |
| 2169 | |
| 2170 | fn finish_upload(&mut self, id: u64, result: UploadResult) -> Option<UploadedFile> { |
| 2171 | self.uploads.remove(&id); |
| 2172 | self.tray.finish_upload(id, result) |
| 2173 | } |
| 2174 | |
| 2175 | fn finish_upload_in_demo(&mut self, id: u64, result: UploadResult, cx: &mut Context<Self>) { |
| 2176 | self.finish_upload(id, result); |
| 2177 | cx.notify(); |
| 2178 | } |
| 2179 | |
| 2180 | fn remove_attachment(&mut self, id: u64, _: &mut Window, cx: &mut Context<Self>) { |
| 2181 | if let Some(handle) = self.uploads.remove(&id) { |
| 2182 | handle.cancel(); |
| 2183 | } |
| 2184 | let uploaded = self.tray.remove(id); |
| 2185 | self.discard_uploaded(uploaded.into_iter().collect(), cx); |
| 2186 | self.schedule_draft_save(cx); |
| 2187 | cx.notify(); |
| 2188 | } |
| 2189 | |
| 2190 | fn retry_attachment(&mut self, id: u64, _: &mut Window, cx: &mut Context<Self>) { |
| 2191 | if let Some(job) = self.tray.retry(id) { |
| 2192 | self.start_upload(job, cx); |
| 2193 | } |
| 2194 | cx.notify(); |
| 2195 | } |
| 2196 | |
| 2197 | pub fn submit_current(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 2198 | if self.scheduling.is_some() { |
| 2199 | return; |
| 2200 | } |
| 2201 | let Some(outgoing) = self.outgoing(cx) else { |
| 2202 | return; |
| 2203 | }; |
| 2204 | self.typing.reset(); |
| 2205 | self.load_draft(Draft::default(), InputContent::new(""), window, cx); |
| 2206 | self.mention_inputs.clear(); |
| 2207 | if self.editing.is_none() { |
| 2208 | self.tray.clear(); |
| 2209 | self.clear_stored_draft(cx); |
| 2210 | } |
| 2211 | self.reply = None; |
| 2212 | self.editing = None; |
| 2213 | self.link.reset(); |
| 2214 | self.close_popup(); |
| 2215 | self.reset_importance(); |
| 2216 | cx.emit(ComposerEvent::Submit(Box::new(outgoing))); |
| 2217 | cx.notify(); |
| 2218 | } |
| 2219 | |
| 2220 | fn with_mention_tokens( |
| 2221 | &mut self, |
| 2222 | mut content: InputContent, |
| 2223 | draft: &Draft, |
| 2224 | mentions: &[MentionInput], |
| 2225 | ) -> InputContent { |
| 2226 | let text = draft.text(); |
| 2227 | let mut cursor = 0; |
| 2228 | for mention in mentions { |
| 2229 | let Some(range) = teams_core::plain_mention_spans(draft, &mention.text, false) |
| 2230 | .into_iter() |
| 2231 | .find(|span| span.start >= cursor) |
| 2232 | else { |
| 2233 | continue; |
| 2234 | }; |
| 2235 | self.mention_counter += 1; |
| 2236 | let token_id = format!("mention-{}", self.mention_counter); |
| 2237 | let token = InlineToken::new(token_id.clone(), text[range.clone()].to_owned()); |
| 2238 | if let Ok(next) = content.clone().with_token(range.clone(), token) { |
| 2239 | content = next; |
| 2240 | self.mention_inputs.insert(token_id, mention.clone()); |
| 2241 | cursor = range.end; |
| 2242 | } |
| 2243 | } |
| 2244 | content |
| 2245 | } |
| 2246 | |
| 2247 | pub fn restore(&mut self, outgoing: &Outgoing, window: &mut Window, cx: &mut Context<Self>) { |
| 2248 | self.mention_inputs.clear(); |
| 2249 | let image_ids = if outgoing.edit.is_none() { |
| 2250 | self.cancel_uploads(); |
| 2251 | self.tray.restore(&outgoing.images, &outgoing.files) |
| 2252 | } else { |
| 2253 | Vec::new() |
| 2254 | }; |
| 2255 | let text = outgoing.draft.text(); |
| 2256 | let mut content = InputContent::new(text.to_owned()); |
| 2257 | for ((offset, _), id) in text.match_indices(OBJECT_MARK).zip(image_ids) { |
| 2258 | let range = offset..offset + OBJECT_MARK.len_utf8(); |
| 2259 | if let Ok(next) = content.clone().with_token(range, image_token(id)) { |
| 2260 | content = next; |
| 2261 | } |
| 2262 | } |
| 2263 | let content = self.with_mention_tokens(content, &outgoing.draft, &outgoing.mentions); |
| 2264 | self.load_draft(outgoing.draft.clone(), content, window, cx); |
| 2265 | self.reply = outgoing.reply.clone(); |
| 2266 | self.editing = outgoing.edit.clone(); |
| 2267 | self.link.restore(outgoing.link_preview.clone()); |
| 2268 | self.close_popup(); |
| 2269 | self.schedule_draft_save(cx); |
| 2270 | cx.notify(); |
| 2271 | } |
| 2272 | |
| 2273 | /// Text and mentions replace the draft; images join the tray, where oversized ones upload as files. |
| 2274 | pub fn load_proposal( |
| 2275 | &mut self, |
| 2276 | text: &str, |
| 2277 | mentions: &[MentionInput], |
| 2278 | pictures: Vec<(String, Vec<u8>)>, |
| 2279 | window: &mut Window, |
| 2280 | cx: &mut Context<Self>, |
| 2281 | ) { |
| 2282 | let draft = Draft::from_markdown(text); |
| 2283 | self.mention_inputs.clear(); |
| 2284 | let content = |
| 2285 | self.with_mention_tokens(InputContent::new(draft.text().to_owned()), &draft, mentions); |
| 2286 | self.load_draft(draft, content, window, cx); |
| 2287 | self.update_emoji(cx); |
| 2288 | self.update_mention(cx); |
| 2289 | self.update_link_preview(cx); |
| 2290 | for (name, bytes) in pictures { |
| 2291 | let ids = self.add_pending(std::slice::from_ref(&name), cx); |
| 2292 | let Some(&id) = ids.first() else { |
| 2293 | continue; |
| 2294 | }; |
| 2295 | let loaded = LoadedFile { |
| 2296 | file_name: Some(name), |
| 2297 | ..LoadedFile::new(bytes) |
| 2298 | }; |
| 2299 | self.finish_reading(id, Ok(loaded), window, cx); |
| 2300 | } |
| 2301 | cx.notify(); |
| 2302 | } |
| 2303 | |
| 2304 | /// Markdown in `text` arrives formatted. |
| 2305 | pub fn set_text(&mut self, text: &str, window: &mut Window, cx: &mut Context<Self>) { |
| 2306 | let draft = Draft::from_markdown(text); |
| 2307 | let content = InputContent::new(draft.text().to_owned()); |
| 2308 | self.load_draft(draft, content, window, cx); |
| 2309 | self.mention_inputs.clear(); |
| 2310 | self.update_emoji(cx); |
| 2311 | self.update_mention(cx); |
| 2312 | self.update_link_preview(cx); |
| 2313 | cx.notify(); |
| 2314 | } |
| 2315 | |
| 2316 | pub fn is_empty(&self, cx: &App) -> bool { |
| 2317 | self.draft.is_blank() && !self.has_attachments(cx) |
| 2318 | } |
| 2319 | |
| 2320 | pub fn focus(&self, window: &mut Window, cx: &mut Context<Self>) { |
| 2321 | self.input.update(cx, |state, cx| state.focus(window, cx)); |
| 2322 | } |
| 2323 | |
| 2324 | fn render_popup(&self, cx: &mut Context<Self>) -> Option<AnyElement> { |
| 2325 | let popup = self |
| 2326 | .popup |
| 2327 | .as_ref() |
| 2328 | .filter(|popup| !popup.candidates.is_empty())?; |
| 2329 | let directory = &self.app.read(cx).directory; |
| 2330 | let rows = popup |
| 2331 | .candidates |
| 2332 | .iter() |
| 2333 | .enumerate() |
| 2334 | .map(|(index, candidate)| { |
| 2335 | let name = candidate.display_name().to_owned(); |
| 2336 | let avatar = match candidate { |
| 2337 | MentionCandidate::Person(person) => { |
| 2338 | person_avatar(directory, Some(&person.user_id), &name, 28.) |
| 2339 | } |
| 2340 | other => square_avatar(&name, candidate_key(other), 28., 7.).into_any_element(), |
| 2341 | }; |
| 2342 | let subtitle = candidate_subtitle(candidate); |
| 2343 | h_flex() |
| 2344 | .id(ElementId::Name(format!("mention-row-{index}").into())) |
| 2345 | .mx(px(6.)) |
| 2346 | .px(px(6.)) |
| 2347 | .h(px(POPUP_ROW_HEIGHT)) |
| 2348 | .flex_none() |
| 2349 | .gap(px(8.)) |
| 2350 | .items_center() |
| 2351 | .rounded(px(6.)) |
| 2352 | .cursor_pointer() |
| 2353 | .when(index == popup.highlighted, |row| { |
| 2354 | row.bg(theme::popup_hover()) |
| 2355 | }) |
| 2356 | .hover(|row| row.bg(theme::popup_hover())) |
| 2357 | .child(avatar) |
| 2358 | .child( |
| 2359 | v_flex() |
| 2360 | .flex_1() |
| 2361 | .min_w_0() |
| 2362 | .child( |
| 2363 | div() |
| 2364 | .truncate() |
| 2365 | .text_size(px(13.5)) |
| 2366 | .text_color(theme::text()) |
| 2367 | .child(name), |
| 2368 | ) |
| 2369 | .when(!subtitle.is_empty(), |column| { |
| 2370 | column.child( |
| 2371 | div() |
| 2372 | .truncate() |
| 2373 | .text_size(px(12.)) |
| 2374 | .text_color(theme::text_muted()) |
| 2375 | .child(subtitle), |
| 2376 | ) |
| 2377 | }), |
| 2378 | ) |
| 2379 | .on_click(cx.listener(move |this, _, window, cx| { |
| 2380 | if let Some(popup) = this.popup.as_mut() { |
| 2381 | popup.highlighted = index; |
| 2382 | } |
| 2383 | this.accept_mention(window, cx); |
| 2384 | })) |
| 2385 | }); |
| 2386 | Some( |
| 2387 | v_flex() |
| 2388 | .id("mention-popup") |
| 2389 | .absolute() |
| 2390 | .bottom(relative(1.)) |
| 2391 | .left_0() |
| 2392 | .mb(px(6.)) |
| 2393 | .w(px(POPUP_WIDTH)) |
| 2394 | .py(px(6.)) |
| 2395 | .rounded(px(10.)) |
| 2396 | .flyout_surface() |
| 2397 | .occlude() |
| 2398 | .children(rows) |
| 2399 | .into_any_element(), |
| 2400 | ) |
| 2401 | } |
| 2402 | |
| 2403 | fn render_emoji_popup( |
| 2404 | &self, |
| 2405 | window: &mut Window, |
| 2406 | cx: &mut Context<Self>, |
| 2407 | ) -> Option<AnyElement> { |
| 2408 | let popup = self.emoji_popup.as_ref()?; |
| 2409 | let colon = popup.range.start..popup.range.start + 1; |
| 2410 | let anchor = self |
| 2411 | .input |
| 2412 | .read(cx) |
| 2413 | .range_to_bounds(&colon) |
| 2414 | .map(|bounds| bounds.origin); |
| 2415 | if anchor.is_none() { |
| 2416 | window.request_animation_frame(); |
| 2417 | } |
| 2418 | let composer = cx.entity().downgrade(); |
| 2419 | Some(emoji_popup::render( |
| 2420 | popup, |
| 2421 | anchor, |
| 2422 | move |index, window, cx| { |
| 2423 | composer |
| 2424 | .update(cx, |this, cx| { |
| 2425 | if let Some(popup) = this.emoji_popup.as_mut() { |
| 2426 | popup.highlighted = index; |
| 2427 | } |
| 2428 | this.accept_emoji(window, cx); |
| 2429 | }) |
| 2430 | .ok(); |
| 2431 | }, |
| 2432 | )) |
| 2433 | } |
| 2434 | |
| 2435 | fn render_toolbar(&self, focused: bool, cx: &mut Context<Self>) -> Option<AnyElement> { |
| 2436 | let composer = cx.entity().downgrade(); |
| 2437 | let floating = focused && self.toolbar_requested(cx) && !self.format_bar_open(); |
| 2438 | if self.link_editor.is_none() && !floating { |
| 2439 | return None; |
| 2440 | } |
| 2441 | let state = self.input.read(cx); |
| 2442 | let range = self |
| 2443 | .link_editor |
| 2444 | .as_ref() |
| 2445 | .map_or_else(|| state.selected_range(), |editor| editor.range.clone()); |
| 2446 | let start = state.range_to_bounds(&(range.start..range.start))?; |
| 2447 | let end = state.range_to_bounds(&(range.end..range.end))?; |
| 2448 | let input_bounds = state.input_bounds(); |
| 2449 | let center_x = if start.origin.y == end.origin.y { |
| 2450 | (start.origin.x + end.origin.x) / 2. |
| 2451 | } else { |
| 2452 | input_bounds.center().x |
| 2453 | }; |
| 2454 | let edges = self |
| 2455 | .composer_bounds |
| 2456 | .get() |
| 2457 | .map_or((input_bounds.left(), input_bounds.right()), |bounds| { |
| 2458 | (bounds.left(), bounds.right()) |
| 2459 | }); |
| 2460 | let width = match self.link_editor { |
| 2461 | Some(_) => format_toolbar::link_width(), |
| 2462 | None => format_toolbar::bar_width(), |
| 2463 | }; |
| 2464 | let placement = format_toolbar::place(center_x, start.origin.y, edges, width); |
| 2465 | let element = match &self.link_editor { |
| 2466 | Some(editor) => format_toolbar::render( |
| 2467 | placement, |
| 2468 | format_toolbar::Mode::Link { |
| 2469 | field: &editor.field, |
| 2470 | refused: editor.refused, |
| 2471 | on_apply: Rc::new(move |window, cx| { |
| 2472 | composer |
| 2473 | .update(cx, |this, cx| this.apply_link(window, cx)) |
| 2474 | .ok(); |
| 2475 | }), |
| 2476 | }, |
| 2477 | ), |
| 2478 | None => { |
| 2479 | let state_of = |button| ButtonState::enabled(self.format_state(button, &range)); |
| 2480 | format_toolbar::render( |
| 2481 | placement, |
| 2482 | format_toolbar::Mode::Bar { |
| 2483 | state: &state_of, |
| 2484 | on_press: Rc::new(move |button, window, cx| { |
| 2485 | composer |
| 2486 | .update(cx, |this, cx| this.press_format(button, window, cx)) |
| 2487 | .ok(); |
| 2488 | }), |
| 2489 | }, |
| 2490 | ) |
| 2491 | } |
| 2492 | }; |
| 2493 | Some(element) |
| 2494 | } |
| 2495 | |
| 2496 | fn render_format_bar(&self, window: &Window, cx: &mut Context<Self>) -> AnyElement { |
| 2497 | let selection = self.selection(cx); |
| 2498 | let in_table = self.in_table(window, cx); |
| 2499 | let state_of = |button| self.button_state(button, &selection, in_table); |
| 2500 | let composer = cx.weak_entity(); |
| 2501 | let on_press: format_toolbar::PressHandler = Rc::new(move |button, window, cx| { |
| 2502 | composer |
| 2503 | .update(cx, |this, cx| this.press_format(button, window, cx)) |
| 2504 | .ok(); |
| 2505 | }); |
| 2506 | let mut overlay = |button| match button { |
| 2507 | FormatButton::Table => self.render_table_picker(cx), |
| 2508 | FormatButton::TableInsertLine => self.render_table_menu(TableLineMenu::Insert, cx), |
| 2509 | FormatButton::TableRemoveLine => self.render_table_menu(TableLineMenu::Remove, cx), |
| 2510 | _ => None, |
| 2511 | }; |
| 2512 | let row = format_toolbar::render_format_bar(&state_of, on_press, &mut overlay); |
| 2513 | v_flex() |
| 2514 | .debug_selector(|| COMPOSER_FORMAT_BAR_TAG.into()) |
| 2515 | .w_full() |
| 2516 | .child(row.w_full().p(px(COMPOSER_BUTTON_INSET))) |
| 2517 | .child( |
| 2518 | div() |
| 2519 | .debug_selector(|| COMPOSER_FORMAT_DIVIDER_TAG.into()) |
| 2520 | .w_full() |
| 2521 | .h(px(1.)) |
| 2522 | .bg(theme::border()), |
| 2523 | ) |
| 2524 | .into_any_element() |
| 2525 | } |
| 2526 | |
| 2527 | fn render_paste_hint(&self) -> Option<Div> { |
| 2528 | self.paste_hint.as_ref()?; |
| 2529 | Some( |
| 2530 | h_flex() |
| 2531 | .mt(px(6.)) |
| 2532 | .gap(px(6.)) |
| 2533 | .items_center() |
| 2534 | .text_size(px(12.)) |
| 2535 | .text_color(theme::text_muted()) |
| 2536 | .child("Formatted from Markdown") |
| 2537 | .child( |
| 2538 | div() |
| 2539 | .px(px(4.)) |
| 2540 | .py(px(2.)) |
| 2541 | .rounded(px(4.)) |
| 2542 | .bg(theme::surface_raised()) |
| 2543 | .text_size(px(11.)) |
| 2544 | .text_color(theme::text()) |
| 2545 | .child("Ctrl+Z"), |
| 2546 | ) |
| 2547 | .child("plain text"), |
| 2548 | ) |
| 2549 | } |
| 2550 | |
| 2551 | fn render_edit_strip(&self, cx: &mut Context<Self>) -> Option<Div> { |
| 2552 | let edit = self.editing.as_ref()?; |
| 2553 | Some( |
| 2554 | h_flex() |
| 2555 | .w_full() |
| 2556 | .h(px(52.)) |
| 2557 | .mb(px(8.)) |
| 2558 | .gap(px(12.)) |
| 2559 | .items_center() |
| 2560 | .px(px(12.)) |
| 2561 | .rounded(px(10.)) |
| 2562 | .bg(theme::row_hover()) |
| 2563 | .child(match edit.scheduled { |
| 2564 | Some(_) => { |
| 2565 | symbol("schedule_send", 14., theme::accent_text()).into_any_element() |
| 2566 | } |
| 2567 | None => icon(IconName::Pencil, 14., theme::accent_text()).into_any_element(), |
| 2568 | }) |
| 2569 | .child( |
| 2570 | v_flex() |
| 2571 | .flex_1() |
| 2572 | .min_w_0() |
| 2573 | .child( |
| 2574 | div() |
| 2575 | .truncate() |
| 2576 | .text_size(px(12.)) |
| 2577 | .font_weight(FontWeight::SEMIBOLD) |
| 2578 | .text_color(theme::accent_text()) |
| 2579 | .child(match edit.scheduled { |
| 2580 | Some(send_at) => format!( |
| 2581 | "Editing scheduled message - {}", |
| 2582 | scheduled_label(send_at, Utc::now()) |
| 2583 | ), |
| 2584 | None => "Editing message".to_owned(), |
| 2585 | }), |
| 2586 | ) |
| 2587 | .child( |
| 2588 | div() |
| 2589 | .truncate() |
| 2590 | .text_size(px(12.)) |
| 2591 | .text_color(theme::text_muted()) |
| 2592 | .child(edit.excerpt.clone()), |
| 2593 | ), |
| 2594 | ) |
| 2595 | .child( |
| 2596 | div() |
| 2597 | .id("edit-close") |
| 2598 | .size(px(28.)) |
| 2599 | .flex_none() |
| 2600 | .flex() |
| 2601 | .items_center() |
| 2602 | .justify_center() |
| 2603 | .rounded(px(7.)) |
| 2604 | .cursor_pointer() |
| 2605 | .hover(|button| button.bg(theme::row_hover())) |
| 2606 | .child(icon(IconName::Close, 14., theme::text_muted())) |
| 2607 | .on_click(cx.listener(|this, _, window, cx| this.cancel_edit(window, cx))), |
| 2608 | ), |
| 2609 | ) |
| 2610 | } |
| 2611 | |
| 2612 | fn render_reply_strip(&self, cx: &mut Context<Self>) -> Option<Div> { |
| 2613 | let reply = self.reply.as_ref()?; |
| 2614 | Some( |
| 2615 | h_flex() |
| 2616 | .w_full() |
| 2617 | .h(px(52.)) |
| 2618 | .mb(px(8.)) |
| 2619 | .gap(px(12.)) |
| 2620 | .items_center() |
| 2621 | .px(px(12.)) |
| 2622 | .rounded(px(10.)) |
| 2623 | .bg(theme::row_hover()) |
| 2624 | .child(icon(IconName::Undo2, 14., theme::accent_text())) |
| 2625 | .child( |
| 2626 | v_flex() |
| 2627 | .flex_1() |
| 2628 | .min_w_0() |
| 2629 | .child( |
| 2630 | div() |
| 2631 | .truncate() |
| 2632 | .text_size(px(12.)) |
| 2633 | .font_weight(FontWeight::SEMIBOLD) |
| 2634 | .text_color(theme::accent_text()) |
| 2635 | .child(format!("Replying to {}", reply.author)), |
| 2636 | ) |
| 2637 | .child( |
| 2638 | div() |
| 2639 | .truncate() |
| 2640 | .text_size(px(12.)) |
| 2641 | .text_color(theme::text_muted()) |
| 2642 | .child(reply.excerpt.clone()), |
| 2643 | ), |
| 2644 | ) |
| 2645 | .child( |
| 2646 | div() |
| 2647 | .id("reply-close") |
| 2648 | .size(px(28.)) |
| 2649 | .flex_none() |
| 2650 | .flex() |
| 2651 | .items_center() |
| 2652 | .justify_center() |
| 2653 | .rounded(px(7.)) |
| 2654 | .cursor_pointer() |
| 2655 | .hover(|button| button.bg(theme::row_hover())) |
| 2656 | .child(icon(IconName::Close, 14., theme::text_muted())) |
| 2657 | .on_click(cx.listener(|this, _, _, cx| this.set_reply(None, cx))), |
| 2658 | ), |
| 2659 | ) |
| 2660 | } |
| 2661 | } |
| 2662 | |
| 2663 | impl Render for Composer { |
| 2664 | fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 2665 | let focused = self.input.focus_handle(cx).contains_focused(window, cx); |
| 2666 | let can_send = self.can_send(cx); |
| 2667 | let can_attach = self.editing.is_none(); |
| 2668 | let send = div() |
| 2669 | .id("composer-send") |
| 2670 | .debug_selector(|| COMPOSER_SEND_TAG.into()) |
| 2671 | .size(px(COMPOSER_BUTTON_SIZE)) |
| 2672 | .ml(px(COMPOSER_BUTTON_INSET)) |
| 2673 | .flex_none() |
| 2674 | .flex() |
| 2675 | .items_center() |
| 2676 | .justify_center() |
| 2677 | .rounded(px(8.)) |
| 2678 | .child(symbol("keyboard_return", 20., theme::text())) |
| 2679 | .on_mouse_down( |
| 2680 | MouseButton::Right, |
| 2681 | cx.listener(|this, _, _, cx| this.open_schedule_menu(cx)), |
| 2682 | ) |
| 2683 | .when(!can_send, |button| button.opacity(0.4)) |
| 2684 | .when(can_send, |button| { |
| 2685 | button |
| 2686 | .cursor_pointer() |
| 2687 | .hover(|button| button.bg(theme::popup_hover())) |
| 2688 | .on_click(cx.listener(|this, _, window, cx| this.submit_current(window, cx))) |
| 2689 | }); |
| 2690 | let fun = can_attach.then(|| { |
| 2691 | let picker = self.fun_picker.clone(); |
| 2692 | let reset_picker = self.fun_picker.clone(); |
| 2693 | Popover::new("composer-fun-popover") |
| 2694 | .anchor(Anchor::BottomLeft) |
| 2695 | .offset(px(10.)) |
| 2696 | .p_0() |
| 2697 | .trigger( |
| 2698 | Button::new("composer-fun") |
| 2699 | .custom( |
| 2700 | ButtonCustomVariant::new(cx) |
| 2701 | .hover(theme::popup_hover()) |
| 2702 | .active(theme::popup_hover()), |
| 2703 | ) |
| 2704 | .rounded(px(8.)) |
| 2705 | .with_size(px(COMPOSER_ICON_SIZE / BUTTON_ICON_SCALE)) |
| 2706 | .size(px(COMPOSER_BUTTON_SIZE)) |
| 2707 | .tooltip("Emoji, GIFs und Sticker") |
| 2708 | .icon(symbol("mood", COMPOSER_ICON_SIZE, theme::text_muted())), |
| 2709 | ) |
| 2710 | .on_open_change(move |open, window, cx| { |
| 2711 | if *open { |
| 2712 | reset_picker.update(cx, |picker, cx| picker.reset(window, cx)); |
| 2713 | } |
| 2714 | }) |
| 2715 | .content(move |_, _, cx| { |
| 2716 | let popover = cx.entity().downgrade(); |
| 2717 | picker.update(cx, |picker, _| picker.set_popover(popover)); |
| 2718 | picker.clone() |
| 2719 | }) |
| 2720 | }); |
| 2721 | let attach = can_attach.then(|| { |
| 2722 | div() |
| 2723 | .id("composer-attach") |
| 2724 | .size(px(COMPOSER_BUTTON_SIZE)) |
| 2725 | .flex_none() |
| 2726 | .flex() |
| 2727 | .items_center() |
| 2728 | .justify_center() |
| 2729 | .rounded(px(8.)) |
| 2730 | .cursor_pointer() |
| 2731 | .hover(|button| button.bg(theme::popup_hover())) |
| 2732 | .tooltip(|window, cx| Tooltip::new("Attach file").build(window, cx)) |
| 2733 | .child(symbol( |
| 2734 | "attach_file", |
| 2735 | COMPOSER_ICON_SIZE, |
| 2736 | theme::text_muted(), |
| 2737 | )) |
| 2738 | .on_click(cx.listener(|this, _, window, cx| this.open_picker(window, cx))) |
| 2739 | }); |
| 2740 | let format_open = self.format_bar_open(); |
| 2741 | let format_toggle = div() |
| 2742 | .id("composer-format") |
| 2743 | .debug_selector(|| COMPOSER_FORMAT_TAG.into()) |
| 2744 | .size(px(COMPOSER_BUTTON_SIZE)) |
| 2745 | .flex_none() |
| 2746 | .flex() |
| 2747 | .items_center() |
| 2748 | .justify_center() |
| 2749 | .rounded(px(8.)) |
| 2750 | .cursor_pointer() |
| 2751 | .when(format_open, |button| button.bg(theme::popup_hover())) |
| 2752 | .hover(|button| button.bg(theme::popup_hover())) |
| 2753 | .tooltip(move |window, cx| { |
| 2754 | Tooltip::new(format_toolbar::toggle_tooltip(format_open)).build(window, cx) |
| 2755 | }) |
| 2756 | .child(symbol( |
| 2757 | format_toolbar::toggle_symbol(format_open), |
| 2758 | COMPOSER_ICON_SIZE, |
| 2759 | if format_open { |
| 2760 | theme::accent_soft() |
| 2761 | } else { |
| 2762 | theme::text_muted() |
| 2763 | }, |
| 2764 | )) |
| 2765 | .on_mouse_down(MouseButton::Left, |_, window, cx| { |
| 2766 | window.prevent_default(); |
| 2767 | cx.stop_propagation(); |
| 2768 | }) |
| 2769 | .on_click(cx.listener(|this, _, window, cx| this.toggle_format_bar(window, cx))); |
| 2770 | let format_bar = format_open.then(|| self.render_format_bar(window, cx)); |
| 2771 | let composer = cx.weak_entity(); |
| 2772 | let tokens_composer = composer.clone(); |
| 2773 | let input = Textarea::new(&self.input) |
| 2774 | .line_box_height(px(COMPOSER_BUTTON_SIZE)) |
| 2775 | .appearance(false) |
| 2776 | .bordered(false) |
| 2777 | .on_paste(move |item, window, cx| { |
| 2778 | composer |
| 2779 | .update(cx, |this, cx| this.paste(item, window, cx)) |
| 2780 | .unwrap_or(false) |
| 2781 | }) |
| 2782 | .token(move |context, _, cx| { |
| 2783 | if context.is_block() && is_table_token(context.token()) { |
| 2784 | let token_id = context.token().id().to_string(); |
| 2785 | let width = context.available_width(); |
| 2786 | let selected = context.is_selected(); |
| 2787 | if let Some(table) = tokens_composer |
| 2788 | .read_with(cx, |this, _| { |
| 2789 | this.render_table_token(&token_id, width, selected) |
| 2790 | }) |
| 2791 | .ok() |
| 2792 | .flatten() |
| 2793 | { |
| 2794 | return table; |
| 2795 | } |
| 2796 | } |
| 2797 | if let Some(id) = context |
| 2798 | .is_block() |
| 2799 | .then(|| image_id(context.token())) |
| 2800 | .flatten() |
| 2801 | { |
| 2802 | let preview = tokens_composer |
| 2803 | .read_with(cx, |this, _| this.tray.image(id)) |
| 2804 | .ok() |
| 2805 | .flatten(); |
| 2806 | let remove_composer = tokens_composer.clone(); |
| 2807 | return inline_preview( |
| 2808 | id, |
| 2809 | preview, |
| 2810 | context.available_width(), |
| 2811 | context.is_selected(), |
| 2812 | Rc::new(move |window, cx| { |
| 2813 | remove_composer |
| 2814 | .update(cx, |this, cx| this.remove_image_token(id, window, cx)) |
| 2815 | .ok(); |
| 2816 | }), |
| 2817 | ); |
| 2818 | } |
| 2819 | div() |
| 2820 | .h(context.line_height()) |
| 2821 | .px(px(5.)) |
| 2822 | .rounded(px(5.)) |
| 2823 | .bg(if context.is_selected() { |
| 2824 | theme::accent().opacity(0.4) |
| 2825 | } else { |
| 2826 | theme::mention_background(false) |
| 2827 | }) |
| 2828 | .text_color(theme::mention_text()) |
| 2829 | .font_weight(FontWeight::SEMIBOLD) |
| 2830 | .child(context.token().label().clone()) |
| 2831 | .into_any_element() |
| 2832 | }); |
| 2833 | let tray = can_attach |
| 2834 | .then(|| { |
| 2835 | render_tray( |
| 2836 | &self.tray, |
| 2837 | Self::remove_attachment, |
| 2838 | Self::retry_attachment, |
| 2839 | cx, |
| 2840 | ) |
| 2841 | }) |
| 2842 | .flatten(); |
| 2843 | let notice = self.tray.notice().filter(|_| can_attach).map(|notice| { |
| 2844 | div() |
| 2845 | .w_full() |
| 2846 | .mb(px(6.)) |
| 2847 | .text_size(px(12.)) |
| 2848 | .text_color(theme::amber()) |
| 2849 | .child(notice.to_owned()) |
| 2850 | }); |
| 2851 | let composer_bounds = self.composer_bounds.clone(); |
| 2852 | let single_line = { |
| 2853 | let state = self.input.read(cx); |
| 2854 | state |
| 2855 | .line_height() |
| 2856 | .is_none_or(|line_height| state.input_bounds().size.height < line_height * 2.) |
| 2857 | }; |
| 2858 | let (inset, top, bottom) = if self.inline { |
| 2859 | (0., 0., 0.) |
| 2860 | } else { |
| 2861 | (20., 8., 16.) |
| 2862 | }; |
| 2863 | v_flex() |
| 2864 | .w_full() |
| 2865 | .flex_none() |
| 2866 | .px(px(inset)) |
| 2867 | .pt(px(top)) |
| 2868 | .pb(px(bottom)) |
| 2869 | .key_context(COMPOSER_KEY_CONTEXT) |
| 2870 | .on_action(cx.listener(|this, _: &ToggleBold, window, cx| { |
| 2871 | if this.link_editor.is_none() && !this.cell_has_focus(window, cx) { |
| 2872 | this.toggle_mark(MarkKind::Bold, cx); |
| 2873 | } |
| 2874 | })) |
| 2875 | .on_action(cx.listener(|this, _: &ToggleItalic, window, cx| { |
| 2876 | if this.link_editor.is_none() && !this.cell_has_focus(window, cx) { |
| 2877 | this.toggle_mark(MarkKind::Italic, cx); |
| 2878 | } |
| 2879 | })) |
| 2880 | .on_action(cx.listener(|this, _: &ToggleUnderline, window, cx| { |
| 2881 | if this.link_editor.is_none() && !this.cell_has_focus(window, cx) { |
| 2882 | this.toggle_mark(MarkKind::Underline, cx); |
| 2883 | } |
| 2884 | })) |
| 2885 | .on_action(cx.listener(|this, _: &ToggleStrike, window, cx| { |
| 2886 | if this.link_editor.is_none() && !this.cell_has_focus(window, cx) { |
| 2887 | this.toggle_mark(MarkKind::Strike, cx); |
| 2888 | } |
| 2889 | })) |
| 2890 | .on_action(cx.listener(|this, _: &ToggleSuperscript, window, cx| { |
| 2891 | if this.link_editor.is_none() && !this.cell_has_focus(window, cx) { |
| 2892 | this.toggle_mark(MarkKind::Superscript, cx); |
| 2893 | } |
| 2894 | })) |
| 2895 | .on_action(cx.listener(|this, _: &ToggleSubscript, window, cx| { |
| 2896 | if this.link_editor.is_none() && !this.cell_has_focus(window, cx) { |
| 2897 | this.toggle_mark(MarkKind::Subscript, cx); |
| 2898 | } |
| 2899 | })) |
| 2900 | .on_action(cx.listener(|this, _: &ToggleCode, window, cx| { |
| 2901 | if this.link_editor.is_none() && !this.cell_has_focus(window, cx) { |
| 2902 | this.toggle_mark(MarkKind::Code, cx); |
| 2903 | } |
| 2904 | })) |
| 2905 | .on_action(cx.listener(|this, _: &EditLink, window, cx| { |
| 2906 | if !this.cell_has_focus(window, cx) { |
| 2907 | this.edit_link(window, cx); |
| 2908 | } |
| 2909 | })) |
| 2910 | .on_action( |
| 2911 | cx.listener(|this, _: &PastePlainText, window, cx| this.paste_plain(window, cx)), |
| 2912 | ) |
| 2913 | .on_action(cx.listener(|this, _: &InsertTableRowAbove, window, cx| { |
| 2914 | this.edit_table(TableEdit::InsertRowAbove, window, cx) |
| 2915 | })) |
| 2916 | .on_action(cx.listener(|this, _: &InsertTableRowBelow, window, cx| { |
| 2917 | this.edit_table(TableEdit::InsertRowBelow, window, cx) |
| 2918 | })) |
| 2919 | .on_action(cx.listener(|this, _: &InsertTableColumnLeft, window, cx| { |
| 2920 | this.edit_table(TableEdit::InsertColumnLeft, window, cx) |
| 2921 | })) |
| 2922 | .on_action(cx.listener(|this, _: &InsertTableColumnRight, window, cx| { |
| 2923 | this.edit_table(TableEdit::InsertColumnRight, window, cx) |
| 2924 | })) |
| 2925 | .on_action(cx.listener(|this, _: &DeleteTableRow, window, cx| { |
| 2926 | this.edit_table(TableEdit::DeleteRow, window, cx) |
| 2927 | })) |
| 2928 | .on_action(cx.listener(|this, _: &DeleteTableColumn, window, cx| { |
| 2929 | this.edit_table(TableEdit::DeleteColumn, window, cx) |
| 2930 | })) |
| 2931 | .on_action(cx.listener(|this, _: &DeleteTable, window, cx| { |
| 2932 | this.edit_table(TableEdit::DeleteTable, window, cx) |
| 2933 | })) |
| 2934 | .on_action(cx.listener(|this, _: &ScheduleSend, _, cx| this.open_schedule_picker(cx))) |
| 2935 | .on_action(cx.listener(|this, _: &ToggleFormatBar, window, cx| { |
| 2936 | this.toggle_format_bar(window, cx) |
| 2937 | })) |
| 2938 | .on_action(cx.listener(|this, _: &InsertTableLine, window, cx| { |
| 2939 | this.open_table_menu(TableLineMenu::Insert, window, cx); |
| 2940 | })) |
| 2941 | .on_action(cx.listener(|this, _: &RemoveTableLine, window, cx| { |
| 2942 | this.open_table_menu(TableLineMenu::Remove, window, cx); |
| 2943 | })) |
| 2944 | .capture_action(cx.listener(|this, _: &Undo, window, cx| { |
| 2945 | if this.link_editor.is_none() { |
| 2946 | this.step_history(true, window, cx); |
| 2947 | cx.stop_propagation(); |
| 2948 | } |
| 2949 | })) |
| 2950 | .capture_action(cx.listener(|this, _: &Redo, window, cx| { |
| 2951 | if this.link_editor.is_none() { |
| 2952 | this.step_history(false, window, cx); |
| 2953 | cx.stop_propagation(); |
| 2954 | } |
| 2955 | })) |
| 2956 | .capture_action(cx.listener(|this, _: &Copy, window, cx| { |
| 2957 | if this.link_editor.is_none() |
| 2958 | && !this.cell_has_focus(window, cx) |
| 2959 | && this.copy_selection(cx) |
| 2960 | { |
| 2961 | cx.stop_propagation(); |
| 2962 | } |
| 2963 | })) |
| 2964 | .capture_action(cx.listener(|this, _: &Cut, window, cx| { |
| 2965 | if this.link_editor.is_none() |
| 2966 | && !this.cell_has_focus(window, cx) |
| 2967 | && this.copy_selection(cx) |
| 2968 | { |
| 2969 | this.input |
| 2970 | .update(cx, |state, cx| state.replace("", window, cx)); |
| 2971 | cx.stop_propagation(); |
| 2972 | } |
| 2973 | })) |
| 2974 | .capture_action(cx.listener(|this, _: &MoveUp, window, cx| { |
| 2975 | if let Some(menu) = this.table_menu.as_mut() { |
| 2976 | menu.step(-1); |
| 2977 | cx.notify(); |
| 2978 | cx.stop_propagation(); |
| 2979 | } else if this.cell_has_focus(window, cx) { |
| 2980 | if this.table_key(TableKey::Up, window, cx) { |
| 2981 | cx.stop_propagation(); |
| 2982 | } |
| 2983 | } else if this.link_editor.is_some() { |
| 2984 | } else if this.schedule.is_some() { |
| 2985 | this.move_schedule_highlight(-1, window, cx); |
| 2986 | cx.stop_propagation(); |
| 2987 | } else if this.popup_is_open() { |
| 2988 | this.move_highlight(-1, cx); |
| 2989 | cx.stop_propagation(); |
| 2990 | } else if this.editing.is_none() && this.is_empty(cx) { |
| 2991 | cx.emit(ComposerEvent::EditLast); |
| 2992 | cx.stop_propagation(); |
| 2993 | } |
| 2994 | })) |
| 2995 | .capture_action(cx.listener(|this, _: &MoveDown, window, cx| { |
| 2996 | if let Some(menu) = this.table_menu.as_mut() { |
| 2997 | menu.step(1); |
| 2998 | cx.notify(); |
| 2999 | cx.stop_propagation(); |
| 3000 | } else if this.cell_has_focus(window, cx) { |
| 3001 | if this.table_key(TableKey::Down, window, cx) { |
| 3002 | cx.stop_propagation(); |
| 3003 | } |
| 3004 | } else if this.link_editor.is_some() { |
| 3005 | } else if this.schedule.is_some() { |
| 3006 | this.move_schedule_highlight(1, window, cx); |
| 3007 | cx.stop_propagation(); |
| 3008 | } else if this.popup_is_open() { |
| 3009 | this.move_highlight(1, cx); |
| 3010 | cx.stop_propagation(); |
| 3011 | } else if this.leave_quote(window, cx) { |
| 3012 | cx.stop_propagation(); |
| 3013 | } |
| 3014 | })) |
| 3015 | .capture_action(cx.listener(|this, _: &MoveRight, window, cx| { |
| 3016 | if this.link_editor.is_none() |
| 3017 | && !this.cell_has_focus(window, cx) |
| 3018 | && this.leave_quote(window, cx) |
| 3019 | { |
| 3020 | cx.stop_propagation(); |
| 3021 | } |
| 3022 | })) |
| 3023 | .capture_action(cx.listener(|this, action: &Enter, window, cx| { |
| 3024 | if this.table_menu.is_some() { |
| 3025 | this.accept_table_menu(None, window, cx); |
| 3026 | cx.stop_propagation(); |
| 3027 | } else if this.cell_has_focus(window, cx) { |
| 3028 | } else if this.link_editor.is_some() { |
| 3029 | this.apply_link(window, cx); |
| 3030 | cx.stop_propagation(); |
| 3031 | } else if this.schedule.is_some() { |
| 3032 | this.activate_schedule_row(window, cx); |
| 3033 | cx.stop_propagation(); |
| 3034 | } else if this.popup_is_open() { |
| 3035 | this.accept_popup(window, cx); |
| 3036 | cx.stop_propagation(); |
| 3037 | } else if this.break_line(action.shift, window, cx) { |
| 3038 | cx.stop_propagation(); |
| 3039 | } |
| 3040 | })) |
| 3041 | .capture_action(cx.listener(|this, _: &IndentInline, window, cx| { |
| 3042 | if this.table_key(TableKey::Next, window, cx) { |
| 3043 | cx.stop_propagation(); |
| 3044 | } else if this.link_editor.is_some() { |
| 3045 | } else if this.popup_is_open() { |
| 3046 | this.accept_popup(window, cx); |
| 3047 | cx.stop_propagation(); |
| 3048 | } else if this.indent(false, window, cx) { |
| 3049 | cx.stop_propagation(); |
| 3050 | } |
| 3051 | })) |
| 3052 | .capture_action(cx.listener(|this, _: &OutdentInline, window, cx| { |
| 3053 | if this.table_key(TableKey::Previous, window, cx) |
| 3054 | || (this.link_editor.is_none() && this.indent(true, window, cx)) |
| 3055 | { |
| 3056 | cx.stop_propagation(); |
| 3057 | } |
| 3058 | })) |
| 3059 | .capture_action(cx.listener(|this, _: &Backspace, window, cx| { |
| 3060 | if this.link_editor.is_none() |
| 3061 | && !this.cell_has_focus(window, cx) |
| 3062 | && (this.undo_conversion(window, cx) |
| 3063 | || this.backspace_at_start(window, cx) |
| 3064 | || this.enter_table_beside(true, window, cx)) |
| 3065 | { |
| 3066 | cx.stop_propagation(); |
| 3067 | } |
| 3068 | })) |
| 3069 | .capture_action(cx.listener(|this, _: &Delete, window, cx| { |
| 3070 | if this.link_editor.is_none() |
| 3071 | && !this.cell_has_focus(window, cx) |
| 3072 | && (this.enter_table_beside(false, window, cx) |
| 3073 | || this.delete_forward(window, cx)) |
| 3074 | { |
| 3075 | cx.stop_propagation(); |
| 3076 | } |
| 3077 | })) |
| 3078 | .capture_action(cx.listener(|this, _: &Escape, window, cx| { |
| 3079 | if this.table_picker.take().is_some() || this.table_menu.take().is_some() { |
| 3080 | cx.notify(); |
| 3081 | cx.stop_propagation(); |
| 3082 | return; |
| 3083 | } |
| 3084 | if this.schedule.is_some() { |
| 3085 | this.close_schedule(window, cx); |
| 3086 | cx.stop_propagation(); |
| 3087 | return; |
| 3088 | } |
| 3089 | if this.link_editor.is_some() { |
| 3090 | this.close_link_editor(window, cx); |
| 3091 | cx.stop_propagation(); |
| 3092 | return; |
| 3093 | } |
| 3094 | if this.toolbar_requested(cx) && !this.format_bar_open() { |
| 3095 | this.toolbar_dismissed = Some(this.selection(cx)); |
| 3096 | cx.notify(); |
| 3097 | cx.stop_propagation(); |
| 3098 | return; |
| 3099 | } |
| 3100 | if let Some(popup) = &this.emoji_popup { |
| 3101 | this.emoji_dismissed_at = Some(popup.range.start); |
| 3102 | } |
| 3103 | if this.popup.is_some() || this.emoji_popup.is_some() { |
| 3104 | this.close_popup(); |
| 3105 | cx.notify(); |
| 3106 | cx.stop_propagation(); |
| 3107 | } else if this.editing.is_some() { |
| 3108 | this.cancel_edit(window, cx); |
| 3109 | cx.stop_propagation(); |
| 3110 | } |
| 3111 | })) |
| 3112 | .children(notice) |
| 3113 | .children(self.render_reply_strip(cx)) |
| 3114 | .children(self.render_edit_strip(cx)) |
| 3115 | .children(self.render_urgent_banner(cx)) |
| 3116 | .children(self.render_meeting_chip()) |
| 3117 | .children(self.render_link_preview(cx)) |
| 3118 | .child( |
| 3119 | div() |
| 3120 | .relative() |
| 3121 | .w_full() |
| 3122 | .children(self.render_popup(cx)) |
| 3123 | .children(self.render_emoji_popup(window, cx)) |
| 3124 | .children(self.render_toolbar(focused, cx)) |
| 3125 | .children(self.render_schedule(cx)) |
| 3126 | .children(self.render_delivery_menu(cx)) |
| 3127 | .child( |
| 3128 | v_flex() |
| 3129 | .debug_selector(|| COMPOSER_BOX_TAG.into()) |
| 3130 | .relative() |
| 3131 | .w_full() |
| 3132 | .rounded(px(12.)) |
| 3133 | .bg(theme::surface_raised()) |
| 3134 | .border_1() |
| 3135 | .border_color(if focused { |
| 3136 | theme::accent() |
| 3137 | } else { |
| 3138 | transparent_black() |
| 3139 | }) |
| 3140 | .child( |
| 3141 | canvas( |
| 3142 | move |bounds, _, _| composer_bounds.set(Some(bounds)), |
| 3143 | |_, _, _, _| {}, |
| 3144 | ) |
| 3145 | .absolute() |
| 3146 | .size_full(), |
| 3147 | ) |
| 3148 | .children(format_bar) |
| 3149 | .children(self.render_importance_label(cx)) |
| 3150 | .children(tray) |
| 3151 | .child( |
| 3152 | h_flex() |
| 3153 | .w_full() |
| 3154 | .map(|row| { |
| 3155 | if single_line { |
| 3156 | row.items_center() |
| 3157 | } else { |
| 3158 | row.items_end() |
| 3159 | } |
| 3160 | }) |
| 3161 | .py(px(COMPOSER_BUTTON_INSET)) |
| 3162 | .px(px(COMPOSER_BUTTON_INSET)) |
| 3163 | .child( |
| 3164 | h_flex() |
| 3165 | .debug_selector(|| COMPOSER_LEADING_TAG.into()) |
| 3166 | .flex_none() |
| 3167 | .gap(px(COMPOSER_BUTTON_INSET)) |
| 3168 | .child(format_toggle) |
| 3169 | .children(self.render_delivery_button(cx)) |
| 3170 | .children(fun) |
| 3171 | .children(attach), |
| 3172 | ) |
| 3173 | .child( |
| 3174 | div() |
| 3175 | .flex_1() |
| 3176 | .min_w_0() |
| 3177 | .capture_any_mouse_down(cx.listener( |
| 3178 | |this, _: &MouseDownEvent, _, _| { |
| 3179 | this.mouse_selecting = true; |
| 3180 | this.toolbar_dismissed = None; |
| 3181 | this.link_editor = None; |
| 3182 | }, |
| 3183 | )) |
| 3184 | .capture_any_mouse_up(cx.listener( |
| 3185 | |this, _: &MouseUpEvent, _, cx| { |
| 3186 | this.mouse_selecting = false; |
| 3187 | cx.notify(); |
| 3188 | }, |
| 3189 | )) |
| 3190 | .on_mouse_up_out( |
| 3191 | MouseButton::Left, |
| 3192 | cx.listener(|this, _, _, cx| { |
| 3193 | this.mouse_selecting = false; |
| 3194 | cx.notify(); |
| 3195 | }), |
| 3196 | ) |
| 3197 | .child(input), |
| 3198 | ) |
| 3199 | .child(send), |
| 3200 | ), |
| 3201 | ), |
| 3202 | ) |
| 3203 | .children(self.render_paste_hint()) |
| 3204 | } |
| 3205 | } |
| 3206 | |
| 3207 | #[cfg(test)] |
| 3208 | mod tests { |
| 3209 | use super::{active_mention, submitted_text}; |
| 3210 | use gpui_kit::component::input::InputEvent; |
| 3211 | |
| 3212 | fn press(shift: bool) -> InputEvent { |
| 3213 | InputEvent::PressEnter { |
| 3214 | secondary: false, |
| 3215 | shift, |
| 3216 | } |
| 3217 | } |
| 3218 | |
| 3219 | #[test] |
| 3220 | fn enter_sends_trimmed_text() { |
| 3221 | assert_eq!( |
| 3222 | submitted_text(&press(false), " hello \n"), |
| 3223 | Some("hello".into()) |
| 3224 | ); |
| 3225 | } |
| 3226 | |
| 3227 | #[test] |
| 3228 | fn shift_enter_never_sends() { |
| 3229 | assert_eq!(submitted_text(&press(true), "hello"), None); |
| 3230 | } |
| 3231 | |
| 3232 | #[test] |
| 3233 | fn blank_text_never_sends() { |
| 3234 | assert_eq!(submitted_text(&press(false), " \n "), None); |
| 3235 | } |
| 3236 | |
| 3237 | #[test] |
| 3238 | fn other_events_never_send() { |
| 3239 | assert_eq!(submitted_text(&InputEvent::Change, "hello"), None); |
| 3240 | } |
| 3241 | |
| 3242 | #[test] |
| 3243 | fn at_sign_opens_a_mention_query() { |
| 3244 | assert_eq!(active_mention("hi @Ma", 6, &[]), Some((3..6, "Ma".into()))); |
| 3245 | assert_eq!(active_mention("@", 1, &[]), Some((0..1, String::new()))); |
| 3246 | } |
| 3247 | |
| 3248 | #[test] |
| 3249 | fn names_with_a_space_keep_the_query_open() { |
| 3250 | assert_eq!( |
| 3251 | active_mention("@Mara Lin", 9, &[]), |
| 3252 | Some((0..9, "Mara Lin".into())) |
| 3253 | ); |
| 3254 | } |
| 3255 | |
| 3256 | #[test] |
| 3257 | fn mail_addresses_and_closed_queries_do_not_open() { |
| 3258 | assert_eq!(active_mention("a@b", 3, &[]), None); |
| 3259 | assert_eq!(active_mention("@Mara ", 6, &[]), None); |
| 3260 | assert_eq!(active_mention("@ x", 3, &[]), None); |
| 3261 | assert_eq!(active_mention("@Ma\nrest", 8, &[]), None); |
| 3262 | } |
| 3263 | |
| 3264 | #[test] |
| 3265 | fn text_inside_a_token_does_not_reopen() { |
| 3266 | let token = 0..15; |
| 3267 | assert_eq!( |
| 3268 | active_mention("@Mara Lindqvist", 15, std::slice::from_ref(&token)), |
| 3269 | None |
| 3270 | ); |
| 3271 | assert_eq!( |
| 3272 | active_mention("@Mara Lindqvist @Pr", 19, std::slice::from_ref(&token)), |
| 3273 | Some((16..19, "Pr".into())) |
| 3274 | ); |
| 3275 | } |
| 3276 | |
| 3277 | #[test] |
| 3278 | fn extras_carry_image_bytes_with_their_mime_type_and_the_file_references() { |
| 3279 | use std::sync::Arc; |
| 3280 | |
| 3281 | use gpui_kit::{Image, ImageFormat}; |
| 3282 | use teams_core::FileKind; |
| 3283 | |
| 3284 | use super::super::attachment_tray::InlineImage; |
| 3285 | use crate::outgoing::{Outgoing, OutgoingFile, OutgoingImage}; |
| 3286 | |
| 3287 | let outgoing = Outgoing { |
| 3288 | draft: teams_core::Draft::default(), |
| 3289 | subject: None, |
| 3290 | mentions: Vec::new(), |
| 3291 | link_preview: None, |
| 3292 | reply: None, |
| 3293 | edit: None, |
| 3294 | images: vec![OutgoingImage::Inline(InlineImage { |
| 3295 | name: "pasted-image.png".into(), |
| 3296 | image: Arc::new(Image::from_bytes(ImageFormat::Png, vec![1, 2, 3])), |
| 3297 | dimensions: Some((1, 1)), |
| 3298 | })], |
| 3299 | files: vec![OutgoingFile { |
| 3300 | name: "plan.pdf".into(), |
| 3301 | size: 9, |
| 3302 | kind: FileKind::Pdf, |
| 3303 | uploaded: None, |
| 3304 | reference: teams_core::FileReference { |
| 3305 | attachment_id: "guid".into(), |
| 3306 | content_url: "https://files.example/plan.pdf".into(), |
| 3307 | name: "plan.pdf".into(), |
| 3308 | }, |
| 3309 | }], |
| 3310 | importance: Default::default(), |
| 3311 | }; |
| 3312 | let extras = outgoing.extras(); |
| 3313 | assert_eq!(extras.images[0].content_type, "image/png"); |
| 3314 | assert_eq!(extras.images[0].bytes.as_slice(), [1, 2, 3]); |
| 3315 | assert_eq!(extras.files[0].attachment_id, "guid"); |
| 3316 | assert!(outgoing.has_attachments()); |
| 3317 | } |
| 3318 | |
| 3319 | fn placed_image(width: u32) -> crate::outgoing::OutgoingImage { |
| 3320 | use std::sync::Arc; |
| 3321 | |
| 3322 | use gpui_kit::{Image, ImageFormat}; |
| 3323 | |
| 3324 | crate::outgoing::OutgoingImage::Inline(super::super::attachment_tray::InlineImage { |
| 3325 | name: "pasted-image.png".into(), |
| 3326 | image: Arc::new(Image::from_bytes(ImageFormat::Png, vec![width as u8])), |
| 3327 | dimensions: Some((width, width)), |
| 3328 | }) |
| 3329 | } |
| 3330 | |
| 3331 | fn remote_gif(title: &str) -> crate::outgoing::OutgoingImage { |
| 3332 | crate::outgoing::OutgoingImage::Remote(super::RemoteImage { |
| 3333 | kind: crate::remote_image::RemoteKind::Gif, |
| 3334 | url: "https://media0.giphy.com/media/a/giphy.gif".into(), |
| 3335 | title: title.into(), |
| 3336 | width: 200, |
| 3337 | height: 100, |
| 3338 | }) |
| 3339 | } |
| 3340 | |
| 3341 | fn outgoing_with( |
| 3342 | text: &str, |
| 3343 | images: Vec<crate::outgoing::OutgoingImage>, |
| 3344 | ) -> crate::outgoing::Outgoing { |
| 3345 | crate::outgoing::Outgoing { |
| 3346 | draft: teams_core::Draft::plain(text), |
| 3347 | subject: None, |
| 3348 | mentions: Vec::new(), |
| 3349 | link_preview: None, |
| 3350 | reply: None, |
| 3351 | edit: None, |
| 3352 | images, |
| 3353 | files: Vec::new(), |
| 3354 | importance: Default::default(), |
| 3355 | } |
| 3356 | } |
| 3357 | |
| 3358 | #[test] |
| 3359 | fn an_image_is_placed_in_the_html_where_its_mark_sits() { |
| 3360 | let outgoing = outgoing_with("a \u{FFFC} b", vec![placed_image(2)]); |
| 3361 | assert_eq!( |
| 3362 | outgoing.html(), |
| 3363 | "a <img src=\"../hostedContents/1/$value\"> b" |
| 3364 | ); |
| 3365 | assert_eq!(outgoing.text(), "a b"); |
| 3366 | } |
| 3367 | |
| 3368 | #[test] |
| 3369 | fn images_keep_the_order_of_their_marks() { |
| 3370 | let outgoing = outgoing_with("\u{FFFC}x\u{FFFC}", vec![placed_image(2), placed_image(4)]); |
| 3371 | assert_eq!( |
| 3372 | outgoing.html(), |
| 3373 | "<img src=\"../hostedContents/1/$value\">x<img src=\"../hostedContents/2/$value\">" |
| 3374 | ); |
| 3375 | } |
| 3376 | |
| 3377 | #[test] |
| 3378 | fn gifs_sit_in_place_and_only_uploaded_images_are_numbered() { |
| 3379 | let outgoing = outgoing_with( |
| 3380 | "\u{FFFC}a\u{FFFC}b\u{FFFC}", |
| 3381 | vec![placed_image(2), remote_gif("Wave"), placed_image(4)], |
| 3382 | ); |
| 3383 | assert_eq!( |
| 3384 | outgoing.html(), |
| 3385 | "<img src=\"../hostedContents/1/$value\">a<img src=\"https://media0.giphy.com/media/a/giphy.gif\" width=\"200\" height=\"100\" alt=\"Wave\" itemtype=\"http://schema.skype.com/Giphy\">b<img src=\"../hostedContents/2/$value\">" |
| 3386 | ); |
| 3387 | assert_eq!(outgoing.extras().images.len(), 2); |
| 3388 | } |
| 3389 | |
| 3390 | #[test] |
| 3391 | fn a_gif_title_is_escaped_in_the_html() { |
| 3392 | let outgoing = outgoing_with("\u{FFFC}", vec![remote_gif("a \"b\" <c>")]); |
| 3393 | assert!( |
| 3394 | outgoing |
| 3395 | .html() |
| 3396 | .contains("alt=\"a "b" <c>\"") |
| 3397 | ); |
| 3398 | } |
| 3399 | |
| 3400 | #[test] |
| 3401 | fn a_sticker_carries_the_sticker_itemtype() { |
| 3402 | let sticker = super::RemoteImage::sticker(crate::stickers::popular()[0]); |
| 3403 | let outgoing = outgoing_with( |
| 3404 | "\u{FFFC}", |
| 3405 | vec![crate::outgoing::OutgoingImage::Remote(sticker)], |
| 3406 | ); |
| 3407 | assert!( |
| 3408 | outgoing |
| 3409 | .html() |
| 3410 | .contains("itemtype=\"http://schema.skype.com/Sticker\"") |
| 3411 | ); |
| 3412 | assert!(outgoing.html().contains("width=\"250\" height=\"250\"")); |
| 3413 | } |
| 3414 | |
| 3415 | #[test] |
| 3416 | fn a_mark_without_an_image_leaves_no_trace() { |
| 3417 | assert_eq!(outgoing_with("a\u{FFFC}b", Vec::new()).html(), "ab"); |
| 3418 | } |
| 3419 | |
| 3420 | #[test] |
| 3421 | fn enter_sends_attachments_even_without_text() { |
| 3422 | assert!(super::is_plain_enter(&press(false))); |
| 3423 | assert!(!super::is_plain_enter(&press(true))); |
| 3424 | assert!(!super::is_plain_enter(&InputEvent::Change)); |
| 3425 | } |
| 3426 | |
| 3427 | mod keys { |
| 3428 | use std::sync::Arc; |
| 3429 | |
| 3430 | use gpui_kit::test::TestWindowExt as _; |
| 3431 | use gpui_kit::{AppContext as _, TestAppContext, WindowOptions}; |
| 3432 | use store::Store; |
| 3433 | |
| 3434 | use super::super::Composer; |
| 3435 | use crate::app_state::{AppState, Mode}; |
| 3436 | |
| 3437 | #[gpui_kit::test] |
| 3438 | fn ctrl_a_selects_the_whole_draft(cx: &mut TestAppContext) { |
| 3439 | cx.update(gpui_kit::init); |
| 3440 | let store = Arc::new(Store::open_in_memory().unwrap()); |
| 3441 | let mode = Mode { |
| 3442 | demo: true, |
| 3443 | read_only: false, |
| 3444 | demo_sync: None, |
| 3445 | }; |
| 3446 | let (handle, composer) = cx.update(|cx| { |
| 3447 | let app = cx.new(|_| AppState::new(store, mode)); |
| 3448 | gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { |
| 3449 | cx.new(|cx| Composer::new(app, window, cx)) |
| 3450 | }) |
| 3451 | .unwrap() |
| 3452 | }); |
| 3453 | let value = cx |
| 3454 | .update_window(handle, |_, window, cx| { |
| 3455 | composer.update(cx, |composer, cx| composer.focus(window, cx)); |
| 3456 | window.render_frame(cx); |
| 3457 | window.input("first line", cx); |
| 3458 | window.press("shift-enter", cx); |
| 3459 | window.input("second line", cx); |
| 3460 | window.press("ctrl-a", cx); |
| 3461 | window.input("X", cx); |
| 3462 | composer.read(cx).input.read(cx).value() |
| 3463 | }) |
| 3464 | .unwrap(); |
| 3465 | assert_eq!(value, "X"); |
| 3466 | } |
| 3467 | |
| 3468 | struct Typist<'a> { |
| 3469 | cx: &'a mut TestAppContext, |
| 3470 | handle: gpui_kit::AnyWindowHandle, |
| 3471 | composer: gpui_kit::Entity<Composer>, |
| 3472 | } |
| 3473 | |
| 3474 | impl Typist<'_> { |
| 3475 | fn act(&mut self, action: impl FnOnce(&mut gpui_kit::Window, &mut gpui_kit::App)) { |
| 3476 | self.cx |
| 3477 | .update_window(self.handle, |_, window, cx| action(window, cx)) |
| 3478 | .unwrap(); |
| 3479 | self.cx.run_until_parked(); |
| 3480 | } |
| 3481 | |
| 3482 | fn type_text(&mut self, text: &str) { |
| 3483 | for character in text.chars() { |
| 3484 | self.act(|window, cx| window.input(&character.to_string(), cx)); |
| 3485 | } |
| 3486 | } |
| 3487 | |
| 3488 | fn press(&mut self, keys: &str) { |
| 3489 | for key in keys.split(' ') { |
| 3490 | self.act(|window, cx| window.press(key, cx)); |
| 3491 | } |
| 3492 | } |
| 3493 | |
| 3494 | fn value(&mut self) -> String { |
| 3495 | let composer = self.composer.clone(); |
| 3496 | self.cx |
| 3497 | .update(|cx| composer.read(cx).input.read(cx).value().to_string()) |
| 3498 | } |
| 3499 | |
| 3500 | fn draft(&mut self) -> teams_core::Draft { |
| 3501 | let composer = self.composer.clone(); |
| 3502 | self.cx.update(|cx| composer.read(cx).draft.clone()) |
| 3503 | } |
| 3504 | |
| 3505 | fn outgoing(&mut self) -> crate::outgoing::Outgoing { |
| 3506 | let composer = self.composer.clone(); |
| 3507 | self.cx |
| 3508 | .update(|cx| composer.read(cx).outgoing(cx)) |
| 3509 | .expect("text to send") |
| 3510 | } |
| 3511 | } |
| 3512 | |
| 3513 | fn typist(cx: &mut TestAppContext) -> Typist<'_> { |
| 3514 | let (handle, composer) = demo_composer(cx); |
| 3515 | cx.update(super::super::bind_keys); |
| 3516 | let mut typist = Typist { |
| 3517 | cx, |
| 3518 | handle, |
| 3519 | composer: composer.clone(), |
| 3520 | }; |
| 3521 | typist.act(|window, cx| { |
| 3522 | composer.update(cx, |composer, cx| composer.focus(window, cx)); |
| 3523 | window.render_frame(cx); |
| 3524 | }); |
| 3525 | typist |
| 3526 | } |
| 3527 | |
| 3528 | fn png_bytes(side: u32) -> Vec<u8> { |
| 3529 | let mut bytes = Vec::new(); |
| 3530 | image::RgbaImage::new(side, side) |
| 3531 | .write_to( |
| 3532 | &mut std::io::Cursor::new(&mut bytes), |
| 3533 | image::ImageFormat::Png, |
| 3534 | ) |
| 3535 | .unwrap(); |
| 3536 | bytes |
| 3537 | } |
| 3538 | |
| 3539 | fn paste_image(typist: &mut Typist<'_>, side: u32) { |
| 3540 | typist.cx.write_to_clipboard(gpui_kit::ClipboardItem { |
| 3541 | entries: vec![gpui_kit::ClipboardEntry::Image( |
| 3542 | gpui_kit::Image::from_bytes(gpui_kit::ImageFormat::Png, png_bytes(side)), |
| 3543 | )], |
| 3544 | }); |
| 3545 | typist.press("ctrl-v"); |
| 3546 | } |
| 3547 | |
| 3548 | fn image_ids(composer: &Composer) -> usize { |
| 3549 | composer |
| 3550 | .tray |
| 3551 | .items() |
| 3552 | .iter() |
| 3553 | .filter(|item| composer.tray.image(item.id).is_some()) |
| 3554 | .count() |
| 3555 | } |
| 3556 | |
| 3557 | fn image_tokens(typist: &mut Typist<'_>) -> Vec<(std::ops::Range<usize>, bool)> { |
| 3558 | let composer = typist.composer.clone(); |
| 3559 | typist.cx.update(|cx| { |
| 3560 | composer |
| 3561 | .read(cx) |
| 3562 | .input |
| 3563 | .read(cx) |
| 3564 | .tokens() |
| 3565 | .iter() |
| 3566 | .map(|span| (span.range(), span.token().is_block())) |
| 3567 | .collect() |
| 3568 | }) |
| 3569 | } |
| 3570 | |
| 3571 | #[gpui_kit::test] |
| 3572 | fn a_pasted_image_sits_at_the_cursor_and_is_sent_there(cx: &mut TestAppContext) { |
| 3573 | let mut typist = typist(cx); |
| 3574 | typist.type_text("ab"); |
| 3575 | typist.press("left"); |
| 3576 | paste_image(&mut typist, 2); |
| 3577 | assert_eq!(typist.value(), "a\u{FFFC}b"); |
| 3578 | assert_eq!(image_tokens(&mut typist), vec![(1..4, true)]); |
| 3579 | let outgoing = typist.outgoing(); |
| 3580 | assert_eq!(outgoing.images.len(), 1); |
| 3581 | assert_eq!( |
| 3582 | outgoing.html(), |
| 3583 | "a<img src=\"../hostedContents/1/$value\">b" |
| 3584 | ); |
| 3585 | let composer = typist.composer.clone(); |
| 3586 | assert!(!typist.cx.update(|cx| composer.read(cx).tray.has_chips())); |
| 3587 | } |
| 3588 | |
| 3589 | fn wide_png_bytes() -> Vec<u8> { |
| 3590 | let mut bytes = Vec::new(); |
| 3591 | image::RgbaImage::new(600, 300) |
| 3592 | .write_to( |
| 3593 | &mut std::io::Cursor::new(&mut bytes), |
| 3594 | image::ImageFormat::Png, |
| 3595 | ) |
| 3596 | .unwrap(); |
| 3597 | bytes |
| 3598 | } |
| 3599 | |
| 3600 | fn caret_is_visible(typist: &mut Typist<'_>) -> bool { |
| 3601 | let composer = typist.composer.clone(); |
| 3602 | typist.cx.update(|cx| { |
| 3603 | let state = composer.read(cx).input.read(cx); |
| 3604 | let cursor = state.cursor(); |
| 3605 | let caret = state.range_to_bounds(&(cursor..cursor)).unwrap(); |
| 3606 | let viewport = state.input_bounds(); |
| 3607 | caret.origin.y >= viewport.origin.y && caret.bottom() <= viewport.bottom() |
| 3608 | }) |
| 3609 | } |
| 3610 | |
| 3611 | #[gpui_kit::test] |
| 3612 | fn typing_after_a_tall_image_keeps_the_caret_in_view(cx: &mut TestAppContext) { |
| 3613 | let mut typist = typist(cx); |
| 3614 | typist.type_text("look at this "); |
| 3615 | typist.cx.write_to_clipboard(gpui_kit::ClipboardItem { |
| 3616 | entries: vec![gpui_kit::ClipboardEntry::Image( |
| 3617 | gpui_kit::Image::from_bytes(gpui_kit::ImageFormat::Png, wide_png_bytes()), |
| 3618 | )], |
| 3619 | }); |
| 3620 | typist.press("ctrl-v"); |
| 3621 | typist.act(|window, _| window.refresh()); |
| 3622 | typist.type_text("and then more text"); |
| 3623 | typist.act(|window, _| window.refresh()); |
| 3624 | assert!(caret_is_visible(&mut typist)); |
| 3625 | } |
| 3626 | |
| 3627 | #[gpui_kit::test] |
| 3628 | fn deleting_the_token_drops_the_image_and_undo_brings_it_back(cx: &mut TestAppContext) { |
| 3629 | let mut typist = typist(cx); |
| 3630 | typist.type_text("ab"); |
| 3631 | typist.press("left"); |
| 3632 | paste_image(&mut typist, 2); |
| 3633 | typist.press("backspace"); |
| 3634 | assert_eq!(typist.value(), "ab"); |
| 3635 | assert!(image_tokens(&mut typist).is_empty()); |
| 3636 | let outgoing = typist.outgoing(); |
| 3637 | assert!(outgoing.images.is_empty()); |
| 3638 | assert_eq!(outgoing.html(), "ab"); |
| 3639 | typist.press("ctrl-z"); |
| 3640 | assert_eq!(typist.value(), "a\u{FFFC}b"); |
| 3641 | assert_eq!(image_tokens(&mut typist), vec![(1..4, true)]); |
| 3642 | assert_eq!(typist.outgoing().images.len(), 1); |
| 3643 | } |
| 3644 | |
| 3645 | #[gpui_kit::test] |
| 3646 | fn the_remove_button_drops_the_image_and_one_undo_brings_it_back(cx: &mut TestAppContext) { |
| 3647 | let mut typist = typist(cx); |
| 3648 | typist.type_text("ab"); |
| 3649 | paste_image(&mut typist, 2); |
| 3650 | typist.type_text("cd"); |
| 3651 | let composer = typist.composer.clone(); |
| 3652 | let id = typist.cx.update(|cx| { |
| 3653 | let state = composer.read(cx).input.read(cx); |
| 3654 | super::super::image_id(state.tokens()[0].token()).unwrap() |
| 3655 | }); |
| 3656 | typist.act(|window, cx| { |
| 3657 | composer.update(cx, |composer, cx| { |
| 3658 | composer.remove_image_token(id, window, cx) |
| 3659 | }); |
| 3660 | }); |
| 3661 | assert_eq!(typist.value(), "abcd"); |
| 3662 | assert!(image_tokens(&mut typist).is_empty()); |
| 3663 | assert!(typist.outgoing().images.is_empty()); |
| 3664 | typist.press("ctrl-z"); |
| 3665 | assert_eq!(typist.value(), "ab\u{FFFC}cd"); |
| 3666 | assert_eq!(image_tokens(&mut typist), vec![(2..5, true)]); |
| 3667 | assert_eq!(typist.outgoing().images.len(), 1); |
| 3668 | } |
| 3669 | |
| 3670 | #[gpui_kit::test] |
| 3671 | fn images_are_sent_in_the_order_they_appear_in_the_text(cx: &mut TestAppContext) { |
| 3672 | let mut typist = typist(cx); |
| 3673 | typist.type_text("x"); |
| 3674 | paste_image(&mut typist, 2); |
| 3675 | typist.type_text("y"); |
| 3676 | typist.press("ctrl-home"); |
| 3677 | paste_image(&mut typist, 4); |
| 3678 | assert_eq!(typist.value(), "\u{FFFC}x\u{FFFC}y"); |
| 3679 | let outgoing = typist.outgoing(); |
| 3680 | let dimensions: Vec<_> = outgoing |
| 3681 | .images |
| 3682 | .iter() |
| 3683 | .map(|image| match image { |
| 3684 | crate::outgoing::OutgoingImage::Inline(inline) => inline.dimensions, |
| 3685 | crate::outgoing::OutgoingImage::Remote(_) => None, |
| 3686 | }) |
| 3687 | .collect(); |
| 3688 | assert_eq!(dimensions, [Some((4, 4)), Some((2, 2))]); |
| 3689 | assert_eq!( |
| 3690 | outgoing.html(), |
| 3691 | "<img src=\"../hostedContents/1/$value\">x<img src=\"../hostedContents/2/$value\">y" |
| 3692 | ); |
| 3693 | } |
| 3694 | |
| 3695 | #[gpui_kit::test] |
| 3696 | fn an_image_alone_can_be_sent_and_never_leaks_into_copied_text(cx: &mut TestAppContext) { |
| 3697 | let mut typist = typist(cx); |
| 3698 | paste_image(&mut typist, 2); |
| 3699 | let composer = typist.composer.clone(); |
| 3700 | assert!(typist.cx.update(|cx| composer.read(cx).can_send(cx))); |
| 3701 | let outgoing = typist.outgoing(); |
| 3702 | assert_eq!(outgoing.text(), ""); |
| 3703 | assert_eq!(outgoing.images.len(), 1); |
| 3704 | typist.type_text("hi"); |
| 3705 | typist.press("ctrl-a ctrl-c"); |
| 3706 | let copied = typist.cx.read_from_clipboard().and_then(|item| item.text()); |
| 3707 | assert_eq!(copied.as_deref(), Some("hi")); |
| 3708 | } |
| 3709 | |
| 3710 | #[gpui_kit::test] |
| 3711 | fn an_inserted_gif_alone_can_be_sent_and_undo_removes_it(cx: &mut TestAppContext) { |
| 3712 | let mut typist = typist(cx); |
| 3713 | let composer = typist.composer.clone(); |
| 3714 | assert!(!typist.cx.update(|cx| composer.read(cx).can_send(cx))); |
| 3715 | typist.act(|window, cx| { |
| 3716 | composer.update(cx, |composer, cx| { |
| 3717 | composer.insert_remote_image( |
| 3718 | super::super::RemoteImage { |
| 3719 | kind: crate::remote_image::RemoteKind::Gif, |
| 3720 | url: "https://media0.giphy.com/media/a/giphy.gif".into(), |
| 3721 | title: "Wave".into(), |
| 3722 | width: 200, |
| 3723 | height: 100, |
| 3724 | }, |
| 3725 | window, |
| 3726 | cx, |
| 3727 | ) |
| 3728 | }); |
| 3729 | }); |
| 3730 | assert!(typist.cx.update(|cx| composer.read(cx).can_send(cx))); |
| 3731 | assert_eq!(typist.value(), "\u{FFFC}"); |
| 3732 | let outgoing = typist.outgoing(); |
| 3733 | assert!( |
| 3734 | outgoing |
| 3735 | .html() |
| 3736 | .starts_with("<img src=\"https://media0.giphy.com/") |
| 3737 | ); |
| 3738 | assert!(outgoing.extras().images.is_empty()); |
| 3739 | typist.press("ctrl-z"); |
| 3740 | assert_eq!(typist.value(), ""); |
| 3741 | assert!(!typist.cx.update(|cx| composer.read(cx).can_send(cx))); |
| 3742 | } |
| 3743 | |
| 3744 | #[gpui_kit::test] |
| 3745 | fn an_inserted_emoji_lands_at_the_cursor(cx: &mut TestAppContext) { |
| 3746 | let mut typist = typist(cx); |
| 3747 | typist.type_text("ab"); |
| 3748 | typist.press("left"); |
| 3749 | let composer = typist.composer.clone(); |
| 3750 | typist.act(|window, cx| { |
| 3751 | composer.update(cx, |composer, cx| { |
| 3752 | composer.insert_emoji("\u{1F44D}", window, cx) |
| 3753 | }); |
| 3754 | }); |
| 3755 | assert_eq!(typist.value(), "a\u{1F44D}b"); |
| 3756 | } |
| 3757 | |
| 3758 | #[gpui_kit::test] |
| 3759 | fn pasted_text_loses_object_marks(cx: &mut TestAppContext) { |
| 3760 | let mut typist = typist(cx); |
| 3761 | typist |
| 3762 | .cx |
| 3763 | .write_to_clipboard(gpui_kit::ClipboardItem::new_string("a\u{FFFC}b".to_owned())); |
| 3764 | typist.press("ctrl-v"); |
| 3765 | assert_eq!(typist.value(), "ab"); |
| 3766 | } |
| 3767 | |
| 3768 | #[gpui_kit::test] |
| 3769 | fn a_restored_outgoing_gets_its_image_tokens_back(cx: &mut TestAppContext) { |
| 3770 | let mut typist = typist(cx); |
| 3771 | let outgoing = super::outgoing_with("x\u{FFFC}y", vec![super::placed_image(3)]); |
| 3772 | let composer = typist.composer.clone(); |
| 3773 | typist.act(|window, cx| { |
| 3774 | composer.update(cx, |composer, cx| composer.restore(&outgoing, window, cx)); |
| 3775 | }); |
| 3776 | assert_eq!(typist.value(), "x\u{FFFC}y"); |
| 3777 | assert_eq!(image_tokens(&mut typist), vec![(1..4, true)]); |
| 3778 | let again = typist.outgoing(); |
| 3779 | assert_eq!(again.images, outgoing.images); |
| 3780 | assert_eq!(again.html(), "x<img src=\"../hostedContents/1/$value\">y"); |
| 3781 | } |
| 3782 | |
| 3783 | fn load( |
| 3784 | typist: &mut Typist<'_>, |
| 3785 | entry: &crate::agent_api::ProposalEntry, |
| 3786 | ) -> Vec<(String, Vec<u8>)> { |
| 3787 | let composer = typist.composer.clone(); |
| 3788 | let mentions = crate::views::proposal_card::outgoing_for(entry, None).mentions; |
| 3789 | let pictures = crate::views::proposal_card::picture_files(entry); |
| 3790 | let loaded = pictures.clone(); |
| 3791 | let text = entry.proposal.text.clone(); |
| 3792 | typist.act(|window, cx| { |
| 3793 | composer.update(cx, |composer, cx| { |
| 3794 | composer.load_proposal(&text, &mentions, pictures, window, cx) |
| 3795 | }); |
| 3796 | }); |
| 3797 | loaded |
| 3798 | } |
| 3799 | |
| 3800 | #[gpui_kit::test] |
| 3801 | fn a_loaded_proposal_keeps_its_mentions_images_and_the_reply(cx: &mut TestAppContext) { |
| 3802 | use crate::views::proposal_card::{sample_entry, sample_png}; |
| 3803 | |
| 3804 | let mut typist = typist(cx); |
| 3805 | let entry = sample_entry( |
| 3806 | "Thanks @Ada Example", |
| 3807 | &[("u1", "Ada Example")], |
| 3808 | &[sample_png(3, 0)], |
| 3809 | ); |
| 3810 | let composer = typist.composer.clone(); |
| 3811 | let reply = super::super::ReplyPreview { |
| 3812 | message_id: "m1".into(), |
| 3813 | author: "Bo".into(), |
| 3814 | excerpt: "question".into(), |
| 3815 | }; |
| 3816 | typist.act(|_, cx| { |
| 3817 | composer.update(cx, |composer, cx| { |
| 3818 | composer.set_reply(Some(reply.clone()), cx) |
| 3819 | }); |
| 3820 | }); |
| 3821 | load(&mut typist, &entry); |
| 3822 | let tokens = image_tokens(&mut typist); |
| 3823 | assert_eq!(tokens.iter().filter(|(_, block)| *block).count(), 1); |
| 3824 | assert_eq!(tokens.len(), 2); |
| 3825 | let outgoing = typist.outgoing(); |
| 3826 | assert_eq!(outgoing.images.len(), 1); |
| 3827 | assert_eq!( |
| 3828 | outgoing.mentions, |
| 3829 | [teams_core::MentionInput::user("u1", "Ada Example")] |
| 3830 | ); |
| 3831 | assert_eq!(outgoing.reply, Some(reply)); |
| 3832 | assert!(outgoing.html().starts_with("Thanks @Ada Example")); |
| 3833 | } |
| 3834 | |
| 3835 | #[gpui_kit::test] |
| 3836 | fn a_mention_at_the_start_of_a_line_is_placed_on_send(cx: &mut TestAppContext) { |
| 3837 | use crate::views::proposal_card::sample_entry; |
| 3838 | |
| 3839 | let mut typist = typist(cx); |
| 3840 | let entry = sample_entry("Hi team\n\n@Ada\n\nthanks", &[], &[]); |
| 3841 | load(&mut typist, &entry); |
| 3842 | let composer = typist.composer.clone(); |
| 3843 | typist.act(|window, cx| { |
| 3844 | composer.update(cx, |composer, cx| { |
| 3845 | composer.load_proposal( |
| 3846 | "Hi team\n\n@Ada\n\nthanks", |
| 3847 | &[teams_core::MentionInput::user("u1", "Ada")], |
| 3848 | Vec::new(), |
| 3849 | window, |
| 3850 | cx, |
| 3851 | ) |
| 3852 | }); |
| 3853 | }); |
| 3854 | let outgoing = typist.outgoing(); |
| 3855 | let (_, placed) = teams_core::apply_mentions(&outgoing.html(), &outgoing.mentions); |
| 3856 | assert_eq!(placed.len(), 1, "{}", outgoing.html()); |
| 3857 | } |
| 3858 | |
| 3859 | #[gpui_kit::test] |
| 3860 | fn a_text_only_proposal_keeps_a_running_upload(cx: &mut TestAppContext) { |
| 3861 | use crate::views::proposal_card::sample_entry; |
| 3862 | |
| 3863 | let mut typist = typist(cx); |
| 3864 | let composer = typist.composer.clone(); |
| 3865 | typist.act(|_, cx| { |
| 3866 | composer.update(cx, |composer, _| { |
| 3867 | let ids = composer.tray.add_pending(&["plan.pdf".to_owned()]); |
| 3868 | assert_eq!(ids.len(), 1); |
| 3869 | }); |
| 3870 | }); |
| 3871 | load(&mut typist, &sample_entry("just text", &[], &[])); |
| 3872 | let items = typist.cx.update(|cx| composer.read(cx).tray.items().len()); |
| 3873 | assert_eq!(items, 1); |
| 3874 | assert_eq!(typist.value(), "just text"); |
| 3875 | } |
| 3876 | |
| 3877 | #[gpui_kit::test] |
| 3878 | fn proposal_images_join_the_tray_and_big_ones_become_uploads(cx: &mut TestAppContext) { |
| 3879 | use crate::views::proposal_card::{sample_entry, sample_png}; |
| 3880 | |
| 3881 | let mut typist = typist(cx); |
| 3882 | let composer = typist.composer.clone(); |
| 3883 | typist.act(|_, cx| { |
| 3884 | composer.update(cx, |composer, _| { |
| 3885 | composer.conversation_id = Some("c1".to_owned()); |
| 3886 | composer.tray.add_pending(&["kept.pdf".to_owned()]); |
| 3887 | }); |
| 3888 | }); |
| 3889 | let entry = sample_entry( |
| 3890 | "pictures", |
| 3891 | &[], |
| 3892 | &[sample_png(2, 0), sample_png(2, 2 * 1024 * 1024)], |
| 3893 | ); |
| 3894 | load(&mut typist, &entry); |
| 3895 | let (items, images) = typist.cx.update(|cx| { |
| 3896 | let composer = composer.read(cx); |
| 3897 | (composer.tray.items().len(), image_ids(composer)) |
| 3898 | }); |
| 3899 | assert_eq!(items, 3); |
| 3900 | assert_eq!(images, 1); |
| 3901 | } |
| 3902 | |
| 3903 | #[gpui_kit::test] |
| 3904 | fn typed_markdown_is_formatted_and_backspace_brings_it_back(cx: &mut TestAppContext) { |
| 3905 | use teams_core::{FormatState, MarkKind}; |
| 3906 | |
| 3907 | let mut typist = typist(cx); |
| 3908 | typist.type_text("a **bold**"); |
| 3909 | assert_eq!(typist.value(), "a bold"); |
| 3910 | assert_eq!(typist.draft().state(2..6, &MarkKind::Bold), FormatState::On); |
| 3911 | typist.press("backspace"); |
| 3912 | assert_eq!(typist.value(), "a **bold**"); |
| 3913 | assert!(typist.draft().marks().is_empty()); |
| 3914 | let composer = typist.composer.clone(); |
| 3915 | assert_eq!( |
| 3916 | typist |
| 3917 | .cx |
| 3918 | .update(|cx| composer.read(cx).input.read(cx).cursor()), |
| 3919 | 10 |
| 3920 | ); |
| 3921 | typist.press("backspace"); |
| 3922 | typist.type_text("* x"); |
| 3923 | assert_eq!(typist.value(), "a bold x"); |
| 3924 | assert_eq!( |
| 3925 | typist.draft().state(6..8, &MarkKind::Bold), |
| 3926 | FormatState::Off |
| 3927 | ); |
| 3928 | } |
| 3929 | |
| 3930 | #[gpui_kit::test] |
| 3931 | fn typed_url_is_linked_and_backspace_takes_the_link_back(cx: &mut TestAppContext) { |
| 3932 | use teams_core::{Mark, MarkKind}; |
| 3933 | |
| 3934 | let mut typist = typist(cx); |
| 3935 | typist.type_text("see https://a.b "); |
| 3936 | assert_eq!(typist.value(), "see https://a.b "); |
| 3937 | assert_eq!( |
| 3938 | typist.draft().marks(), |
| 3939 | [Mark { |
| 3940 | range: 4..15, |
| 3941 | kind: MarkKind::Link("https://a.b".into()) |
| 3942 | }] |
| 3943 | ); |
| 3944 | typist.press("backspace"); |
| 3945 | assert_eq!(typist.value(), "see https://a.b "); |
| 3946 | assert!(typist.draft().marks().is_empty()); |
| 3947 | } |
| 3948 | |
| 3949 | #[gpui_kit::test] |
| 3950 | fn ctrl_z_takes_an_autolink_back(cx: &mut TestAppContext) { |
| 3951 | let mut typist = typist(cx); |
| 3952 | typist.type_text("https://a.b "); |
| 3953 | assert_eq!(typist.draft().marks().len(), 1); |
| 3954 | typist.press("ctrl-z"); |
| 3955 | assert!(typist.draft().marks().is_empty()); |
| 3956 | } |
| 3957 | |
| 3958 | #[gpui_kit::test] |
| 3959 | fn pasted_url_is_linked_and_ctrl_z_restores_the_raw_text(cx: &mut TestAppContext) { |
| 3960 | use teams_core::{Mark, MarkKind}; |
| 3961 | |
| 3962 | let mut typist = typist(cx); |
| 3963 | typist |
| 3964 | .cx |
| 3965 | .write_to_clipboard(gpui_kit::ClipboardItem::new_string( |
| 3966 | "x https://a.b y".to_owned(), |
| 3967 | )); |
| 3968 | typist.press("ctrl-v"); |
| 3969 | assert_eq!(typist.value(), "x https://a.b y"); |
| 3970 | assert_eq!( |
| 3971 | typist.draft().marks(), |
| 3972 | [Mark { |
| 3973 | range: 2..13, |
| 3974 | kind: MarkKind::Link("https://a.b".into()) |
| 3975 | }] |
| 3976 | ); |
| 3977 | typist.press("ctrl-z"); |
| 3978 | assert_eq!(typist.value(), "x https://a.b y"); |
| 3979 | assert!(typist.draft().marks().is_empty()); |
| 3980 | } |
| 3981 | |
| 3982 | #[gpui_kit::test] |
| 3983 | fn the_link_editor_changes_and_removes_an_autolink(cx: &mut TestAppContext) { |
| 3984 | use teams_core::{Mark, MarkKind}; |
| 3985 | |
| 3986 | let mut typist = typist(cx); |
| 3987 | typist.type_text("https://a.b x"); |
| 3988 | typist.press("home"); |
| 3989 | typist.press("shift-right ".repeat(11).trim_end()); |
| 3990 | typist.press("ctrl-k"); |
| 3991 | typist.type_text("other.de"); |
| 3992 | typist.press("enter"); |
| 3993 | assert_eq!( |
| 3994 | typist.draft().marks(), |
| 3995 | [Mark { |
| 3996 | range: 0..11, |
| 3997 | kind: MarkKind::Link("https://other.de".into()) |
| 3998 | }] |
| 3999 | ); |
| 4000 | typist.press("ctrl-k backspace enter"); |
| 4001 | assert!(typist.draft().marks().is_empty()); |
| 4002 | assert_eq!(typist.value(), "https://a.b x"); |
| 4003 | } |
| 4004 | |
| 4005 | #[gpui_kit::test] |
| 4006 | fn enter_in_a_list_links_the_url_before_the_cursor(cx: &mut TestAppContext) { |
| 4007 | use teams_core::{Mark, MarkKind}; |
| 4008 | |
| 4009 | let mut typist = typist(cx); |
| 4010 | typist.type_text("- https://a.b"); |
| 4011 | typist.press("enter"); |
| 4012 | assert_eq!(typist.value(), "• https://a.b\n• "); |
| 4013 | assert_eq!( |
| 4014 | typist.draft().marks(), |
| 4015 | [Mark { |
| 4016 | range: 4..15, |
| 4017 | kind: MarkKind::Link("https://a.b".into()) |
| 4018 | }] |
| 4019 | ); |
| 4020 | } |
| 4021 | |
| 4022 | #[gpui_kit::test] |
| 4023 | fn a_url_only_paste_links_without_the_paste_hint(cx: &mut TestAppContext) { |
| 4024 | let mut typist = typist(cx); |
| 4025 | typist |
| 4026 | .cx |
| 4027 | .write_to_clipboard(gpui_kit::ClipboardItem::new_string( |
| 4028 | "x https://a.b y".to_owned(), |
| 4029 | )); |
| 4030 | typist.press("ctrl-v"); |
| 4031 | assert_eq!(typist.draft().marks().len(), 1); |
| 4032 | let composer = typist.composer.clone(); |
| 4033 | assert!( |
| 4034 | typist |
| 4035 | .cx |
| 4036 | .update(|cx| composer.read(cx).paste_hint.is_none()) |
| 4037 | ); |
| 4038 | typist.press("ctrl-z"); |
| 4039 | assert!(typist.draft().marks().is_empty()); |
| 4040 | } |
| 4041 | |
| 4042 | #[gpui_kit::test] |
| 4043 | fn enter_continues_a_list_instead_of_sending(cx: &mut TestAppContext) { |
| 4044 | let mut typist = typist(cx); |
| 4045 | typist.type_text("1. one"); |
| 4046 | typist.press("enter"); |
| 4047 | typist.type_text("two"); |
| 4048 | assert_eq!(typist.value(), "1. one\n2. two"); |
| 4049 | typist.press("enter enter"); |
| 4050 | assert_eq!(typist.value(), "1. one\n2. two\n"); |
| 4051 | typist.type_text("after"); |
| 4052 | typist.press("enter"); |
| 4053 | assert_eq!(typist.value(), ""); |
| 4054 | } |
| 4055 | |
| 4056 | #[gpui_kit::test] |
| 4057 | fn tab_nests_a_list_item(cx: &mut TestAppContext) { |
| 4058 | let mut typist = typist(cx); |
| 4059 | typist.type_text("- a"); |
| 4060 | typist.press("enter tab"); |
| 4061 | typist.type_text("b"); |
| 4062 | assert_eq!(typist.value(), "• a\n◦ b"); |
| 4063 | typist.press("shift-tab"); |
| 4064 | assert_eq!(typist.value(), "• a\n• b"); |
| 4065 | } |
| 4066 | |
| 4067 | #[gpui_kit::test] |
| 4068 | fn ctrl_b_toggles_bold_on_a_selection_and_for_the_next_text(cx: &mut TestAppContext) { |
| 4069 | use teams_core::{FormatState, MarkKind}; |
| 4070 | |
| 4071 | let mut typist = typist(cx); |
| 4072 | typist.type_text("hello"); |
| 4073 | typist.press("shift-left shift-left ctrl-b"); |
| 4074 | assert_eq!(typist.draft().state(3..5, &MarkKind::Bold), FormatState::On); |
| 4075 | assert_eq!( |
| 4076 | typist.draft().state(0..5, &MarkKind::Bold), |
| 4077 | FormatState::Mixed |
| 4078 | ); |
| 4079 | typist.press("ctrl-b"); |
| 4080 | assert!(typist.draft().marks().is_empty()); |
| 4081 | typist.press("end ctrl-i"); |
| 4082 | typist.type_text("!"); |
| 4083 | assert_eq!(typist.outgoing().html(), "hello<i>!</i>"); |
| 4084 | } |
| 4085 | |
| 4086 | #[gpui_kit::test] |
| 4087 | fn script_shortcuts_exclude_each_other_and_send_teams_html(cx: &mut TestAppContext) { |
| 4088 | let mut typist = typist(cx); |
| 4089 | typist.type_text("x2"); |
| 4090 | typist.press("shift-left ctrl-shift-="); |
| 4091 | assert_eq!(typist.outgoing().html(), "x<sup>2</sup>"); |
| 4092 | typist.press("ctrl-="); |
| 4093 | assert_eq!(typist.outgoing().html(), "x<sub>2</sub>"); |
| 4094 | } |
| 4095 | |
| 4096 | #[gpui_kit::test] |
| 4097 | fn the_toolbar_state_follows_the_selection(cx: &mut TestAppContext) { |
| 4098 | use teams_core::FormatState; |
| 4099 | |
| 4100 | use super::super::FormatButton; |
| 4101 | |
| 4102 | let mut typist = typist(cx); |
| 4103 | typist.type_text("big small"); |
| 4104 | typist.press("shift-left shift-left shift-left shift-left shift-left"); |
| 4105 | let composer = typist.composer.clone(); |
| 4106 | typist.act(|window, cx| { |
| 4107 | composer.update(cx, |composer, cx| { |
| 4108 | composer.press_format(FormatButton::SizeSmall, window, cx) |
| 4109 | }) |
| 4110 | }); |
| 4111 | let states = |typist: &mut Typist<'_>, range: std::ops::Range<usize>| { |
| 4112 | let composer = typist.composer.clone(); |
| 4113 | typist.cx.update(|cx| { |
| 4114 | let composer = composer.read(cx); |
| 4115 | [ |
| 4116 | FormatButton::SizeSmall, |
| 4117 | FormatButton::SizeNormal, |
| 4118 | FormatButton::SizeLarge, |
| 4119 | ] |
| 4120 | .map(|button| composer.format_state(button, &range)) |
| 4121 | }) |
| 4122 | }; |
| 4123 | assert_eq!( |
| 4124 | states(&mut typist, 4..9), |
| 4125 | [FormatState::On, FormatState::Off, FormatState::Off] |
| 4126 | ); |
| 4127 | assert_eq!( |
| 4128 | states(&mut typist, 0..9), |
| 4129 | [FormatState::Mixed, FormatState::Off, FormatState::Off] |
| 4130 | ); |
| 4131 | assert_eq!( |
| 4132 | states(&mut typist, 0..3), |
| 4133 | [FormatState::Off, FormatState::On, FormatState::Off] |
| 4134 | ); |
| 4135 | assert_eq!( |
| 4136 | typist.outgoing().html(), |
| 4137 | "big <span style=\"font-size:xx-small;\">small</span>" |
| 4138 | ); |
| 4139 | } |
| 4140 | |
| 4141 | #[gpui_kit::test] |
| 4142 | fn pasted_markdown_converts_and_ctrl_z_restores_the_raw_text(cx: &mut TestAppContext) { |
| 4143 | let mut typist = typist(cx); |
| 4144 | typist |
| 4145 | .cx |
| 4146 | .write_to_clipboard(gpui_kit::ClipboardItem::new_string( |
| 4147 | "**hi** there".to_owned(), |
| 4148 | )); |
| 4149 | typist.press("ctrl-v"); |
| 4150 | assert_eq!(typist.value(), "hi there"); |
| 4151 | let composer = typist.composer.clone(); |
| 4152 | assert!( |
| 4153 | typist |
| 4154 | .cx |
| 4155 | .update(|cx| composer.read(cx).paste_hint.is_some()) |
| 4156 | ); |
| 4157 | typist.press("ctrl-z"); |
| 4158 | assert_eq!(typist.value(), "**hi** there"); |
| 4159 | assert!(typist.draft().marks().is_empty()); |
| 4160 | } |
| 4161 | |
| 4162 | #[gpui_kit::test] |
| 4163 | fn copy_and_paste_inside_the_composer_keeps_formatting(cx: &mut TestAppContext) { |
| 4164 | use teams_core::{FormatState, MarkKind}; |
| 4165 | |
| 4166 | let mut typist = typist(cx); |
| 4167 | typist.type_text("`code` "); |
| 4168 | typist.press("ctrl-a ctrl-c end ctrl-v"); |
| 4169 | assert_eq!(typist.value(), "code code "); |
| 4170 | assert_eq!(typist.draft().state(5..9, &MarkKind::Code), FormatState::On); |
| 4171 | } |
| 4172 | |
| 4173 | #[gpui_kit::test] |
| 4174 | fn undo_brings_the_formatting_back_with_the_text(cx: &mut TestAppContext) { |
| 4175 | use teams_core::{FormatState, MarkKind}; |
| 4176 | |
| 4177 | let mut typist = typist(cx); |
| 4178 | typist.type_text("~~old~~"); |
| 4179 | typist.press("ctrl-a"); |
| 4180 | typist.type_text("new"); |
| 4181 | typist.press("ctrl-z ctrl-z"); |
| 4182 | assert_eq!(typist.value(), "old"); |
| 4183 | assert_eq!( |
| 4184 | typist.draft().state(0..3, &MarkKind::Strike), |
| 4185 | FormatState::On |
| 4186 | ); |
| 4187 | } |
| 4188 | |
| 4189 | #[gpui_kit::test] |
| 4190 | fn ctrl_k_turns_the_selection_into_a_link(cx: &mut TestAppContext) { |
| 4191 | let mut typist = typist(cx); |
| 4192 | typist.type_text("see docs"); |
| 4193 | typist.press("shift-left shift-left shift-left shift-left ctrl-k"); |
| 4194 | let composer = typist.composer.clone(); |
| 4195 | assert!( |
| 4196 | typist |
| 4197 | .cx |
| 4198 | .update(|cx| composer.read(cx).link_editor.is_some()) |
| 4199 | ); |
| 4200 | typist.type_text("example.com"); |
| 4201 | typist.press("enter"); |
| 4202 | assert!( |
| 4203 | typist |
| 4204 | .cx |
| 4205 | .update(|cx| composer.read(cx).link_editor.is_none()) |
| 4206 | ); |
| 4207 | assert_eq!( |
| 4208 | typist.outgoing().html(), |
| 4209 | "see <a href=\"https://example.com\">docs</a>" |
| 4210 | ); |
| 4211 | } |
| 4212 | |
| 4213 | #[gpui_kit::test] |
| 4214 | fn ctrl_z_takes_back_bold_before_the_typing_and_ctrl_y_redoes_it(cx: &mut TestAppContext) { |
| 4215 | use teams_core::{FormatState, MarkKind}; |
| 4216 | |
| 4217 | let mut typist = typist(cx); |
| 4218 | typist.type_text("hello"); |
| 4219 | typist.press("shift-left shift-left ctrl-b"); |
| 4220 | typist.press("ctrl-z"); |
| 4221 | assert_eq!(typist.value(), "hello"); |
| 4222 | assert!(typist.draft().marks().is_empty()); |
| 4223 | typist.press("ctrl-y"); |
| 4224 | assert_eq!(typist.draft().state(3..5, &MarkKind::Bold), FormatState::On); |
| 4225 | } |
| 4226 | |
| 4227 | #[gpui_kit::test] |
| 4228 | fn undo_after_typing_undoes_the_typing_not_an_older_bold(cx: &mut TestAppContext) { |
| 4229 | use teams_core::{FormatState, MarkKind}; |
| 4230 | |
| 4231 | let mut typist = typist(cx); |
| 4232 | typist.type_text("hello"); |
| 4233 | typist.press("shift-left shift-left ctrl-b end"); |
| 4234 | typist.type_text(" x"); |
| 4235 | typist.press("backspace backspace"); |
| 4236 | assert_eq!(typist.value(), "hello"); |
| 4237 | typist.press("ctrl-z"); |
| 4238 | assert_ne!(typist.value(), "hello"); |
| 4239 | assert_eq!(typist.draft().state(3..5, &MarkKind::Bold), FormatState::On); |
| 4240 | } |
| 4241 | |
| 4242 | #[gpui_kit::test] |
| 4243 | fn delete_at_a_code_line_end_joins_without_padding(cx: &mut TestAppContext) { |
| 4244 | let mut typist = typist(cx); |
| 4245 | typist.type_text("```"); |
| 4246 | typist.press("enter"); |
| 4247 | typist.type_text("a"); |
| 4248 | typist.press("enter"); |
| 4249 | typist.type_text("b"); |
| 4250 | typist.press("up end delete"); |
| 4251 | assert_eq!(typist.outgoing().html(), "<pre>ab</pre>"); |
| 4252 | } |
| 4253 | |
| 4254 | #[gpui_kit::test] |
| 4255 | fn copied_code_has_no_padding_in_the_plain_text(cx: &mut TestAppContext) { |
| 4256 | let mut typist = typist(cx); |
| 4257 | typist.type_text("```"); |
| 4258 | typist.press("enter"); |
| 4259 | typist.type_text(" x"); |
| 4260 | typist.press("ctrl-a ctrl-c"); |
| 4261 | let text = typist.cx.read_from_clipboard().and_then(|item| item.text()); |
| 4262 | assert_eq!(text.as_deref(), Some(" x")); |
| 4263 | } |
| 4264 | |
| 4265 | #[gpui_kit::test] |
| 4266 | fn a_refused_link_keeps_the_field_open_and_says_why(cx: &mut TestAppContext) { |
| 4267 | let mut typist = typist(cx); |
| 4268 | typist.type_text("docs"); |
| 4269 | typist.press("ctrl-a ctrl-k"); |
| 4270 | typist.type_text("javascript:alert(1)"); |
| 4271 | typist.press("enter"); |
| 4272 | let composer = typist.composer.clone(); |
| 4273 | assert!(typist.cx.update(|cx| { |
| 4274 | composer |
| 4275 | .read(cx) |
| 4276 | .link_editor |
| 4277 | .as_ref() |
| 4278 | .is_some_and(|editor| editor.refused) |
| 4279 | })); |
| 4280 | assert!(typist.draft().marks().is_empty()); |
| 4281 | typist.type_text("x"); |
| 4282 | assert!(typist.cx.update(|cx| { |
| 4283 | composer |
| 4284 | .read(cx) |
| 4285 | .link_editor |
| 4286 | .as_ref() |
| 4287 | .is_some_and(|editor| !editor.refused) |
| 4288 | })); |
| 4289 | } |
| 4290 | |
| 4291 | #[gpui_kit::test] |
| 4292 | fn undo_takes_back_a_list_toggle_then_the_bold_before_it(cx: &mut TestAppContext) { |
| 4293 | use teams_core::{FormatState, LineKind, MarkKind}; |
| 4294 | |
| 4295 | let mut typist = typist(cx); |
| 4296 | typist.type_text("hello"); |
| 4297 | typist.press("ctrl-a ctrl-b"); |
| 4298 | let composer = typist.composer.clone(); |
| 4299 | typist.act(|window, cx| { |
| 4300 | composer.update(cx, |composer, cx| { |
| 4301 | composer.press_format(super::super::FormatButton::Bulleted, window, cx) |
| 4302 | }) |
| 4303 | }); |
| 4304 | assert_eq!(typist.value(), "• hello"); |
| 4305 | typist.press("ctrl-z"); |
| 4306 | assert_eq!(typist.value(), "hello"); |
| 4307 | assert_eq!(typist.draft().state(0..5, &MarkKind::Bold), FormatState::On); |
| 4308 | typist.press("ctrl-z"); |
| 4309 | assert!(typist.draft().marks().is_empty()); |
| 4310 | assert_eq!(typist.draft().lines(), [LineKind::Text]); |
| 4311 | } |
| 4312 | |
| 4313 | #[gpui_kit::test] |
| 4314 | fn redo_brings_back_typing_and_bold_in_order(cx: &mut TestAppContext) { |
| 4315 | use teams_core::{FormatState, MarkKind}; |
| 4316 | |
| 4317 | let mut typist = typist(cx); |
| 4318 | typist.type_text("hello"); |
| 4319 | typist.press("ctrl-a ctrl-b ctrl-z ctrl-z"); |
| 4320 | assert_eq!(typist.value(), ""); |
| 4321 | typist.press("ctrl-y"); |
| 4322 | assert_eq!(typist.value(), "hello"); |
| 4323 | assert!(typist.draft().marks().is_empty()); |
| 4324 | typist.press("ctrl-y"); |
| 4325 | assert_eq!(typist.draft().state(0..5, &MarkKind::Bold), FormatState::On); |
| 4326 | } |
| 4327 | |
| 4328 | fn cursor(typist: &mut Typist<'_>) -> usize { |
| 4329 | let composer = typist.composer.clone(); |
| 4330 | typist |
| 4331 | .cx |
| 4332 | .update(|cx| composer.read(cx).input.read(cx).cursor()) |
| 4333 | } |
| 4334 | |
| 4335 | #[gpui_kit::test] |
| 4336 | fn typing_elsewhere_is_its_own_undo_step_and_undo_puts_the_caret_there( |
| 4337 | cx: &mut TestAppContext, |
| 4338 | ) { |
| 4339 | let mut typist = typist(cx); |
| 4340 | typist.type_text("hello"); |
| 4341 | typist.press("home"); |
| 4342 | typist.type_text("X"); |
| 4343 | typist.press("end ctrl-z"); |
| 4344 | assert_eq!(typist.value(), "hello"); |
| 4345 | assert_eq!(cursor(&mut typist), 0); |
| 4346 | typist.press("ctrl-y"); |
| 4347 | assert_eq!(typist.value(), "Xhello"); |
| 4348 | assert_eq!(cursor(&mut typist), 1); |
| 4349 | } |
| 4350 | |
| 4351 | #[gpui_kit::test] |
| 4352 | fn undo_keeps_the_scroll_position(cx: &mut TestAppContext) { |
| 4353 | let mut typist = typist(cx); |
| 4354 | for line in 0..14 { |
| 4355 | typist.type_text(&format!("line {line}")); |
| 4356 | typist.press("shift-enter"); |
| 4357 | } |
| 4358 | typist.type_text("end"); |
| 4359 | let composer = typist.composer.clone(); |
| 4360 | let scroll = |typist: &mut Typist<'_>| { |
| 4361 | typist |
| 4362 | .cx |
| 4363 | .update(|cx| composer.read(cx).input.read(cx).scroll_offset().y) |
| 4364 | }; |
| 4365 | typist.act(|window, cx| window.render_frame(cx)); |
| 4366 | let before = scroll(&mut typist); |
| 4367 | assert!(before < gpui_kit::px(0.)); |
| 4368 | typist.press("ctrl-z"); |
| 4369 | typist.act(|window, cx| window.render_frame(cx)); |
| 4370 | assert_eq!(scroll(&mut typist), before); |
| 4371 | } |
| 4372 | |
| 4373 | #[gpui_kit::test] |
| 4374 | fn a_smiley_taken_back_with_ctrl_z_is_sent_as_typed(cx: &mut TestAppContext) { |
| 4375 | let mut typist = typist(cx); |
| 4376 | typist.type_text("ok :) "); |
| 4377 | assert!(typist.value().contains('🙂')); |
| 4378 | typist.press("ctrl-z"); |
| 4379 | assert_eq!(typist.value(), "ok :) "); |
| 4380 | assert_eq!(typist.outgoing().text(), "ok :)"); |
| 4381 | } |
| 4382 | |
| 4383 | #[gpui_kit::test] |
| 4384 | fn an_undo_with_nothing_to_undo_leaves_conversions_working(cx: &mut TestAppContext) { |
| 4385 | let mut typist = typist(cx); |
| 4386 | typist.press("ctrl-z"); |
| 4387 | typist.type_text("**a**"); |
| 4388 | assert_eq!(typist.value(), "a"); |
| 4389 | } |
| 4390 | |
| 4391 | #[gpui_kit::test] |
| 4392 | fn code_blocks_take_markdown_and_smileys_literally(cx: &mut TestAppContext) { |
| 4393 | let mut typist = typist(cx); |
| 4394 | typist.type_text("```"); |
| 4395 | typist.press("enter"); |
| 4396 | typist.type_text(":) "); |
| 4397 | typist |
| 4398 | .cx |
| 4399 | .write_to_clipboard(gpui_kit::ClipboardItem::new_string("**x**".to_owned())); |
| 4400 | typist.press("ctrl-v"); |
| 4401 | assert_eq!(typist.value(), ":) **x**"); |
| 4402 | let composer = typist.composer.clone(); |
| 4403 | assert!( |
| 4404 | typist |
| 4405 | .cx |
| 4406 | .update(|cx| composer.read(cx).paste_hint.is_none()) |
| 4407 | ); |
| 4408 | let outgoing = typist.outgoing(); |
| 4409 | assert_eq!(outgoing.html(), "<pre>:) **x**</pre>"); |
| 4410 | } |
| 4411 | |
| 4412 | #[gpui_kit::test] |
| 4413 | fn quotes_continue_on_shift_enter_and_send_as_blockquote(cx: &mut TestAppContext) { |
| 4414 | let mut typist = typist(cx); |
| 4415 | typist.type_text(">quoted"); |
| 4416 | typist.press("shift-enter"); |
| 4417 | typist.type_text("more"); |
| 4418 | let outgoing = typist.outgoing(); |
| 4419 | assert_eq!(outgoing.html(), "<blockquote>quoted<br>more</blockquote>"); |
| 4420 | assert_eq!(outgoing.text(), "quoted\nmore"); |
| 4421 | } |
| 4422 | |
| 4423 | #[gpui_kit::test] |
| 4424 | fn enter_in_a_quote_adds_a_line_and_enter_on_the_empty_one_leaves_it( |
| 4425 | cx: &mut TestAppContext, |
| 4426 | ) { |
| 4427 | let mut typist = typist(cx); |
| 4428 | typist.type_text(">quoted"); |
| 4429 | typist.press("enter"); |
| 4430 | typist.type_text("more"); |
| 4431 | typist.press("enter enter"); |
| 4432 | typist.type_text("after"); |
| 4433 | assert_eq!(typist.value(), "quoted\nmore\nafter"); |
| 4434 | assert_eq!( |
| 4435 | typist.outgoing().html(), |
| 4436 | "<blockquote>quoted<br>more</blockquote>after" |
| 4437 | ); |
| 4438 | } |
| 4439 | |
| 4440 | #[gpui_kit::test] |
| 4441 | fn down_at_the_end_of_a_quote_leaves_it(cx: &mut TestAppContext) { |
| 4442 | let mut typist = typist(cx); |
| 4443 | typist.type_text(">quoted"); |
| 4444 | typist.press("down"); |
| 4445 | typist.type_text("after"); |
| 4446 | assert_eq!( |
| 4447 | typist.outgoing().html(), |
| 4448 | "<blockquote>quoted</blockquote>after" |
| 4449 | ); |
| 4450 | } |
| 4451 | |
| 4452 | fn insert_table(typist: &mut Typist<'_>, rows: usize, columns: usize) { |
| 4453 | let composer = typist.composer.clone(); |
| 4454 | typist.act(|window, cx| { |
| 4455 | composer.update(cx, |composer, cx| { |
| 4456 | composer.insert_table(rows, columns, window, cx) |
| 4457 | }); |
| 4458 | window.render_frame(cx); |
| 4459 | }); |
| 4460 | } |
| 4461 | |
| 4462 | #[gpui_kit::test] |
| 4463 | fn a_table_takes_tab_grows_a_row_at_the_end_and_sends_as_teams_html( |
| 4464 | cx: &mut TestAppContext, |
| 4465 | ) { |
| 4466 | let mut typist = typist(cx); |
| 4467 | typist.type_text("before"); |
| 4468 | insert_table(&mut typist, 1, 2); |
| 4469 | typist.type_text("a"); |
| 4470 | typist.press("tab"); |
| 4471 | typist.type_text("b"); |
| 4472 | typist.press("tab"); |
| 4473 | typist.act(|window, cx| window.render_frame(cx)); |
| 4474 | typist.type_text("c"); |
| 4475 | typist.press("down"); |
| 4476 | typist.type_text("after"); |
| 4477 | assert_eq!( |
| 4478 | typist.outgoing().html(), |
| 4479 | "before<figure class=\"table\"><table class=\"copy-paste-table\"><tbody>\ |
| 4480 | <tr><td>a</td><td>b</td></tr><tr><td>c</td><td> </td></tr>\ |
| 4481 | </tbody></table></figure>after" |
| 4482 | ); |
| 4483 | } |
| 4484 | |
| 4485 | fn format_bar_open(typist: &mut Typist<'_>) -> bool { |
| 4486 | let composer = typist.composer.clone(); |
| 4487 | typist.cx.update(|cx| composer.read(cx).format_bar_open()) |
| 4488 | } |
| 4489 | |
| 4490 | #[gpui_kit::test] |
| 4491 | fn ctrl_shift_x_opens_the_format_bar_per_conversation(cx: &mut TestAppContext) { |
| 4492 | let mut typist = typist(cx); |
| 4493 | let composer = typist.composer.clone(); |
| 4494 | let open_chat = |typist: &mut Typist<'_>, id: &'static str| { |
| 4495 | let composer = composer.clone(); |
| 4496 | typist.act(|window, cx| { |
| 4497 | composer.update(cx, |composer, cx| { |
| 4498 | composer.set_conversation(id, "", window, cx); |
| 4499 | composer.focus(window, cx); |
| 4500 | }) |
| 4501 | }); |
| 4502 | }; |
| 4503 | open_chat(&mut typist, "chat-one"); |
| 4504 | assert!(!format_bar_open(&mut typist)); |
| 4505 | typist.press("ctrl-shift-x"); |
| 4506 | assert!(format_bar_open(&mut typist)); |
| 4507 | open_chat(&mut typist, "chat-two"); |
| 4508 | assert!(!format_bar_open(&mut typist)); |
| 4509 | open_chat(&mut typist, "chat-one"); |
| 4510 | assert!(format_bar_open(&mut typist)); |
| 4511 | typist.press("ctrl-shift-x"); |
| 4512 | assert!(!format_bar_open(&mut typist)); |
| 4513 | } |
| 4514 | |
| 4515 | #[gpui_kit::test] |
| 4516 | fn ctrl_alt_x_strikes_through_now_that_ctrl_shift_x_is_format(cx: &mut TestAppContext) { |
| 4517 | let mut typist = typist(cx); |
| 4518 | typist.type_text("gone"); |
| 4519 | typist.press("ctrl-a ctrl-alt-x"); |
| 4520 | assert_eq!(typist.outgoing().html(), "<s>gone</s>"); |
| 4521 | assert!(!format_bar_open(&mut typist)); |
| 4522 | } |
| 4523 | |
| 4524 | #[gpui_kit::test] |
| 4525 | fn the_format_bar_shows_the_style_at_the_caret_without_a_selection( |
| 4526 | cx: &mut TestAppContext, |
| 4527 | ) { |
| 4528 | use teams_core::FormatState; |
| 4529 | |
| 4530 | use super::super::FormatButton; |
| 4531 | |
| 4532 | let mut typist = typist(cx); |
| 4533 | typist.press("ctrl-shift-x ctrl-b"); |
| 4534 | let composer = typist.composer.clone(); |
| 4535 | let bold = |typist: &mut Typist<'_>| { |
| 4536 | let composer = composer.clone(); |
| 4537 | typist.cx.update(|cx| { |
| 4538 | let composer = composer.read(cx); |
| 4539 | let caret = composer.selection(cx); |
| 4540 | composer.button_state(FormatButton::Bold, &caret, false) |
| 4541 | }) |
| 4542 | }; |
| 4543 | assert_eq!(bold(&mut typist).format, FormatState::On); |
| 4544 | typist.type_text("x"); |
| 4545 | assert_eq!(bold(&mut typist).format, FormatState::On); |
| 4546 | typist.press("ctrl-b"); |
| 4547 | assert_eq!(bold(&mut typist).format, FormatState::Off); |
| 4548 | assert!(bold(&mut typist).enabled); |
| 4549 | } |
| 4550 | |
| 4551 | #[gpui_kit::test] |
| 4552 | fn the_format_bar_inserts_a_table_without_a_selection(cx: &mut TestAppContext) { |
| 4553 | use super::super::FormatButton; |
| 4554 | |
| 4555 | let mut typist = typist(cx); |
| 4556 | typist.press("ctrl-shift-x"); |
| 4557 | let composer = typist.composer.clone(); |
| 4558 | typist.act(|window, cx| { |
| 4559 | composer.update(cx, |composer, cx| { |
| 4560 | composer.press_format(FormatButton::Table, window, cx) |
| 4561 | }) |
| 4562 | }); |
| 4563 | let picker_open = typist |
| 4564 | .cx |
| 4565 | .update(|cx| composer.read(cx).table_picker.is_some()); |
| 4566 | assert!(picker_open); |
| 4567 | insert_table(&mut typist, 2, 2); |
| 4568 | assert!(typist.draft().lines()[0].is_table()); |
| 4569 | } |
| 4570 | |
| 4571 | #[gpui_kit::test] |
| 4572 | fn ctrl_alt_plus_and_minus_open_the_teams_row_and_column_menus(cx: &mut TestAppContext) { |
| 4573 | use super::super::table::TableLineMenu; |
| 4574 | |
| 4575 | let mut typist = typist(cx); |
| 4576 | let composer = typist.composer.clone(); |
| 4577 | let menu = |typist: &mut Typist<'_>| { |
| 4578 | let composer = composer.clone(); |
| 4579 | typist |
| 4580 | .cx |
| 4581 | .update(|cx| composer.read(cx).table_menu.map(|menu| menu.kind)) |
| 4582 | }; |
| 4583 | typist.press("ctrl-alt-="); |
| 4584 | assert_eq!(menu(&mut typist), None); |
| 4585 | insert_table(&mut typist, 1, 1); |
| 4586 | typist.type_text("a"); |
| 4587 | typist.press("ctrl-alt-="); |
| 4588 | assert_eq!(menu(&mut typist), Some(TableLineMenu::Insert)); |
| 4589 | assert!(format_bar_open(&mut typist)); |
| 4590 | typist.press("enter"); |
| 4591 | assert_eq!(menu(&mut typist), None); |
| 4592 | let shape = |typist: &mut Typist<'_>| { |
| 4593 | let draft = typist.draft(); |
| 4594 | let table = draft.table(0).expect("table").clone(); |
| 4595 | (table.row_count(), table.column_count()) |
| 4596 | }; |
| 4597 | assert_eq!(shape(&mut typist), (1, 2)); |
| 4598 | typist.press("ctrl-alt-- enter"); |
| 4599 | assert_eq!(shape(&mut typist), (1, 1)); |
| 4600 | typist.press("ctrl-alt-- escape"); |
| 4601 | assert_eq!(menu(&mut typist), None); |
| 4602 | assert_eq!(shape(&mut typist), (1, 1)); |
| 4603 | } |
| 4604 | |
| 4605 | #[gpui_kit::test] |
| 4606 | fn ctrl_a_highlights_a_table_like_selected_text(cx: &mut TestAppContext) { |
| 4607 | use gpui_kit::VisualTestContext; |
| 4608 | |
| 4609 | use super::super::table::SELECTED_TABLE_TAG; |
| 4610 | |
| 4611 | let mut typist = typist(cx); |
| 4612 | typist.type_text("before"); |
| 4613 | insert_table(&mut typist, 1, 2); |
| 4614 | typist.type_text("a"); |
| 4615 | typist.press("down"); |
| 4616 | typist.type_text("after"); |
| 4617 | let handle = typist.handle; |
| 4618 | let mut visual = VisualTestContext::from_window(handle, typist.cx); |
| 4619 | visual.update(|window, cx| window.render_frame(cx)); |
| 4620 | assert!(visual.debug_bounds(SELECTED_TABLE_TAG).is_none()); |
| 4621 | visual.simulate_keystrokes("ctrl-a"); |
| 4622 | visual.run_until_parked(); |
| 4623 | visual.update(|window, cx| window.render_frame(cx)); |
| 4624 | assert!(visual.debug_bounds(SELECTED_TABLE_TAG).is_some()); |
| 4625 | } |
| 4626 | |
| 4627 | #[gpui_kit::test] |
| 4628 | fn deleting_the_last_column_deletes_the_table(cx: &mut TestAppContext) { |
| 4629 | let mut typist = typist(cx); |
| 4630 | insert_table(&mut typist, 1, 1); |
| 4631 | let composer = typist.composer.clone(); |
| 4632 | typist.act(|window, cx| { |
| 4633 | composer.update(cx, |composer, cx| { |
| 4634 | composer.edit_table(teams_core::TableEdit::DeleteColumn, window, cx) |
| 4635 | }); |
| 4636 | }); |
| 4637 | assert!(typist.draft().lines().iter().all(|kind| !kind.is_table())); |
| 4638 | } |
| 4639 | |
| 4640 | #[gpui_kit::test] |
| 4641 | fn a_pasted_markdown_table_becomes_a_table(cx: &mut TestAppContext) { |
| 4642 | let mut typist = typist(cx); |
| 4643 | typist |
| 4644 | .cx |
| 4645 | .write_to_clipboard(gpui_kit::ClipboardItem::new_string( |
| 4646 | "| A | B |\n|---|---|\n| 1 | 2 |".to_owned(), |
| 4647 | )); |
| 4648 | typist.press("ctrl-v"); |
| 4649 | assert!(typist.draft().lines()[0].is_table()); |
| 4650 | assert_eq!( |
| 4651 | typist.outgoing().html(), |
| 4652 | "<figure class=\"table\"><table class=\"copy-paste-table\"><tbody>\ |
| 4653 | <tr><td>A</td><td>B</td></tr><tr><td>1</td><td>2</td></tr>\ |
| 4654 | </tbody></table></figure>" |
| 4655 | ); |
| 4656 | } |
| 4657 | |
| 4658 | #[gpui_kit::test] |
| 4659 | fn ctrl_shift_v_pastes_markdown_as_plain_text(cx: &mut TestAppContext) { |
| 4660 | let mut typist = typist(cx); |
| 4661 | typist |
| 4662 | .cx |
| 4663 | .write_to_clipboard(gpui_kit::ClipboardItem::new_string( |
| 4664 | "**hi** there".to_owned(), |
| 4665 | )); |
| 4666 | typist.press("ctrl-shift-v"); |
| 4667 | assert_eq!(typist.value(), "**hi** there"); |
| 4668 | assert!(typist.draft().marks().is_empty()); |
| 4669 | } |
| 4670 | |
| 4671 | #[gpui_kit::test] |
| 4672 | fn demo_upload_runs_to_done_and_a_file_alone_can_be_sent(cx: &mut TestAppContext) { |
| 4673 | cx.update(gpui_kit::init); |
| 4674 | let store = Arc::new(Store::open_in_memory().unwrap()); |
| 4675 | let mode = Mode { |
| 4676 | demo: true, |
| 4677 | read_only: false, |
| 4678 | demo_sync: None, |
| 4679 | }; |
| 4680 | let (handle, composer) = cx.update(|cx| { |
| 4681 | let app = cx.new(|_| AppState::new(store, mode)); |
| 4682 | gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { |
| 4683 | cx.new(|cx| Composer::new(app, window, cx)) |
| 4684 | }) |
| 4685 | .unwrap() |
| 4686 | }); |
| 4687 | let directory = tempfile::tempdir().unwrap(); |
| 4688 | let path = directory.path().join("plan.pdf"); |
| 4689 | std::fs::write(&path, b"%PDF-1.7").unwrap(); |
| 4690 | cx.update_window(handle, |_, window, cx| { |
| 4691 | composer.update(cx, |composer, cx| { |
| 4692 | composer.set_conversation("demo-chat", "Demo", window, cx); |
| 4693 | composer.add_paths(vec![path], window, cx); |
| 4694 | }); |
| 4695 | }) |
| 4696 | .unwrap(); |
| 4697 | cx.run_until_parked(); |
| 4698 | cx.update(|cx| { |
| 4699 | let composer = composer.read(cx); |
| 4700 | assert!(composer.tray.blocks_send()); |
| 4701 | assert!(!composer.can_send(cx)); |
| 4702 | }); |
| 4703 | cx.executor() |
| 4704 | .advance_clock(std::time::Duration::from_secs(3)); |
| 4705 | cx.run_until_parked(); |
| 4706 | cx.update(|cx| { |
| 4707 | let composer = composer.read(cx); |
| 4708 | assert!(composer.can_send(cx)); |
| 4709 | let outgoing = composer |
| 4710 | .outgoing(cx) |
| 4711 | .expect("an attachment alone is a message"); |
| 4712 | assert_eq!(outgoing.text(), ""); |
| 4713 | assert_eq!(outgoing.files.len(), 1); |
| 4714 | assert_eq!(outgoing.files[0].name, "plan.pdf"); |
| 4715 | }); |
| 4716 | } |
| 4717 | |
| 4718 | #[gpui_kit::test] |
| 4719 | fn attachments_are_dropped_when_the_conversation_changes(cx: &mut TestAppContext) { |
| 4720 | cx.update(gpui_kit::init); |
| 4721 | let store = Arc::new(Store::open_in_memory().unwrap()); |
| 4722 | let mode = Mode { |
| 4723 | demo: true, |
| 4724 | read_only: false, |
| 4725 | demo_sync: None, |
| 4726 | }; |
| 4727 | let (handle, composer) = cx.update(|cx| { |
| 4728 | let app = cx.new(|_| AppState::new(store, mode)); |
| 4729 | gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { |
| 4730 | cx.new(|cx| Composer::new(app, window, cx)) |
| 4731 | }) |
| 4732 | .unwrap() |
| 4733 | }); |
| 4734 | let directory = tempfile::tempdir().unwrap(); |
| 4735 | let path = directory.path().join("plan.pdf"); |
| 4736 | std::fs::write(&path, b"%PDF-1.7").unwrap(); |
| 4737 | cx.update_window(handle, |_, window, cx| { |
| 4738 | composer.update(cx, |composer, cx| { |
| 4739 | composer.set_conversation("demo-chat", "Demo", window, cx); |
| 4740 | composer.add_paths(vec![path], window, cx); |
| 4741 | }); |
| 4742 | }) |
| 4743 | .unwrap(); |
| 4744 | cx.run_until_parked(); |
| 4745 | cx.update_window(handle, |_, window, cx| { |
| 4746 | composer.update(cx, |composer, cx| { |
| 4747 | composer.set_conversation("other-chat", "Other", window, cx) |
| 4748 | }); |
| 4749 | }) |
| 4750 | .unwrap(); |
| 4751 | cx.update(|cx| assert!(composer.read(cx).tray.items().is_empty())); |
| 4752 | } |
| 4753 | |
| 4754 | fn demo_composer( |
| 4755 | cx: &mut TestAppContext, |
| 4756 | ) -> (gpui_kit::AnyWindowHandle, gpui_kit::Entity<Composer>) { |
| 4757 | cx.update(gpui_kit::init); |
| 4758 | let store = Arc::new(Store::open_in_memory().unwrap()); |
| 4759 | let mode = Mode { |
| 4760 | demo: true, |
| 4761 | read_only: false, |
| 4762 | demo_sync: None, |
| 4763 | }; |
| 4764 | let (handle, composer) = cx.update(|cx| { |
| 4765 | let app = cx.new(|_| AppState::new(store, mode)); |
| 4766 | gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { |
| 4767 | cx.new(|cx| Composer::new(app, window, cx)) |
| 4768 | }) |
| 4769 | .unwrap() |
| 4770 | }); |
| 4771 | (handle, composer) |
| 4772 | } |
| 4773 | |
| 4774 | fn small_png(directory: &std::path::Path) -> std::path::PathBuf { |
| 4775 | let path = directory.join("dot.png"); |
| 4776 | image::RgbaImage::new(2, 2).save(&path).unwrap(); |
| 4777 | path |
| 4778 | } |
| 4779 | |
| 4780 | #[gpui_kit::test] |
| 4781 | fn editing_keeps_the_attachments_and_hides_them_from_the_edit(cx: &mut TestAppContext) { |
| 4782 | use super::super::{EditPreview, Outgoing}; |
| 4783 | |
| 4784 | let (handle, composer) = demo_composer(cx); |
| 4785 | let directory = tempfile::tempdir().unwrap(); |
| 4786 | let path = directory.path().join("plan.pdf"); |
| 4787 | std::fs::write(&path, b"%PDF-1.7").unwrap(); |
| 4788 | cx.update_window(handle, |_, window, cx| { |
| 4789 | composer.update(cx, |composer, cx| { |
| 4790 | composer.set_conversation("demo-chat", "Demo", window, cx); |
| 4791 | composer.add_paths(vec![path], window, cx); |
| 4792 | }); |
| 4793 | }) |
| 4794 | .unwrap(); |
| 4795 | cx.run_until_parked(); |
| 4796 | cx.executor() |
| 4797 | .advance_clock(std::time::Duration::from_secs(3)); |
| 4798 | cx.run_until_parked(); |
| 4799 | cx.update_window(handle, |_, window, cx| { |
| 4800 | composer.update(cx, |composer, cx| { |
| 4801 | let draft = Outgoing { |
| 4802 | draft: teams_core::Draft::plain("old"), |
| 4803 | subject: None, |
| 4804 | mentions: Vec::new(), |
| 4805 | link_preview: None, |
| 4806 | reply: None, |
| 4807 | edit: Some(EditPreview { |
| 4808 | message_id: "m1".into(), |
| 4809 | excerpt: "old".into(), |
| 4810 | scheduled: None, |
| 4811 | }), |
| 4812 | images: Vec::new(), |
| 4813 | files: Vec::new(), |
| 4814 | importance: Default::default(), |
| 4815 | }; |
| 4816 | composer.begin_edit(draft, window, cx); |
| 4817 | }); |
| 4818 | }) |
| 4819 | .unwrap(); |
| 4820 | cx.update(|cx| { |
| 4821 | let composer = composer.read(cx); |
| 4822 | assert_eq!(composer.tray.items().len(), 1); |
| 4823 | let outgoing = composer.outgoing(cx).unwrap(); |
| 4824 | assert!(outgoing.files.is_empty()); |
| 4825 | assert_eq!(outgoing.text(), "old"); |
| 4826 | }); |
| 4827 | cx.update_window(handle, |_, window, cx| { |
| 4828 | composer.update(cx, |composer, cx| composer.cancel_edit(window, cx)); |
| 4829 | }) |
| 4830 | .unwrap(); |
| 4831 | cx.update(|cx| { |
| 4832 | let composer = composer.read(cx); |
| 4833 | assert_eq!(composer.tray.items().len(), 1); |
| 4834 | assert!(composer.can_send(cx)); |
| 4835 | }); |
| 4836 | } |
| 4837 | |
| 4838 | #[gpui_kit::test] |
| 4839 | fn draft_images_follow_only_into_the_chat_that_was_chosen(cx: &mut TestAppContext) { |
| 4840 | let (handle, composer) = demo_composer(cx); |
| 4841 | let directory = tempfile::tempdir().unwrap(); |
| 4842 | let path = small_png(directory.path()); |
| 4843 | for (carry, chosen, expected) in [ |
| 4844 | (Some("chat-a"), "chat-a", 1), |
| 4845 | (None, "chat-b", 0), |
| 4846 | (Some("chat-a"), "chat-b", 0), |
| 4847 | ] { |
| 4848 | cx.update_window(handle, |_, window, cx| { |
| 4849 | composer.update(cx, |composer, cx| { |
| 4850 | composer.set_conversation("", "", window, cx); |
| 4851 | composer.add_paths(vec![path.clone()], window, cx); |
| 4852 | }); |
| 4853 | }) |
| 4854 | .unwrap(); |
| 4855 | cx.run_until_parked(); |
| 4856 | cx.update_window(handle, |_, window, cx| { |
| 4857 | composer.update(cx, |composer, cx| { |
| 4858 | if let Some(chat) = carry { |
| 4859 | composer.carry_images_into(chat.to_owned()); |
| 4860 | } |
| 4861 | composer.set_conversation(chosen, "Chat", window, cx); |
| 4862 | }); |
| 4863 | }) |
| 4864 | .unwrap(); |
| 4865 | cx.update(|cx| assert_eq!(composer.read(cx).tray.items().len(), expected)); |
| 4866 | } |
| 4867 | } |
| 4868 | |
| 4869 | #[gpui_kit::test] |
| 4870 | fn each_conversation_keeps_its_own_draft(cx: &mut TestAppContext) { |
| 4871 | let (handle, composer) = demo_composer(cx); |
| 4872 | cx.update_window(handle, |_, window, cx| { |
| 4873 | composer.update(cx, |composer, cx| { |
| 4874 | composer.set_conversation("chat-a", "A", window, cx); |
| 4875 | composer.set_text("**hello** there", window, cx); |
| 4876 | composer.set_conversation("chat-b", "B", window, cx); |
| 4877 | }); |
| 4878 | }) |
| 4879 | .unwrap(); |
| 4880 | cx.update(|cx| { |
| 4881 | let composer = composer.read(cx); |
| 4882 | assert!(composer.draft.is_blank()); |
| 4883 | let stored = composer |
| 4884 | .app |
| 4885 | .read(cx) |
| 4886 | .store |
| 4887 | .draft("chat-a") |
| 4888 | .unwrap() |
| 4889 | .unwrap(); |
| 4890 | assert_eq!(stored.preview, "hello there"); |
| 4891 | }); |
| 4892 | cx.update_window(handle, |_, window, cx| { |
| 4893 | composer.update(cx, |composer, cx| { |
| 4894 | composer.set_text("other", window, cx); |
| 4895 | composer.set_conversation("chat-a", "A", window, cx); |
| 4896 | }); |
| 4897 | }) |
| 4898 | .unwrap(); |
| 4899 | cx.update(|cx| { |
| 4900 | let composer = composer.read(cx); |
| 4901 | assert_eq!(composer.draft.text(), "hello there"); |
| 4902 | assert!(composer.draft.to_html().contains("<b>")); |
| 4903 | assert!( |
| 4904 | composer |
| 4905 | .app |
| 4906 | .read(cx) |
| 4907 | .store |
| 4908 | .draft("chat-b") |
| 4909 | .unwrap() |
| 4910 | .is_some() |
| 4911 | ); |
| 4912 | }); |
| 4913 | } |
| 4914 | |
| 4915 | #[gpui_kit::test] |
| 4916 | fn sending_clears_the_stored_draft_and_a_blank_composer_deletes_it( |
| 4917 | cx: &mut TestAppContext, |
| 4918 | ) { |
| 4919 | let (handle, composer) = demo_composer(cx); |
| 4920 | cx.update_window(handle, |_, window, cx| { |
| 4921 | composer.update(cx, |composer, cx| { |
| 4922 | composer.set_conversation("chat-a", "A", window, cx); |
| 4923 | composer.set_text("hi", window, cx); |
| 4924 | composer.save_draft_now(cx); |
| 4925 | }); |
| 4926 | }) |
| 4927 | .unwrap(); |
| 4928 | cx.update(|cx| { |
| 4929 | assert!( |
| 4930 | composer |
| 4931 | .read(cx) |
| 4932 | .app |
| 4933 | .read(cx) |
| 4934 | .store |
| 4935 | .draft("chat-a") |
| 4936 | .unwrap() |
| 4937 | .is_some() |
| 4938 | ); |
| 4939 | }); |
| 4940 | cx.update_window(handle, |_, window, cx| { |
| 4941 | composer.update(cx, |composer, cx| composer.submit_current(window, cx)); |
| 4942 | }) |
| 4943 | .unwrap(); |
| 4944 | cx.update(|cx| { |
| 4945 | assert!( |
| 4946 | composer |
| 4947 | .read(cx) |
| 4948 | .app |
| 4949 | .read(cx) |
| 4950 | .store |
| 4951 | .draft("chat-a") |
| 4952 | .unwrap() |
| 4953 | .is_none() |
| 4954 | ); |
| 4955 | }); |
| 4956 | } |
| 4957 | |
| 4958 | #[gpui_kit::test] |
| 4959 | fn a_draft_target_change_drops_the_files_of_the_old_target_and_keeps_images( |
| 4960 | cx: &mut TestAppContext, |
| 4961 | ) { |
| 4962 | let (handle, composer) = demo_composer(cx); |
| 4963 | let directory = tempfile::tempdir().unwrap(); |
| 4964 | let png = small_png(directory.path()); |
| 4965 | let pdf = directory.path().join("plan.pdf"); |
| 4966 | std::fs::write(&pdf, b"%PDF-1.7").unwrap(); |
| 4967 | cx.update_window(handle, |_, window, cx| { |
| 4968 | composer.update(cx, |composer, cx| { |
| 4969 | composer.set_conversation("bob-chat", "Bob", window, cx); |
| 4970 | composer.add_paths(vec![png, pdf], window, cx); |
| 4971 | }); |
| 4972 | }) |
| 4973 | .unwrap(); |
| 4974 | cx.run_until_parked(); |
| 4975 | cx.update(|cx| assert_eq!(composer.read(cx).tray.items().len(), 2)); |
| 4976 | cx.update_window(handle, |_, window, cx| { |
| 4977 | composer.update(cx, |composer, cx| { |
| 4978 | composer.carry_images_into(String::new()); |
| 4979 | composer.set_conversation("", "", window, cx); |
| 4980 | }); |
| 4981 | }) |
| 4982 | .unwrap(); |
| 4983 | cx.update(|cx| { |
| 4984 | let composer = composer.read(cx); |
| 4985 | assert_eq!(composer.tray.items().len(), 1); |
| 4986 | assert_eq!(composer.tray.items()[0].name, "dot.png"); |
| 4987 | assert!(!composer.files_allowed()); |
| 4988 | }); |
| 4989 | } |
| 4990 | } |
| 4991 | } |