From e6a7862259f202b94bee752e717eb5ea0ca63ff3 Mon Sep 17 00:00:00 2001 From: Felipe Coury Date: Thu, 3 Dec 2026 00:01:05 +0000 Subject: [PATCH] Fix clipboard image paste in WSL terminals (#65362) ## Why VS Code on Windows can interpret `Ctrl+Alt+V` as AltGr input before Codex receives it. The Windows clipboard fallback also needs to work when WSL does not import the Windows `PATH`. ## What changed - Show `Alt+V` in the shortcut guide for WSL sessions in VS Code or when terminal detection is inconclusive. Let active bindings and chord prefixes take precedence over `Alt+V` and hide the hint when it conflicts in the composer. - Read Windows clipboard images through WinForms, which supports both Windows PowerShell 5.1 and PowerShell 7. Try their standard Windows installation paths when lookup on `PATH` fails, within the same paste deadline. ## Testing Cover shortcut selection, hint visibility, and `Alt+V` dispatch, including submitting or queueing a draft when `composer.queue` is bound to `Alt+V`. GitOrigin-RevId: ad59ce6c5de65600aa01028f0d173c42a752beae --- codex-rs/tui/src/bottom_pane/chat_composer.rs | 7 ++- .../bottom_pane/chat_composer/footer_state.rs | 16 +----- codex-rs/tui/src/bottom_pane/footer.rs | 24 +-------- .../tui/src/bottom_pane/shortcut_overlay.rs | 49 +++++++++-------- .../src/bottom_pane/shortcut_overlay_tests.rs | 43 +++++++++++++-- ...ests__shortcut_overlay_customized_wsl.snap | 2 +- codex-rs/tui/src/chatwidget/clipboard.rs | 6 ++- .../chatwidget/tests/composer_submission.rs | 35 ++++++++++++ codex-rs/tui/src/clipboard_paste.rs | 54 +++++++++++++++++-- codex-rs/tui/src/keymap.rs | 15 ++++++ codex-rs/tui/src/terminal_capabilities.rs | 20 +++++++ .../tui/src/terminal_capabilities_tests.rs | 17 ++++++ 12 files changed, 216 insertions(+), 72 deletions(-) diff --git a/codex-rs/tui/src/bottom_pane/chat_composer.rs b/codex-rs/tui/src/bottom_pane/chat_composer.rs index 450e9576c2..2af301654a 100644 --- a/codex-rs/tui/src/bottom_pane/chat_composer.rs +++ b/codex-rs/tui/src/bottom_pane/chat_composer.rs @@ -839,6 +839,7 @@ impl ChatComposer { .primary_hint(KeymapContext::Composer, "toggle_shortcuts"), shortcut_guide_keys: super::shortcut_overlay::ShortcutGuideKeys::from_keymap( &default_keymap, + crate::terminal_capabilities::image_paste_shortcut(), ), history_search_key: default_keymap .primary_hint(KeymapContext::Composer, "history_search_previous"), @@ -1154,8 +1155,10 @@ impl ChatComposer { self.footer.queue_key = keymap.primary_hint(KeymapContext::Composer, "queue"); self.footer.toggle_shortcuts_key = keymap.primary_hint(KeymapContext::Composer, "toggle_shortcuts"); - self.footer.shortcut_guide_keys = - super::shortcut_overlay::ShortcutGuideKeys::from_keymap(keymap); + self.footer.shortcut_guide_keys = super::shortcut_overlay::ShortcutGuideKeys::from_keymap( + keymap, + crate::terminal_capabilities::image_paste_shortcut(), + ); self.footer.history_search_key = keymap.primary_hint(KeymapContext::Composer, "history_search_previous"); self.footer.reasoning_down_key = diff --git a/codex-rs/tui/src/bottom_pane/chat_composer/footer_state.rs b/codex-rs/tui/src/bottom_pane/chat_composer/footer_state.rs index bfc3cdde52..67e21c974f 100644 --- a/codex-rs/tui/src/bottom_pane/chat_composer/footer_state.rs +++ b/codex-rs/tui/src/bottom_pane/chat_composer/footer_state.rs @@ -102,27 +102,13 @@ pub(crate) struct ComposerRenderOptions<'a> { impl super::ChatComposer { pub(super) fn footer_props(&self) -> FooterProps { - let mode = self.footer_mode(); - let is_wsl = { - #[cfg(target_os = "linux")] - { - matches!(mode, FooterMode::ShortcutOverlay(_)) - && crate::clipboard_paste::is_probably_wsl() - } - #[cfg(not(target_os = "linux"))] - { - false - } - }; - FooterProps { - mode, + mode: self.footer_mode(), esc_backtrack_hint: self.footer.esc_backtrack_hint, is_task_running: self.is_task_running, queue_submissions: self.queue_submissions, quit_shortcut_key: self.footer.quit_shortcut_key, collaboration_modes_enabled: self.collaboration_modes_enabled, - is_wsl, status_line_value: self.footer.status_line_value.clone(), status_line_enabled: self.footer.status_line_enabled, key_hints: FooterKeyHints { diff --git a/codex-rs/tui/src/bottom_pane/footer.rs b/codex-rs/tui/src/bottom_pane/footer.rs index 25365c0f61..d1db7c4e7d 100644 --- a/codex-rs/tui/src/bottom_pane/footer.rs +++ b/codex-rs/tui/src/bottom_pane/footer.rs @@ -57,7 +57,6 @@ pub(crate) struct FooterProps { pub(crate) is_task_running: bool, pub(crate) queue_submissions: bool, pub(crate) collaboration_modes_enabled: bool, - pub(crate) is_wsl: bool, /// Which key the user must press again to quit. /// /// This is rendered when `mode` is `FooterMode::QuitShortcutReminder`. @@ -116,6 +115,7 @@ impl FooterKeyHints { toggle_shortcuts: Some(key_hint::plain(KeyCode::Char('?')).into()), shortcut_guide: shortcut_overlay::ShortcutGuideKeys::from_keymap( &crate::keymap::RuntimeKeymap::defaults(), + key_hint::ctrl(KeyCode::Char('v')), ), queue: Some(key_hint::plain(KeyCode::Tab).into()), insert_newline: Some(key_hint::ctrl(KeyCode::Char('j')).into()), @@ -1283,7 +1283,6 @@ mod tests { is_task_running: false, queue_submissions: false, collaboration_modes_enabled: false, - is_wsl: false, quit_shortcut_key: key_hint::ctrl(KeyCode::Char('c')), status_line_value: None, status_line_enabled: false, @@ -1300,7 +1299,6 @@ mod tests { is_task_running: false, queue_submissions: false, collaboration_modes_enabled: false, - is_wsl: false, quit_shortcut_key: key_hint::ctrl(KeyCode::Char('c')), status_line_value: None, status_line_enabled: false, @@ -1320,7 +1318,6 @@ mod tests { is_task_running: false, queue_submissions: false, collaboration_modes_enabled: true, - is_wsl: false, quit_shortcut_key: key_hint::ctrl(KeyCode::Char('c')), status_line_value: None, status_line_enabled: false, @@ -1337,7 +1334,6 @@ mod tests { is_task_running: true, queue_submissions: false, collaboration_modes_enabled: false, - is_wsl: false, quit_shortcut_key: key_hint::ctrl(KeyCode::Char('c')), status_line_value: None, status_line_enabled: false, @@ -1354,7 +1350,6 @@ mod tests { is_task_running: false, queue_submissions: false, collaboration_modes_enabled: false, - is_wsl: false, quit_shortcut_key: key_hint::ctrl(KeyCode::Char('c')), status_line_value: None, status_line_enabled: false, @@ -1371,7 +1366,6 @@ mod tests { is_task_running: false, queue_submissions: false, collaboration_modes_enabled: false, - is_wsl: false, quit_shortcut_key: key_hint::ctrl(KeyCode::Char('c')), status_line_value: None, status_line_enabled: false, @@ -1388,7 +1382,6 @@ mod tests { is_task_running: false, queue_submissions: false, collaboration_modes_enabled: false, - is_wsl: false, quit_shortcut_key: key_hint::ctrl(KeyCode::Char('c')), status_line_value: None, status_line_enabled: false, @@ -1405,7 +1398,6 @@ mod tests { is_task_running: true, queue_submissions: false, collaboration_modes_enabled: false, - is_wsl: false, quit_shortcut_key: key_hint::ctrl(KeyCode::Char('c')), status_line_value: None, status_line_enabled: false, @@ -1424,7 +1416,6 @@ mod tests { is_task_running: false, queue_submissions: false, collaboration_modes_enabled: false, - is_wsl: false, quit_shortcut_key: key_hint::ctrl(KeyCode::Char('c')), status_line_value: None, status_line_enabled: false, @@ -1443,7 +1434,6 @@ mod tests { is_task_running: true, queue_submissions: false, collaboration_modes_enabled: false, - is_wsl: false, quit_shortcut_key: key_hint::ctrl(KeyCode::Char('c')), status_line_value: None, status_line_enabled: false, @@ -1458,7 +1448,6 @@ mod tests { is_task_running: false, queue_submissions: false, collaboration_modes_enabled: true, - is_wsl: false, quit_shortcut_key: key_hint::ctrl(KeyCode::Char('c')), status_line_value: None, status_line_enabled: false, @@ -1486,7 +1475,6 @@ mod tests { is_task_running: true, queue_submissions: false, collaboration_modes_enabled: true, - is_wsl: false, quit_shortcut_key: key_hint::ctrl(KeyCode::Char('c')), status_line_value: None, status_line_enabled: false, @@ -1507,7 +1495,6 @@ mod tests { is_task_running: false, queue_submissions: false, collaboration_modes_enabled: false, - is_wsl: false, quit_shortcut_key: key_hint::ctrl(KeyCode::Char('c')), status_line_value: Some(Line::from("Status line content".to_string()).into()), status_line_enabled: true, @@ -1523,7 +1510,6 @@ mod tests { is_task_running: true, queue_submissions: false, collaboration_modes_enabled: false, - is_wsl: false, quit_shortcut_key: key_hint::ctrl(KeyCode::Char('c')), status_line_value: Some(Line::from("Status line content".to_string()).into()), status_line_enabled: true, @@ -1539,7 +1525,6 @@ mod tests { is_task_running: false, queue_submissions: false, collaboration_modes_enabled: false, - is_wsl: false, quit_shortcut_key: key_hint::ctrl(KeyCode::Char('c')), status_line_value: Some(Line::from("Status line content".to_string()).into()), status_line_enabled: true, @@ -1555,7 +1540,6 @@ mod tests { is_task_running: false, queue_submissions: false, collaboration_modes_enabled: true, - is_wsl: false, quit_shortcut_key: key_hint::ctrl(KeyCode::Char('c')), status_line_value: None, // command timed out / empty status_line_enabled: true, @@ -1585,7 +1569,6 @@ mod tests { is_task_running: false, queue_submissions: false, collaboration_modes_enabled: true, - is_wsl: false, quit_shortcut_key: key_hint::ctrl(KeyCode::Char('c')), status_line_value: None, status_line_enabled: false, @@ -1607,7 +1590,6 @@ mod tests { is_task_running: false, queue_submissions: false, collaboration_modes_enabled: false, - is_wsl: false, quit_shortcut_key: key_hint::ctrl(KeyCode::Char('c')), status_line_value: None, status_line_enabled: true, @@ -1630,7 +1612,6 @@ mod tests { is_task_running: false, queue_submissions: false, collaboration_modes_enabled: true, - is_wsl: false, quit_shortcut_key: key_hint::ctrl(KeyCode::Char('c')), status_line_value: Some( Line::from( @@ -1658,7 +1639,6 @@ mod tests { is_task_running: false, queue_submissions: false, collaboration_modes_enabled: false, - is_wsl: false, quit_shortcut_key: key_hint::ctrl(KeyCode::Char('c')), status_line_value: None, status_line_enabled: false, @@ -1674,7 +1654,6 @@ mod tests { is_task_running: false, queue_submissions: false, collaboration_modes_enabled: false, - is_wsl: false, quit_shortcut_key: key_hint::ctrl(KeyCode::Char('c')), status_line_value: Some(Line::from("Status line content".to_string()).into()), status_line_enabled: true, @@ -1693,7 +1672,6 @@ mod tests { is_task_running: false, queue_submissions: false, collaboration_modes_enabled: true, - is_wsl: false, quit_shortcut_key: key_hint::ctrl(KeyCode::Char('c')), status_line_value: Some(Line::from( "Status line content that is definitely too long to fit alongside the mode label" diff --git a/codex-rs/tui/src/bottom_pane/shortcut_overlay.rs b/codex-rs/tui/src/bottom_pane/shortcut_overlay.rs index 16a9adac53..fd02bc3c47 100644 --- a/codex-rs/tui/src/bottom_pane/shortcut_overlay.rs +++ b/codex-rs/tui/src/bottom_pane/shortcut_overlay.rs @@ -11,6 +11,7 @@ use crate::key_hint; use crate::key_hint::KeyBinding; use crate::key_hint::ShortcutHint; use crate::keymap::KeymapContext; +use crate::keymap::KeymapContextSet; use crate::keymap::RuntimeKeymap; use crate::keymap::bindings_for_action; use crate::multi_agents::next_agent_shortcut; @@ -34,36 +35,43 @@ use ratatui::widgets::Paragraph; use ratatui::widgets::Widget; use std::collections::HashMap; -/// The view switch takes the first free key. Layer browsing uses the next one. +/// Guide shortcuts selected from keys available in the current composer. #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub(crate) struct ShortcutGuideKeys { view: Option, layer: Option, + paste_image: Option, } impl ShortcutGuideKeys { - pub(super) fn from_keymap(keymap: &RuntimeKeymap) -> Self { + pub(super) fn from_keymap(keymap: &RuntimeKeymap, paste_image: KeyBinding) -> Self { let conflicts = |context: KeymapContext| { context == KeymapContext::Pager || context.overlaps(KeymapContext::Composer) }; - let mut free = - std::iter::once(KeyCode::Tab) - .chain((1..=12).map(KeyCode::F)) - .filter(|&code| { - let key = key_hint::plain(code); - // The guide opens on an empty draft, so queue has nothing to send. - !crate::keymap::keymap_action_ids().any(|id| { - conflicts(id.context) - && (id.context != KeymapContext::Composer || id.action != "queue") - && bindings_for_action(keymap, id.context.config_name(), id.action) - .is_some_and(|bindings| bindings.contains(&key)) - }) && !keymap.chords.bindings.iter().any(|binding| { - conflicts(binding.action.context) && binding.chord.prefix == key - }) - }); + let is_free = |key| { + // The guide opens on an empty draft, so queue has nothing to send. + !crate::keymap::keymap_action_ids().any(|id| { + conflicts(id.context) + && (id.context != KeymapContext::Composer || id.action != "queue") + && bindings_for_action(keymap, id.context.config_name(), id.action) + .is_some_and(|bindings| bindings.contains(&key)) + }) && !keymap + .chords + .bindings + .iter() + .any(|binding| conflicts(binding.action.context) && binding.chord.prefix == key) + }; + let mut free = std::iter::once(KeyCode::Tab) + .chain((1..=12).map(KeyCode::F)) + .filter(|&code| is_free(key_hint::plain(code))); + let (code, modifiers) = paste_image.parts(); + let composer = KeymapContextSet::new(KeymapContext::Composer).with(KeymapContext::Editor); Self { view: free.next(), layer: free.next(), + paste_image: keymap + .allows_image_paste(KeyEvent::new(code, modifiers), composer) + .then_some(paste_image.into()), } } } @@ -184,11 +192,6 @@ impl Entry { fn bindings(props: &FooterProps) -> [(&'static str, Vec); 3] { use KeyCode as K; let hints = props.key_hints; - let paste = if props.is_wsl { - key_hint::ctrl_alt(K::Char('v')) - } else { - key_hint::ctrl(K::Char('v')) - }; let edit_previous = match hints.edit_previous { Some(ShortcutHint::Single(key)) if !props.esc_backtrack_hint => Some(ShortcutHint::Chord { prefix: key, @@ -201,7 +204,7 @@ fn bindings(props: &FooterProps) -> [(&'static str, Vec); 3] { Entry::key(key_hint::plain(K::Char('@')), "Mention files"), Entry::key(key_hint::plain(K::Char('!')), "Shell command"), Entry::hint(hints.insert_newline, "New line"), - Entry::key(paste, "Paste image"), + Entry::hint(hints.shortcut_guide.paste_image, "Paste image"), Entry::hint(hints.external_editor, "External editor"), Entry::hint(hints.history_search, "Search history"), Entry::hint(edit_previous, "Edit last message"), diff --git a/codex-rs/tui/src/bottom_pane/shortcut_overlay_tests.rs b/codex-rs/tui/src/bottom_pane/shortcut_overlay_tests.rs index e9cda46d91..957812e0ff 100644 --- a/codex-rs/tui/src/bottom_pane/shortcut_overlay_tests.rs +++ b/codex-rs/tui/src/bottom_pane/shortcut_overlay_tests.rs @@ -25,7 +25,6 @@ fn props() -> FooterProps { is_task_running: true, queue_submissions: false, collaboration_modes_enabled: true, - is_wsl: false, quit_shortcut_key: key_hint::ctrl(KeyCode::Char('c')), status_line_value: None, status_line_enabled: false, @@ -77,7 +76,10 @@ fn snapshot(name: &str, width: u16, height: Option, props: &FooterProps) { fn shortcut_overlay_uses_runtime_bindings_and_wsl_paste() { let mut props = props(); props.is_task_running = false; - props.is_wsl = true; + let paste = key_hint::alt(KeyCode::Char('v')); + let config = toml::from_str(r#"pager.mark_markdown = "alt-v""#).unwrap(); + let keymap = crate::keymap::RuntimeKeymap::from_config(&config).unwrap(); + props.key_hints.shortcut_guide = ShortcutGuideKeys::from_keymap(&keymap, paste); props.esc_backtrack_hint = true; props.key_hints.external_editor = Some(ShortcutHint::Chord { prefix: key_hint::ctrl(KeyCode::Char('x')), @@ -106,6 +108,40 @@ fn shortcut_overlay_uses_runtime_bindings_and_wsl_paste() { /*height*/ None, &props, ); + props.mode = FooterMode::ShortcutOverlay(ShortcutView::List); + for queue in ["alt-v", "alt-v q"] { + let config = toml::from_str(&format!(r#"composer.queue = "{queue}""#)).unwrap(); + let keymap = crate::keymap::RuntimeKeymap::from_config(&config).unwrap(); + let hint = keymap + .primary_hint(crate::keymap::KeymapContext::Composer, "queue") + .unwrap(); + props.key_hints.queue = Some(hint); + props.key_hints.shortcut_guide = ShortcutGuideKeys::from_keymap(&keymap, paste); + let guide = ratatui::text::Text::from(lines(&props, /*width*/ 80)).to_string(); + assert!( + guide + .lines() + .any(|row| row.contains(&hint.display_label()) && row.contains("Send message")) + ); + assert!(!guide.contains("Paste image")); + } + + let config = toml::from_str( + r#" + global.copy = "alt-v" + pager.mark_markdown = "ctrl-v" + "#, + ) + .unwrap(); + let keymap = crate::keymap::RuntimeKeymap::from_config(&config).unwrap(); + props.key_hints.shortcut_guide = ShortcutGuideKeys::from_keymap(&keymap, paste); + let guide = ratatui::text::Text::from(lines(&props, /*width*/ 80)).to_string(); + assert!(!guide.contains("Paste image")); + let ctrl_v = key_hint::ctrl(KeyCode::Char('v')); + assert_eq!( + ShortcutGuideKeys::from_keymap(&keymap, ctrl_v).paste_image, + Some(ctrl_v.into()) + ); } #[test] @@ -120,7 +156,8 @@ fn shortcut_overlay_leaves_remapped_tab_and_function_keys_available() { .unwrap(); let keymap = crate::keymap::RuntimeKeymap::from_config(&config).unwrap(); let mut props = props(); - props.key_hints.shortcut_guide = ShortcutGuideKeys::from_keymap(&keymap); + props.key_hints.shortcut_guide = + ShortcutGuideKeys::from_keymap(&keymap, key_hint::ctrl(KeyCode::Char('v'))); let mut view = ShortcutView::List; let keys = props.key_hints.shortcut_guide; diff --git a/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__shortcut_overlay__tests__shortcut_overlay_customized_wsl.snap b/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__shortcut_overlay__tests__shortcut_overlay_customized_wsl.snap index 166b105993..0ff6848634 100644 --- a/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__shortcut_overlay__tests__shortcut_overlay_customized_wsl.snap +++ b/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__shortcut_overlay__tests__shortcut_overlay_customized_wsl.snap @@ -9,7 +9,7 @@ expression: terminal.backend() " @ Mention files ⇧tab Change mode " " ! Shell command ⌥. More reasoning " " ⇧enter New line f8 Voice " -" ⌃⌥v Paste image ← Agents (empty prompt) " +" ⌥v Paste image ← Agents (empty prompt) " " ⌃x ⌃e External editor ⌥← / ⌥→ Subagents (empty prompt) " " esc Edit last message ⌃5 Inspect activity " " ⌃c Quit " diff --git a/codex-rs/tui/src/chatwidget/clipboard.rs b/codex-rs/tui/src/chatwidget/clipboard.rs index 5cf5778bf0..7fcc6bce9a 100644 --- a/codex-rs/tui/src/chatwidget/clipboard.rs +++ b/codex-rs/tui/src/chatwidget/clipboard.rs @@ -114,7 +114,11 @@ impl ChatWidget { && ((matches!(key.code, KeyCode::Char(c) if c.eq_ignore_ascii_case(&'v')) && key .modifiers - .intersects(KeyModifiers::CONTROL | KeyModifiers::ALT)) + .intersects(KeyModifiers::CONTROL | KeyModifiers::ALT) + && self + .bottom_pane + .keymap() + .allows_image_paste(key, self.keymap_contexts())) || shift_insert); if !image_paste { if matches!(key.kind, KeyEventKind::Press | KeyEventKind::Release) { diff --git a/codex-rs/tui/src/chatwidget/tests/composer_submission.rs b/codex-rs/tui/src/chatwidget/tests/composer_submission.rs index 873ed18168..3eb93bb5d0 100644 --- a/codex-rs/tui/src/chatwidget/tests/composer_submission.rs +++ b/codex-rs/tui/src/chatwidget/tests/composer_submission.rs @@ -123,6 +123,41 @@ async fn reconnect_queues_enter_and_resumes_ordinary_next_turn_submission() { assert_eq!(sequence, ["prompt", "submit"]); } +#[tokio::test] +async fn alt_v_pastes_with_a_pager_remap_but_sends_with_a_queue_remap() { + let (mut chat, _events, mut operations) = make_chatwidget_manual(/*model_override*/ None).await; + chat.thread_id = Some(ThreadId::new()); + let alt_v = KeyEvent::new(KeyCode::Char('v'), KeyModifiers::ALT); + let config = toml::from_str(r#"pager.mark_markdown = "alt-v""#).unwrap(); + let keymap = RuntimeKeymap::from_config(&config).unwrap(); + chat.apply_keymap_update(config, &keymap); + chat.apply_external_edit("draft".into()); + assert_matches!( + chat.handle_key_event(alt_v), + KeyEventAction::PasteImage { .. } + ); + + let config = toml::from_str(r#"composer.queue = "alt-v""#).unwrap(); + let keymap = RuntimeKeymap::from_config(&config).unwrap(); + chat.apply_keymap_update(config, &keymap); + assert_matches!(chat.handle_key_event(alt_v), KeyEventAction::None); + let Op::UserTurn { items, .. } = next_submit_op(&mut operations) else { + panic!("expected the existing draft to be submitted"); + }; + assert_eq!( + items, + vec![UserInput::Text { + text: "draft".into(), + text_elements: Vec::new(), + }] + ); + + chat.on_task_started(); + chat.apply_external_edit("next".into()); + assert_matches!(chat.handle_key_event(alt_v), KeyEventAction::None); + assert_eq!(chat.queued_user_message_texts(), ["next"]); +} + #[tokio::test] async fn composer_submission_sends_once_and_requests_latest() { let (mut chat, mut events, mut operations) = diff --git a/codex-rs/tui/src/clipboard_paste.rs b/codex-rs/tui/src/clipboard_paste.rs index 821abe0872..ae8b9284e3 100644 --- a/codex-rs/tui/src/clipboard_paste.rs +++ b/codex-rs/tui/src/clipboard_paste.rs @@ -253,14 +253,60 @@ fn try_wsl_clipboard_fallback( } tracing::debug!("attempting Windows PowerShell clipboard fallback"); + // WinForms can read bitmaps in both Windows PowerShell 5.1 and PowerShell 7. // Return bytes so no Windows temp files are needed, even for a rejected paste. - let script = r#"$img = Get-Clipboard -Format Image; if ($null -eq $img) { exit 1 }; $png = New-Object System.IO.MemoryStream; $img.Save($png, [System.Drawing.Imaging.ImageFormat]::Png); if ($png.Length -gt 1GB) { exit 1 }; $png.WriteTo([Console]::OpenStandardOutput())"#; + let script = r#" +$ErrorActionPreference = 'Stop' +Add-Type -AssemblyName System.Windows.Forms +Add-Type -AssemblyName System.Drawing +$img = [System.Windows.Forms.Clipboard]::GetImage() +if ($null -eq $img) { exit 1 } +$png = New-Object System.IO.MemoryStream +$img.Save($png, [System.Drawing.Imaging.ImageFormat]::Png) +if ($png.Length -gt 1GB) { exit 1 } +$png.WriteTo([Console]::OpenStandardOutput()) +"#; + let runtime = tokio::runtime::Builder::new_current_thread() .enable_all() .build()?; - for cmd in ["powershell.exe", "pwsh", "powershell"] { - let mut command = crate::command_output::wsl_command(Path::new(cmd)); - command.args(["-NoProfile", "-Command", script]); + let on_path = ["powershell.exe", "pwsh", "powershell", "pwsh.exe"] + .into_iter() + .map(|name| crate::command_output::wsl_command(Path::new(name))); + // With Windows PATH import disabled, resolve the installed engines in the child + // so even drive translation and executable lookup share the paste deadline. + let installed = [ + "Windows/System32/WindowsPowerShell/v1.0/powershell.exe", + "Program Files/PowerShell/7/pwsh.exe", + ] + .into_iter() + .map(|location| { + let mut command = crate::command_output::wsl_command(Path::new("/bin/sh")); + command.args([ + "-c", + r#" +if [ -x /bin/wslpath ]; then + drive=$(/bin/wslpath -u 'C:\') || exit 1 +else + /usr/bin/mountpoint -q /mnt/c || exit 1 + drive=/mnt/c +fi +case "$drive" in /*) ;; *) exit 1 ;; esac +exe="${drive%/}/$1" +shift +[ "$exe" -ef "$(command -v "${exe##*/}")" ] && exit 1 +exec "$exe" "$@" +"#, + "codex-wsl-image", + location, + ]); + command + }); + for mut command in on_path.chain(installed) { + // Avoid workspace-relative assembly loads and excessively long WSL working directories. + command + .current_dir("/") + .args(["-NoProfile", "-Command", script]); let Some(output) = runtime.block_on(wsl_image::read_command( command, deadline, diff --git a/codex-rs/tui/src/keymap.rs b/codex-rs/tui/src/keymap.rs index a2c180b235..05b7cdc957 100644 --- a/codex-rs/tui/src/keymap.rs +++ b/codex-rs/tui/src/keymap.rs @@ -713,6 +713,21 @@ impl RuntimeKeymap { } } + /// Ctrl paste is reserved. Alt paste yields to any active binding or chord prefix. + pub(crate) fn allows_image_paste(&self, event: KeyEvent, contexts: KeymapContextSet) -> bool { + if event.modifiers.contains(KeyModifiers::CONTROL) { + return true; + } + let contexts = contexts + .with(KeymapContext::Global) + .with(KeymapContext::Chat); + !self.has_single_binding(event, contexts) + && !self.chords.bindings.iter().any(|binding| { + contexts.contains_action(binding.action) + && chord_stroke_matches(binding.chord.prefix, event) + }) + } + pub(crate) fn has_single_binding(&self, event: KeyEvent, contexts: KeymapContextSet) -> bool { runtime_action_bindings(self) .any(|action| contexts.contains_action(action.id) && action.bindings.is_pressed(event)) diff --git a/codex-rs/tui/src/terminal_capabilities.rs b/codex-rs/tui/src/terminal_capabilities.rs index d6c6feaa92..4b3c895fbd 100644 --- a/codex-rs/tui/src/terminal_capabilities.rs +++ b/codex-rs/tui/src/terminal_capabilities.rs @@ -236,6 +236,26 @@ impl ClipboardCapabilities { } } +/// Prefer Alt+V if Windows-hosted VS Code might consume Ctrl+Alt+V as AltGr. +pub(crate) fn image_paste_shortcut() -> crate::key_hint::KeyBinding { + image_paste_shortcut_for(is_wsl_session().then(keyboard_modes::detect_vscode_terminal)) +} + +fn image_paste_shortcut_for( + wsl_host: Option, +) -> crate::key_hint::KeyBinding { + use crate::key_hint; + use crossterm::event::KeyCode; + use keyboard_modes::VscodeDetection; + + let key = KeyCode::Char('v'); + match wsl_host { + None => key_hint::ctrl(key), + Some(VscodeDetection::Other) => key_hint::ctrl_alt(key), + Some(VscodeDetection::VsCode | VscodeDetection::Unknown) => key_hint::alt(key), + } +} + pub(crate) fn forwards_copy_shortcut() -> bool { forwards_copy_shortcut_for(&terminal_info()) } diff --git a/codex-rs/tui/src/terminal_capabilities_tests.rs b/codex-rs/tui/src/terminal_capabilities_tests.rs index 7f5406692d..ad62cae076 100644 --- a/codex-rs/tui/src/terminal_capabilities_tests.rs +++ b/codex-rs/tui/src/terminal_capabilities_tests.rs @@ -33,3 +33,20 @@ fn live_palette_requires_a_supported_direct_local_terminal() { ); } } + +#[test] +fn wsl_image_hint_avoids_altgr_when_the_terminal_probe_is_inconclusive() { + use super::keyboard_modes::VscodeDetection; + use crate::key_hint; + use crossterm::event::KeyCode; + + let key = KeyCode::Char('v'); + for (host, hint) in [ + (None, key_hint::ctrl(key)), + (Some(VscodeDetection::Other), key_hint::ctrl_alt(key)), + (Some(VscodeDetection::VsCode), key_hint::alt(key)), + (Some(VscodeDetection::Unknown), key_hint::alt(key)), + ] { + assert_eq!(super::image_paste_shortcut_for(host), hint, "{host:?}"); + } +}