* 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>
75 lines
2.0 KiB
Rust
75 lines
2.0 KiB
Rust
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)
|
||
}
|