diff --git a/src-tauri/src/commands/hotkey.rs b/src-tauri/src/commands/hotkey.rs index 2dd16e32..85fb0504 100644 --- a/src-tauri/src/commands/hotkey.rs +++ b/src-tauri/src/commands/hotkey.rs @@ -25,6 +25,7 @@ pub async fn update_hotkey(hotkey: Model) -> Result { #[tauri::command] pub async fn stop_hotkeys() -> Result<(), String> { + println!("stop_hotkeys"); Ok(unregister_hotkeys(false)) } diff --git a/src-tauri/src/service/window.rs b/src-tauri/src/service/window.rs index b553ca99..e2464330 100644 --- a/src-tauri/src/service/window.rs +++ b/src-tauri/src/service/window.rs @@ -1,16 +1,14 @@ +use crate::{ + types::types::{Config, DataPath}, + utils::{hotkey::hotkey_manager::register_hotkeys, setup::APP}, +}; use std::{ fs::{self, read_to_string}, path::{Path, PathBuf}, }; - use tauri::{api::dialog::blocking::FileDialogBuilder, Manager, Window}; use tauri_plugin_positioner::{Position, WindowExt}; -use crate::{ - types::types::{Config, DataPath}, - utils::{hotkey::hotkey_listener::init_hotkey_listener, setup::APP}, -}; - pub fn get_main_window() -> Window { APP.get().unwrap().get_window("main").unwrap() } @@ -19,12 +17,12 @@ pub fn toggle_main_window() { let window = get_main_window(); if window.is_visible().unwrap() { let _ = window.hide(); - init_hotkey_listener(false); + register_hotkeys(false); } else { let _ = window.move_window(Position::BottomRight); let _ = window.show(); let _ = window.set_focus(); - init_hotkey_listener(true) + register_hotkeys(true) // init_event(); } } diff --git a/src-tauri/src/utils/setup.rs b/src-tauri/src/utils/setup.rs index 24b4854c..e061ce2f 100644 --- a/src-tauri/src/utils/setup.rs +++ b/src-tauri/src/utils/setup.rs @@ -49,16 +49,6 @@ define_hotkey_event! { pub static GLOBAL_EVENTS: [&'static str; 2] = ["window_display_toggle", "type_clipboard"]; -// pub static VIEW_MORE_EVENTS: [&'static str; 4] = -// ["sync_clipboard_history", "preferences", "about", "exit"]; - -// pub static SIDEBAR_ICON_EVENTS: [&'static str; 4] = [ -// "recent_clipboards", -// "starred_clipboards", -// "history", -// "view_more", -// ]; - pub fn setup(app: &mut tauri::App) -> Result<(), Box<(dyn std::error::Error + 'static)>> { APP.set(app.handle()).expect("error initializing tauri app"); let _ = HOTKEY_MANAGER.set(GlobalHotKeyManager::new().unwrap()); diff --git a/src/index.tsx b/src/index.tsx index 8f123374..de420a97 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,13 +1,14 @@ -import { listen } from "@tauri-apps/api/event"; +import { invoke } from "@tauri-apps/api"; +import { appWindow } from "@tauri-apps/api/window"; import { createResource, onMount } from "solid-js"; import { render } from "solid-js/web"; import App from "./components/pages/app/App"; import HotkeyStore from "./store/HotkeyStore"; import SettingsStore from "./store/SettingsStore"; import "./styles.css"; -import { invoke } from "@tauri-apps/api"; const Index = () => { + let timer: NodeJS.Timeout; const { setGlobalHotkeyEvent } = HotkeyStore; const { init } = SettingsStore; @@ -16,27 +17,21 @@ const Index = () => { onMount(async () => { window.onfocus = async () => { setGlobalHotkeyEvent(true); - - setTimeout(async () => { + clearInterval(timer); + timer = setTimeout(async () => { setGlobalHotkeyEvent(false); + await invoke("stop_hotkeys"); }, 5000); }; window.onblur = async () => { - await invoke("window_display_toggle"); - setGlobalHotkeyEvent(false); - }; - - // const focus = await appWindow.onFocusChanged( - // async ({ payload }) => - // !payload && (await invoke("window_display_toggle")), - // ); + if (timer) { + appWindow.hide(); + return clearInterval(timer); + } - const init_listener = await listen("init_listener", init); - - return async () => { - init_listener(); - focus(); + await invoke("stop_hotkeys"); + setGlobalHotkeyEvent(false); }; });