mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-16 03:09:33 +01:00
Merge fix/desktop-keep-awake-window-close: closing the window no longer ends Keep This Mac Awake
# Conflicts: # packages/desktop/README.md
This commit is contained in:
commit
9518f4ecec
3 files changed
+54
-7
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -727,6 +727,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,
|
||||
@@ -743,7 +755,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
|
||||
@@ -815,18 +827,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(),
|
||||
|
||||
Reference in new issue
Block a user