fix(bar): open the macOS settings, Activity Monitor and links with open

The macOS bar's battery click spawned cmd.exe /C start ms-settings:batterysaver
and the update click spawned explorer.exe, neither of which exists on macOS
(the move kept them on purpose, handcheck D.B). Both now go through `open`,
as the CPU, memory and network clicks already did.

The decision which program and arguments a click runs is a pure function,
system_tool_command(&SystemTool) -> (program, args), so the argv is unit
tested: task manager is `open -a 'Activity Monitor'`, the battery is
`open x-apple.systempreferences:com.apple.Battery-Settings.extension` (the
pane id of System Settings in macOS 13 and later, the scheme the network pane
already used; unconfirmed on a real Mac, hand-check row added), a URL is
opened as is, a storage label opens its path. The module is macOS only
(cfg(target_os = "macos") at the crate root), so none of this is built
elsewhere.

BEHAVIOUR (macOS): clicks on the battery and update widgets now open
something. Gate: macOS hand-check rows M1-M3 in docs/dev/macos-handcheck.md and
the unit tests on the Mac (cargo test -p komorebi-bar). Verified: type-check
only on Linux (see the fast-check result in the progress log); tests not run
here.
This commit is contained in:
LGUG2Z committed 2026-11-05 15:03:26 -08:00
1 parent d7673d436f
commit 89f2872609
1 file changed
+83 -21
+83 -21
View File
@@ -10,27 +10,8 @@ pub struct MacBar;
impl BarPlatform for MacBar {
fn open_system_tool(&self, tool: SystemTool) -> io::Result<()> {
match tool {
SystemTool::TaskManager => Command::new("open")
.args(["-a", "Activity Monitor"])
.spawn(),
// cmd.exe does not exist on macOS: this is what the battery widget does today
SystemTool::PowerSettings => Command::new("cmd.exe")
.args(["/C", "start", "ms-settings:batterysaver"])
.spawn(),
SystemTool::NetworkSettings => Command::new("open")
.args(["x-apple.systempreferences:com.apple.Network-Settings.extension"])
.spawn(),
// explorer.exe does not exist on macOS: this is what the update widget does today
SystemTool::Url(url) => Command::new("explorer.exe").args([url]).spawn(),
SystemTool::StorageLocation(label) => {
let parts: Vec<&str> = label.split(' ').collect();
let path = parts[..parts.len() - 1].join(" ");
Command::new("open").args([path]).spawn()
}
}
.map(|_| ())
let (program, args) = system_tool_command(&tool);
Command::new(program).args(args).spawn().map(|_| ())
}
fn latest_release_api_url(&self) -> &'static str {
@@ -62,3 +43,84 @@ impl BarPlatform for MacBar {
None
}
}
/// The program and arguments that open `tool` on macOS: everything goes through `open`.
fn system_tool_command(tool: &SystemTool) -> (&'static str, Vec<String>) {
let args: Vec<String> = match tool {
SystemTool::TaskManager => vec!["-a".into(), "Activity Monitor".into()],
// System Settings of macOS 13 and later (the same scheme as the network pane)
SystemTool::PowerSettings => {
vec!["x-apple.systempreferences:com.apple.Battery-Settings.extension".into()]
}
SystemTool::NetworkSettings => {
vec!["x-apple.systempreferences:com.apple.Network-Settings.extension".into()]
}
SystemTool::Url(url) => vec![url.clone()],
SystemTool::StorageLocation(label) => {
// the label is "<path> <usage>": the path is everything but the last word
let parts: Vec<&str> = label.split(' ').collect();
vec![parts[..parts.len() - 1].join(" ")]
}
};
("open", args)
}
#[cfg(test)]
mod tests {
use super::*;
fn argv(tool: SystemTool) -> Vec<String> {
let (program, args) = system_tool_command(&tool);
assert_eq!(
program, "open",
"nothing on macOS is started by another program"
);
args
}
#[test]
fn the_task_manager_is_activity_monitor() {
assert_eq!(argv(SystemTool::TaskManager), ["-a", "Activity Monitor"]);
}
#[test]
fn the_battery_opens_the_battery_pane_of_system_settings() {
assert_eq!(
argv(SystemTool::PowerSettings),
["x-apple.systempreferences:com.apple.Battery-Settings.extension"]
);
}
#[test]
fn the_network_opens_the_network_pane_of_system_settings() {
assert_eq!(
argv(SystemTool::NetworkSettings),
["x-apple.systempreferences:com.apple.Network-Settings.extension"]
);
}
#[test]
fn a_url_is_opened_in_the_default_browser() {
assert_eq!(
argv(SystemTool::Url("https://example.com/a b".into())),
["https://example.com/a b"]
);
}
#[test]
fn a_storage_label_opens_its_path_without_the_free_space() {
assert_eq!(
argv(SystemTool::StorageLocation("/Volumes/My Disk 43%".into())),
["/Volumes/My Disk"]
);
}
#[test]
fn the_release_page_is_the_one_of_the_mac_repository() {
assert_eq!(
MacBar.release_page_url("0.1.2"),
"https://github.com/LGUG2Z/komorebi-for-mac/releases/tag/v0.1.2"
);
}
}