mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-16 11:19:33 +01:00
feat(desktop): a simpler Keep This Mac Awake menu
The owner found the options confusing after the first test build. The
menu now reads Off / While Agents Work / Always, then on a MacBook
Only When Plugged In and With the Lid Closed…, then one status line.
- "Always While Codeman Is Open" is now "Always"; the status line says
"Awake: Codeman is open".
- "Only on Power Adapter" is now "Only When Plugged In".
- "Remove the Lid Helper…" is gone: the lid item is the helper.
Ticking it installs the helper behind the administrator prompt,
unticking it removes it behind another. The setting changes only
once the prompt's command ran, so a cancelled prompt leaves the item
as it was.
- Shorter status lines ("Awake: agents working, lid closed too",
"Paused: on battery", "Sleeps as usual: no agent working").
Config keys and values are unchanged, so a saved choice carries over.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
79a6c03363
commit
2f9843c94a
5 files changed
+89
-99
No files matched your search
@@ -22,11 +22,16 @@
|
||||
//!
|
||||
//! When it holds is the user's pick: **While Agents Work** (an agent at work, and for
|
||||
//! `GRACE` after the last one stopped, so the gap between two turns or a reply from
|
||||
//! the phone does not let the Mac drop off), or **Always While Codeman Is Open**. Only
|
||||
//! for a Codeman on this Mac: keeping this Mac awake does nothing for agents that run
|
||||
//! on another machine. Only on the power adapter unless the user says otherwise (a
|
||||
//! the phone does not let the Mac drop off), or **Always** (while the app is open).
|
||||
//! Only for a Codeman on this Mac: keeping this Mac awake does nothing for agents that
|
||||
//! run on another machine. Only when plugged in unless the user says otherwise (a
|
||||
//! MacBook only), and the lid never on battery, whatever the setting.
|
||||
//!
|
||||
//! The menu is kept short on purpose: three modes, then on a MacBook two check items
|
||||
//! (Only When Plugged In, With the Lid Closed…). The lid item IS the helper: ticking it
|
||||
//! installs the helper behind the administrator prompt, unticking it removes it behind
|
||||
//! 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.
|
||||
//!
|
||||
@@ -74,17 +79,16 @@ const REQUEST_FILE: &str = "keep-awake-lid.pid";
|
||||
|
||||
pub const AC_ONLY_ID: &str = "keep-awake-ac-only";
|
||||
pub const LID_ID: &str = "keep-awake-lid";
|
||||
pub const REMOVE_HELPER_ID: &str = "keep-awake-remove-helper";
|
||||
|
||||
/// The status line while nothing is watched.
|
||||
pub const OFF_TEXT: &str = "Off: this Mac sleeps as usual";
|
||||
pub const NO_SERVER_TEXT: &str = "Starts when Codeman on this Mac is open";
|
||||
pub const OFF_TEXT: &str = "Sleeps as usual";
|
||||
pub const NO_SERVER_TEXT: &str = "Waiting for Codeman on this Mac";
|
||||
|
||||
/// What macOS shows in the administrator prompt (`with prompt`). No double quote or
|
||||
/// backslash: they would end the AppleScript string (`applescript` refuses them).
|
||||
const INSTALL_PROMPT: &str = "Codeman wants to install a small helper that keeps this Mac awake with the lid \
|
||||
closed while your agents work. It only acts on the power adapter, and only while the Codeman app asks it to.";
|
||||
const REMOVE_PROMPT: &str = "Codeman wants to remove its lid helper.";
|
||||
const REMOVE_PROMPT: &str = "Codeman wants to remove its lid helper, so this Mac sleeps again when the lid closes.";
|
||||
|
||||
/// When the Mac is kept awake.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
|
||||
@@ -104,7 +108,7 @@ impl Mode {
|
||||
match self {
|
||||
Mode::Off => "Off",
|
||||
Mode::WhileWorking => "While Agents Work",
|
||||
Mode::Always => "Always While Codeman Is Open",
|
||||
Mode::Always => "Always",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,14 +206,14 @@ impl Status {
|
||||
fn text(self) -> &'static str {
|
||||
match self {
|
||||
Status::Off => OFF_TEXT,
|
||||
Status::Remote => "Nothing to keep awake: this Codeman runs on another machine",
|
||||
Status::OnBattery => "Paused on battery power",
|
||||
Status::NoAnswer => "Codeman does not answer, so this Mac may sleep",
|
||||
Status::Refused => "Codeman refused the login, so the agents cannot be seen",
|
||||
Status::Idle => "No agent is working, so this Mac may sleep",
|
||||
Status::Working => "Awake: agents are working",
|
||||
Status::Recently => "Awake: an agent worked in the last 5 minutes",
|
||||
Status::Always => "Awake while Codeman is open",
|
||||
Status::Remote => "Sleeps as usual: Codeman runs on another machine",
|
||||
Status::OnBattery => "Paused: on battery",
|
||||
Status::NoAnswer => "Paused: Codeman does not answer",
|
||||
Status::Refused => "Paused: Codeman refused the login",
|
||||
Status::Idle => "Sleeps as usual: no agent working",
|
||||
Status::Working => "Awake: agents working",
|
||||
Status::Recently => "Awake: an agent finished in the last 5 minutes",
|
||||
Status::Always => "Awake: Codeman is open",
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -256,15 +260,15 @@ pub fn status_line(decision: Decision, settings: Settings, facts: Facts, covered
|
||||
// Asked for, but missing or from another version of the app (an update that
|
||||
// changed the script, a removal by hand): the lid item offers the install again.
|
||||
let lid = if !facts.helper {
|
||||
"the lid helper needs installing again"
|
||||
"not with the lid closed: turn it on again"
|
||||
} else if facts.on_battery {
|
||||
"not with the lid closed on battery"
|
||||
} else if covered == Some(true) {
|
||||
"also with the lid closed"
|
||||
"lid closed too"
|
||||
} else {
|
||||
"the lid helper has not answered yet"
|
||||
"lid closed in a moment"
|
||||
};
|
||||
format!("{text}; {lid}")
|
||||
format!("{text}, {lid}")
|
||||
}
|
||||
|
||||
/// Whether `server` is this Mac: a loopback address or `localhost`, or an address
|
||||
@@ -1313,14 +1317,11 @@ mod tests {
|
||||
let lid = Settings { lid: true, ..WORK };
|
||||
let ready = Facts { helper: true, ..HERE };
|
||||
let line = |settings, facts, covered| status_line(decide(settings, facts), settings, facts, covered);
|
||||
assert_eq!(line(WORK, HERE, None), "Awake: agents are working");
|
||||
assert_eq!(
|
||||
line(lid, ready, Some(true)),
|
||||
"Awake: agents are working; also with the lid closed"
|
||||
);
|
||||
assert_eq!(line(WORK, HERE, None), "Awake: agents working");
|
||||
assert_eq!(line(lid, ready, Some(true)), "Awake: agents working, lid closed too");
|
||||
assert_eq!(
|
||||
line(lid, ready, Some(false)),
|
||||
"Awake: agents are working; the lid helper has not answered yet"
|
||||
"Awake: agents working, lid closed in a moment"
|
||||
);
|
||||
let battery = Facts {
|
||||
on_battery: true,
|
||||
@@ -1329,19 +1330,19 @@ mod tests {
|
||||
let anywhere = Settings { ac_only: false, ..lid };
|
||||
assert_eq!(
|
||||
line(anywhere, battery, None),
|
||||
"Awake: agents are working; not with the lid closed on battery"
|
||||
"Awake: agents working, not with the lid closed on battery"
|
||||
);
|
||||
// Asked for, but no helper (an update changed it): the line says so.
|
||||
assert_eq!(
|
||||
line(lid, HERE, None),
|
||||
"Awake: agents are working; the lid helper needs installing again"
|
||||
"Awake: agents working, not with the lid closed: turn it on again"
|
||||
);
|
||||
// Not held: nothing about the lid.
|
||||
let idle = Facts {
|
||||
agents: Agents::Idle,
|
||||
..ready
|
||||
};
|
||||
assert_eq!(line(lid, idle, None), "No agent is working, so this Mac may sleep");
|
||||
assert_eq!(line(lid, idle, None), "Sleeps as usual: no agent working");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1623,28 +1624,25 @@ mod tests {
|
||||
fn the_worker_holds_while_agents_work_and_lets_go_after_the_grace() {
|
||||
let mut rig = rig(true);
|
||||
rig.watch(WORK);
|
||||
assert_eq!(
|
||||
rig.look_at(0).as_deref(),
|
||||
Some("No agent is working, so this Mac may sleep")
|
||||
);
|
||||
assert_eq!(rig.look_at(0).as_deref(), Some("Sleeps as usual: no agent working"));
|
||||
assert_eq!(
|
||||
rig.mac.last_hold(),
|
||||
Some(Hold::Watching),
|
||||
"App Nap off while it watches"
|
||||
);
|
||||
rig.answer(vec![seen(true)]);
|
||||
assert_eq!(rig.look_at(2).as_deref(), Some("Awake: agents are working"));
|
||||
assert_eq!(rig.look_at(2).as_deref(), Some("Awake: agents working"));
|
||||
assert_eq!(rig.mac.last_hold(), Some(Hold::Holding));
|
||||
rig.answer(vec![seen(false)]);
|
||||
assert_eq!(
|
||||
rig.look_at(4).as_deref(),
|
||||
Some("Awake: an agent worked in the last 5 minutes")
|
||||
Some("Awake: an agent finished in the last 5 minutes")
|
||||
);
|
||||
assert_eq!(rig.mac.last_hold(), Some(Hold::Holding));
|
||||
assert_eq!(rig.look_at(200), None, "unchanged: nothing to report");
|
||||
assert_eq!(
|
||||
rig.look_at(2 + GRACE.as_secs()).as_deref(),
|
||||
Some("No agent is working, so this Mac may sleep")
|
||||
Some("Sleeps as usual: no agent working")
|
||||
);
|
||||
assert_eq!(rig.mac.last_hold(), Some(Hold::Watching));
|
||||
}
|
||||
@@ -1660,7 +1658,7 @@ mod tests {
|
||||
assert_eq!(rig.mac.last_hold(), Some(Hold::Holding));
|
||||
assert_eq!(
|
||||
rig.look_at(GRACE.as_secs()).as_deref(),
|
||||
Some("Codeman does not answer, so this Mac may sleep")
|
||||
Some("Paused: Codeman does not answer")
|
||||
);
|
||||
assert_eq!(rig.mac.last_hold(), Some(Hold::Watching));
|
||||
}
|
||||
@@ -1670,10 +1668,7 @@ mod tests {
|
||||
let mut rig = rig(true);
|
||||
rig.watch(WORK);
|
||||
rig.answer(vec![Poll::Refused]);
|
||||
assert_eq!(
|
||||
rig.look_at(0).as_deref(),
|
||||
Some("Codeman refused the login, so the agents cannot be seen")
|
||||
);
|
||||
assert_eq!(rig.look_at(0).as_deref(), Some("Paused: Codeman refused the login"));
|
||||
rig.look_at(2);
|
||||
rig.look_at(4);
|
||||
assert_eq!(*rig.asked.borrow(), 1);
|
||||
@@ -1693,7 +1688,7 @@ mod tests {
|
||||
fn always_holds_without_asking_the_server() {
|
||||
let mut rig = rig(true);
|
||||
rig.watch(ALWAYS);
|
||||
assert_eq!(rig.look_at(0).as_deref(), Some("Awake while Codeman is open"));
|
||||
assert_eq!(rig.look_at(0).as_deref(), Some("Awake: Codeman is open"));
|
||||
assert_eq!(rig.mac.last_hold(), Some(Hold::Holding));
|
||||
assert_eq!(*rig.asked.borrow(), 0);
|
||||
}
|
||||
@@ -1705,7 +1700,7 @@ mod tests {
|
||||
assert!(!rig.worker.ticking());
|
||||
assert_eq!(
|
||||
rig.look_at(0).as_deref(),
|
||||
Some("Nothing to keep awake: this Codeman runs on another machine")
|
||||
Some("Sleeps as usual: Codeman runs on another machine")
|
||||
);
|
||||
assert_eq!(*rig.asked.borrow(), 0);
|
||||
assert!(rig.mac.0.borrow().holds.is_empty());
|
||||
@@ -1717,7 +1712,7 @@ mod tests {
|
||||
rig.watch(ALWAYS);
|
||||
rig.look_at(0);
|
||||
rig.mac.0.borrow_mut().battery = true;
|
||||
assert_eq!(rig.look_at(2).as_deref(), Some("Paused on battery power"));
|
||||
assert_eq!(rig.look_at(2).as_deref(), Some("Paused: on battery"));
|
||||
assert_eq!(rig.mac.last_hold(), Some(Hold::None));
|
||||
rig.mac.0.borrow_mut().battery = false;
|
||||
rig.look_at(4);
|
||||
@@ -1732,7 +1727,7 @@ mod tests {
|
||||
rig.watch(Settings { lid: true, ..ALWAYS });
|
||||
assert_eq!(
|
||||
rig.look_at(0).as_deref(),
|
||||
Some("Awake while Codeman is open; also with the lid closed")
|
||||
Some("Awake: Codeman is open, lid closed too")
|
||||
);
|
||||
assert_eq!(rig.mac.0.borrow().writes, 1);
|
||||
rig.look_at(10);
|
||||
@@ -1753,7 +1748,7 @@ mod tests {
|
||||
rig.mac.0.borrow_mut().battery = true;
|
||||
assert_eq!(
|
||||
rig.look_at(32).as_deref(),
|
||||
Some("Awake while Codeman is open; not with the lid closed on battery")
|
||||
Some("Awake: Codeman is open, not with the lid closed on battery")
|
||||
);
|
||||
assert_eq!(rig.mac.0.borrow().clears, 1);
|
||||
// Nothing to watch: everything let go.
|
||||
@@ -1800,7 +1795,7 @@ mod tests {
|
||||
run(&rx, worker, &|generation, line| {
|
||||
lines.borrow_mut().push((generation, line))
|
||||
});
|
||||
assert_eq!(lines.into_inner(), vec![(2, "Awake while Codeman is open".to_owned())]);
|
||||
assert_eq!(lines.into_inner(), vec![(2, "Awake: Codeman is open".to_owned())]);
|
||||
assert_eq!(mac.last_hold(), Some(Hold::None), "the end lets go");
|
||||
}
|
||||
}
|
||||
@@ -1138,28 +1138,41 @@ impl App {
|
||||
self.sync_keep_awake();
|
||||
}
|
||||
|
||||
/// Choosing the lid item asks for the administrator password first while the
|
||||
/// helper is missing or from another version; the setting goes on once it is in.
|
||||
/// The lid item is the helper: ticking it installs the helper behind the
|
||||
/// administrator prompt (unless this version's is already in), unticking it removes
|
||||
/// it behind another. The setting changes only once the prompt's command ran, so a
|
||||
/// cancelled or failed prompt leaves the item as it was.
|
||||
fn toggle_keep_awake_lid(&mut self) {
|
||||
let Some(items) = &self.menu.keep_awake else {
|
||||
use keep_awake::HelperState;
|
||||
let Some(wanted) = self
|
||||
.menu
|
||||
.keep_awake
|
||||
.as_ref()
|
||||
.and_then(|items| items.lid.as_ref())
|
||||
.map(|item| item.is_checked())
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let Some(item) = &items.lid else {
|
||||
return;
|
||||
let state =
|
||||
keep_awake::request_path().map_or(HelperState::Missing, |request| keep_awake::helper_state(&request));
|
||||
let prompt = match (wanted, state) {
|
||||
_ if self.lid_helper_busy => None,
|
||||
(true, HelperState::Installed) | (false, HelperState::Missing) => {
|
||||
self.config.keep_awake.lid = wanted;
|
||||
self.config_dirty = true;
|
||||
None
|
||||
}
|
||||
(install, _) => Some(install),
|
||||
};
|
||||
let state = keep_awake::request_path().map_or(keep_awake::HelperState::Missing, |request| {
|
||||
keep_awake::helper_state(&request)
|
||||
});
|
||||
if !item.is_checked() || state == keep_awake::HelperState::Installed {
|
||||
self.config.keep_awake.lid = item.is_checked();
|
||||
self.config_dirty = true;
|
||||
// The click flipped the item already. Until a prompt's command ran it shows
|
||||
// what is true.
|
||||
if let Some(items) = &self.menu.keep_awake {
|
||||
items.show_helper(state, self.config.keep_awake.lid);
|
||||
self.sync_keep_awake();
|
||||
return;
|
||||
}
|
||||
// Not on until the helper is in.
|
||||
item.set_checked(false);
|
||||
self.run_lid_helper(true);
|
||||
match prompt {
|
||||
Some(install) => self.run_lid_helper(install),
|
||||
None => self.sync_keep_awake(),
|
||||
}
|
||||
}
|
||||
|
||||
fn run_lid_helper(&mut self, install: bool) {
|
||||
@@ -1229,7 +1242,6 @@ impl App {
|
||||
match id {
|
||||
keep_awake::AC_ONLY_ID => self.toggle_keep_awake_ac_only(),
|
||||
keep_awake::LID_ID => self.toggle_keep_awake_lid(),
|
||||
keep_awake::REMOVE_HELPER_ID => self.run_lid_helper(false),
|
||||
menu_id::SWITCH => self.show_connect_page(None, true),
|
||||
menu_id::ADD_AGENT => self.add_agent(),
|
||||
menu_id::RELOAD => self.reload_focused(),
|
||||
|
||||
@@ -34,35 +34,25 @@ pub struct AppMenu {
|
||||
pub keep_awake: Option<KeepAwakeMenu>,
|
||||
}
|
||||
|
||||
/// App menu > Keep This Mac Awake.
|
||||
/// App menu > Keep This Mac Awake: Off / While Agents Work / Always, then on a
|
||||
/// MacBook Only When Plugged In and With the Lid Closed…, then one status line.
|
||||
pub struct KeepAwakeMenu {
|
||||
pub modes: Vec<(Mode, CheckMenuItem)>,
|
||||
/// Only on a Mac with a lid: a desktop Mac always runs on mains and cannot close.
|
||||
pub ac_only: Option<CheckMenuItem>,
|
||||
/// Ticked = the helper is in and asked for. Ticking or unticking it asks for the
|
||||
/// administrator password (install, remove), hence the ellipsis.
|
||||
pub lid: Option<CheckMenuItem>,
|
||||
pub remove_helper: Option<MenuItem>,
|
||||
/// What it does right now (keep_awake::status_line). Not clickable.
|
||||
pub status: MenuItem,
|
||||
}
|
||||
|
||||
impl KeepAwakeMenu {
|
||||
/// The lid items as the helper now is. The lid item ends in an ellipsis while
|
||||
/// choosing it asks for the administrator password first.
|
||||
/// The lid item as the helper now is: ticked only while it is installed and on.
|
||||
pub fn show_helper(&self, state: HelperState, lid_on: bool) {
|
||||
if let Some(item) = &self.lid {
|
||||
item.set_text(lid_label(state));
|
||||
item.set_checked(lid_on && state == HelperState::Installed);
|
||||
}
|
||||
if let Some(item) = &self.remove_helper {
|
||||
item.set_enabled(state != HelperState::Missing);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn lid_label(state: HelperState) -> &'static str {
|
||||
match state {
|
||||
HelperState::Installed => "Also With the Lid Closed",
|
||||
_ => "Also With the Lid Closed\u{2026}",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,32 +65,26 @@ fn build_keep_awake(config: &Config) -> muda::Result<(Submenu, KeepAwakeMenu)> {
|
||||
submenu.append(&item)?;
|
||||
modes.push((mode, item));
|
||||
}
|
||||
let (mut ac_only, mut lid, mut remove_helper) = (None, None, None);
|
||||
let (mut ac_only, mut lid) = (None, None);
|
||||
if keep_awake::has_lid() {
|
||||
let state =
|
||||
keep_awake::request_path().map_or(HelperState::Missing, |request| keep_awake::helper_state(&request));
|
||||
let ac = CheckMenuItem::with_id(
|
||||
keep_awake::AC_ONLY_ID,
|
||||
"Only on Power Adapter",
|
||||
"Only When Plugged In",
|
||||
true,
|
||||
settings.ac_only,
|
||||
None,
|
||||
);
|
||||
let lid_item = CheckMenuItem::with_id(
|
||||
keep_awake::LID_ID,
|
||||
lid_label(state),
|
||||
"With the Lid Closed\u{2026}",
|
||||
true,
|
||||
settings.lid && state == HelperState::Installed,
|
||||
None,
|
||||
);
|
||||
let remove = MenuItem::with_id(
|
||||
keep_awake::REMOVE_HELPER_ID,
|
||||
"Remove the Lid Helper\u{2026}",
|
||||
state != HelperState::Missing,
|
||||
None,
|
||||
);
|
||||
submenu.append_items(&[&PredefinedMenuItem::separator(), &ac, &lid_item, &remove])?;
|
||||
(ac_only, lid, remove_helper) = (Some(ac), Some(lid_item), Some(remove));
|
||||
submenu.append_items(&[&PredefinedMenuItem::separator(), &ac, &lid_item])?;
|
||||
(ac_only, lid) = (Some(ac), Some(lid_item));
|
||||
}
|
||||
let initial = match settings.mode {
|
||||
Mode::Off => keep_awake::OFF_TEXT,
|
||||
@@ -114,7 +98,6 @@ fn build_keep_awake(config: &Config) -> muda::Result<(Submenu, KeepAwakeMenu)> {
|
||||
modes,
|
||||
ac_only,
|
||||
lid,
|
||||
remove_helper,
|
||||
status,
|
||||
},
|
||||
))
|
||||
|
||||
Reference in new issue
Block a user