Merge #800: fix(desktop): closing the window quits when keep-awake watches nothing here

# Conflicts:
#	packages/desktop/README.md
This commit is contained in:
Codeman maintainer committed 2026-11-10 18:01:05 +01:00
commit 91a4aa6b4e
3 files changed
+58 -30

No files matched your search

+42 -17
View File
@@ -33,9 +33,9 @@
//! another, so no separate "remove" item and no helper left behind unasked.
//!
//! It lives in the app, so it ends with the app: Codeman's LaunchAgent keeps the server
//! running after the app quits, but nothing keeps the Mac awake then. So while a mode is
//! on, closing the main window hides the app instead of quitting it
//! (`Mode::close_hides_app`): only Cmd+Q (or Quit from the Dock) ends it.
//! running after the app quits, but nothing keeps the Mac awake then. So while it watches
//! a Codeman on this Mac, closing the main window hides the app instead of quitting it
//! (`close_hides_app`): only Cmd+Q (or Quit from the Dock) ends it.
//!
//! The agents are asked natively, like the logo blink does (`agents`): WebKit stalls the
//! event stream of a hidden page. A worker thread does the asking and the holding, and
@@ -125,14 +125,6 @@ impl Mode {
pub fn from_menu_id(id: &str) -> Option<Self> {
Self::ALL.into_iter().find(|mode| mode.menu_id() == id)
}
/// Whether closing the main window hides the app instead of quitting it.
/// Keep-awake ends with the app, so while a mode is on, the red button (or
/// Cmd+W) must not end it: the app hides as Cmd+H does, a click on its Dock
/// icon brings it back, and Cmd+Q still quits.
pub fn close_hides_app(self) -> bool {
available() && self != Mode::Off
}
}
/// The `keepAwake` object in `desktop.json`.
@@ -281,6 +273,29 @@ pub fn status_line(decision: Decision, settings: Settings, facts: Facts, covered
format!("{text}, {lid}")
}
/// Whether closing the main window hides the app instead of quitting it.
/// Keep-awake ends with the app, so while it watches a Codeman on this Mac the
/// red button (or Cmd+W) must not end it: the app hides as Cmd+H does, a click
/// on its Dock icon brings it back, and Cmd+Q still quits. That holds while it
/// is only paused (on battery, no agent at work, no answer), since it carries
/// on by itself once that changes. With no server open (the connect page), or
/// one on another machine, it keeps nothing awake, so closing the window quits.
/// `local_addresses` is only read with a mode on and a server open.
pub fn close_hides_app(mode: Mode, server: Option<&Url>, local_addresses: impl FnOnce() -> Vec<IpAddr>) -> bool {
mode != Mode::Off && server.is_some_and(|server| is_this_mac(server, &local_addresses()))
}
/// Every address of this Mac's interfaces.
#[cfg(target_os = "macos")]
pub fn local_addresses() -> Vec<IpAddr> {
system::local_addresses()
}
#[cfg(not(target_os = "macos"))]
pub fn local_addresses() -> Vec<IpAddr> {
Vec::new()
}
/// Whether `server` is this Mac: a loopback address or `localhost`, or an address
/// literal one of this Mac's interfaces has. A host name is not looked up.
pub fn is_this_mac(server: &Url, local_addresses: &[IpAddr]) -> bool {
@@ -944,7 +959,7 @@ mod system {
}
/// Every address of this Mac's interfaces.
fn local_addresses() -> Vec<IpAddr> {
pub(super) fn local_addresses() -> Vec<IpAddr> {
let mut found = Vec::new();
// SAFETY: getifaddrs fills a list that freeifaddrs frees; each ifa_addr is
// NULL or a sockaddr whose family says which struct it is.
@@ -1232,13 +1247,23 @@ mod tests {
}
#[test]
fn closing_the_window_hides_the_app_only_while_a_mode_is_on() {
// Off quits as it always did; a mode that keeps the Mac awake must not
// end with the red button, since keep-awake ends with the app.
assert!(!Mode::Off.close_hides_app());
fn closing_the_window_hides_the_app_only_while_it_watches_this_mac() {
let own = || vec!["192.168.1.20".parse::<IpAddr>().unwrap()];
let unread = || -> Vec<IpAddr> { panic!("the addresses decide nothing here") };
let here = url("http://127.0.0.1:3000/");
let lan = url("http://192.168.1.20:3000/");
let elsewhere = url("http://192.168.1.21:3000/");
for mode in [Mode::WhileWorking, Mode::Always] {
assert_eq!(mode.close_hides_app(), available(), "{mode:?}");
// Keep-awake ends with the app, so the red button must not end it.
assert!(close_hides_app(mode, Some(&here), own), "{mode:?}");
assert!(close_hides_app(mode, Some(&lan), own), "{mode:?}");
// Another machine's Codeman, or the connect page: nothing is kept
// awake, so closing the window quits.
assert!(!close_hides_app(mode, Some(&elsewhere), own), "{mode:?}");
assert!(!close_hides_app(mode, None, unread), "{mode:?}");
}
// Off quits as it always did.
assert!(!close_hides_app(Mode::Off, Some(&here), unread));
}
#[test]
+15 -12
View File
@@ -759,7 +759,9 @@ impl App {
Event::UserEvent(event) => self.on_user_event(event, target),
// A click on the Dock icon while no window is visible, which is
// the main window minimized: tao only reports it, so bring the
// window back here.
// window back here. An app hidden by closing its window
// (`hide_app`) needs nothing here: AppKit unhides it, with every
// window as it was, before it reports the click.
Event::Reopen {
has_visible_windows: false,
..
@@ -835,24 +837,25 @@ impl App {
control_flow: &mut ControlFlow,
) {
match event {
WindowEvent::CloseRequested if id != self.main_id => self.remove_window(id),
WindowEvent::CloseRequested => {
if id != self.main_id {
self.remove_window(id);
} else if self.config.keep_awake.mode.close_hides_app() {
self.record_main_frame();
self.save_config();
let server = self.phase.borrow().loaded().cloned();
let mode = self.config.keep_awake.mode;
if keep_awake::available()
&& keep_awake::close_hides_app(mode, server.as_ref(), keep_awake::local_addresses)
{
// Keep This Mac Awake ends with the app, so closing the
// window while it is on hides Codeman instead, as Cmd+H
// does (full screen and pop-outs included): the Mac stays
// awake, a click on the Dock icon brings it back, and
// Cmd+Q still quits.
self.record_main_frame();
self.save_config();
// window while it watches a Codeman on this Mac hides
// Codeman instead, as Cmd+H does (full screen and pop-outs
// included): the Mac stays awake, a click on the Dock icon
// brings it back, and Cmd+Q still quits.
hide_app(target);
} else {
// Closing the main window quits; the server and its
// sessions keep running, so nothing is lost.
self.keep_awake.shutdown();
self.record_main_frame();
self.save_config();
*control_flow = ControlFlow::Exit;
}
}