mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2025-05-20 07:26:58 -04:00
refactor: upgrade tauri 2.0
This commit is contained in:
@@ -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
|
||||
|
12
apps/gpgui-helper/src-tauri/capabilities/default.json
Normal file
12
apps/gpgui-helper/src-tauri/capabilities/default.json
Normal 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"
|
||||
]
|
||||
}
|
@@ -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!())?;
|
||||
|
@@ -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()
|
||||
}
|
||||
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
@@ -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"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user