Skip to content

Commit f299ad7

Browse files
committed
Support Command-V image paste shortcut
1 parent ebe75bb commit f299ad7

3 files changed

Lines changed: 52 additions & 8 deletions

File tree

‎codex-rs/tui/src/chatwidget.rs‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,21 @@ fn queued_message_edit_hint_binding(
251251
.or_else(|| bindings.first().copied())
252252
}
253253

254+
fn is_paste_image_key_event(key_event: &KeyEvent) -> bool {
255+
if key_event.kind != KeyEventKind::Press {
256+
return false;
257+
}
258+
259+
let KeyCode::Char(c) = key_event.code else {
260+
return false;
261+
};
262+
263+
// Some macOS terminals forward Command as SUPER when enhanced keyboard reporting is active.
264+
let image_paste_modifiers =
265+
KeyModifiers::CONTROL | KeyModifiers::ALT | KeyModifiers::SUPER;
266+
c.eq_ignore_ascii_case(&'v') && key_event.modifiers.intersects(image_paste_modifiers)
267+
}
268+
254269
use crate::app_event::AppEvent;
255270
use crate::app_event::ConnectorsSnapshot;
256271
use crate::app_event::ExitMode;
@@ -5173,14 +5188,7 @@ impl ChatWidget {
51735188
self.quit_shortcut_expires_at = None;
51745189
self.quit_shortcut_key = None;
51755190
}
5176-
KeyEvent {
5177-
code: KeyCode::Char(c),
5178-
modifiers,
5179-
kind: KeyEventKind::Press,
5180-
..
5181-
} if modifiers.intersects(KeyModifiers::CONTROL | KeyModifiers::ALT)
5182-
&& c.eq_ignore_ascii_case(&'v') =>
5183-
{
5191+
key_event if is_paste_image_key_event(&key_event) => {
51845192
match paste_image_to_temp_png() {
51855193
Ok((path, info)) => {
51865194
tracing::debug!(

‎codex-rs/tui/src/chatwidget/tests.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ mod guardian;
231231
mod helpers;
232232
mod history_replay;
233233
mod mcp_startup;
234+
mod paste_shortcuts;
234235
mod permissions;
235236
mod plan_mode;
236237
mod popups_and_settings;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use super::*;
2+
3+
#[test]
4+
fn paste_image_shortcut_accepts_control_alt_and_command_v() {
5+
assert!(is_paste_image_key_event(&KeyEvent::new(
6+
KeyCode::Char('v'),
7+
KeyModifiers::CONTROL,
8+
)));
9+
assert!(is_paste_image_key_event(&KeyEvent::new(
10+
KeyCode::Char('v'),
11+
KeyModifiers::CONTROL | KeyModifiers::ALT,
12+
)));
13+
assert!(is_paste_image_key_event(&KeyEvent::new(
14+
KeyCode::Char('v'),
15+
KeyModifiers::SUPER,
16+
)));
17+
}
18+
19+
#[test]
20+
fn paste_image_shortcut_ignores_plain_v_and_non_press_events() {
21+
assert!(!is_paste_image_key_event(&KeyEvent::new(
22+
KeyCode::Char('v'),
23+
KeyModifiers::NONE,
24+
)));
25+
assert!(!is_paste_image_key_event(&KeyEvent::new(
26+
KeyCode::Char('x'),
27+
KeyModifiers::SUPER,
28+
)));
29+
30+
let release = KeyEvent {
31+
kind: crossterm::event::KeyEventKind::Release,
32+
..KeyEvent::new(KeyCode::Char('v'), KeyModifiers::SUPER)
33+
};
34+
assert!(!is_paste_image_key_event(&release));
35+
}

0 commit comments

Comments
 (0)