feat: optimize hotkey behavior and window management logic

This commit is contained in:
wonfen 2025-02-26 11:03:50 +08:00
parent e8c1e6f241
commit b6677f0f72
3 changed files with 45 additions and 87 deletions

View File

@ -394,85 +394,4 @@ impl CoreManager {
} }
} }
} }
} }
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use std::path::Path;
async fn create_test_script() -> Result<String> {
let temp_dir = std::env::temp_dir();
let script_path = temp_dir.join("test_script.js");
let script_content = r#"
// This is a test script
function main(config) {
console.log("Testing script");
return config;
}
"#;
fs::write(&script_path, script_content)?;
Ok(script_path.to_string_lossy().to_string())
}
async fn create_invalid_script() -> Result<String> {
let temp_dir = std::env::temp_dir();
let script_path = temp_dir.join("invalid_script.js");
let script_content = r#"
// This is an invalid script
function main(config { // Missing closing parenthesis
console.log("Testing script");
return config;
}
"#;
fs::write(&script_path, script_content)?;
Ok(script_path.to_string_lossy().to_string())
}
async fn create_no_main_script() -> Result<String> {
let temp_dir = std::env::temp_dir();
let script_path = temp_dir.join("no_main_script.js");
let script_content = r#"
// This script has no main function
function helper(config) {
console.log("Testing script");
return config;
}
"#;
fs::write(&script_path, script_content)?;
Ok(script_path.to_string_lossy().to_string())
}
#[tokio::test]
async fn test_validate_script_file() -> Result<()> {
let core_manager = CoreManager::global();
// 测试有效脚本
let script_path = create_test_script().await?;
let result = core_manager.validate_config_file(&script_path).await?;
assert!(result.0, "有效脚本应该通过验证");
// 测试无效脚本
let invalid_script_path = create_invalid_script().await?;
let result = core_manager.validate_config_file(&invalid_script_path).await?;
assert!(!result.0, "无效脚本不应该通过验证");
assert!(result.1.contains("脚本语法错误"), "无效脚本应该返回语法错误");
// 测试缺少main函数的脚本
let no_main_script_path = create_no_main_script().await?;
let result = core_manager.validate_config_file(&no_main_script_path).await?;
assert!(!result.0, "缺少main函数的脚本不应该通过验证");
assert!(result.1.contains("缺少main函数"), "应该提示缺少main函数");
// 清理测试文件
let _ = fs::remove_file(script_path);
let _ = fs::remove_file(invalid_script_path);
let _ = fs::remove_file(no_main_script_path);
Ok(())
}
}

View File

@ -104,9 +104,32 @@ impl Hotkey {
// 使用 spawn_blocking 来确保在正确的线程上执行 // 使用 spawn_blocking 来确保在正确的线程上执行
async_runtime::spawn_blocking(|| { async_runtime::spawn_blocking(|| {
println!("Creating window in spawn_blocking"); println!("Toggle dashboard window visibility");
log::info!(target: "app", "Creating window in spawn_blocking"); log::info!(target: "app", "Toggle dashboard window visibility");
resolve::create_window();
// 检查窗口是否存在
if let Some(window) = handle::Handle::global().get_window() {
// 如果窗口可见,则隐藏它
if window.is_visible().unwrap_or(false) {
println!("Window is visible, hiding it");
log::info!(target: "app", "Window is visible, hiding it");
let _ = window.hide();
} else {
// 如果窗口不可见,则显示它
println!("Window is hidden, showing it");
log::info!(target: "app", "Window is hidden, showing it");
if window.is_minimized().unwrap_or(false) {
let _ = window.unminimize();
}
let _ = window.show();
let _ = window.set_focus();
}
} else {
// 如果窗口不存在,创建一个新窗口
println!("Window does not exist, creating a new one");
log::info!(target: "app", "Window does not exist, creating a new one");
resolve::create_window();
}
}); });
println!("=== Hotkey Dashboard Window Operation End ==="); println!("=== Hotkey Dashboard Window Operation End ===");
@ -146,7 +169,23 @@ impl Hotkey {
// 直接执行函数,不做任何状态检查 // 直接执行函数,不做任何状态检查
println!("Executing function directly"); println!("Executing function directly");
log::info!(target: "app", "Executing function directly"); log::info!(target: "app", "Executing function directly");
f();
// 获取轻量模式状态和全局热键状态
let is_lite_mode = Config::verge().latest().enable_lite_mode.unwrap_or(false);
let is_enable_global_hotkey = Config::verge().latest().enable_global_hotkey.unwrap_or(true);
// 在轻量模式下或配置了全局热键时,始终执行热键功能
if is_lite_mode || is_enable_global_hotkey {
f();
} else if let Some(window) = app_handle.get_webview_window("main") {
// 非轻量模式且未启用全局热键时,只在窗口可见且有焦点的情况下响应热键
let is_visible = window.is_visible().unwrap_or(false);
let is_focused = window.is_focused().unwrap_or(false);
if is_focused && is_visible {
f();
}
}
} }
} }
}); });

View File

@ -16,7 +16,7 @@
"copyright": "GNU General Public License v3.0", "copyright": "GNU General Public License v3.0",
"category": "DeveloperTool", "category": "DeveloperTool",
"shortDescription": "Clash Verge Rev", "shortDescription": "Clash Verge Rev",
"createUpdaterArtifacts": true "createUpdaterArtifacts": "v1Compatible"
}, },
"build": { "build": {
"beforeBuildCommand": "pnpm run web:build", "beforeBuildCommand": "pnpm run web:build",