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 c17c786 commit 510f256
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 35 deletions.
1 change: 1 addition & 0 deletions src-tauri/src/commands/hotkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub async fn update_hotkey(hotkey: Model) -> Result<Model, String> {

#[tauri::command]
pub async fn stop_hotkeys() -> Result<(), String> {
println!("stop_hotkeys");
Ok(unregister_hotkeys(false))
}

Expand Down
14 changes: 6 additions & 8 deletions src-tauri/src/service/window.rs
Original file line number Diff line number Diff line change
@@ -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()
}
Expand All @@ -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();
}
}
Expand Down
10 changes: 0 additions & 10 deletions src-tauri/src/utils/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
29 changes: 12 additions & 17 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
};
});

Expand Down

0 comments on commit 510f256

Please sign in to comment.