mgui-shell-winit 0.4.0

winit 适配层:Window → WindowShell / ShellEvent,以及软件回退窗口句柄
Documentation
//! `notify-rust` → 平台无关 `Notification` 的映射。
//!
//! 独立成模块,便于在没有窗口的测试/工具里复用同一套字段映射。

use mgui_shell::{Notification, NotificationLevel, NotificationTimeout};

/// 发送系统通知。`default_app_id` 是 Runner 配置的应用 id(Windows AUMID 兜底)。
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(&notification.title);
    if !notification.body.is_empty() {
        n.body(&notification.body);
    }
    if let Some(name) = &notification.app_name {
        n.appname(name);
    }
    if let Some(icon) = &notification.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")]
    {
        // 未设置 app_id 时 notify-rust 会退化为 PowerShell 来源;
        // 优先使用显式通知 id,其次使用 Runner 注册的应用 id。
        if let Some(app_id) = notification.app_id.as_deref().or(default_app_id) {
            n.app_id(app_id);
        }
        // winrt-notification: None = 静音,Some(Default) = 系统默认提示音。
        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("标题"));
    }

    /// 手动验收:会真的弹出系统通知,因此默认 ignore。
    ///
    /// `cargo test -p mgui-shell-winit -- --ignored manual_notification`
    #[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");
    }
}