Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src-tauri/src/commands/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use log::info;
use std::sync::atomic::{AtomicBool, Ordering};

use super::types::SystemInfo;
use super::types::WebView2DirInfo;
use super::utils::get_maafw_dir;

/// 标记是否检测到可能缺少 VC++ 运行库
Expand Down Expand Up @@ -722,3 +723,20 @@ pub fn get_system_info() -> SystemInfo {
tauri_version,
}
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
}

/// 获取当前使用的 WebView2 目录
#[tauri::command]
pub fn get_webview2_dir() -> WebView2DirInfo {
if let Ok(folder) = std::env::var("WEBVIEW2_BROWSER_EXECUTABLE_FOLDER") {
WebView2DirInfo {
path: folder,
system: false,
}
} else {
// 没有设置自定义目录,使用系统 WebView2
WebView2DirInfo {
path: String::new(),
system: true,
}
}
}
7 changes: 7 additions & 0 deletions src-tauri/src/commands/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,10 @@ pub struct GitHubRelease {
pub prerelease: bool,
pub assets: Vec<GitHubAsset>,
}

/// WebView2 目录信息
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebView2DirInfo {
pub path: String,
pub system: bool,
}
1 change: 1 addition & 0 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ pub fn run() {
commands::system::get_arch,
commands::system::get_os,
commands::system::get_system_info,
commands::system::get_webview2_dir,
// 托盘相关命令
commands::tray::set_minimize_to_tray,
commands::tray::get_minimize_to_tray,
Expand Down
18 changes: 17 additions & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,26 @@ fn main() {
// 确保目录存在
let _ = std::fs::create_dir_all(&webview_data_dir);
std::env::set_var("WEBVIEW2_USER_DATA_FOLDER", &webview_data_dir);

// 检测已缓存的 WebView2 固定版本运行时
// 验证目录包含关键文件以确保运行时完整可用
if let Ok(webview2_runtime_dir) = webview2::get_webview2_runtime_dir() {
if webview2_runtime_dir.is_dir()
&& webview2_runtime_dir.join("msedgewebview2.exe").exists()
{
std::env::set_var(
"WEBVIEW2_BROWSER_EXECUTABLE_FOLDER",
&webview2_runtime_dir,
);
}
}
}
}

if !webview2::ensure_webview2() {
// 已有本地运行时时跳过检测,否则检测系统安装或自动下载
if std::env::var_os("WEBVIEW2_BROWSER_EXECUTABLE_FOLDER").is_none()
&& !webview2::ensure_webview2()
{
std::process::exit(1);
}

Expand Down
41 changes: 36 additions & 5 deletions src-tauri/src/webview2/dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ struct DialogState {
status_hwnd: Option<HWND>,
button_hwnd: Option<HWND>,
hfont: Option<HGDIOBJ>,
dialog_type: Option<DialogType>,
}

impl DialogState {
fn clear(&mut self) {
self.progress_hwnd = None;
self.status_hwnd = None;
self.button_hwnd = None;
self.dialog_type = None;
}
}

Expand Down Expand Up @@ -90,7 +92,17 @@ unsafe extern "system" fn dialog_wnd_proc(
}
LRESULT(0)
}
WM_DIALOG_CLOSE | WM_CLOSE => {
WM_CLOSE => {
// 用户点击 X 关闭窗口:进度对话框直接退出进程(此时 Tauri 尚未启动)
DIALOG_STATE.with(|s| {
if s.borrow().dialog_type == Some(DialogType::Progress) {
std::process::exit(0);
}
});
PostQuitMessage(0);
LRESULT(0)
}
WM_DIALOG_CLOSE => {
PostQuitMessage(0);
LRESULT(0)
}
Expand Down Expand Up @@ -216,23 +228,38 @@ impl CustomDialog {
RegisterClassW(&wc);

let title_wide = to_wide(&title_owned);
let wnd_style = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU;
// width/height represent desired client area; compute actual window size
let mut rc = windows::Win32::Foundation::RECT {
left: 0,
top: 0,
right: width,
bottom: height,
};
let success = AdjustWindowRect(&mut rc, wnd_style, false).is_ok();
let (wnd_w, wnd_h) = if success {
(rc.right - rc.left, rc.bottom - rc.top)
} else {
(width, height)
};

let hwnd = CreateWindowExW(
WINDOW_EX_STYLE::default(),
PCWSTR::from_raw(class_name.as_ptr()),
PCWSTR::from_raw(title_wide.as_ptr()),
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU,
wnd_style,
CW_USEDEFAULT,
CW_USEDEFAULT,
width,
height,
wnd_w,
wnd_h,
None,
None,
hinstance,
None,
)
.unwrap_or_default();

center_window(hwnd, width, height);
center_window(hwnd, wnd_w, wnd_h);

const MARGIN: i32 = 24;
const BTN_W: i32 = 96;
Expand Down Expand Up @@ -279,6 +306,7 @@ impl CustomDialog {
let mut g = s.borrow_mut();
g.status_hwnd = Some(status_hwnd);
g.progress_hwnd = Some(progressbar_hwnd);
g.dialog_type = Some(dialog_type);
});
}
DialogType::Success | DialogType::Error => {
Expand Down Expand Up @@ -380,6 +408,9 @@ impl CustomDialog {
}

pub(crate) fn set_status(&self, text: &str) {
// 安全说明:wide_text 分配在栈上,其指针通过 LPARAM 传递给 UI 线程。
// 这里必须使用 SendMessageW(同步)而非 PostMessageW(异步),
// 因为 SendMessageW 会阻塞直到消息处理完成,确保 wide_text 在被使用期间有效。
let wide_text = to_wide(text);
unsafe {
let _ = SendMessageW(
Expand Down
Loading
Loading