refactor: upgrade tauri 2.0

This commit is contained in:
Kevin Yue
2024-12-26 21:35:55 +08:00
parent 0f67be465b
commit 8f8ad466f4
73 changed files with 7232 additions and 5026 deletions

View File

Before

Width:  |  Height:  |  Size: 6.7 KiB

After

Width:  |  Height:  |  Size: 6.7 KiB

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -5,8 +5,8 @@
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>GlobalProtect</title>
<script type="module" crossorigin src="/assets/main-c159dd55.js"></script>
<link rel="stylesheet" href="/assets/index-11e7064a.css">
<script type="module" crossorigin src="/assets/main-CQPVXkdn.js"></script>
<link rel="stylesheet" crossorigin href="/assets/main-B3YRsHQ2.css">
</head>
<body>
<script>
@@ -16,6 +16,5 @@
document.documentElement.style.fontSize = 16 / ratio + "px";
</script>
<div id="root" data-tauri-drag-region></div>
</body>
</html>

View File

@@ -9,29 +9,29 @@
"tauri": "tauri"
},
"dependencies": {
"@emotion/react": "^11.13.0",
"@emotion/styled": "^11.13.0",
"@mui/icons-material": "^5.16.7",
"@mui/material": "^5.16.7",
"@tauri-apps/api": "^1.6.0",
"react": "^18.3.1",
"react-dom": "^18.3.1"
"@emotion/react": "^11.14.0",
"@emotion/styled": "^11.14.0",
"@mui/icons-material": "^6.3.0",
"@mui/material": "^6.3.0",
"@tauri-apps/api": "^2.1.1",
"react": "^19.0.0",
"react-dom": "^19.0.0"
},
"devDependencies": {
"@tauri-apps/cli": "^1.6.0",
"@types/node": "^20.14.15",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
"@vitejs/plugin-react": "^4.3.1",
"eslint": "^8.57.0",
"@tauri-apps/cli": "^2.1.0",
"@types/node": "^22.10.2",
"@types/react": "^19.0.2",
"@types/react-dom": "^19.0.2",
"@typescript-eslint/eslint-plugin": "^8.18.2",
"@typescript-eslint/parser": "^8.18.2",
"@vitejs/plugin-react": "^4.3.4",
"eslint": "^9.17.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-react": "^7.35.0",
"eslint-plugin-react-hooks": "^4.6.2",
"prettier": "3.1.0",
"typescript": "^5.5.4",
"vite": "^4.5.3"
"eslint-plugin-react": "^7.37.3",
"eslint-plugin-react-hooks": "^5.1.0",
"prettier": "3.4.2",
"typescript": "^5.7.2",
"vite": "^6.0.5"
},
"packageManager": "pnpm@8.15.7"
"packageManager": "pnpm@9.15.1"
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,16 +1,18 @@
[package]
name = "gpgui-helper"
rust-version.workspace = true
authors.workspace = true
version.workspace = true
edition.workspace = true
license.workspace = true
[build-dependencies]
tauri-build = { version = "1.5", features = [] }
tauri-build = { version = "2", features = [] }
[dependencies]
gpapi = { path = "../../../crates/gpapi", features = ["tauri"] }
tauri = { workspace = true, features = ["window-start-dragging"] }
tauri.workspace = true
tokio.workspace = true
anyhow.workspace = true
log.workspace = true

View File

@@ -0,0 +1,12 @@
{
"$schema": "../gen/schemas/desktop-schema.json",
"identifier": "default",
"description": "Capability for the main window",
"windows": ["main"],
"permissions": [
"core:window:allow-start-dragging",
"core:event:allow-listen",
"core:event:allow-emit",
"core:event:allow-unlisten"
]
}

View File

