fix: update main window creation logic with improved logging and UI readiness handling

This commit is contained in:
wonfen
2025-05-16 17:22:53 +08:00
parent 9d89eeb974
commit 32008b9364

View File

@@ -302,8 +302,8 @@ pub fn create_window(is_show: bool) -> bool {
.visible(false) .visible(false)
.build() .build()
{ {
Ok(_) => { Ok(newly_created_window) => {
logging!(info, Type::Window, true, "主窗口创建成功"); logging!(debug, Type::Window, true, "主窗口实例创建成功");
*creating = (false, Instant::now()); *creating = (false, Instant::now());
@@ -311,18 +311,47 @@ pub fn create_window(is_show: bool) -> bool {
AsyncHandler::spawn(move || async move { AsyncHandler::spawn(move || async move {
handle::Handle::global().mark_startup_completed(); handle::Handle::global().mark_startup_completed();
logging!(info, Type::Window, true, "标记启动完成"); logging!(
debug,
if let Some(app_handle) = handle::Handle::global().app_handle() { Type::Window,
if let Some(window) = app_handle.get_webview_window("main") { true,
let _ = window.emit("verge://startup-completed", ()); "异步窗口任务开始 (启动已标记完成)"
logging!(info, Type::Window, true, "已发送启动完成事件"); );
if is_show { if is_show {
let window_clone = window.clone(); let window_clone = newly_created_window.clone();
let timeout_seconds = // Attempt to show and focus the window first.
if crate::module::lightweight::is_in_lightweight_mode() { let _ = window_clone.show();
let _ = window_clone.set_focus();
logging!(debug, Type::Window, true, "窗口已尝试显示和聚焦");
tokio::time::sleep(Duration::from_millis(100)).await; // Crucial delay
logging!(
debug,
Type::Window,
true,
"延时后,尝试发送 verge://startup-completed 事件"
);
if let Err(e) = newly_created_window.emit("verge://startup-completed", ()) {
logging!(
error,
Type::Window,
true,
"发送 verge://startup-completed 事件失败: {}",
e
);
} else {
logging!(
info,
Type::Window,
true,
"已发送 verge://startup-completed 事件"
);
}
let timeout_seconds = if crate::module::lightweight::is_in_lightweight_mode() {
2 2
} else { } else {
5 5
@@ -332,27 +361,13 @@ pub fn create_window(is_show: bool) -> bool {
info, info,
Type::Window, Type::Window,
true, true,
"等待UI就绪最多{}秒", "等待UI就绪 (最多{}秒)...",
timeout_seconds timeout_seconds
); );
let wait_result = let wait_result =
tokio::time::timeout(Duration::from_secs(timeout_seconds), async { tokio::time::timeout(Duration::from_secs(timeout_seconds), async {
let mut check_count = 0;
while !*get_ui_ready().read() { while !*get_ui_ready().read() {
check_count += 1;
if check_count % 10 == 0 {
let state = get_ui_ready_state();
let stage = *state.stage.read();
logging!(
info,
Type::Window,
true,
"等待UI就绪中... 当前阶段: {:?}, 已等待: {}ms",
stage,
check_count * 100
);
}
tokio::time::sleep(Duration::from_millis(100)).await; tokio::time::sleep(Duration::from_millis(100)).await;
} }
}) })
@@ -360,33 +375,50 @@ pub fn create_window(is_show: bool) -> bool {
match wait_result { match wait_result {
Ok(_) => { Ok(_) => {
logging!(info, Type::Window, true, "UI就绪,显示窗口"); logging!(info, Type::Window, true, "UI就绪");
} }
Err(_) => { Err(_) => {
logging!( logging!(
warn, warn,
Type::Window, Type::Window,
true, true,
"等待UI就绪超时({}秒),强制显示窗口", "等待UI就绪超时({}秒),强制标记就绪",
timeout_seconds timeout_seconds
); );
*get_ui_ready().write() = true; *get_ui_ready().write() = true;
} }
} }
logging!(info, Type::Window, true, "窗口显示流程完成");
let _ = window_clone.show(); } else {
let _ = window_clone.set_focus(); logging!(
debug,
logging!(info, Type::Window, true, "窗口创建和显示流程已完成"); Type::Window,
} true,
"is_show为false窗口保持隐藏。尝试发送启动事件。"
);
if let Err(e) = newly_created_window.emit("verge://startup-completed", ()) {
logging!(
warn,
Type::Window,
true,
"发送 verge://startup-completed 事件失败 (is_show=false, 窗口隐藏): {}",
e
);
} else {
logging!(
debug,
Type::Window,
true,
"已发送 verge://startup-completed 事件 (is_show=false, 窗口隐藏)"
);
} }
} }
}); });
true true
} }
Err(e) => { Err(e) => {
logging!(error, Type::Window, true, "Failed to create window: {}", e); logging!(error, Type::Window, true, "主窗口构建失败: {}", e);
*creating = (false, Instant::now()); // Reset the creating state if window creation failed
false false
} }
} }