Skip to content

Commit

Permalink
easy
Browse files Browse the repository at this point in the history
  • Loading branch information
0-don committed Oct 2, 2023
1 parent c568b5a commit 03864db
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 50 deletions.
18 changes: 17 additions & 1 deletion src-tauri/src/service/window.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{fs, path::PathBuf};

use tauri::Manager;
use tauri::{Manager, Window};
use tauri_plugin_positioner::{Position, WindowExt};

use crate::{types::types::DataPath, utils::setup::APP};

Expand All @@ -22,6 +23,21 @@ pub fn init_hotkey() {
.unwrap();
}

pub fn get_main_window() -> Window {
APP.get().unwrap().get_window("main").unwrap()
}

pub fn toggle_main_window() {
let window = get_main_window();
if window.is_visible().unwrap() {
let _ = window.hide();
} else {
let _ = window.move_window(Position::BottomRight);
let _ = window.show();
let _ = window.set_focus();
}
}

pub fn get_data_path() -> DataPath {
let config_path = APP
.get()
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/types/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ pub struct DatabaseInfo {
#[derive(Debug)]
pub struct Key {
pub id: u32,
pub key: &'static str,
pub key: String,
pub hotkey: HotKey,
}
31 changes: 25 additions & 6 deletions src-tauri/src/utils/hotkey/hotkey_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,43 @@ use core::time::Duration;
use global_hotkey::hotkey::HotKey;
use global_hotkey::GlobalHotKeyEvent;

use crate::utils::setup::HOTKEY_MANAGER;
use crate::{
service::window::toggle_main_window,
types::types::Key,
utils::setup::{HOTKEYS, HOTKEY_MANAGER},
};

pub fn init_hotkey_listener() -> () {
println!("init_hotkey_listener");

let hotkey_manager = HOTKEY_MANAGER.get().unwrap();

let hotkey_str = parse_shortcut(true, false, false, "y");
let hotkey_str: String = parse_shortcut(true, false, false, "y");
let hotkey: HotKey = hotkey_str.parse().unwrap();

HOTKEYS.get().unwrap().lock().unwrap().insert(
hotkey.id(),
Key {
id: hotkey.id(),
key: hotkey_str,
hotkey: hotkey.clone(),
},
);

let _ = hotkey_manager.register(hotkey).unwrap();

let receiver = GlobalHotKeyEvent::receiver();
std::thread::spawn(|| loop {
if let Ok(event) = receiver.try_recv() {
println!("tray event: {event:?}");
std::thread::spawn(|| {
let hotkeys = HOTKEYS.get().unwrap().lock().unwrap();
loop {
if let Ok(event) = receiver.try_recv() {
if let Some(hotkey) = hotkeys.get(&event.id) {
println!("Hotkey Pressed: {:?}", hotkey);
toggle_main_window();
}
}
std::thread::sleep(Duration::from_millis(100));
}
std::thread::sleep(Duration::from_millis(100));
});
}

Expand Down
47 changes: 5 additions & 42 deletions src-tauri/src/utils/tray.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use tauri::{CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu};
use tauri_plugin_positioner::{on_tray_event, Position, WindowExt};
use tauri::{CustomMenuItem, SystemTray, SystemTrayEvent, SystemTrayMenu};
use tauri_plugin_positioner::on_tray_event;

use crate::service::window::init_hotkey;
use crate::service::window::toggle_main_window;

pub fn system_tray() -> SystemTray {
let quit = CustomMenuItem::new("quit".to_string(), "Quit");
Expand All @@ -18,46 +18,9 @@ pub fn system_tray_event(app: &tauri::AppHandle, event: SystemTrayEvent) {
position: _,
size: _,
..
} => {
println!("Clicked on tray icon");
let win = app.get_window("main").unwrap();
let _ = win.move_window(Position::TrayCenter);
init_hotkey();
let _ = win.show();
let _ = win.set_focus();
}
SystemTrayEvent::RightClick {
position: _,
size: _,
..
} => {
println!("Clicked on tray icon");
let win = app.get_window("main").unwrap();
let _ = win.move_window(Position::TrayCenter);
init_hotkey();
let _ = win.show();
let _ = win.set_focus();
}
SystemTrayEvent::DoubleClick {
position: _,
size: _,
..
} => {
println!("Clicked on tray icon");
let win = app.get_window("main").unwrap();
let _ = win.move_window(Position::TrayCenter);
init_hotkey();
let _ = win.show();
let _ = win.set_focus();
}
} => toggle_main_window(),
SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() {
"open" => {
let win = app.get_window("main").unwrap();
let _ = win.move_window(Position::BottomRight);
init_hotkey();
let _ = win.show();
let _ = win.set_focus();
}
"open" => toggle_main_window(),
"quit" => app.exit(1),
_ => {
println!("Unhandled tray event");
Expand Down

0 comments on commit 03864db

Please sign in to comment.