Avoid unnecessary TUI redraws on terminal focus changes (#64752)

## Why

Switching away from an idle composer can write to the terminal even when
there are no animations to clear. Regaining focus already triggers a draw.

## What changed

On focus loss, request a redraw when composer sparkles are visible or
when an active empty-state replay is canceled.

## Testing

Add a PTY regression test that checks for zero terminal output after idle
focus loss and verifies that input is accepted immediately after focus
returns, in both inline and fullscreen modes.

GitOrigin-RevId: c961a4274049b98fff7307092f6ac89d51cf8e95
This commit is contained in:
Eric Traut
2026-11-30 16:25:43 +00:00
committed by copyberry
parent 31d8ed6d0f
commit f62d4836b8
4 changed files with 70 additions and 8 deletions
+5 -4
View File
@@ -1322,12 +1322,13 @@ impl App {
if !tui.is_owned_screen() || self.overlay.is_some() {
return Ok(false);
}
if matches!(event, TuiEvent::FocusLost) {
self.chat_widget
if matches!(event, TuiEvent::FocusLost)
&& self
.chat_widget
.empty_state_animation
.borrow_mut()
.cancel_replay();
// Show the static, faded decoration immediately when the terminal loses focus.
.cancel_replay()
{
tui.frame_requester().schedule_frame();
}
if self.transcript_view.history == TranscriptHistoryState::Idle {
@@ -139,8 +139,13 @@ impl BottomPane {
}
pub(crate) fn set_sparkle_terminal_focus(&mut self, focused: bool) {
if self.composer.sparkle.terminal_focused != focused {
self.composer.sparkle.terminal_focused = focused;
let sparkle = &mut self.composer.sparkle;
if sparkle.terminal_focused == focused {
return;
}
sparkle.terminal_focused = focused;
// Focus gain already draws. Focus loss needs a frame when stars may need clearing.
if !focused && matches!(sparkle.phase.get(), Phase::Visible(_)) {
self.request_redraw();
}
}
+4 -2
View File
@@ -85,10 +85,12 @@ impl EmptyStateAnimation {
self.cancel_replay();
}
pub(crate) fn cancel_replay(&mut self) {
/// Returns whether an active replay was cancelled.
pub(crate) fn cancel_replay(&mut self) -> bool {
self.stage = None;
self.replaying = false;
let was_replaying = std::mem::take(&mut self.replaying);
self.pause_clock();
was_replaying
}
/// Replay only on the drawn blossom; unrelated gestures keep their normal owner.
+54
View File
@@ -85,6 +85,60 @@ fn focus_gained_with_unanswered_palette_queries_preserves_immediate_input() -> R
Ok(())
}
/// Switching away from a settled composer should leave both terminal modes quiet.
#[test]
fn idle_focus_loss_does_not_write_to_the_terminal() -> Result<()> {
let workspace = tempfile::tempdir()?;
let quiet_period = Duration::from_secs(/*secs*/ 1);
for fullscreen in [false, true] {
let codex_home = tempfile::tempdir()?;
write_test_config(codex_home.path(), workspace.path())?;
let mut args = vec![
"--no-daemon",
"-c",
"tui.animations=false",
"-c",
"check_for_update_on_startup=false",
"-c",
"openai_base_url=\"http://127.0.0.1:9/v1\"",
];
if !fullscreen {
args.push("--no-alt-screen");
}
let mut terminal = PtyCodex::start(workspace.path(), codex_home, &args)?;
terminal.wait_for_startup()?;
terminal.wait_for_screen("GPT-5.6-Terra")?;
assert_eq!(terminal.parser.screen().alternate_screen(), fullscreen);
let deadline = Instant::now() + STARTUP_TIMEOUT;
let before_focus_loss = loop {
let output_len = terminal.output.len();
terminal.read_output(quiet_period)?;
terminal.ensure_running()?;
if terminal.output.len() == output_len {
break output_len;
}
ensure!(Instant::now() < deadline, "startup output did not settle");
};
terminal.write_input(b"\x1b[O")?;
terminal.read_output(quiet_period)?;
terminal.ensure_running()?;
assert_eq!(
&terminal.output[before_focus_loss..],
b"",
"idle focus loss wrote terminal output with fullscreen={fullscreen}"
);
let focus_started = Instant::now();
terminal.write_input(format!("\x1b[I{FOCUS_PROBE_INPUT}").as_bytes())?;
terminal.wait_for_focus_input(FOCUS_PROBE_INPUT, focus_started, before_focus_loss)?;
}
Ok(())
}
#[cfg(target_os = "macos")]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn interactive_startup_honors_codex_home_symlink_opt_out() -> Result<()> {