Files
clash-proxy/src-tauri/src/utils/notification.rs
oomeow 8c734a5a35 fix: disable tun mode menu on tray when tun mode is unavailable (#4975)
* fix: check if service installed when toggle tun mode on tray

* chore: cargo fmt

* fix: auto disable tun mode

* docs: update UPDATELOG.md

* fix: init Tun mode status

* chore: update

* feat: disable tun mode tray menu when tun mode is unavailable

* fix: restart core when uninstall service is canceled

* chore: remove check notification when toggle tun mode

* chore: fix updatelog

---------

Co-authored-by: Tunglies <77394545+Tunglies@users.noreply.github.com>
2025-10-31 17:31:40 +08:00

75 lines
2.0 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
use crate::{core::handle, utils::i18n::t};
use tauri_plugin_notification::NotificationExt;
pub enum NotificationEvent<'a> {
DashboardToggled,
ClashModeChanged {
mode: &'a str,
},
SystemProxyToggled,
TunModeToggled,
LightweightModeEntered,
AppQuit,
#[cfg(target_os = "macos")]
AppHidden,
}
fn notify(title: &str, body: &str) {
let app_handle = handle::Handle::app_handle();
app_handle
.notification()
.builder()
.title(title)
.body(body)
.show()
.ok();
}
pub async fn notify_event<'a>(event: NotificationEvent<'a>) {
match event {
NotificationEvent::DashboardToggled => {
notify(
&t("DashboardToggledTitle").await,
&t("DashboardToggledBody").await,
);
}
NotificationEvent::ClashModeChanged { mode } => {
notify(
&t("ClashModeChangedTitle").await,
&t_with_args("ClashModeChangedBody", mode).await,
);
}
NotificationEvent::SystemProxyToggled => {
notify(
&t("SystemProxyToggledTitle").await,
&t("SystemProxyToggledBody").await,
);
}
NotificationEvent::TunModeToggled => {
notify(
&t("TunModeToggledTitle").await,
&t("TunModeToggledBody").await,
);
}
NotificationEvent::LightweightModeEntered => {
notify(
&t("LightweightModeEnteredTitle").await,
&t("LightweightModeEnteredBody").await,
);
}
NotificationEvent::AppQuit => {
notify(&t("AppQuitTitle").await, &t("AppQuitBody").await);
}
#[cfg(target_os = "macos")]
NotificationEvent::AppHidden => {
notify(&t("AppHiddenTitle").await, &t("AppHiddenBody").await);
}
}
}
// 辅助函数带参数的i18n
async fn t_with_args(key: &str, mode: &str) -> String {
t(key).await.replace("{mode}", mode)
}