diff --git a/codex-rs/tui/src/app.rs b/codex-rs/tui/src/app.rs index fa3895be0e..f945bd778c 100644 --- a/codex-rs/tui/src/app.rs +++ b/codex-rs/tui/src/app.rs @@ -1417,7 +1417,7 @@ impl App { tui.frame_requester().schedule_frame(); return Ok(AppRunControl::Continue); } - if self.overlay.is_some() { + if self.overlay_owns_input() { let _ = self .handle_backtrack_overlay_event(tui, app_server, event) .await?; diff --git a/codex-rs/tui/src/app/clipboard.rs b/codex-rs/tui/src/app/clipboard.rs index 62718c3fe6..911257de21 100644 --- a/codex-rs/tui/src/app/clipboard.rs +++ b/codex-rs/tui/src/app/clipboard.rs @@ -1,4 +1,4 @@ -//! Dispatch clipboard key actions in order and apply completions to their selection owner. +//! Dispatch widget key actions in order and apply clipboard completions to their selection owner. use super::*; use crate::chatwidget::KeyEventAction; @@ -7,11 +7,7 @@ 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, - ) { + pub(super) fn handle_widget_key_action(&mut self, tui: &mut tui::Tui, action: KeyEventAction) { match action { KeyEventAction::None => {} KeyEventAction::CopyLastResponse(text) => { diff --git a/codex-rs/tui/src/app/cloud_switch_wait.rs b/codex-rs/tui/src/app/cloud_switch_wait.rs index c18920b026..d8f5ec0c3a 100644 --- a/codex-rs/tui/src/app/cloud_switch_wait.rs +++ b/codex-rs/tui/src/app/cloud_switch_wait.rs @@ -96,7 +96,7 @@ fn handle_cloud_switch_event( let action = app .chat_widget .handle_restricted_key(key, RestrictedInputMode::DraftHandoff); - app.handle_clipboard_key_action(tui, action); + app.handle_widget_key_action(tui, action); } } show_cloud_switch_hint(app, progress); diff --git a/codex-rs/tui/src/app/input.rs b/codex-rs/tui/src/app/input.rs index f52c35b12c..5502bb8c86 100644 --- a/codex-rs/tui/src/app/input.rs +++ b/codex-rs/tui/src/app/input.rs @@ -13,6 +13,10 @@ use crate::keymap::keymap_action_ids; mod select_mode; impl App { + pub(super) fn overlay_owns_input(&self) -> bool { + self.overlay.is_some() + } + pub(super) fn should_recover_vim_insert_escape( &self, tui: &tui::Tui, @@ -39,7 +43,7 @@ impl App { // bindings and either stroke of a chord win; inactive bindings do not consume input. cfg!(unix) && !self.enhanced_keys_supported - && self.overlay.is_none() + && !self.overlay_owns_input() && !self.transcript_view.is_search_editing() && insert_escape && matches!(key_event.code, KeyCode::Char(_)) @@ -811,7 +815,7 @@ impl App { let action = self .chat_widget .handle_restricted_key(key_event, RestrictedInputMode::DraftHandoff); - self.handle_clipboard_key_action(tui, action); + self.handle_widget_key_action(tui, action); return; } @@ -839,7 +843,7 @@ impl App { let action = self .chat_widget .handle_key_event_with_activation_key(key_event, activation_key); - self.handle_clipboard_key_action(tui, action); + self.handle_widget_key_action(tui, action); } return; } @@ -876,7 +880,7 @@ impl App { let action = self .chat_widget .handle_key_event_with_activation_key(key_event, activation_key); - self.handle_clipboard_key_action(tui, action); + self.handle_widget_key_action(tui, action); } _ => { self.chat_widget diff --git a/codex-rs/tui/src/app/owned_transcript.rs b/codex-rs/tui/src/app/owned_transcript.rs index 088625a0f0..88453e3d13 100644 --- a/codex-rs/tui/src/app/owned_transcript.rs +++ b/codex-rs/tui/src/app/owned_transcript.rs @@ -272,7 +272,7 @@ impl App { changed || search_changed } - pub(super) fn render_owned_transcript( + pub(crate) fn render_owned_transcript( &mut self, tui: &mut tui::Tui, screen_size: Size, diff --git a/codex-rs/tui/src/app/right_click_paste.rs b/codex-rs/tui/src/app/right_click_paste.rs index 237324a21c..1324dbed7d 100644 --- a/codex-rs/tui/src/app/right_click_paste.rs +++ b/codex-rs/tui/src/app/right_click_paste.rs @@ -101,7 +101,7 @@ impl App { PasteSource::Primary => tui.primary_selection_available(), }; if (shortcut.is_none() && !tui.is_owned_screen()) - || self.overlay.is_some() + || self.overlay_owns_input() || self.transcript_view.is_search_editing() || !allowed { diff --git a/codex-rs/tui/src/app/side.rs b/codex-rs/tui/src/app/side.rs index aa97106ffb..2c94cce117 100644 --- a/codex-rs/tui/src/app/side.rs +++ b/codex-rs/tui/src/app/side.rs @@ -453,7 +453,7 @@ impl App { app_server: &mut AppServerSession, key: KeyEvent, ) -> bool { - if self.overlay.is_none() + if !self.overlay_owns_input() && self.chat_widget.no_modal_or_popup_active() && self.chat_widget.composer_is_empty() { diff --git a/codex-rs/tui/src/app_backtrack.rs b/codex-rs/tui/src/app_backtrack.rs index c99db697ef..b46101687e 100644 --- a/codex-rs/tui/src/app_backtrack.rs +++ b/codex-rs/tui/src/app_backtrack.rs @@ -206,21 +206,7 @@ impl App { return; } let _ = tui.enter_alt_screen(); - self.overlay = Some( - Overlay::new_transcript( - self.transcript_cells.clone(), - self.keymap.pager.clone(), - self.local_settings.copy_on_select(), - self.local_settings - .tui - .mouse_scroll_speed - .unwrap_or(/*default*/ 1.0), - tui.terminal.bidi_layout, - ) - .with_copy_on_select_clear_selection( - self.local_settings.tui.copy_on_select_clear_selection, - ), - ); + self.overlay = Some(self.new_transcript_reader(tui, self.transcript_cells.clone())); if self.scrollback_has_older_history && let Some(Overlay::Transcript(overlay)) = self.overlay.as_mut() { @@ -229,6 +215,24 @@ impl App { tui.frame_requester().schedule_frame(); } + pub(crate) fn new_transcript_reader( + &self, + tui: &tui::Tui, + cells: Vec>, + ) -> Overlay { + Overlay::new_transcript( + cells, + self.keymap.pager.clone(), + self.local_settings.copy_on_select(), + self.local_settings + .tui + .mouse_scroll_speed + .unwrap_or(/*default*/ 1.0), + tui.terminal.bidi_layout, + ) + .with_copy_on_select_clear_selection(self.local_settings.tui.copy_on_select_clear_selection) + } + /// Close the current overlay and restore normal UI, retaining Analytics navigation state. pub(crate) fn close_transcript_overlay(&mut self, tui: &mut tui::Tui) { if tui.is_owned_screen() && self.overlay.is_none() { @@ -448,13 +452,13 @@ impl App { } }); // Refresh visible rows before selection captures this live revision. - let result = tui.with_shortcut_hud(hud, |tui| { - if input { t.draw(tui) } else { Ok(()) }.and_then(|()| t.handle_event(tui, event)) - }); + let completed_copy = t.prepare_event(tui, &event); + let result = tui.with_shortcut_hud(hud, |tui| t.draw(tui)); if result.is_err() { let _ = tui.set_mouse_capture(tui::MouseCapture::Inherit); } - let completed_copy = result?; + result?; + t.handle_input(tui, event); let thread_link = t.pending_thread_link.take(); let close_overlay = t.is_done() || thread_link.is_some(); let select_mode_closed = was_select_mode && (!t.is_select_mode() || close_overlay); diff --git a/codex-rs/tui/src/chatwidget/input_restore.rs b/codex-rs/tui/src/chatwidget/input_restore.rs index 0d0cdc4481..3547396a81 100644 --- a/codex-rs/tui/src/chatwidget/input_restore.rs +++ b/codex-rs/tui/src/chatwidget/input_restore.rs @@ -27,7 +27,7 @@ impl ChatWidget { } pub(crate) fn composer_draft_snapshot(&mut self) -> ComposerDraftSnapshot { - self.bottom_pane.flush_pending_composer_input(); + self.flush_pending_composer_input(); let mut draft = self.bottom_pane.composer_draft_snapshot(); // This snapshot comes from an active host, where disabled shell commands // also mean a leading bang was entered literally. diff --git a/codex-rs/tui/src/chatwidget/interaction.rs b/codex-rs/tui/src/chatwidget/interaction.rs index 7ab30af010..30e6b4ffdd 100644 --- a/codex-rs/tui/src/chatwidget/interaction.rs +++ b/codex-rs/tui/src/chatwidget/interaction.rs @@ -697,6 +697,10 @@ impl ChatWidget { self.bottom_pane.is_in_paste_burst() } + pub(crate) fn flush_pending_composer_input(&mut self) { + self.bottom_pane.flush_pending_composer_input(); + } + // Returns true if caller should skip rendering this frame (a future frame is scheduled). pub(crate) fn handle_paste_burst_tick(&mut self, frame_requester: FrameRequester) -> bool { let flushed = self.bottom_pane.flush_paste_burst_if_due(); diff --git a/codex-rs/tui/src/pager_overlay/transcript.rs b/codex-rs/tui/src/pager_overlay/transcript.rs index 50d68dcfb3..acb21574ce 100644 --- a/codex-rs/tui/src/pager_overlay/transcript.rs +++ b/codex-rs/tui/src/pager_overlay/transcript.rs @@ -4,6 +4,7 @@ //! scrolling, wrapped layouts, selection, search and the live tail. use super::*; +use crate::custom_terminal::Frame; use crate::history_cell::HistoryRenderMode; use crate::history_cell::is_session_history_prefix_cell; use crate::keymap::RuntimeKeymap; @@ -91,14 +92,17 @@ impl TranscriptOverlay { && !self.pending_select_mode && !self.view.has_active_interaction() && self.view.history != TranscriptHistoryState::Failed; - let chrome_height = if browsing { 2 } else { 5 }; - let content_height = area.height.saturating_sub(chrome_height); - self.content_area = Rect::new( - area.x, - area.y.saturating_add(/*rhs*/ 1), - area.width, - content_height, - ); + let mut body = area; + { + let header = Rect::new(area.x, area.y, area.width, area.height.min(/*other*/ 1)); + Span::from("/ ".repeat(area.width as usize / 2)) + .dim() + .render(header, buf); + "/ T R A N S C R I P T".dim().render(header, buf); + body.y = area.y.saturating_add(/*rhs*/ 1); + body.height = area.height.saturating_sub(if browsing { 2 } else { 5 }); + } + self.content_area = body; self.view.render(self.content_area, buf, &self.cells); if self.pending_select_mode { self.pending_select_mode = false; @@ -112,11 +116,6 @@ impl TranscriptOverlay { self.view.ensure_entry_visible(&self.cells, index); self.view.render(self.content_area, buf, &self.cells); } - let header = Rect::new(area.x, area.y, area.width, area.height.min(/*other*/ 1)); - Span::from("/ ".repeat(area.width as usize / 2)) - .dim() - .render(header, buf); - "/ T R A N S C R I P T".dim().render(header, buf); let status = Rect::new( area.x, self.content_area.bottom(), @@ -174,20 +173,29 @@ impl TranscriptOverlay { } } - pub(crate) fn draw(&mut self, tui: &mut tui::Tui) -> Result<()> { - let mut scanning = false; - tui.draw(u16::MAX, |frame| { - self.view.prepare_width(frame.area().width); - scanning = self.view.advance_search(&self.cells); - self.render(frame.area(), frame.buffer); - if let Some(cursor) = self.cursor { - frame.set_cursor_position(cursor); - } - })?; + /// Paint a reader into its host's frame. Return the delay for the next animation or search tick. + pub(crate) fn paint( + &mut self, + frame: &mut Frame<'_>, + area: Rect, + ) -> Option { + self.view.prepare_width(area.width); + let scanning = self.view.advance_search(&self.cells); + self.render(area, frame.buffer); + if let Some(cursor) = self.cursor { + frame.set_cursor_position(cursor); + } let loading = self.view.is_loading_history() && self.motion == MotionMode::Animated; - if scanning || loading { - tui.frame_requester() - .schedule_frame_in(crate::tui::TARGET_FRAME_INTERVAL); + (scanning || loading).then_some(crate::tui::TARGET_FRAME_INTERVAL) + } + + pub(crate) fn draw(&mut self, tui: &mut tui::Tui) -> Result<()> { + let mut delay = None; + tui.draw(u16::MAX, |frame| { + delay = self.paint(frame, frame.area()); + })?; + if let Some(delay) = delay { + tui.frame_requester().schedule_frame_in(delay); } Ok(()) } @@ -197,6 +205,22 @@ impl TranscriptOverlay { tui: &mut tui::Tui, event: TuiEvent, ) -> Result> { + let completed_copy = self.prepare_event(tui, &event); + // Apply a queued prompt jump before navigation can move away from it. + if matches!( + event, + TuiEvent::Draw | TuiEvent::Resize(_) | TuiEvent::FocusGained | TuiEvent::Resume + ) || ((self.pending_select_mode || self.pending_highlight.is_some()) + && (matches!(&event, TuiEvent::Key(key) if key.kind != KeyEventKind::Release) + || matches!(&event, TuiEvent::Mouse(mouse) if mouse.kind != MouseEventKind::Moved))) + { + self.draw(tui)?; + } + self.handle_input(tui, event); + Ok(completed_copy) + } + + pub(crate) fn prepare_event(&mut self, tui: &mut tui::Tui, event: &TuiEvent) -> Option { let completed_copy = if let Some(completion) = tui.clipboard.poll() { let id = completion.0; self.finish_pending_copy(completion); @@ -208,13 +232,14 @@ impl TranscriptOverlay { self.view.cancel_primary(); self.view.end_drag(); } - // Apply a queued prompt jump before navigation can move away from it. - if (self.pending_select_mode || self.pending_highlight.is_some()) - && (matches!(&event, TuiEvent::Key(key) if key.kind != KeyEventKind::Release) - || matches!(&event, TuiEvent::Mouse(mouse) if mouse.kind != MouseEventKind::Moved)) - { - self.draw(tui)?; + if matches!(event, TuiEvent::Draw) && self.view.tick_selection(&self.cells) { + tui.frame_requester() + .schedule_frame_in(crate::tui::TARGET_FRAME_INTERVAL); } + completed_copy + } + + pub(crate) fn handle_input(&mut self, tui: &mut tui::Tui, event: TuiEvent) { let action = match event { TuiEvent::Key(key) => self.handle_key(key), TuiEvent::Mouse(mut mouse) => { @@ -230,28 +255,23 @@ impl TranscriptOverlay { } TuiEvent::Paste(text) => { if self.view.is_select_mode() && !self.view.is_search_editing() { - return Ok(completed_copy); + return; } if self.view.is_search_editing() { self.view.end_selection(&self.cells); } self.view.paste_search(&text).then_some(ViewAction::Changed) } - TuiEvent::Draw | TuiEvent::Resize(_) | TuiEvent::FocusGained | TuiEvent::Resume => { - if matches!(event, TuiEvent::Draw) && self.view.tick_selection(&self.cells) { - tui.frame_requester() - .schedule_frame_in(crate::tui::TARGET_FRAME_INTERVAL); - } - self.draw(tui)?; - return Ok(completed_copy); - } - TuiEvent::FocusLost => None, + TuiEvent::Draw + | TuiEvent::Resize(_) + | TuiEvent::FocusGained + | TuiEvent::Resume + | TuiEvent::FocusLost => None, }; if let Some(action) = action { self.apply_action(tui, action); tui.frame_requester().schedule_frame(); } - Ok(completed_copy) } pub(crate) fn is_done(&self) -> bool {