Skip to content

Commit

Permalink
optimize: speed up the shortcuts popup (openai-translator#681)
Browse files Browse the repository at this point in the history
  • Loading branch information
yetone authored May 7, 2023
1 parent d262685 commit 0960e61
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 12 deletions.
30 changes: 26 additions & 4 deletions src-tauri/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use tauri::Manager;
use parking_lot::Mutex;

use crate::APP_HANDLE;

Expand All @@ -15,12 +16,18 @@ pub fn copy() {
enigo.key_click(Key::Layout('c'));
enigo.key_up(Key::Control);
}

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

#[allow(dead_code)]
#[cfg(target_os = "macos")]
pub fn copy() {
// use std::{thread, time::Duration};

use enigo::*;

let _guard = COPY_PASTE.lock();

let mut enigo = Enigo::new();
enigo.key_up(Key::Control);
enigo.key_up(Key::Meta);
Expand All @@ -36,6 +43,7 @@ pub fn copy() {
// enigo.key_up(Key::Layout('c'));
enigo.key_up(Key::Meta);
}

#[allow(dead_code)]
#[cfg(target_os = "linux")]
pub fn copy() {
Expand All @@ -52,6 +60,10 @@ pub fn copy() {

#[cfg(not(target_os = "macos"))]
pub fn get_selected_text() -> Result<String, Box<dyn std::error::Error>> {
get_selected_text_by_clipboard()
}

pub fn get_selected_text_by_clipboard() -> Result<String, Box<dyn std::error::Error>> {
use arboard::Clipboard;
use std::{thread, time::Duration};

Expand All @@ -63,6 +75,8 @@ pub fn get_selected_text() -> Result<String, Box<dyn std::error::Error>> {

write_clipboard.set_text(not_selected_placeholder)?;

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

copy();

thread::sleep(Duration::from_millis(100));
Expand All @@ -87,7 +101,11 @@ pub fn get_selected_text() -> Result<String, Box<dyn std::error::Error>> {
// Old Content is Image
write_clipboard.set_image(image)?;
if let Ok(new) = new_text {
Ok(new)
if new.trim() == not_selected_placeholder.trim() {
Ok(String::new())
} else {
Ok(new)
}
} else {
Ok(String::new())
}
Expand All @@ -96,7 +114,11 @@ pub fn get_selected_text() -> Result<String, Box<dyn std::error::Error>> {
// Old Content is Empty
write_clipboard.clear()?;
if let Ok(new) = new_text {
Ok(new)
if new.trim() == not_selected_placeholder.trim() {
Ok(String::new())
} else {
Ok(new)
}
} else {
Ok(String::new())
}
Expand All @@ -110,7 +132,7 @@ pub fn get_selected_text() -> Result<String, Box<dyn std::error::Error>> {
Ok(text) => Ok(text),
Err(err) => {
println!("get_selected_text_by_ax error: {}", err);
get_selected_text_by_clipboard()
get_selected_text_by_clipboard_using_applescript()
}
}
}
Expand Down Expand Up @@ -152,7 +174,7 @@ pub fn get_selected_text_by_ax() -> Result<String, Box<dyn std::error::Error>> {
}

#[cfg(target_os = "macos")]
pub fn get_selected_text_by_clipboard() -> Result<String, Box<dyn std::error::Error>> {
pub fn get_selected_text_by_clipboard_using_applescript() -> Result<String, Box<dyn std::error::Error>> {
let apple_script = APP_HANDLE
.get()
.unwrap()
Expand Down
25 changes: 18 additions & 7 deletions src-tauri/src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,24 @@ pub fn get_main_window_always_on_top() -> bool {
#[tauri::command]
pub fn show_main_window_with_selected_text() {
let mut window = show_main_window(false, false);
let selected_text = match utils::get_selected_text() {
Ok(text) => text,
Err(e) => {
eprintln!("Error getting selected text: {}", e);
"".to_string()
}
};
let selected_text;
if cfg!(target_os = "macos") {
selected_text = match utils::get_selected_text_by_clipboard() {
Ok(text) => text,
Err(e) => {
eprintln!("Error getting selected text: {}", e);
"".to_string()
}
};
} else {
selected_text = match utils::get_selected_text() {
Ok(text) => text,
Err(e) => {
eprintln!("Error getting selected text: {}", e);
"".to_string()
}
};
}
if !selected_text.is_empty() {
utils::send_text(selected_text);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/common/components/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,7 @@ export function Settings(props: IPopupProps) {
<FormItem name='defaultTranslateMode' label={t('Default Translate Mode')}>
<TranslateModeSelector onBlur={onBlur} />
</FormItem>
<FormItem name='alwaysShowIcons' label={t('Always show icons')}>
<FormItem name='alwaysShowIcons' label={t('Show icon when text is selected')}>
<AlwaysShowIconsCheckbox onBlur={onBlur} />
</FormItem>
<FormItem name='autoTranslate' label={t('Auto Translate')}>
Expand Down
2 changes: 2 additions & 0 deletions src/common/i18n/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,7 @@
"vocabulary": "Vocabulary",
"generate article": "Generate Article",
"insert to editor": "Insert to Editor",
"Show button when text is selected": "Show button when text is selected",
"Show icon when text is selected": "Show icon when text is selected",
"disable collecting statistics": "Disable collecting statistics"
}
2 changes: 2 additions & 0 deletions src/common/i18n/locales/ja/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,7 @@
"vocabulary": "語彙",
"generate article": "記事を生成",
"insert to editor": "エディタに挿入",
"Show button when text is selected": "テキストが選択されているときにボタンを表示",
"Show icon when text is selected": "テキストが選択されているときにアイコンを表示",
"disable collecting statistics": "統計情報の収集を無効にする"
}
2 changes: 2 additions & 0 deletions src/common/i18n/locales/th/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,7 @@
"vocabulary": "คำศัพท์",
"generate article": "สร้างบทความ",
"insert to editor": "แทรกลงในตัวแก้ไข",
"Show button when text is selected": "แสดงปุ่มเมื่อเลือกข้อความ",
"Show icon when text is selected": "แสดงไอคอนเมื่อเลือกข้อความ",
"disable collecting statistics": "ปิดใช้งานการสถิติการเก็บรวบรวม"
}
2 changes: 2 additions & 0 deletions src/common/i18n/locales/zh-Hans/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,7 @@
"vocabulary": "词汇表",
"generate article": "生成文章",
"insert to editor": "插入编辑器",
"Show button when text is selected": "选中文字时显示按钮",
"Show icon when text is selected": "选中文字时显示图标",
"disable collecting statistics": "禁用统计"
}
2 changes: 2 additions & 0 deletions src/common/i18n/locales/zh-Hant/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,7 @@
"vocabulary": "單詞",
"generate article": "創作文章",
"insert to editor": "插入編輯器",
"Show button when text is selected": "選中文字時顯示按鈕",
"Show icon when text is selected": "選中文字時顯示圖標",
"disable collecting statistics": "停用統計"
}

0 comments on commit 0960e61

Please sign in to comment.