mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-10-28 00:42:08 +08:00
refactor: reduce webview lock contention (#6271)
* refactor: replace handle::Handle::get_window() with WindowManager::get_main_window() in multiple files * refactor: enhance WindowManager to return window state alongside the main window instance * refactor: update useProfiles and ProfilePage to support profile overrides and improve patchProfilesConfig return type * refactor: enhance handle_success to check main window existence before notifying profile changes * refactor: simplify get_main_window_with_state by using pattern matching and improve window state handling * refactor: fix window activation by removing unnecessary reference in activate_existing_main_window * refactor: remove redundant macOS conditional for window_manager import
This commit is contained in:
@@ -59,6 +59,28 @@ fn should_handle_window_operation() -> bool {
|
||||
pub struct WindowManager;
|
||||
|
||||
impl WindowManager {
|
||||
pub fn get_main_window_with_state() -> (Option<WebviewWindow<Wry>>, WindowState) {
|
||||
let Some(window) = Self::get_main_window() else {
|
||||
return (None, WindowState::NotExist);
|
||||
};
|
||||
|
||||
let is_minimized = window.is_minimized().unwrap_or(false);
|
||||
let is_visible = window.is_visible().unwrap_or(false);
|
||||
let is_focused = window.is_focused().unwrap_or(false);
|
||||
|
||||
let state = if is_minimized {
|
||||
WindowState::Minimized
|
||||
} else if !is_visible {
|
||||
WindowState::Hidden
|
||||
} else if is_focused {
|
||||
WindowState::VisibleFocused
|
||||
} else {
|
||||
WindowState::VisibleUnfocused
|
||||
};
|
||||
|
||||
(Some(window), state)
|
||||
}
|
||||
|
||||
pub fn get_main_window_state() -> WindowState {
|
||||
match Self::get_main_window() {
|
||||
Some(window) => {
|
||||
@@ -119,12 +141,12 @@ impl WindowManager {
|
||||
WindowOperationResult::NoAction
|
||||
}
|
||||
WindowState::VisibleUnfocused | WindowState::Minimized | WindowState::Hidden => {
|
||||
if let Some(window) = Self::get_main_window() {
|
||||
let state_after_check = Self::get_main_window_state();
|
||||
if state_after_check == WindowState::VisibleFocused {
|
||||
logging!(info, Type::Window, "窗口在检查期间已变为可见和有焦点状态");
|
||||
return WindowOperationResult::NoAction;
|
||||
}
|
||||
let (window, state_after_check) = Self::get_main_window_with_state();
|
||||
if state_after_check == WindowState::VisibleFocused {
|
||||
logging!(info, Type::Window, "窗口在检查期间已变为可见和有焦点状态");
|
||||
return WindowOperationResult::NoAction;
|
||||
}
|
||||
if let Some(window) = window {
|
||||
Self::activate_window(&window)
|
||||
} else {
|
||||
WindowOperationResult::Failed
|
||||
@@ -135,25 +157,18 @@ impl WindowManager {
|
||||
|
||||
/// 切换主窗口显示状态(显示/隐藏)
|
||||
pub async fn toggle_main_window() -> WindowOperationResult {
|
||||
// 防抖检查
|
||||
if !should_handle_window_operation() {
|
||||
return WindowOperationResult::NoAction;
|
||||
};
|
||||
logging!(info, Type::Window, "开始切换主窗口显示状态");
|
||||
}
|
||||
|
||||
let current_state = Self::get_main_window_state();
|
||||
logging!(
|
||||
info,
|
||||
Type::Window,
|
||||
"当前窗口状态: {:?} | 详细状态: {}",
|
||||
current_state,
|
||||
Self::get_window_status_info()
|
||||
);
|
||||
let (window, state) = Self::get_main_window_with_state();
|
||||
|
||||
match current_state {
|
||||
logging!(debug, Type::Window, "当前状态: {:?}", state);
|
||||
|
||||
match state {
|
||||
WindowState::NotExist => Self::handle_not_exist_toggle().await,
|
||||
WindowState::VisibleFocused | WindowState::VisibleUnfocused => Self::hide_main_window(),
|
||||
WindowState::Minimized | WindowState::Hidden => Self::activate_existing_main_window(),
|
||||
WindowState::VisibleFocused | WindowState::VisibleUnfocused => Self::hide_main_window(window.as_ref()),
|
||||
WindowState::Minimized | WindowState::Hidden => Self::activate_existing_main_window(window.as_ref()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -169,9 +184,9 @@ impl WindowManager {
|
||||
}
|
||||
|
||||
// 隐藏主窗口
|
||||
fn hide_main_window() -> WindowOperationResult {
|
||||
fn hide_main_window(window: Option<&WebviewWindow<Wry>>) -> WindowOperationResult {
|
||||
logging!(info, Type::Window, "窗口可见,将隐藏窗口");
|
||||
if let Some(window) = Self::get_main_window() {
|
||||
if let Some(window) = window {
|
||||
match window.hide() {
|
||||
Ok(_) => {
|
||||
logging!(info, Type::Window, "窗口已成功隐藏");
|
||||
@@ -189,10 +204,10 @@ impl WindowManager {
|
||||
}
|
||||
|
||||
// 激活已存在的主窗口
|
||||
fn activate_existing_main_window() -> WindowOperationResult {
|
||||
fn activate_existing_main_window(window: Option<&WebviewWindow<Wry>>) -> WindowOperationResult {
|
||||
logging!(info, Type::Window, "窗口存在但被隐藏或最小化,将激活窗口");
|
||||
if let Some(window) = Self::get_main_window() {
|
||||
Self::activate_window(&window)
|
||||
if let Some(window) = window {
|
||||
Self::activate_window(window)
|
||||
} else {
|
||||
logging!(warn, Type::Window, "无法获取窗口实例");
|
||||
WindowOperationResult::Failed
|
||||
@@ -255,24 +270,18 @@ impl WindowManager {
|
||||
}
|
||||
|
||||
/// 检查窗口是否可见
|
||||
pub fn is_main_window_visible() -> bool {
|
||||
Self::get_main_window()
|
||||
.map(|window| window.is_visible().unwrap_or(false))
|
||||
.unwrap_or(false)
|
||||
pub fn is_main_window_visible(window: Option<&WebviewWindow<Wry>>) -> bool {
|
||||
window.map(|w| w.is_visible().unwrap_or(false)).unwrap_or(false)
|
||||
}
|
||||
|
||||
/// 检查窗口是否有焦点
|
||||
pub fn is_main_window_focused() -> bool {
|
||||
Self::get_main_window()
|
||||
.map(|window| window.is_focused().unwrap_or(false))
|
||||
.unwrap_or(false)
|
||||
pub fn is_main_window_focused(window: Option<&WebviewWindow<Wry>>) -> bool {
|
||||
window.map(|w| w.is_focused().unwrap_or(false)).unwrap_or(false)
|
||||
}
|
||||
|
||||
/// 检查窗口是否最小化
|
||||
pub fn is_main_window_minimized() -> bool {
|
||||
Self::get_main_window()
|
||||
.map(|window| window.is_minimized().unwrap_or(false))
|
||||
.unwrap_or(false)
|
||||
pub fn is_main_window_minimized(window: Option<&WebviewWindow<Wry>>) -> bool {
|
||||
window.map(|w| w.is_minimized().unwrap_or(false)).unwrap_or(false)
|
||||
}
|
||||
|
||||
/// 创建新窗口,防抖避免重复调用
|
||||
@@ -320,11 +329,11 @@ impl WindowManager {
|
||||
}
|
||||
|
||||
/// 获取详细的窗口状态信息
|
||||
pub fn get_window_status_info() -> String {
|
||||
let state = Self::get_main_window_state();
|
||||
let is_visible = Self::is_main_window_visible();
|
||||
let is_focused = Self::is_main_window_focused();
|
||||
let is_minimized = Self::is_main_window_minimized();
|
||||
fn get_window_status_info() -> String {
|
||||
let (window, state) = Self::get_main_window_with_state();
|
||||
let is_visible = Self::is_main_window_visible(window.as_ref());
|
||||
let is_focused = Self::is_main_window_focused(window.as_ref());
|
||||
let is_minimized = Self::is_main_window_minimized(window.as_ref());
|
||||
|
||||
format!("窗口状态: {state:?} | 可见: {is_visible} | 有焦点: {is_focused} | 最小化: {is_minimized}")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user