From 5a77610fb955255df82ce5f1b9d8957ee207051f Mon Sep 17 00:00:00 2001 From: Pylogmon Date: Fri, 12 May 2023 14:40:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=AE=8C=E5=96=84Linux=E5=8F=96?= =?UTF-8?q?=E8=AF=8D=E9=80=BB=E8=BE=91=EF=BC=8C=E4=BF=AE=E5=A4=8D=E9=83=A8?= =?UTF-8?q?=E5=88=86=E5=BA=94=E7=94=A8=E5=86=85=E6=97=A0=E6=B3=95=E5=8F=96?= =?UTF-8?q?=E8=AF=8D=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #42 --- src-tauri/Cargo.toml | 7 +++---- src-tauri/src/config.rs | 13 ++++++++----- src-tauri/src/main.rs | 12 ++++-------- src-tauri/src/selection.rs | 38 ++++++++++++++++++++++++++------------ 4 files changed, 41 insertions(+), 29 deletions(-) diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index aeff6011ff..6b6ce9a3b8 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -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" @@ -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" diff --git a/src-tauri/src/config.rs b/src-tauri/src/config.rs index 4b02cbc376..e74cfe56c6 100644 --- a/src-tauri/src/config.rs +++ b/src-tauri/src/config.rs @@ -140,12 +140,15 @@ pub fn set_config(key: &str, value: Value, state: tauri::State) { #[tauri::command] pub fn write_config(state: tauri::State) -> 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] diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 2a8f0e131e..85cd52624a 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -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() { diff --git a/src-tauri/src/selection.rs b/src-tauri/src/selection.rs index bd701a91cb..f4f689256f 100644 --- a/src-tauri/src/selection.rs +++ b/src-tauri/src/selection.rs @@ -1,4 +1,5 @@ use crate::StringWrapper; +use crate::APP; // 获取选择的文本(Linux) #[cfg(target_os = "linux")] @@ -8,27 +9,39 @@ pub fn get_selection_text() -> Result { // 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()) } @@ -123,6 +136,7 @@ pub fn get_selection_text() -> Result { } } } + #[cfg(target_os = "macos")] pub fn get_selection_text() -> Result { let apple_script = r#" @@ -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(); @@ -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}"); }