@@ -1,8 +1,7 @@
use std::sync::Arc;
use gpapi::utils::window::WindowExt;
use log::info;
use tauri::Manager;
use tauri::{Listener, Manager};
use crate::updater::{GuiUpdater, Installer, ProgressNotifier};
@@ -25,15 +24,15 @@ impl App {
tauri::Builder::default()
.setup(move |app| {
let win = app.get_window("main").expect("no main window");
win.hide_menu();
let win = app.get_webview_window("main").expect("no main window");
let _ = win.hide_menu();
let notifier = ProgressNotifier::new(win.clone());
let installer = Installer::new(api_key);
let updater = Arc::new(GuiUpdater::new(gui_version, notifier, installer));
let win_clone = win.clone();
app.listen_global("app://update-done", move |_event| {
app.listen_any("app://update-done", move |_event| {
info!("Update done");
let _ = win_clone.close();
});
@@ -41,12 +40,15 @@ impl App {
// Listen for the update event
win.listen("app://update", move |_event| {
let updater = Arc::clone(&updater);
if updater.is_in_progress() {
info!("Update already in progress");
updater.notify_progress();
return;
}
tokio::spawn(async move { updater.update().await });
});
// Update the GUI on startup
win.trigger("app://update", None);
Ok(())
})
.run(tauri::generate_context!())?;

View File

@@ -1,5 +1,5 @@
use clap::Parser;
use gpapi::utils::base64;
use gpapi::utils::{base64, env_utils};
use log::{info, LevelFilter};
use crate::app::App;
@@ -22,6 +22,8 @@ impl Cli {
let api_key = self.read_api_key()?;
let app = App::new(api_key, &self.gui_version);
env_utils::patch_gui_runtime_env(false);
app.run()
}

View File

@@ -1,39 +1,39 @@
use std::sync::Arc;
use std::sync::{Arc, RwLock};
use gpapi::{
service::request::UpdateGuiRequest,
utils::{checksum::verify_checksum, crypto::Crypto, endpoint::http_endpoint},
};
use log::{info, warn};
use tauri::{Manager, Window};
use tauri::{Emitter, WebviewWindow};
use crate::downloader::{ChecksumFetcher, FileDownloader};
#[cfg(not(debug_assertions))]
const SNAPSHOT: &str = match option_env!("SNAPSHOT") {
Some(val) => val,
None => "false"
Some(val) => val,
None => "false",
};
pub struct ProgressNotifier {
win: Window,
win: WebviewWindow,
}
impl ProgressNotifier {
pub fn new(win: Window) -> Self {
pub fn new(win: WebviewWindow) -> Self {
Self { win }
}
fn notify(&self, progress: Option<f64>) {
let _ = self.win.emit_all("app://update-progress", progress);
let _ = self.win.emit("app://update-progress", progress);
}
fn notify_error(&self) {
let _ = self.win.emit_all("app://update-error", ());
let _ = self.win.emit("app://update-error", ());
}
fn notify_done(&self) {
let _ = self.win.emit_and_trigger("app://update-done", ());
let _ = self.win.emit("app://update-done", ());
}
}
@@ -72,6 +72,8 @@ pub struct GuiUpdater {
version: String,
notifier: Arc<ProgressNotifier>,
installer: Installer,
in_progress: RwLock<bool>,
progress: Arc<RwLock<Option<f64>>>,
}
impl GuiUpdater {
@@ -80,6 +82,8 @@ impl GuiUpdater {
version,
notifier: Arc::new(notifier),
installer,
in_progress: Default::default(),
progress: Default::default(),
}
}
@@ -112,15 +116,23 @@ impl GuiUpdater {
let cf = ChecksumFetcher::new(&checksum_url);
let notifier = Arc::clone(&self.notifier);
dl.on_progress(move |progress| notifier.notify(progress));
let progress_ref = Arc::clone(&self.progress);
dl.on_progress(move |progress| {
// Save progress to shared state so that it can be notified to the UI when needed
if let Ok(mut guard) = progress_ref.try_write() {
*guard = progress;
}
notifier.notify(progress);
});
self.set_in_progress(true);
let res = tokio::try_join!(dl.download(), cf.fetch());
let (file, checksum) = match res {
Ok((file, checksum)) => (file, checksum),
Err(err) => {
warn!("Download error: {}", err);
self.notifier.notify_error();
self.notify_error();
return;
}
};
@@ -130,7 +142,7 @@ impl GuiUpdater {
if let Err(err) = verify_checksum(&file_path, &checksum) {
warn!("Checksum error: {}", err);
self.notifier.notify_error();
self.notify_error();
return;
}
@@ -138,10 +150,48 @@ impl GuiUpdater {
if let Err(err) = self.installer.install(&file_path, &checksum).await {
warn!("Install error: {}", err);
self.notifier.notify_error();
self.notify_error();
} else {
info!("Install success");
self.notifier.notify_done();
self.notify_done();
}
}
pub fn is_in_progress(&self) -> bool {
if let Ok(guard) = self.in_progress.try_read() {
*guard
} else {
info!("Failed to acquire in_progress lock");
false
}
}
fn set_in_progress(&self, in_progress: bool) {
if let Ok(mut guard) = self.in_progress.try_write() {
*guard = in_progress;
} else {
info!("Failed to acquire in_progress lock");
}
}
fn notify_error(&self) {
self.set_in_progress(false);
self.notifier.notify_error();
}
fn notify_done(&self) {
self.set_in_progress(false);
self.notifier.notify_done();
}
pub fn notify_progress(&self) {
let progress = if let Ok(guard) = self.progress.try_read() {
*guard
} else {
info!("Failed to acquire progress lock");
None
};
self.notifier.notify(progress);
}
}

View File

@@ -1,35 +1,15 @@
{
"$schema": "../node_modules/@tauri-apps/cli/schema.json",
"$schema": "../node_modules/@tauri-apps/cli/config.schema.json",
"build": {
"beforeDevCommand": "pnpm dev",
"beforeBuildCommand": "pnpm build",
"devPath": "http://localhost:1421",
"distDir": "../dist",
"withGlobalTauri": false
"devUrl": "http://localhost:1421",
"frontendDist": "../dist"
},
"package": {
"productName": "gpgui-helper"
},
"tauri": {
"allowlist": {
"all": false,
"window": {
"all": false,
"startDragging": true
}
},
"bundle": {
"active": false,
"targets": "deb",
"identifier": "com.yuezk.gpgui-helper",
"icon": [
"icons/32x32.png",
"icons/128x128.png",
"icons/128x128@2x.png",
"icons/icon.icns",
"icons/icon.ico"
]
},
"identifier": "com.yuezk.gpgui-helper",
"productName": "gpgui-helper",
"app": {
"withGlobalTauri": false,
"security": {
"csp": null
},
@@ -48,5 +28,16 @@
"decorations": false
}
]
},
"bundle": {
"active": false,
"targets": "deb",
"icon": [
"icons/32x32.png",
"icons/128x128.png",
"icons/128x128@2x.png",
"icons/icon.icns",
"icons/icon.ico"
]
}
}

View File

@@ -1,10 +1,12 @@
import { Box, Button, CssBaseline, LinearProgress, Typography } from "@mui/material";
import { appWindow } from "@tauri-apps/api/window";
import { getCurrentWindow } from "@tauri-apps/api/window";
import logo from "../../assets/icon.svg";
import { useEffect, useState } from "react";
import "./styles.css";
const appWindow = getCurrentWindow();
function useUpdateProgress() {
const [progress, setProgress] = useState<number | null>(null);
@@ -25,6 +27,8 @@ export default function App() {
const [error, setError] = useState(false);
useEffect(() => {
appWindow.emit("app://update");
const unlisten = appWindow.listen("app://update-error", () => {
setError(true);
});