Make the quit check say whether the agent is waiting on you, with one icon

AI Assisted
This commit is contained in:
jparkerweb committed 2026-10-28 01:14:50 -07:00
1 parent 478ec28bc5
commit 8db8df5791
6 files changed
+272 -9

No files matched your search

+3
View File
@@ -10,7 +10,10 @@ pub mod handoff;
pub mod node_check;
pub mod project;
pub mod pty_host;
pub mod quit_prompt;
pub mod restart_brief;
pub mod session_reader;
pub mod settings;
#[cfg(windows)]
pub mod task_dialog;
pub mod versions;
+65 -7
View File
@@ -1,6 +1,7 @@
// Hide the extra console window on Windows release builds.
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, OnceLock};
use plan2code_app_lib::agent_manager::{AgentEvent, AgentManager, AgentState, BeforeFresh, Sink};
@@ -12,8 +13,11 @@ use plan2code_app_lib::console_supervisor::{
};
use plan2code_app_lib::node_check;
use plan2code_app_lib::project::{self, ProjectDir};
use plan2code_app_lib::quit_prompt::{quit_prompt, QuitCase};
use plan2code_app_lib::restart_brief;
use plan2code_app_lib::settings;
#[cfg(windows)]
use plan2code_app_lib::task_dialog;
use plan2code_app_lib::versions;
use tauri::ipc::{Channel, InvokeResponseBody};
use tauri::{Emitter, Manager, State};
@@ -405,17 +409,70 @@ fn agent_reattach(
})
}
// Asks before quitting while an agent runs; "no" keeps the app open.
// Set while a quit box is open, so a second close or quit does not stack another.
static QUIT_OPEN: AtomicBool = AtomicBool::new(false);
// Clears QUIT_OPEN when the box closes, however its handler ends.
struct QuitOpen;
impl Drop for QuitOpen {
fn drop(&mut self) {
QUIT_OPEN.store(false, Ordering::SeqCst);
}
}
// Asks before quitting while an agent runs; anything but Quit keeps the app
// open. The words follow whose turn the page last reported (quit_prompt.rs).
fn confirm_quit(app: &tauri::AppHandle) {
if QUIT_OPEN.swap(true, Ordering::SeqCst) {
return;
}
let open = QuitOpen;
let last = app
.state::<AppState>()
.attention
.lock()
.ok()
.and_then(|guard| guard.last);
let prompt = quit_prompt(last);
#[cfg(windows)]
{
if let Some(parent) = app
.get_webview_window("main")
.and_then(|window| window.hwnd().ok())
{
// TaskDialog blocks until it closes, so it runs off the event loop.
let app = app.clone();
let parent = parent.0 as isize;
std::thread::spawn(move || {
let _open = open;
if task_dialog::ask(parent as _, &prompt) {
quit_app(&app);
}
});
return;
}
}
let kind = match prompt.case {
QuitCase::Waiting => MessageDialogKind::Info,
QuitCase::Working => MessageDialogKind::Warning,
};
// The plugin makes its first button the default, so off Windows Enter
// means Quit. Stay first would make Esc quit instead, since the plugin
// reads Esc as "not the first button".
let app = app.clone();
app.dialog()
.message("An agent is still running. Quit and stop it?")
.title("Quit Plan2Code")
.kind(MessageDialogKind::Warning)
.buttons(MessageDialogButtons::YesNo)
.message(prompt.message)
.title(prompt.heading)
.kind(kind)
.buttons(MessageDialogButtons::OkCancelCustom(
prompt.quit.to_string(),
prompt.stay.to_string(),
))
.show({
let app = app.clone();
move |quit| {
let _open = open;
if quit {
quit_app(&app);
}
@@ -506,10 +563,11 @@ fn apply_badge(window: &tauri::WebviewWindow, badge: Badge) {
}
// No dot and no flash: the agent ended, the page went away, or the switch was
// turned off. The next state the page tells starts from plain.
// turned off. Whose turn it is is unknown until the page tells again, so the
// quit check uses its working words meanwhile (the agent may be mid-step).
fn clear_attention(app: &tauri::AppHandle) {
if let Ok(mut guard) = app.state::<AppState>().attention.lock() {
guard.last = Some(Signal::Plain);
guard.last = None;
}
let Some(window) = app.get_webview_window("main") else {
return;
+102
View File
@@ -0,0 +1,102 @@
//! The words the quit check shows while an agent runs. Most of the time the
//! agent is not mid-step at all but waiting on the person's answer in the web
//! console, so the check reads the page's last taskbar signal (attention.rs)
//! and says which case it is. Anything it cannot be sure of is the working
//! case, the safer warning. Knows nothing about Tauri; main.rs shows it.
use crate::attention::Signal;
/// Whether quitting would stop the agent partway through something.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum QuitCase {
/// The person's move, or nothing left to do: nothing is half done.
Waiting,
/// Busy, quiet for too long, gone, or not known yet.
Working,
}
/// Everything the box says.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct QuitPrompt {
pub case: QuitCase,
pub title: &'static str,
pub heading: &'static str,
pub message: &'static str,
pub quit: &'static str,
pub stay: &'static str,
}
/// The prompt for the page's last signal (`None` before the page has sent one).
pub fn quit_prompt(last: Option<Signal>) -> QuitPrompt {
match last {
Some(Signal::Ready | Signal::Plain) => QuitPrompt {
case: QuitCase::Waiting,
title: "Plan2Code",
heading: "Done for now?",
message: "Your agent is just waiting on your next answer. Quit and this session ends.",
quit: "Quit",
stay: "Stay",
},
Some(Signal::Busy | Signal::Attention) | None => QuitPrompt {
case: QuitCase::Working,
title: "Plan2Code",
heading: "Leave while your agent is busy?",
message: "If you quit now, the agent stops mid-step. Everything it has saved so far stays put.",
quit: "Quit and stop it",
stay: "Stay",
},
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_persons_move_and_nothing_left_are_the_waiting_case() {
assert_eq!(quit_prompt(Some(Signal::Ready)).case, QuitCase::Waiting);
assert_eq!(quit_prompt(Some(Signal::Plain)).case, QuitCase::Waiting);
}
#[test]
fn busy_attention_and_unknown_are_the_working_case() {
assert_eq!(quit_prompt(Some(Signal::Busy)).case, QuitCase::Working);
assert_eq!(
quit_prompt(Some(Signal::Attention)).case,
QuitCase::Working,
"attention covers a quiet agent that may still be mid-step"
);
assert_eq!(quit_prompt(None).case, QuitCase::Working);
}
#[test]
fn each_case_has_its_own_words() {
let waiting = quit_prompt(Some(Signal::Ready));
assert_eq!(waiting.heading, "Done for now?");
assert_eq!(waiting.quit, "Quit");
let working = quit_prompt(Some(Signal::Busy));
assert_eq!(working.heading, "Leave while your agent is busy?");
assert_eq!(working.quit, "Quit and stop it");
assert!(working.message.contains("mid-step"));
}
#[test]
fn stay_is_never_mistaken_for_quit() {
for last in [None, Some(Signal::Busy), Some(Signal::Ready)] {
let prompt = quit_prompt(last);
assert_eq!(prompt.stay, "Stay");
assert_ne!(prompt.stay, prompt.quit);
assert_eq!(prompt.title, "Plan2Code");
}
}
#[test]
fn no_text_carries_an_em_dash() {
for last in [None, Some(Signal::Busy), Some(Signal::Ready)] {
let p = quit_prompt(last);
for text in [p.title, p.heading, p.message, p.quit, p.stay] {
assert!(!text.contains('\u{2014}'), "{text}");
}
}
}
}
@@ -0,0 +1,98 @@
//! The quit check's box on Windows, drawn with TaskDialog directly. The dialog
//! plugin has no heading, always makes its first button the default, and
//! leaves Windows to copy the body's icon into the title bar, so the same
//! warning showed twice. Here the heading is the question, Stay is the default
//! (Enter, Esc and the close button all keep the app open), and the title bar
//! wears the app's own icon. The words come from quit_prompt.rs.
use crate::quit_prompt::{QuitCase, QuitPrompt};
use std::ptr;
use winapi::shared::basetsd::LONG_PTR;
use winapi::shared::minwindef::{LPARAM, UINT, WPARAM};
use winapi::shared::ntdef::HRESULT;
use winapi::shared::windef::HWND;
use winapi::shared::winerror::S_OK;
use winapi::um::commctrl::{
TaskDialogIndirect, TASKDIALOGCONFIG, TASKDIALOG_BUTTON, TDF_ALLOW_DIALOG_CANCELLATION,
TDF_POSITION_RELATIVE_TO_WINDOW, TDN_CREATED, TD_INFORMATION_ICON, TD_WARNING_ICON,
};
use winapi::um::winuser::{
GetClassLongPtrW, SendMessageW, GCLP_HICONSM, ICON_BIG, ICON_SMALL, ICON_SMALL2, WM_GETICON,
WM_SETICON,
};
const ID_QUIT: i32 = 100;
const ID_STAY: i32 = 101;
fn wide(text: &str) -> Vec<u16> {
text.encode_utf16().chain(Some(0)).collect()
}
// Once the box exists, give its title bar the parent window's icon instead of
// the copy of the body's icon Windows puts there.
unsafe extern "system" fn on_event(
hwnd: HWND,
msg: UINT,
_wparam: WPARAM,
_lparam: LPARAM,
parent: LONG_PTR,
) -> HRESULT {
if msg == TDN_CREATED {
let parent = parent as HWND;
let mut icon = SendMessageW(parent, WM_GETICON, ICON_SMALL2 as WPARAM, 0);
if icon == 0 {
icon = GetClassLongPtrW(parent, GCLP_HICONSM) as isize;
}
SendMessageW(hwnd, WM_SETICON, ICON_SMALL as WPARAM, icon);
SendMessageW(hwnd, WM_SETICON, ICON_BIG as WPARAM, icon);
}
S_OK
}
/// Shows the box over `parent` and blocks until it closes. True only when
/// Quit was pressed; anything else (Stay, Esc, the close button, a failure to
/// show the box) keeps the app open.
pub fn ask(parent: HWND, prompt: &QuitPrompt) -> bool {
let title = wide(prompt.title);
let heading = wide(prompt.heading);
let message = wide(prompt.message);
let quit = wide(prompt.quit);
let stay = wide(prompt.stay);
let buttons = [
TASKDIALOG_BUTTON {
nButtonID: ID_QUIT,
pszButtonText: quit.as_ptr(),
},
TASKDIALOG_BUTTON {
nButtonID: ID_STAY,
pszButtonText: stay.as_ptr(),
},
];
let icon = match prompt.case {
QuitCase::Waiting => TD_INFORMATION_ICON,
QuitCase::Working => TD_WARNING_ICON,
};
// SAFETY: every pointer in the config points at a local above that
// outlives the blocking TaskDialogIndirect call, and an all-zero
// TASKDIALOGCONFIG is the documented "nothing set" for every other field.
unsafe {
let mut config: TASKDIALOGCONFIG = std::mem::zeroed();
config.cbSize = std::mem::size_of::<TASKDIALOGCONFIG>() as UINT;
config.hwndParent = parent;
config.dwFlags = TDF_ALLOW_DIALOG_CANCELLATION | TDF_POSITION_RELATIVE_TO_WINDOW;
config.pszWindowTitle = title.as_ptr();
*config.u1.pszMainIcon_mut() = icon;
config.pszMainInstruction = heading.as_ptr();
config.pszContent = message.as_ptr();
config.cButtons = buttons.len() as UINT;
config.pButtons = buttons.as_ptr();
config.nDefaultButton = ID_STAY;
config.pfCallback = Some(on_event);
config.lpCallbackData = parent as LONG_PTR;
let mut pressed = 0;
let result = TaskDialogIndirect(&config, &mut pressed, ptr::null_mut(), ptr::null_mut());
result == S_OK && pressed == ID_QUIT
}
}