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