use std::process::Command;
pub fn window(app_name: &str) -> bool {
#[cfg(target_os = "macos")]
{
activate_macos(app_name)
}
#[cfg(target_os = "linux")]
{
activate_linux(app_name)
}
#[cfg(not(any(target_os = "macos", target_os = "linux")))]
{
let _ = app_name;
false
}
}
#[cfg(target_os = "macos")]
fn activate_macos(app_name: &str) -> bool {
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
const ACTIVATE_TIMEOUT: Duration = Duration::from_secs(5);
let script = format!(r#"tell application "{app_name}" to activate"#);
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let result = Command::new("osascript")
.args(["-e", &script])
.status()
.map(|s| s.success())
.unwrap_or(false);
let _ = tx.send(result);
});
rx.recv_timeout(ACTIVATE_TIMEOUT).unwrap_or(false)
}
#[cfg(target_os = "linux")]
fn activate_linux(app_name: &str) -> bool {
if Command::new("wmctrl")
.args(["-a", app_name])
.status()
.map(|s| s.success())
.unwrap_or(false)
{
return true;
}
if Command::new("xdotool")
.args(["search", "--name", app_name, "windowactivate"])
.status()
.map(|s| s.success())
.unwrap_or(false)
{
return true;
}
let sway_cmd = format!(r#"[app_id="{app_name}"] focus"#);
if Command::new("swaymsg")
.arg(&sway_cmd)
.status()
.map(|s| s.success())
.unwrap_or(false)
{
return true;
}
if Command::new("hyprctl")
.args(["dispatch", "focuswindow", app_name])
.status()
.map(|s| s.success())
.unwrap_or(false)
{
return true;
}
false
}
#[cfg(test)]
mod tests {
#[test]
fn test_window_nonexistent_app() {
let result = super::window("definitely-not-a-real-app-12345");
assert!(!result);
}
}