添加linux支持
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 11 KiB |
+4
-3
@@ -1,6 +1,5 @@
|
||||
const fs = require('fs');
|
||||
const { promisify } = require('util');
|
||||
const toast = require('./notify');
|
||||
const { debounce } = require('lodash');
|
||||
|
||||
const config = {
|
||||
@@ -14,7 +13,8 @@ const config = {
|
||||
extraCssForMini: '',
|
||||
minimizeToTrayWhenClose: true,
|
||||
minimizeToTrayWenStart: true,
|
||||
keepActiveInterval: 60000
|
||||
keepActiveInterval: 60000,
|
||||
skipTrayIcon: false
|
||||
};
|
||||
|
||||
try {
|
||||
@@ -31,7 +31,8 @@ async function flushConfig() {
|
||||
await flush('./config.json', JSON.stringify(config, null, 4));
|
||||
}
|
||||
catch {
|
||||
toast.show("保存配置失败。", { messageType: 'error' });
|
||||
const { notify } = require('./tools');
|
||||
notify("保存配置失败。", { messageType: 'error' });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+36
-87
@@ -1,34 +1,53 @@
|
||||
const path = require('path');
|
||||
const { app, globalShortcut, BrowserWindow, Menu, Tray } = require('electron');
|
||||
const { createMiniWindow, toggleMiniWindowVisibility } = require('./mini');
|
||||
const { createMainWindow, toggleMainWindowVisibility } = require('./main');
|
||||
const { app, globalShortcut, BrowserWindow } = require('electron');
|
||||
const { config } = require('./config');
|
||||
const { createMainWindow } = require('./main');
|
||||
const { createMiniWindow } = require('./mini');
|
||||
const getMenu = require('./menu');
|
||||
const { config, flushConfig } = require('./config');
|
||||
const toast = require('./notify');
|
||||
const main = require('./main');
|
||||
|
||||
//将tray保存到全局变量,防止其被自动回收
|
||||
let tray;
|
||||
const {
|
||||
initialSetup,
|
||||
notify,
|
||||
dismissNotify,
|
||||
toggleMiniShortcut,
|
||||
toggleTrayIcon,
|
||||
} = require('./tools');
|
||||
|
||||
/** @type BrowserWindow */
|
||||
let mainWindow;
|
||||
/** @type BrowserWindow */
|
||||
let miniWindow;
|
||||
|
||||
app.on('ready', () => {
|
||||
setupTray();
|
||||
toggleAutoStart(true);
|
||||
// 保证单一实例
|
||||
const gotTheLock = app.requestSingleInstanceLock()
|
||||
if (!gotTheLock) {
|
||||
app.exit();
|
||||
return;
|
||||
}
|
||||
app.on('second-instance', (event, commandLine, workingDirectory) => {
|
||||
if (mainWindow) {
|
||||
mainWindow.show();
|
||||
if (mainWindow.isMinimized()) {
|
||||
mainWindow.restore();
|
||||
}
|
||||
mainWindow.focus();
|
||||
}
|
||||
})
|
||||
|
||||
app.on('ready', () => {
|
||||
mainWindow = createMainWindow();
|
||||
miniWindow = createMiniWindow();
|
||||
initialSetup(mainWindow, miniWindow);
|
||||
mainWindow.on('close', ev => {
|
||||
if (config.minimizeToTrayWhenClose) {
|
||||
ev.preventDefault();
|
||||
mainWindow.hide();
|
||||
}
|
||||
else {
|
||||
app.quit();
|
||||
}
|
||||
});
|
||||
mainWindow.on('closed', () => {
|
||||
if (miniWindow) {
|
||||
toast.dismiss();
|
||||
dismissNotify();
|
||||
globalShortcut.unregisterAll();
|
||||
miniWindow.destroy();
|
||||
}
|
||||
@@ -51,18 +70,19 @@ app.on('ready', () => {
|
||||
mainWindow.show();
|
||||
}
|
||||
else {
|
||||
toast.show("Google Translate Desktop已在后台运行。", null, {
|
||||
notify("Google Translate Electron已在后台运行。", null, {
|
||||
use: 'notification',
|
||||
clickCallback: () => mainWindow.show()
|
||||
});
|
||||
}
|
||||
toggleTrayIcon(config.skipTrayIcon);
|
||||
registerMiniWindow();
|
||||
const mainMenu = getMenu(mainWindow, miniWindow, mainWindow);
|
||||
mainWindow.setMenu(mainMenu);
|
||||
});
|
||||
|
||||
|
||||
function registerMiniWindow() {
|
||||
miniWindow = createMiniWindow();
|
||||
miniWindow.setParentWindow(mainWindow);
|
||||
const miniMenu = getMenu(mainWindow, miniWindow, miniWindow);
|
||||
miniWindow.setMenu(miniMenu);
|
||||
@@ -71,74 +91,3 @@ function registerMiniWindow() {
|
||||
}
|
||||
}
|
||||
|
||||
function setupTray() {
|
||||
tray = new Tray(path.resolve(__dirname, 'assets/icon.ico'));
|
||||
const contextMenu = Menu.buildFromTemplate([
|
||||
{ label: "显示主界面", click: () => toggleMainWindowVisibility(mainWindow) },
|
||||
{ label: "显示Mini窗口", click: () => toggleMiniWindowVisibility(miniWindow, mainWindow) },
|
||||
{
|
||||
type: 'checkbox',
|
||||
label: "开机启动",
|
||||
checked: config.autoStart,
|
||||
click: menu => menu.checked = toggleAutoStart()
|
||||
},
|
||||
{
|
||||
type: 'checkbox',
|
||||
label: "启用MINI窗口快捷键",
|
||||
checked: config.miniWindowEnabled,
|
||||
click: menu => menu.checked = toggleMiniShortcut()
|
||||
},
|
||||
{ label: "退出", click: () => app.exit() }
|
||||
]);
|
||||
tray.setToolTip("Google Translate Desktop");
|
||||
tray.on('click', () => toggleMainWindowVisibility(mainWindow));
|
||||
tray.setContextMenu(contextMenu);
|
||||
}
|
||||
|
||||
function toggleAutoStart(initial) {
|
||||
if (!initial) {
|
||||
config.autoStart = !config.autoStart;
|
||||
}
|
||||
app.setLoginItemSettings({
|
||||
openAtLogin: config.autoStart,
|
||||
path: process.execPath,
|
||||
openAsHidden: false,
|
||||
args: [
|
||||
'--autoStart'
|
||||
]
|
||||
});
|
||||
if (config.autoStart) {
|
||||
toast.show("已允许Google Translate Desktop在您登录时自动启动。", "提示");
|
||||
}
|
||||
else {
|
||||
toast.show("已禁止Google Translate Desktop在您登录时自动启动。", "提示");
|
||||
}
|
||||
if (!initial) {
|
||||
flushConfig();
|
||||
}
|
||||
return config.autoStart;
|
||||
}
|
||||
|
||||
//启用/禁用mini窗口快捷键
|
||||
function toggleMiniShortcut(initial) {
|
||||
let enabled = initial ? config.miniWindowEnabled : !config.miniWindowEnabled;
|
||||
let success = true;
|
||||
if (enabled) {
|
||||
if (!globalShortcut.register(config.miniShortcut, () => toggleMiniWindowVisibility(miniWindow, mainWindow))) {
|
||||
toast.show("快捷键注册失败,请检查设置的快捷键是否被其他软件占用。", "错误", {
|
||||
type: 'error'
|
||||
});
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
globalShortcut.unregister(config.miniShortcut);
|
||||
}
|
||||
if (success) {
|
||||
config.miniWindowEnabled = enabled;
|
||||
if (!initial) {
|
||||
flushConfig();
|
||||
}
|
||||
}
|
||||
return enabled;
|
||||
}
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@ function createMainWindow() {
|
||||
const win = new BrowserWindow({
|
||||
width: config.mainWindowWidth,
|
||||
height: config.mainWindowHeight,
|
||||
icon: path.resolve(__dirname, './assets/icon.ico'),
|
||||
icon: path.resolve(__dirname, './assets/icon.png'),
|
||||
title: 'Google Translate',
|
||||
show: false,
|
||||
webPreferences: {
|
||||
|
||||
+8
-3
@@ -1,4 +1,5 @@
|
||||
const { Menu } = require('electron');
|
||||
const { app, Menu } = require('electron');
|
||||
const { toggleTrayIcon } = require('./tools');
|
||||
|
||||
function getMenu(/** @type Electron.BrowserWindow */mainWindow, miniWindow, current) {
|
||||
const template = [{
|
||||
@@ -25,8 +26,12 @@ function getMenu(/** @type Electron.BrowserWindow */mainWindow, miniWindow, curr
|
||||
click: () => current.webContents.openDevTools()
|
||||
},
|
||||
{
|
||||
label: "退出",
|
||||
role: 'quit'
|
||||
label: "显示托盘图标",
|
||||
click: () => toggleTrayIcon()
|
||||
},
|
||||
{
|
||||
label: "退出程序",
|
||||
click: () => app.exit(0)
|
||||
}
|
||||
])
|
||||
}];
|
||||
|
||||
+11
-33
@@ -2,13 +2,6 @@ const path = require('path');
|
||||
const { BrowserWindow } = require('electron');
|
||||
const { config } = require('./config');
|
||||
|
||||
const miniControl = {
|
||||
/** 迷你窗口是否处于可视区域 */
|
||||
isMiniWindowVisible: true,
|
||||
previousPos: null
|
||||
}
|
||||
|
||||
|
||||
function createMiniWindow () {
|
||||
let miniWindow = new BrowserWindow({
|
||||
width: 500,
|
||||
@@ -17,7 +10,7 @@ function createMiniWindow () {
|
||||
minHeight: 315,
|
||||
maxWidth: 500,
|
||||
maxHeight: 315,
|
||||
icon: path.resolve(__dirname, './assets/icon.ico'),
|
||||
icon: path.resolve(__dirname, './assets/icon.png'),
|
||||
webPreferences: {
|
||||
nodeIntegration: false
|
||||
},
|
||||
@@ -29,7 +22,6 @@ function createMiniWindow () {
|
||||
alwaysOnTop: true,
|
||||
fullscreenable: false,
|
||||
skipTaskbar: false,
|
||||
|
||||
});
|
||||
|
||||
miniWindow.on('focus', () => {
|
||||
@@ -94,14 +86,11 @@ function createMiniWindow () {
|
||||
miniWindow.insertCSS(config.extraCssForMini);
|
||||
}
|
||||
});
|
||||
miniControl.previousPos = miniWindow.getPosition();
|
||||
toggleMiniWindowVisibility(miniWindow);
|
||||
miniWindow.show();
|
||||
let interval = config.keepActiveInterval | 0;
|
||||
if (miniWindow && interval !== 0) {
|
||||
interval = Math.max(1000, interval);
|
||||
setInterval(() => {
|
||||
if (!miniControl.isMiniWindowVisible) {
|
||||
if (!miniWindow.isVisible()) {
|
||||
miniWindow.webContents.executeJavaScript(`
|
||||
var source = document.getElementById('source');
|
||||
if (source) {
|
||||
@@ -114,28 +103,17 @@ function createMiniWindow () {
|
||||
return miniWindow;
|
||||
}
|
||||
|
||||
function toggleMiniWindowVisibility(window, mainWindow) {
|
||||
if (!window) { return; }
|
||||
miniControl.isMiniWindowVisible = !miniControl.isMiniWindowVisible;
|
||||
if (miniControl.isMiniWindowVisible) {
|
||||
if (miniControl.previousPos) {
|
||||
window.setPosition(...miniControl.previousPos);
|
||||
}
|
||||
else {
|
||||
window.show();
|
||||
}
|
||||
if (!mainWindow || !mainWindow.isVisible()) {
|
||||
window.setSkipTaskbar(false);
|
||||
}
|
||||
clearInput(window);
|
||||
focusInput(window);
|
||||
window.focus();
|
||||
function toggleMiniWindowVisibility(miniWindow, mainWindow) {
|
||||
if (!miniWindow) { return; }
|
||||
if (miniWindow.isVisible()) {
|
||||
miniWindow.hide();
|
||||
clearInput(miniWindow);
|
||||
}
|
||||
else {
|
||||
miniControl.previousPos = window.getPosition();
|
||||
window.setPosition(-1000, -1000, false);
|
||||
window.setSkipTaskbar(true);
|
||||
clearInput(window);
|
||||
miniWindow.show();
|
||||
clearInput(miniWindow);
|
||||
focusInput(miniWindow);
|
||||
miniWindow.focus();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
const path = require('path');
|
||||
const { Notification, dialog } = require('electron');
|
||||
|
||||
let current = null;
|
||||
|
||||
function notify(message, title, ops) {
|
||||
const use = ops ? ops.use : 'auto';
|
||||
if (use === 'dialog' || (!Notification.isSupported() && use !== 'notification')) {
|
||||
let options = { message, title: title || "信息" };
|
||||
if (ops && ops.messageType) {
|
||||
options.type = ops.messageType;
|
||||
}
|
||||
dialog.showMessageBox(null, options);
|
||||
return;
|
||||
}
|
||||
if (current) {
|
||||
current.close();
|
||||
}
|
||||
current = new Notification({
|
||||
title: "Google Translate Desktop",
|
||||
body: message,
|
||||
subtitle: ops && ops.subtitle,
|
||||
icon: path.resolve(__dirname, './assets/icon.ico')
|
||||
});
|
||||
current.show();
|
||||
if (ops && ops.clickCallback) {
|
||||
current.on('click', ops.clickCallback);
|
||||
}
|
||||
}
|
||||
|
||||
function dismiss() {
|
||||
current && current.close();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
show: notify,
|
||||
dismiss
|
||||
};
|
||||
+151
@@ -0,0 +1,151 @@
|
||||
const { app, globalShortcut, ipcMain } = require('electron');
|
||||
const path = require('path');
|
||||
const { Menu, Tray, Notification, dialog } = require('electron');
|
||||
const { config, flushConfig } = require('./config');
|
||||
const { toggleMainWindowVisibility } = require('./main');
|
||||
const { toggleMiniWindowVisibility } = require('./mini');
|
||||
|
||||
|
||||
let mainWindow, miniWindow, tray, currentNotification;
|
||||
|
||||
function initialSetup(_mainWindow, _miniWindow) {
|
||||
mainWindow = _mainWindow;
|
||||
miniWindow = _miniWindow;
|
||||
}
|
||||
//启用/禁用mini窗口快捷键
|
||||
function toggleMiniShortcut(initial) {
|
||||
let enabled = initial ? config.miniWindowEnabled : !config.miniWindowEnabled;
|
||||
let success = true;
|
||||
if (enabled) {
|
||||
if (!globalShortcut.register(config.miniShortcut, () => toggleMiniWindowVisibility(miniWindow, mainWindow))) {
|
||||
notify("快捷键注册失败,请检查设置的快捷键是否被其他软件占用。", "错误", {
|
||||
type: 'error'
|
||||
});
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
globalShortcut.unregister(config.miniShortcut);
|
||||
}
|
||||
if (success) {
|
||||
config.miniWindowEnabled = enabled;
|
||||
if (!initial) {
|
||||
flushConfig();
|
||||
}
|
||||
}
|
||||
return enabled;
|
||||
}
|
||||
|
||||
//切换是否自动启动
|
||||
function toggleAutoStart(initial) {
|
||||
if (!initial) {
|
||||
config.autoStart = !config.autoStart;
|
||||
}
|
||||
app.setLoginItemSettings({
|
||||
openAtLogin: config.autoStart,
|
||||
path: process.execPath,
|
||||
openAsHidden: false,
|
||||
args: [
|
||||
'--autoStart'
|
||||
]
|
||||
});
|
||||
if (config.autoStart) {
|
||||
notify("已允许Google Translate Electron在您登录时自动启动。", "提示");
|
||||
}
|
||||
else {
|
||||
notify("已禁止Google Translate Electron在您登录时自动启动。", "提示");
|
||||
}
|
||||
if (!initial) {
|
||||
flushConfig();
|
||||
}
|
||||
return config.autoStart;
|
||||
}
|
||||
|
||||
function notify(message, title, ops) {
|
||||
const use = ops ? ops.use : 'auto';
|
||||
if (use === 'dialog' || (!Notification.isSupported() && use !== 'notification')) {
|
||||
let options = { message, title: title || "信息" };
|
||||
if (ops && ops.messageType) {
|
||||
options.type = ops.messageType;
|
||||
}
|
||||
dialog.showMessageBox(null, options);
|
||||
return;
|
||||
}
|
||||
if (currentNotification) {
|
||||
currentNotification.close();
|
||||
}
|
||||
let extName = process.platform === 'win32' ? 'ico' : 'png';
|
||||
currentNotification = new Notification({
|
||||
title: "Google Translate Electron",
|
||||
body: message,
|
||||
subtitle: ops && ops.subtitle,
|
||||
icon: path.resolve(__dirname, './assets/icon.' + extName)
|
||||
});
|
||||
currentNotification.show();
|
||||
if (ops && ops.clickCallback) {
|
||||
currentNotification.on('click', ops.clickCallback);
|
||||
}
|
||||
}
|
||||
|
||||
function dismissNotify() {
|
||||
currentNotification && currentNotification.close();
|
||||
}
|
||||
|
||||
function setupTrayIcon() {
|
||||
if (tray) { return; }
|
||||
let extName = process.platform === 'win32' ? 'ico' : 'png';
|
||||
tray = new Tray(path.resolve(__dirname, 'assets/icon.' + extName));
|
||||
const contextMenu = Menu.buildFromTemplate([
|
||||
{ label: "显示主界面", click: () => toggleMainWindowVisibility(mainWindow) },
|
||||
{ label: "显示Mini窗口", click: () => toggleMiniWindowVisibility(miniWindow, mainWindow) },
|
||||
{
|
||||
type: 'checkbox',
|
||||
label: "开机启动",
|
||||
checked: config.autoStart,
|
||||
click: menu => menu.checked = toggleAutoStart()
|
||||
},
|
||||
{
|
||||
type: 'checkbox',
|
||||
label: "启用MINI窗口快捷键",
|
||||
checked: config.miniWindowEnabled,
|
||||
click: menu => menu.checked = toggleMiniShortcut()
|
||||
},
|
||||
{ label: "退出", click: () => app.exit() }
|
||||
]);
|
||||
|
||||
tray.setTitle("Google Translate Electron");
|
||||
tray.setToolTip("Google Translate Electron");
|
||||
tray.on('click', () => toggleMainWindowVisibility(mainWindow));
|
||||
tray.setContextMenu(contextMenu);
|
||||
}
|
||||
function destroyTrayIcon() {
|
||||
if (tray) {
|
||||
tray.destroy();
|
||||
}
|
||||
tray = null;
|
||||
}
|
||||
function toggleTrayIcon(show) {
|
||||
if (typeof show === 'boolean') {
|
||||
config.skipTrayIcon = show;
|
||||
}
|
||||
else {
|
||||
config.skipTrayIcon = !config.skipTrayIcon;
|
||||
}
|
||||
if (config.skipTrayIcon) {
|
||||
destroyTrayIcon();
|
||||
}
|
||||
else {
|
||||
setupTrayIcon();
|
||||
}
|
||||
flushConfig();
|
||||
}
|
||||
|
||||
|
||||
module.exports = {
|
||||
initialSetup,
|
||||
notify,
|
||||
dismissNotify,
|
||||
toggleTrayIcon,
|
||||
toggleAutoStart,
|
||||
toggleMiniShortcut,
|
||||
}
|
||||
Reference in New Issue
Block a user