use std::process::{Command, Stdio};
use std::thread;
use crate::models::Track;
pub fn now_playing(track: &Track) {
let title = track.title.clone();
let body = if track.album.is_empty() {
track.artist.clone()
} else {
format!("{} — {}", track.artist, track.album)
};
let _ = thread::Builder::new()
.name("notify".to_string())
.spawn(move || send(&title, &body));
}
#[cfg(target_os = "linux")]
fn send(title: &str, body: &str) {
let _ = Command::new("notify-send")
.args(["-a", "mfe-player", "-c", "player", title, body])
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status();
}
#[cfg(target_os = "macos")]
fn send(title: &str, body: &str) {
let script = "on run argv
display notification (item 2 of argv) with title (item 1 of argv)
end run";
let _ = Command::new("osascript")
.arg("-e")
.arg(script)
.arg("--")
.arg(title)
.arg(body)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status();
}
#[cfg(windows)]
fn send(title: &str, body: &str) {
let script = r#"
$ErrorActionPreference = 'SilentlyContinue'
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
$xml = New-Object Windows.Data.Xml.Dom.XmlDocument
$t = [System.Security.SecurityElement]::Escape($env:MFE_NOTIFY_TITLE)
$b = [System.Security.SecurityElement]::Escape($env:MFE_NOTIFY_BODY)
$xml.LoadXml("<toast><visual><binding template=""ToastGeneric""><text>$t</text><text>$b</text></binding></visual></toast>")
$toast = [Windows.UI.Notifications.ToastNotification]::new($xml)
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier('mfe-player').Show($toast)
"#;
let _ = Command::new("powershell")
.args(["-NoProfile", "-NonInteractive", "-Command", script])
.env("MFE_NOTIFY_TITLE", title)
.env("MFE_NOTIFY_BODY", body)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status();
}
#[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]
fn send(_title: &str, _body: &str) {}