mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-04-13 05:20:28 +08:00
- Remove Rust-side `eval(INITIAL_LOADING_OVERLAY)` that prematurely dismissed the overlay before React/MUI theme was ready - Defer `window.show()` from Rust `activate_window` to an inline `<script>` in index.html, executed after the themed overlay is in DOM - Remove `useAppInitialization` hook (duplicate of `useLoadingOverlay` with no themeReady gate) - Simplify overlay to pure theme-colored background — no spinner or loading text — so fast startup feels instant - Simplify `hideInitialOverlay` API and reduce overlay fade to 0.2s - Clean up unused CSS variables (spinner-track, spinner-top, etc.)
86 lines
2.8 KiB
Rust
86 lines
2.8 KiB
Rust
use dark_light::{Mode as SystemTheme, detect as detect_system_theme};
|
|
use tauri::utils::config::Color;
|
|
use tauri::{Theme, WebviewWindow};
|
|
|
|
use crate::{config::Config, core::handle, utils::resolve::window_script::build_window_initial_script};
|
|
use clash_verge_logging::{Type, logging_error};
|
|
|
|
const DARK_BACKGROUND_COLOR: Color = Color(46, 48, 61, 255); // #2E303D
|
|
const LIGHT_BACKGROUND_COLOR: Color = Color(245, 245, 245, 255); // #F5F5F5
|
|
const DARK_BACKGROUND_HEX: &str = "#2E303D";
|
|
const LIGHT_BACKGROUND_HEX: &str = "#F5F5F5";
|
|
|
|
// 定义默认窗口尺寸常量
|
|
const DEFAULT_WIDTH: f64 = 940.0;
|
|
const DEFAULT_HEIGHT: f64 = 700.0;
|
|
|
|
const MINIMAL_WIDTH: f64 = 520.0;
|
|
const MINIMAL_HEIGHT: f64 = 520.0;
|
|
|
|
#[cfg(target_os = "linux")]
|
|
const DEFAULT_DECORATIONS: bool = false;
|
|
#[cfg(not(target_os = "linux"))]
|
|
const DEFAULT_DECORATIONS: bool = true;
|
|
|
|
/// 构建新的 WebView 窗口
|
|
pub async fn build_new_window() -> Result<WebviewWindow, String> {
|
|
let app_handle = handle::Handle::app_handle();
|
|
|
|
let config = Config::verge().await;
|
|
let latest = config.latest_arc();
|
|
let start_page = latest.start_page.as_deref().unwrap_or("/");
|
|
let initial_theme_mode = match latest.theme_mode.as_deref() {
|
|
Some("dark") => "dark",
|
|
Some("light") => "light",
|
|
_ => "system",
|
|
};
|
|
|
|
let resolved_theme = match initial_theme_mode {
|
|
"dark" => Some(Theme::Dark),
|
|
"light" => Some(Theme::Light),
|
|
_ => None,
|
|
};
|
|
|
|
let prefers_dark_background = match resolved_theme {
|
|
Some(Theme::Dark) => true,
|
|
Some(Theme::Light) => false,
|
|
_ => !matches!(detect_system_theme().ok(), Some(SystemTheme::Light)),
|
|
};
|
|
|
|
let background_color = if prefers_dark_background {
|
|
DARK_BACKGROUND_COLOR
|
|
} else {
|
|
LIGHT_BACKGROUND_COLOR
|
|
};
|
|
|
|
let initial_script = build_window_initial_script(initial_theme_mode, DARK_BACKGROUND_HEX, LIGHT_BACKGROUND_HEX);
|
|
|
|
let mut builder = tauri::WebviewWindowBuilder::new(
|
|
app_handle,
|
|
"main", /* the unique window label */
|
|
tauri::WebviewUrl::App(start_page.into()),
|
|
)
|
|
.title("Clash Verge")
|
|
.center()
|
|
.decorations(DEFAULT_DECORATIONS)
|
|
.fullscreen(false)
|
|
.inner_size(DEFAULT_WIDTH, DEFAULT_HEIGHT)
|
|
.min_inner_size(MINIMAL_WIDTH, MINIMAL_HEIGHT)
|
|
.visible(false) // 等待主题色准备好后再展示,避免启动色差
|
|
.initialization_script(&initial_script);
|
|
|
|
if let Some(theme) = resolved_theme {
|
|
builder = builder.theme(Some(theme));
|
|
}
|
|
|
|
builder = builder.background_color(background_color);
|
|
|
|
match builder.build() {
|
|
Ok(window) => {
|
|
logging_error!(Type::Window, window.set_background_color(Some(background_color)));
|
|
Ok(window)
|
|
}
|
|
Err(e) => Err(e.to_string()),
|
|
}
|
|
}
|