Skip to content

Commit

Permalink
easy
Browse files Browse the repository at this point in the history
  • Loading branch information
0-don committed Oct 6, 2023
1 parent 38f2206 commit c17c786
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 25 deletions.
15 changes: 10 additions & 5 deletions src-tauri/src/commands/hotkey.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::{
service::{
hotkey::{get_all_hotkeys_db, update_hotkey_db},
window::init_event,
service::hotkey::{get_all_hotkeys_db, update_hotkey_db},
utils::hotkey::{
hotkey_listener::init_hotkey_listener,
hotkey_manager::{register_hotkeys, unregister_hotkeys},
},
utils::hotkey::hotkey_manager::unregister_hotkeys,
};
use entity::hotkey::Model;

Expand All @@ -18,7 +18,7 @@ pub async fn get_hotkeys() -> Result<Vec<Model>, String> {
pub async fn update_hotkey(hotkey: Model) -> Result<Model, String> {
let res = update_hotkey_db(hotkey).await;

init_event();
init_hotkey_listener(false);

Ok(res.unwrap())
}
Expand All @@ -27,3 +27,8 @@ pub async fn update_hotkey(hotkey: Model) -> Result<Model, String> {
pub async fn stop_hotkeys() -> Result<(), String> {
Ok(unregister_hotkeys(false))
}

#[tauri::command]
pub async fn start_hotkeys() -> Result<(), String> {
Ok(register_hotkeys(true))
}
7 changes: 2 additions & 5 deletions src-tauri/src/commands/settings.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use entity::settings::Model;

use crate::service::{
settings::{get_settings_db, update_settings_db},
window::init_event,
};
use crate::{service::settings::{get_settings_db, update_settings_db}, utils::hotkey::hotkey_listener::init_hotkey_listener};

#[tauri::command]
pub async fn get_settings() -> Result<Model, String> {
Expand All @@ -16,7 +13,7 @@ pub async fn get_settings() -> Result<Model, String> {
pub async fn update_settings(settings: Model) -> Result<Model, String> {
let res = update_settings_db(settings).await;

init_event();
init_hotkey_listener(false);

Ok(res.unwrap())
}
1 change: 1 addition & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ async fn main() {
hotkey::get_hotkeys,
hotkey::update_hotkey,
hotkey::stop_hotkeys,
hotkey::start_hotkeys,
settings::get_settings,
settings::update_settings,
window::window_display_toggle,
Expand Down
9 changes: 0 additions & 9 deletions src-tauri/src/service/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,6 @@ use crate::{
utils::{hotkey::hotkey_listener::init_hotkey_listener, setup::APP},
};

pub fn init_event() {
APP.get()
.unwrap()
.get_window("main")
.unwrap()
.emit("init_listener", Some(()))
.unwrap();
}

pub fn get_main_window() -> Window {
APP.get().unwrap().get_window("main").unwrap()
}
Expand Down
2 changes: 2 additions & 0 deletions src-tauri/src/utils/hotkey/hotkey_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ pub async fn parse_hotkey_event(key: &Key) {

let window = APP.get().unwrap().get_window("main").unwrap();

println!("event: {:?}", event);

match event {
Ok(HotkeyEvent::WindowDisplayToggle) => toggle_main_window(),
Ok(HotkeyEvent::TypeClipboard) => type_last_clipboard().await,
Expand Down
8 changes: 2 additions & 6 deletions src-tauri/src/utils/hotkey/hotkey_manager.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use crate::{
service::hotkey::get_all_hotkeys_db,
types::types::Key,
utils::setup::{APP, GLOBAL_EVENTS, HOTKEYS, HOTKEY_MANAGER},
utils::setup::{GLOBAL_EVENTS, HOTKEYS, HOTKEY_MANAGER},
};
use global_hotkey::hotkey::HotKey;
use tauri::Manager;

pub fn register_hotkeys(all: bool) {
let hotkeys_store = HOTKEYS.get().unwrap().lock().unwrap();
let hotkey_manager = HOTKEY_MANAGER.get().unwrap();
let window = APP.get().unwrap().get_window("main").unwrap();

for (_, hotkey) in hotkeys_store.iter() {
if all || hotkey.global {
Expand All @@ -26,9 +24,7 @@ pub fn unregister_hotkeys(all: bool) {
let hotkey_manager = HOTKEY_MANAGER.get().unwrap();

for (_, hotkey) in hotkeys_store.iter() {
if all {
hotkey_manager.unregister(hotkey.hotkey.clone()).unwrap();
} else if !hotkey.global {
if all || !hotkey.global {
hotkey_manager.unregister(hotkey.hotkey.clone()).unwrap();
}
}
Expand Down

0 comments on commit c17c786

Please sign in to comment.