Skip to content
This repository has been archived by the owner on Apr 29, 2022. It is now read-only.

Add translation of selected text by hotkey #328

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/linux-arm64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ jobs:
uses: samuelmeuli/action-snapcraft@v1
with:
snapcraft_token: ${{ secrets.SNAP_TOKEN }}
- name: Install libxtst-dev
run: sudo apt-get install libxtst-dev
- name: Set up Node.js
uses: actions/setup-node@v1
with:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/linux-x64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ jobs:
uses: samuelmeuli/action-snapcraft@v1
with:
snapcraft_token: ${{ secrets.SNAP_TOKEN }}
- name: Install libxtst-dev
run: sudo apt-get install libxtst-dev
- name: Set up Node.js
uses: actions/setup-node@v1
with:
Expand Down
29 changes: 23 additions & 6 deletions main-src/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const {
} = require('electron');
const isDev = require('electron-is-dev');
const settings = require('electron-settings');
const { keyTap } = require('robotjs');

settings.configure({
fileName: 'Settings', // backward compatible with electron-settings@3
Expand Down Expand Up @@ -108,6 +109,21 @@ if (!gotTheLock) {
app.setAsDefaultProtocolClient('translatium');
}

const getSelectedText = async () => {
// get current text in clipboard
const currentClipboardContent = clipboard.readText();

// save selected text (by saving it to clipboard)
keyTap('c', process.platform === 'darwin' ? 'command' : 'control');
await new Promise((resolve) => setTimeout(resolve, 200));
const selectedText = clipboard.readText();

// restore clipboard
clipboard.writeText(currentClipboardContent);

return selectedText || currentClipboardContent;
};

// Load listeners
loadListeners();

Expand Down Expand Up @@ -230,15 +246,16 @@ if (!gotTheLock) {
try {
globalShortcut.register(combinator, () => {
if (isHidden) {
mb.showWindow();
const translateClipboardOnShortcut = getPreference('translateClipboardOnShortcut');
if (translateClipboardOnShortcut) {
const text = clipboard.readText();
if (text.length > 0) {
mb.window.send('set-input-text', text);
mb.window.send('go-to-home');
}
getSelectedText().then((text) => {
if (text.length > 0) {
mb.window.send('set-input-text', text);
mb.window.send('go-to-home');
}
});
}
mb.showWindow();
} else {
mb.hideWindow();
}
Expand Down
4 changes: 2 additions & 2 deletions main-src/libs/locales/en/ui.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@
"general": "General",
"advanced": "Advanced",
"alwaysOnTop": "Keep window always on top",
"translateClipboardOnShortcut": "Translate clipboard on shortcut",
"translateClipboardOnShortcutDesc": "When you open {appName} with a shortcut, it will automatically translate your clipboard contents.",
"translateClipboardOnShortcut": "Translate selection or clipboard on shortcut",
"translateClipboardOnShortcutDesc": "When you open {appName} with a shortcut, it will automatically translate your selected or clipboard contents.",
"openKeyboardShortcut": "Define a keyboard shortcut to open {appName} quickly",
"typeNewKeyboardCombinator": "Type the new keyboard combination.",
"useHardwareAcceleration": "Use hardware acceleration when available",
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"node": "14"
},
"scripts": {
"postinstall": "npx patch-package && yarn generate-open-source-notices",
"postinstall": "npx patch-package && electron-builder install-app-deps && yarn generate-open-source-notices",
"generate-open-source-notices": "yarn licenses generate-disclaimer > public/open-source-notices.txt",
"start": "cross-env BROWSER=none react-scripts start",
"electron-dev": "concurrently \"yarn start\" \"wait-on http://localhost:3000 && electron .\" -k",
Expand All @@ -27,7 +27,9 @@
"test": "mocha"
},
"dependencies": {
"node-mac-permissions": "2.2.0"
"node-loader": "^2.0.0",
"node-mac-permissions": "2.2.0",
"robotjs": "0.6.0"
},
"devDependencies": {
"@fontsource/roboto": "4.4.5",
Expand Down
521 changes: 479 additions & 42 deletions public/open-source-notices.txt

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ const getElectronMainConfig = () => {
},
devtool: 'source-map',
plugins,
module: {
rules: [
{
test: /\.node$/,
use: 'node-loader'
},
]
}
};
};

Expand Down
Loading