From b0b393991b089588a0c0a8c662625f872adee20e Mon Sep 17 00:00:00 2001 From: OnlyYu1996 <1158673577@qq.com> Date: Sun, 17 May 2026 21:27:35 +0800 Subject: [PATCH] fix(tui): reset quit confirmation on esc --- src/cortex-tui/src/app/methods.rs | 37 +++++++++++++++++++ src/cortex-tui/src/runner/event_loop/input.rs | 2 + 2 files changed, 39 insertions(+) diff --git a/src/cortex-tui/src/app/methods.rs b/src/cortex-tui/src/app/methods.rs index 6156596ee..1dff74e9b 100644 --- a/src/cortex-tui/src/app/methods.rs +++ b/src/cortex-tui/src/app/methods.rs @@ -165,6 +165,8 @@ impl AppState { /// Handle Ctrl+C press (double-tap to quit) pub fn handle_ctrl_c(&mut self) -> bool { + self.reset_esc(); + let now = Instant::now(); if let Some(last) = self.last_ctrl_c && now.duration_since(last) < Duration::from_millis(500) @@ -183,6 +185,8 @@ impl AppState { /// Handle ESC press (double-tap to quit when idle) pub fn handle_esc(&mut self) -> bool { + self.reset_ctrl_c(); + let now = Instant::now(); if let Some(last) = self.last_esc && now.duration_since(last) < Duration::from_millis(500) @@ -200,6 +204,39 @@ impl AppState { } } +#[cfg(test)] +mod tests { + use super::AppState; + + #[test] + fn test_esc_resets_ctrl_c_quit_confirmation() { + let mut app_state = AppState::new(); + + assert!(!app_state.handle_ctrl_c()); + assert!(app_state.last_ctrl_c.is_some()); + + assert!(!app_state.handle_esc()); + assert!(app_state.last_ctrl_c.is_none()); + + assert!(!app_state.handle_ctrl_c()); + assert!(!app_state.should_quit()); + } + + #[test] + fn test_ctrl_c_resets_esc_quit_confirmation() { + let mut app_state = AppState::new(); + + assert!(!app_state.handle_esc()); + assert!(app_state.last_esc.is_some()); + + assert!(!app_state.handle_ctrl_c()); + assert!(app_state.last_esc.is_none()); + + assert!(!app_state.handle_esc()); + assert!(!app_state.should_quit()); + } +} + // ============================================================================ // APPSTATE METHODS - Modals // ============================================================================ diff --git a/src/cortex-tui/src/runner/event_loop/input.rs b/src/cortex-tui/src/runner/event_loop/input.rs index 8b20ecbb2..83cf116e5 100644 --- a/src/cortex-tui/src/runner/event_loop/input.rs +++ b/src/cortex-tui/src/runner/event_loop/input.rs @@ -367,6 +367,8 @@ impl EventLoop { /// Handle ESC key with double-tap to quit fn handle_esc(&mut self, terminal: &mut CortexTerminal) -> Result<()> { + self.app_state.reset_ctrl_c(); + // Priority 1: If viewing a subagent conversation, return to main conversation if self.app_state.is_viewing_subagent() { self.app_state.return_to_main_conversation();