refactor: 优化侧边栏进程清理时机与错误提示
- 仅在应用退出事件时清理 Sidecar 进程,避免重复执行。 - 在开发模式下优化窗口创建错误提示并增强可读性,同时在生产模式下才执行 Sidecar 进程清理逻辑。
This commit is contained in:
@@ -25,11 +25,9 @@ pub fn run() {
|
|||||||
.expect("error while building tauri application")
|
.expect("error while building tauri application")
|
||||||
.run(|app_handle, event| {
|
.run(|app_handle, event| {
|
||||||
// 监听应用退出事件,清理 Sidecar 进程
|
// 监听应用退出事件,清理 Sidecar 进程
|
||||||
match event {
|
if let tauri::RunEvent::Exit = event {
|
||||||
tauri::RunEvent::ExitRequested { .. } | tauri::RunEvent::Exit => {
|
// 只在 Exit 事件时清理,避免重复执行
|
||||||
sidecar::cleanup_sidecar_process(app_handle);
|
sidecar::cleanup_sidecar_process(app_handle);
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,17 +56,22 @@ pub fn spawn_sidecar(app_handle: tauri::AppHandle) {
|
|||||||
// 开发模式:直接创建窗口连接到 Vite 开发服务器
|
// 开发模式:直接创建窗口连接到 Vite 开发服务器
|
||||||
println!("🔧 开发模式:连接到 Vite 开发服务器 (localhost:3000)");
|
println!("🔧 开发模式:连接到 Vite 开发服务器 (localhost:3000)");
|
||||||
|
|
||||||
let url = "http://localhost:3000";
|
match tauri::WebviewWindowBuilder::new(
|
||||||
tauri::WebviewWindowBuilder::new(
|
|
||||||
&app_handle,
|
&app_handle,
|
||||||
"main",
|
"main",
|
||||||
tauri::WebviewUrl::External(url.parse().unwrap()),
|
tauri::WebviewUrl::External("http://localhost:3000".parse().unwrap()),
|
||||||
)
|
)
|
||||||
.title(WINDOW_TITLE)
|
.title(WINDOW_TITLE)
|
||||||
.inner_size(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT)
|
.inner_size(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT)
|
||||||
.center()
|
.center()
|
||||||
.build()
|
.build()
|
||||||
.expect("创建窗口失败");
|
{
|
||||||
|
Ok(_) => println!("✓ 开发窗口创建成功"),
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("✗ 窗口创建失败: {}", e);
|
||||||
|
eprintln!("提示: 请确保 Vite 开发服务器在 localhost:3000 运行");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -127,10 +132,7 @@ pub fn spawn_sidecar(app_handle: tauri::AppHandle) {
|
|||||||
|
|
||||||
// 超时检查
|
// 超时检查
|
||||||
if start_time.elapsed() > timeout {
|
if start_time.elapsed() > timeout {
|
||||||
eprintln!(
|
eprintln!("✗ 启动超时: App 未能在 {} 秒内启动", STARTUP_TIMEOUT_SECS);
|
||||||
"✗ 启动超时: App 未能在 {} 秒内启动",
|
|
||||||
STARTUP_TIMEOUT_SECS
|
|
||||||
);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -144,6 +146,12 @@ pub fn spawn_sidecar(app_handle: tauri::AppHandle) {
|
|||||||
|
|
||||||
/// 清理 Sidecar 进程 (在应用退出时调用)
|
/// 清理 Sidecar 进程 (在应用退出时调用)
|
||||||
pub fn cleanup_sidecar_process(app_handle: &tauri::AppHandle) {
|
pub fn cleanup_sidecar_process(app_handle: &tauri::AppHandle) {
|
||||||
|
// 只在生产模式下清理 sidecar 进程
|
||||||
|
let is_dev = cfg!(debug_assertions);
|
||||||
|
if is_dev {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
println!("应用退出,正在清理 Sidecar 进程...");
|
println!("应用退出,正在清理 Sidecar 进程...");
|
||||||
if let Some(state) = app_handle.try_state::<SidecarProcess>() {
|
if let Some(state) = app_handle.try_state::<SidecarProcess>() {
|
||||||
if let Ok(mut process) = state.0.lock() {
|
if let Ok(mut process) = state.0.lock() {
|
||||||
|
|||||||
Reference in New Issue
Block a user