From 323a5cb7e70cc9ae815dc43edb4d2292437483b3 Mon Sep 17 00:00:00 2001 From: Jeremy Rose <172423086+nornagon-openai@users.noreply.github.com> Date: Fri, 5 Sep 2025 07:02:11 -0700 Subject: [PATCH] refactor: remove AttachImage tui event (#3191) TuiEvent is supposed to be purely events that come from the "driver", i.e. events from the terminal. Everything app-specific should be an AppEvent. In this case, it didn't need to be an event at all. --- codex-rs/tui/src/app.rs | 9 --------- codex-rs/tui/src/chatwidget.rs | 12 ++++++++++++ codex-rs/tui/src/tui.rs | 34 ---------------------------------- 3 files changed, 12 insertions(+), 43 deletions(-) diff --git a/codex-rs/tui/src/app.rs b/codex-rs/tui/src/app.rs index 40a7f208..12cd789c 100644 --- a/codex-rs/tui/src/app.rs +++ b/codex-rs/tui/src/app.rs @@ -183,15 +183,6 @@ impl App { }, )?; } - TuiEvent::AttachImage { - path, - width, - height, - format_label, - } => { - self.chat_widget - .attach_image(path, width, height, format_label); - } } } Ok(true) diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rs index 7acb802c..65ecbf74 100644 --- a/codex-rs/tui/src/chatwidget.rs +++ b/codex-rs/tui/src/chatwidget.rs @@ -57,6 +57,7 @@ use crate::bottom_pane::CancellationEvent; use crate::bottom_pane::InputResult; use crate::bottom_pane::SelectionAction; use crate::bottom_pane::SelectionItem; +use crate::clipboard_paste::paste_image_to_temp_png; use crate::get_git_diff::get_git_diff; use crate::history_cell; use crate::history_cell::CommandOutput; @@ -745,6 +746,17 @@ impl ChatWidget { self.on_ctrl_c(); return; } + KeyEvent { + code: KeyCode::Char('v'), + modifiers: KeyModifiers::CONTROL, + kind: KeyEventKind::Press, + .. + } => { + if let Ok((path, info)) = paste_image_to_temp_png() { + self.attach_image(path, info.width, info.height, info.encoded_format.label()); + } + return; + } other if other.kind == KeyEventKind::Press => { self.bottom_pane.clear_ctrl_c_quit_hint(); } diff --git a/codex-rs/tui/src/tui.rs b/codex-rs/tui/src/tui.rs index df957fee..26c18866 100644 --- a/codex-rs/tui/src/tui.rs +++ b/codex-rs/tui/src/tui.rs @@ -1,7 +1,6 @@ use std::io::Result; use std::io::Stdout; use std::io::stdout; -use std::path::PathBuf; use std::pin::Pin; use std::sync::Arc; use std::sync::atomic::AtomicBool; @@ -20,10 +19,7 @@ use crossterm::cursor::MoveTo; use crossterm::event::DisableBracketedPaste; use crossterm::event::EnableBracketedPaste; use crossterm::event::Event; -use crossterm::event::KeyCode; use crossterm::event::KeyEvent; -use crossterm::event::KeyEventKind; -use crossterm::event::KeyModifiers; use crossterm::event::KeyboardEnhancementFlags; use crossterm::event::PopKeyboardEnhancementFlags; use crossterm::event::PushKeyboardEnhancementFlags; @@ -38,7 +34,6 @@ use ratatui::crossterm::terminal::enable_raw_mode; use ratatui::layout::Offset; use ratatui::text::Line; -use crate::clipboard_paste::paste_image_to_temp_png; use crate::custom_terminal; use crate::custom_terminal::Terminal as CustomTerminal; use tokio::select; @@ -154,12 +149,6 @@ pub enum TuiEvent { Key(KeyEvent), Paste(String), Draw, - AttachImage { - path: PathBuf, - width: u32, - height: u32, - format_label: &'static str, - }, } pub struct Tui { @@ -305,29 +294,6 @@ impl Tui { select! { Some(Ok(event)) = crossterm_events.next() => { match event { - // Detect Ctrl+V to attach an image from the clipboard. - Event::Key(key_event @ KeyEvent { - code: KeyCode::Char('v'), - modifiers: KeyModifiers::CONTROL, - kind: KeyEventKind::Press, - .. - }) => { - match paste_image_to_temp_png() { - Ok((path, info)) => { - yield TuiEvent::AttachImage { - path, - width: info.width, - height: info.height, - format_label: info.encoded_format.label(), - }; - } - Err(_) => { - // Fall back to normal key handling if no image is available. - yield TuiEvent::Key(key_event); - } - } - } - crossterm::event::Event::Key(key_event) => { #[cfg(unix)] if matches!(