diff --git a/codex-rs/tui/src/app/owned_transcript.rs b/codex-rs/tui/src/app/owned_transcript.rs index e9fe0f42f0..088625a0f0 100644 --- a/codex-rs/tui/src/app/owned_transcript.rs +++ b/codex-rs/tui/src/app/owned_transcript.rs @@ -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 { diff --git a/codex-rs/tui/src/bottom_pane/chat_composer/sparkle.rs b/codex-rs/tui/src/bottom_pane/chat_composer/sparkle.rs index 4954757655..9734c3e99b 100644 --- a/codex-rs/tui/src/bottom_pane/chat_composer/sparkle.rs +++ b/codex-rs/tui/src/bottom_pane/chat_composer/sparkle.rs @@ -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(); } } diff --git a/codex-rs/tui/src/empty_state_animation.rs b/codex-rs/tui/src/empty_state_animation.rs index db8113fd7e..2f2d4a9a83 100644 --- a/codex-rs/tui/src/empty_state_animation.rs +++ b/codex-rs/tui/src/empty_state_animation.rs @@ -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. diff --git a/codex-rs/tui/tests/suite/focus_palette.rs b/codex-rs/tui/tests/suite/focus_palette.rs index 8f4c82af5f..d20a479eb3 100644 --- a/codex-rs/tui/tests/suite/focus_palette.rs +++ b/codex-rs/tui/tests/suite/focus_palette.rs @@ -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<()> {