Skip to content

Commit

Permalink
fix: 完善Linux取词逻辑,修复部分应用内无法取词的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Pylogmon committed May 12, 2023
1 parent a259563 commit 5a77610
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 29 deletions.
7 changes: 3 additions & 4 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ tauri = { version = "1.3", features = ["clipboard-all", "dialog-open", "fs-read-
tauri-plugin-single-instance = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "dev" }
tauri-plugin-autostart = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "dev" }
reqwest = { version = "0.11", features = ["blocking", "json"] }
enigo = {git = "https://github.com/enigo-rs/enigo"}
tiny_http = "0.12.0"
once_cell = "1.17.1"
toml = "0.7.3"
Expand All @@ -27,15 +28,13 @@ dunce = "1.0.4"

[target.'cfg(windows)'.dependencies]
windows = {version="0.44.0",features= ["Win32_UI_WindowsAndMessaging", "Win32_Foundation"] }
enigo = {git = "https://github.com/enigo-rs/enigo"}
arboard = "3.2.0"
window-shadows = "0.2"
arboard = "3.2.0"

[target.'cfg(target_os = "macos")'.dependencies ]
enigo = {git = "https://github.com/enigo-rs/enigo"}
arboard = "3.2.0"
window-shadows = "0.2"
core-graphics = "0.22.3"
arboard = "3.2.0"

[target.'cfg(target_os = "linux")'.dependencies ]
mouse_position = "0.1.3"
Expand Down
13 changes: 8 additions & 5 deletions src-tauri/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,15 @@ pub fn set_config(key: &str, value: Value, state: tauri::State<ConfigWrapper>) {

#[tauri::command]
pub fn write_config(state: tauri::State<ConfigWrapper>) -> Result<(), String> {
let proxy=state.0.lock().unwrap().get("proxy", Value::String(String::from("")));
std::env::set_var("http_proxy",proxy.as_str().unwrap());
std::env::set_var("https_proxy",proxy.as_str().unwrap());
std::env::set_var("all_proxy",proxy.as_str().unwrap());
let proxy = state
.0
.lock()
.unwrap()
.get("proxy", Value::String(String::from("")));
std::env::set_var("http_proxy", proxy.as_str().unwrap());
std::env::set_var("https_proxy", proxy.as_str().unwrap());
std::env::set_var("all_proxy", proxy.as_str().unwrap());
state.0.lock().unwrap().write()

}

#[tauri::command]
Expand Down
12 changes: 4 additions & 8 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,11 @@ fn main() {
toml::Value::String(String::from("")),
app_handle.state(),
);
std::env::set_var("http_proxy",proxy.as_str().unwrap());
std::env::set_var("https_proxy",proxy.as_str().unwrap());
std::env::set_var("all_proxy",proxy.as_str().unwrap());
std::env::set_var("http_proxy", proxy.as_str().unwrap());
std::env::set_var("https_proxy", proxy.as_str().unwrap());
std::env::set_var("all_proxy", proxy.as_str().unwrap());
// 检查更新
let enable = get_config(
"auto_check",
toml::Value::Boolean(true),
app_handle.state(),
);
let enable = get_config("auto_check", toml::Value::Boolean(true), app_handle.state());
let handle = app.handle();

if enable.as_bool().unwrap() {
Expand Down
38 changes: 26 additions & 12 deletions src-tauri/src/selection.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::StringWrapper;
use crate::APP;

// 获取选择的文本(Linux)
#[cfg(target_os = "linux")]
Expand All @@ -8,27 +9,39 @@ pub fn get_selection_text() -> Result<String, String> {
// match session_type.as_str() {
// "x11" => {
use std::time::Duration;
use tauri::Manager;
use x11_clipboard::Clipboard;

if let Ok(clipboard) = Clipboard::new() {
if let Ok(v) = clipboard.load(
if let Ok(primary) = clipboard.load(
clipboard.getter.atoms.primary,
clipboard.getter.atoms.utf8_string,
clipboard.getter.atoms.property,
Duration::from_millis(100),
) {
let v = String::from_utf8_lossy(&v)
let mut result = String::from_utf8_lossy(&primary)
.trim_matches('\u{0}')
.trim()
.to_string();
clipboard
.store(
clipboard.getter.atoms.primary,

let app_handle = APP.get().unwrap();
let last = get_translate_text(app_handle.state());
// 如果Primary没有变化,就尝试复制一次
if result.is_empty() || result == last {
copy();
if let Ok(main_clipboard) = clipboard.load(
clipboard.getter.atoms.clipboard,
clipboard.getter.atoms.utf8_string,
"",
)
.unwrap();
Ok(v)
clipboard.getter.atoms.property,
Duration::from_millis(100),
) {
result = String::from_utf8_lossy(&main_clipboard)
.trim_matches('\u{0}')
.trim()
.to_string();
}
}
Ok(result)
} else {
Err("Clipboard Read Failed".to_string())
}
Expand Down Expand Up @@ -123,6 +136,7 @@ pub fn get_selection_text() -> Result<String, String> {
}
}
}

#[cfg(target_os = "macos")]
pub fn get_selection_text() -> Result<String, String> {
let apple_script = r#"
Expand Down Expand Up @@ -229,8 +243,9 @@ error "not found AXSelectedText"
Err(e) => Err(e.to_string()),
}
}
// windows 复制操作
#[cfg(target_os = "windows")]

// 复制操作
#[cfg(any(target_os = "windows", target_os = "linux"))]
pub fn copy() {
use enigo::*;
let mut enigo = Enigo::new();
Expand All @@ -243,7 +258,6 @@ pub fn copy() {
enigo.key_up(Key::Tab);
enigo.key_up(Key::Escape);
enigo.key_up(Key::CapsLock);
enigo.key_up(Key::Option);
// 发送CtrlC
enigo.key_sequence_parse("{+CTRL}c{-CTRL}");
}
Expand Down

0 comments on commit 5a77610

Please sign in to comment.