Skip to content

Commit 9964859

Browse files
committed
refactor(windows_utils): 移除 WebView2 检测功能,简化启动错误处理 (close #37)
1 parent 8cd0888 commit 9964859

3 files changed

Lines changed: 37 additions & 122 deletions

File tree

src-tauri/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ objc2 = { version = "0.6", features = ["relax-sign-encoding"] }
7878
[target.'cfg(target_os = "windows")'.dependencies]
7979
windows-sys = { version = "0.59", features = [
8080
"Win32_UI_WindowsAndMessaging",
81-
"Win32_System_Registry",
8281
"Win32_Foundation",
8382
] }
8483

src-tauri/src/lib.rs

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,6 @@ pub fn run() {
4848
)
4949
.init();
5050

51-
// On Windows, verify that WebView2 Runtime is installed before proceeding.
52-
// Without WebView2 the app window would start hidden and never become visible
53-
// because the frontend JS (which calls `show()`) cannot load.
54-
#[cfg(target_os = "windows")]
55-
{
56-
if !windows_utils::is_webview2_installed() {
57-
tracing::error!("WebView2 Runtime not found — cannot start AQBot");
58-
let user_ok = windows_utils::show_warning_ok_cancel(
59-
"AQBot",
60-
"未检测到 Microsoft Edge WebView2 Runtime,AQBot 无法启动。\n\n\
61-
点击「确定」打开下载页面进行安装,安装完成后重新启动 AQBot。",
62-
);
63-
if user_ok {
64-
let _ = std::process::Command::new("cmd")
65-
.args(["/c", "start", "https://developer.microsoft.com/en-us/microsoft-edge/webview2/?form=MA13LH#download"])
66-
.spawn();
67-
}
68-
std::process::exit(1);
69-
}
70-
}
71-
7251
#[allow(unused_mut)]
7352
let mut builder = tauri::Builder::default()
7453
.plugin(tauri_plugin_opener::init())
@@ -88,7 +67,7 @@ pub fn run() {
8867
builder = builder.plugin(tauri_plugin_mcp_bridge::init());
8968
}
9069

91-
builder
70+
let build_result = builder
9271
.invoke_handler(tauri::generate_handler![
9372
// providers
9473
commands::providers::list_providers,
@@ -655,9 +634,41 @@ pub fn run() {
655634
}
656635
}
657636
})
658-
.build(tauri::generate_context!())
659-
.expect("error while building tauri application")
660-
.run(|app, event| {
637+
.build(tauri::generate_context!());
638+
639+
let app = match build_result {
640+
Ok(app) => app,
641+
Err(e) => {
642+
let error_msg = e.to_string();
643+
tracing::error!("Failed to build Tauri application: {}", error_msg);
644+
645+
#[cfg(target_os = "windows")]
646+
{
647+
let lower = error_msg.to_lowercase();
648+
if lower.contains("webview2") || lower.contains("webview") || lower.contains("edge") {
649+
let user_ok = windows_utils::show_warning_ok_cancel(
650+
"AQBot",
651+
"未检测到 Microsoft Edge WebView2 Runtime,AQBot 无法启动。\n\n\
652+
点击「确定」打开下载页面进行安装,安装完成后重新启动 AQBot。",
653+
);
654+
if user_ok {
655+
let _ = std::process::Command::new("cmd")
656+
.args(["/c", "start", "https://developer.microsoft.com/en-us/microsoft-edge/webview2/?form=MA13LH#download"])
657+
.spawn();
658+
}
659+
} else {
660+
windows_utils::show_error_dialog(
661+
"AQBot",
662+
&format!("应用启动失败:{}", error_msg),
663+
);
664+
}
665+
}
666+
667+
std::process::exit(1);
668+
}
669+
};
670+
671+
app.run(|app, event| {
661672
#[cfg(target_os = "macos")]
662673
if let tauri::RunEvent::Reopen { has_visible_windows, .. } = event {
663674
if !has_visible_windows {

src-tauri/src/windows_utils.rs

Lines changed: 1 addition & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Windows-specific utilities: native error dialogs and WebView2 detection.
1+
//! Windows-specific utilities: native error dialogs for fatal startup failures.
22
33
use windows_sys::Win32::Foundation::HWND;
44
use windows_sys::Win32::UI::WindowsAndMessaging::{
@@ -39,98 +39,3 @@ pub fn show_warning_ok_cancel(title: &str, message: &str) -> bool {
3939
};
4040
result == IDOK
4141
}
42-
43-
/// Check whether the WebView2 Runtime is installed by querying the registry.
44-
///
45-
/// Checks both per-machine and per-user install locations. Returns `true` if
46-
/// a version string (the `pv` value) is found and is not empty, meaning at
47-
/// least one WebView2 distribution is present.
48-
pub fn is_webview2_installed() -> bool {
49-
use windows_sys::Win32::System::Registry::{HKEY, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE};
50-
51-
const WEBVIEW2_GUID: &str = "{F3017226-FE2A-4295-8BEE-154267D3BAFE}";
52-
53-
let sub_keys: &[(HKEY, &str)] = &[
54-
(
55-
HKEY_LOCAL_MACHINE,
56-
&format!(
57-
"SOFTWARE\\Microsoft\\EdgeUpdate\\Clients\\{}",
58-
WEBVIEW2_GUID
59-
),
60-
),
61-
(
62-
HKEY_LOCAL_MACHINE,
63-
&format!(
64-
"SOFTWARE\\WOW6432Node\\Microsoft\\EdgeUpdate\\Clients\\{}",
65-
WEBVIEW2_GUID
66-
),
67-
),
68-
(
69-
HKEY_CURRENT_USER,
70-
&format!(
71-
"Software\\Microsoft\\EdgeUpdate\\Clients\\{}",
72-
WEBVIEW2_GUID
73-
),
74-
),
75-
];
76-
77-
for (root, sub_key) in sub_keys {
78-
if read_registry_string(*root, sub_key, "pv")
79-
.map(|v| !v.is_empty())
80-
.unwrap_or(false)
81-
{
82-
return true;
83-
}
84-
}
85-
false
86-
}
87-
88-
/// Read a REG_SZ value from the Windows registry. Returns `None` on any failure.
89-
fn read_registry_string(
90-
root: windows_sys::Win32::System::Registry::HKEY,
91-
sub_key: &str,
92-
value_name: &str,
93-
) -> Option<String> {
94-
use windows_sys::Win32::System::Registry::*;
95-
96-
let wide_sub_key = to_wide(sub_key);
97-
let wide_value = to_wide(value_name);
98-
99-
let mut hkey: HKEY = std::ptr::null_mut();
100-
let status =
101-
unsafe { RegOpenKeyExW(root, wide_sub_key.as_ptr(), 0, KEY_READ, &mut hkey) };
102-
if status != 0 {
103-
return None;
104-
}
105-
106-
let mut buf = [0u16; 256];
107-
let mut buf_len = (buf.len() * 2) as u32;
108-
let mut reg_type: u32 = 0;
109-
110-
let status = unsafe {
111-
RegQueryValueExW(
112-
hkey,
113-
wide_value.as_ptr(),
114-
std::ptr::null(),
115-
&mut reg_type,
116-
buf.as_mut_ptr() as *mut u8,
117-
&mut buf_len,
118-
)
119-
};
120-
121-
unsafe { RegCloseKey(hkey) };
122-
123-
if status != 0 || reg_type != REG_SZ {
124-
return None;
125-
}
126-
127-
let char_count = (buf_len as usize) / 2;
128-
let s = String::from_utf16_lossy(
129-
&buf[..char_count]
130-
.iter()
131-
.copied()
132-
.take_while(|&c| c != 0)
133-
.collect::<Vec<u16>>(),
134-
);
135-
Some(s)
136-
}

0 commit comments

Comments
 (0)