fix: 改进用户体验

定期执行自动操作保持mini窗口活跃防止长时间无操作后唤醒卡顿;
主窗口隐藏时允许mini窗口出现在Windows切换视图中(取消skipTaskBar标识)以快速切换;
This commit is contained in:
2020-06-13 14:11:06 +08:00
parent a7a76dfde2
commit dc054e40db
5 changed files with 107 additions and 61 deletions
+1
View File
@@ -14,6 +14,7 @@ const config = {
extraCssForMini: '',
minimizeToTrayWhenClose: true,
minimizeToTrayWenStart: true,
keepActiveInterval: 60000
};
try {
+9 -32
View File
@@ -1,7 +1,7 @@
const path = require('path');
const { app, globalShortcut, BrowserWindow, Menu, Tray } = require('electron');
const createMiniWindow = require('./mini');
const createMainWindow = require('./main');
const { createMiniWindow, toggleMiniWindowVisibility } = require('./mini');
const { createMainWindow, toggleMainWindowVisibility } = require('./main');
const getMenu = require('./menu');
const { config, flushConfig } = require('./config');
const toast = require('./notify');
@@ -36,12 +36,14 @@ app.on('ready', () => {
mainWindow.on('hide', () => {
if (miniWindow) {
miniWindow.setParentWindow(mainWindow);
miniWindow.setSkipTaskbar(false);
}
});
//主窗口显示时如果mini窗口的父窗口是主窗口,mini窗口获得焦点时父窗口也会弹出来。。emm又没找到其他靠谱的方法
mainWindow.on('show', () => {
if (miniWindow) {
miniWindow.setParentWindow(null);
miniWindow.setSkipTaskbar(true);
}
});
if (!config.minimizeToTrayWenStart) {
@@ -59,10 +61,7 @@ app.on('ready', () => {
});
function registerMiniWindow() {
// if (!config.miniWindowEnabled) {
// return;
// }
miniWindow = createMiniWindow();
miniWindow = createMiniWindow();
miniWindow.setParentWindow(mainWindow);
const miniMenu = getMenu(mainWindow, miniWindow, miniWindow);
miniWindow.setMenu(miniMenu);
@@ -74,8 +73,8 @@ function registerMiniWindow() {
function setupTray() {
tray = new Tray(path.resolve(__dirname, 'assets/icon.ico'));
const contextMenu = Menu.buildFromTemplate([
{ label: "显示主界面", click: () => toggleMainWindowVisibility() },
{ label: "显示Mini窗口", click: () => toggleMiniWindowVisibility() },
{ label: "显示主界面", click: () => toggleMainWindowVisibility(mainWindow) },
{ label: "显示Mini窗口", click: () => toggleMiniWindowVisibility(miniWindow) },
{
type: 'checkbox',
label: "开机启动",
@@ -91,7 +90,7 @@ function setupTray() {
{ label: "退出", click: () => app.exit() }
]);
tray.setToolTip("Google Translate Desktop");
tray.on('click', () => toggleMainWindowVisibility());
tray.on('click', () => toggleMainWindowVisibility(mainWindow));
tray.setContextMenu(contextMenu);
}
@@ -124,7 +123,7 @@ function toggleMiniShortcut(initial) {
let enabled = initial ? config.miniWindowEnabled : !config.miniWindowEnabled;
let success = true;
if (enabled) {
if (!globalShortcut.register(config.miniShortcut, () => toggleMiniWindowVisibility())) {
if (!globalShortcut.register(config.miniShortcut, () => toggleMiniWindowVisibility(miniWindow))) {
toast.show("快捷键注册失败,请检查设置的快捷键是否被其他软件占用。", "错误", {
type: 'error'
});
@@ -142,25 +141,3 @@ function toggleMiniShortcut(initial) {
}
return enabled;
}
function toggleMiniWindowVisibility() {
if (!miniWindow) { return; }
if (miniWindow.isVisible()) {
miniWindow.hide();
}
else {
miniWindow.show();
miniWindow.focus();
}
}
function toggleMainWindowVisibility() {
if (!mainWindow) { return; }
if (mainWindow.isVisible()) {
mainWindow.hide();
}
else {
mainWindow.show();
mainWindow.focus();
}
}
+16 -2
View File
@@ -1,6 +1,5 @@
const path = require('path');
const { BrowserWindow } = require('electron');
const getMenu = require('./menu');
const { config } = require('./config');
function createMainWindow() {
@@ -36,4 +35,19 @@ function createMainWindow() {
return win;
}
module.exports = createMainWindow;
function toggleMainWindowVisibility(window) {
if (!window) { return; }
if (window.isVisible()) {
window.hide();
}
else {
window.show();
window.focus();
}
}
module.exports = {
createMainWindow,
toggleMainWindowVisibility
}
+71 -24
View File
@@ -1,8 +1,14 @@
const path = require('path');
const { BrowserWindow } = require('electron');
const getMenu = require('./menu');
const { config } = require('./config');
const miniControl = {
/** 迷你窗口是否处于可视区域 */
isMiniWindowVisible: true,
previousPos: null
}
function createMiniWindow () {
let miniWindow = new BrowserWindow({
width: 500,
@@ -22,7 +28,8 @@ function createMiniWindow () {
maximizable: true,
alwaysOnTop: true,
fullscreenable: false,
skipTaskbar: true,
skipTaskbar: false,
});
miniWindow.on('focus', () => {
@@ -33,28 +40,11 @@ function createMiniWindow () {
});
miniWindow.on('close', ev => {
ev.preventDefault();
miniWindow.hide();
toggleMiniWindowVisibility(miniWindow);
});
miniWindow.loadURL(config.url);
miniWindow.webContents.on('dom-ready', () => {
miniWindow.webContents.executeJavaScript(`
setTimeout(() => {
console.log('throw');
throw('crashed');
}, 10000);
var source = document.getElementById('source');
if (source) {
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'hidden') {
source.value = '';
}
else {
source.focus();
}
});
}
`);
miniWindow.webContents.insertCSS(`
/* 隐藏通知,文本/文件切换栏 */
.container .input-button-container,
@@ -62,8 +52,9 @@ function createMiniWindow () {
display: none !important;
}
/* mini窗口隐藏header和历史记录按钮和拼音 */
.tlid-source-transliteration-container.source-transliteration-container,
header.gb_sa.gb_2a.gb_5e.gb_ta, .gp-footer {
header,
.gp-footer,
.tlid-source-transliteration-container.source-transliteration-container {
display: none !important;
}
/* 防止高度较小时出现双滚动条 */
@@ -103,8 +94,64 @@ 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) {
miniWindow.webContents.executeJavaScript(`
var source = document.getElementById('source');
if (source) {
source.value = source.value;
}
`);
}
}, interval);
}
return miniWindow;
}
module.exports = createMiniWindow;
function toggleMiniWindowVisibility(window) {
if (!window) { return; }
miniControl.isMiniWindowVisible = !miniControl.isMiniWindowVisible;
if (miniControl.isMiniWindowVisible) {
if (miniControl.previousPos) {
window.setPosition(...miniControl.previousPos);
}
else {
window.show();
}
focusInput(window);
window.focus();
}
else {
miniControl.previousPos = window.getPosition();
window.setPosition(-1000, -1000, false);
clearInput(window);
}
}
function clearInput(window) {
window.webContents.executeJavaScript(`
var source = document.getElementById('source');
if (source) {
source.value = '';
}
`);
}
function focusInput (window) {
window.webContents.executeJavaScript(`
var source = document.getElementById('source');
if (source) {
source.focus();
}
`);
}
module.exports = {
createMiniWindow,
toggleMiniWindowVisibility
}