use std::cell::Cell; use std::collections::{HashMap, HashSet}; use std::ops::Range; use std::path::PathBuf; use std::rc::Rc; use std::time::{Duration, Instant}; use crate::views::title_bar::TITLE_BAR_HEIGHT; use chrono::{DateTime, Utc}; use gpui_kit::assets::IconName; use gpui_kit::component::{ ActiveTheme as _, Sizable as _, button::{Button, ButtonCustomVariant, ButtonVariants as _}, h_flex, input::{ Backspace, Copy, Cut, Delete, Enter, Escape, IndentInline, InlineToken, InputContent, InputEvent, InputState, MoveDown, MoveRight, MoveUp, OutdentInline, RangeDecorationCollection, Redo, TextDecorationCollection, Textarea, TextareaMode, TextareaState, Undo, }, popover::Popover, tooltip::Tooltip, v_flex, }; use gpui_kit::prelude::FluentBuilder as _; use gpui_kit::*; use teams_core::{ CODE_HEADER_MARK, Draft, DraftLine, Edit, EnterKeyBehavior, FileReference, FormatState, LineKind, LinkPreview, MarkKind, MentionCandidate, MentionInput, OBJECT_MARK, SizeStep, TABLE_MARK, TableEdit, TypingStyle, UpArrowBehavior, UploadedFile, changed_span, has_markdown, link_url, map_offset, reverse_edits, }; mod attach_menu; mod cloud_picker; mod code_language; pub(crate) mod delivery; mod drafts; mod format_menus; mod kvt; pub(crate) mod plus_menu; mod post; mod rewrite; pub use post::{ POST_ANNOUNCEMENT_TAG, POST_ANNOUNCEMENT_TOGGLE_TAG, POST_AUDIENCE_TAG, POST_CARD_DIVIDER_TAG, POST_CARD_FOOTER_TAG, POST_CARD_HEADER_TAG, POST_CARD_SEND_TAG, POST_CARD_SUBJECT_TAG, SUBJECT_PLACEHOLDER, }; mod rule; mod schedule; mod slash; mod table; use super::actions::{ COMPOSER_KEY_CONTEXT, DeleteTable, DeleteTableColumn, DeleteTableRow, EditLastMessage, EditLink, InsertTableColumnLeft, InsertTableColumnRight, InsertTableLine, InsertTableRowAbove, InsertTableRowBelow, OpenSwitcher, PastePlainText, RemoveTableLine, ScheduleSend, SetHeading1, SetHeading2, SetHeading3, SetParagraph, ToggleBold, ToggleCode, ToggleCodeBlock, ToggleFormatBar, ToggleItalic, ToggleQuote, ToggleStrike, ToggleSubscript, ToggleSuperscript, ToggleUnderline, }; use super::attachment_tray::{ AttachmentTray, DoneFile, JobKind, LoadedFile, MAX_ATTACHMENTS, PasteAction, UploadJob, UploadResult, discard_uploaded, inline_preview, names_of, paste_action, pasted_image_name, prepare_pasted_image, read_attachment, render_tray, }; use super::avatar::{person_avatar, square_avatar}; use super::draft_style::draft_style; use super::emoji_popup::{self, EmojiPopup}; use super::format_toolbar::{self, ButtonState, FormatButton}; use super::fun_picker::{FunPicker, FunPickerEvent}; use super::link_preview::{ComposeLink, draft_link, link_preview_card, meeting_join_chip}; use super::widgets::{icon, symbol}; use crate::app_state::AppState; use crate::data::Directory; use crate::emoji; pub use crate::outgoing::{EditPreview, Outgoing, ReplyPreview}; use crate::remote_image::RemoteImage; use crate::runtime; use crate::scheduled_rows::scheduled_label; use crate::theme::{self, ElevatedSurface as _}; use crate::typing::OutgoingTyping; use code_language::is_code_header_token; use format_menus::{FormatPopup, paragraph_label}; use rule::{is_rule_token, render_rule_token}; use slash::SlashPopup; use table::{TableKey, TableLineMenu, TableMenu, TablePicker, TableViews, is_table_token}; const MIN_ROWS: usize = 1; const MAX_ROWS: usize = 8; const MENTION_DEBOUNCE: Duration = Duration::from_millis(150); const MENTION_LIMIT: usize = 8; const LINK_PREVIEW_DEBOUNCE: Duration = Duration::from_millis(600); const MENTION_QUERY_MAX_CHARS: usize = 32; const MENTION_QUERY_MAX_WORDS: usize = 3; const POPUP_WIDTH: f32 = 380.; const POPUP_ROW_HEIGHT: f32 = 44.; const POPUP_PADDING: f32 = 6.; const POPUP_GAP: f32 = 6.; pub const MENTION_POPUP_TAG: &str = "composer/mention-popup"; const DEMO_UPLOAD_STEPS: u8 = 10; const COMPOSER_PLACEHOLDER: &str = "Type a message"; const SEND_DIVIDER_HEIGHT: f32 = 20.; const COMPOSER_ICON_SIZE: f32 = 20.; const COMPOSER_BUTTON_SIZE: f32 = 32.; const COMPOSER_BUTTON_INSET: f32 = 6.; pub const COMPOSER_BOX_TAG: &str = "composer"; pub const COMPOSER_ACTIONS_TAG: &str = "composer/actions"; fn send_divider() -> Div { div() .flex_none() .w(px(1.)) .h(px(SEND_DIVIDER_HEIGHT)) .ml(px(COMPOSER_BUTTON_INSET)) .bg(theme::border()) } pub const COMPOSER_STACKED_ACTIONS_TAG: &str = "composer/stacked-actions"; const STACKED_ACTIONS_WIDTH: f32 = 400.; pub const COMPOSER_INPUT_TAG: &str = "composer/input"; pub const COMPOSER_SEND_TAG: &str = "composer/send"; pub const COMPOSER_FORMAT_TAG: &str = "composer/actions/format"; pub const COMPOSER_FORMAT_BAR_TAG: &str = "composer/format-bar"; pub const COMPOSER_FORMAT_DIVIDER_TAG: &str = "composer/format-bar/divider"; /// gpui-kit draws a `Size::Size` button's icon at this fraction of the button size. const BUTTON_ICON_SCALE: f32 = 0.75; const DEMO_UPLOAD_STEP: Duration = Duration::from_millis(120); const DEMO_FAILURE_STEP: u8 = 6; const HISTORY_LIMIT: usize = 200; const PASTE_HINT_DURATION: Duration = Duration::from_secs(4); const IMAGE_TOKEN_PREFIX: &str = "image-"; pub fn bind_keys(cx: &mut App) { super::shortcuts::bind_scopes(cx, &[super::shortcuts::Scope::Composer]); } enum UploadEvent { Progress(u8), Uploaded(UploadedFile), Finished(UploadResult), } enum UploadHandle { Network(tokio::task::AbortHandle), Demo(Task<()>), } impl UploadHandle { fn cancel(self) { match self { UploadHandle::Network(handle) => handle.abort(), UploadHandle::Demo(task) => drop(task), } } } pub enum ComposerEvent { Submit(Box), Schedule { outgoing: Box, send_at: DateTime, }, EditLast, FocusMessages, Typing(bool), Edited, ClosePost, RestorePost(Option), } struct MentionPopup { range: Range, query: String, candidates: Vec, highlighted: usize, } /// An automatic edit Backspace takes back while nothing changed since. struct Conversion { value: String, cursor: usize, before: Draft, restore: Vec, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum StepKind { Typing { at: usize, after_space: bool, }, Deleting { start: usize, end: usize, }, CellTyping { table: usize, row: usize, column: usize, }, Other, } /// One state of the composer's own undo history: text, tokens, formatting and selection. struct HistoryStep { draft: Draft, tokens: Vec<(Range, InlineToken)>, selection: Range, kind: StepKind, } struct LinkEditor { range: Range, field: Entity, refused: bool, _edits: Subscription, } type DraftDecorations = ( TextDecorationCollection, RangeDecorationCollection, ); pub struct Composer { app: Entity, input: Entity, conversation_id: Option, inline: bool, reply: Option, editing: Option, mention_inputs: HashMap, mention_counter: usize, popup: Option, lookup: Option>, emoji_popup: Option, emoji_dismissed_at: Option, slash_popup: Option, slash_dismissed: bool, recent_emoji: emoji::Recent, conversion: Option, undone_value: Option, previous_value: String, typing: OutgoingTyping, draft: Draft, decorations: DraftDecorations, pending_style: Option, history: Vec, history_index: usize, pasted_markdown: Option, paste_hint: Option>, copied: Option<(String, Vec)>, toolbar_dismissed: Option>, mouse_selecting: bool, link_editor: Option, composer_bounds: Rc>>>, tray: AttachmentTray, uploads: HashMap, demo_failed: HashSet, carry_images_into: Option, draft_save: Option>, draft_writes: drafts::DraftWrites, saved_images: Option, schedule: Option, scheduling: Option, scheduling_for: Option, draft_before_edit: Option<(Outgoing, Vec)>, pending_anchors: HashMap, link: ComposeLink, fun_picker: Entity, tables: TableViews, table_counter: usize, table_picker: Option, table_menu: Option, /// The first line of the code block whose language menu is open. code_language_menu: Option, format_popup: Option, format_overflow: bool, format_bar: HashSet, post_subject: Option>, announcement: Option, audience: Option<(String, usize)>, active_cell: Option<(String, teams_core::Cell)>, caret_before_table: usize, caret_text: String, importance: store::MessageImportance, rewrite: rewrite::RewritePanel, attach_menu: attach_menu::AttachMenu, cloud_picker: Option<(Entity, Subscription)>, delivery_menu: bool, plus_menu: bool, _subscriptions: [Subscription; 4], } impl EventEmitter for Composer {} /// With "Start a new line", plain Enter breaks the line and only Ctrl+Enter sends. pub fn enter_starts_line(behavior: EnterKeyBehavior, shift: bool, secondary: bool) -> bool { behavior == EnterKeyBehavior::NewLine && !shift && !secondary } pub fn enter_behavior(setting: EnterKeyBehavior, expanded: bool) -> EnterKeyBehavior { if expanded { EnterKeyBehavior::NewLine } else { setting } } pub fn up_arrow_event(behavior: UpArrowBehavior) -> Option { match behavior { UpArrowBehavior::EditLastMessage => Some(ComposerEvent::EditLast), UpArrowBehavior::NavigateToMessages => Some(ComposerEvent::FocusMessages), UpArrowBehavior::StayInCompose => None, } } pub fn is_plain_enter(event: &InputEvent) -> bool { matches!(event, InputEvent::PressEnter { shift: false, .. }) } /// Enter sends, Shift+Enter is a newline (handled by the input itself). Blank text never sends. pub fn submitted_text(event: &InputEvent, value: &str) -> Option { match event { InputEvent::PressEnter { shift: false, .. } => { let trimmed = value.trim(); (!trimmed.is_empty()).then(|| trimmed.to_owned()) } _ => None, } } pub fn active_mention( text: &str, cursor: usize, tokens: &[Range], ) -> Option<(Range, String)> { if cursor > text.len() || !text.is_char_boundary(cursor) { return None; } let before = &text[..cursor]; let at = before.rfind('@')?; if before[..at] .chars() .next_back() .is_some_and(|previous| !previous.is_whitespace()) { return None; } let query = &before[at + 1..]; let rejected = query.contains('\n') || query.starts_with(char::is_whitespace) || query.ends_with(char::is_whitespace) || query.chars().count() > MENTION_QUERY_MAX_CHARS || query.split_whitespace().count() > MENTION_QUERY_MAX_WORDS || tokens .iter() .any(|token| token.start < cursor && at < token.end); (!rejected).then(|| (at..cursor, query.to_owned())) } fn candidate_subtitle(candidate: &MentionCandidate) -> String { match candidate { MentionCandidate::Person(person) => person .job_title .clone() .filter(|title| !title.trim().is_empty()) .or_else(|| person.mail.clone()) .unwrap_or_default(), MentionCandidate::Channel { .. } => "Channel".to_owned(), MentionCandidate::Team { .. } => "Team".to_owned(), MentionCandidate::Tag { .. } => "Tag".to_owned(), MentionCandidate::Everyone { .. } => String::new(), } } fn candidate_avatar(candidate: &MentionCandidate, directory: &Directory) -> AnyElement { let name = candidate.display_name(); match candidate { MentionCandidate::Person(person) => { person_avatar(directory, Some(&person.user_id), name, 28.) } MentionCandidate::Everyone { .. } => symbol_avatar("group", 28., 14.), MentionCandidate::Tag { .. } => symbol_avatar("tag", 28., 7.), other => square_avatar(name, candidate_key(other), 28., 7.).into_any_element(), } } fn symbol_avatar(symbol_name: &str, size: f32, radius: f32) -> AnyElement { div() .size(px(size)) .flex_none() .flex() .items_center() .justify_center() .rounded(px(radius)) .bg(theme::surface_raised()) .child(symbol(symbol_name, 16., theme::text())) .into_any_element() } fn step_kind(previous: &str, value: &str, cursor: usize) -> StepKind { if let Some(typed) = emoji::typed_char(previous, value, cursor).filter(|typed| *typed != '\n') { return StepKind::Typing { at: cursor - typed.len_utf8(), after_space: typed.is_whitespace(), }; } let removed = previous.len().saturating_sub(value.len()); let one_char_removed = removed > 0 && previous.is_char_boundary(cursor) && previous .get(cursor..cursor + removed) .is_some_and(|gone| gone.chars().count() == 1 && gone != "\n") && previous.get(..cursor) == value.get(..cursor) && previous.get(cursor + removed..) == value.get(cursor..); if one_char_removed { StepKind::Deleting { start: cursor, end: cursor + removed, } } else { StepKind::Other } } fn continues(last: &HistoryStep, kind: StepKind) -> bool { let caret = &last.selection; match (last.kind, kind) { ( StepKind::Typing { after_space, .. }, StepKind::Typing { at, after_space: space, }, ) => (!after_space || space) && caret.is_empty() && caret.start == at, (StepKind::Deleting { .. }, StepKind::Deleting { start, end }) => { caret.is_empty() && (caret.start == end || caret.start == start) } (previous @ StepKind::CellTyping { .. }, next @ StepKind::CellTyping { .. }) => { previous == next } _ => false, } } fn looks_like_url(text: &str) -> bool { ["https://", "http://", "www."] .iter() .any(|prefix| text.starts_with(prefix)) && !text.contains(char::is_whitespace) } fn image_token(id: u64) -> InlineToken { InlineToken::new(format!("{IMAGE_TOKEN_PREFIX}{id}"), OBJECT_MARK.to_string()).block() } fn image_id(token: &InlineToken) -> Option { token.id().strip_prefix(IMAGE_TOKEN_PREFIX)?.parse().ok() } fn candidate_key(candidate: &MentionCandidate) -> &str { match candidate { MentionCandidate::Person(person) => &person.user_id, MentionCandidate::Channel { channel_id, .. } => channel_id, MentionCandidate::Team { team_id, .. } => team_id, MentionCandidate::Everyone { chat_id } => chat_id, MentionCandidate::Tag { tag_id, .. } => tag_id, } } impl Composer { pub fn new(app: Entity, window: &mut Window, cx: &mut Context) -> Self { let input = cx.new(|cx| { TextareaState::new(window, cx) .auto_grow(MIN_ROWS, MAX_ROWS) .submit_on_enter(true) .placeholder("Type a message") }); let subscription = cx.subscribe_in(&input, window, Self::on_input_event); let observer = cx.observe_in(&input, window, |this, _, window, cx| { this.redirect_caret_from_table(window, cx); cx.notify(); }); let image_observer = cx.observe(&app, |this, _, cx| { if this.link_image_pending(cx) { cx.notify(); } }); let fun_picker = cx.new(|cx| FunPicker::new(app.clone(), window, cx)); let picker_subscription = cx.subscribe_in(&fun_picker, window, Self::on_fun_pick); let decorations = input.update(cx, |state, cx| { ( state.create_decorations_collection(Vec::new(), cx), state.create_range_decorations_collection(Vec::new(), cx), ) }); let recent_emoji = emoji::Recent::load(&app.read(cx).store); Composer { app, input, conversation_id: None, inline: false, reply: None, editing: None, mention_inputs: HashMap::new(), mention_counter: 0, popup: None, lookup: None, emoji_popup: None, emoji_dismissed_at: None, slash_popup: None, slash_dismissed: false, recent_emoji, conversion: None, undone_value: None, previous_value: String::new(), typing: OutgoingTyping::default(), draft: Draft::default(), decorations, pending_style: None, history: vec![HistoryStep { draft: Draft::default(), tokens: Vec::new(), selection: 0..0, kind: StepKind::Other, }], history_index: 0, pasted_markdown: None, paste_hint: None, copied: None, toolbar_dismissed: None, mouse_selecting: false, link_editor: None, composer_bounds: Rc::new(Cell::new(None)), tray: AttachmentTray::default(), uploads: HashMap::new(), demo_failed: HashSet::new(), carry_images_into: None, draft_save: None, draft_writes: drafts::DraftWrites::default(), saved_images: None, schedule: None, scheduling: None, scheduling_for: None, draft_before_edit: None, pending_anchors: HashMap::new(), link: ComposeLink::default(), fun_picker, tables: TableViews::default(), table_counter: 0, table_picker: None, table_menu: None, code_language_menu: None, format_popup: None, format_overflow: false, format_bar: HashSet::new(), post_subject: None, announcement: None, audience: None, active_cell: None, caret_before_table: 0, caret_text: String::new(), importance: store::MessageImportance::Normal, rewrite: rewrite::RewritePanel::default(), attach_menu: attach_menu::AttachMenu::default(), cloud_picker: None, delivery_menu: false, plus_menu: false, _subscriptions: [subscription, observer, image_observer, picker_subscription], } } fn on_input_event( &mut self, input: &Entity, event: &InputEvent, window: &mut Window, cx: &mut Context, ) { cx.notify(); if matches!(event, InputEvent::Change) { self.on_change(window, cx); self.update_emoji(cx); self.update_mention(cx); self.update_slash(cx); self.update_link_preview(cx); self.schedule_draft_save(cx); cx.emit(ComposerEvent::Edited); } let value = input.read(cx).value(); if submitted_text(event, &value).is_some() || (is_plain_enter(event) && self.has_attachments(cx)) { self.submit_current(window, cx); } } fn update_mention(&mut self, cx: &mut Context) { let state = self.input.read(cx); let tokens: Vec> = state.tokens().iter().map(|span| span.range()).collect(); let found = active_mention(&state.value(), state.cursor(), &tokens); let Some((range, query)) = found else { self.close_mention_popup(); return; }; let unchanged = self .popup .as_ref() .is_some_and(|popup| popup.query == query && popup.range == range); if unchanged { return; } let previous = self.popup.take(); self.popup = Some(MentionPopup { range, query: query.clone(), candidates: previous.map(|popup| popup.candidates).unwrap_or_default(), highlighted: 0, }); self.request_candidates(query, cx); } fn close_popup(&mut self) { self.close_mention_popup(); self.emoji_popup = None; self.slash_popup = None; } fn close_mention_popup(&mut self) { self.popup = None; self.lookup = None; } fn report_typing(&mut self, has_text: bool, cx: &mut Context) { if self.editing.is_some() { return; } if let Some(active) = self.typing.on_text(has_text, Instant::now()) { cx.emit(ComposerEvent::Typing(active)); } } pub fn stop_typing(&mut self) -> bool { self.typing.stop() } fn on_change(&mut self, window: &mut Window, cx: &mut Context) { let state = self.input.read(cx); let (value, input_cursor) = (state.value().to_string(), state.cursor()); self.report_typing(!value.trim().is_empty(), cx); let previous = std::mem::replace(&mut self.previous_value, value.clone()); if previous != value { // The menu names its block by line, which an edit can move. self.code_language_menu = None; let (start, old_end, new_end) = changed_span(&previous, &value); self.shift_anchors(start, old_end, new_end); } if value == self.draft.text() { self.refresh_style(cx); return; } let kind = step_kind(&previous, &value, input_cursor); let typing = self.pending_style.take(); let cursor = self.draft.apply_edit(&value, input_cursor, typing.as_ref()); let edits = self.draft.take_edits(); self.apply_to_input(edits, cursor..cursor, window, cx); self.record(kind, cx); if let Some(pasted) = self.pasted_markdown.take() && self.convert_paste(&pasted, cursor, window, cx) { return; } let typed = emoji::typed_char(&previous, self.draft.text(), cursor); if typed.is_some() && !self.app.read(cx).markdown_mode && self.convert_markdown(cursor, window, cx) { return; } if matches!(typed, Some(' ' | '\n')) && self.convert_link(cursor, window, cx) { return; } self.convert_emoji(typed, cursor, window, cx); } fn convert_link(&mut self, cursor: usize, window: &mut Window, cx: &mut Context) -> bool { let before = self.draft.clone(); if !self.draft.autolink_typed(cursor) { return false; } self.finish_conversion(before, cursor, window, cx); true } fn convert_markdown( &mut self, cursor: usize, window: &mut Window, cx: &mut Context, ) -> bool { let before = self.draft.clone(); let Some((cursor, style)) = self.draft.convert_typed(cursor) else { self.draft.take_edits(); return false; }; self.finish_conversion(before, cursor, window, cx); self.pending_style = (!style.is_empty()).then_some(style); true } fn convert_emoji( &mut self, typed: Option, cursor: usize, window: &mut Window, cx: &mut Context, ) { if self.draft.in_code(cursor) { return; } let value = self.draft.text(); let found = match typed { Some(':') => emoji::closing_code(value, cursor) .and_then(|(range, code)| Some((range, emoji::lookup(code)?))), Some(' ') => emoji::smiley_before_space(value, cursor), _ => None, }; let Some((range, glyph)) = found else { return; }; let before = self.draft.clone(); self.draft.replace(range.clone(), glyph); self.finish_conversion(before, cursor + glyph.len() - range.len(), window, cx); self.remember_emoji(glyph, cx); } fn finish_conversion( &mut self, before: Draft, cursor: usize, window: &mut Window, cx: &mut Context, ) { let edits = self.draft.take_edits(); let restore = reverse_edits(&edits, before.text()); self.apply_to_input(edits, cursor..cursor, window, cx); self.record(StepKind::Other, cx); self.conversion = Some(Conversion { value: self.draft.text().to_owned(), cursor, before, restore, }); } fn convert_paste( &mut self, pasted: &str, cursor: usize, window: &mut Window, cx: &mut Context, ) -> bool { let pasted = pasted.replace("\r\n", "\n"); let Some(start) = cursor .checked_sub(pasted.len()) .filter(|start| self.draft.text().get(*start..cursor) == Some(pasted.as_str())) else { return false; }; let lines = Draft::from_markdown(&pasted).slice(0..usize::MAX); let cursor = self.draft.insert_lines(start..cursor, &lines); let changed_text = self.draft.text().get(start..cursor) != Some(pasted.as_str()); self.commit(Some(cursor), window, cx); if !changed_text { return true; } self.paste_hint = Some(cx.spawn(async move |this, cx| { cx.background_executor().timer(PASTE_HINT_DURATION).await; this.update(cx, |this, cx| { this.paste_hint = None; cx.notify(); }) .ok(); })); true } /// Puts the draft's pending edits into the input, then places the selection. fn commit(&mut self, cursor: Option, window: &mut Window, cx: &mut Context) { let edits = self.draft.take_edits(); let selection = self.input.read(cx).selected_range(); let selection = match cursor { Some(cursor) => cursor..cursor, None if edits.is_empty() => selection, None => map_offset(&edits, selection.start)..map_offset(&edits, selection.end), }; self.apply_to_input(edits, selection, window, cx); self.record(StepKind::Other, cx); self.schedule_draft_save(cx); } fn apply_to_input( &mut self, edits: Vec, selection: Range, window: &mut Window, cx: &mut Context, ) { self.input.update(cx, |state, cx| { let edited = !edits.is_empty(); if edited { state.apply_edits(&edits, window, cx); } if edited || state.selected_range() != selection { state.set_selected_range(selection.clone(), cx); } }); for anchor in self.pending_anchors.values_mut() { *anchor = map_offset(&edits, *anchor); } let value = self.input.read(cx).value().to_string(); if value != self.draft.text() { self.draft.apply_edit(&value, selection.end, None); self.draft.take_edits(); } self.previous_value = value; self.refresh_style(cx); self.sync_tables(window, cx); } fn shift_anchors(&mut self, start: usize, old_end: usize, new_end: usize) { for anchor in self.pending_anchors.values_mut() { *anchor = if *anchor <= start { *anchor } else if *anchor >= old_end { *anchor - old_end + new_end } else { new_end }; } } /// Adds the current state as an undo step. Typing and deleting runs merge into one step; /// a typed space ends a typing step. fn record(&mut self, kind: StepKind, cx: &App) { let state = self.input.read(cx); let step = HistoryStep { draft: self.draft.clone(), tokens: state .tokens() .iter() .map(|span| (span.range(), span.token().clone())) .collect(), selection: state.selected_range(), kind, }; let at_end = self.history_index + 1 == self.history.len(); let merges = at_end && self .history .last() .is_some_and(|last| continues(last, kind)); if merges { if let Some(last) = self.history.last_mut() { *last = step; } return; } self.history.truncate(self.history_index + 1); self.history.push(step); if self.history.len() > HISTORY_LIMIT { self.history.remove(0); } self.history_index = self.history.len() - 1; } /// Ctrl+Z / Ctrl+Y move through the composer's own history; the input's is never used. fn step_history(&mut self, back: bool, window: &mut Window, cx: &mut Context) { let target = if back { self.history_index.checked_sub(1) } else { Some(self.history_index + 1).filter(|index| *index < self.history.len()) }; let Some(target) = target else { return; }; let source_selection = self.history[self.history_index].selection.clone(); self.history_index = target; let step = &self.history[target]; let (draft, tokens) = (step.draft.clone(), step.tokens.clone()); let current = self.input.read(cx).value().to_string(); let (start, old_end, new_end) = changed_span(¤t, draft.text()); let selection = match (start == old_end && start == new_end, back) { (false, _) => new_end..new_end, (true, true) => source_selection, (true, false) => step.selection.clone(), }; if start != old_end || start != new_end { self.shift_anchors(start, old_end, new_end); } self.draft = draft.clone(); self.previous_value = draft.text().to_owned(); self.input.update(cx, |state, cx| { if start != old_end || start != new_end { state.apply_edits( &[(start..old_end, draft.text()[start..new_end].to_owned())], window, cx, ); } for (range, token) in tokens { let present = state.tokens().iter().any(|span| span.range() == range); if !present && range.start >= start && range.end <= new_end { state .replace_range_with_token(range, token, window, cx) .ok(); } } state.set_selected_range(selection, cx); }); self.undone_value = Some(self.input.read(cx).value().to_string()); self.pending_style = None; self.conversion = None; self.paste_hint = None; self.refresh_style(cx); self.sync_tables(window, cx); self.update_emoji(cx); self.update_mention(cx); self.schedule_draft_save(cx); cx.notify(); } fn refresh_style(&mut self, cx: &mut Context) { let style = draft_style(&self.draft, cx.theme().mono_font_family.clone()); let (text, ranges) = &self.decorations; text.set(style.text, cx); ranges.set(style.ranges, cx); self.input.update(cx, |state, cx| { state.set_line_indents(style.indents, cx); state.set_line_labels(style.labels, theme::text_faint(), cx); state.set_hanging_indents(style.markers, cx); }); } fn load_draft( &mut self, draft: Draft, content: InputContent, window: &mut Window, cx: &mut Context, ) { let end = draft.text().len(); self.input.update(cx, |state, cx| { state.set_value(content, window, cx); state.set_selected_range(end..end, cx); }); self.previous_value = draft.text().to_owned(); self.draft = draft; self.code_language_menu = None; self.pending_style = None; self.conversion = None; self.pasted_markdown = None; self.paste_hint = None; self.link_editor = None; self.toolbar_dismissed = None; self.pending_anchors.clear(); self.active_cell = None; self.table_picker = None; self.format_popup = None; self.format_overflow = false; self.sync_tables(window, cx); self.history.clear(); self.history_index = 0; self.record(StepKind::Other, cx); self.refresh_style(cx); } /// Returns the cursor, kept at the same spot relative to the text after `range`. fn replace_text( &mut self, range: Range, text: &str, cursor: usize, window: &mut Window, cx: &mut Context, ) -> usize { let cursor = cursor + text.len() - range.len(); self.input.update(cx, |state, cx| { state.set_selected_range(range, cx); state.replace(text.to_owned(), window, cx); state.set_selected_range(cursor..cursor, cx); }); cursor } fn undo_conversion(&mut self, window: &mut Window, cx: &mut Context) -> bool { let state = self.input.read(cx); let unchanged = self.conversion.as_ref().is_some_and(|conversion| { conversion.value == state.value().as_ref() && conversion.cursor == state.cursor() }); let Some(conversion) = self.conversion.take().filter(|_| unchanged) else { return false; }; let cursor = map_offset(&conversion.restore, conversion.cursor); self.draft = conversion.before; self.pending_style = None; self.apply_to_input(conversion.restore, cursor..cursor, window, cx); self.record(StepKind::Other, cx); self.undone_value = Some(self.input.read(cx).value().to_string()); self.update_emoji(cx); true } fn selection(&self, cx: &App) -> Range { self.input.read(cx).selected_range() } fn toggle_mark(&mut self, kind: MarkKind, cx: &mut Context) { let selection = self.selection(cx); if selection.is_empty() { let cursor = selection.start; let mut pending = self .pending_style .take() .filter(|pending| pending.at == cursor) .unwrap_or(TypingStyle { at: cursor, ..TypingStyle::default() }); pending.toggle(kind, &self.draft.style_at(cursor)); self.pending_style = (!pending.is_empty()).then_some(pending); } else { self.draft.toggle(selection, kind); self.draft.take_edits(); self.record(StepKind::Other, cx); self.refresh_style(cx); self.schedule_draft_save(cx); } cx.notify(); } fn set_size(&mut self, size: Option, cx: &mut Context) { let selection = self.selection(cx); if selection.is_empty() { let cursor = selection.start; let mut pending = self .pending_style .take() .filter(|pending| pending.at == cursor) .unwrap_or(TypingStyle { at: cursor, ..TypingStyle::default() }); pending.set_size(size, &self.draft.style_at(cursor)); self.pending_style = (!pending.is_empty()).then_some(pending); } else { self.draft.set_size(selection, size); self.draft.take_edits(); self.record(StepKind::Other, cx); self.refresh_style(cx); self.schedule_draft_save(cx); } cx.notify(); } fn normal_size_state(&self, range: Range) -> FormatState { let sized = [SizeStep::Small, SizeStep::Large] .into_iter() .any(|step| self.draft.state(range.clone(), &MarkKind::Size(step)) != FormatState::Off); if sized { FormatState::Off } else { FormatState::On } } fn toggle_lines(&mut self, kind: LineKind, window: &mut Window, cx: &mut Context) { let selection = self.selection(cx); self.draft.toggle_lines(selection, kind); self.commit(None, window, cx); cx.notify(); } fn press_format(&mut self, button: FormatButton, window: &mut Window, cx: &mut Context) { if !button.opens_popup() { self.format_overflow = false; } match button { FormatButton::Bold => self.toggle_mark(MarkKind::Bold, cx), FormatButton::Italic => self.toggle_mark(MarkKind::Italic, cx), FormatButton::Underline => self.toggle_mark(MarkKind::Underline, cx), FormatButton::Strike => self.toggle_mark(MarkKind::Strike, cx), FormatButton::Superscript => self.toggle_mark(MarkKind::Superscript, cx), FormatButton::Subscript => self.toggle_mark(MarkKind::Subscript, cx), FormatButton::SizeSmall => self.set_size(Some(SizeStep::Small), cx), FormatButton::SizeNormal => self.set_size(None, cx), FormatButton::SizeLarge => self.set_size(Some(SizeStep::Large), cx), FormatButton::Code => self.toggle_mark(MarkKind::Code, cx), FormatButton::Link => self.open_link_editor(window, cx), FormatButton::Bulleted => self.toggle_lines(LineKind::Bullet(0), window, cx), FormatButton::Numbered => self.toggle_lines(LineKind::Numbered(0), window, cx), FormatButton::Quote => self.toggle_lines(LineKind::Quote, window, cx), FormatButton::Table => { self.table_menu = None; self.toolbar_dismissed = Some(self.selection(cx)); self.open_table_picker(cx); } FormatButton::TableInsertLine => { self.open_table_menu(TableLineMenu::Insert, window, cx); } FormatButton::TableRemoveLine => { self.open_table_menu(TableLineMenu::Remove, window, cx); } FormatButton::Highlight => self.toggle_format_popup(FormatPopup::Highlight, cx), FormatButton::FontColor => self.toggle_format_popup(FormatPopup::FontColor, cx), FormatButton::ParagraphStyle => { self.toggle_format_popup(FormatPopup::ParagraphStyle, cx) } FormatButton::ClearFormatting => self.clear_formatting(window, cx), FormatButton::Outdent => self.indent_blocks(true, window, cx), FormatButton::Indent => self.indent_blocks(false, window, cx), FormatButton::CodeBlock => self.toggle_code_block(window, cx), FormatButton::Rule => self.insert_rule(window, cx), FormatButton::FontSize => self.toggle_format_popup(FormatPopup::FontSize, cx), FormatButton::More => self.toggle_format_overflow(cx), } } fn typing_state(&self, cursor: usize, kind: &MarkKind) -> FormatState { let style = self.draft.style_at(cursor); let style = match self.pending_style.as_ref() { Some(pending) if pending.at == cursor => pending.apply(style), _ => style, }; if style.iter().any(|active| active.same_format(kind)) { FormatState::On } else { FormatState::Off } } fn format_state(&self, button: FormatButton, selection: &Range) -> FormatState { let range = selection.clone(); let mark = match button { FormatButton::Bold => Some(MarkKind::Bold), FormatButton::Italic => Some(MarkKind::Italic), FormatButton::Underline => Some(MarkKind::Underline), FormatButton::Strike => Some(MarkKind::Strike), FormatButton::Superscript => Some(MarkKind::Superscript), FormatButton::Subscript => Some(MarkKind::Subscript), FormatButton::SizeSmall => Some(MarkKind::Size(SizeStep::Small)), FormatButton::SizeLarge => Some(MarkKind::Size(SizeStep::Large)), FormatButton::Code => Some(MarkKind::Code), FormatButton::Link => Some(MarkKind::Link(String::new())), _ => None, }; if range.is_empty() { if let Some(kind) = &mark { return self.typing_state(range.start, kind); } if button == FormatButton::SizeNormal { let sized = [SizeStep::Small, SizeStep::Large].into_iter().any(|step| { self.typing_state(range.start, &MarkKind::Size(step)) == FormatState::On }); return if sized { FormatState::Off } else { FormatState::On }; } } match button { FormatButton::Bold => self.draft.state(range, &MarkKind::Bold), FormatButton::Italic => self.draft.state(range, &MarkKind::Italic), FormatButton::Underline => self.draft.state(range, &MarkKind::Underline), FormatButton::Strike => self.draft.state(range, &MarkKind::Strike), FormatButton::Superscript => self.draft.state(range, &MarkKind::Superscript), FormatButton::Subscript => self.draft.state(range, &MarkKind::Subscript), FormatButton::SizeSmall => self.draft.state(range, &MarkKind::Size(SizeStep::Small)), FormatButton::SizeLarge => self.draft.state(range, &MarkKind::Size(SizeStep::Large)), FormatButton::SizeNormal => self.normal_size_state(range), FormatButton::Code => self.draft.state(range, &MarkKind::Code), FormatButton::Link => self.draft.state(range, &MarkKind::Link(String::new())), FormatButton::Bulleted => self.draft.line_state(range, &LineKind::Bullet(0)), FormatButton::Numbered => self.draft.line_state(range, &LineKind::Numbered(0)), FormatButton::Quote => self.draft.line_state(range, &LineKind::Quote), FormatButton::Table if self.table_picker.is_some() => FormatState::On, FormatButton::TableInsertLine if self .table_menu .is_some_and(|menu| menu.kind == TableLineMenu::Insert) => { FormatState::On } FormatButton::TableRemoveLine if self .table_menu .is_some_and(|menu| menu.kind == TableLineMenu::Remove) => { FormatState::On } FormatButton::Table | FormatButton::TableInsertLine | FormatButton::TableRemoveLine => { FormatState::Off } FormatButton::Highlight if self.format_popup == Some(FormatPopup::Highlight) => { FormatState::On } FormatButton::FontColor if self.format_popup == Some(FormatPopup::FontColor) => { FormatState::On } FormatButton::ParagraphStyle if self.format_popup == Some(FormatPopup::ParagraphStyle) => { FormatState::On } FormatButton::FontSize if self.format_popup == Some(FormatPopup::FontSize) => { FormatState::On } FormatButton::More if self.format_overflow => FormatState::On, FormatButton::CodeBlock => self.draft.line_state(range, &LineKind::Code(None)), FormatButton::Highlight | FormatButton::FontColor | FormatButton::FontSize | FormatButton::More | FormatButton::ParagraphStyle | FormatButton::ClearFormatting | FormatButton::Outdent | FormatButton::Indent | FormatButton::Rule => FormatState::Off, } } /// Text formats do not apply inside a table cell; the row and column menus only there. fn button_state( &self, button: FormatButton, selection: &Range, in_table: bool, ) -> ButtonState { let enabled = match button { FormatButton::TableInsertLine | FormatButton::TableRemoveLine => in_table, FormatButton::Table | FormatButton::More => true, FormatButton::Outdent => !in_table && self.draft.can_outdent(selection.clone()), _ => !in_table, }; let state = if enabled { ButtonState::enabled(self.format_state(button, selection)) } else { ButtonState::disabled() }; match button { FormatButton::ParagraphStyle => { state.with_label(paragraph_label(self.draft.heading_at(selection.start))) } _ => state, } } pub(super) fn format_bar_open(&self) -> bool { self.format_bar .contains(self.conversation_id.as_deref().unwrap_or_default()) } pub(super) fn set_format_bar(&mut self, open: bool, cx: &mut Context) { let key = self.conversation_id.clone().unwrap_or_default(); if open { self.format_bar.insert(key); } else { self.format_bar.remove(&key); self.table_picker = None; self.table_menu = None; self.format_popup = None; self.format_overflow = false; } cx.notify(); } fn toggle_format_bar(&mut self, window: &mut Window, cx: &mut Context) { let open = !self.format_bar_open(); self.set_format_bar(open, cx); if !self.cell_has_focus(window, cx) { self.focus(window, cx); } } fn edit_link(&mut self, window: &mut Window, cx: &mut Context) { if self.link_editor.is_some() { return; } if self.draft.is_blank() && self.selection(cx).is_empty() { window.dispatch_action(Box::new(OpenSwitcher), cx); return; } self.open_link_editor(window, cx); } fn open_link_editor(&mut self, window: &mut Window, cx: &mut Context) { let range = self.selection(cx); let prefill = self .draft .link_in(range.clone()) .or_else(|| { cx.read_from_clipboard() .and_then(|item| item.text()) .map(|text| text.trim().to_owned()) .filter(|text| looks_like_url(text)) }) .unwrap_or_default(); let field = cx.new(|cx| { InputState::new(window, cx) .placeholder("Paste or type a link") .default_value(prefill) }); field.update(cx, |state, cx| { state.focus(window, cx); state.select_all(window, cx); }); let edits = cx.subscribe(&field, |this, _, event: &InputEvent, cx| { if matches!(event, InputEvent::Change) && let Some(editor) = this.link_editor.as_mut() { editor.refused = false; cx.notify(); } }); self.link_editor = Some(LinkEditor { range, field, refused: false, _edits: edits, }); cx.notify(); } fn apply_link(&mut self, window: &mut Window, cx: &mut Context) { let Some(editor) = self.link_editor.take() else { return; }; let typed = editor.field.read(cx).value().trim().to_owned(); let url = if typed.is_empty() { String::new() } else if let Some(url) = link_url(&typed) { url } else { self.link_editor = Some(LinkEditor { refused: true, ..editor }); cx.notify(); return; }; self.focus(window, cx); if editor.range.is_empty() { if !url.is_empty() { let end = self.draft.insert_link(editor.range.start, &url, &url); self.commit(Some(end), window, cx); } } else { self.draft.set_link(editor.range.clone(), &url); self.draft.take_edits(); self.refresh_style(cx); self.input.update(cx, |state, cx| { state.set_selected_range(editor.range.clone(), cx) }); self.record(StepKind::Other, cx); self.toolbar_dismissed = Some(editor.range); } cx.notify(); } fn close_link_editor(&mut self, window: &mut Window, cx: &mut Context) { let Some(editor) = self.link_editor.take() else { return; }; self.focus(window, cx); self.input .update(cx, |state, cx| state.set_selected_range(editor.range, cx)); cx.notify(); } fn break_line(&mut self, shift: bool, window: &mut Window, cx: &mut Context) -> bool { let selection = self.selection(cx); if !selection.is_empty() { return false; } match self.draft.break_line(selection.start, shift) { Some(cursor) => { self.draft.link_word_ending_at(selection.start); self.commit(Some(cursor), window, cx); true } None => false, } } fn backspace_at_start(&mut self, window: &mut Window, cx: &mut Context) -> bool { let selection = self.selection(cx); if !selection.is_empty() { return false; } match self.draft.backspace_at_start(selection.start) { Some(cursor) => { self.commit(Some(cursor), window, cx); true } None => false, } } fn delete_forward(&mut self, window: &mut Window, cx: &mut Context) -> bool { let selection = self.selection(cx); if !selection.is_empty() { return false; } match self.draft.delete_forward(selection.start) { Some(cursor) => { self.commit(Some(cursor), window, cx); true } None => false, } } fn leave_quote(&mut self, window: &mut Window, cx: &mut Context) -> bool { let selection = self.selection(cx); if !selection.is_empty() || self.input.read(cx).value().as_ref() != self.draft.text() { return false; } match self.draft.leave_quote_at_end(selection.start) { Some(cursor) => { self.commit(Some(cursor), window, cx); true } None => false, } } /// Backspace right after a table or Delete right before one goes into it instead of /// joining the line with the table's. fn enter_table_beside( &mut self, after: bool, window: &mut Window, cx: &mut Context, ) -> bool { let selection = self.selection(cx); if !selection.is_empty() || self.input.read(cx).value().as_ref() != self.draft.text() { return false; } let found = if after { self.draft.table_before(selection.start) } else { self.draft.table_after(selection.start) }; let Some(index) = found else { return false; }; self.focus_table_line(index, after, window, cx); true } fn side_input_focused(&self, window: &Window, cx: &App) -> bool { self.rewrite.input_focused(window, cx) || self.attach_menu.input_focused(window, cx) } fn cell_has_focus(&self, window: &Window, cx: &App) -> bool { self.focused_cell(window, cx).is_some() } fn paste_plain(&mut self, window: &mut Window, cx: &mut Context) { let Some(text) = cx.read_from_clipboard().and_then(|item| item.text()) else { return; }; let text = text .replace("\r\n", "\n") .replace([OBJECT_MARK, TABLE_MARK], ""); if let Some(input) = self.focused_cell_input(window, cx) { input.update(cx, |state, cx| state.replace(text, window, cx)); return; } self.pasted_markdown = None; self.input .update(cx, |state, cx| state.replace(text, window, cx)); } fn indent(&mut self, outdent: bool, window: &mut Window, cx: &mut Context) -> bool { let selection = self.selection(cx); if !self.draft.indent(selection, outdent) { return false; } self.commit(None, window, cx); true } fn copy_selection(&mut self, cx: &mut Context) -> bool { let selection = self.selection(cx); if selection.is_empty() || self.draft.text().get(selection.clone()).is_none() { return false; } let selected = &self.draft.text()[selection.clone()]; let lines = if selected.contains(OBJECT_MARK) { self.draft.slice_without_objects(selection) } else { self.draft.slice(selection) }; let text = if selected.contains(TABLE_MARK) { Draft::from_lines(&lines).plain_text() } else { selected.replace([OBJECT_MARK, CODE_HEADER_MARK], "") }; if text.is_empty() { return true; } cx.write_to_clipboard(ClipboardItem::new_string_with_json_metadata( text.clone(), lines.clone(), )); self.copied = Some((text, lines)); true } fn copied_lines(&self, item: &ClipboardItem, text: &str) -> Option> { item.entries() .iter() .find_map(|entry| match entry { ClipboardEntry::String(string) if string.text() == text => { string.metadata_json::>() } _ => None, }) .or_else(|| { let normalized = text.replace("\r\n", "\n"); self.copied .as_ref() .filter(|(copied, _)| *copied == normalized) .map(|(_, lines)| lines.clone()) }) } fn toolbar_requested(&self, cx: &App) -> bool { let selection = self.selection(cx); !selection.is_empty() && !self.mouse_selecting && self.toolbar_dismissed.as_ref() != Some(&selection) && !self.popup_is_open() } fn remember_emoji(&mut self, glyph: &str, cx: &App) { let store = &self.app.read(cx).store; self.recent_emoji.reload(store); self.recent_emoji.push(glyph); self.recent_emoji.save(store); } fn update_emoji(&mut self, cx: &mut Context) { let state = self.input.read(cx); let found = emoji::active_query(&state.value(), state.cursor()) .filter(|_| !self.draft.in_code(state.cursor())); if found.as_ref().map(|(range, _)| range.start) != self.emoji_dismissed_at { self.emoji_dismissed_at = None; } let found = found.filter(|_| self.emoji_dismissed_at.is_none()); let Some((range, query)) = found else { self.emoji_popup = None; return; }; let unchanged = self .emoji_popup .as_ref() .is_some_and(|popup| popup.query == query && popup.range == range); if unchanged { return; } let matches = emoji::search(&query, &self.recent_emoji.glyphs(), emoji_popup::LIMIT); let skin_tone = teams_core::skin_tone(&self.app.read(cx).store); self.emoji_popup = (!matches.is_empty()).then(|| EmojiPopup::new(range, query, matches, skin_tone)); } fn accept_emoji(&mut self, window: &mut Window, cx: &mut Context) { let Some(popup) = self.emoji_popup.take() else { return; }; let Some(glyph) = popup.selected_glyph() else { return; }; let cursor = popup.range.end; self.replace_text(popup.range, &glyph, cursor, window, cx); self.remember_emoji(&crate::emoticons::without_tone(&glyph), cx); cx.notify(); } fn request_candidates(&mut self, query: String, cx: &mut Context) { let Some(conversation_id) = self.conversation_id.clone() else { return; }; let (demo, engine) = { let state = self.app.read(cx); (state.mode.demo, state.engine.clone()) }; if demo { let candidates = crate::demo::mention_candidates(&conversation_id, &query); self.apply_candidates(&query, candidates, cx); return; } let Some(engine) = engine else { return; }; self.lookup = Some(cx.spawn(async move |this, cx| { cx.background_executor().timer(MENTION_DEBOUNCE).await; let task_query = query.clone(); let receiver = runtime::spawn(async move { engine .mention_candidates(&conversation_id, &task_query, MENTION_LIMIT) .await }); if let Ok(Ok(candidates)) = receiver.await { this.update(cx, |this, cx| this.apply_candidates(&query, candidates, cx)) .ok(); } })); } fn apply_candidates( &mut self, query: &str, candidates: Vec, cx: &mut Context, ) { let Some(popup) = self.popup.as_mut().filter(|popup| popup.query == query) else { return; }; popup.candidates = candidates; popup.highlighted = 0; cx.notify(); } fn update_link_preview(&mut self, cx: &mut Context) { if self.editing.is_some() { self.link.reset(); return; } let draft = self.current_draft(cx); let meeting = teams_core::meeting_link_in_html(&draft.to_html()); if let Some(url) = self.link.observe(draft_link(&draft), draft.is_blank()) { self.fetch_link_preview(url, cx); } self.link.meeting = meeting; cx.notify(); } fn fetch_link_preview(&mut self, url: String, cx: &mut Context) { let (demo, engine) = { let state = self.app.read(cx); (state.mode.demo, state.engine.clone()) }; if demo { let preview = crate::demo::link_preview(&url); self.finish_link_preview(&url, preview, cx); return; } let Some(engine) = engine else { return; }; self.link.lookup = Some(cx.spawn(async move |this, cx| { cx.background_executor().timer(LINK_PREVIEW_DEBOUNCE).await; let task_url = url.clone(); let receiver = runtime::spawn(async move { engine.link_preview_for(&task_url).await }); if let Ok(Ok(preview)) = receiver.await { this.update(cx, |this, cx| this.finish_link_preview(&url, preview, cx)) .ok(); } })); } fn finish_link_preview( &mut self, url: &str, preview: Option, cx: &mut Context, ) { let image = preview.as_ref().and_then(LinkPreview::image); if !self.link.apply(url, preview) { return; } if let Some(image) = image { self.app .update(cx, |state, cx| state.request_images(vec![image], cx)); } cx.notify(); } fn link_image_pending(&self, cx: &App) -> bool { self.link .shown() .and_then(LinkPreview::image) .is_some_and(|image| self.app.read(cx).directory.image(&image.url).is_none()) } fn render_meeting_chip(&self) -> Option
{ let url = self.link.meeting.clone()?; Some( h_flex() .w_full() .mb(px(6.)) .child(meeting_join_chip("composer-meeting-join".to_owned(), url)), ) } fn render_link_preview(&self, cx: &mut Context) -> Option
{ let preview = self.link.shown()?; let composer = cx.weak_entity(); let close: super::link_preview::CloseHandler = Rc::new(move |_, cx| { composer .update(cx, |this, cx| { this.link.dismiss(); cx.notify(); }) .ok(); }); Some(div().w_full().mb(px(6.)).child(link_preview_card( preview, "composer-link-preview".to_owned(), &self.app.read(cx).directory, true, Some(close), ))) } fn popup_is_open(&self) -> bool { self.slash_popup .as_ref() .is_some_and(|popup| !popup.commands.is_empty()) || self.emoji_popup.is_some() || self .popup .as_ref() .is_some_and(|popup| !popup.candidates.is_empty()) } fn move_highlight(&mut self, delta: isize, cx: &mut Context) { if let Some(popup) = self.slash_popup.as_mut() { popup.move_highlight(delta); cx.notify(); } else if let Some(popup) = self.emoji_popup.as_mut() { popup.move_highlight(delta); cx.notify(); } else if let Some(popup) = self.popup.as_mut() { let last = popup.candidates.len().saturating_sub(1) as isize; popup.highlighted = (popup.highlighted as isize + delta).clamp(0, last) as usize; cx.notify(); } } fn accept_popup(&mut self, window: &mut Window, cx: &mut Context) { if self.slash_popup.is_some() { self.accept_slash(window, cx); } else if self.emoji_popup.is_some() { self.accept_emoji(window, cx); } else { self.accept_mention(window, cx); } } fn accept_mention(&mut self, window: &mut Window, cx: &mut Context) { let Some(popup) = self.popup.take() else { return; }; self.lookup = None; let Some(candidate) = popup.candidates.get(popup.highlighted) else { return; }; self.mention_counter += 1; let token_id = format!( "mention-{}-{}", self.mention_counter, candidate_key(candidate) ); let label = format!("@{}", candidate.display_name()); self.mention_inputs .insert(token_id.clone(), candidate.to_mention()); self.input.update(cx, |state, cx| { let token = InlineToken::new(token_id, label); if state .replace_range_with_token(popup.range, token, window, cx) .is_ok() { state.insert(" ", window, cx); } }); self.close_popup(); cx.notify(); } pub fn inline(mut self) -> Self { self.inline = true; self } pub fn set_placeholder(&mut self, text: &str, window: &mut Window, cx: &mut Context) { let text = text.to_owned(); self.input .update(cx, |state, cx| state.set_placeholder(text, window, cx)); } pub fn set_conversation( &mut self, conversation_id: &str, window: &mut Window, cx: &mut Context, ) { self.set_placeholder(COMPOSER_PLACEHOLDER, window, cx); if self.conversation_id.as_deref() != Some(conversation_id) { self.save_draft_now(cx); self.draft_save = None; self.saved_images = None; self.draft_before_edit = None; let carry = self.carry_images_into.take().as_deref() == Some(conversation_id); self.conversation_id = Some(conversation_id.to_owned()); self.close_rewrite(); self.close_attach_menu(window, cx); self.cancel_uploads(); let dropped = if carry { self.tray.keep_images_only() } else { self.tray.clear() }; self.discard_uploaded(dropped, cx); self.remove_orphan_image_tokens(window, cx); self.reply = None; self.link.reset(); if self.editing.take().is_some() { self.load_draft(Draft::default(), InputContent::new(""), window, cx); } self.mention_inputs.clear(); self.close_popup(); self.schedule = None; self.table_picker = None; self.table_menu = None; self.format_popup = None; self.format_overflow = false; self.reset_importance(); if !carry { self.load_stored_draft(conversation_id, window, cx); } cx.notify(); } } /// The draft's images follow the user into this chat; any other switch clears the tray. pub fn carry_images_into(&mut self, conversation_id: String) { self.carry_images_into = Some(conversation_id); } fn discard_uploaded(&self, files: Vec, cx: &App) { discard_uploaded(self.app.read(cx).engine.clone(), files); } pub fn set_reply(&mut self, reply: Option, cx: &mut Context) { self.reply = reply; if self.reply.is_some() { self.editing = None; } self.schedule_draft_save(cx); cx.notify(); } pub fn begin_edit(&mut self, draft: Outgoing, window: &mut Window, cx: &mut Context) { if self.editing.is_none() { self.save_draft_now(cx); let image_ids = self.live_image_ids(cx); self.draft_before_edit = Some((self.compose(cx), image_ids)); } self.restore(&draft, window, cx); self.focus(window, cx); } pub fn cancel_edit(&mut self, window: &mut Window, cx: &mut Context) { self.editing = None; self.restore_draft_before_edit(window, cx); } fn live_image_ids(&self, cx: &App) -> Vec { self.input .read(cx) .tokens() .iter() .filter_map(|span| image_id(span.token())) .filter(|id| self.tray.image(*id).is_some()) .collect() } fn restore_draft_before_edit(&mut self, window: &mut Window, cx: &mut Context) { match self.draft_before_edit.take() { Some((outgoing, image_ids)) => { let image_ids = image_ids .into_iter() .filter(|id| self.tray.image(*id).is_some()) .collect(); self.restore_content(&outgoing, image_ids, window, cx); } None => self.set_text("", window, cx), } } fn outgoing(&self, cx: &App) -> Option { self.can_send(cx).then(|| self.compose(cx)) } fn compose(&self, cx: &App) -> Outgoing { let state = self.input.read(cx); let mut draft = self.current_draft(cx); let image_spans: Vec<(Range, u64)> = state .tokens() .iter() .filter_map(|span| Some((span.range(), image_id(span.token())?))) .collect(); let image_ids: Vec = image_spans .iter() .filter(|(_, id)| self.tray.image(*id).is_some()) .map(|(_, id)| *id) .collect(); for (range, _) in image_spans .iter() .rev() .filter(|(_, id)| self.tray.image(*id).is_none()) { draft.replace(range.clone(), ""); } draft.take_edits(); let mut draft = draft.trimmed(); let attachments_apply = self.editing.is_none(); if self.undone_value.as_deref() != Some(state.value().as_ref()) && let Some((range, glyph)) = emoji::trailing_smiley(draft.text()) && !draft.in_code(range.end - 1) { draft.replace(range, glyph); draft.take_edits(); } let mentions: Vec = state .tokens() .iter() .filter_map(|span| self.mention_inputs.get(span.token().id().as_ref())) .cloned() .collect(); if self.app.read(cx).markdown_mode && mentions.is_empty() && image_ids.is_empty() { draft = Draft::from_markdown(draft.text()); } Outgoing { draft, subject: self.post_subject_text(cx), mentions, reply: self.reply.clone(), edit: self.editing.clone(), link_preview: self.link.shown().filter(|_| attachments_apply).cloned(), images: if attachments_apply { self.tray.images_in(&image_ids) } else { Vec::new() }, files: if attachments_apply { self.tray.outgoing_files() } else { Vec::new() }, importance: self.sendable_importance(cx), banner: self.announcement_banner(cx), } } /// The draft, or the plain input text while a change is still on its way to the draft. fn current_draft(&self, cx: &App) -> Draft { let value = self.input.read(cx).value(); if self.draft.text() == value.as_ref() { self.draft.clone() } else { Draft::plain(&value) } } fn can_send(&self, cx: &App) -> bool { let has_text = !self.current_draft(cx).is_blank(); if self.editing.is_some() { return has_text; } (has_text || self.has_attachments(cx)) && !self.tray.blocks_send() } fn has_attachments(&self, cx: &App) -> bool { self.tray.has_chips() || self .input .read(cx) .tokens() .iter() .any(|span| image_id(span.token()).is_some_and(|id| self.tray.image(id).is_some())) } fn image_token_range(&self, id: u64, cx: &App) -> Option> { self.input .read(cx) .tokens() .iter() .find(|span| image_id(span.token()) == Some(id)) .map(|span| span.range()) } fn remove_orphan_image_tokens(&mut self, window: &mut Window, cx: &mut Context) { let edits: Vec = self .input .read(cx) .tokens() .iter() .filter(|span| image_id(span.token()).is_some_and(|id| self.tray.image(id).is_none())) .map(|span| (span.range(), String::new())) .collect(); if !edits.is_empty() { self.input .update(cx, |state, cx| state.apply_edits(&edits, window, cx)); } } fn remove_image_token(&mut self, id: u64, window: &mut Window, cx: &mut Context) { let Some(range) = self.image_token_range(id, cx) else { return; }; self.input.update(cx, |state, cx| { state.set_selected_range(range, cx); state.replace("", window, cx); }); } fn insert_image_token(&mut self, id: u64, window: &mut Window, cx: &mut Context) { let state = self.input.read(cx); let value = state.value().to_string(); let selection = state.selected_range(); let mut offset = self .pending_anchors .remove(&id) .unwrap_or(selection.end) .min(value.len()); while !value.is_char_boundary(offset) { offset -= 1; } if value == self.draft.text() && self.draft.in_code(offset) { offset = value.len(); } if let Some(span) = state .tokens() .iter() .find(|span| span.range().start < offset && offset < span.range().end) { offset = span.range().end; } let length = OBJECT_MARK.len_utf8(); let after = |position: usize| { if position >= offset { position + length } else { position } }; let inserted = self.input.update(cx, |state, cx| { let inserted = state .replace_range_with_token(offset..offset, image_token(id), window, cx) .is_ok(); if inserted { state.set_selected_range(after(selection.start)..after(selection.end), cx); } inserted }); if !inserted { return; } for (other, anchor) in self.pending_anchors.iter_mut() { if *anchor > offset || (*anchor == offset && *other > id) { *anchor += length; } } self.previous_value = self.input.read(cx).value().to_string(); } pub fn is_editing(&self) -> bool { self.editing.is_some() } fn files_allowed(&self) -> bool { self.conversation_id .as_deref() .is_some_and(|conversation_id| !conversation_id.is_empty()) } fn cancel_uploads(&mut self) { for (_, handle) in self.uploads.drain() { handle.cancel(); } } pub fn open_picker(&mut self, window: &mut Window, cx: &mut Context) { if self.editing.is_some() { return; } let receiver = cx.prompt_for_paths(PathPromptOptions { files: true, directories: false, multiple: true, prompt: None, }); cx.spawn_in(window, async move |this, cx| { if let Ok(Ok(Some(paths))) = receiver.await { this.update_in(cx, |this, window, cx| this.add_paths(paths, window, cx)) .ok(); } }) .detach(); } pub fn add_paths(&mut self, paths: Vec, window: &mut Window, cx: &mut Context) { if self.editing.is_some() || paths.is_empty() { return; } let ids = self.add_pending(&names_of(&paths), cx); for (id, path) in ids.into_iter().zip(paths) { cx.spawn_in(window, async move |this, cx| { let loaded = cx .background_executor() .spawn(async move { read_attachment(&path) }) .await; this.update_in(cx, |this, window, cx| { this.finish_reading(id, loaded, window, cx) }) .ok(); }) .detach(); } cx.notify(); } /// Images that no longer sit in the text give way to new attachments. fn make_room(&mut self, needed: usize, cx: &App) { if self.tray.attachment_count() + needed > MAX_ATTACHMENTS { let referenced: Vec = self .input .read(cx) .tokens() .iter() .filter_map(|span| image_id(span.token())) .collect(); self.tray.discard_images_except(&referenced); } } fn add_pending(&mut self, names: &[String], cx: &App) -> Vec { self.make_room(names.len(), cx); let ids = self.tray.add_pending(names); let anchor = self.selection(cx).end; self.pending_anchors .extend(ids.iter().map(|id| (*id, anchor))); ids } fn add_pasted_image(&mut self, pasted: Image, window: &mut Window, cx: &mut Context) { let ids = self.add_pending(&[pasted_image_name()], cx); cx.notify(); let Some(&id) = ids.first() else { return; }; cx.spawn_in(window, async move |this, cx| { let loaded = cx .background_executor() .spawn(async move { prepare_pasted_image(pasted) }) .await; this.update_in(cx, |this, window, cx| { this.finish_reading(id, loaded, window, cx) }) .ok(); }) .detach(); } fn on_fun_pick( &mut self, _: &Entity, event: &FunPickerEvent, window: &mut Window, cx: &mut Context, ) { match event { FunPickerEvent::Emoji(glyph) => self.insert_emoji(glyph, window, cx), FunPickerEvent::Image(image) => self.insert_remote_image(image.clone(), window, cx), } } fn insert_emoji(&mut self, glyph: &str, window: &mut Window, cx: &mut Context) { let range = self.selection(cx); let cursor = range.end; self.replace_text(range, glyph, cursor, window, cx); self.remember_emoji(glyph, cx); cx.notify(); } fn insert_remote_image( &mut self, image: RemoteImage, window: &mut Window, cx: &mut Context, ) { if !image.is_custom_emoji() { self.make_room(1, cx); } if let Some(id) = self.tray.add_remote(image) { self.insert_image_token(id, window, cx); } self.focus(window, cx); cx.notify(); } fn paste(&mut self, item: &ClipboardItem, window: &mut Window, cx: &mut Context) -> bool { if self.editing.is_none() { match paste_action(item) { PasteAction::Text => {} PasteAction::Files(paths) => { self.add_paths(paths, window, cx); return true; } PasteAction::Image(pasted) => { self.add_pasted_image(pasted, window, cx); return true; } } } let Some(text) = item.text() else { return false; }; if text.contains([OBJECT_MARK, TABLE_MARK]) { let cleaned = text.replace([OBJECT_MARK, TABLE_MARK], ""); self.input .update(cx, |state, cx| state.replace(cleaned, window, cx)); return true; } if self.draft.in_code(self.selection(cx).start) { return false; } if let Some(lines) = self.copied_lines(item, &text) { let cursor = self.draft.insert_lines(self.selection(cx), &lines); self.commit(Some(cursor), window, cx); return true; } self.pasted_markdown = has_markdown(&text).then_some(text); false } fn finish_reading( &mut self, id: u64, loaded: Result, window: &mut Window, cx: &mut Context, ) { match loaded { Ok(loaded) => { let files_allowed = self.files_allowed(); let job = self.tray.finish_reading(id, loaded, files_allowed); if self.tray.image(id).is_some() { self.insert_image_token(id, window, cx); } else { self.pending_anchors.remove(&id); } if let Some(job) = job { self.start_upload(job, cx); } } Err(message) => { self.pending_anchors.remove(&id); self.tray.fail_reading(id, message); } } cx.notify(); } fn start_upload(&mut self, job: UploadJob, cx: &mut Context) { let Some(conversation_id) = self.conversation_id.clone().filter(|id| !id.is_empty()) else { self.tray.finish_upload(job.id, UploadResult::Failed); return; }; let state = self.app.read(cx); let (demo, engine) = (state.mode.demo, state.engine.clone()); if demo { self.simulate_upload(job, cx); return; } let Some(engine) = engine else { self.tray.finish_upload(job.id, UploadResult::Failed); return; }; let id = job.id; let (event_sender, mut event_receiver) = tokio::sync::mpsc::unbounded_channel::(); let finished_sender = event_sender.clone(); let (_, handle) = runtime::spawn_abortable(async move { let result = async { let uploaded = match job.kind { JobKind::Upload => { let progress_sender = event_sender.clone(); let uploaded = engine .upload_attachment_file( &conversation_id, &job.name, &job.bytes, move |percent| { let _ = progress_sender.send(UploadEvent::Progress(percent)); }, ) .await; match uploaded { Ok(uploaded) => { let _ = event_sender.send(UploadEvent::Uploaded(uploaded.clone())); uploaded } Err(error) => { app_base::diagnostics::record(format!( "attachments: upload failed: {error}" )); return UploadResult::Failed; } } } JobKind::Share(uploaded) => uploaded, }; let Some(reference) = uploaded.reference() else { return UploadResult::Failed; }; let end = match engine.share_attachment(&conversation_id, &uploaded).await { Ok(link) => super::attachment_tray::ShareEnd::Linked(link), Err(error) => { app_base::diagnostics::record(format!( "attachments: share failed: {error}" )); if matches!( error, teams_core::Error::Graph(graph::Error::SharingDenied(_)) ) { super::attachment_tray::ShareEnd::Denied } else { super::attachment_tray::ShareEnd::Failed } } }; super::attachment_tray::result_of_share(uploaded, reference, end) } .await; let _ = finished_sender.send(UploadEvent::Finished(result)); }); self.uploads.insert(id, UploadHandle::Network(handle)); cx.spawn(async move |this, cx| { while let Some(event) = event_receiver.recv().await { let updated = this.update(cx, |this, cx| { let orphan = match event { UploadEvent::Progress(percent) => { this.tray.set_progress(id, percent); None } UploadEvent::Uploaded(uploaded) => this.tray.mark_uploaded(id, uploaded), UploadEvent::Finished(result) => this.finish_upload(id, result), }; this.discard_uploaded(orphan.into_iter().collect(), cx); cx.notify(); }); if updated.is_err() { break; } } }) .detach(); } fn simulate_upload(&mut self, job: UploadJob, cx: &mut Context) { let id = job.id; let fails = job.name.to_lowercase().contains("fail") && self.demo_failed.insert(id); let task = cx.spawn(async move |this, cx| { for step in 1..=DEMO_UPLOAD_STEPS { cx.background_executor().timer(DEMO_UPLOAD_STEP).await; if fails && step == DEMO_FAILURE_STEP { this.update(cx, |this, cx| { this.finish_upload_in_demo(id, UploadResult::Failed, cx) }) .ok(); return; } let percent = (u32::from(step) * 100 / u32::from(DEMO_UPLOAD_STEPS)) as u8; this.update(cx, |this, cx| this.set_progress(id, percent, cx)) .ok(); } let reference = FileReference { attachment_id: format!("demo-attachment-{id}"), content_url: format!("https://demo.invalid/files/{}", job.name), name: job.name, }; let done = DoneFile { reference, uploaded: None, }; this.update(cx, |this, cx| { this.finish_upload_in_demo(id, UploadResult::Done(done), cx) }) .ok(); }); self.uploads.insert(id, UploadHandle::Demo(task)); } fn set_progress(&mut self, id: u64, percent: u8, cx: &mut Context) { self.tray.set_progress(id, percent); cx.notify(); } fn finish_upload(&mut self, id: u64, result: UploadResult) -> Option { self.uploads.remove(&id); self.tray.finish_upload(id, result) } fn finish_upload_in_demo(&mut self, id: u64, result: UploadResult, cx: &mut Context) { self.finish_upload(id, result); cx.notify(); } fn remove_attachment(&mut self, id: u64, _: &mut Window, cx: &mut Context) { if let Some(handle) = self.uploads.remove(&id) { handle.cancel(); } let uploaded = self.tray.remove(id); self.discard_uploaded(uploaded.into_iter().collect(), cx); self.schedule_draft_save(cx); cx.notify(); } fn retry_attachment(&mut self, id: u64, _: &mut Window, cx: &mut Context) { if let Some(job) = self.tray.retry(id) { self.start_upload(job, cx); } cx.notify(); } pub fn submit_current(&mut self, window: &mut Window, cx: &mut Context) { if self.scheduling.is_some() && self.scheduling_for == self.conversation_id { return; } let was_editing = self.editing.is_some(); let Some(outgoing) = self.outgoing(cx) else { return; }; self.typing.reset(); self.load_draft(Draft::default(), InputContent::new(""), window, cx); self.mention_inputs.clear(); if self.editing.is_none() { self.tray.clear(); self.clear_stored_draft(cx); } self.reply = None; self.editing = None; self.link.reset(); self.close_popup(); self.reset_importance(); self.close_announcement(window, cx); if was_editing { self.restore_draft_before_edit(window, cx); } cx.emit(ComposerEvent::Submit(Box::new(outgoing))); cx.notify(); } fn with_mention_tokens( &mut self, mut content: InputContent, draft: &Draft, mentions: &[MentionInput], ) -> InputContent { let text = draft.text(); let mut cursor = 0; for mention in mentions { let Some(range) = teams_core::plain_mention_spans(draft, &mention.text, false) .into_iter() .find(|span| span.start >= cursor) else { continue; }; self.mention_counter += 1; let token_id = format!("mention-{}", self.mention_counter); let token = InlineToken::new(token_id.clone(), text[range.clone()].to_owned()); if let Ok(next) = content.clone().with_token(range.clone(), token) { content = next; self.mention_inputs.insert(token_id, mention.clone()); cursor = range.end; } } content } pub fn restore(&mut self, outgoing: &Outgoing, window: &mut Window, cx: &mut Context) { let image_ids = if outgoing.edit.is_none() { self.cancel_uploads(); self.tray.restore(&outgoing.images, &outgoing.files) } else { Vec::new() }; self.restore_content(outgoing, image_ids, window, cx); } fn restore_content( &mut self, outgoing: &Outgoing, image_ids: Vec, window: &mut Window, cx: &mut Context, ) { self.mention_inputs.clear(); let text = outgoing.draft.text(); let mut content = InputContent::new(text.to_owned()); for ((offset, _), id) in text.match_indices(OBJECT_MARK).zip(image_ids) { let range = offset..offset + OBJECT_MARK.len_utf8(); if let Ok(next) = content.clone().with_token(range, image_token(id)) { content = next; } } let content = self.with_mention_tokens(content, &outgoing.draft, &outgoing.mentions); self.load_draft(outgoing.draft.clone(), content, window, cx); self.reply = outgoing.reply.clone(); self.editing = outgoing.edit.clone(); self.link.restore(outgoing.link_preview.clone()); if outgoing.edit.is_none() { self.restore_announcement(outgoing.banner.as_ref(), window, cx); if !self.inline && (outgoing.subject.is_some() || outgoing.banner.is_some()) { cx.emit(ComposerEvent::RestorePost(outgoing.subject.clone())); } } self.close_popup(); self.schedule_draft_save(cx); cx.notify(); } /// Text and mentions replace the draft; images join the tray, where oversized ones upload as files. pub fn load_proposal( &mut self, text: &str, mentions: &[MentionInput], pictures: Vec<(String, Vec)>, window: &mut Window, cx: &mut Context, ) { let draft = Draft::from_markdown(text); self.mention_inputs.clear(); let content = self.with_mention_tokens(InputContent::new(draft.text().to_owned()), &draft, mentions); self.load_draft(draft, content, window, cx); self.update_emoji(cx); self.update_mention(cx); self.update_link_preview(cx); for (name, bytes) in pictures { let ids = self.add_pending(std::slice::from_ref(&name), cx); let Some(&id) = ids.first() else { continue; }; let loaded = LoadedFile { file_name: Some(name), ..LoadedFile::new(bytes) }; self.finish_reading(id, Ok(loaded), window, cx); } cx.notify(); } /// Markdown in `text` arrives formatted. pub fn set_text(&mut self, text: &str, window: &mut Window, cx: &mut Context) { let draft = Draft::from_markdown(text); let content = InputContent::new(draft.text().to_owned()); self.load_draft(draft, content, window, cx); self.mention_inputs.clear(); self.update_emoji(cx); self.update_mention(cx); self.update_link_preview(cx); cx.notify(); } pub fn unsent(&self, cx: &App) -> Option { (!self.draft.is_blank() && self.editing.is_none()).then(|| self.compose(cx)) } pub fn is_empty(&self, cx: &App) -> bool { self.draft.is_blank() && !self.has_attachments(cx) } pub fn focus(&self, window: &mut Window, cx: &mut Context) { self.input.update(cx, |state, cx| state.focus(window, cx)); } fn render_popup(&self, window: &mut Window, cx: &mut Context) -> Option { let popup = self .popup .as_ref() .filter(|popup| !popup.candidates.is_empty())?; let at_sign = popup.range.start..popup.range.start + 1; let anchor = self.input.read(cx).range_to_bounds(&at_sign); if anchor.is_none() { window.request_animation_frame(); } let popup_height = popup.candidates.len() as f32 * POPUP_ROW_HEIGHT + 2. * POPUP_PADDING; let directory = &self.app.read(cx).directory; let rows = popup .candidates .iter() .enumerate() .map(|(index, candidate)| { let name = candidate.display_name().to_owned(); let avatar = candidate_avatar(candidate, directory); let subtitle = candidate_subtitle(candidate); h_flex() .id(ElementId::Name(format!("mention-row-{index}").into())) .mx(px(6.)) .px(px(6.)) .h(px(POPUP_ROW_HEIGHT)) .flex_none() .gap(px(8.)) .items_center() .rounded(px(6.)) .cursor_pointer() .when(index == popup.highlighted, |row| { row.bg(theme::popup_hover()) }) .hover(|row| row.bg(theme::popup_hover())) .child(avatar) .child( v_flex() .flex_1() .min_w_0() .child( div() .truncate() .text_size(px(13.5)) .text_color(theme::text()) .child(name), ) .when(!subtitle.is_empty(), |column| { column.child( div() .truncate() .text_size(px(12.)) .text_color(theme::text_muted()) .child(subtitle), ) }), ) .on_click(cx.listener(move |this, _, window, cx| { if let Some(popup) = this.popup.as_mut() { popup.highlighted = index; } this.accept_mention(window, cx); })) }); let panel = v_flex() .id("mention-popup") .debug_selector(|| MENTION_POPUP_TAG.into()) .w(px(POPUP_WIDTH)) .py(px(POPUP_PADDING)) .rounded(px(10.)) .flyout_surface() .occlude() .children(rows); let Some(anchor) = anchor else { return Some( div() .absolute() .bottom(relative(1.)) .left_0() .mb(px(POPUP_GAP)) .child(panel) .into_any_element(), ); }; let room_above = f32::from(anchor.top()) - POPUP_GAP - TITLE_BAR_HEIGHT; let (position, corner) = if room_above >= popup_height { ( point(anchor.left(), anchor.top() - px(POPUP_GAP)), Anchor::BottomLeft, ) } else { ( point(anchor.left(), anchor.bottom() + px(POPUP_GAP)), Anchor::TopLeft, ) }; Some( deferred( anchored() .position(position) .anchor(corner) .snap_to_window_with_margin(px(POPUP_GAP)) .child(panel), ) .with_priority(1) .into_any_element(), ) } fn render_emoji_popup( &self, window: &mut Window, cx: &mut Context, ) -> Option { let popup = self.emoji_popup.as_ref()?; let colon = popup.range.start..popup.range.start + 1; let anchor = self .input .read(cx) .range_to_bounds(&colon) .map(|bounds| bounds.origin); if anchor.is_none() { window.request_animation_frame(); } let composer = cx.entity().downgrade(); Some(emoji_popup::render( popup, anchor, move |index, window, cx| { composer .update(cx, |this, cx| { if let Some(popup) = this.emoji_popup.as_mut() { popup.highlighted = index; } this.accept_emoji(window, cx); }) .ok(); }, )) } fn render_toolbar(&self, focused: bool, cx: &mut Context) -> Option { let composer = cx.entity().downgrade(); let floating = focused && self.toolbar_requested(cx) && !self.format_bar_open() && !self.app.read(cx).markdown_mode; if self.link_editor.is_none() && !floating { return None; } let state = self.input.read(cx); let range = self .link_editor .as_ref() .map_or_else(|| state.selected_range(), |editor| editor.range.clone()); let start = state.range_to_bounds(&(range.start..range.start))?; let end = state.range_to_bounds(&(range.end..range.end))?; let input_bounds = state.input_bounds(); let center_x = if start.origin.y == end.origin.y { (start.origin.x + end.origin.x) / 2. } else { input_bounds.center().x }; let edges = self .composer_bounds .get() .map_or((input_bounds.left(), input_bounds.right()), |bounds| { (bounds.left(), bounds.right()) }); let width = match self.link_editor { Some(_) => format_toolbar::link_width(), None => format_toolbar::bar_width(), }; let placement = format_toolbar::place(center_x, start.origin.y, edges, width); let element = match &self.link_editor { Some(editor) => format_toolbar::render( placement, format_toolbar::Mode::Link { field: &editor.field, refused: editor.refused, on_apply: Rc::new(move |window, cx| { composer .update(cx, |this, cx| this.apply_link(window, cx)) .ok(); }), }, ), None => { let state_of = |button| ButtonState::enabled(self.format_state(button, &range)); let mut overlay = |button| match button { FormatButton::FontSize => self.render_format_popup(FormatPopup::FontSize, cx), _ => None, }; format_toolbar::render( placement, format_toolbar::Mode::Bar { state: &state_of, on_press: Rc::new(move |button, window, cx| { composer .update(cx, |this, cx| this.press_format(button, window, cx)) .ok(); }), overlay: &mut overlay, }, ) } }; Some(element) } fn format_press_handler(&self, cx: &mut Context) -> format_toolbar::PressHandler { let composer = cx.weak_entity(); Rc::new(move |button, window, cx| { composer .update(cx, |this, cx| this.press_format(button, window, cx)) .ok(); }) } fn format_bar_fit(&self) -> format_toolbar::BarFit { let toggle_width = if self.post_layout() { COMPOSER_BUTTON_SIZE } else { 0. }; let available = self.composer_bounds.get().map_or(f32::INFINITY, |bounds| { f32::from(bounds.size.width) - 2. * (COMPOSER_BUTTON_INSET + 1.) - toggle_width }); format_toolbar::fit_groups(available, &format_toolbar::BAR_GROUPS) } fn render_format_overlay( &self, button: FormatButton, overflow: &[Vec], window: &Window, cx: &mut Context, ) -> Option { match button { FormatButton::Table => self.render_table_picker(cx), FormatButton::TableInsertLine => self.render_table_menu(TableLineMenu::Insert, cx), FormatButton::TableRemoveLine => self.render_table_menu(TableLineMenu::Remove, cx), FormatButton::Highlight => self.render_format_popup(FormatPopup::Highlight, cx), FormatButton::FontColor => self.render_format_popup(FormatPopup::FontColor, cx), FormatButton::FontSize => self.render_format_popup(FormatPopup::FontSize, cx), FormatButton::ParagraphStyle => { self.render_format_popup(FormatPopup::ParagraphStyle, cx) } FormatButton::More if self.format_overflow => { let selection = self.selection(cx); let in_table = self.in_table(window, cx); let state_of = |button| self.button_state(button, &selection, in_table); let on_press = self.format_press_handler(cx); let mut overlay = |button| self.render_format_overlay(button, &[], window, cx); let menu = format_toolbar::render_overflow_menu( overflow, &state_of, on_press, &mut overlay, ); Some(self.render_overflow_surface(menu, cx)) } _ => None, } } fn render_format_bar(&self, window: &Window, cx: &mut Context) -> AnyElement { let selection = self.selection(cx); let in_table = self.in_table(window, cx); let state_of = |button| self.button_state(button, &selection, in_table); let on_press = self.format_press_handler(cx); let fit = self.format_bar_fit(); let mut overlay = |button| self.render_format_overlay(button, &fit.overflow, window, cx); let row = format_toolbar::render_format_bar(&fit, &state_of, on_press, &mut overlay); v_flex() .debug_selector(|| COMPOSER_FORMAT_BAR_TAG.into()) .w_full() .child( h_flex() .w_full() .p(px(COMPOSER_BUTTON_INSET)) .child(row.flex_1().min_w_0()) .children(self.render_announcement_toggle(cx)), ) .child( div() .debug_selector(|| COMPOSER_FORMAT_DIVIDER_TAG.into()) .w_full() .h(px(1.)) .bg(theme::border()), ) .into_any_element() } fn render_paste_hint(&self) -> Option
{ self.paste_hint.as_ref()?; Some( h_flex() .mt(px(6.)) .gap(px(6.)) .items_center() .text_size(px(12.)) .text_color(theme::text_muted()) .child("Formatted from Markdown") .child( div() .px(px(4.)) .py(px(2.)) .rounded(px(4.)) .bg(theme::surface_raised()) .text_size(px(11.)) .text_color(theme::text()) .child("Ctrl+Z"), ) .child("plain text"), ) } fn render_edit_strip(&self, cx: &mut Context) -> Option
{ let edit = self.editing.as_ref()?; Some( h_flex() .w_full() .h(px(52.)) .mb(px(8.)) .gap(px(12.)) .items_center() .px(px(12.)) .rounded(px(10.)) .bg(theme::row_hover()) .child(match edit.scheduled { Some(_) => { symbol("schedule_send", 14., theme::accent_text()).into_any_element() } None => icon(IconName::Pencil, 14., theme::accent_text()).into_any_element(), }) .child( v_flex() .flex_1() .min_w_0() .child( div() .truncate() .text_size(px(12.)) .font_weight(FontWeight::SEMIBOLD) .text_color(theme::accent_text()) .child(match edit.scheduled { Some(send_at) => format!( "Editing scheduled message - {}", scheduled_label(send_at, Utc::now()) ), None => "Editing message".to_owned(), }), ) .child( div() .truncate() .text_size(px(12.)) .text_color(theme::text_muted()) .child(edit.excerpt.clone()), ), ) .child( div() .id("edit-close") .size(px(28.)) .flex_none() .flex() .items_center() .justify_center() .rounded(px(7.)) .cursor_pointer() .hover(|button| button.bg(theme::row_hover())) .child(icon(IconName::Close, 14., theme::text_muted())) .on_click(cx.listener(|this, _, window, cx| this.cancel_edit(window, cx))), ), ) } fn render_reply_strip(&self, cx: &mut Context) -> Option
{ let reply = self.reply.as_ref()?; Some( h_flex() .w_full() .h(px(52.)) .mb(px(8.)) .gap(px(12.)) .items_center() .px(px(12.)) .rounded(px(10.)) .bg(theme::row_hover()) .child(icon(IconName::Undo2, 14., theme::accent_text())) .child( v_flex() .flex_1() .min_w_0() .child( div() .truncate() .text_size(px(12.)) .font_weight(FontWeight::SEMIBOLD) .text_color(theme::accent_text()) .child(format!("Replying to {}", reply.author)), ) .child( div() .truncate() .text_size(px(12.)) .text_color(theme::text_muted()) .child(reply.excerpt.clone()), ), ) .child( div() .id("reply-close") .size(px(28.)) .flex_none() .flex() .items_center() .justify_center() .rounded(px(7.)) .cursor_pointer() .hover(|button| button.bg(theme::row_hover())) .child(icon(IconName::Close, 14., theme::text_muted())) .on_click(cx.listener(|this, _, _, cx| this.set_reply(None, cx))), ), ) } } impl Render for Composer { fn render(&mut self, window: &mut Window, cx: &mut Context) -> impl IntoElement { let focused = self.input.focus_handle(cx).contains_focused(window, cx); let can_send = self.can_send(cx); let can_attach = self.editing.is_none(); let send = div() .id("composer-send") .debug_selector(|| COMPOSER_SEND_TAG.into()) .size(px(COMPOSER_BUTTON_SIZE)) .ml(px(COMPOSER_BUTTON_INSET)) .flex_none() .flex() .items_center() .justify_center() .rounded(px(8.)) .child(symbol("send", COMPOSER_ICON_SIZE, theme::text())) .on_mouse_down( MouseButton::Right, cx.listener(|this, _, _, cx| this.open_schedule_menu(cx)), ) .when(!can_send, |button| button.opacity(0.4)) .when(can_send, |button| { button .cursor_pointer() .hover(|button| button.bg(theme::popup_hover())) .on_click(cx.listener(|this, _, window, cx| this.submit_current(window, cx))) }); let fun = can_attach.then(|| { let picker = self.fun_picker.clone(); let reset_picker = self.fun_picker.clone(); Popover::new("composer-fun-popover") .anchor(Anchor::BottomRight) .offset(px(10.)) .p_0() .trigger( Button::new("composer-fun") .custom( ButtonCustomVariant::new(cx) .hover(theme::popup_hover()) .active(theme::popup_hover()), ) .rounded(px(8.)) .with_size(px(COMPOSER_ICON_SIZE / BUTTON_ICON_SCALE)) .size(px(COMPOSER_BUTTON_SIZE)) .tooltip("Emoji, GIFs und Sticker") .icon(symbol("mood", COMPOSER_ICON_SIZE, theme::text_muted())), ) .on_open_change(move |open, window, cx| { if *open { reset_picker.update(cx, |picker, cx| picker.reset(window, cx)); } }) .content(move |_, _, cx| { let popover = cx.entity().downgrade(); picker.update(cx, |picker, _| picker.set_popover(popover)); picker.clone() }) }); let attach = can_attach.then(|| self.render_attach_button(cx)); let markdown_mode = self.app.read(cx).markdown_mode; let post_layout = self.post_layout(); let format_open = (self.format_bar_open() || post_layout) && !markdown_mode; let format_toggle = (!markdown_mode && !post_layout).then(|| { div() .id("composer-format") .debug_selector(|| COMPOSER_FORMAT_TAG.into()) .size(px(COMPOSER_BUTTON_SIZE)) .flex_none() .flex() .items_center() .justify_center() .rounded(px(8.)) .cursor_pointer() .when(format_open, |button| button.bg(theme::popup_hover())) .hover(|button| button.bg(theme::popup_hover())) .tooltip(move |window, cx| { Tooltip::new(format_toolbar::toggle_tooltip(format_open)).build(window, cx) }) .child(symbol( format_toolbar::toggle_symbol(format_open), COMPOSER_ICON_SIZE, if format_open { theme::accent_soft() } else { theme::text_muted() }, )) .on_mouse_down(MouseButton::Left, |_, window, cx| { window.prevent_default(); cx.stop_propagation(); }) .on_click(cx.listener(|this, _, window, cx| this.toggle_format_bar(window, cx))) }); let format_bar = format_open.then(|| self.render_format_bar(window, cx)); let actions = h_flex() .debug_selector(|| COMPOSER_ACTIONS_TAG.into()) .flex_none() .gap(px(COMPOSER_BUTTON_INSET)) .children(format_toggle) .children(fun) .children(attach) .children(self.render_rewrite_button(cx)) .children(self.render_plus_button(cx)); // Teams gives a code block the full width and keeps the actions right, beside Send. let below_code = self .draft .lines() .iter() .any(|kind| matches!(kind, LineKind::Code(_))); let stacked = !post_layout && (below_code || self .composer_bounds .get() .is_some_and(|bounds| f32::from(bounds.size.width) < STACKED_ACTIONS_WIDTH)); let mut send = Some(send); let (row_actions, stacked_actions, post_footer) = if post_layout { ( None, None, Some(self.render_post_footer(actions, can_send, cx)), ) } else if stacked { (None, Some((actions, send.take())), None) } else { (Some(actions.ml(px(COMPOSER_BUTTON_INSET))), None, None) }; let post_header = self.render_post_header(cx); let composer = cx.weak_entity(); let tokens_composer = composer.clone(); let input = Textarea::new(&self.input) .line_box_height(px(COMPOSER_BUTTON_SIZE)) .appearance(false) .bordered(false) .on_paste(move |item, window, cx| { composer .update(cx, |this, cx| this.paste(item, window, cx)) .unwrap_or(false) }) .token(move |context, _, cx| { if context.is_block() && is_code_header_token(context.token()) { let weak = tokens_composer.clone(); let (at, width) = (context.range().start, context.available_width()); if let Ok(header) = tokens_composer .read_with(cx, |this, _| this.render_code_header(weak, at, width)) { return header; } } if context.is_block() && is_rule_token(context.token()) { return render_rule_token( context.available_width(), context.line_height(), context.is_selected(), ); } if context.is_block() && is_table_token(context.token()) { let token_id = context.token().id().to_string(); let width = context.available_width(); let selected = context.is_selected(); if let Some(table) = tokens_composer .read_with(cx, |this, _| { this.render_table_token(&token_id, width, selected) }) .ok() .flatten() { return table; } } if let Some(id) = context .is_block() .then(|| image_id(context.token())) .flatten() { let preview = tokens_composer .read_with(cx, |this, _| this.tray.image(id)) .ok() .flatten(); let remove_composer = tokens_composer.clone(); return inline_preview( id, preview, context.available_width(), context.is_selected(), Rc::new(move |window, cx| { remove_composer .update(cx, |this, cx| this.remove_image_token(id, window, cx)) .ok(); }), ); } div() .h(context.line_height()) .px(px(5.)) .rounded(px(5.)) .bg(if context.is_selected() { theme::accent().opacity(0.4) } else { theme::mention_background(false) }) .text_color(theme::mention_text()) .font_weight(FontWeight::SEMIBOLD) .child(context.token().label().clone()) .into_any_element() }); let tray = can_attach .then(|| { render_tray( &self.tray, Self::remove_attachment, Self::retry_attachment, cx, ) }) .flatten(); let notice = self.tray.notice().filter(|_| can_attach).map(|notice| { div() .w_full() .mb(px(6.)) .text_size(px(12.)) .text_color(theme::amber()) .child(notice.to_owned()) }); let composer_bounds = self.composer_bounds.clone(); let single_line = { let state = self.input.read(cx); state .line_height() .is_none_or(|line_height| state.input_bounds().size.height < line_height * 2.) }; let (inset, top, bottom) = if self.inline { (0., 0., 0.) } else { (20., 0., 16.) }; v_flex() .w_full() .flex_none() .px(px(inset)) .pt(px(top)) .pb(px(bottom)) .key_context(COMPOSER_KEY_CONTEXT) .on_action(cx.listener(|this, _: &ToggleBold, window, cx| { if this.link_editor.is_none() && !this.cell_has_focus(window, cx) { this.toggle_mark(MarkKind::Bold, cx); } })) .on_action(cx.listener(|this, _: &ToggleItalic, window, cx| { if this.link_editor.is_none() && !this.cell_has_focus(window, cx) { this.toggle_mark(MarkKind::Italic, cx); } })) .on_action(cx.listener(|this, _: &ToggleUnderline, window, cx| { if this.link_editor.is_none() && !this.cell_has_focus(window, cx) { this.toggle_mark(MarkKind::Underline, cx); } })) .on_action(cx.listener(|this, _: &ToggleStrike, window, cx| { if this.link_editor.is_none() && !this.cell_has_focus(window, cx) { this.toggle_mark(MarkKind::Strike, cx); } })) .on_action(cx.listener(|this, _: &ToggleSuperscript, window, cx| { if this.link_editor.is_none() && !this.cell_has_focus(window, cx) { this.toggle_mark(MarkKind::Superscript, cx); } })) .on_action(cx.listener(|this, _: &ToggleSubscript, window, cx| { if this.link_editor.is_none() && !this.cell_has_focus(window, cx) { this.toggle_mark(MarkKind::Subscript, cx); } })) .on_action(cx.listener(|this, _: &ToggleCode, window, cx| { if this.link_editor.is_none() && !this.cell_has_focus(window, cx) { this.toggle_mark(MarkKind::Code, cx); } })) .on_action(cx.listener(|this, _: &EditLink, window, cx| { if !this.cell_has_focus(window, cx) { this.edit_link(window, cx); } })) .on_action( cx.listener(|this, _: &PastePlainText, window, cx| this.paste_plain(window, cx)), ) .on_action(cx.listener(|this, _: &InsertTableRowAbove, window, cx| { this.edit_table(TableEdit::InsertRowAbove, window, cx) })) .on_action(cx.listener(|this, _: &InsertTableRowBelow, window, cx| { this.edit_table(TableEdit::InsertRowBelow, window, cx) })) .on_action(cx.listener(|this, _: &InsertTableColumnLeft, window, cx| { this.edit_table(TableEdit::InsertColumnLeft, window, cx) })) .on_action(cx.listener(|this, _: &InsertTableColumnRight, window, cx| { this.edit_table(TableEdit::InsertColumnRight, window, cx) })) .on_action(cx.listener(|this, _: &DeleteTableRow, window, cx| { this.edit_table(TableEdit::DeleteRow, window, cx) })) .on_action(cx.listener(|this, _: &DeleteTableColumn, window, cx| { this.edit_table(TableEdit::DeleteColumn, window, cx) })) .on_action(cx.listener(|this, _: &DeleteTable, window, cx| { this.edit_table(TableEdit::DeleteTable, window, cx) })) .on_action(cx.listener(|this, _: &ScheduleSend, _, cx| this.open_schedule_picker(cx))) .on_action(cx.listener(|this, _: &ToggleFormatBar, window, cx| { this.toggle_format_bar(window, cx) })) .on_action( cx.listener(|_, _: &EditLastMessage, _, cx| cx.emit(ComposerEvent::EditLast)), ) .on_action(cx.listener(|this, _: &InsertTableLine, window, cx| { this.open_table_menu(TableLineMenu::Insert, window, cx); })) .on_action(cx.listener(|this, _: &RemoveTableLine, window, cx| { this.open_table_menu(TableLineMenu::Remove, window, cx); })) .on_action(cx.listener(|this, _: &SetParagraph, window, cx| { this.set_paragraph_style(None, window, cx) })) .on_action(cx.listener(|this, _: &SetHeading1, window, cx| { this.set_paragraph_style(Some(1), window, cx) })) .on_action(cx.listener(|this, _: &SetHeading2, window, cx| { this.set_paragraph_style(Some(2), window, cx) })) .on_action(cx.listener(|this, _: &SetHeading3, window, cx| { this.set_paragraph_style(Some(3), window, cx) })) .on_action(cx.listener(|this, _: &ToggleCodeBlock, window, cx| { this.toggle_code_block(window, cx) })) .on_action(cx.listener(|this, _: &ToggleQuote, window, cx| { if !this.cell_has_focus(window, cx) { this.toggle_lines(LineKind::Quote, window, cx) } })) .capture_action(cx.listener(|this, _: &Undo, window, cx| { if this.side_input_focused(window, cx) { return; } if this.link_editor.is_none() { this.step_history(true, window, cx); cx.stop_propagation(); } })) .capture_action(cx.listener(|this, _: &Redo, window, cx| { if this.side_input_focused(window, cx) { return; } if this.link_editor.is_none() { this.step_history(false, window, cx); cx.stop_propagation(); } })) .capture_action(cx.listener(|this, _: &Copy, window, cx| { if this.side_input_focused(window, cx) { return; } if this.link_editor.is_none() && !this.cell_has_focus(window, cx) && this.copy_selection(cx) { cx.stop_propagation(); } })) .capture_action(cx.listener(|this, _: &Cut, window, cx| { if this.side_input_focused(window, cx) { return; } if this.link_editor.is_none() && !this.cell_has_focus(window, cx) && this.copy_selection(cx) { this.input .update(cx, |state, cx| state.replace("", window, cx)); cx.stop_propagation(); } })) .capture_action(cx.listener(|this, _: &MoveUp, window, cx| { if this.side_input_focused(window, cx) { return; } if let Some(menu) = this.table_menu.as_mut() { menu.step(-1); cx.notify(); cx.stop_propagation(); } else if this.cell_has_focus(window, cx) { if this.table_key(TableKey::Up, window, cx) { cx.stop_propagation(); } } else if this.link_editor.is_some() { } else if this.schedule.is_some() { this.move_schedule_highlight(-1, window, cx); cx.stop_propagation(); } else if this.popup_is_open() { this.move_highlight(-1, cx); cx.stop_propagation(); } else if this.editing.is_none() && this.is_empty(cx) && let Some(event) = up_arrow_event(this.app.read(cx).account_chat_settings().up_arrow) { cx.emit(event); cx.stop_propagation(); } })) .capture_action(cx.listener(|this, _: &MoveDown, window, cx| { if this.side_input_focused(window, cx) { return; } if let Some(menu) = this.table_menu.as_mut() { menu.step(1); cx.notify(); cx.stop_propagation(); } else if this.cell_has_focus(window, cx) { if this.table_key(TableKey::Down, window, cx) { cx.stop_propagation(); } } else if this.link_editor.is_some() { } else if this.schedule.is_some() { this.move_schedule_highlight(1, window, cx); cx.stop_propagation(); } else if this.popup_is_open() { this.move_highlight(1, cx); cx.stop_propagation(); } else if this.leave_quote(window, cx) { cx.stop_propagation(); } })) .capture_action(cx.listener(|this, _: &MoveRight, window, cx| { if this.side_input_focused(window, cx) { return; } if this.link_editor.is_none() && !this.cell_has_focus(window, cx) && this.leave_quote(window, cx) { cx.stop_propagation(); } })) .capture_action(cx.listener(|this, action: &Enter, window, cx| { if this.side_input_focused(window, cx) { return; } if this.table_menu.is_some() { this.accept_table_menu(None, window, cx); cx.stop_propagation(); } else if this.cell_has_focus(window, cx) { } else if this.link_editor.is_some() { this.apply_link(window, cx); cx.stop_propagation(); } else if this.schedule.is_some() { this.activate_schedule_row(window, cx); cx.stop_propagation(); } else if this.popup_is_open() { this.accept_popup(window, cx); cx.stop_propagation(); } else if this.break_line(action.shift, window, cx) { cx.stop_propagation(); } else if enter_starts_line( enter_behavior( this.app.read(cx).account_chat_settings().enter_key, this.format_bar_open() && !this.app.read(cx).markdown_mode, ), action.shift, action.secondary, ) { cx.stop_propagation(); window.dispatch_action( Box::new(Enter { secondary: false, shift: true, }), cx, ); } })) .capture_action(cx.listener(|this, _: &IndentInline, window, cx| { if this.side_input_focused(window, cx) { return; } if this.table_key(TableKey::Next, window, cx) { cx.stop_propagation(); } else if this.link_editor.is_some() { } else if this.popup_is_open() { this.accept_popup(window, cx); cx.stop_propagation(); } else if this.indent(false, window, cx) { cx.stop_propagation(); } })) .capture_action(cx.listener(|this, _: &OutdentInline, window, cx| { if this.side_input_focused(window, cx) { return; } if this.table_key(TableKey::Previous, window, cx) || (this.link_editor.is_none() && this.indent(true, window, cx)) { cx.stop_propagation(); } })) .capture_action(cx.listener(|this, _: &Backspace, window, cx| { if this.side_input_focused(window, cx) { return; } if this.link_editor.is_none() && !this.cell_has_focus(window, cx) && (this.undo_conversion(window, cx) || this.backspace_at_start(window, cx) || this.enter_table_beside(true, window, cx)) { cx.stop_propagation(); } })) .capture_action(cx.listener(|this, _: &Delete, window, cx| { if this.side_input_focused(window, cx) { return; } if this.link_editor.is_none() && !this.cell_has_focus(window, cx) && (this.enter_table_beside(false, window, cx) || this.delete_forward(window, cx)) { cx.stop_propagation(); } })) .capture_action(cx.listener(|this, _: &Escape, window, cx| { if this.table_picker.take().is_some() || this.table_menu.take().is_some() || this.code_language_menu.take().is_some() { cx.notify(); cx.stop_propagation(); return; } if this.schedule.is_some() { this.close_schedule(window, cx); cx.stop_propagation(); return; } if this.link_editor.is_some() { this.close_link_editor(window, cx); cx.stop_propagation(); return; } if this.close_attach_menu(window, cx) { cx.stop_propagation(); return; } if this.close_rewrite() { cx.notify(); cx.stop_propagation(); return; } if this.toolbar_requested(cx) && !this.format_bar_open() { this.toolbar_dismissed = Some(this.selection(cx)); cx.notify(); cx.stop_propagation(); return; } if let Some(popup) = &this.emoji_popup { this.emoji_dismissed_at = Some(popup.range.start); } if this.slash_popup.is_some() { this.slash_dismissed = true; } if this.popup.is_some() || this.emoji_popup.is_some() || this.slash_popup.is_some() { this.close_popup(); cx.notify(); cx.stop_propagation(); } else if this.editing.is_some() { this.cancel_edit(window, cx); cx.stop_propagation(); } })) .children(notice) .children(self.render_reply_strip(cx)) .children(self.render_edit_strip(cx)) .children(self.render_urgent_banner(cx)) .children(self.render_audience_hint()) .children(self.render_meeting_chip()) .children(self.render_link_preview(cx)) .child( div() .relative() .w_full() .children(self.render_popup(window, cx)) .children(self.render_slash_popup(cx)) .children(self.render_emoji_popup(window, cx)) .children(self.render_toolbar(focused, cx)) .children(self.render_code_language_menu(cx)) .children(self.render_schedule(cx)) .children(self.render_delivery_menu(cx)) .children(self.render_plus_menu(cx)) .children(self.render_attach_menu(cx)) .children(self.cloud_picker.as_ref().map(|(picker, _)| picker.clone())) .children(self.render_rewrite_panel(cx)) .child( v_flex() .debug_selector(|| COMPOSER_BOX_TAG.into()) .relative() .w_full() .rounded(px(12.)) .bg(theme::surface_raised()) .border_1() .border_color(if focused { theme::accent() } else { transparent_black() }) .child( canvas( move |bounds, window, _| { let previous = composer_bounds.replace(Some(bounds)); if previous.map(|previous| previous.size.width) != Some(bounds.size.width) { window.refresh(); } }, |_, _, _, _| {}, ) .absolute() .size_full(), ) .children(post_header) .children(format_bar) .children(self.render_importance_label(cx)) .children(tray) .child( h_flex() .w_full() .map(|row| { if single_line { row.items_center() } else { row.items_end() } }) .py(px(COMPOSER_BUTTON_INSET)) .px(px(COMPOSER_BUTTON_INSET)) .child( div() .debug_selector(|| COMPOSER_INPUT_TAG.into()) .flex_1() .min_w_0() .capture_any_mouse_down(cx.listener( |this, _: &MouseDownEvent, _, _| { this.mouse_selecting = true; this.toolbar_dismissed = None; this.link_editor = None; }, )) .capture_any_mouse_up(cx.listener( |this, _: &MouseUpEvent, _, cx| { this.mouse_selecting = false; cx.notify(); }, )) .on_mouse_up_out( MouseButton::Left, cx.listener(|this, _, _, cx| { this.mouse_selecting = false; cx.notify(); }), ) .child(input), ) .children(row_actions) .when_some(send.filter(|_| !post_layout), |row, send| { row.child(send_divider()).child(send) }), ) .children(stacked_actions.map(|(actions, send)| { h_flex() .debug_selector(|| COMPOSER_STACKED_ACTIONS_TAG.into()) .w_full() .items_center() .px(px(COMPOSER_BUTTON_INSET)) .pb(px(COMPOSER_BUTTON_INSET)) .when(below_code, |row| row.child(div().flex_1())) .child(actions) .when(!below_code, |row| row.child(div().flex_1())) .when(below_code && send.is_some(), |row| { row.child(send_divider()) }) .children(send) })) .children(post_footer), ), ) .children(self.render_paste_hint()) } } #[cfg(test)] mod tests { use super::{ ComposerEvent, EnterKeyBehavior, UpArrowBehavior, active_mention, enter_behavior, enter_starts_line, submitted_text, up_arrow_event, }; #[test] fn new_line_mode_keeps_plain_enter_and_sends_on_ctrl_enter() { assert!(enter_starts_line(EnterKeyBehavior::NewLine, false, false)); assert!(!enter_starts_line(EnterKeyBehavior::NewLine, false, true)); assert!(!enter_starts_line(EnterKeyBehavior::NewLine, true, false)); assert!(!enter_starts_line(EnterKeyBehavior::Send, false, false)); } #[test] fn expanded_compose_box_starts_a_line_on_enter_like_teams() { assert_eq!( enter_behavior(EnterKeyBehavior::Send, true), EnterKeyBehavior::NewLine ); assert_eq!( enter_behavior(EnterKeyBehavior::Send, false), EnterKeyBehavior::Send ); assert_eq!( enter_behavior(EnterKeyBehavior::NewLine, false), EnterKeyBehavior::NewLine ); } #[test] fn up_arrow_follows_the_account_setting() { assert!(matches!( up_arrow_event(UpArrowBehavior::EditLastMessage), Some(ComposerEvent::EditLast) )); assert!(matches!( up_arrow_event(UpArrowBehavior::NavigateToMessages), Some(ComposerEvent::FocusMessages) )); assert!(up_arrow_event(UpArrowBehavior::StayInCompose).is_none()); } use gpui_kit::component::input::InputEvent; fn press(shift: bool) -> InputEvent { InputEvent::PressEnter { secondary: false, shift, } } #[test] fn enter_sends_trimmed_text() { assert_eq!( submitted_text(&press(false), " hello \n"), Some("hello".into()) ); } #[test] fn shift_enter_never_sends() { assert_eq!(submitted_text(&press(true), "hello"), None); } #[test] fn blank_text_never_sends() { assert_eq!(submitted_text(&press(false), " \n "), None); } #[test] fn other_events_never_send() { assert_eq!(submitted_text(&InputEvent::Change, "hello"), None); } #[test] fn at_sign_opens_a_mention_query() { assert_eq!(active_mention("hi @Ma", 6, &[]), Some((3..6, "Ma".into()))); assert_eq!(active_mention("@", 1, &[]), Some((0..1, String::new()))); } #[test] fn names_with_a_space_keep_the_query_open() { assert_eq!( active_mention("@Mara Lin", 9, &[]), Some((0..9, "Mara Lin".into())) ); } #[test] fn mail_addresses_and_closed_queries_do_not_open() { assert_eq!(active_mention("a@b", 3, &[]), None); assert_eq!(active_mention("@Mara ", 6, &[]), None); assert_eq!(active_mention("@ x", 3, &[]), None); assert_eq!(active_mention("@Ma\nrest", 8, &[]), None); } #[test] fn text_inside_a_token_does_not_reopen() { let token = 0..15; assert_eq!( active_mention("@Mara Lindqvist", 15, std::slice::from_ref(&token)), None ); assert_eq!( active_mention("@Mara Lindqvist @Pr", 19, std::slice::from_ref(&token)), Some((16..19, "Pr".into())) ); } #[test] fn extras_carry_image_bytes_with_their_mime_type_and_the_file_references() { use std::sync::Arc; use gpui_kit::{Image, ImageFormat}; use teams_core::FileKind; use super::super::attachment_tray::InlineImage; use crate::outgoing::{Outgoing, OutgoingFile, OutgoingImage}; let outgoing = Outgoing { banner: None, draft: teams_core::Draft::default(), subject: None, mentions: Vec::new(), link_preview: None, reply: None, edit: None, images: vec![OutgoingImage::Inline(InlineImage { name: "pasted-image.png".into(), image: Arc::new(Image::from_bytes(ImageFormat::Png, vec![1, 2, 3])), dimensions: Some((1, 1)), })], files: vec![OutgoingFile { name: "plan.pdf".into(), size: 9, kind: FileKind::Pdf, uploaded: None, reference: teams_core::FileReference { attachment_id: "guid".into(), content_url: "https://files.example/plan.pdf".into(), name: "plan.pdf".into(), }, }], importance: Default::default(), }; let extras = outgoing.extras(); assert_eq!(extras.images[0].content_type, "image/png"); assert_eq!(extras.images[0].bytes.as_slice(), [1, 2, 3]); assert_eq!(extras.files[0].attachment_id, "guid"); assert!(outgoing.has_attachments()); } fn placed_image(width: u32) -> crate::outgoing::OutgoingImage { use std::sync::Arc; use gpui_kit::{Image, ImageFormat}; crate::outgoing::OutgoingImage::Inline(super::super::attachment_tray::InlineImage { name: "pasted-image.png".into(), image: Arc::new(Image::from_bytes(ImageFormat::Png, vec![width as u8])), dimensions: Some((width, width)), }) } fn remote_gif(title: &str) -> crate::outgoing::OutgoingImage { crate::outgoing::OutgoingImage::Remote(super::RemoteImage { kind: crate::remote_image::RemoteKind::Gif, url: "https://media0.giphy.com/media/a/giphy.gif".into(), title: title.into(), width: 200, height: 100, }) } fn outgoing_with( text: &str, images: Vec, ) -> crate::outgoing::Outgoing { crate::outgoing::Outgoing { banner: None, draft: teams_core::Draft::plain(text), subject: None, mentions: Vec::new(), link_preview: None, reply: None, edit: None, images, files: Vec::new(), importance: Default::default(), } } #[test] fn an_image_is_placed_in_the_html_where_its_mark_sits() { let outgoing = outgoing_with("a \u{FFFC} b", vec![placed_image(2)]); assert_eq!( outgoing.html(), "a b" ); assert_eq!(outgoing.text(), "a b"); } #[test] fn images_keep_the_order_of_their_marks() { let outgoing = outgoing_with("\u{FFFC}x\u{FFFC}", vec![placed_image(2), placed_image(4)]); assert_eq!( outgoing.html(), "x" ); } #[test] fn gifs_sit_in_place_and_only_uploaded_images_are_numbered() { let outgoing = outgoing_with( "\u{FFFC}a\u{FFFC}b\u{FFFC}", vec![placed_image(2), remote_gif("Wave"), placed_image(4)], ); assert_eq!( outgoing.html(), "a\"Wave\"b" ); assert_eq!(outgoing.extras().images.len(), 2); } #[test] fn a_gif_title_is_escaped_in_the_html() { let outgoing = outgoing_with("\u{FFFC}", vec![remote_gif("a \"b\" ")]); assert!( outgoing .html() .contains("alt=\"a "b" <c>\"") ); } #[test] fn a_sticker_carries_the_sticker_itemtype() { let sticker = super::RemoteImage::sticker(crate::stickers::popular()[0]); let outgoing = outgoing_with( "\u{FFFC}", vec![crate::outgoing::OutgoingImage::Remote(sticker)], ); assert!( outgoing .html() .contains("itemtype=\"http://schema.skype.com/Sticker\"") ); assert!(outgoing.html().contains("width=\"250\" height=\"250\"")); } #[test] fn a_mark_without_an_image_leaves_no_trace() { assert_eq!(outgoing_with("a\u{FFFC}b", Vec::new()).html(), "ab"); } #[test] fn enter_sends_attachments_even_without_text() { assert!(super::is_plain_enter(&press(false))); assert!(!super::is_plain_enter(&press(true))); assert!(!super::is_plain_enter(&InputEvent::Change)); } mod keys { use std::sync::Arc; use gpui_kit::test::TestWindowExt as _; use gpui_kit::{AppContext as _, TestAppContext, WindowOptions}; use store::Store; use super::super::Composer; use crate::app_state::{AppState, Mode}; #[cfg_attr(target_os = "macos", ignore = "needs Ctrl bindings")] #[gpui_kit::test] fn ctrl_a_selects_the_whole_draft(cx: &mut TestAppContext) { cx.update(gpui_kit::init); let store = Arc::new(Store::open_in_memory().unwrap()); let mode = Mode { demo: true, read_only: false, demo_sync: None, }; let (handle, composer) = cx.update(|cx| { let app = cx.new(|_| AppState::new(store, mode)); gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { cx.new(|cx| Composer::new(app, window, cx)) }) .unwrap() }); let value = cx .update_window(handle, |_, window, cx| { composer.update(cx, |composer, cx| composer.focus(window, cx)); window.render_frame(cx); window.input("first line", cx); window.press("shift-enter", cx); window.input("second line", cx); window.press("ctrl-a", cx); window.input("X", cx); composer.read(cx).input.read(cx).value() }) .unwrap(); assert_eq!(value, "X"); } struct Typist<'a> { cx: &'a mut TestAppContext, handle: gpui_kit::AnyWindowHandle, composer: gpui_kit::Entity, } impl Typist<'_> { fn act(&mut self, action: impl FnOnce(&mut gpui_kit::Window, &mut gpui_kit::App)) { self.cx .update_window(self.handle, |_, window, cx| action(window, cx)) .unwrap(); self.cx.run_until_parked(); } fn type_text(&mut self, text: &str) { for character in text.chars() { self.act(|window, cx| window.input(&character.to_string(), cx)); } } fn press(&mut self, keys: &str) { for key in keys.split(' ') { self.act(|window, cx| window.press(key, cx)); } } fn value(&mut self) -> String { let composer = self.composer.clone(); self.cx .update(|cx| composer.read(cx).input.read(cx).value().to_string()) } fn draft(&mut self) -> teams_core::Draft { let composer = self.composer.clone(); self.cx.update(|cx| composer.read(cx).draft.clone()) } fn outgoing(&mut self) -> crate::outgoing::Outgoing { let composer = self.composer.clone(); self.cx .update(|cx| composer.read(cx).outgoing(cx)) .expect("text to send") } } fn typist(cx: &mut TestAppContext) -> Typist<'_> { let (handle, composer) = demo_composer(cx); cx.update(super::super::bind_keys); let mut typist = Typist { cx, handle, composer: composer.clone(), }; typist.act(|window, cx| { composer.update(cx, |composer, cx| composer.focus(window, cx)); window.render_frame(cx); }); typist } fn png_bytes(side: u32) -> Vec { let mut bytes = Vec::new(); image::RgbaImage::new(side, side) .write_to( &mut std::io::Cursor::new(&mut bytes), image::ImageFormat::Png, ) .unwrap(); bytes } fn paste_image(typist: &mut Typist<'_>, side: u32) { typist.cx.write_to_clipboard(gpui_kit::ClipboardItem { entries: vec![gpui_kit::ClipboardEntry::Image( gpui_kit::Image::from_bytes(gpui_kit::ImageFormat::Png, png_bytes(side)), )], }); typist.press("ctrl-v"); } fn image_ids(composer: &Composer) -> usize { composer .tray .items() .iter() .filter(|item| composer.tray.image(item.id).is_some()) .count() } fn image_tokens(typist: &mut Typist<'_>) -> Vec<(std::ops::Range, bool)> { let composer = typist.composer.clone(); typist.cx.update(|cx| { composer .read(cx) .input .read(cx) .tokens() .iter() .map(|span| (span.range(), span.token().is_block())) .collect() }) } #[cfg_attr(target_os = "macos", ignore = "needs Ctrl bindings")] #[gpui_kit::test] fn a_pasted_image_sits_at_the_cursor_and_is_sent_there(cx: &mut TestAppContext) { let mut typist = typist(cx); typist.type_text("ab"); typist.press("left"); paste_image(&mut typist, 2); assert_eq!(typist.value(), "a\u{FFFC}b"); assert_eq!(image_tokens(&mut typist), vec![(1..4, true)]); let outgoing = typist.outgoing(); assert_eq!(outgoing.images.len(), 1); assert_eq!( outgoing.html(), "ab" ); let composer = typist.composer.clone(); assert!(!typist.cx.update(|cx| composer.read(cx).tray.has_chips())); } fn wide_png_bytes() -> Vec { let mut bytes = Vec::new(); image::RgbaImage::new(600, 300) .write_to( &mut std::io::Cursor::new(&mut bytes), image::ImageFormat::Png, ) .unwrap(); bytes } fn caret_is_visible(typist: &mut Typist<'_>) -> bool { let composer = typist.composer.clone(); typist.cx.update(|cx| { let state = composer.read(cx).input.read(cx); let cursor = state.cursor(); let caret = state.range_to_bounds(&(cursor..cursor)).unwrap(); let viewport = state.input_bounds(); caret.origin.y >= viewport.origin.y && caret.bottom() <= viewport.bottom() }) } #[gpui_kit::test] fn typing_after_a_tall_image_keeps_the_caret_in_view(cx: &mut TestAppContext) { let mut typist = typist(cx); typist.type_text("look at this "); typist.cx.write_to_clipboard(gpui_kit::ClipboardItem { entries: vec![gpui_kit::ClipboardEntry::Image( gpui_kit::Image::from_bytes(gpui_kit::ImageFormat::Png, wide_png_bytes()), )], }); typist.press("ctrl-v"); typist.act(|window, _| window.refresh()); typist.type_text("and then more text"); typist.act(|window, _| window.refresh()); assert!(caret_is_visible(&mut typist)); } #[cfg_attr(target_os = "macos", ignore = "needs Ctrl bindings")] #[gpui_kit::test] fn deleting_the_token_drops_the_image_and_undo_brings_it_back(cx: &mut TestAppContext) { let mut typist = typist(cx); typist.type_text("ab"); typist.press("left"); paste_image(&mut typist, 2); typist.press("backspace"); assert_eq!(typist.value(), "ab"); assert!(image_tokens(&mut typist).is_empty()); let outgoing = typist.outgoing(); assert!(outgoing.images.is_empty()); assert_eq!(outgoing.html(), "ab"); typist.press("ctrl-z"); assert_eq!(typist.value(), "a\u{FFFC}b"); assert_eq!(image_tokens(&mut typist), vec![(1..4, true)]); assert_eq!(typist.outgoing().images.len(), 1); } #[cfg_attr(target_os = "macos", ignore = "needs Ctrl bindings")] #[gpui_kit::test] fn the_remove_button_drops_the_image_and_one_undo_brings_it_back(cx: &mut TestAppContext) { let mut typist = typist(cx); typist.type_text("ab"); paste_image(&mut typist, 2); typist.type_text("cd"); let composer = typist.composer.clone(); let id = typist.cx.update(|cx| { let state = composer.read(cx).input.read(cx); super::super::image_id(state.tokens()[0].token()).unwrap() }); typist.act(|window, cx| { composer.update(cx, |composer, cx| { composer.remove_image_token(id, window, cx) }); }); assert_eq!(typist.value(), "abcd"); assert!(image_tokens(&mut typist).is_empty()); assert!(typist.outgoing().images.is_empty()); typist.press("ctrl-z"); assert_eq!(typist.value(), "ab\u{FFFC}cd"); assert_eq!(image_tokens(&mut typist), vec![(2..5, true)]); assert_eq!(typist.outgoing().images.len(), 1); } #[cfg_attr(target_os = "macos", ignore = "needs Ctrl bindings")] #[gpui_kit::test] fn images_are_sent_in_the_order_they_appear_in_the_text(cx: &mut TestAppContext) { let mut typist = typist(cx); typist.type_text("x"); paste_image(&mut typist, 2); typist.type_text("y"); typist.press("ctrl-home"); paste_image(&mut typist, 4); assert_eq!(typist.value(), "\u{FFFC}x\u{FFFC}y"); let outgoing = typist.outgoing(); let dimensions: Vec<_> = outgoing .images .iter() .map(|image| match image { crate::outgoing::OutgoingImage::Inline(inline) => inline.dimensions, crate::outgoing::OutgoingImage::Remote(_) => None, }) .collect(); assert_eq!(dimensions, [Some((4, 4)), Some((2, 2))]); assert_eq!( outgoing.html(), "xy" ); } #[cfg_attr(target_os = "macos", ignore = "needs Ctrl bindings")] #[gpui_kit::test] fn an_image_alone_can_be_sent_and_never_leaks_into_copied_text(cx: &mut TestAppContext) { let mut typist = typist(cx); paste_image(&mut typist, 2); let composer = typist.composer.clone(); assert!(typist.cx.update(|cx| composer.read(cx).can_send(cx))); let outgoing = typist.outgoing(); assert_eq!(outgoing.text(), ""); assert_eq!(outgoing.images.len(), 1); typist.type_text("hi"); typist.press("ctrl-a ctrl-c"); let copied = typist.cx.read_from_clipboard().and_then(|item| item.text()); assert_eq!(copied.as_deref(), Some("hi")); } #[cfg_attr(target_os = "macos", ignore = "needs Ctrl bindings")] #[gpui_kit::test] fn an_inserted_gif_alone_can_be_sent_and_undo_removes_it(cx: &mut TestAppContext) { let mut typist = typist(cx); let composer = typist.composer.clone(); assert!(!typist.cx.update(|cx| composer.read(cx).can_send(cx))); typist.act(|window, cx| { composer.update(cx, |composer, cx| { composer.insert_remote_image( super::super::RemoteImage { kind: crate::remote_image::RemoteKind::Gif, url: "https://media0.giphy.com/media/a/giphy.gif".into(), title: "Wave".into(), width: 200, height: 100, }, window, cx, ) }); }); assert!(typist.cx.update(|cx| composer.read(cx).can_send(cx))); assert_eq!(typist.value(), "\u{FFFC}"); let outgoing = typist.outgoing(); assert!( outgoing .html() .starts_with("y"); } fn load( typist: &mut Typist<'_>, entry: &crate::agent_api::ProposalEntry, ) -> Vec<(String, Vec)> { let composer = typist.composer.clone(); let mentions = crate::views::proposal_card::outgoing_for(entry, None).mentions; let pictures = crate::views::proposal_card::picture_files(entry); let loaded = pictures.clone(); let text = entry.proposal.text.clone(); typist.act(|window, cx| { composer.update(cx, |composer, cx| { composer.load_proposal(&text, &mentions, pictures, window, cx) }); }); loaded } #[gpui_kit::test] fn a_loaded_proposal_keeps_its_mentions_images_and_the_reply(cx: &mut TestAppContext) { use crate::views::proposal_card::{sample_entry, sample_png}; let mut typist = typist(cx); let entry = sample_entry( "Thanks @Ada Example", &[("u1", "Ada Example")], &[sample_png(3, 0)], ); let composer = typist.composer.clone(); let reply = super::super::ReplyPreview { message_id: "m1".into(), author: "Bo".into(), excerpt: "question".into(), }; typist.act(|_, cx| { composer.update(cx, |composer, cx| { composer.set_reply(Some(reply.clone()), cx) }); }); load(&mut typist, &entry); let tokens = image_tokens(&mut typist); assert_eq!(tokens.iter().filter(|(_, block)| *block).count(), 1); assert_eq!(tokens.len(), 2); let outgoing = typist.outgoing(); assert_eq!(outgoing.images.len(), 1); assert_eq!( outgoing.mentions, [teams_core::MentionInput::user("u1", "Ada Example")] ); assert_eq!(outgoing.reply, Some(reply)); assert!(outgoing.html().starts_with("Thanks @Ada Example")); } #[gpui_kit::test] fn a_mention_at_the_start_of_a_line_is_placed_on_send(cx: &mut TestAppContext) { use crate::views::proposal_card::sample_entry; let mut typist = typist(cx); let entry = sample_entry("Hi team\n\n@Ada\n\nthanks", &[], &[]); load(&mut typist, &entry); let composer = typist.composer.clone(); typist.act(|window, cx| { composer.update(cx, |composer, cx| { composer.load_proposal( "Hi team\n\n@Ada\n\nthanks", &[teams_core::MentionInput::user("u1", "Ada")], Vec::new(), window, cx, ) }); }); let outgoing = typist.outgoing(); let (_, placed) = teams_core::apply_mentions(&outgoing.html(), &outgoing.mentions); assert_eq!(placed.len(), 1, "{}", outgoing.html()); } #[gpui_kit::test] fn a_text_only_proposal_keeps_a_running_upload(cx: &mut TestAppContext) { use crate::views::proposal_card::sample_entry; let mut typist = typist(cx); let composer = typist.composer.clone(); typist.act(|_, cx| { composer.update(cx, |composer, _| { let ids = composer.tray.add_pending(&["plan.pdf".to_owned()]); assert_eq!(ids.len(), 1); }); }); load(&mut typist, &sample_entry("just text", &[], &[])); let items = typist.cx.update(|cx| composer.read(cx).tray.items().len()); assert_eq!(items, 1); assert_eq!(typist.value(), "just text"); } #[gpui_kit::test] fn proposal_images_join_the_tray_and_big_ones_become_uploads(cx: &mut TestAppContext) { use crate::views::proposal_card::{sample_entry, sample_png}; let mut typist = typist(cx); let composer = typist.composer.clone(); typist.act(|_, cx| { composer.update(cx, |composer, _| { composer.conversation_id = Some("c1".to_owned()); composer.tray.add_pending(&["kept.pdf".to_owned()]); }); }); let entry = sample_entry( "pictures", &[], &[sample_png(2, 0), sample_png(2, 2 * 1024 * 1024)], ); load(&mut typist, &entry); let (items, images) = typist.cx.update(|cx| { let composer = composer.read(cx); (composer.tray.items().len(), image_ids(composer)) }); assert_eq!(items, 3); assert_eq!(images, 1); } #[gpui_kit::test] fn typed_markdown_is_formatted_and_backspace_brings_it_back(cx: &mut TestAppContext) { use teams_core::{FormatState, MarkKind}; let mut typist = typist(cx); typist.type_text("a **bold**"); assert_eq!(typist.value(), "a bold"); assert_eq!(typist.draft().state(2..6, &MarkKind::Bold), FormatState::On); typist.press("backspace"); assert_eq!(typist.value(), "a **bold**"); assert!(typist.draft().marks().is_empty()); let composer = typist.composer.clone(); assert_eq!( typist .cx .update(|cx| composer.read(cx).input.read(cx).cursor()), 10 ); typist.press("backspace"); typist.type_text("* x"); assert_eq!(typist.value(), "a bold x"); assert_eq!( typist.draft().state(6..8, &MarkKind::Bold), FormatState::Off ); } #[gpui_kit::test] fn typed_url_is_linked_and_backspace_takes_the_link_back(cx: &mut TestAppContext) { use teams_core::{Mark, MarkKind}; let mut typist = typist(cx); typist.type_text("see https://a.b "); assert_eq!(typist.value(), "see https://a.b "); assert_eq!( typist.draft().marks(), [Mark { range: 4..15, kind: MarkKind::Link("https://a.b".into()) }] ); typist.press("backspace"); assert_eq!(typist.value(), "see https://a.b "); assert!(typist.draft().marks().is_empty()); } #[cfg_attr(target_os = "macos", ignore = "needs Ctrl bindings")] #[gpui_kit::test] fn ctrl_z_takes_an_autolink_back(cx: &mut TestAppContext) { let mut typist = typist(cx); typist.type_text("https://a.b "); assert_eq!(typist.draft().marks().len(), 1); typist.press("ctrl-z"); assert!(typist.draft().marks().is_empty()); } #[cfg_attr(target_os = "macos", ignore = "needs Ctrl bindings")] #[gpui_kit::test] fn pasted_url_is_linked_and_ctrl_z_restores_the_raw_text(cx: &mut TestAppContext) { use teams_core::{Mark, MarkKind}; let mut typist = typist(cx); typist .cx .write_to_clipboard(gpui_kit::ClipboardItem::new_string( "x https://a.b y".to_owned(), )); typist.press("ctrl-v"); assert_eq!(typist.value(), "x https://a.b y"); assert_eq!( typist.draft().marks(), [Mark { range: 2..13, kind: MarkKind::Link("https://a.b".into()) }] ); typist.press("ctrl-z"); assert_eq!(typist.value(), "x https://a.b y"); assert!(typist.draft().marks().is_empty()); } #[gpui_kit::test] fn the_link_editor_changes_and_removes_an_autolink(cx: &mut TestAppContext) { use teams_core::{Mark, MarkKind}; let mut typist = typist(cx); typist.type_text("https://a.b x"); typist.press("home"); typist.press("shift-right ".repeat(11).trim_end()); typist.press("ctrl-k"); typist.type_text("other.de"); typist.press("enter"); assert_eq!( typist.draft().marks(), [Mark { range: 0..11, kind: MarkKind::Link("https://other.de".into()) }] ); typist.press("ctrl-k backspace enter"); assert!(typist.draft().marks().is_empty()); assert_eq!(typist.value(), "https://a.b x"); } #[gpui_kit::test] fn enter_in_a_list_links_the_url_before_the_cursor(cx: &mut TestAppContext) { use teams_core::{Mark, MarkKind}; let mut typist = typist(cx); typist.type_text("- https://a.b"); typist.press("enter"); assert_eq!(typist.value(), "• https://a.b\n• "); assert_eq!( typist.draft().marks(), [Mark { range: 4..15, kind: MarkKind::Link("https://a.b".into()) }] ); } #[cfg_attr(target_os = "macos", ignore = "needs Ctrl bindings")] #[gpui_kit::test] fn a_url_only_paste_links_without_the_paste_hint(cx: &mut TestAppContext) { let mut typist = typist(cx); typist .cx .write_to_clipboard(gpui_kit::ClipboardItem::new_string( "x https://a.b y".to_owned(), )); typist.press("ctrl-v"); assert_eq!(typist.draft().marks().len(), 1); let composer = typist.composer.clone(); assert!( typist .cx .update(|cx| composer.read(cx).paste_hint.is_none()) ); typist.press("ctrl-z"); assert!(typist.draft().marks().is_empty()); } #[gpui_kit::test] fn enter_continues_a_list_instead_of_sending(cx: &mut TestAppContext) { let mut typist = typist(cx); typist.type_text("1. one"); typist.press("enter"); typist.type_text("two"); assert_eq!(typist.value(), "1. one\n2. two"); typist.press("enter enter"); assert_eq!(typist.value(), "1. one\n2. two\n"); typist.type_text("after"); typist.press("enter"); assert_eq!(typist.value(), ""); } #[gpui_kit::test] fn tab_nests_a_list_item(cx: &mut TestAppContext) { let mut typist = typist(cx); typist.type_text("- a"); typist.press("enter tab"); typist.type_text("b"); assert_eq!(typist.value(), "• a\n◦ b"); typist.press("shift-tab"); assert_eq!(typist.value(), "• a\n• b"); } #[gpui_kit::test] fn ctrl_b_toggles_bold_on_a_selection_and_for_the_next_text(cx: &mut TestAppContext) { use teams_core::{FormatState, MarkKind}; let mut typist = typist(cx); typist.type_text("hello"); typist.press("shift-left shift-left ctrl-b"); assert_eq!(typist.draft().state(3..5, &MarkKind::Bold), FormatState::On); assert_eq!( typist.draft().state(0..5, &MarkKind::Bold), FormatState::Mixed ); typist.press("ctrl-b"); assert!(typist.draft().marks().is_empty()); typist.press("end ctrl-i"); typist.type_text("!"); assert_eq!(typist.outgoing().html(), "hello!"); } #[gpui_kit::test] fn script_shortcuts_exclude_each_other_and_send_teams_html(cx: &mut TestAppContext) { let mut typist = typist(cx); typist.type_text("x2"); typist.press("shift-left ctrl-shift-="); assert_eq!(typist.outgoing().html(), "x2"); typist.press("ctrl-="); assert_eq!(typist.outgoing().html(), "x2"); } #[gpui_kit::test] fn the_toolbar_state_follows_the_selection(cx: &mut TestAppContext) { use teams_core::FormatState; use super::super::FormatButton; let mut typist = typist(cx); typist.type_text("big small"); typist.press("shift-left shift-left shift-left shift-left shift-left"); let composer = typist.composer.clone(); typist.act(|window, cx| { composer.update(cx, |composer, cx| { composer.press_format(FormatButton::SizeSmall, window, cx) }) }); let states = |typist: &mut Typist<'_>, range: std::ops::Range| { let composer = typist.composer.clone(); typist.cx.update(|cx| { let composer = composer.read(cx); [ FormatButton::SizeSmall, FormatButton::SizeNormal, FormatButton::SizeLarge, ] .map(|button| composer.format_state(button, &range)) }) }; assert_eq!( states(&mut typist, 4..9), [FormatState::On, FormatState::Off, FormatState::Off] ); assert_eq!( states(&mut typist, 0..9), [FormatState::Mixed, FormatState::Off, FormatState::Off] ); assert_eq!( states(&mut typist, 0..3), [FormatState::Off, FormatState::On, FormatState::Off] ); assert_eq!( typist.outgoing().html(), "big small" ); } #[cfg_attr(target_os = "macos", ignore = "needs Ctrl bindings")] #[gpui_kit::test] fn pasted_markdown_converts_and_ctrl_z_restores_the_raw_text(cx: &mut TestAppContext) { let mut typist = typist(cx); typist .cx .write_to_clipboard(gpui_kit::ClipboardItem::new_string( "**hi** there".to_owned(), )); typist.press("ctrl-v"); assert_eq!(typist.value(), "hi there"); let composer = typist.composer.clone(); assert!( typist .cx .update(|cx| composer.read(cx).paste_hint.is_some()) ); typist.press("ctrl-z"); assert_eq!(typist.value(), "**hi** there"); assert!(typist.draft().marks().is_empty()); } #[cfg_attr(target_os = "macos", ignore = "needs Ctrl bindings")] #[gpui_kit::test] fn copy_and_paste_inside_the_composer_keeps_formatting(cx: &mut TestAppContext) { use teams_core::{FormatState, MarkKind}; let mut typist = typist(cx); typist.type_text("`code` "); typist.press("ctrl-a ctrl-c end ctrl-v"); assert_eq!(typist.value(), "code code "); assert_eq!(typist.draft().state(5..9, &MarkKind::Code), FormatState::On); } #[cfg_attr(target_os = "macos", ignore = "needs Ctrl bindings")] #[gpui_kit::test] fn undo_brings_the_formatting_back_with_the_text(cx: &mut TestAppContext) { use teams_core::{FormatState, MarkKind}; let mut typist = typist(cx); typist.type_text("~~old~~"); typist.press("ctrl-a"); typist.type_text("new"); typist.press("ctrl-z ctrl-z"); assert_eq!(typist.value(), "old"); assert_eq!( typist.draft().state(0..3, &MarkKind::Strike), FormatState::On ); } #[gpui_kit::test] fn ctrl_k_turns_the_selection_into_a_link(cx: &mut TestAppContext) { let mut typist = typist(cx); typist.type_text("see docs"); typist.press("shift-left shift-left shift-left shift-left ctrl-k"); let composer = typist.composer.clone(); assert!( typist .cx .update(|cx| composer.read(cx).link_editor.is_some()) ); typist.type_text("example.com"); typist.press("enter"); assert!( typist .cx .update(|cx| composer.read(cx).link_editor.is_none()) ); assert_eq!( typist.outgoing().html(), "see docs" ); } #[cfg_attr(target_os = "macos", ignore = "needs Ctrl bindings")] #[gpui_kit::test] fn ctrl_z_takes_back_bold_before_the_typing_and_ctrl_y_redoes_it(cx: &mut TestAppContext) { use teams_core::{FormatState, MarkKind}; let mut typist = typist(cx); typist.type_text("hello"); typist.press("shift-left shift-left ctrl-b"); typist.press("ctrl-z"); assert_eq!(typist.value(), "hello"); assert!(typist.draft().marks().is_empty()); typist.press("ctrl-y"); assert_eq!(typist.draft().state(3..5, &MarkKind::Bold), FormatState::On); } #[cfg_attr(target_os = "macos", ignore = "needs Ctrl bindings")] #[gpui_kit::test] fn undo_after_typing_undoes_the_typing_not_an_older_bold(cx: &mut TestAppContext) { use teams_core::{FormatState, MarkKind}; let mut typist = typist(cx); typist.type_text("hello"); typist.press("shift-left shift-left ctrl-b end"); typist.type_text(" x"); typist.press("backspace backspace"); assert_eq!(typist.value(), "hello"); typist.press("ctrl-z"); assert_ne!(typist.value(), "hello"); assert_eq!(typist.draft().state(3..5, &MarkKind::Bold), FormatState::On); } #[gpui_kit::test] fn backspace_after_a_fence_gives_it_back_for_a_language(cx: &mut TestAppContext) { let mut typist = typist(cx); typist.type_text("```"); assert_eq!(typist.draft().lines(), [teams_core::LineKind::Code(None)]); typist.press("backspace"); assert_eq!(typist.value(), "```"); typist.type_text("py"); typist.press("enter"); assert_eq!( typist.draft().lines(), [teams_core::LineKind::Code(Some("python".into()))] ); } #[gpui_kit::test] fn the_language_picker_in_the_header_sets_the_whole_code_block(cx: &mut TestAppContext) { use gpui_kit::VisualTestContext; use super::super::code_language::{CODE_LANGUAGE_ROW_TAG, CODE_LANGUAGE_TAG}; let mut typist = typist(cx); typist.type_text("x"); typist.press("shift-enter"); typist.type_text("```"); typist.type_text("a"); typist.press("enter"); typist.type_text("b"); let composer = typist.composer.clone(); let handle = typist.handle; let mut visual = VisualTestContext::from_window(handle, typist.cx); visual.update(|window, cx| window.render_frame(cx)); let header = visual .debug_bounds(CODE_LANGUAGE_TAG) .expect("a header on the block"); visual.simulate_click(header.center(), Default::default()); visual.run_until_parked(); assert_eq!( composer.read_with(&visual, |composer, _| composer.code_language_menu), Some(1) ); visual.update(|window, cx| window.render_frame(cx)); let row = visual .debug_bounds(Box::leak( format!("{CODE_LANGUAGE_ROW_TAG}dart").into_boxed_str(), )) .expect("Dart in the open menu"); visual.simulate_click(row.center(), Default::default()); visual.run_until_parked(); let draft = composer.read_with(&visual, |composer, _| composer.draft.clone()); let dart = teams_core::LineKind::Code(Some("dart".into())); assert_eq!( draft.lines(), [teams_core::LineKind::Text, dart.clone(), dart] ); assert_eq!( draft.to_html(), "x
a
b
" ); } #[gpui_kit::test] fn code_typed_after_picking_a_language_is_highlighted(cx: &mut TestAppContext) { let mut typist = typist(cx); typist.type_text("```"); let composer = typist.composer.clone(); typist.act(|window, cx| { composer.update(cx, |composer, cx| { composer.choose_code_language(0, "python", window, cx) }) }); typist.type_text("def f():"); typist.press("enter"); typist.type_text("class A: pass"); let draft = typist.draft(); assert_eq!( draft.lines()[1], teams_core::LineKind::Code(Some("python".into())) ); let style = super::super::draft_style(&draft, "Mono".into()); let words: Vec<_> = style .text .iter() .map(|decoration| &draft.text()[decoration.range.clone()]) .collect(); for keyword in ["def", "class", "pass"] { assert!(words.contains(&keyword), "{keyword} in {words:?}"); } } #[gpui_kit::test] fn copied_code_leaves_its_header_behind_and_escape_closes_the_picker( cx: &mut TestAppContext, ) { let mut typist = typist(cx); typist.type_text("x"); typist.press("shift-enter"); typist.type_text("```"); typist.type_text("a"); let composer = typist.composer.clone(); let (input, end) = typist.cx.update(|cx| { let composer = composer.read(cx); (composer.input.clone(), composer.draft.text().len()) }); typist.act(|_, cx| { input.update(cx, |state, cx| state.set_selected_range(0..end, cx)); composer.update(cx, |composer, cx| { composer.copy_selection(cx); composer.code_language_menu = Some(1); }) }); let copied = typist.cx.read_from_clipboard().and_then(|item| item.text()); assert_eq!(copied.as_deref(), Some("x\na")); typist.press("escape"); assert_eq!( typist.cx.update(|cx| composer.read(cx).code_language_menu), None ); } #[gpui_kit::test] fn delete_at_a_code_line_end_joins_without_padding(cx: &mut TestAppContext) { let mut typist = typist(cx); typist.type_text("```"); typist.type_text("a"); typist.press("enter"); typist.type_text("b"); typist.press("up end delete"); assert_eq!( typist.outgoing().draft.to_html(), "
ab
" ); } #[cfg_attr(target_os = "macos", ignore = "needs Ctrl bindings")] #[gpui_kit::test] fn copied_code_has_no_padding_in_the_plain_text(cx: &mut TestAppContext) { let mut typist = typist(cx); typist.type_text("```"); typist.type_text(" x"); typist.press("ctrl-a ctrl-c"); let text = typist.cx.read_from_clipboard().and_then(|item| item.text()); assert_eq!(text.as_deref(), Some(" x")); } #[gpui_kit::test] fn a_refused_link_keeps_the_field_open_and_says_why(cx: &mut TestAppContext) { let mut typist = typist(cx); typist.type_text("docs"); typist.press("ctrl-a ctrl-k"); typist.type_text("javascript:alert(1)"); typist.press("enter"); let composer = typist.composer.clone(); assert!(typist.cx.update(|cx| { composer .read(cx) .link_editor .as_ref() .is_some_and(|editor| editor.refused) })); assert!(typist.draft().marks().is_empty()); typist.type_text("x"); assert!(typist.cx.update(|cx| { composer .read(cx) .link_editor .as_ref() .is_some_and(|editor| !editor.refused) })); } #[cfg_attr(target_os = "macos", ignore = "needs Ctrl bindings")] #[gpui_kit::test] fn undo_takes_back_a_list_toggle_then_the_bold_before_it(cx: &mut TestAppContext) { use teams_core::{FormatState, LineKind, MarkKind}; let mut typist = typist(cx); typist.type_text("hello"); typist.press("ctrl-a ctrl-b"); let composer = typist.composer.clone(); typist.act(|window, cx| { composer.update(cx, |composer, cx| { composer.press_format(super::super::FormatButton::Bulleted, window, cx) }) }); assert_eq!(typist.value(), "• hello"); typist.press("ctrl-z"); assert_eq!(typist.value(), "hello"); assert_eq!(typist.draft().state(0..5, &MarkKind::Bold), FormatState::On); typist.press("ctrl-z"); assert!(typist.draft().marks().is_empty()); assert_eq!(typist.draft().lines(), [LineKind::Text]); } #[cfg_attr(target_os = "macos", ignore = "needs Ctrl bindings")] #[gpui_kit::test] fn redo_brings_back_typing_and_bold_in_order(cx: &mut TestAppContext) { use teams_core::{FormatState, MarkKind}; let mut typist = typist(cx); typist.type_text("hello"); typist.press("ctrl-a ctrl-b ctrl-z ctrl-z"); assert_eq!(typist.value(), ""); typist.press("ctrl-y"); assert_eq!(typist.value(), "hello"); assert!(typist.draft().marks().is_empty()); typist.press("ctrl-y"); assert_eq!(typist.draft().state(0..5, &MarkKind::Bold), FormatState::On); } fn cursor(typist: &mut Typist<'_>) -> usize { let composer = typist.composer.clone(); typist .cx .update(|cx| composer.read(cx).input.read(cx).cursor()) } #[cfg_attr(target_os = "macos", ignore = "needs Ctrl bindings")] #[gpui_kit::test] fn typing_elsewhere_is_its_own_undo_step_and_undo_puts_the_caret_there( cx: &mut TestAppContext, ) { let mut typist = typist(cx); typist.type_text("hello"); typist.press("home"); typist.type_text("X"); typist.press("end ctrl-z"); assert_eq!(typist.value(), "hello"); assert_eq!(cursor(&mut typist), 0); typist.press("ctrl-y"); assert_eq!(typist.value(), "Xhello"); assert_eq!(cursor(&mut typist), 1); } #[gpui_kit::test] fn undo_keeps_the_scroll_position(cx: &mut TestAppContext) { let mut typist = typist(cx); for line in 0..14 { typist.type_text(&format!("line {line}")); typist.press("shift-enter"); } typist.type_text("end"); let composer = typist.composer.clone(); let scroll = |typist: &mut Typist<'_>| { typist .cx .update(|cx| composer.read(cx).input.read(cx).scroll_offset().y) }; typist.act(|window, cx| window.render_frame(cx)); let before = scroll(&mut typist); assert!(before < gpui_kit::px(0.)); typist.press("ctrl-z"); typist.act(|window, cx| window.render_frame(cx)); assert_eq!(scroll(&mut typist), before); } #[cfg_attr(target_os = "macos", ignore = "needs Ctrl bindings")] #[gpui_kit::test] fn a_smiley_taken_back_with_ctrl_z_is_sent_as_typed(cx: &mut TestAppContext) { let mut typist = typist(cx); typist.type_text("ok :) "); assert!(typist.value().contains('🙂')); typist.press("ctrl-z"); assert_eq!(typist.value(), "ok :) "); assert_eq!(typist.outgoing().text(), "ok :)"); } #[gpui_kit::test] fn an_undo_with_nothing_to_undo_leaves_conversions_working(cx: &mut TestAppContext) { let mut typist = typist(cx); typist.press("ctrl-z"); typist.type_text("**a**"); assert_eq!(typist.value(), "a"); } #[cfg_attr(target_os = "macos", ignore = "needs Ctrl bindings")] #[gpui_kit::test] fn code_blocks_take_markdown_and_smileys_literally(cx: &mut TestAppContext) { let mut typist = typist(cx); typist.type_text("```"); typist.type_text(":) "); typist .cx .write_to_clipboard(gpui_kit::ClipboardItem::new_string("**x**".to_owned())); typist.press("ctrl-v"); assert_eq!( typist.value(), format!("{}:) **x**", teams_core::CODE_HEADER_MARK) ); let composer = typist.composer.clone(); assert!( typist .cx .update(|cx| composer.read(cx).paste_hint.is_none()) ); let outgoing = typist.outgoing(); assert_eq!( outgoing.draft.to_html(), "
:) **x**
" ); } #[gpui_kit::test] fn quotes_continue_on_shift_enter_and_send_as_blockquote(cx: &mut TestAppContext) { let mut typist = typist(cx); typist.type_text(">quoted"); typist.press("shift-enter"); typist.type_text("more"); let outgoing = typist.outgoing(); assert_eq!(outgoing.html(), "
quoted
more
"); assert_eq!(outgoing.text(), "quoted\nmore"); } #[gpui_kit::test] fn enter_in_a_quote_adds_a_line_and_enter_on_the_empty_one_leaves_it( cx: &mut TestAppContext, ) { let mut typist = typist(cx); typist.type_text(">quoted"); typist.press("enter"); typist.type_text("more"); typist.press("enter enter"); typist.type_text("after"); assert_eq!(typist.value(), "quoted\nmore\nafter"); assert_eq!( typist.outgoing().html(), "
quoted
more
after" ); } #[gpui_kit::test] fn enter_on_an_empty_quote_line_leaves_it_with_the_format_bar_open( cx: &mut TestAppContext, ) { let mut typist = typist(cx); typist.press("ctrl-shift-x"); typist.type_text(">quoted"); typist.press("enter enter"); typist.type_text("after"); assert_eq!( typist.outgoing().html(), "
quoted
after" ); } #[gpui_kit::test] fn down_at_the_end_of_a_quote_leaves_it(cx: &mut TestAppContext) { let mut typist = typist(cx); typist.type_text(">quoted"); typist.press("down"); typist.type_text("after"); assert_eq!( typist.outgoing().html(), "
quoted
after" ); } fn insert_table(typist: &mut Typist<'_>, rows: usize, columns: usize) { let composer = typist.composer.clone(); typist.act(|window, cx| { composer.update(cx, |composer, cx| { composer.insert_table(rows, columns, window, cx) }); window.render_frame(cx); }); } #[gpui_kit::test] fn a_table_takes_tab_grows_a_row_at_the_end_and_sends_as_teams_html( cx: &mut TestAppContext, ) { let mut typist = typist(cx); typist.type_text("before"); insert_table(&mut typist, 1, 2); typist.type_text("a"); typist.press("tab"); typist.type_text("b"); typist.press("tab"); typist.act(|window, cx| window.render_frame(cx)); typist.type_text("c"); typist.press("down"); typist.type_text("after"); assert_eq!( typist.outgoing().html(), "before
\ \
ab
c 
after" ); } fn format_bar_open(typist: &mut Typist<'_>) -> bool { let composer = typist.composer.clone(); typist.cx.update(|cx| composer.read(cx).format_bar_open()) } #[gpui_kit::test] fn ctrl_shift_x_opens_the_format_bar_per_conversation(cx: &mut TestAppContext) { let mut typist = typist(cx); let composer = typist.composer.clone(); let open_chat = |typist: &mut Typist<'_>, id: &'static str| { let composer = composer.clone(); typist.act(|window, cx| { composer.update(cx, |composer, cx| { composer.set_conversation(id, window, cx); composer.focus(window, cx); }) }); }; open_chat(&mut typist, "chat-one"); assert!(!format_bar_open(&mut typist)); typist.press("ctrl-shift-x"); assert!(format_bar_open(&mut typist)); open_chat(&mut typist, "chat-two"); assert!(!format_bar_open(&mut typist)); open_chat(&mut typist, "chat-one"); assert!(format_bar_open(&mut typist)); typist.press("ctrl-shift-x"); assert!(!format_bar_open(&mut typist)); } #[cfg_attr(target_os = "macos", ignore = "needs Ctrl bindings")] #[gpui_kit::test] fn ctrl_alt_x_strikes_through_now_that_ctrl_shift_x_is_format(cx: &mut TestAppContext) { let mut typist = typist(cx); typist.type_text("gone"); typist.press("ctrl-a ctrl-alt-x"); assert_eq!(typist.outgoing().html(), "gone"); assert!(!format_bar_open(&mut typist)); } #[gpui_kit::test] fn the_format_bar_shows_the_style_at_the_caret_without_a_selection( cx: &mut TestAppContext, ) { use teams_core::FormatState; use super::super::FormatButton; let mut typist = typist(cx); typist.press("ctrl-shift-x ctrl-b"); let composer = typist.composer.clone(); let bold = |typist: &mut Typist<'_>| { let composer = composer.clone(); typist.cx.update(|cx| { let composer = composer.read(cx); let caret = composer.selection(cx); composer.button_state(FormatButton::Bold, &caret, false) }) }; assert_eq!(bold(&mut typist).format, FormatState::On); typist.type_text("x"); assert_eq!(bold(&mut typist).format, FormatState::On); typist.press("ctrl-b"); assert_eq!(bold(&mut typist).format, FormatState::Off); assert!(bold(&mut typist).enabled); } #[gpui_kit::test] fn the_format_bar_inserts_a_table_without_a_selection(cx: &mut TestAppContext) { use super::super::FormatButton; let mut typist = typist(cx); typist.press("ctrl-shift-x"); let composer = typist.composer.clone(); typist.act(|window, cx| { composer.update(cx, |composer, cx| { composer.press_format(FormatButton::Table, window, cx) }) }); let picker_open = typist .cx .update(|cx| composer.read(cx).table_picker.is_some()); assert!(picker_open); insert_table(&mut typist, 2, 2); assert!(typist.draft().lines()[0].is_table()); } #[gpui_kit::test] fn ctrl_alt_plus_and_minus_open_the_teams_row_and_column_menus(cx: &mut TestAppContext) { use super::super::table::TableLineMenu; let mut typist = typist(cx); let composer = typist.composer.clone(); let menu = |typist: &mut Typist<'_>| { let composer = composer.clone(); typist .cx .update(|cx| composer.read(cx).table_menu.map(|menu| menu.kind)) }; typist.press("ctrl-alt-="); assert_eq!(menu(&mut typist), None); insert_table(&mut typist, 1, 1); typist.type_text("a"); typist.press("ctrl-alt-="); assert_eq!(menu(&mut typist), Some(TableLineMenu::Insert)); assert!(format_bar_open(&mut typist)); typist.press("enter"); assert_eq!(menu(&mut typist), None); let shape = |typist: &mut Typist<'_>| { let draft = typist.draft(); let table = draft.table(0).expect("table").clone(); (table.row_count(), table.column_count()) }; assert_eq!(shape(&mut typist), (1, 2)); typist.press("ctrl-alt-- enter"); assert_eq!(shape(&mut typist), (1, 1)); typist.press("ctrl-alt-- escape"); assert_eq!(menu(&mut typist), None); assert_eq!(shape(&mut typist), (1, 1)); } #[cfg_attr(target_os = "macos", ignore = "needs Ctrl bindings")] #[gpui_kit::test] fn ctrl_a_highlights_a_table_like_selected_text(cx: &mut TestAppContext) { use gpui_kit::VisualTestContext; use super::super::table::SELECTED_TABLE_TAG; let mut typist = typist(cx); typist.type_text("before"); insert_table(&mut typist, 1, 2); typist.type_text("a"); typist.press("down"); typist.type_text("after"); let handle = typist.handle; let mut visual = VisualTestContext::from_window(handle, typist.cx); visual.update(|window, cx| window.render_frame(cx)); assert!(visual.debug_bounds(SELECTED_TABLE_TAG).is_none()); visual.simulate_keystrokes("ctrl-a"); visual.run_until_parked(); visual.update(|window, cx| window.render_frame(cx)); assert!(visual.debug_bounds(SELECTED_TABLE_TAG).is_some()); } #[gpui_kit::test] fn deleting_the_last_column_deletes_the_table(cx: &mut TestAppContext) { let mut typist = typist(cx); insert_table(&mut typist, 1, 1); let composer = typist.composer.clone(); typist.act(|window, cx| { composer.update(cx, |composer, cx| { composer.edit_table(teams_core::TableEdit::DeleteColumn, window, cx) }); }); assert!(typist.draft().lines().iter().all(|kind| !kind.is_table())); } #[cfg_attr(target_os = "macos", ignore = "needs Ctrl bindings")] #[gpui_kit::test] fn a_pasted_markdown_table_becomes_a_table(cx: &mut TestAppContext) { let mut typist = typist(cx); typist .cx .write_to_clipboard(gpui_kit::ClipboardItem::new_string( "| A | B |\n|---|---|\n| 1 | 2 |".to_owned(), )); typist.press("ctrl-v"); assert!(typist.draft().lines()[0].is_table()); assert_eq!( typist.outgoing().html(), "
\ \
AB
12
" ); } #[gpui_kit::test] fn ctrl_shift_v_pastes_markdown_as_plain_text(cx: &mut TestAppContext) { let mut typist = typist(cx); typist .cx .write_to_clipboard(gpui_kit::ClipboardItem::new_string( "**hi** there".to_owned(), )); typist.press("ctrl-shift-v"); assert_eq!(typist.value(), "**hi** there"); assert!(typist.draft().marks().is_empty()); } #[gpui_kit::test] fn demo_upload_runs_to_done_and_a_file_alone_can_be_sent(cx: &mut TestAppContext) { cx.update(gpui_kit::init); let store = Arc::new(Store::open_in_memory().unwrap()); let mode = Mode { demo: true, read_only: false, demo_sync: None, }; let (handle, composer) = cx.update(|cx| { let app = cx.new(|_| AppState::new(store, mode)); gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { cx.new(|cx| Composer::new(app, window, cx)) }) .unwrap() }); let directory = tempfile::tempdir().unwrap(); let path = directory.path().join("plan.pdf"); std::fs::write(&path, b"%PDF-1.7").unwrap(); cx.update_window(handle, |_, window, cx| { composer.update(cx, |composer, cx| { composer.set_conversation("demo-chat", window, cx); composer.add_paths(vec![path], window, cx); }); }) .unwrap(); cx.run_until_parked(); cx.update(|cx| { let composer = composer.read(cx); assert!(composer.tray.blocks_send()); assert!(!composer.can_send(cx)); }); cx.executor() .advance_clock(std::time::Duration::from_secs(3)); cx.run_until_parked(); cx.update(|cx| { let composer = composer.read(cx); assert!(composer.can_send(cx)); let outgoing = composer .outgoing(cx) .expect("an attachment alone is a message"); assert_eq!(outgoing.text(), ""); assert_eq!(outgoing.files.len(), 1); assert_eq!(outgoing.files[0].name, "plan.pdf"); }); } #[gpui_kit::test] fn attachments_are_dropped_when_the_conversation_changes(cx: &mut TestAppContext) { cx.update(gpui_kit::init); let store = Arc::new(Store::open_in_memory().unwrap()); let mode = Mode { demo: true, read_only: false, demo_sync: None, }; let (handle, composer) = cx.update(|cx| { let app = cx.new(|_| AppState::new(store, mode)); gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { cx.new(|cx| Composer::new(app, window, cx)) }) .unwrap() }); let directory = tempfile::tempdir().unwrap(); let path = directory.path().join("plan.pdf"); std::fs::write(&path, b"%PDF-1.7").unwrap(); cx.update_window(handle, |_, window, cx| { composer.update(cx, |composer, cx| { composer.set_conversation("demo-chat", window, cx); composer.add_paths(vec![path], window, cx); }); }) .unwrap(); cx.run_until_parked(); cx.update_window(handle, |_, window, cx| { composer.update(cx, |composer, cx| { composer.set_conversation("other-chat", window, cx) }); }) .unwrap(); cx.update(|cx| assert!(composer.read(cx).tray.items().is_empty())); } fn demo_composer( cx: &mut TestAppContext, ) -> (gpui_kit::AnyWindowHandle, gpui_kit::Entity) { cx.update(gpui_kit::init); let store = Arc::new(Store::open_in_memory().unwrap()); let mode = Mode { demo: true, read_only: false, demo_sync: None, }; let (handle, composer) = cx.update(|cx| { let app = cx.new(|_| AppState::new(store, mode)); gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { cx.new(|cx| Composer::new(app, window, cx)) }) .unwrap() }); (handle, composer) } fn small_png(directory: &std::path::Path) -> std::path::PathBuf { let path = directory.join("dot.png"); image::RgbaImage::new(2, 2).save(&path).unwrap(); path } #[gpui_kit::test] fn editing_keeps_the_attachments_and_hides_them_from_the_edit(cx: &mut TestAppContext) { use super::super::{EditPreview, Outgoing}; let (handle, composer) = demo_composer(cx); let directory = tempfile::tempdir().unwrap(); let path = directory.path().join("plan.pdf"); std::fs::write(&path, b"%PDF-1.7").unwrap(); cx.update_window(handle, |_, window, cx| { composer.update(cx, |composer, cx| { composer.set_conversation("demo-chat", window, cx); composer.add_paths(vec![path], window, cx); }); }) .unwrap(); cx.run_until_parked(); cx.executor() .advance_clock(std::time::Duration::from_secs(3)); cx.run_until_parked(); cx.update_window(handle, |_, window, cx| { composer.update(cx, |composer, cx| { let draft = Outgoing { banner: None, draft: teams_core::Draft::plain("old"), subject: None, mentions: Vec::new(), link_preview: None, reply: None, edit: Some(EditPreview { message_id: "m1".into(), excerpt: "old".into(), scheduled: None, }), images: Vec::new(), files: Vec::new(), importance: Default::default(), }; composer.set_text("half reply", window, cx); composer.begin_edit(draft, window, cx); }); }) .unwrap(); cx.update(|cx| { let composer = composer.read(cx); assert_eq!(composer.tray.items().len(), 1); let outgoing = composer.outgoing(cx).unwrap(); assert!(outgoing.files.is_empty()); assert_eq!(outgoing.text(), "old"); }); cx.update_window(handle, |_, window, cx| { composer.update(cx, |composer, cx| composer.cancel_edit(window, cx)); }) .unwrap(); cx.update(|cx| { let composer = composer.read(cx); assert_eq!(composer.tray.items().len(), 1); assert!(composer.can_send(cx)); assert_eq!(composer.outgoing(cx).unwrap().text(), "half reply"); }); } #[gpui_kit::test] fn draft_images_follow_only_into_the_chat_that_was_chosen(cx: &mut TestAppContext) { let (handle, composer) = demo_composer(cx); let directory = tempfile::tempdir().unwrap(); let path = small_png(directory.path()); for (carry, chosen, expected) in [ (Some("chat-a"), "chat-a", 1), (None, "chat-b", 0), (Some("chat-a"), "chat-b", 0), ] { cx.update_window(handle, |_, window, cx| { composer.update(cx, |composer, cx| { composer.set_conversation("", window, cx); composer.add_paths(vec![path.clone()], window, cx); }); }) .unwrap(); cx.run_until_parked(); cx.update_window(handle, |_, window, cx| { composer.update(cx, |composer, cx| { if let Some(chat) = carry { composer.carry_images_into(chat.to_owned()); } composer.set_conversation(chosen, window, cx); }); }) .unwrap(); cx.update(|cx| assert_eq!(composer.read(cx).tray.items().len(), expected)); } } #[gpui_kit::test] fn each_conversation_keeps_its_own_draft(cx: &mut TestAppContext) { let (handle, composer) = demo_composer(cx); cx.update_window(handle, |_, window, cx| { composer.update(cx, |composer, cx| { composer.set_conversation("chat-a", window, cx); composer.set_text("**hello** there", window, cx); composer.set_conversation("chat-b", window, cx); }); }) .unwrap(); cx.update(|cx| { let composer = composer.read(cx); assert!(composer.draft.is_blank()); let stored = composer .app .read(cx) .store .draft("chat-a") .unwrap() .unwrap(); assert_eq!(stored.preview, "hello there"); }); cx.update_window(handle, |_, window, cx| { composer.update(cx, |composer, cx| { composer.set_text("other", window, cx); composer.set_conversation("chat-a", window, cx); }); }) .unwrap(); cx.update(|cx| { let composer = composer.read(cx); assert_eq!(composer.draft.text(), "hello there"); assert!(composer.draft.to_html().contains("")); assert!( composer .app .read(cx) .store .draft("chat-b") .unwrap() .is_some() ); }); } #[gpui_kit::test] fn a_post_draft_keeps_subject_and_banner_across_a_channel_switch(cx: &mut TestAppContext) { use std::cell::RefCell; use std::rc::Rc; use gpui_kit::component::input::InputState; use super::super::ComposerEvent; let (handle, composer) = demo_composer(cx); let restored = Rc::new(RefCell::new(Vec::new())); cx.update_window(handle, |_, window, cx| { let seen = restored.clone(); cx.subscribe(&composer, move |_, event: &ComposerEvent, _| { if let ComposerEvent::RestorePost(subject) = event { seen.borrow_mut().push(subject.clone()); } }) .detach(); let subject = cx.new(|cx| InputState::new(window, cx)); subject.update(cx, |input, cx| input.set_value("Code freeze", window, cx)); composer.update(cx, |composer, cx| { composer.set_conversation("channel-a", window, cx); composer.set_post_layout(Some(subject), cx); composer.toggle_announcement(window, cx); let headline = composer.announcement.as_ref().unwrap().headline.clone(); headline.update(cx, |input, cx| input.set_value("Release 43", window, cx)); composer.leave_conversation("channel-b", cx); composer.set_post_layout(None, cx); composer.set_conversation("channel-b", window, cx); composer.set_conversation("channel-a", window, cx); }); }) .unwrap(); cx.run_until_parked(); cx.update(|cx| { let composer = composer.read(cx); let draft = composer.announcement.as_ref().unwrap(); assert_eq!(draft.headline.read(cx).value().as_ref(), "Release 43"); assert_eq!(*restored.borrow(), vec![Some("Code freeze".to_owned())]); }); } #[gpui_kit::test] fn sending_clears_the_stored_draft_and_a_blank_composer_deletes_it( cx: &mut TestAppContext, ) { let (handle, composer) = demo_composer(cx); cx.update_window(handle, |_, window, cx| { composer.update(cx, |composer, cx| { composer.set_conversation("chat-a", window, cx); composer.set_text("hi", window, cx); composer.save_draft_now(cx); }); }) .unwrap(); cx.update(|cx| { assert!( composer .read(cx) .app .read(cx) .store .draft("chat-a") .unwrap() .is_some() ); }); cx.update_window(handle, |_, window, cx| { composer.update(cx, |composer, cx| composer.submit_current(window, cx)); }) .unwrap(); cx.update(|cx| { assert!( composer .read(cx) .app .read(cx) .store .draft("chat-a") .unwrap() .is_none() ); }); } #[gpui_kit::test] fn a_draft_target_change_drops_the_files_of_the_old_target_and_keeps_images( cx: &mut TestAppContext, ) { let (handle, composer) = demo_composer(cx); let directory = tempfile::tempdir().unwrap(); let png = small_png(directory.path()); let pdf = directory.path().join("plan.pdf"); std::fs::write(&pdf, b"%PDF-1.7").unwrap(); cx.update_window(handle, |_, window, cx| { composer.update(cx, |composer, cx| { composer.set_conversation("bob-chat", window, cx); composer.add_paths(vec![png, pdf], window, cx); }); }) .unwrap(); cx.run_until_parked(); cx.update(|cx| assert_eq!(composer.read(cx).tray.items().len(), 2)); cx.update_window(handle, |_, window, cx| { composer.update(cx, |composer, cx| { composer.carry_images_into(String::new()); composer.set_conversation("", window, cx); }); }) .unwrap(); cx.update(|cx| { let composer = composer.read(cx); assert_eq!(composer.tray.items().len(), 1); assert_eq!(composer.tray.items()[0].name, "dot.png"); assert!(!composer.files_allowed()); }); } } }