use mgui_shell::{Notification, NotificationLevel, NotificationTimeout};
pub(crate) fn show(notification: &Notification, default_app_id: Option<&str>) -> Result<(), String> {
if notification.title.trim().is_empty() {
return Err("通知标题不能为空".to_string());
}
let mut n = notify_rust::Notification::new();
n.summary(¬ification.title);
if !notification.body.is_empty() {
n.body(¬ification.body);
}
if let Some(name) = ¬ification.app_name {
n.appname(name);
}
if let Some(icon) = ¬ification.icon {
n.icon(icon);
}
n.urgency(match notification.level {
NotificationLevel::Low => notify_rust::Urgency::Low,
NotificationLevel::Normal => notify_rust::Urgency::Normal,
NotificationLevel::Critical => notify_rust::Urgency::Critical,
});
match notification.timeout {
NotificationTimeout::Default => {}
NotificationTimeout::Never => {
n.timeout(notify_rust::Timeout::Never);
}
NotificationTimeout::Milliseconds(ms) => {
n.timeout(notify_rust::Timeout::Milliseconds(ms));
}
}
#[cfg(target_os = "windows")]
{
if let Some(app_id) = notification.app_id.as_deref().or(default_app_id) {
n.app_id(app_id);
}
if !notification.silent {
n.sound_name("Default");
}
}
#[cfg(all(unix, not(target_os = "macos")))]
if notification.silent {
n.hint(notify_rust::Hint::SuppressSound(true));
}
#[cfg(not(target_os = "windows"))]
let _ = default_app_id;
n.show().map(|_| ()).map_err(|e| format!("send notification: {e}"))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rejects_empty_title_before_touching_platform() {
let err = show(&Notification::new(" "), None).expect_err("empty title");
assert!(err.contains("标题"));
}
#[test]
#[ignore = "manual: shows a real desktop notification"]
fn manual_notification() {
let n = Notification::new("mgui notification smoke")
.with_body("notify-rust / 平台映射正常")
.with_timeout(NotificationTimeout::Milliseconds(5000));
show(&n, Some("studio.qianbai.mgui.showcase")).expect("show notification");
}
}