Skip to content

Commit

Permalink
add type support for linux
Browse files Browse the repository at this point in the history
  • Loading branch information
0-don committed Oct 11, 2023
1 parent 9e31b77 commit 8932c64
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 16 deletions.
11 changes: 2 additions & 9 deletions src-tauri/src/commands/clipboard.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
extern crate alloc;
use crate::{service::clipboard::{
use crate::service::clipboard::{
clear_clipboards_db, copy_clipboard_from_id, delete_clipboard_db, get_clipboards_db,
star_clipboard_db,
}, utils::clipboard_manager::type_last_clipboard};
};
use entity::clipboard::Model;

#[tauri::command]
Expand Down Expand Up @@ -42,10 +42,3 @@ pub async fn clear_clipboards() -> Result<bool, ()> {

Ok(deleted.unwrap())
}

#[tauri::command]
pub async fn type_clipboard() -> Result<bool, ()> {
type_last_clipboard().await;

Ok(true)
}
10 changes: 8 additions & 2 deletions src-tauri/src/events/hotkey_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
},
types::types::Key,
utils::{
clipboard_manager::type_last_clipboard,
clipboard_manager::{type_last_clipboard, type_last_clipboard_linux},
hotkey_manager::{register_hotkeys, unregister_hotkeys, upsert_hotkeys_in_store},
tauri::config::{HotkeyEvent, APP, HOTKEYS, HOTKEY_RUNNING, HOTKEY_STOP_TX, MAIN_WINDOW},
},
Expand Down Expand Up @@ -71,7 +71,13 @@ pub async fn parse_hotkey_event(key: &Key) {

match event {
Ok(HotkeyEvent::WindowDisplayToggle) => toggle_main_window(),
Ok(HotkeyEvent::TypeClipboard) => type_last_clipboard().await,
Ok(HotkeyEvent::TypeClipboard) => {
if cfg!(target_os = "linux") {
type_last_clipboard_linux().await.unwrap();
} else {
type_last_clipboard().await;
}
}
Ok(HotkeyEvent::SyncClipboardHistory) => sync_clipboard_history().await.unwrap(),
Ok(e @ (HotkeyEvent::Preferences | HotkeyEvent::About)) => {
printlog!("open_window: {:?}", e);
Expand Down
1 change: 0 additions & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ async fn main() {
clipboard::star_clipboard,
clipboard::copy_clipboard,
clipboard::clear_clipboards,
clipboard::type_clipboard,
hotkey::get_hotkeys,
hotkey::update_hotkey,
settings::get_settings,
Expand Down
56 changes: 52 additions & 4 deletions src-tauri/src/utils/clipboard_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ use crate::{
connection,
service::clipboard::{get_last_clipboard_db, insert_clipboard_db},
};
use core::time::Duration;
use enigo::{Enigo, KeyboardControllable};
use entity::clipboard::{self, ActiveModel};
use image::{ImageBuffer, RgbaImage};
use sea_orm::{EntityTrait, QueryOrder, Set};
use std::io::Cursor;
use tauri::{regex::Regex, Manager};
use std::{io::Cursor, process::Command};
use tauri::{
api::dialog::{MessageDialogBuilder, MessageDialogButtons, MessageDialogKind},
regex::Regex,
Manager,
};

pub fn get_os_clipboard() -> (Option<String>, Option<RgbaImage>) {
let mut text: Option<String> = CLIPBOARD
Expand Down Expand Up @@ -165,9 +170,52 @@ pub async fn type_last_clipboard() {
let r#type = clipboard.clone().r#type;

if r#type != "image" && content.len() < 32 {
println!("clipboard: {:?}", clipboard.clone());
let mut enigo = Enigo::new();
enigo.key_sequence("a");
enigo.key_sequence(&content);
}
}
}

pub async fn type_last_clipboard_linux() -> Result<(), Box<dyn std::error::Error>> {
println!("type_last_clipboard_linux");
// Check if xdotool is installed
if !is_tool_installed("xdotool") {
MessageDialogBuilder::new(
"Missing Dependency",
"xdotool is not installed. Please install it to continue.",
)
.kind(MessageDialogKind::Error) // this will indicate that the message is an error
.buttons(MessageDialogButtons::Ok) // this will add an "Ok" button to the dialog
.show(|pressed_ok| {
if pressed_ok {
// Handle the case when the user presses the "Ok" button
}
});
return Ok(());
}

let clipboard = get_last_clipboard_db().await;

if clipboard.is_ok() {
let clipboard = clipboard?;
let content = clipboard.clone().content.unwrap();
let r#type = clipboard.clone().r#type;

if r#type != "image" && content.len() < 32 {
std::thread::sleep(Duration::from_millis(1000));
Command::new("xdotool")
.args(&["type", "--clearmodifiers", "--", &content])
.output()?;
}
}

return Ok(());
}

pub fn is_tool_installed(tool: &str) -> bool {
Command::new(tool)
.arg("--version")
.output()
.map(|output| output.status.success())
.unwrap_or(false)
}

0 comments on commit 8932c64

Please sign in to comment.