refactor: pake-cli support tauri 2.x beta

This commit is contained in:
jeasonnow
2024-07-30 16:14:29 +08:00
parent 8c07aaae3c
commit a631e117ca
23 changed files with 269 additions and 258 deletions

View File

@@ -51,6 +51,7 @@ pub struct PakeConfig {
pub windows: Vec<WindowConfig>,
pub user_agent: UserAgent,
pub system_tray: FunctionON,
pub system_tray_path: String,
}
impl PakeConfig {

View File

@@ -1,16 +1,44 @@
use tauri::{
menu::{MenuBuilder, MenuItemBuilder}, tray::TrayIconBuilder, AppHandle, Manager
image::Image, include_image, menu::{ContextMenu, MenuBuilder, MenuItemBuilder}, tray::{MouseButton, MouseButtonState, TrayIcon, TrayIconBuilder, TrayIconEvent}, AppHandle, Config, Manager
};
use tauri_plugin_window_state::{AppHandleExt, StateFlags};
pub fn set_system_tray(app: &AppHandle) -> tauri::Result<()> {
use super::config::PakeConfig;
pub fn set_system_tray(app: &AppHandle, pake_config: &PakeConfig) -> tauri::Result<()> {
let hide_app = MenuItemBuilder::with_id("hide_app", "Hide").build(app)?;
let show_app = MenuItemBuilder::with_id("show_app", "Show").build(app)?;
let quit = MenuItemBuilder::with_id("quit", "Quit").build(app)?;
let menu = MenuBuilder::new(app).items(&[&hide_app, &show_app, &quit]).build()?;
TrayIconBuilder::new()
app.app_handle().remove_tray_by_id("pake-tray");
let tray = TrayIconBuilder::new()
.icon(Image::from_path(pake_config.system_tray_path.as_str())?)
.menu(&menu)
.on_tray_icon_event(move |tray, event| {
if let TrayIconEvent::Click {
button: MouseButton::Left,
button_state: MouseButtonState::Up,
..
} = event {
println!("click");
let app = tray.app_handle();
#[cfg(not(target_os = "macos"))]
{
if let Some(webview_window) = app.get_webview_window("pake") {
let _ = webview_window.show();
let _ = webview_window.set_focus();
}
}
#[cfg(target_os = "macos")]
{
tauri::AppHandle::show(&app.app_handle()).unwrap();
}
}
})
.on_menu_event(move |app, event| match event.id().as_ref() {
"hide_app" => {
app.get_webview_window("pake").unwrap().minimize().unwrap();
@@ -26,5 +54,6 @@ pub fn set_system_tray(app: &AppHandle) -> tauri::Result<()> {
})
.build(app)?;
tray.set_icon_as_template(false)?;
Ok(())
}

View File

@@ -5,7 +5,7 @@ use tauri::{App, WebviewUrl, WebviewWindow, WebviewWindowBuilder};
#[cfg(target_os = "macos")]
use tauri::TitleBarStyle;
pub fn get_window(app: &mut App, config: PakeConfig, _data_dir: PathBuf) -> WebviewWindow {
pub fn get_window(app: &mut App, config: &PakeConfig, _data_dir: PathBuf) -> WebviewWindow {
let window_config = config
.windows
.first()

View File

@@ -1,6 +0,0 @@
/*
* This file serves as a collection point for external JS and CSS dependencies.
* It amalgamates these external resources for easier injection into the application.
* Additionally, you can directly include any script files in this file
* that you wish to attach to the application.
*/

View File

@@ -8,14 +8,14 @@ use app::{invoke, menu::set_system_tray, window};
use invoke::{download_file, download_file_by_binary};
use tauri::Manager;
use tauri_plugin_window_state::Builder as windowStatePlugin;
use tauri_plugin_global_shortcut::Shortcut;
use tauri_plugin_global_shortcut::{GlobalShortcutExt, Shortcut};
use util::{get_data_dir, get_pake_config};
use window::get_window;
pub fn run_app() {
let (pake_config, tauri_config) = get_pake_config();
let mut tauri_app = tauri::Builder::default();
let tauri_app = tauri::Builder::default();
let show_system_tray = pake_config.show_system_tray();
@@ -31,24 +31,26 @@ pub fn run_app() {
download_file_by_binary
])
.setup(move |app| {
let data_dir = get_data_dir(&app.app_handle(), tauri_config);
let data_dir = get_data_dir(&app.app_handle(), tauri_config.clone());
let _window = get_window(app, pake_config, data_dir);
let _window = get_window(app, &pake_config, data_dir);
// Prevent initial shaking
_window.show().unwrap();
if show_system_tray {
let _ = set_system_tray(&app.app_handle());
let _ = set_system_tray(&app.app_handle(), &pake_config);
} else {
app.app_handle().remove_tray_by_id("pake-tray");
}
if !activation_shortcut.is_empty() {
let app_handle = app.app_handle().clone();
let shortcut_hotkey = Shortcut::from_str(&activation_shortcut.as_str()).unwrap();
app_handle
.plugin(
tauri_plugin_global_shortcut::Builder::new()
.with_shortcut(activation_shortcut.as_str())
?.with_handler(move |app, event, _shortcut| {
let shortcut_hotkey = Shortcut::from_str(&activation_shortcut.as_str()).unwrap();
.with_handler(move |app, event, _shortcut| {
if shortcut_hotkey.eq(event) {
let window = app.get_webview_window("pake").unwrap();
@@ -63,6 +65,8 @@ pub fn run_app() {
}
).build())
.expect("Error registering global evoke shortcuts!");
app.global_shortcut().register(shortcut_hotkey)?;
}
Ok(())