add splashscreen

This commit is contained in:
Julian Freeman
2026-03-14 21:19:57 -04:00
parent 60113e9629
commit 63b469be97
5 changed files with 172 additions and 34 deletions

View File

@@ -17,7 +17,7 @@ pub struct LogPayload {
pub timestamp: String,
pub command: String,
pub output: String,
pub status: String, // "info", "success", "error"
pub status: String,
}
pub fn emit_log(handle: &AppHandle, command: &str, output: &str, status: &str) {
@@ -30,6 +30,14 @@ pub fn emit_log(handle: &AppHandle, command: &str, output: &str, status: &str) {
});
}
#[tauri::command]
async fn initialize_app(app: AppHandle) -> Result<bool, String> {
// 执行耗时的环境配置
tokio::task::spawn_blocking(move || {
ensure_winget_dependencies(&app).map(|_| true)
}).await.unwrap_or(Err("Initialization Task Panicked".to_string()))
}
#[tauri::command]
fn get_essentials(app: AppHandle) -> Vec<Software> {
let app_data_dir = app.path().app_data_dir().unwrap_or_default();
@@ -86,7 +94,6 @@ async fn install_software(id: String, state: State<'_, AppState>) -> Result<(),
#[tauri::command]
fn get_logs_history() -> Vec<LogPayload> {
// 暂时返回空,后续可以考虑存储
vec![]
}
@@ -105,15 +112,8 @@ pub fn run() {
let (tx, mut rx) = mpsc::channel::<String>(100);
app.manage(AppState { install_tx: tx });
// 环境初始化逻辑
let init_handle = handle.clone();
tauri::async_runtime::spawn(async move {
let _ = tokio::task::spawn_blocking(move || {
let _ = ensure_winget_dependencies(&init_handle);
}).await;
});
// 移除了在 setup 中直接执行异步 init 的逻辑,改为由前端指令触发
// 安装队列
tauri::async_runtime::spawn(async move {
while let Some(id) = rx.recv().await {
let _ = handle.emit("install-status", InstallProgress {
@@ -161,6 +161,7 @@ pub fn run() {
Ok(())
})
.invoke_handler(tauri::generate_handler![
initialize_app,
get_essentials,
get_all_software,
get_updates,

View File

@@ -26,10 +26,9 @@ struct WingetPackage {
}
pub fn ensure_winget_dependencies(handle: &AppHandle) -> Result<(), String> {
emit_log(handle, "Check Environment", "Initializing system components...", "info");
emit_log(handle, "Check Environment", "Initializing system components and updating sources...", "info");
let setup_script = r#"
# 设置容错
$ErrorActionPreference = 'SilentlyContinue'
Write-Output "Step 1: Enabling TLS 1.2"
@@ -42,23 +41,24 @@ pub fn ensure_winget_dependencies(handle: &AppHandle) -> Result<(), String> {
Write-Output "Step 3: Checking NuGet provider"
$provider = Get-PackageProvider -Name NuGet -ErrorAction SilentlyContinue
if ($null -eq $provider) {
Write-Output "Installing NuGet provider..."
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -Confirm:$false -ErrorAction SilentlyContinue
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -Confirm:$false
}
Write-Output "Step 4: Configuring Repository Trust"
Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted -ErrorAction SilentlyContinue
Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted
Write-Output "Step 5: Setting Execution Policy"
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser -Force -ErrorAction SilentlyContinue
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser -Force
Write-Output "Step 6: Checking Microsoft.WinGet.Client"
if (-not (Get-Module -ListAvailable Microsoft.WinGet.Client)) {
Write-Output "Installing Winget Client module (this may take 1-2 minutes)..."
Write-Output "Installing Winget Client module..."
Install-Module -Name Microsoft.WinGet.Client -Force -AllowClobber -Scope CurrentUser -Confirm:$false
} else {
Write-Output "Winget Client module is already present."
}
Write-Output "Step 7: Updating Winget Sources (apt-get update style)"
# 这一步非常关键,确保 winget 的本地数据库是最新的
winget source update --accept-source-agreements
"#;
let output = Command::new("powershell")
@@ -70,7 +70,7 @@ pub fn ensure_winget_dependencies(handle: &AppHandle) -> Result<(), String> {
Ok(out) => {
let msg = String::from_utf8_lossy(&out.stdout).to_string();
let err = String::from_utf8_lossy(&out.stderr).to_string();
// 只要最终模块存在,就认为成功,忽略过程中的次要警告
let check_final = Command::new("powershell")
.args(["-NoProfile", "-Command", "Get-Module -ListAvailable Microsoft.WinGet.Client"])
.creation_flags(0x08000000)
@@ -79,7 +79,7 @@ pub fn ensure_winget_dependencies(handle: &AppHandle) -> Result<(), String> {
let is_success = check_final.map(|o| !o.stdout.is_empty()).unwrap_or(false);
if is_success {
emit_log(handle, "Environment Setup", "Winget module is ready.", "success");
emit_log(handle, "Environment Setup", "Winget module and sources are ready.", "success");
Ok(())
} else {
emit_log(handle, "Environment Setup Error", &format!("OUT: {}\nERR: {}", msg, err), "error");