refactor(window_manager): simplify window state handling and improve method organization

This commit is contained in:
Tunglies
2025-10-23 16:21:51 +08:00
parent d7859b07a6
commit f061bce2a1

View File

@@ -189,8 +189,14 @@ impl WindowManager {
); );
match current_state { match current_state {
WindowState::NotExist => { 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(),
}
}
// 窗口不存在时创建新窗口
async fn handle_not_exist_toggle() -> WindowOperationResult {
logging!(info, Type::Window, "窗口不存在,将创建新窗口"); logging!(info, Type::Window, "窗口不存在,将创建新窗口");
// 由于已经有防抖保护,直接调用内部方法 // 由于已经有防抖保护,直接调用内部方法
if Self::create_window(true).await { if Self::create_window(true).await {
@@ -199,17 +205,10 @@ impl WindowManager {
WindowOperationResult::Failed WindowOperationResult::Failed
} }
} }
WindowState::VisibleFocused | WindowState::VisibleUnfocused => {
logging!( // 隐藏主窗口
info, fn hide_main_window() -> WindowOperationResult {
Type::Window, logging!(info, Type::Window, "窗口可见,将隐藏窗口");
"窗口可见(焦点状态: {}),将隐藏窗口",
if current_state == WindowState::VisibleFocused {
"有焦点"
} else {
"无焦点"
}
);
if let Some(window) = Self::get_main_window() { if let Some(window) = Self::get_main_window() {
match window.hide() { match window.hide() {
Ok(_) => { Ok(_) => {
@@ -226,7 +225,9 @@ impl WindowManager {
WindowOperationResult::Failed WindowOperationResult::Failed
} }
} }
WindowState::Minimized | WindowState::Hidden => {
// 激活已存在的主窗口
fn activate_existing_main_window() -> WindowOperationResult {
logging!(info, Type::Window, "窗口存在但被隐藏或最小化,将激活窗口"); logging!(info, Type::Window, "窗口存在但被隐藏或最小化,将激活窗口");
if let Some(window) = Self::get_main_window() { if let Some(window) = Self::get_main_window() {
Self::activate_window(&window) Self::activate_window(&window)
@@ -235,8 +236,6 @@ impl WindowManager {
WindowOperationResult::Failed WindowOperationResult::Failed
} }
} }
}
}
/// 激活窗口(取消最小化、显示、设置焦点) /// 激活窗口(取消最小化、显示、设置焦点)
fn activate_window(window: &WebviewWindow<Wry>) -> WindowOperationResult { fn activate_window(window: &WebviewWindow<Wry>) -> WindowOperationResult {