chore: trim excessive comments

This commit is contained in:
wonfen
2026-08-19 07:27:43 +08:00
parent 42b91b6294
commit 807f9a206d
103 changed files with 293 additions and 2517 deletions
+9 -37
View File
@@ -4,17 +4,15 @@ use tauri::webview::PageLoadEvent;
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};
// logging 仅在 macOS 的渲染进程恢复逻辑中使用
#[cfg(target_os = "macos")]
use clash_verge_logging::logging;
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;
@@ -46,7 +44,6 @@ fn restore_default_size_if_needed(window: &WebviewWindow) {
logging_error!(Type::Window, window.center());
}
/// 构建新的 WebView 窗口
pub async fn build_new_window() -> Result<WebviewWindow, String> {
let app_handle = handle::Handle::app_handle();
@@ -112,7 +109,7 @@ pub async fn build_new_window() -> Result<WebviewWindow, String> {
Ok(window) => {
logging_error!(Type::Window, window.set_background_color(Some(background_color)));
restore_default_size_if_needed(&window);
// 全新窗口的页面即为最新状态,丢弃旧窗口遗留的待重载标记,避免多余 reload
// A new page supersedes any reload marker left by the old window.
#[cfg(target_os = "macos")]
take_webview_needs_reload();
Ok(window)
@@ -121,40 +118,19 @@ pub async fn build_new_window() -> Result<WebviewWindow, String> {
}
}
/// 渲染进程死亡、页面待重载标记(macOS)
///
/// 渲染进程在窗口不可见时被系统终止后置位,由下次激活窗口的路径取走并执行 reload。
/// Defers recovery of a terminated hidden main webview until its next activation.
#[cfg(target_os = "macos")]
static WEBVIEW_NEEDS_RELOAD: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
/// 取出并清除"页面待重载"标记
///
/// # Returns
/// * `bool` - 渲染进程是否曾在窗口不可见时被系统终止、页面需要 reload
#[cfg(target_os = "macos")]
pub fn take_webview_needs_reload() -> bool {
WEBVIEW_NEEDS_RELOAD.swap(false, std::sync::atomic::Ordering::SeqCst)
}
/// WebView 渲染进程被系统终止后的恢复处理(macOS)
///
/// macOS 在内存压力下可能杀掉 WKWebView 的 WebContent 渲染进程:
/// 1. 页面内容层消失,窗口打开后表现为白屏;
/// 2. 前端 JS 状态随之丢失,无法调用 `ws_disconnect` 清理 Mihomo WebSocket 订阅,
/// 孤儿订阅持续把大于 1KB 的 payload(如 `/connections` 全量快照)塞进 tauri 的
/// `ChannelDataIpcQueue`,且没有存活的页面来取走,导致主进程内存无限增长。
///
/// 恢复策略:
/// * 窗口可见(前台被杀的罕见场景)——立即 reload 恢复页面;
/// * 窗口隐藏/最小化(托盘常驻的常见场景)——只置位待重载标记,等用户下次打开
/// 窗口时再 reload。系统正是因内存压力才杀掉不可见窗口的渲染进程,此时立即
/// 重建渲染进程既浪费内存,也可能形成"系统杀→拉起→再杀"的循环。
///
/// 注意:应用层注册 `on_web_content_process_terminate` 后会覆盖 tauri-runtime-wry
/// 的默认自动 reload 行为,因此页面死亡状态会一直保持到我们主动 reload。
///
/// # Arguments
/// * `webview` - 渲染进程被终止的 WebView
/// macOS may kill hidden WebContent under memory pressure. Clean orphaned Mihomo subscriptions,
/// reload visible webviews immediately, and defer a hidden main-window reload until activation.
/// Registering this callback replaces Tauri's default automatic reload.
#[cfg(target_os = "macos")]
pub fn on_web_content_process_terminated(webview: &tauri::Webview) {
if handle::Handle::global().is_exiting() {
@@ -171,18 +147,16 @@ pub fn on_web_content_process_terminated(webview: &tauri::Webview) {
let window = webview.window();
let is_user_visible = window.is_visible().unwrap_or(false) && !window.is_minimized().unwrap_or(false);
// 懒重载标记仅供主窗口;其它 webview(update-splash)无消费通道,不可见时直接 reload 兜底
// Only the main window has a path that consumes the deferred marker.
let is_main_window = webview.label() == "main";
let reload_now = is_user_visible || !is_main_window;
if !reload_now {
// 主窗口不可见:置标记,延迟到下次 activate_window / reload_main_window_if_needed 再 reload
WEBVIEW_NEEDS_RELOAD.store(true, std::sync::atomic::Ordering::SeqCst);
logging!(info, Type::Window, "窗口不可见,页面将在下次打开窗口时重载");
}
// 清理全部 Mihomo WS 订阅,阻断 ChannelDataIpcQueue 泄漏(托盘速率任务约 1s 后自重连)。
// reload 必须与清理同任务、排在其后,否则清理可能误清重载后新页面的订阅(竞态)。
// Clean before reload so cleanup cannot remove subscriptions created by the new page.
let webview = webview.clone();
crate::process::AsyncHandler::spawn(move || async move {
if let Err(err) = handle::Handle::mihomo().clear_all_ws_connections().await {
@@ -196,9 +170,7 @@ pub fn on_web_content_process_terminated(webview: &tauri::Webview) {
});
}
/// 消费待重载标记并 reload 主窗口(macOS)。
/// 兜底原生取消最小化(Dock 缩略图/调度中心/窗口菜单)只触发 Focused(true)、不走
/// activate_window 的情况。与 activate_window 共用 swap 标记,先取先得、不重复 reload。
/// Consumes the shared marker for native unminimize paths that bypass `activate_window`.
#[cfg(target_os = "macos")]
pub fn reload_main_window_if_needed() {
if !take_webview_needs_reload() {