fix(desktop): closing the window no longer ends Keep This Mac Awake

Keep This Mac Awake lives in the app and ends when the app quits, and
closing the main window quit the app. So with a mode on, the red button
(or Cmd+W) followed by closing the lid let the MacBook sleep: the lid
request file was cleared and the helper released disablesleep at its
next run.

While a mode is on, closing the main window now hides the app the way
Cmd+H does (hide_application), so full screen and pop-outs come back as
they were on a click on the Dock icon. Cmd+Q and Quit from the Dock
still end it, through LoopDestroyed as before. With keep-awake off,
closing the window quits as it always did.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Codeman maintainer committed 2026-11-09 07:57:40 +01:00
1 parent 2f9843c94a
commit 430bf6217b
3 files changed
+54 -7

No files matched your search

+21 -1
View File
@@ -33,7 +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.
//! 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.
//!
//! 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
@@ -123,6 +125,14 @@ 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`.
@@ -1221,6 +1231,16 @@ mod tests {
assert!(Status::Recently.text().contains("5 minutes"));
}
#[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());
for mode in [Mode::WhileWorking, Mode::Always] {
assert_eq!(mode.close_hides_app(), available(), "{mode:?}");
}
}
#[test]
fn a_menu_id_names_one_mode() {
for mode in Mode::ALL {
+32 -5
View File
@@ -725,6 +725,18 @@ fn build_webview(
Ok(webview)
}
/// Hides the whole app, as Cmd+H does. A click on the Dock icon (or Cmd+Tab)
/// shows it again with every window as it was, a full-screen one included,
/// which hiding the main window alone would not do.
#[cfg(target_os = "macos")]
fn hide_app(target: &EventLoopWindowTarget<UserEvent>) {
use tao::platform::macos::EventLoopWindowTargetExtMacOS;
target.hide_application();
}
#[cfg(not(target_os = "macos"))]
fn hide_app(_target: &EventLoopWindowTarget<UserEvent>) {}
impl App {
fn handle(
&mut self,
@@ -741,7 +753,7 @@ impl App {
self.menu.help_menu.set_as_help_menu_for_nsapp();
}
}
Event::WindowEvent { window_id, event, .. } => self.on_window_event(window_id, event, control_flow),
Event::WindowEvent { window_id, event, .. } => self.on_window_event(window_id, event, target, control_flow),
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
@@ -813,18 +825,33 @@ impl App {
self.fullscreen_retry = Some((now + FULLSCREEN_RETRY_EVERY, attempts + 1));
}
fn on_window_event(&mut self, id: WindowId, event: WindowEvent, control_flow: &mut ControlFlow) {
fn on_window_event(
&mut self,
id: WindowId,
event: WindowEvent,
target: &EventLoopWindowTarget<UserEvent>,
control_flow: &mut ControlFlow,
) {
match event {
WindowEvent::CloseRequested => {
if id == self.main_id {
if id != self.main_id {
self.remove_window(id);
} else if self.config.keep_awake.mode.close_hides_app() {
// 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();
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;
} else {
self.remove_window(id);
}
}
WindowEvent::Moved(_) | WindowEvent::Resized(_) if id == self.main_id => self.record_main_frame(),