Skip to content

Commit

Permalink
feat: update enigo
Browse files Browse the repository at this point in the history
  • Loading branch information
yetone committed May 4, 2024
1 parent a644090 commit c964e60
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 201 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ description = "A tiny Rust library that allows you to easily obtain selected tex

[target.'cfg(not(target_os = "macos"))'.dependencies]
arboard = "3.2.0"
enigo = "0.1.3"
enigo = "0.2.0"
parking_lot = "0.12.1"

[target.'cfg(target_os = "macos")'.dependencies]
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[cfg(not(target_os = "macos"))]
mod utils;

#[cfg(target_os = "linux")]
Expand Down
99 changes: 2 additions & 97 deletions src/linux.rs
Original file line number Diff line number Diff line change
@@ -1,101 +1,6 @@
use enigo::*;
use parking_lot::Mutex;
use std::{thread, time::Duration};

static COPY_PASTE_LOCKER: Mutex<()> = Mutex::new(());

fn copy(enigo: &mut Enigo) {
let _guard = COPY_PASTE_LOCKER.lock();

crate::utils::up_control_keys(enigo);

enigo.key_down(Key::Control);
thread::sleep(Duration::from_millis(50));
enigo.key_click(Key::Layout('c'));
thread::sleep(Duration::from_millis(50));
enigo.key_up(Key::Control);
}

fn paste(enigo: &mut Enigo) {
let _guard = COPY_PASTE_LOCKER.lock();

crate::utils::up_control_keys(enigo);

enigo.key_down(Key::Control);
enigo.key_click(Key::Layout('v'));
enigo.key_up(Key::Control);
}

pub fn get_selected_text() -> Result<String, Box<dyn std::error::Error>> {
let mut enigo = Enigo::new();
get_selected_text_by_clipboard(&mut enigo, false)
}

fn get_selected_text_by_clipboard(
enigo: &mut Enigo,
cancel_select: bool,
) -> Result<String, Box<dyn std::error::Error>> {
use arboard::Clipboard;

let old_clipboard = (Clipboard::new()?.get_text(), Clipboard::new()?.get_image());

let mut write_clipboard = Clipboard::new()?;

let not_selected_placeholder = "";

write_clipboard.set_text(not_selected_placeholder)?;

thread::sleep(Duration::from_millis(50));

copy(enigo);

if cancel_select {
crate::utils::right_arrow_click(enigo, 1);
}

thread::sleep(Duration::from_millis(100));

let new_text = Clipboard::new()?.get_text();

match old_clipboard {
(Ok(old_text), _) => {
// Old Content is Text
write_clipboard.set_text(old_text.clone())?;
if let Ok(new) = new_text {
if new.trim() == not_selected_placeholder.trim() {
Ok(String::new())
} else {
Ok(new)
}
} else {
Ok(String::new())
}
}
(_, Ok(image)) => {
// Old Content is Image
write_clipboard.set_image(image)?;
if let Ok(new) = new_text {
if new.trim() == not_selected_placeholder.trim() {
Ok(String::new())
} else {
Ok(new)
}
} else {
Ok(String::new())
}
}
_ => {
// Old Content is Empty
write_clipboard.clear()?;
if let Ok(new) = new_text {
if new.trim() == not_selected_placeholder.trim() {
Ok(String::new())
} else {
Ok(new)
}
} else {
Ok(String::new())
}
}
}
let mut enigo = Enigo::new(&Settings::default()).unwrap();
crate::utils::get_selected_text_by_clipboard(&mut enigo, false)
}
96 changes: 90 additions & 6 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,104 @@
use enigo::*;
use parking_lot::Mutex;
use std::{thread, time::Duration};

static COPY_PASTE_LOCKER: Mutex<()> = Mutex::new(());
static INPUT_LOCK_LOCKER: Mutex<()> = Mutex::new(());

pub(crate) fn right_arrow_click(enigo: &mut Enigo, n: usize) {
let _guard = INPUT_LOCK_LOCKER.lock();

for _ in 0..n {
enigo.key_click(Key::RightArrow);
enigo.key(Key::RightArrow, Direction::Click).unwrap();
}
}

pub(crate) fn up_control_keys(enigo: &mut Enigo) {
enigo.key_up(Key::Control);
enigo.key_up(Key::Alt);
enigo.key_up(Key::Shift);
enigo.key_up(Key::Space);
enigo.key_up(Key::Tab);
enigo.key(Key::Control, Direction::Release).unwrap();
enigo.key(Key::Alt, Direction::Release).unwrap();
enigo.key(Key::Shift, Direction::Release).unwrap();
enigo.key(Key::Space, Direction::Release).unwrap();
enigo.key(Key::Tab, Direction::Release).unwrap();
}

