Files
codex/codex-rs/tui/src/app/clipboard.rs
T
Felipe Coury 76696132f0 Fix clipboard notices in the transcript overlay (#63996)
## Why

Copying a whole message can leave the overlay showing “Copying selection…”
because the copy has no active selection to receive the clipboard result.

## What changed

Track which clipboard request owns the notice so its completion can show
success or failure even without an active selection. Older results leave a
newer notice intact.

Track the last rendered transcript width for resize detection and render
prewrapped rows without hyperlinks using Ratatui’s `Paragraph`.

## Testing

Add a snapshot test that checks pending, failure, and success notices
without a selection, verifies that an older result leaves the notice
intact, and confirms the overlay stays open.

GitOrigin-RevId: c4076ccc425240ca39af9fd5c8cf6fca96303dda
2026-11-25 22:43:24 +00:00

128 lines
5.3 KiB
Rust

//! Dispatch clipboard key actions in order and apply completions to their selection owner.
use super::*;
use crate::chatwidget::KeyEventAction;
use crate::clipboard_copy::CopyFormat;
use crate::clipboard_copy::worker::PasteSource;
use crate::clipboard_paste::PasteImageError;
impl App {
pub(super) fn handle_clipboard_key_action(
&mut self,
tui: &mut tui::Tui,
action: KeyEventAction,
) {
match action {
KeyEventAction::None => {}
KeyEventAction::CopyLastResponse(text) => {
let result = tui
.clipboard
.copy(text, CopyFormat::Markdown, tui.frame_requester());
self.chat_widget.show_copy_result("last message", result);
}
KeyEventAction::PasteImage {
text_fallback,
context,
} => {
if text_fallback.is_some()
&& self
.right_click_paste_target(tui, PasteSource::Clipboard, text_fallback)
.is_none()
{
return;
}
// Finish paste before the next key, but never wait on the copy worker's lock.
if tui.clipboard.is_busy() || self.pending_right_click_paste.is_some() {
self.chat_widget
.add_info_message("Clipboard busy; try again".into(), /*hint*/ None);
} else {
match self.chat_widget.paste_image(context, text_fallback) {
Ok(()) => {}
Err(err) if text_fallback.is_some() && err.allows_text_fallback() => {
let image_error = matches!(err, PasteImageError::ImageReadFailed(_))
.then(|| format!("Failed to paste image: {err}"));
self.start_clipboard_text_paste(
tui,
PasteSource::Clipboard,
text_fallback,
image_error,
);
}
Err(err) => {
tracing::warn!("failed to paste image: {err}");
self.chat_widget
.add_error_message(format!("Failed to paste image: {err}"));
}
}
}
}
}
}
pub(super) fn finish_clipboard(&mut self, tui: &mut tui::Tui, event: &TuiEvent) {
let current = tui.is_owned_screen() && self.overlay.is_none();
if let Some(completion) = tui.clipboard.poll() {
if let Some(characters) = self.chat_widget.finish_clipboard(completion, current) {
self.transcript_view
.show_copy_feedback(&completion.1, characters);
}
{
let mut selection = self.chat_widget.rendered_selection.borrow_mut();
let crate::rendered_selection::RenderedSelection { view, cells, .. } =
&mut *selection;
view.finish_copy(cells, completion, current);
}
let passive = self
.conversation_frame
.side_pane()
.map(|panes| panes.passive_thread);
for (id, companion) in &mut self.side_companions {
let visible = current && passive == Some(*id);
if companion
.view
.finish_copy(&companion.cells, completion, visible)
== Some(true)
{
companion.view.jump_to_latest();
}
}
let parent = self.sidebar_parent();
let panel_visible = current && self.conversation_frame.panel_area().is_some();
for (owner, pane) in &mut self.sidebars {
pane.finish_document_copy(completion, panel_visible && parent == *owner);
}
let follow =
self.transcript_view
.finish_copy(&self.transcript_cells, completion, current);
let pending_exit_key = if current {
self.backtrack
.finish_pending_select_exit_key(completion.0, /*select_mode_closed*/ true)
} else {
None
};
if follow == Some(true) {
if self.backtrack.overlay_preview_active {
self.close_transcript_overlay(tui);
}
self.transcript_view.finish_select_copy();
self.backtrack.select_exit_key = pending_exit_key;
}
}
if !current || matches!(event, TuiEvent::FocusLost | TuiEvent::Resume) {
self.transcript_view.cancel_primary();
for companion in self.side_companions.values_mut() {
companion.view.cancel_primary();
}
for pane in self.sidebars.values_mut() {
pane.cancel_hidden_document_primary(/*visible*/ false);
}
self.chat_widget.cancel_composer_primary();
self.chat_widget
.rendered_selection
.borrow_mut()
.view
.cancel_primary();
}
}
}