* fix: replace robotjs by nutjs (#2295) * fix: do not create quick ask window on test
This commit is contained in:
parent
1ce3f1158d
commit
1fac400d70
@ -27,6 +27,7 @@ import { setupReactDevTool } from './utils/dev'
|
|||||||
import { cleanLogs } from './utils/log'
|
import { cleanLogs } from './utils/log'
|
||||||
|
|
||||||
import { registerShortcut } from './utils/selectedText'
|
import { registerShortcut } from './utils/selectedText'
|
||||||
|
import { createSystemTray } from './utils/tray'
|
||||||
|
|
||||||
const preloadPath = join(__dirname, 'preload.js')
|
const preloadPath = join(__dirname, 'preload.js')
|
||||||
const rendererPath = join(__dirname, '..', 'renderer')
|
const rendererPath = join(__dirname, '..', 'renderer')
|
||||||
@ -48,33 +49,14 @@ app
|
|||||||
.then(setupMenu)
|
.then(setupMenu)
|
||||||
.then(handleIPCs)
|
.then(handleIPCs)
|
||||||
.then(handleAppUpdates)
|
.then(handleAppUpdates)
|
||||||
.then(createQuickAskWindow)
|
.then(() => process.env.CI !== 'e2e' && createQuickAskWindow())
|
||||||
.then(createMainWindow)
|
.then(createMainWindow)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
if (!app.isPackaged) {
|
if (!app.isPackaged) {
|
||||||
windowManager.mainWindow?.webContents.openDevTools()
|
windowManager.mainWindow?.webContents.openDevTools()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => process.env.CI !== 'e2e' && createSystemTray())
|
||||||
const iconPath = join(app.getAppPath(), 'icons', 'icon-tray.png')
|
|
||||||
const tray = new Tray(iconPath)
|
|
||||||
tray.setToolTip(app.getName())
|
|
||||||
|
|
||||||
const contextMenu = Menu.buildFromTemplate([
|
|
||||||
{
|
|
||||||
label: 'Open Jan',
|
|
||||||
type: 'normal',
|
|
||||||
click: () => windowManager.showMainWindow(),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Open Quick Ask',
|
|
||||||
type: 'normal',
|
|
||||||
click: () => windowManager.showQuickAskWindow(),
|
|
||||||
},
|
|
||||||
{ label: 'Quit', type: 'normal', click: () => app.quit() },
|
|
||||||
])
|
|
||||||
tray.setContextMenu(contextMenu)
|
|
||||||
})
|
|
||||||
.then(() => {
|
.then(() => {
|
||||||
log(`Version: ${app.getVersion()}`)
|
log(`Version: ${app.getVersion()}`)
|
||||||
})
|
})
|
||||||
|
|||||||
@ -41,7 +41,6 @@
|
|||||||
"notarize": {
|
"notarize": {
|
||||||
"teamId": "F8AH6NHVY5"
|
"teamId": "F8AH6NHVY5"
|
||||||
},
|
},
|
||||||
|
|
||||||
"icon": "icons/icon.png"
|
"icon": "icons/icon.png"
|
||||||
},
|
},
|
||||||
"linux": {
|
"linux": {
|
||||||
@ -92,7 +91,7 @@
|
|||||||
"request": "^2.88.2",
|
"request": "^2.88.2",
|
||||||
"request-progress": "^3.0.0",
|
"request-progress": "^3.0.0",
|
||||||
"ulid": "^2.3.0",
|
"ulid": "^2.3.0",
|
||||||
"@hurdlegroup/robotjs": "^0.11.4"
|
"@nut-tree/nut-js": "^4.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@electron/notarize": "^2.1.0",
|
"@electron/notarize": "^2.1.0",
|
||||||
|
|||||||
@ -1,19 +1,24 @@
|
|||||||
import { clipboard, globalShortcut } from "electron";
|
import { clipboard, globalShortcut } from 'electron'
|
||||||
import { keyTap, keys } from "@hurdlegroup/robotjs";
|
import { keyboard, Key } from '@nut-tree/nut-js'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets selected text by synthesizing the keyboard shortcut
|
* Gets selected text by synthesizing the keyboard shortcut
|
||||||
* "CommandOrControl+c" then reading text from the clipboard
|
* "CommandOrControl+c" then reading text from the clipboard
|
||||||
*/
|
*/
|
||||||
export const getSelectedText = async () => {
|
export const getSelectedText = async () => {
|
||||||
const currentClipboardContent = clipboard.readText(); // preserve clipboard content
|
const currentClipboardContent = clipboard.readText() // preserve clipboard content
|
||||||
clipboard.clear();
|
clipboard.clear()
|
||||||
keyTap("c" as keys, process.platform === "darwin" ? "command" : "control");
|
const hotkeys: Key[] = [
|
||||||
await new Promise((resolve) => setTimeout(resolve, 200)); // add a delay before checking clipboard
|
process.platform === 'darwin' ? Key.LeftCmd : Key.LeftControl,
|
||||||
const selectedText = clipboard.readText();
|
Key.C,
|
||||||
clipboard.writeText(currentClipboardContent);
|
]
|
||||||
return selectedText;
|
await keyboard.pressKey(...hotkeys)
|
||||||
};
|
await keyboard.releaseKey(...hotkeys)
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 200)) // add a delay before checking clipboard
|
||||||
|
const selectedText = clipboard.readText()
|
||||||
|
clipboard.writeText(currentClipboardContent)
|
||||||
|
return selectedText
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers a global shortcut of `accelerator`. The `callback` is called
|
* Registers a global shortcut of `accelerator`. The `callback` is called
|
||||||
@ -26,14 +31,14 @@ export const registerShortcut = (
|
|||||||
callback: (selectedText: string) => void
|
callback: (selectedText: string) => void
|
||||||
) => {
|
) => {
|
||||||
return globalShortcut.register(accelerator, async () => {
|
return globalShortcut.register(accelerator, async () => {
|
||||||
callback(await getSelectedText());
|
callback(await getSelectedText())
|
||||||
});
|
})
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unregisters a global shortcut of `accelerator` and
|
* Unregisters a global shortcut of `accelerator` and
|
||||||
* is equivalent to electron.globalShortcut.unregister
|
* is equivalent to electron.globalShortcut.unregister
|
||||||
*/
|
*/
|
||||||
export const unregisterShortcut = (accelerator: Electron.Accelerator) => {
|
export const unregisterShortcut = (accelerator: Electron.Accelerator) => {
|
||||||
globalShortcut.unregister(accelerator);
|
globalShortcut.unregister(accelerator)
|
||||||
};
|
}
|
||||||
|
|||||||
24
electron/utils/tray.ts
Normal file
24
electron/utils/tray.ts
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import { join } from 'path'
|
||||||
|
import { Tray, app, Menu } from 'electron'
|
||||||
|
import { windowManager } from '../managers/window'
|
||||||
|
|
||||||
|
export const createSystemTray = () => {
|
||||||
|
const iconPath = join(app.getAppPath(), 'icons', 'icon-tray.png')
|
||||||
|
const tray = new Tray(iconPath)
|
||||||
|
tray.setToolTip(app.getName())
|
||||||
|
|
||||||
|
const contextMenu = Menu.buildFromTemplate([
|
||||||
|
{
|
||||||
|
label: 'Open Jan',
|
||||||
|
type: 'normal',
|
||||||
|
click: () => windowManager.showMainWindow(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Open Quick Ask',
|
||||||
|
type: 'normal',
|
||||||
|
click: () => windowManager.showQuickAskWindow(),
|
||||||
|
},
|
||||||
|
{ label: 'Quit', type: 'normal', click: () => app.quit() },
|
||||||
|
])
|
||||||
|
tray.setContextMenu(contextMenu)
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user