[feat] more hw info and update feature

This commit is contained in:
2025-11-08 18:09:35 +08:00
parent 41105d3bab
commit c8d8339f30
13 changed files with 813 additions and 500 deletions

View File

@@ -9,6 +9,8 @@ use std::fs::File;
use std::fs;
use std::path::Path;
use std::io::{BufRead, BufReader};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, OnceLock};
use tauri::path::BaseDirectory;
use tauri::Manager;
use serde::{Deserialize, Serialize};
@@ -17,6 +19,13 @@ use serde::{Deserialize, Serialize};
// pub type Result<T, String = ()> = Result<T, String>;
// 全局下载取消标志
static DOWNLOAD_CANCELLED: OnceLock<Arc<AtomicBool>> = OnceLock::new();
fn get_download_cancelled() -> Arc<AtomicBool> {
DOWNLOAD_CANCELLED.get_or_init(|| Arc::new(AtomicBool::new(false))).clone()
}
#[tauri::command]
pub fn greet(name: &str) -> Result<String, String> {
Ok(format!("Hello, {}! You've been greeted from Rust!", name))
@@ -301,20 +310,46 @@ pub fn read_vprof_report(console_log_path: &str) -> Result<String, String> {
// 更新相关命令
/// 检查更新
/// 检查更新(支持 GitHub Release 和自定义端点)
#[tauri::command]
pub async fn check_app_update(
app: tauri::AppHandle,
custom_endpoint: Option<String>,
github_repo: Option<String>,
endpoint: Option<String>,
use_mirror: Option<bool>,
include_prerelease: Option<bool>,
) -> Result<Option<UpdateInfo>, String> {
let current_version = app.package_info().version.to_string();
let use_mirror = use_mirror.unwrap_or(false);
let include_prerelease = include_prerelease.unwrap_or(false);
wrap_err!(check_update(
custom_endpoint.as_deref(),
github_repo.as_deref(),
&current_version
).await)
println!("[检查更新命令] 当前应用版本: {}", current_version);
println!("[检查更新命令] 使用镜像: {}", use_mirror);
println!("[检查更新命令] 包含预发布版本: {}", include_prerelease);
if let Some(ref ep) = endpoint {
println!("[检查更新命令] 自定义端点: {}", ep);
}
// 从环境变量获取 GitHub 仓库信息,如果没有则使用默认值
const DEFAULT_GITHUB_REPO: &str = "plsgo/cstb";
let github_repo_str = std::env::var("GITHUB_REPO").ok();
let github_repo = github_repo_str.as_deref().unwrap_or(DEFAULT_GITHUB_REPO);
println!("[检查更新命令] GitHub 仓库: {}", github_repo);
let result = wrap_err!(check_update(
endpoint.as_deref(),
&current_version,
use_mirror,
Some(github_repo),
include_prerelease
).await)?;
if result.is_some() {
println!("[检查更新命令] ✓ 返回更新信息");
} else {
println!("[检查更新命令] ✗ 无更新可用");
}
Ok(result)
}
/// 下载更新
@@ -323,15 +358,22 @@ pub async fn download_app_update(
app: tauri::AppHandle,
download_url: String,
) -> Result<String, String> {
let path = wrap_err!(download_update(
&app,
&download_url,
None // 可以添加进度回调
).await)?;
// 重置取消标志
let cancelled = get_download_cancelled();
cancelled.store(false, Ordering::Relaxed);
let path = wrap_err!(download_update(&app, &download_url, cancelled).await)?;
Ok(path.to_string_lossy().to_string())
}
/// 取消下载
#[tauri::command]
pub fn cancel_download_update() -> Result<(), String> {
let cancelled = get_download_cancelled();
cancelled.store(true, Ordering::Relaxed);
Ok(())
}
/// 安装更新
#[tauri::command]
pub fn install_app_update(installer_path: String) -> Result<(), String> {
@@ -638,7 +680,7 @@ pub async fn get_monitor_info() -> Result<Vec<MonitorInfo>, String> {
}
// 尝试获取刷新率和分辨率信息
let display_output = Command::new("powershell")
let _display_output = Command::new("powershell")
.args(&[
"-NoProfile",
"-Command",
@@ -648,7 +690,7 @@ pub async fn get_monitor_info() -> Result<Vec<MonitorInfo>, String> {
.await;
// 获取刷新率信息
let refresh_output = Command::new("powershell")
let _refresh_output = Command::new("powershell")
.args(&[
"-NoProfile",
"-Command",