pub(crate) fn copy(enigo: &mut Enigo) {
let _guard = COPY_PASTE_LOCKER.lock();

crate::utils::up_control_keys(enigo);

enigo.key(Key::Control, Direction::Press).unwrap();
#[cfg(target_os = "windows")]
enigo.key(Key::C, Direction::Click).unwrap();
#[cfg(target_os = "linux")]
enigo.key(Key::Unicode('c'), Direction::Click).unwrap();
enigo.key(Key::Control, Direction::Release).unwrap();
}

pub(crate) fn get_selected_text_by_clipboard(
enigo: &mut Enigo,
cancel_select: bool,
) -> Result<String, Box<dyn std::error::Error>> {
use arboard::Clipboard;

let old_clipboard = (Clipboard::new()?.get_text(), Clipboard::new()?.get_image());

let mut write_clipboard = Clipboard::new()?;

let not_selected_placeholder = "";

write_clipboard.set_text(not_selected_placeholder)?;

thread::sleep(Duration::from_millis(50));

copy(enigo);

if cancel_select {
crate::utils::right_arrow_click(enigo, 1);
}

thread::sleep(Duration::from_millis(100));

let new_text = Clipboard::new()?.get_text();

match old_clipboard {
(Ok(old_text), _) => {
// Old Content is Text
write_clipboard.set_text(old_text.clone())?;
if let Ok(new) = new_text {
if new.trim() == not_selected_placeholder.trim() {
Ok(String::new())
} else {
Ok(new)
}
} else {
Ok(String::new())
}
}
(_, Ok(image)) => {
// Old Content is Image
write_clipboard.set_image(image)?;
if let Ok(new) = new_text {
if new.trim() == not_selected_placeholder.trim() {
Ok(String::new())
} else {
Ok(new)
}
} else {
Ok(String::new())
}
}
_ => {
// Old Content is Empty
write_clipboard.clear()?;
if let Ok(new) = new_text {
if new.trim() == not_selected_placeholder.trim() {
Ok(String::new())
} else {
Ok(new)
}
} else {
Ok(String::new())
}
}
}
}
99 changes: 2 additions & 97 deletions src/windows.rs
Original file line number Diff line number Diff line change
@@ -1,101 +1,6 @@
use enigo::*;
use parking_lot::Mutex;
use std::{thread, time::Duration};

static COPY_PASTE_LOCKER: Mutex<()> = Mutex::new(());

fn copy(enigo: &mut Enigo) {
let _guard = COPY_PASTE_LOCKER.lock();

crate::utils::up_control_keys(enigo);

enigo.key_down(Key::Control);
thread::sleep(Duration::from_millis(50));
enigo.key_click(Key::Layout('c'));
thread::sleep(Duration::from_millis(50));
enigo.key_up(Key::Control);
}

fn paste(enigo: &mut Enigo) {
let _guard = COPY_PASTE_LOCKER.lock();

crate::utils::up_control_keys(enigo);

enigo.key_down(Key::Control);
enigo.key_click(Key::Layout('v'));
enigo.key_up(Key::Control);
}

pub fn get_selected_text() -> Result<String, Box<dyn std::error::Error>> {
let mut enigo = Enigo::new();
get_selected_text_by_clipboard(&mut enigo, false)
}

fn get_selected_text_by_clipboard(
enigo: &mut Enigo,
cancel_select: bool,
) -> Result<String, Box<dyn std::error::Error>> {
use arboard::Clipboard;

let old_clipboard = (Clipboard::new()?.get_text(), Clipboard::new()?.get_image());

let mut write_clipboard = Clipboard::new()?;

let not_selected_placeholder = "";

write_clipboard.set_text(not_selected_placeholder)?;

thread::sleep(Duration::from_millis(50));

copy(enigo);

if cancel_select {
crate::utils::right_arrow_click(enigo, 1);
}

thread::sleep(Duration::from_millis(100));

let new_text = Clipboard::new()?.get_text();

match old_clipboard {
(Ok(old_text), _) => {
// Old Content is Text
write_clipboard.set_text(old_text.clone())?;
if let Ok(new) = new_text {
if new.trim() == not_selected_placeholder.trim() {
Ok(String::new())
} else {
Ok(new)
}
} else {
Ok(String::new())
}
}
(_, Ok(image)) => {
// Old Content is Image
write_clipboard.set_image(image)?;
if let Ok(new) = new_text {
if new.trim() == not_selected_placeholder.trim() {
Ok(String::new())
} else {
Ok(new)
}
} else {
Ok(String::new())
}
}
_ => {
// Old Content is Empty
write_clipboard.clear()?;
if let Ok(new) = new_text {
if new.trim() == not_selected_placeholder.trim() {
Ok(String::new())
} else {
Ok(new)
}
} else {
Ok(String::new())
}
}
}
let mut enigo = Enigo::new(&Settings::default()).unwrap();
crate::utils::get_selected_text_by_clipboard(&mut enigo, false)
}

0 comments on commit c964e60

Please sign in to comment.