2025-10-31 17:31:40 +08:00
|
|
|
|
use crate::{core::handle, utils::i18n::t};
|
2025-08-23 00:20:58 +08:00
|
|
|
|
|
2025-06-26 23:09:07 +08:00
|
|
|
|
use tauri_plugin_notification::NotificationExt;
|
|
|
|
|
|
|
|
|
|
|
|
pub enum NotificationEvent<'a> {
|
|
|
|
|
|
DashboardToggled,
|
|
|
|
|
|
ClashModeChanged {
|
|
|
|
|
|
mode: &'a str,
|
|
|
|
|
|
},
|
|
|
|
|
|
SystemProxyToggled,
|
|
|
|
|
|
TunModeToggled,
|
|
|
|
|
|
LightweightModeEntered,
|
|
|
|
|
|
AppQuit,
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
|
AppHidden,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-31 17:31:40 +08:00
|
|
|
|
fn notify(title: &str, body: &str) {
|
|
|
|
|
|
let app_handle = handle::Handle::app_handle();
|
|
|
|
|
|
app_handle
|
|
|
|
|
|
.notification()
|
2025-06-26 23:09:07 +08:00
|
|
|
|
.builder()
|
|
|
|
|
|
.title(title)
|
|
|
|
|
|
.body(body)
|
|
|
|
|
|
.show()
|
|
|
|
|
|
.ok();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-31 17:31:40 +08:00
|
|
|
|
pub async fn notify_event<'a>(event: NotificationEvent<'a>) {
|
2025-06-26 23:09:07 +08:00
|
|
|
|
match event {
|
|
|
|
|
|
NotificationEvent::DashboardToggled => {
|
2025-08-26 01:49:51 +08:00
|
|
|
|
notify(
|
|
|
|
|
|
&t("DashboardToggledTitle").await,
|
|
|
|
|
|
&t("DashboardToggledBody").await,
|
|
|
|
|
|
);
|
2025-06-26 23:09:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
NotificationEvent::ClashModeChanged { mode } => {
|
|
|
|
|
|
notify(
|
2025-08-26 01:49:51 +08:00
|
|
|
|
&t("ClashModeChangedTitle").await,
|
|
|
|
|
|
&t_with_args("ClashModeChangedBody", mode).await,
|
2025-06-26 23:09:07 +08:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
NotificationEvent::SystemProxyToggled => {
|
|
|
|
|
|
notify(
|
2025-08-26 01:49:51 +08:00
|
|
|
|
&t("SystemProxyToggledTitle").await,
|
|
|
|
|
|
&t("SystemProxyToggledBody").await,
|
2025-06-26 23:09:07 +08:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
NotificationEvent::TunModeToggled => {
|
2025-08-26 01:49:51 +08:00
|
|
|
|
notify(
|
|
|
|
|
|
&t("TunModeToggledTitle").await,
|
|
|
|
|
|
&t("TunModeToggledBody").await,
|
|
|
|
|
|
);
|
2025-06-26 23:09:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
NotificationEvent::LightweightModeEntered => {
|
|
|
|
|
|
notify(
|
2025-08-26 01:49:51 +08:00
|
|
|
|
&t("LightweightModeEnteredTitle").await,
|
|
|
|
|
|
&t("LightweightModeEnteredBody").await,
|
2025-06-26 23:09:07 +08:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
NotificationEvent::AppQuit => {
|
2025-10-31 17:31:40 +08:00
|
|
|
|
notify(&t("AppQuitTitle").await, &t("AppQuitBody").await);
|
2025-06-26 23:09:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
|
NotificationEvent::AppHidden => {
|
2025-10-31 17:31:40 +08:00
|
|
|
|
notify(&t("AppHiddenTitle").await, &t("AppHiddenBody").await);
|
2025-06-26 23:09:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 辅助函数,带参数的i18n
|
2025-08-26 01:49:51 +08:00
|
|
|
|
async fn t_with_args(key: &str, mode: &str) -> String {
|
|
|
|
|
|
t(key).await.replace("{mode}", mode)
|
2025-06-26 23:09:07 +08:00
|
|
|
|
}
|