feat(widget): add macOS desktop widget

Ship a native WidgetKit extension (routing mode, TUN and system proxy
toggles) as part of every macOS build. The extension talks to the Rust
backend over an App Group Unix socket with mutual code-signing checks,
reuses the backend i18n catalogs, and stays inert on macOS < 14.

A beforeBundle hook builds, localizes and pre-signs the extension before
Tauri signs the host; pnpm build resolves a single local signing identity
automatically and CI keeps using its injected credentials. An Xcode
style-preview host is included for iterating on the widget layout. The
i18n cleanup scanner now also scans Swift sources so widget-only keys
are not flagged unused.

Signed-off-by: Tunglies <77394545+Tunglies@users.noreply.github.com>
This commit is contained in:
Tunglies
2026-09-14 13:59:40 +08:00
parent bfef4e84ea
commit 143ed48aa9
41 changed files with 1666 additions and 7 deletions
+6
View File
@@ -36,10 +36,14 @@ impl Handle {
}
pub fn refresh_clash() {
#[cfg(target_os = "macos")]
clash_verge_widget::reload();
Self::send_event(FrontendEvent::RefreshClash);
}
pub fn refresh_verge() {
#[cfg(target_os = "macos")]
clash_verge_widget::reload();
Self::send_event(FrontendEvent::RefreshVerge);
}
@@ -56,6 +60,8 @@ impl Handle {
/// Sent on every transition, so the frontend does not have to poll to notice that the Core
/// stopped or that the Service came back.
pub fn notify_run_state(state: &crate::core::runstate::RunStateView) {
#[cfg(target_os = "macos")]
clash_verge_widget::reload();
let Ok(state) = serde_json::to_value(state) else {
return;
};
+2
View File
@@ -19,6 +19,8 @@ pub mod timer;
pub mod tray;
pub mod updater;
pub mod validate;
#[cfg(target_os = "macos")]
pub mod widget;
pub mod win_uwp;
pub use self::{manager::CoreManager, timer::Timer, updater::SilentUpdater};
+83
View File
@@ -0,0 +1,83 @@
use crate::{cmd, config::IVerge, core::handle::Handle, feat, process::AsyncHandler};
use clash_verge_widget::{Action, Backend, Mode, Status};
use tauri_plugin_mihomo::models::ClashMode;
struct AppBackend;
impl Backend for AppBackend {
async fn snapshot(&self) -> Status {
let core = Handle::mihomo().get_base_config().await;
let proxy = {
let manual = cmd::network::get_sys_proxy()
.await
.ok()
.and_then(|v| v.get("enable").and_then(|v| v.as_bool()));
let pac = cmd::network::get_auto_proxy()
.await
.ok()
.and_then(|v| v.get("enable").and_then(|v| v.as_bool()));
match (manual, pac) {
(Some(true), _) | (_, Some(true)) => Some(true),
(Some(false), Some(false)) => Some(false),
_ => None,
}
};
let (mode, tun) = match core {
Ok(config) => (
Some(match config.mode {
ClashMode::Rule => Mode::Rule,
ClashMode::Global => Mode::Global,
ClashMode::Direct => Mode::Direct,
}),
Some(config.tun.enable),
),
_ => (None, None),
};
Status {
mode,
tun,
proxy,
error: None,
language: clash_verge_i18n::current_language(
crate::config::Config::verge().await.data_arc().language.as_deref(),
)
.into_owned(),
}
}
async fn language(&self) -> String {
clash_verge_i18n::current_language(crate::config::Config::verge().await.data_arc().language.as_deref())
.into_owned()
}
async fn apply(&self, action: Action) -> Result<(), String> {
if Handle::global().is_exiting() {
return Err("widget.unavailable".into());
}
match action {
Action::Mode(mode) => feat::change_clash_mode(mode.as_str().into())
.await
.map_err(|_| "widget.failed".into()),
Action::Tun(_) | Action::Proxy(_) => {
let payload = IVerge {
enable_tun_mode: match action {
Action::Tun(value) => Some(value),
_ => None,
},
enable_system_proxy: match action {
Action::Proxy(value) => Some(value),
_ => None,
},
..Default::default()
};
cmd::verge::patch_verge_config(payload)
.await
.map_err(|_| "widget.failed".into())
}
}
}
}
pub fn start() {
AsyncHandler::spawn(|| async { clash_verge_widget::start(AppBackend) });
}
+2
View File
@@ -341,6 +341,8 @@ pub fn run() -> std::process::ExitCode {
logging!(info, Type::System, "应用就绪");
crate::utils::server::set_commands_ready();
#[cfg(target_os = "macos")]
crate::core::widget::start();
#[cfg(target_os = "macos")]
if let Some(window) = _app_handle.get_webview_window("main") {