fix(theme/windows): switch to dark-light based theme detection

This commit is contained in:
Slinetrac
2025-12-25 14:43:27 +08:00
parent 712b8ff19b
commit 1c044f053f
4 changed files with 110 additions and 19 deletions

View File

@@ -22,6 +22,8 @@ use clash_verge_signal;
pub mod dns;
pub mod scheme;
#[cfg(target_os = "windows")]
pub mod theme;
pub mod ui;
pub mod window;
pub mod window_script;
@@ -64,6 +66,8 @@ pub fn resolve_setup_async() {
init_verge_config().await;
Config::verify_config_initialization().await;
init_window().await;
#[cfg(target_os = "windows")]
theme::start_windows_app_theme_watcher();
let core_init = AsyncHandler::spawn(|| async {
init_service_manager().await;

View File

@@ -0,0 +1,55 @@
//! Windows app theme watcher.
//!
//! NOTE:
//! Tauri's theme API is unreliable on Windows and may miss or delay
//! system theme change events. As a workaround, we poll the system
//! theme via the `dark-light` crate and emit a custom
//! `verge://app-theme-changed` event to keep the frontend in sync.
//!
//! Windows-only, best-effort.
use std::time::Duration;
use dark_light::{Mode as SystemTheme, detect as detect_system_theme};
use tauri::Emitter as _;
use crate::{core::handle, process::AsyncHandler};
const APP_THEME_EVENT: &str = "verge://app-theme-changed";
fn resolve_apps_theme_mode() -> Option<&'static str> {
match detect_system_theme().ok()? {
SystemTheme::Dark => Some("dark"),
SystemTheme::Light => Some("light"),
SystemTheme::Unspecified => None,
}
}
pub fn start_windows_app_theme_watcher() {
AsyncHandler::spawn(|| async move {
let app_handle = handle::Handle::app_handle().clone();
let mut last_theme = resolve_apps_theme_mode();
if let Some(theme) = last_theme {
let _ = app_handle.emit(APP_THEME_EVENT, theme);
}
loop {
if handle::Handle::global().is_exiting() {
break;
}
tokio::time::sleep(Duration::from_millis(500)).await;
let Some(theme) = resolve_apps_theme_mode() else {
continue;
};
if last_theme.as_ref() == Some(&theme) {
continue;
}
last_theme = Some(theme);
let _ = app_handle.emit(APP_THEME_EVENT, theme);
}
});
}