Skip to content

Commit

Permalink
easy
Browse files Browse the repository at this point in the history
  • Loading branch information
0-don committed Oct 1, 2023
1 parent bdcd53b commit c568b5a
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 26 deletions.
27 changes: 27 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ tauri = { version = "1", features = [ "app-all", "notification-all", "dialog-ope
"window-set-focus",
"window-show",
] }
anyhow = "1"
global-hotkey = "0"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
clipboard-master = "3"
Expand Down
8 changes: 8 additions & 0 deletions src-tauri/src/types/types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use global_hotkey::hotkey::HotKey;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
Expand All @@ -17,3 +18,10 @@ pub struct DatabaseInfo {
pub records: u64,
pub size: u64,
}

#[derive(Debug)]
pub struct Key {
pub id: u32,
pub key: &'static str,
pub hotkey: HotKey,
}
45 changes: 45 additions & 0 deletions src-tauri/src/utils/hotkey/hotkey_listener.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use core::time::Duration;

use global_hotkey::hotkey::HotKey;
use global_hotkey::GlobalHotKeyEvent;

use crate::utils::setup::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: HotKey = hotkey_str.parse().unwrap();

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::sleep(Duration::from_millis(100));
});
}

pub fn parse_shortcut(ctrl: bool, alt: bool, shift: bool, key: &str) -> String {
let mut modifiers = Vec::new();
if ctrl {
modifiers.push("Control");
}
if alt {
modifiers.push("Alt");
}
if shift {
modifiers.push("Shift");
}

format!(
"{}{}Key{}",
modifiers.join("+"),
if !modifiers.is_empty() { "+" } else { "" },
key.to_uppercase()
)
}
1 change: 1 addition & 0 deletions src-tauri/src/utils/hotkey/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod hotkey_listener;
1 change: 1 addition & 0 deletions src-tauri/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod clipboard;
pub mod setup;
pub mod tray;
pub mod hotkey;
25 changes: 16 additions & 9 deletions src-tauri/src/utils/setup.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
use super::hotkey::hotkey_listener::init_hotkey_listener;
use crate::types::types::Key;
use crate::{
service::window::get_data_path, types::types::Config,
utils::clipboard::clipboard_handler::Handler,
};
use arboard::Clipboard;
use clipboard_master::Master;

use global_hotkey::GlobalHotKeyManager;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::{fs, path::Path, sync::OnceLock};
use tauri::{LogicalSize, Manager};
// use window_shadows::set_shadow;

use crate::{
service::window::get_data_path, types::types::Config,
utils::clipboard::clipboard_handler::Handler,
};

pub static MAIN_WINDOW_X: i32 = 375;
pub static MAIN_WINDOW_Y: i32 = 600;

pub static APP: OnceLock<tauri::AppHandle> = OnceLock::new();
pub static HOTKEY_MANAGER: OnceLock<GlobalHotKeyManager> = OnceLock::new();
pub static HOTKEYS: OnceLock<Arc<Mutex<HashMap<u32, Key>>>> = OnceLock::new();
pub static CLIPBOARD: OnceLock<Arc<Mutex<Clipboard>>> = OnceLock::new();

pub fn setup(app: &mut tauri::App) -> Result<(), Box<(dyn std::error::Error + 'static)>> {
APP.set(app.handle()).expect("error initializing tauri app");

let clipboard = Clipboard::new()?;
let _ = CLIPBOARD.set(Arc::new(Mutex::new(clipboard)));
let _ = HOTKEY_MANAGER.set(GlobalHotKeyManager::new().unwrap());
let _ = HOTKEYS.set(Arc::new(Mutex::new(HashMap::new())));
let _ = CLIPBOARD.set(Arc::new(Mutex::new(Clipboard::new()?)));

create_config();

Expand All @@ -38,6 +42,9 @@ pub fn setup(app: &mut tauri::App) -> Result<(), Box<(dyn std::error::Error + 's
}

tauri::async_runtime::spawn(async { Master::new(Handler).run() });
// tauri::async_runtime::spawn(async { init_hotkey_listener() });

init_hotkey_listener();

Ok(())
}
Expand Down
4 changes: 4 additions & 0 deletions src/@types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@ export type HotkeyEvent =
| "exit"
| "toggle_dev_tools"
| "scroll_to_top";

interface ImportMeta {
env: any;
}
24 changes: 11 additions & 13 deletions src/store/HotkeyStore.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { invoke } from "@tauri-apps/api";
import { isRegistered, register } from "@tauri-apps/api/globalShortcut";
import { createRoot, createSignal } from "solid-js";
import { Hotkey, HotkeyEvent } from "../@types";
import { parseShortcut, registerHotkeys } from "../utils/hotkeyRegister";
import AppStore from "./AppStore";

function createHotkeyStore() {
const [globalHotkeyEvent, setGlobalHotkeyEvent] = createSignal<boolean>(true);
Expand Down Expand Up @@ -32,17 +30,17 @@ function createHotkeyStore() {

setHotkeys(hotkeys);

// Display and hide the app window
const windowHotkey = hotkeys.find(
(h) => h.event === "window_display_toggle",
);

if (windowHotkey?.status && !(await isRegistered(windowHotkey.shortcut))) {
register(windowHotkey.shortcut, () => {
AppStore.updateSidebarIcons("Recent Clipboards");
invoke("window_display_toggle");
}).catch(() => {});
}
// // Display and hide the app window
// const windowHotkey = hotkeys.find(
// (h) => h.event === "window_display_toggle",
// );

// if (windowHotkey?.status && !(await isRegistered(windowHotkey.shortcut))) {
// register(windowHotkey.shortcut, () => {
// AppStore.updateSidebarIcons("Recent Clipboards");
// invoke("window_display_toggle");
// }).catch(() => {});
// }

if (reg) await registerHotkeys(hotkeys);
};
Expand Down
8 changes: 4 additions & 4 deletions src/store/SettingsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ function createSettingsStore() {

const setCurrentTab = (tabName: SettingsTabName) =>
setTabs((prev) =>
prev.map((tab) => ({ ...tab, current: tab.name === tabName }))
prev.map((tab) => ({ ...tab, current: tab.name === tabName })),
);

const getCurrentTab = () => tabs().find((tab) => tab.current);

const updateSettings = async (
settings: Settings,
upload: boolean | undefined = true
upload: boolean | undefined = true,
) => {
if (upload) await invoke("update_settings", { settings });
setSettings(settings);

try {
const env = import.meta.env;
const env = (import.meta as any).env;
env.PROD && settings.startup ? await enable() : await disable();
} catch (_) {}
};
Expand All @@ -71,7 +71,7 @@ function createSettingsStore() {
setSettings(settings);

try {
const env = import.meta.env;
const env = (import.meta as any).env;
env.PROD && settings.startup ? await enable() : await disable();
} catch (_) {}
};
Expand Down

0 comments on commit c568b5a

Please sign in to comment.