mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 17:15:38 +08:00
Refactor logging macros to remove print control parameter
- Updated logging macros to eliminate the boolean parameter for print control, simplifying the logging calls throughout the codebase. - Adjusted all logging calls in various modules (lib.rs, lightweight.rs, help.rs, init.rs, logging.rs, resolve/mod.rs, resolve/scheme.rs, resolve/ui.rs, resolve/window.rs, server.rs, singleton.rs, window_manager.rs) to reflect the new macro structure. - Ensured consistent logging behavior across the application by standardizing the logging format.
This commit is contained in:
@@ -42,7 +42,7 @@ pub async fn read_mapping(path: &PathBuf) -> Result<Mapping> {
|
||||
}
|
||||
Err(err) => {
|
||||
let error_msg = format!("YAML syntax error in {}: {}", path.display(), err);
|
||||
logging!(error, Type::Config, true, "{}", error_msg);
|
||||
logging!(error, Type::Config, "{}", error_msg);
|
||||
|
||||
crate::core::handle::Handle::notice_message(
|
||||
"config_validate::yaml_syntax_error",
|
||||
|
||||
@@ -136,13 +136,7 @@ pub async fn delete_log() -> Result<()> {
|
||||
_ => return Ok(()),
|
||||
};
|
||||
|
||||
logging!(
|
||||
info,
|
||||
Type::Setup,
|
||||
true,
|
||||
"try to delete log files, day: {}",
|
||||
day
|
||||
);
|
||||
logging!(info, Type::Setup, "try to delete log files, day: {}", day);
|
||||
|
||||
// %Y-%m-%d to NaiveDateTime
|
||||
let parse_time_str = |s: &str| {
|
||||
@@ -177,7 +171,7 @@ pub async fn delete_log() -> Result<()> {
|
||||
if duration.num_days() > day {
|
||||
let file_path = file.path();
|
||||
let _ = fs::remove_file(file_path).await;
|
||||
logging!(info, Type::Setup, true, "delete log file: {}", file_name);
|
||||
logging!(info, Type::Setup, "delete log file: {}", file_name);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
@@ -304,7 +298,7 @@ async fn init_dns_config() -> Result<()> {
|
||||
let dns_path = app_dir.join("dns_config.yaml");
|
||||
|
||||
if !dns_path.exists() {
|
||||
logging!(info, Type::Setup, true, "Creating default DNS config file");
|
||||
logging!(info, Type::Setup, "Creating default DNS config file");
|
||||
help::save_yaml(
|
||||
&dns_path,
|
||||
&default_dns_config,
|
||||
@@ -329,14 +323,7 @@ async fn ensure_directories() -> Result<()> {
|
||||
fs::create_dir_all(&dir).await.map_err(|e| {
|
||||
anyhow::anyhow!("Failed to create {} directory {:?}: {}", name, dir, e)
|
||||
})?;
|
||||
logging!(
|
||||
info,
|
||||
Type::Setup,
|
||||
true,
|
||||
"Created {} directory: {:?}",
|
||||
name,
|
||||
dir
|
||||
);
|
||||
logging!(info, Type::Setup, "Created {} directory: {:?}", name, dir);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -352,13 +339,7 @@ async fn initialize_config_files() -> Result<()> {
|
||||
help::save_yaml(&path, &template, Some("# Clash Verge"))
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("Failed to create clash config: {}", e))?;
|
||||
logging!(
|
||||
info,
|
||||
Type::Setup,
|
||||
true,
|
||||
"Created clash config at {:?}",
|
||||
path
|
||||
);
|
||||
logging!(info, Type::Setup, "Created clash config at {:?}", path);
|
||||
}
|
||||
|
||||
if let Ok(path) = dirs::verge_path()
|
||||
@@ -368,13 +349,7 @@ async fn initialize_config_files() -> Result<()> {
|
||||
help::save_yaml(&path, &template, Some("# Clash Verge"))
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("Failed to create verge config: {}", e))?;
|
||||
logging!(
|
||||
info,
|
||||
Type::Setup,
|
||||
true,
|
||||
"Created verge config at {:?}",
|
||||
path
|
||||
);
|
||||
logging!(info, Type::Setup, "Created verge config at {:?}", path);
|
||||
}
|
||||
|
||||
if let Ok(path) = dirs::profiles_path()
|
||||
@@ -384,13 +359,7 @@ async fn initialize_config_files() -> Result<()> {
|
||||
help::save_yaml(&path, &template, Some("# Clash Verge"))
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("Failed to create profiles config: {}", e))?;
|
||||
logging!(
|
||||
info,
|
||||
Type::Setup,
|
||||
true,
|
||||
"Created profiles config at {:?}",
|
||||
path
|
||||
);
|
||||
logging!(info, Type::Setup, "Created profiles config at {:?}", path);
|
||||
}
|
||||
|
||||
// 验证并修正verge配置
|
||||
@@ -418,19 +387,13 @@ pub async fn init_config() -> Result<()> {
|
||||
|
||||
AsyncHandler::spawn(|| async {
|
||||
if let Err(e) = delete_log().await {
|
||||
logging!(warn, Type::Setup, true, "Failed to clean old logs: {}", e);
|
||||
logging!(warn, Type::Setup, "Failed to clean old logs: {}", e);
|
||||
}
|
||||
logging!(info, Type::Setup, true, "后台日志清理任务完成");
|
||||
logging!(info, Type::Setup, "后台日志清理任务完成");
|
||||
});
|
||||
|
||||
if let Err(e) = init_dns_config().await {
|
||||
logging!(
|
||||
warn,
|
||||
Type::Setup,
|
||||
true,
|
||||
"DNS config initialization failed: {}",
|
||||
e
|
||||
);
|
||||
logging!(warn, Type::Setup, "DNS config initialization failed: {}", e);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -460,13 +423,12 @@ pub async fn init_resources() -> Result<()> {
|
||||
let handle_copy = |src: PathBuf, dest: PathBuf, file: String| async move {
|
||||
match fs::copy(&src, &dest).await {
|
||||
Ok(_) => {
|
||||
logging!(debug, Type::Setup, true, "resources copied '{}'", file);
|
||||
logging!(debug, Type::Setup, "resources copied '{}'", file);
|
||||
}
|
||||
Err(err) => {
|
||||
logging!(
|
||||
error,
|
||||
Type::Setup,
|
||||
true,
|
||||
"failed to copy resources '{}' to '{:?}', {}",
|
||||
file,
|
||||
dest,
|
||||
@@ -491,13 +453,7 @@ pub async fn init_resources() -> Result<()> {
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
logging!(
|
||||
debug,
|
||||
Type::Setup,
|
||||
true,
|
||||
"failed to get modified '{}'",
|
||||
file
|
||||
);
|
||||
logging!(debug, Type::Setup, "failed to get modified '{}'", file);
|
||||
handle_copy(src_path.clone(), dest_path.clone(), file.to_string()).await;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -113,18 +113,6 @@ macro_rules! wrap_err {
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! logging {
|
||||
// 带 println 的版本(支持格式化参数)
|
||||
($level:ident, $type:expr, true, $($arg:tt)*) => {
|
||||
// We dont need println here anymore
|
||||
// println!("{} {}", $type, format_args!($($arg)*));
|
||||
log::$level!(target: "app", "{} {}", $type, format_args!($($arg)*));
|
||||
};
|
||||
|
||||
// 带 println 的版本(使用 false 明确不打印)
|
||||
($level:ident, $type:expr, false, $($arg:tt)*) => {
|
||||
log::$level!(target: "app", "{} {}", $type, format_args!($($arg)*));
|
||||
};
|
||||
|
||||
// 不带 print 参数的版本(默认不打印)
|
||||
($level:ident, $type:expr, $($arg:tt)*) => {
|
||||
log::$level!(target: "app", "{} {}", $type, format_args!($($arg)*));
|
||||
@@ -133,37 +121,16 @@ macro_rules! logging {
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! logging_error {
|
||||
// 1. 处理 Result<T, E>,带打印控制
|
||||
($type:expr, $print:expr, $expr:expr) => {
|
||||
match $expr {
|
||||
Ok(_) => {},
|
||||
Err(err) => {
|
||||
if $print {
|
||||
println!("[{}] Error: {}", $type, err);
|
||||
}
|
||||
log::error!(target: "app", "[{}] {}", $type, err);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 2. 处理 Result<T, E>,默认不打印
|
||||
// Handle Result<T, E>
|
||||
($type:expr, $expr:expr) => {
|
||||
if let Err(err) = $expr {
|
||||
log::error!(target: "app", "[{}] {}", $type, err);
|
||||
}
|
||||
};
|
||||
|
||||
// 3. 处理格式化字符串,带打印控制
|
||||
($type:expr, $print:expr, $fmt:literal $(, $arg:expr)*) => {
|
||||
if $print {
|
||||
println!("[{}] {}", $type, format_args!($fmt $(, $arg)*));
|
||||
}
|
||||
log::error!(target: "app", "[{}] {}", $type, format_args!($fmt $(, $arg)*));
|
||||
};
|
||||
|
||||
// 4. 处理格式化字符串,不带 bool 时,默认 `false`
|
||||
// Handle formatted message: always print to stdout and log as error
|
||||
($type:expr, $fmt:literal $(, $arg:expr)*) => {
|
||||
logging_error!($type, false, $fmt $(, $arg)*);
|
||||
log::error!(target: "app", "[{}] {}", $type, format_args!($fmt $(, $arg)*));
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -33,7 +33,6 @@ pub fn resolve_setup_async() {
|
||||
logging!(
|
||||
info,
|
||||
Type::Setup,
|
||||
true,
|
||||
"开始执行异步设置任务... 线程ID: {:?}",
|
||||
std::thread::current().id()
|
||||
);
|
||||
@@ -44,7 +43,6 @@ pub fn resolve_setup_async() {
|
||||
logging!(
|
||||
info,
|
||||
Type::ClashVergeRev,
|
||||
true,
|
||||
"Version: {}",
|
||||
env!("CARGO_PKG_VERSION")
|
||||
);
|
||||
@@ -82,38 +80,26 @@ pub fn resolve_setup_async() {
|
||||
});
|
||||
|
||||
let elapsed = start_time.elapsed();
|
||||
logging!(
|
||||
info,
|
||||
Type::Setup,
|
||||
true,
|
||||
"异步设置任务完成,耗时: {:?}",
|
||||
elapsed
|
||||
);
|
||||
logging!(info, Type::Setup, "异步设置任务完成,耗时: {:?}", elapsed);
|
||||
|
||||
if elapsed.as_secs() > 10 {
|
||||
logging!(
|
||||
warn,
|
||||
Type::Setup,
|
||||
true,
|
||||
"异步设置任务耗时较长({:?})",
|
||||
elapsed
|
||||
);
|
||||
logging!(warn, Type::Setup, "异步设置任务耗时较长({:?})", elapsed);
|
||||
}
|
||||
}
|
||||
|
||||
// 其它辅助函数不变
|
||||
pub async fn resolve_reset_async() -> Result<(), anyhow::Error> {
|
||||
logging!(info, Type::Tray, true, "Resetting system proxy");
|
||||
logging!(info, Type::Tray, "Resetting system proxy");
|
||||
sysopt::Sysopt::global().reset_sysproxy().await?;
|
||||
|
||||
logging!(info, Type::Core, true, "Stopping core service");
|
||||
logging!(info, Type::Core, "Stopping core service");
|
||||
CoreManager::global().stop_core().await?;
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
use dns::restore_public_dns;
|
||||
|
||||
logging!(info, Type::System, true, "Restoring system DNS settings");
|
||||
logging!(info, Type::System, "Restoring system DNS settings");
|
||||
restore_public_dns().await;
|
||||
}
|
||||
|
||||
@@ -121,157 +107,116 @@ pub async fn resolve_reset_async() -> Result<(), anyhow::Error> {
|
||||
}
|
||||
|
||||
pub fn init_handle() {
|
||||
logging!(info, Type::Setup, true, "Initializing app handle...");
|
||||
logging!(info, Type::Setup, "Initializing app handle...");
|
||||
handle::Handle::global().init();
|
||||
}
|
||||
|
||||
pub(super) fn init_scheme() {
|
||||
logging!(info, Type::Setup, true, "Initializing custom URL scheme");
|
||||
logging_error!(Type::Setup, true, init::init_scheme());
|
||||
logging!(info, Type::Setup, "Initializing custom URL scheme");
|
||||
logging_error!(Type::Setup, init::init_scheme());
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "tauri-dev"))]
|
||||
pub(super) async fn resolve_setup_logger() {
|
||||
logging!(info, Type::Setup, true, "Initializing global logger...");
|
||||
logging_error!(Type::Setup, true, init::init_logger().await);
|
||||
logging!(info, Type::Setup, "Initializing global logger...");
|
||||
logging_error!(Type::Setup, init::init_logger().await);
|
||||
}
|
||||
|
||||
pub async fn resolve_scheme(param: String) -> Result<()> {
|
||||
logging!(
|
||||
info,
|
||||
Type::Setup,
|
||||
true,
|
||||
"Resolving scheme for param: {}",
|
||||
param
|
||||
);
|
||||
logging_error!(Type::Setup, true, scheme::resolve_scheme(param).await);
|
||||
logging!(info, Type::Setup, "Resolving scheme for param: {}", param);
|
||||
logging_error!(Type::Setup, scheme::resolve_scheme(param).await);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn init_embed_server() {
|
||||
logging!(info, Type::Setup, true, "Initializing embedded server...");
|
||||
logging!(info, Type::Setup, "Initializing embedded server...");
|
||||
server::embed_server();
|
||||
}
|
||||
pub(super) async fn init_resources() {
|
||||
logging!(info, Type::Setup, true, "Initializing resources...");
|
||||
logging_error!(Type::Setup, true, init::init_resources().await);
|
||||
logging!(info, Type::Setup, "Initializing resources...");
|
||||
logging_error!(Type::Setup, init::init_resources().await);
|
||||
}
|
||||
|
||||
pub(super) async fn init_startup_script() {
|
||||
logging!(info, Type::Setup, true, "Initializing startup script");
|
||||
logging_error!(Type::Setup, true, init::startup_script().await);
|
||||
logging!(info, Type::Setup, "Initializing startup script");
|
||||
logging_error!(Type::Setup, init::startup_script().await);
|
||||
}
|
||||
|
||||
pub(super) async fn init_timer() {
|
||||
logging!(info, Type::Setup, true, "Initializing timer...");
|
||||
logging_error!(Type::Setup, true, Timer::global().init().await);
|
||||
logging!(info, Type::Setup, "Initializing timer...");
|
||||
logging_error!(Type::Setup, Timer::global().init().await);
|
||||
}
|
||||
|
||||
pub(super) async fn init_hotkey() {
|
||||
logging!(info, Type::Setup, true, "Initializing hotkey...");
|
||||
logging_error!(Type::Setup, true, Hotkey::global().init().await);
|
||||
logging!(info, Type::Setup, "Initializing hotkey...");
|
||||
logging_error!(Type::Setup, Hotkey::global().init().await);
|
||||
}
|
||||
|
||||
pub(super) async fn init_once_auto_lightweight() {
|
||||
logging!(
|
||||
info,
|
||||
Type::Lightweight,
|
||||
true,
|
||||
"Running auto lightweight mode check..."
|
||||
);
|
||||
run_once_auto_lightweight().await;
|
||||
}
|
||||
|
||||
pub(super) async fn init_auto_lightweight_mode() {
|
||||
logging!(
|
||||
info,
|
||||
Type::Setup,
|
||||
true,
|
||||
"Initializing auto lightweight mode..."
|
||||
);
|
||||
logging_error!(Type::Setup, true, auto_lightweight_mode_init().await);
|
||||
logging!(info, Type::Setup, "Initializing auto lightweight mode...");
|
||||
logging_error!(Type::Setup, auto_lightweight_mode_init().await);
|
||||
}
|
||||
|
||||
pub async fn init_work_config() {
|
||||
logging!(
|
||||
info,
|
||||
Type::Setup,
|
||||
true,
|
||||
"Initializing work configuration..."
|
||||
);
|
||||
logging_error!(Type::Setup, true, init::init_config().await);
|
||||
logging!(info, Type::Setup, "Initializing work configuration...");
|
||||
logging_error!(Type::Setup, init::init_config().await);
|
||||
}
|
||||
|
||||
pub(super) async fn init_tray() {
|
||||
// Check if tray should be disabled via environment variable
|
||||
if std::env::var("CLASH_VERGE_DISABLE_TRAY").unwrap_or_default() == "1" {
|
||||
logging!(
|
||||
info,
|
||||
Type::Setup,
|
||||
true,
|
||||
"System tray disabled via --no-tray flag"
|
||||
);
|
||||
logging!(info, Type::Setup, "System tray disabled via --no-tray flag");
|
||||
return;
|
||||
}
|
||||
|
||||
logging!(info, Type::Setup, true, "Initializing system tray...");
|
||||
logging_error!(Type::Setup, true, Tray::global().init().await);
|
||||
logging!(info, Type::Setup, "Initializing system tray...");
|
||||
logging_error!(Type::Setup, Tray::global().init().await);
|
||||
}
|
||||
|
||||
pub(super) async fn init_verge_config() {
|
||||
logging!(
|
||||
info,
|
||||
Type::Setup,
|
||||
true,
|
||||
"Initializing verge configuration..."
|
||||
);
|
||||
logging_error!(Type::Setup, true, Config::init_config().await);
|
||||
logging!(info, Type::Setup, "Initializing verge configuration...");
|
||||
logging_error!(Type::Setup, Config::init_config().await);
|
||||
}
|
||||
|
||||
pub(super) async fn init_service_manager() {
|
||||
logging!(info, Type::Setup, true, "Initializing service manager...");
|
||||
logging_error!(
|
||||
Type::Setup,
|
||||
true,
|
||||
SERVICE_MANAGER.lock().await.refresh().await
|
||||
);
|
||||
logging!(info, Type::Setup, "Initializing service manager...");
|
||||
logging_error!(Type::Setup, SERVICE_MANAGER.lock().await.refresh().await);
|
||||
}
|
||||
|
||||
pub(super) async fn init_core_manager() {
|
||||
logging!(info, Type::Setup, true, "Initializing core manager...");
|
||||
logging_error!(Type::Setup, true, CoreManager::global().init().await);
|
||||
logging!(info, Type::Setup, "Initializing core manager...");
|
||||
logging_error!(Type::Setup, CoreManager::global().init().await);
|
||||
}
|
||||
|
||||
pub(super) async fn init_system_proxy() {
|
||||
logging!(info, Type::Setup, true, "Initializing system proxy...");
|
||||
logging!(info, Type::Setup, "Initializing system proxy...");
|
||||
logging_error!(
|
||||
Type::Setup,
|
||||
true,
|
||||
sysopt::Sysopt::global().update_sysproxy().await
|
||||
);
|
||||
}
|
||||
|
||||
pub(super) fn init_system_proxy_guard() {
|
||||
logging!(
|
||||
info,
|
||||
Type::Setup,
|
||||
true,
|
||||
"Initializing system proxy guard..."
|
||||
);
|
||||
logging_error!(
|
||||
Type::Setup,
|
||||
true,
|
||||
sysopt::Sysopt::global().init_guard_sysproxy()
|
||||
);
|
||||
logging!(info, Type::Setup, "Initializing system proxy guard...");
|
||||
logging_error!(Type::Setup, sysopt::Sysopt::global().init_guard_sysproxy());
|
||||
}
|
||||
|
||||
pub(super) async fn refresh_tray_menu() {
|
||||
logging!(info, Type::Setup, true, "Refreshing tray menu...");
|
||||
logging_error!(Type::Setup, true, Tray::global().update_part().await);
|
||||
logging!(info, Type::Setup, "Refreshing tray menu...");
|
||||
logging_error!(Type::Setup, Tray::global().update_part().await);
|
||||
}
|
||||
|
||||
pub(super) async fn init_window() {
|
||||
logging!(info, Type::Setup, true, "Initializing main window...");
|
||||
logging!(info, Type::Setup, "Initializing main window...");
|
||||
let is_silent_start =
|
||||
{ Config::verge().await.latest_ref().enable_silent_start }.unwrap_or(false);
|
||||
#[cfg(target_os = "macos")]
|
||||
|
||||
@@ -49,7 +49,7 @@ pub(super) async fn resolve_scheme(param: String) -> Result<()> {
|
||||
let uid = match item.uid.clone() {
|
||||
Some(uid) => uid,
|
||||
None => {
|
||||
logging!(error, Type::Config, true, "Profile item missing UID");
|
||||
logging!(error, Type::Config, "Profile item missing UID");
|
||||
handle::Handle::notice_message(
|
||||
"import_sub_url::error",
|
||||
"Profile item missing UID".to_string(),
|
||||
|
||||
@@ -66,7 +66,7 @@ pub fn update_ui_ready_stage(stage: UiReadyStage) {
|
||||
// 标记UI已准备就绪
|
||||
pub fn mark_ui_ready() {
|
||||
get_ui_ready().store(true, Ordering::Release);
|
||||
logging!(info, Type::Window, true, "UI已标记为完全就绪");
|
||||
logging!(info, Type::Window, "UI已标记为完全就绪");
|
||||
|
||||
// 通知所有等待的任务
|
||||
get_ui_ready_notify().notify_waiters();
|
||||
|
||||
@@ -37,7 +37,7 @@ pub fn build_new_window() -> Result<WebviewWindow, String> {
|
||||
.build()
|
||||
{
|
||||
Ok(window) => {
|
||||
logging_error!(Type::Window, true, window.eval(INITIAL_LOADING_OVERLAY));
|
||||
logging_error!(Type::Window, window.eval(INITIAL_LOADING_OVERLAY));
|
||||
Ok(window)
|
||||
}
|
||||
Err(e) => Err(e.to_string()),
|
||||
|
||||
@@ -95,7 +95,7 @@ pub fn embed_server() {
|
||||
// Spawn async work in a fire-and-forget manner
|
||||
let param = query.param.clone();
|
||||
tokio::task::spawn_local(async move {
|
||||
logging_error!(Type::Setup, true, resolve::resolve_scheme(param).await);
|
||||
logging_error!(Type::Setup, resolve::resolve_scheme(param).await);
|
||||
});
|
||||
warp::reply::with_status("ok".to_string(), warp::http::StatusCode::OK)
|
||||
});
|
||||
|
||||
@@ -50,7 +50,6 @@ macro_rules! singleton_with_logging {
|
||||
$crate::logging!(
|
||||
info,
|
||||
$crate::utils::logging::Type::Setup,
|
||||
true,
|
||||
concat!($struct_name_str, " initialized")
|
||||
);
|
||||
instance
|
||||
@@ -88,7 +87,6 @@ macro_rules! singleton_lazy_with_logging {
|
||||
$crate::logging!(
|
||||
info,
|
||||
$crate::utils::logging::Type::Setup,
|
||||
true,
|
||||
concat!($struct_name_str, " initialized")
|
||||
);
|
||||
instance
|
||||
|
||||
@@ -131,43 +131,32 @@ impl WindowManager {
|
||||
finish_window_operation();
|
||||
});
|
||||
|
||||
logging!(info, Type::Window, true, "开始智能显示主窗口");
|
||||
logging!(
|
||||
debug,
|
||||
Type::Window,
|
||||
true,
|
||||
"{}",
|
||||
Self::get_window_status_info()
|
||||
);
|
||||
logging!(info, Type::Window, "开始智能显示主窗口");
|
||||
logging!(debug, Type::Window, "{}", Self::get_window_status_info());
|
||||
|
||||
let current_state = Self::get_main_window_state();
|
||||
|
||||
match current_state {
|
||||
WindowState::NotExist => {
|
||||
logging!(info, Type::Window, true, "窗口不存在,创建新窗口");
|
||||
logging!(info, Type::Window, "窗口不存在,创建新窗口");
|
||||
if Self::create_window(true).await {
|
||||
logging!(info, Type::Window, true, "窗口创建成功");
|
||||
logging!(info, Type::Window, "窗口创建成功");
|
||||
std::thread::sleep(std::time::Duration::from_millis(100));
|
||||
WindowOperationResult::Created
|
||||
} else {
|
||||
logging!(warn, Type::Window, true, "窗口创建失败");
|
||||
logging!(warn, Type::Window, "窗口创建失败");
|
||||
WindowOperationResult::Failed
|
||||
}
|
||||
}
|
||||
WindowState::VisibleFocused => {
|
||||
logging!(info, Type::Window, true, "窗口已经可见且有焦点,无需操作");
|
||||
logging!(info, Type::Window, "窗口已经可见且有焦点,无需操作");
|
||||
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,
|
||||
true,
|
||||
"窗口在检查期间已变为可见和有焦点状态"
|
||||
);
|
||||
logging!(info, Type::Window, "窗口在检查期间已变为可见和有焦点状态");
|
||||
return WindowOperationResult::NoAction;
|
||||
}
|
||||
Self::activate_window(&window)
|
||||
@@ -188,13 +177,12 @@ impl WindowManager {
|
||||
finish_window_operation();
|
||||
});
|
||||
|
||||
logging!(info, Type::Window, true, "开始切换主窗口显示状态");
|
||||
logging!(info, Type::Window, "开始切换主窗口显示状态");
|
||||
|
||||
let current_state = Self::get_main_window_state();
|
||||
logging!(
|
||||
info,
|
||||
Type::Window,
|
||||
true,
|
||||
"当前窗口状态: {:?} | 详细状态: {}",
|
||||
current_state,
|
||||
Self::get_window_status_info()
|
||||
@@ -203,7 +191,7 @@ impl WindowManager {
|
||||
match current_state {
|
||||
WindowState::NotExist => {
|
||||
// 窗口不存在,创建新窗口
|
||||
logging!(info, Type::Window, true, "窗口不存在,将创建新窗口");
|
||||
logging!(info, Type::Window, "窗口不存在,将创建新窗口");
|
||||
// 由于已经有防抖保护,直接调用内部方法
|
||||
if Self::create_window(true).await {
|
||||
WindowOperationResult::Created
|
||||
@@ -215,7 +203,6 @@ impl WindowManager {
|
||||
logging!(
|
||||
info,
|
||||
Type::Window,
|
||||
true,
|
||||
"窗口可见(焦点状态: {}),将隐藏窗口",
|
||||
if current_state == WindowState::VisibleFocused {
|
||||
"有焦点"
|
||||
@@ -226,30 +213,25 @@ impl WindowManager {
|
||||
if let Some(window) = Self::get_main_window() {
|
||||
match window.hide() {
|
||||
Ok(_) => {
|
||||
logging!(info, Type::Window, true, "窗口已成功隐藏");
|
||||
logging!(info, Type::Window, "窗口已成功隐藏");
|
||||
WindowOperationResult::Hidden
|
||||
}
|
||||
Err(e) => {
|
||||
logging!(warn, Type::Window, true, "隐藏窗口失败: {}", e);
|
||||
logging!(warn, Type::Window, "隐藏窗口失败: {}", e);
|
||||
WindowOperationResult::Failed
|
||||
}
|
||||
}
|
||||
} else {
|
||||
logging!(warn, Type::Window, true, "无法获取窗口实例");
|
||||
logging!(warn, Type::Window, "无法获取窗口实例");
|
||||
WindowOperationResult::Failed
|
||||
}
|
||||
}
|
||||
WindowState::Minimized | WindowState::Hidden => {
|
||||
logging!(
|
||||
info,
|
||||
Type::Window,
|
||||
true,
|
||||
"窗口存在但被隐藏或最小化,将激活窗口"
|
||||
);
|
||||
logging!(info, Type::Window, "窗口存在但被隐藏或最小化,将激活窗口");
|
||||
if let Some(window) = Self::get_main_window() {
|
||||
Self::activate_window(&window)
|
||||
} else {
|
||||
logging!(warn, Type::Window, true, "无法获取窗口实例");
|
||||
logging!(warn, Type::Window, "无法获取窗口实例");
|
||||
WindowOperationResult::Failed
|
||||
}
|
||||
}
|
||||
@@ -258,35 +240,35 @@ impl WindowManager {
|
||||
|
||||
/// 激活窗口(取消最小化、显示、设置焦点)
|
||||
fn activate_window(window: &WebviewWindow<Wry>) -> WindowOperationResult {
|
||||
logging!(info, Type::Window, true, "开始激活窗口");
|
||||
logging!(info, Type::Window, "开始激活窗口");
|
||||
|
||||
let mut operations_successful = true;
|
||||
|
||||
// 1. 如果窗口最小化,先取消最小化
|
||||
if window.is_minimized().unwrap_or(false) {
|
||||
logging!(info, Type::Window, true, "窗口已最小化,正在取消最小化");
|
||||
logging!(info, Type::Window, "窗口已最小化,正在取消最小化");
|
||||
if let Err(e) = window.unminimize() {
|
||||
logging!(warn, Type::Window, true, "取消最小化失败: {}", e);
|
||||
logging!(warn, Type::Window, "取消最小化失败: {}", e);
|
||||
operations_successful = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 显示窗口
|
||||
if let Err(e) = window.show() {
|
||||
logging!(warn, Type::Window, true, "显示窗口失败: {}", e);
|
||||
logging!(warn, Type::Window, "显示窗口失败: {}", e);
|
||||
operations_successful = false;
|
||||
}
|
||||
|
||||
// 3. 设置焦点
|
||||
if let Err(e) = window.set_focus() {
|
||||
logging!(warn, Type::Window, true, "设置窗口焦点失败: {}", e);
|
||||
logging!(warn, Type::Window, "设置窗口焦点失败: {}", e);
|
||||
operations_successful = false;
|
||||
}
|
||||
|
||||
// 4. 平台特定的激活策略
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
logging!(info, Type::Window, true, "应用 macOS 特定的激活策略");
|
||||
logging!(info, Type::Window, "应用 macOS 特定的激活策略");
|
||||
handle::Handle::global().set_activation_policy_regular();
|
||||
}
|
||||
|
||||
@@ -294,31 +276,19 @@ impl WindowManager {
|
||||
{
|
||||
// Windows 尝试额外的激活方法
|
||||
if let Err(e) = window.set_always_on_top(true) {
|
||||
logging!(
|
||||
debug,
|
||||
Type::Window,
|
||||
true,
|
||||
"设置置顶失败(非关键错误): {}",
|
||||
e
|
||||
);
|
||||
logging!(debug, Type::Window, "设置置顶失败(非关键错误): {}", e);
|
||||
}
|
||||
// 立即取消置顶
|
||||
if let Err(e) = window.set_always_on_top(false) {
|
||||
logging!(
|
||||
debug,
|
||||
Type::Window,
|
||||
true,
|
||||
"取消置顶失败(非关键错误): {}",
|
||||
e
|
||||
);
|
||||
logging!(debug, Type::Window, "取消置顶失败(非关键错误): {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
if operations_successful {
|
||||
logging!(info, Type::Window, true, "窗口激活成功");
|
||||
logging!(info, Type::Window, "窗口激活成功");
|
||||
WindowOperationResult::Shown
|
||||
} else {
|
||||
logging!(warn, Type::Window, true, "窗口激活部分失败");
|
||||
logging!(warn, Type::Window, "窗口激活部分失败");
|
||||
WindowOperationResult::Failed
|
||||
}
|
||||
}
|
||||
@@ -350,7 +320,6 @@ impl WindowManager {
|
||||
logging!(
|
||||
info,
|
||||
Type::Window,
|
||||
true,
|
||||
"开始创建/显示主窗口, is_show={}",
|
||||
is_show
|
||||
);
|
||||
@@ -361,10 +330,10 @@ impl WindowManager {
|
||||
|
||||
match build_new_window() {
|
||||
Ok(_) => {
|
||||
logging!(info, Type::Window, true, "新窗口创建成功");
|
||||
logging!(info, Type::Window, "新窗口创建成功");
|
||||
}
|
||||
Err(e) => {
|
||||
logging!(error, Type::Window, true, "新窗口创建失败: {}", e);
|
||||
logging!(error, Type::Window, "新窗口创建失败: {}", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -383,15 +352,15 @@ impl WindowManager {
|
||||
pub fn destroy_main_window() -> WindowOperationResult {
|
||||
if let Some(window) = Self::get_main_window() {
|
||||
let _ = window.destroy();
|
||||
logging!(info, Type::Window, true, "窗口已摧毁");
|
||||
logging!(info, Type::Window, "窗口已摧毁");
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
logging!(info, Type::Window, true, "应用 macOS 特定的激活策略");
|
||||
logging!(info, Type::Window, "应用 macOS 特定的激活策略");
|
||||
handle::Handle::global().set_activation_policy_accessory();
|
||||
}
|
||||
return WindowOperationResult::Destroyed;
|
||||
}
|
||||
logging!(warn, Type::Window, true, "窗口摧毁失败");
|
||||
logging!(warn, Type::Window, "窗口摧毁失败");
|
||||
WindowOperationResult::Failed
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user