Skip to content

Commit

Permalink
easy
Browse files Browse the repository at this point in the history
  • Loading branch information
0-don committed Oct 3, 2023
1 parent 439973e commit 805aefd
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 30 deletions.
29 changes: 10 additions & 19 deletions src-tauri/src/commands/clipboard.rs
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -99,16 +99,7 @@ pub async fn clear_clipboards() -> Result<bool, ()> {

#[tauri::command]
pub async fn type_clipboard() -> Result<bool, ()> {
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)
}
2 changes: 1 addition & 1 deletion src-tauri/src/types/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct DatabaseInfo {
pub size: u64,
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Key {
pub id: u32,
pub global: bool,
Expand Down
16 changes: 15 additions & 1 deletion src-tauri/src/utils/clipboard/clipboard_helper.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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());
}
}
}
21 changes: 12 additions & 9 deletions src-tauri/src/utils/hotkey/hotkey_listener.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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<HotkeyEvent, ()> = key.event.parse::<HotkeyEvent>();

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
}
Expand Down

0 comments on commit 805aefd

Please sign in to comment.