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