🔧 Refactor code to be more structured.
This commit is contained in:
@@ -1,36 +0,0 @@
|
||||
use tauri::{
|
||||
menu::{MenuBuilder, MenuItemBuilder},
|
||||
tray::TrayIconBuilder,
|
||||
AppHandle, Manager,
|
||||
};
|
||||
use tauri_plugin_window_state::{AppHandleExt, StateFlags};
|
||||
|
||||
pub fn set_system_tray(app: &AppHandle) -> 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()?;
|
||||
app.app_handle().remove_tray_by_id("pake-tray");
|
||||
let tray = TrayIconBuilder::new()
|
||||
.menu(&menu)
|
||||
.on_menu_event(move |app, event| match event.id().as_ref() {
|
||||
"hide_app" => {
|
||||
app.get_webview_window("pake").unwrap().minimize().unwrap();
|
||||
}
|
||||
"show_app" => {
|
||||
app.get_webview_window("pake").unwrap().show().unwrap();
|
||||
}
|
||||
"quit" => {
|
||||
let _res = app.save_window_state(StateFlags::all());
|
||||
std::process::exit(0);
|
||||
}
|
||||
_ => (),
|
||||
})
|
||||
.icon(app.default_window_icon().unwrap().clone())
|
||||
.build(app)?;
|
||||
|
||||
tray.set_icon_as_template(false)?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
pub mod config;
|
||||
pub mod invoke;
|
||||
pub mod menu;
|
||||
pub mod setup;
|
||||
pub mod window;
|
||||
|
||||
90
src-tauri/src/app/setup.rs
Normal file
90
src-tauri/src/app/setup.rs
Normal file
@@ -0,0 +1,90 @@
|
||||
use std::str::FromStr;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::{Duration, Instant};
|
||||
use tauri::{
|
||||
menu::{MenuBuilder, MenuItemBuilder},
|
||||
tray::TrayIconBuilder,
|
||||
AppHandle, Manager,
|
||||
};
|
||||
use tauri_plugin_global_shortcut::{GlobalShortcutExt, Shortcut};
|
||||
use tauri_plugin_window_state::{AppHandleExt, StateFlags};
|
||||
|
||||
pub fn set_system_tray(app: &AppHandle, show_system_tray: bool) -> tauri::Result<()> {
|
||||
if !show_system_tray {
|
||||
app.remove_tray_by_id("pake-tray");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
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()?;
|
||||
app.app_handle().remove_tray_by_id("pake-tray");
|
||||
let tray = TrayIconBuilder::new()
|
||||
.menu(&menu)
|
||||
.on_menu_event(move |app, event| match event.id().as_ref() {
|
||||
"hide_app" => {
|
||||
app.get_webview_window("pake").unwrap().minimize().unwrap();
|
||||
}
|
||||
"show_app" => {
|
||||
app.get_webview_window("pake").unwrap().show().unwrap();
|
||||
}
|
||||
"quit" => {
|
||||
let _res = app.save_window_state(StateFlags::all());
|
||||
std::process::exit(0);
|
||||
}
|
||||
_ => (),
|
||||
})
|
||||
.icon(app.default_window_icon().unwrap().clone())
|
||||
.build(app)?;
|
||||
|
||||
tray.set_icon_as_template(false)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn set_global_shortcut(app: &AppHandle, shortcut: String) -> tauri::Result<()> {
|
||||
if shortcut.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
let app_handle = app.clone();
|
||||
let shortcut_hotkey = Shortcut::from_str(shortcut.as_str()).unwrap();
|
||||
let last_triggered = Arc::new(Mutex::new(Instant::now()));
|
||||
|
||||
app_handle
|
||||
.plugin(
|
||||
tauri_plugin_global_shortcut::Builder::new()
|
||||
.with_handler({
|
||||
let last_triggered = Arc::clone(&last_triggered);
|
||||
move |app, event, _shortcut| {
|
||||
let mut last_triggered = last_triggered.lock().unwrap();
|
||||
if Instant::now().duration_since(*last_triggered)
|
||||
< Duration::from_millis(300)
|
||||
{
|
||||
return;
|
||||
}
|
||||
*last_triggered = Instant::now();
|
||||
if shortcut_hotkey.eq(event) {
|
||||
let window = app.get_webview_window("pake").unwrap();
|
||||
let is_visible = window.is_visible().unwrap();
|
||||
|
||||
match is_visible {
|
||||
true => {
|
||||
window.hide().unwrap();
|
||||
}
|
||||
false => {
|
||||
window.show().unwrap();
|
||||
window.set_focus().unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.build(),
|
||||
)
|
||||
.expect("Failed to set global shortcut");
|
||||
app.global_shortcut().register(shortcut_hotkey).unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,11 +1,15 @@
|
||||
use crate::app::config::PakeConfig;
|
||||
use crate::util::get_data_dir;
|
||||
use std::{path::PathBuf, str::FromStr};
|
||||
use tauri::{App, Url, WebviewUrl, WebviewWindow, WebviewWindowBuilder};
|
||||
use tauri::{App, Config, Url, WebviewUrl, WebviewWindow, WebviewWindowBuilder};
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
use tauri::{Theme, TitleBarStyle};
|
||||
|
||||
pub fn get_window(app: &mut App, config: &PakeConfig, _data_dir: PathBuf) -> WebviewWindow {
|
||||
pub fn set_window(app: &mut App, config: &PakeConfig, tauri_config: &Config) -> WebviewWindow {
|
||||
let package_name = tauri_config.clone().product_name.unwrap();
|
||||
let _data_dir = get_data_dir(app.handle(), package_name);
|
||||
|
||||
let window_config = config
|
||||
.windows
|
||||
.first()
|
||||
|
||||
@@ -2,17 +2,17 @@
|
||||
mod app;
|
||||
mod util;
|
||||
|
||||
use app::{invoke, menu::set_system_tray, window};
|
||||
use app::{
|
||||
invoke,
|
||||
setup::{set_global_shortcut, set_system_tray},
|
||||
window::set_window,
|
||||
};
|
||||
use invoke::{download_file, download_file_by_binary, send_notification};
|
||||
use std::str::FromStr;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::{Duration, Instant};
|
||||
use std::time::Duration;
|
||||
|
||||
use tauri::Manager;
|
||||
use tauri_plugin_global_shortcut::{GlobalShortcutExt, Shortcut};
|
||||
use tauri_plugin_window_state::Builder as windowStatePlugin;
|
||||
use util::{get_data_dir, get_pake_config};
|
||||
use window::get_window;
|
||||
use util::get_pake_config;
|
||||
|
||||
pub fn run_app() {
|
||||
let (pake_config, tauri_config) = get_pake_config();
|
||||
@@ -46,60 +46,11 @@ pub fn run_app() {
|
||||
send_notification,
|
||||
])
|
||||
.setup(move |app| {
|
||||
let data_dir = get_data_dir(app.app_handle(), tauri_config.clone());
|
||||
set_window(app, &pake_config, &tauri_config);
|
||||
|
||||
let _window = get_window(app, &pake_config, data_dir);
|
||||
set_system_tray(app.app_handle(), show_system_tray).unwrap();
|
||||
|
||||
// Prevent initial shaking
|
||||
_window.show().unwrap();
|
||||
|
||||
if show_system_tray {
|
||||
let _ = set_system_tray(app.app_handle());
|
||||
} 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();
|
||||
let last_triggered = Arc::new(Mutex::new(Instant::now()));
|
||||
|
||||
app_handle
|
||||
.plugin(
|
||||
tauri_plugin_global_shortcut::Builder::new()
|
||||
.with_handler({
|
||||
let last_triggered = Arc::clone(&last_triggered);
|
||||
move |app, event, _shortcut| {
|
||||
// Fixed the bug of tauri's hidden call, which caused repeated execution
|
||||
let now = Instant::now();
|
||||
let mut last = last_triggered.lock().unwrap();
|
||||
if now.duration_since(*last) < Duration::from_millis(500) {
|
||||
return;
|
||||
}
|
||||
*last = now;
|
||||
|
||||
if shortcut_hotkey.eq(event) {
|
||||
let window = app.get_webview_window("pake").unwrap();
|
||||
let is_visible = window.is_visible().unwrap();
|
||||
|
||||
match is_visible {
|
||||
true => {
|
||||
window.minimize().unwrap();
|
||||
}
|
||||
false => {
|
||||
window.unminimize().unwrap();
|
||||
window.set_focus().unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.build(),
|
||||
)
|
||||
.expect("Error registering global evoke shortcuts!");
|
||||
|
||||
app.global_shortcut().register(shortcut_hotkey)?;
|
||||
}
|
||||
set_global_shortcut(app.app_handle(), activation_shortcut).unwrap();
|
||||
|
||||
Ok(())
|
||||
})
|
||||
|
||||
@@ -23,9 +23,8 @@ pub fn get_pake_config() -> (PakeConfig, Config) {
|
||||
(pake_config, tauri_config)
|
||||
}
|
||||
|
||||
pub fn get_data_dir(app: &AppHandle, _tauri_config: Config) -> PathBuf {
|
||||
pub fn get_data_dir(app: &AppHandle, package_name: String) -> PathBuf {
|
||||
{
|
||||
let package_name = _tauri_config.product_name.unwrap();
|
||||
let data_dir = app
|
||||
.path()
|
||||
.config_dir()
|
||||
|
||||
Reference in New Issue
Block a user