Skip to content

Commit

Permalink
🔧 Refactor code to be more structured.
Browse files Browse the repository at this point in the history
  • Loading branch information
tw93 committed Dec 25, 2024
1 parent c061821 commit a695b41
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 100 deletions.
36 changes: 0 additions & 36 deletions src-tauri/src/app/menu.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src-tauri/src/app/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod config;
pub mod invoke;
pub mod menu;
pub mod setup;
pub mod window;
90 changes: 90 additions & 0 deletions src-tauri/src/app/setup.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
8 changes: 6 additions & 2 deletions src-tauri/src/app/window.rs
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
69 changes: 10 additions & 59 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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(())
})
Expand Down
3 changes: 1 addition & 2 deletions src-tauri/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit a695b41

Please sign in to comment.