🏁 Final commit: Project Token Usage Viewer completed
This commit is contained in:
@@ -8,7 +8,7 @@ use tauri_plugin_shell::ShellExt;
|
||||
// ===== 配置常量 =====
|
||||
|
||||
/// Sidecar App 启动超时时间(秒)
|
||||
const STARTUP_TIMEOUT_SECS: u64 = 5;
|
||||
const STARTUP_TIMEOUT_SECS: u64 = 30;
|
||||
|
||||
/// 默认起始端口
|
||||
const DEFAULT_PORT: u16 = 3000;
|
||||
@@ -47,6 +47,27 @@ async fn find_available_port(start: u16) -> u16 {
|
||||
start // 回退到起始端口
|
||||
}
|
||||
|
||||
// 显示错误对话框
|
||||
fn show_error_dialog(message: &str) {
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
use std::process::Command;
|
||||
let _ = Command::new("powershell")
|
||||
.args([
|
||||
"-Command",
|
||||
&format!(
|
||||
"[System.Windows.Forms.MessageBox]::Show('{}', '启动错误', 'OK', 'Error')",
|
||||
message.replace('\'', "''")
|
||||
),
|
||||
])
|
||||
.spawn();
|
||||
}
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
{
|
||||
eprintln!("错误: {}", message);
|
||||
}
|
||||
}
|
||||
|
||||
/// 启动 Sidecar 进程并创建主窗口
|
||||
pub fn spawn_sidecar(app_handle: tauri::AppHandle) {
|
||||
// 检测是否为开发模式
|
||||
@@ -84,13 +105,23 @@ pub fn spawn_sidecar(app_handle: tauri::AppHandle) {
|
||||
println!("使用端口: {}", port);
|
||||
|
||||
// 启动 sidecar
|
||||
let sidecar = app_handle
|
||||
.shell()
|
||||
.sidecar("app")
|
||||
.expect("无法找到 app")
|
||||
.env("PORT", port.to_string());
|
||||
let sidecar = match app_handle.shell().sidecar("app") {
|
||||
Ok(cmd) => cmd.env("PORT", port.to_string()),
|
||||
Err(e) => {
|
||||
eprintln!("✗ 无法找到 sidecar: {}", e);
|
||||
show_error_dialog("无法找到后端服务程序,请重新安装应用。");
|
||||
std::process::exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
let (mut rx, child) = sidecar.spawn().expect("启动 sidecar 失败");
|
||||
let (mut rx, child) = match sidecar.spawn() {
|
||||
Ok(result) => result,
|
||||
Err(e) => {
|
||||
eprintln!("✗ 启动 sidecar 失败: {}", e);
|
||||
show_error_dialog(&format!("后端服务启动失败: {}", e));
|
||||
std::process::exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
// 保存进程句柄到全局状态
|
||||
if let Some(state) = app_handle.try_state::<SidecarProcess>() {
|
||||
@@ -103,30 +134,46 @@ pub fn spawn_sidecar(app_handle: tauri::AppHandle) {
|
||||
let mut app_ready = false;
|
||||
|
||||
while let Some(event) = rx.recv().await {
|
||||
if let CommandEvent::Stdout(line) = event {
|
||||
let output = String::from_utf8_lossy(&line);
|
||||
println!("App: {}", output);
|
||||
match event {
|
||||
CommandEvent::Stdout(line) => {
|
||||
let output = String::from_utf8_lossy(&line);
|
||||
println!("App: {}", output);
|
||||
|
||||
// 检测 App 启动成功的标志
|
||||
if output.contains("Listening on:") || output.contains("localhost") {
|
||||
app_ready = true;
|
||||
println!("✓ App 启动成功!");
|
||||
// 检测 App 启动成功的标志
|
||||
if output.contains("Listening on:") || output.contains("localhost") {
|
||||
app_ready = true;
|
||||
println!("✓ App 启动成功!");
|
||||
|
||||
// 创建主窗口
|
||||
let url = format!("http://localhost:{}", port);
|
||||
tauri::WebviewWindowBuilder::new(
|
||||
&app_handle,
|
||||
"main",
|
||||
tauri::WebviewUrl::External(url.parse().unwrap()),
|
||||
)
|
||||
.title(WINDOW_TITLE)
|
||||
.inner_size(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT)
|
||||
.center()
|
||||
.build()
|
||||
.expect("创建窗口失败");
|
||||
// 创建主窗口
|
||||
let url = format!("http://localhost:{}", port);
|
||||
match tauri::WebviewWindowBuilder::new(
|
||||
&app_handle,
|
||||
"main",
|
||||
tauri::WebviewUrl::External(url.parse().unwrap()),
|
||||
)
|
||||
.title(WINDOW_TITLE)
|
||||
.inner_size(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT)
|
||||
.center()
|
||||
.build()
|
||||
{
|
||||
Ok(_) => println!("✓ 窗口创建成功"),
|
||||
Err(e) => {
|
||||
eprintln!("✗ 窗口创建失败: {}", e);
|
||||
show_error_dialog(&format!("窗口创建失败: {}", e));
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
break;
|
||||
}
|
||||
}
|
||||
CommandEvent::Stderr(line) => {
|
||||
let output = String::from_utf8_lossy(&line);
|
||||
eprintln!("App Error: {}", output);
|
||||
}
|
||||
CommandEvent::Error(e) => {
|
||||
eprintln!("Sidecar 错误: {}", e);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
// 超时检查
|
||||
|
||||
Reference in New Issue
Block a user