-
-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔧 Refactor code to be more structured.
- Loading branch information
Showing
6 changed files
with
108 additions
and
100 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters