Separate TUI transcript rendering from input handling (#64765)

## What changed

Let the transcript reader paint into a caller-provided frame and area,
returning the delay for the next search or history-loading frame. Split
event preparation, including clipboard completions and selection ticks,
from input dispatch so the app can refresh visible rows before handling
selection input.

Extract transcript reader construction and centralize the app's overlay
input checks. Route draft snapshot flushing through `ChatWidget`.

GitOrigin-RevId: c5e093f585e264c4ffea51ebfcff0b08a1c3ff93
This commit is contained in:
Felipe Coury
2026-11-30 17:00:53 +00:00
committed by copyberry
parent 9baf9789e1
commit 3506461cf4
11 changed files with 106 additions and 78 deletions
+1 -1
View File
@@ -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?;
+2 -6
View File
@@ -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) => {
+1 -1
View File
@@ -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);
+8 -4
View File
@@ -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
+1 -1
View File
@@ -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,
+1 -1
View File
@@ -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
{
+1 -1
View File
@@ -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()
{
+23 -19
View File
@@ -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<Arc<dyn crate::history_cell::HistoryCell>>,
) -> 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);
+1 -1
View File
@@ -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.
@@ -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();
+63 -43
View File
@@ -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<std::time::Duration> {
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<Option<u64>> {
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<u64> {
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 {