//! Clipboard text paste from keyboard shortcuts, right-click, or middle-click PRIMARY. //! The policy also reports why a configured mode is unavailable in this terminal. //! Existing selection handlers run first. Only a draw may deliver a read, so real input //! invalidates pending paste before completion and is never replaced by clipboard text. //! Submit and queue keys wait for a fresh press after a pending draft paste. use super::*; use crate::bottom_pane::ClipboardPasteTarget; use crate::clipboard_copy::worker::PasteSource; use crate::terminal_capabilities::PasteOwner; use crate::transcript_mode::TranscriptMode; use codex_config::types::RightClickPaste; use crossterm::event::MouseButton; use crossterm::event::MouseEvent; use crossterm::event::MouseEventKind; pub(super) struct PendingPaste { target: (Option, ClipboardPasteTarget), source: PasteSource, shortcut: Option, image_error: Option, pub(super) request: Arc<()>, } pub(crate) struct PasteEnvironment { pub(super) platform_default: bool, pub(super) ssh: bool, pub(super) wsl: bool, pub(super) owner: PasteOwner, } impl PasteEnvironment { pub(crate) fn detect() -> Self { Self { platform_default: cfg!(any(target_os = "windows", target_os = "linux")), ssh: crate::terminal_capabilities::is_ssh_session(), wsl: crate::terminal_capabilities::is_wsl_session(), owner: crate::terminal_capabilities::paste_owner(), } } pub(super) fn allows(&self, mode: RightClickPaste, transcript_mode: TranscriptMode) -> bool { self.status(mode, transcript_mode) .unwrap_or(/*default*/ false) } /// Return the effective state or why this terminal must handle paste itself. pub(crate) fn status( &self, mode: RightClickPaste, transcript_mode: TranscriptMode, ) -> Result { if mode == RightClickPaste::Off { return Ok(false); } if !transcript_mode.is_owned() { return Err("Unavailable in inline mode; use your terminal's paste command"); } if self.ssh { return Err("Unavailable over SSH; use your terminal's paste command"); } if let PasteOwner::Terminal(reason) = self.owner { return Err(reason); } if mode == RightClickPaste::Auto && self.wsl && self.owner == PasteOwner::Unknown { return Err( "Auto cannot identify this WSL terminal; use On or your terminal's paste command", ); } match mode { RightClickPaste::Off => Ok(false), RightClickPaste::On if cfg!(target_os = "android") => Err("Unavailable on Android"), RightClickPaste::On => Ok(true), RightClickPaste::Auto => Ok(self.platform_default), } } } impl App { pub(super) fn right_click_paste_target( &self, tui: &tui::Tui, source: PasteSource, shortcut: Option, ) -> Option<(Option, ClipboardPasteTarget)> { let allowed = shortcut.is_some() || match source { PasteSource::Clipboard => { !self.transcript_view.has_selection_range() && !self .chat_widget .rendered_selection .borrow() .view .has_selection_range() && self.right_click_paste_environment.allows( self.local_settings.tui.right_click_paste, self.local_settings.transcript_mode, ) } PasteSource::Primary => tui.primary_selection_available(), }; if (shortcut.is_none() && !tui.is_owned_screen()) || self.overlay.is_some() || self.transcript_view.is_search_editing() || !allowed { return None; } Some(( self.current_displayed_thread_id(), self.chat_widget.clipboard_paste_target(shortcut)?, )) } pub(super) fn start_right_click_paste( &mut self, tui: &mut tui::Tui, mouse: MouseEvent, context: ClipboardPasteContext, ) { let source = match mouse.kind { MouseEventKind::Down(MouseButton::Right) => PasteSource::Clipboard, MouseEventKind::Down(MouseButton::Middle) => PasteSource::Primary, _ => return, }; if !mouse.modifiers.is_empty() || self.pending_right_click_paste.is_some() { return; } // The restricted handoff does not run the ordinary mouse editor first. Settle any // held typing before taking the target of an eligible click. if context == ClipboardPasteContext::DraftHandoff && (self .right_click_paste_target(tui, source, /*shortcut*/ None) .is_none() || !self.chat_widget.prepare_composer_mouse(mouse)) { return; } self.start_clipboard_text_paste( tui, source, /*shortcut*/ None, /*image_error*/ None, ); } pub(crate) fn start_clipboard_text_paste( &mut self, tui: &mut tui::Tui, source: PasteSource, shortcut: Option, image_error: Option, ) { let Some(target) = self.right_click_paste_target(tui, source, shortcut) else { return; }; match tui.clipboard.read_text(source, tui.frame_requester()) { Ok(Some(request)) => { self.pending_right_click_paste = Some(PendingPaste { target, source, shortcut, image_error, request, }); tui.frame_requester().schedule_frame(); } Ok(None) => { if let Some(error) = image_error { self.chat_widget.add_error_message(error); } } Err(error) => self .chat_widget .add_error_message(image_error.unwrap_or(error)), } } /// Never submit unseen clipboard text or send the draft without it. pub(super) fn guard_pending_paste_submission( &mut self, tui: &tui::Tui, event: &TuiEvent, ) -> bool { let (Some(pending), TuiEvent::Key(key)) = (&self.pending_right_click_paste, event) else { return false; }; let composer = &self.keymap.composer; if !(composer.submit.is_pressed(*key) || composer.queue.is_pressed(*key)) || !matches!(pending.target.1, ClipboardPasteTarget::Draft(..)) || self .right_click_paste_target(tui, pending.source, pending.shortcut) .as_ref() != Some(&pending.target) { return false; } if key.kind == KeyEventKind::Press { self.chat_widget.show_paste_pending(); } true } /// Invalidate before polling, even when the worker has already completed. pub(super) fn invalidate_right_click_paste(&mut self, event: &TuiEvent) { let Some(pending) = &self.pending_right_click_paste else { return; }; let button = match pending.source { PasteSource::Clipboard => MouseButton::Right, PasteSource::Primary => MouseButton::Middle, }; let keep = match event { TuiEvent::Draw | TuiEvent::Resize(_) | TuiEvent::FocusGained => true, TuiEvent::Mouse(mouse) => { mouse.kind == MouseEventKind::Moved || (pending.shortcut.is_none() && (mouse.kind == MouseEventKind::Up(button) || (mouse.kind == MouseEventKind::Down(button) && mouse.modifiers.is_empty()))) } TuiEvent::Key(key) if key.kind == KeyEventKind::Release => true, TuiEvent::Key(key) => { pending.shortcut.is_some() && match key.code { KeyCode::Insert => key.modifiers == KeyModifiers::SHIFT, KeyCode::Char(c) if c.eq_ignore_ascii_case(&'v') => { key.modifiers == KeyModifiers::CONTROL } _ => false, } } TuiEvent::Paste(_) | TuiEvent::FocusLost | TuiEvent::Resume => false, }; if !keep { self.pending_right_click_paste = None; } } pub(super) fn finish_right_click_paste( &mut self, tui: &mut tui::Tui, event: TuiEvent, ) -> TuiEvent { if !matches!(event, TuiEvent::Draw) { return event; } let Some(pending) = self.pending_right_click_paste.as_ref() else { tui.clipboard.take_text_result(); return event; }; if self .right_click_paste_target(tui, pending.source, pending.shortcut) .as_ref() != Some(&pending.target) { self.pending_right_click_paste = None; return event; } let Some(result) = tui.clipboard.take_text_result() else { return event; }; let image_error = self .pending_right_click_paste .take() .and_then(|paste| paste.image_error); match result { Ok(text) if !text.is_empty() => { tui.frame_requester().schedule_frame(); TuiEvent::Paste(text) } result => { if let Some(error) = image_error.or_else(|| result.err()) { self.chat_widget.add_error_message(error); } event } } } } #[cfg(test)] #[path = "right_click_paste_tests.rs"] mod tests;