Skip to content

Commit 10360da

Browse files
authored
feat: cache method (#5)
1 parent 4650dee commit 10360da

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

Cargo.toml

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ description = "A tiny Rust library that allows you to easily obtain selected tex
1313
[target.'cfg(not(target_os = "macos"))'.dependencies]
1414
arboard = "3.2.0"
1515
enigo = { version = "0.2.0", features = [ "xdo" ] }
16-
parking_lot = "0.12.1"
1716

1817
[target.'cfg(target_os = "macos")'.dependencies]
1918
cocoa = "0.24"
@@ -24,3 +23,9 @@ core-graphics = "0.22.3"
2423
accessibility-ng = "0.1.6"
2524
accessibility-sys-ng = "0.1.3"
2625

26+
[dependencies]
27+
active-win-pos-rs = "0.8.3"
28+
debug_print = "1.0.0"
29+
lru = "0.12.3"
30+
parking_lot = "0.12.1"
31+

src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ mod tests {
3333

3434
#[test]
3535
fn test_get_selected_text() {
36+
println!("--- get_selected_text ---");
37+
let text = get_selected_text().unwrap();
38+
println!("selected text: {:#?}", text);
39+
println!("--- get_selected_text ---");
40+
let text = get_selected_text().unwrap();
41+
println!("selected text: {:#?}", text);
42+
println!("--- get_selected_text ---");
3643
let text = get_selected_text().unwrap();
3744
println!("selected text: {:#?}", text);
3845
}

src/macos.rs

+40-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,52 @@
1+
use std::num::NonZeroUsize;
2+
13
use accessibility_ng::{AXAttribute, AXUIElement};
24
use accessibility_sys_ng::{kAXFocusedUIElementAttribute, kAXSelectedTextAttribute};
5+
use active_win_pos_rs::get_active_window;
36
use core_foundation::string::CFString;
7+
use debug_print::debug_println;
8+
use lru::LruCache;
9+
use parking_lot::Mutex;
10+
11+
static GET_SELECTED_TEXT_METHOD: Mutex<Option<LruCache<String, u8>>> = Mutex::new(None);
412

513
pub fn get_selected_text() -> Result<String, Box<dyn std::error::Error>> {
14+
if GET_SELECTED_TEXT_METHOD.lock().is_none() {
15+
let cache = LruCache::new(NonZeroUsize::new(100).unwrap());
16+
*GET_SELECTED_TEXT_METHOD.lock() = Some(cache);
17+
}
18+
let mut cache = GET_SELECTED_TEXT_METHOD.lock();
19+
let cache = cache.as_mut().unwrap();
20+
let app_name = match get_active_window() {
21+
Ok(window) => window.app_name,
22+
Err(_) => return Err("No active window found".into()),
23+
};
24+
// debug_println!("app_name: {}", app_name);
25+
if let Some(text) = cache.get(&app_name) {
26+
if *text == 0 {
27+
return get_selected_text_by_ax();
28+
}
29+
return get_selected_text_by_clipboard_using_applescript();
30+
}
631
match get_selected_text_by_ax() {
7-
Ok(text) => Ok(text),
8-
Err(_) => get_selected_text_by_clipboard_using_applescript(),
32+
Ok(text) => {
33+
cache.put(app_name, 0);
34+
Ok(text)
35+
}
36+
Err(_) => match get_selected_text_by_clipboard_using_applescript() {
37+
Ok(text) => {
38+
if !text.is_empty() {
39+
cache.put(app_name, 1);
40+
}
41+
Ok(text)
42+
}
43+
Err(e) => Err(e),
44+
},
945
}
1046
}
1147

1248
fn get_selected_text_by_ax() -> Result<String, Box<dyn std::error::Error>> {
49+
// debug_println!("get_selected_text_by_ax");
1350
let system_element = AXUIElement::system_wide();
1451
let Some(selected_element) = system_element
1552
.attribute(&AXAttribute::new(&CFString::from_static_string(
@@ -79,6 +116,7 @@ theSelectedText
79116

80117
fn get_selected_text_by_clipboard_using_applescript() -> Result<String, Box<dyn std::error::Error>>
81118
{
119+
// debug_println!("get_selected_text_by_clipboard_using_applescript");
82120
let output = std::process::Command::new("osascript")
83121
.arg("-e")
84122
.arg(APPLE_SCRIPT)

0 commit comments

Comments
 (0)