From 805aefdb1ef93fe03192ace82cfcbbde7cce492d Mon Sep 17 00:00:00 2001 From: "don.cryptus" Date: Wed, 4 Oct 2023 00:03:40 +0200 Subject: [PATCH] easy --- src-tauri/src/commands/clipboard.rs | 29 +++++++------------ src-tauri/src/types/types.rs | 2 +- .../src/utils/clipboard/clipboard_helper.rs | 16 +++++++++- src-tauri/src/utils/hotkey/hotkey_listener.rs | 21 ++++++++------ 4 files changed, 38 insertions(+), 30 deletions(-) diff --git a/src-tauri/src/commands/clipboard.rs b/src-tauri/src/commands/clipboard.rs index 48ffc95e..54a9fd58 100644 --- a/src-tauri/src/commands/clipboard.rs +++ b/src-tauri/src/commands/clipboard.rs @@ -1,18 +1,18 @@ extern crate alloc; - -use alloc::borrow::Cow; -use arboard::ImageData; -use enigo::*; -use entity::clipboard::Model; -use tauri::Manager; - use crate::{ service::clipboard::{ clear_clipboards_db, delete_clipboard_db, get_clipboard_db, get_clipboards_db, - get_last_clipboard_db, star_clipboard_db, + star_clipboard_db, + }, + utils::{ + clipboard::clipboard_helper::type_last_clipboard, + setup::{APP, CLIPBOARD}, }, - utils::setup::{APP, CLIPBOARD}, }; +use alloc::borrow::Cow; +use arboard::ImageData; +use entity::clipboard::Model; +use tauri::Manager; #[tauri::command] pub async fn get_clipboards( @@ -99,16 +99,7 @@ pub async fn clear_clipboards() -> Result { #[tauri::command] pub async fn type_clipboard() -> Result { - let clipboard = get_last_clipboard_db().await; - - if clipboard.is_ok() { - let r#type = &clipboard.as_ref().unwrap().r#type; - - if r#type != "image" { - let mut enigo = Enigo::new(); - enigo.key_sequence(clipboard.unwrap().content.unwrap().as_str()); - } - } + type_last_clipboard().await; Ok(true) } diff --git a/src-tauri/src/types/types.rs b/src-tauri/src/types/types.rs index bd9ca392..860c80c7 100644 --- a/src-tauri/src/types/types.rs +++ b/src-tauri/src/types/types.rs @@ -19,7 +19,7 @@ pub struct DatabaseInfo { pub size: u64, } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Key { pub id: u32, pub global: bool, diff --git a/src-tauri/src/utils/clipboard/clipboard_helper.rs b/src-tauri/src/utils/clipboard/clipboard_helper.rs index 11740887..3e4bc8df 100644 --- a/src-tauri/src/utils/clipboard/clipboard_helper.rs +++ b/src-tauri/src/utils/clipboard/clipboard_helper.rs @@ -1,8 +1,9 @@ use crate::{ connection, - service::clipboard::insert_clipboard_db, + service::clipboard::{insert_clipboard_db, get_last_clipboard_db}, utils::setup::{APP, CLIPBOARD}, }; +use enigo::{Enigo, KeyboardControllable}; use entity::clipboard::{self, ActiveModel}; use image::{ImageBuffer, RgbaImage}; use sea_orm::{EntityTrait, QueryOrder, Set}; @@ -154,3 +155,16 @@ impl ClipboardHelper { } } } + +pub async fn type_last_clipboard() { + let clipboard = get_last_clipboard_db().await; + + if clipboard.is_ok() { + let r#type = &clipboard.as_ref().unwrap().r#type; + + if r#type != "image" { + let mut enigo = Enigo::new(); + enigo.key_sequence(clipboard.unwrap().content.unwrap().as_str()); + } + } +} diff --git a/src-tauri/src/utils/hotkey/hotkey_listener.rs b/src-tauri/src/utils/hotkey/hotkey_listener.rs index 3e677557..d47f50e1 100644 --- a/src-tauri/src/utils/hotkey/hotkey_listener.rs +++ b/src-tauri/src/utils/hotkey/hotkey_listener.rs @@ -1,7 +1,10 @@ use crate::{ service::{hotkey::get_all_hotkeys_db, window::toggle_main_window}, types::types::Key, - utils::setup::{HotkeyEvent, APP, GLOBAL_EVENTS, HOTKEYS, HOTKEY_MANAGER, HOTKEY_STOP_TX}, + utils::{ + clipboard::clipboard_helper::type_last_clipboard, + setup::{HotkeyEvent, APP, GLOBAL_EVENTS, HOTKEYS, HOTKEY_MANAGER, HOTKEY_STOP_TX}, + }, }; use core::time::Duration; use global_hotkey::hotkey::HotKey; @@ -30,10 +33,12 @@ pub fn init_hotkey_listener() -> () { tauri::async_runtime::spawn(async move { loop { if let Ok(event) = receiver.try_recv() { - let hotkeys = HOTKEYS.get().unwrap().lock().unwrap(); - - if let Some(hotkey) = hotkeys.get(&event.id) { - parse_hotkey_event(&hotkey); + let hotkey = { + let hotkeys = HOTKEYS.get().unwrap().lock().unwrap(); + hotkeys.get(&event.id).cloned() + }; + if let Some(hotkey) = hotkey { + parse_hotkey_event(&hotkey).await; } } @@ -46,14 +51,12 @@ pub fn init_hotkey_listener() -> () { }); } -pub fn parse_hotkey_event(key: &Key) { +pub async fn parse_hotkey_event(key: &Key) { let event: Result = key.event.parse::(); match event { Ok(HotkeyEvent::WindowDisplayToggle) => toggle_main_window(), - Ok(HotkeyEvent::TypeClipboard) => { - // Handle TypeClipboard event - } + Ok(HotkeyEvent::TypeClipboard) => type_last_clipboard().await, Ok(HotkeyEvent::SyncClipboardHistory) => { // Handle SyncClipboardHistory event }