2025-08-26 01:49:51 +08:00
|
|
|
|
use crate::utils::i18n::t;
|
2025-08-23 00:20:58 +08:00
|
|
|
|
|
2025-06-26 23:09:07 +08:00
|
|
|
|
use tauri::AppHandle;
|
|
|
|
|
|
use tauri_plugin_notification::NotificationExt;
|
|
|
|
|
|
|
|
|
|
|
|
pub enum NotificationEvent<'a> {
|
|
|
|
|
|
DashboardToggled,
|
|
|
|
|
|
ClashModeChanged {
|
|
|
|
|
|
mode: &'a str,
|
|
|
|
|
|
},
|
|
|
|
|
|
SystemProxyToggled,
|
|
|
|
|
|
TunModeToggled,
|
|
|
|
|
|
LightweightModeEntered,
|
|
|
|
|
|
AppQuit,
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
|
AppHidden,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
|
fn notify(app: &AppHandle, title: &str, body: &str) {
|
2025-06-26 23:09:07 +08:00
|
|
|
|
app.notification()
|
|
|
|
|
|
.builder()
|
|
|
|
|
|
.title(title)
|
|
|
|
|
|
.body(body)
|
|
|
|
|
|
.show()
|
|
|
|
|
|
.ok();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
|
pub async fn notify_event<'a>(app: AppHandle, event: NotificationEvent<'a>) {
|
2025-06-26 23:09:07 +08:00
|
|
|
|
match event {
|
|
|
|
|
|
NotificationEvent::DashboardToggled => {
|
2025-08-26 01:49:51 +08:00
|
|
|
|
notify(
|
|
|
|
|
|
&app,
|
|
|
|
|
|
&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
|
|
|
|
&app,
|
|
|
|
|
|
&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
|
|
|
|
&app,
|
|
|
|
|
|
&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(
|
|
|
|
|
|
&app,
|
|
|
|
|
|
&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
|
|
|
|
&app,
|
|
|
|
|
|
&t("LightweightModeEnteredTitle").await,
|
|
|
|
|
|
&t("LightweightModeEnteredBody").await,
|
2025-06-26 23:09:07 +08:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
NotificationEvent::AppQuit => {
|
2025-08-26 01:49:51 +08:00
|
|
|
|
notify(&app, &t("AppQuitTitle").await, &t("AppQuitBody").await);
|
2025-06-26 23:09:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
|
NotificationEvent::AppHidden => {
|
2025-08-26 01:49:51 +08:00
|
|
|
|
notify(&app, &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
|
|
|
|
}
|