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