mirror of
https://github.com/openai/codex.git
synced 2026-11-21 22:50:37 +00:00
Fix clipboard text fallback and guard pending pastes in the TUI (#63022)
## Why On X11, a clipboard owner can answer an image request with text, causing the image read to fail even though text is available. Submitting or queueing a draft while its clipboard read is pending can also send it without the intended paste. ## What changed - Enable `Ctrl+V` text fallback on local Linux outside SSH and WSL, and allow `Shift+Insert` text fallback across platforms. - Try text after eligible Linux image-read failures. Report the original image error if the fallback yields no text. - Keep pending pastes alive across key releases. While a paste targets the current draft, intercept submit and queue shortcuts and show a reminder to review the draft before sending. ## Testing Extend clipboard worker coverage to exercise submit during an active read and queue after a read completes but before its text reaches the draft. Snapshot the pending-paste reminder. GitOrigin-RevId: 21bec5e405e13631a2a72f440482338611307ff3
This commit is contained in:
@@ -956,6 +956,9 @@ impl App {
|
||||
return Ok(AppRunControl::Continue);
|
||||
}
|
||||
self.refresh_link_hover(tui)?;
|
||||
if self.guard_pending_paste_submission(tui, &event) {
|
||||
return Ok(AppRunControl::Continue);
|
||||
}
|
||||
self.invalidate_right_click_paste(&event);
|
||||
self.finish_clipboard(tui, &event);
|
||||
// Clipboard reads started by the composer already have a validated target.
|
||||
|
||||
@@ -38,11 +38,14 @@ impl App {
|
||||
} else {
|
||||
match self.chat_widget.paste_image(context, text_fallback) {
|
||||
Ok(()) => {}
|
||||
Err(PasteImageError::NoImage(_)) if text_fallback.is_some() => {
|
||||
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) => {
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
//! Clipboard text paste from Windows shortcuts, right-click, or X11 middle-click PRIMARY.
|
||||
//! Clipboard text paste from keyboard shortcuts, right-click, or X11 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.
|
||||
//! Releasing or pressing another paste shortcut does not cancel its pending paste.
|
||||
//! 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::transcript_mode::TranscriptMode;
|
||||
use crate::tui::VscodeDetection;
|
||||
@@ -14,9 +15,10 @@ use crossterm::event::MouseEvent;
|
||||
use crossterm::event::MouseEventKind;
|
||||
|
||||
pub(super) struct PendingPaste {
|
||||
target: (Option<ThreadId>, crate::bottom_pane::ClipboardPasteTarget),
|
||||
target: (Option<ThreadId>, ClipboardPasteTarget),
|
||||
source: PasteSource,
|
||||
shortcut: Option<KeyCode>,
|
||||
image_error: Option<String>,
|
||||
pub(super) request: Arc<()>,
|
||||
}
|
||||
|
||||
@@ -82,7 +84,7 @@ impl App {
|
||||
tui: &tui::Tui,
|
||||
source: PasteSource,
|
||||
shortcut: Option<KeyCode>,
|
||||
) -> Option<(Option<ThreadId>, crate::bottom_pane::ClipboardPasteTarget)> {
|
||||
) -> Option<(Option<ThreadId>, ClipboardPasteTarget)> {
|
||||
let allowed = shortcut.is_some()
|
||||
|| match source {
|
||||
PasteSource::Clipboard => {
|
||||
@@ -137,7 +139,9 @@ impl App {
|
||||
{
|
||||
return;
|
||||
}
|
||||
self.start_clipboard_text_paste(tui, source, /*shortcut*/ None);
|
||||
self.start_clipboard_text_paste(
|
||||
tui, source, /*shortcut*/ None, /*image_error*/ None,
|
||||
);
|
||||
}
|
||||
|
||||
pub(crate) fn start_clipboard_text_paste(
|
||||
@@ -145,6 +149,7 @@ impl App {
|
||||
tui: &mut tui::Tui,
|
||||
source: PasteSource,
|
||||
shortcut: Option<KeyCode>,
|
||||
image_error: Option<String>,
|
||||
) {
|
||||
let Some(target) = self.right_click_paste_target(tui, source, shortcut) else {
|
||||
return;
|
||||
@@ -155,15 +160,47 @@ impl App {
|
||||
target,
|
||||
source,
|
||||
shortcut,
|
||||
image_error,
|
||||
request,
|
||||
});
|
||||
tui.frame_requester().schedule_frame();
|
||||
}
|
||||
Ok(None) => {}
|
||||
Err(error) => self.chat_widget.add_error_message(error),
|
||||
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 {
|
||||
@@ -182,16 +219,13 @@ impl App {
|
||||
|| (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.kind == KeyEventKind::Release
|
||||
|| key.modifiers == KeyModifiers::SHIFT
|
||||
}
|
||||
KeyCode::Insert => key.modifiers == KeyModifiers::SHIFT,
|
||||
KeyCode::Char(c) if c.eq_ignore_ascii_case(&'v') => {
|
||||
key.kind == KeyEventKind::Release
|
||||
|| key.modifiers == KeyModifiers::CONTROL
|
||||
key.modifiers == KeyModifiers::CONTROL
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
@@ -226,15 +260,19 @@ impl App {
|
||||
let Some(result) = tui.clipboard.take_text_result() else {
|
||||
return event;
|
||||
};
|
||||
self.pending_right_click_paste = None;
|
||||
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)
|
||||
}
|
||||
Ok(_) => event,
|
||||
Err(error) => {
|
||||
self.chat_widget.add_error_message(error);
|
||||
result => {
|
||||
if let Some(error) = image_error.or_else(|| result.err()) {
|
||||
self.chat_widget.add_error_message(error);
|
||||
}
|
||||
event
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,6 +93,7 @@ async fn pending_primary_paste_survives_release_but_not_other_input() {
|
||||
),
|
||||
source: PasteSource::Primary,
|
||||
shortcut: None,
|
||||
image_error: None,
|
||||
request: Arc::new(()),
|
||||
});
|
||||
app.invalidate_right_click_paste(&TuiEvent::Mouse(MouseEvent {
|
||||
|
||||
@@ -44,8 +44,11 @@ impl ChatWidget {
|
||||
if suppressed {
|
||||
return Some(KeyEventAction::None);
|
||||
}
|
||||
let text_fallback = (cfg!(windows)
|
||||
&& (key.modifiers == KeyModifiers::CONTROL || shift_insert))
|
||||
let local_linux = cfg!(target_os = "linux")
|
||||
&& !crate::clipboard_copy::is_ssh_session()
|
||||
&& !crate::clipboard_copy::is_wsl_session();
|
||||
let text_fallback = (shift_insert
|
||||
|| (key.modifiers == KeyModifiers::CONTROL && (cfg!(windows) || local_linux)))
|
||||
.then_some(key.code);
|
||||
if text_fallback.is_some() && context == ClipboardPasteContext::Composer {
|
||||
self.bottom_pane.prepare_composer_clipboard_paste();
|
||||
@@ -65,7 +68,9 @@ impl ChatWidget {
|
||||
self.suppress_image_paste_until = Instant::now() + IMAGE_PASTE_REPEAT_WINDOW;
|
||||
if context == ClipboardPasteContext::DraftHandoff
|
||||
&& text_fallback.is_some()
|
||||
&& matches!(&result, Err(PasteImageError::NoImage(_)))
|
||||
&& result
|
||||
.as_ref()
|
||||
.is_err_and(PasteImageError::allows_text_fallback)
|
||||
{
|
||||
// Settle typing before capturing the asynchronous text target.
|
||||
self.bottom_pane.prepare_composer_clipboard_paste();
|
||||
@@ -81,6 +86,15 @@ impl ChatWidget {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn show_paste_pending(&mut self) {
|
||||
self.bottom_pane.show_footer_flash(
|
||||
"Paste pending. Review the draft before sending."
|
||||
.dim()
|
||||
.into(),
|
||||
Duration::from_secs(/*secs*/ 3),
|
||||
);
|
||||
}
|
||||
|
||||
pub(super) fn show_clipboard_flash(&mut self, result: &CopyResult) {
|
||||
let line = match result {
|
||||
Ok(status) => Line::from(status.message("selection").dim()),
|
||||
|
||||
@@ -2592,6 +2592,10 @@ async fn slash_keymap_invalid_args_show_usage() {
|
||||
#[tokio::test]
|
||||
async fn copy_shortcut_can_be_remapped() {
|
||||
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
|
||||
let ctrl_v_pastes_text = cfg!(windows)
|
||||
|| (cfg!(target_os = "linux")
|
||||
&& !crate::clipboard_copy::is_ssh_session()
|
||||
&& !crate::clipboard_copy::is_wsl_session());
|
||||
for (code, modifiers) in [
|
||||
(KeyCode::Char('v'), KeyModifiers::CONTROL),
|
||||
(KeyCode::Char('V'), KeyModifiers::CONTROL),
|
||||
@@ -2605,7 +2609,7 @@ async fn copy_shortcut_can_be_remapped() {
|
||||
assert_matches!(
|
||||
chat.handle_key_event(paste),
|
||||
crate::chatwidget::KeyEventAction::PasteImage { text_fallback, .. }
|
||||
if text_fallback == (cfg!(windows) && modifiers == KeyModifiers::CONTROL)
|
||||
if text_fallback == (ctrl_v_pastes_text && modifiers == KeyModifiers::CONTROL)
|
||||
.then_some(code)
|
||||
);
|
||||
chat.handle_key_event(KeyEvent {
|
||||
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
---
|
||||
source: tui/src/clipboard_copy/worker_tests.rs
|
||||
expression: "crate::chatwidget::tests::helpers::normalize_snapshot_paths(crate::chatwidget::tests::helpers::render_bottom_popup(&app.chat_widget, 80))"
|
||||
---
|
||||
› draft
|
||||
|
||||
GPT-5.6-Sol default · /tmp/project
|
||||
Paste pending. Review the draft before sending.
|
||||
@@ -33,7 +33,12 @@ async fn cloud_config_completion_does_not_wait_for_the_clipboard() {
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
app.start_clipboard_text_paste(&mut tui, PasteSource::Clipboard, Some(KeyCode::Char('v')));
|
||||
app.start_clipboard_text_paste(
|
||||
&mut tui,
|
||||
PasteSource::Clipboard,
|
||||
Some(KeyCode::Char('v')),
|
||||
/*image_error*/ None,
|
||||
);
|
||||
assert!(tui.clipboard.is_busy());
|
||||
|
||||
tokio::time::timeout(
|
||||
@@ -369,6 +374,8 @@ async fn right_click_paste_uses_normal_input_and_discards_stale_reads() {
|
||||
(None, "disable", "draft "),
|
||||
(Some(ctrl_v), "none", "draft café\nsecond line\n"),
|
||||
(Some(shift_insert), "none", "draft café\nsecond line\n"),
|
||||
(Some(ctrl_v), "submit", "draft café\nsecond line\n"),
|
||||
(Some(ctrl_v), "queue", "draft café\nsecond line\n"),
|
||||
(Some(ctrl_v), "native", "draft native"),
|
||||
(Some(shift_insert), "edit", "replacement"),
|
||||
(Some(ctrl_v), "click", "draft "),
|
||||
@@ -384,10 +391,15 @@ async fn right_click_paste_uses_normal_input_and_discards_stale_reads() {
|
||||
};
|
||||
app.chat_widget.apply_external_edit("draft ".into());
|
||||
// Cover a read still running, and a completed read awaiting its Draw.
|
||||
let read_completed = shortcut == Some(ctrl_v) && change == "none";
|
||||
let read_completed = shortcut == Some(ctrl_v) && matches!(change, "none" | "queue");
|
||||
if shortcut.is_some() {
|
||||
// Enter at the NoImage fallback, independently of the host clipboard.
|
||||
app.start_clipboard_text_paste(&mut tui, PasteSource::Clipboard, shortcut);
|
||||
app.start_clipboard_text_paste(
|
||||
&mut tui,
|
||||
PasteSource::Clipboard,
|
||||
shortcut,
|
||||
/*image_error*/ None,
|
||||
);
|
||||
} else {
|
||||
app.handle_tui_event(&mut tui, &mut server, TuiEvent::Mouse(click))
|
||||
.await
|
||||
@@ -449,7 +461,41 @@ async fn right_click_paste_uses_normal_input_and_discards_stale_reads() {
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
if change == "native" {
|
||||
if matches!(change, "submit" | "queue") {
|
||||
let code = if change == "submit" {
|
||||
KeyCode::Enter
|
||||
} else {
|
||||
KeyCode::Tab
|
||||
};
|
||||
for kind in [
|
||||
KeyEventKind::Press,
|
||||
KeyEventKind::Repeat,
|
||||
KeyEventKind::Release,
|
||||
] {
|
||||
app.handle_tui_event(
|
||||
&mut tui,
|
||||
&mut server,
|
||||
TuiEvent::Key(KeyEvent {
|
||||
kind,
|
||||
..code.into()
|
||||
}),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
assert_eq!(app.chat_widget.composer_text_with_pending(), "draft ");
|
||||
if change == "submit" {
|
||||
insta::assert_snapshot!(
|
||||
"pending_clipboard_submit",
|
||||
crate::chatwidget::tests::helpers::normalize_snapshot_paths(
|
||||
crate::chatwidget::tests::helpers::render_bottom_popup(
|
||||
&app.chat_widget,
|
||||
/*width*/ 80
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
} else if change == "native" {
|
||||
app.handle_tui_event(&mut tui, &mut server, TuiEvent::Paste("native".into()))
|
||||
.await
|
||||
.unwrap();
|
||||
@@ -578,7 +624,12 @@ async fn cloud_handoff_keeps_an_owned_clipboard_read_until_completion_or_input()
|
||||
let (events_tx, events_rx) = tokio::sync::mpsc::unbounded_channel();
|
||||
if shortcut.is_some() {
|
||||
// Start at the Windows NoImage fallback without accessing the host clipboard.
|
||||
app.start_clipboard_text_paste(&mut tui, PasteSource::Clipboard, shortcut);
|
||||
app.start_clipboard_text_paste(
|
||||
&mut tui,
|
||||
PasteSource::Clipboard,
|
||||
shortcut,
|
||||
/*image_error*/ None,
|
||||
);
|
||||
} else {
|
||||
events_tx
|
||||
.send(TuiEvent::Key(KeyCode::Char('x').into()))
|
||||
|
||||
@@ -31,15 +31,23 @@ pub(crate) fn persist_pasted_image(
|
||||
pub enum PasteImageError {
|
||||
ClipboardUnavailable(String),
|
||||
NoImage(String),
|
||||
ImageReadFailed(String),
|
||||
EncodeFailed(String),
|
||||
IoError(String),
|
||||
}
|
||||
|
||||
impl PasteImageError {
|
||||
pub(crate) fn allows_text_fallback(&self) -> bool {
|
||||
matches!(self, Self::NoImage(_) | Self::ImageReadFailed(_))
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for PasteImageError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
PasteImageError::ClipboardUnavailable(msg) => write!(f, "clipboard unavailable: {msg}"),
|
||||
PasteImageError::NoImage(msg) => write!(f, "no image on clipboard: {msg}"),
|
||||
PasteImageError::ImageReadFailed(msg) => write!(f, "could not read image: {msg}"),
|
||||
PasteImageError::EncodeFailed(msg) => write!(f, "could not encode image: {msg}"),
|
||||
PasteImageError::IoError(msg) => write!(f, "io error: {msg}"),
|
||||
}
|
||||
@@ -119,7 +127,18 @@ fn read_clipboard_image(
|
||||
|
||||
let _span = tracing::debug_span!("get_image").entered();
|
||||
let bitmap = clipboard.get_image().map_err(|e| {
|
||||
if cfg!(windows) && !matches!(e, arboard::Error::ContentNotAvailable) {
|
||||
// X11 owners such as xclip can answer a PNG request with a text type,
|
||||
// which arboard reports as Unknown. A malformed PNG can also coexist with text.
|
||||
if cfg!(target_os = "linux")
|
||||
&& matches!(
|
||||
e,
|
||||
arboard::Error::Unknown { .. } | arboard::Error::ConversionFailure
|
||||
)
|
||||
{
|
||||
PasteImageError::ImageReadFailed(e.to_string())
|
||||
} else if cfg!(any(windows, target_os = "linux"))
|
||||
&& !matches!(e, arboard::Error::ContentNotAvailable)
|
||||
{
|
||||
PasteImageError::ClipboardUnavailable(e.to_string())
|
||||
} else {
|
||||
PasteImageError::NoImage(e.to_string())
|
||||
@@ -177,7 +196,12 @@ fn try_wsl_clipboard_fallback(
|
||||
use PasteImageError::ClipboardUnavailable;
|
||||
use PasteImageError::NoImage;
|
||||
|
||||
if !is_probably_wsl() || !matches!(error, ClipboardUnavailable(_) | NoImage(_)) {
|
||||
if !is_probably_wsl()
|
||||
|| !matches!(
|
||||
error,
|
||||
ClipboardUnavailable(_) | NoImage(_) | PasteImageError::ImageReadFailed(_)
|
||||
)
|
||||
{
|
||||
return Err(error.clone());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user