From 0acfeff006d7de125ad9da55183a8f5497fb7d01 Mon Sep 17 00:00:00 2001 From: lyswhut Date: Fri, 18 Mar 2022 10:56:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=89=98=E7=9B=98=E8=8F=9C?= =?UTF-8?q?=E5=8D=95=E6=93=8D=E4=BD=9C#686?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/events/MainWindow.js | 8 +++++ src/main/events/_name.js | 2 ++ src/main/modules/tray.js | 49 ++++++++++++++++++----------- src/main/rendererEvents/winEvent.js | 5 +++ 4 files changed, 45 insertions(+), 19 deletions(-) diff --git a/src/main/events/MainWindow.js b/src/main/events/MainWindow.js index 59b228a0..27c72859 100644 --- a/src/main/events/MainWindow.js +++ b/src/main/events/MainWindow.js @@ -26,6 +26,14 @@ class MainWindow extends EventEmitter { this.emit(MAIN_WINDOW_EVENT_NAME.show) } + focus() { + this.emit(MAIN_WINDOW_EVENT_NAME.focus) + } + + blur() { + this.emit(MAIN_WINDOW_EVENT_NAME.blur) + } + hide() { this.emit(MAIN_WINDOW_EVENT_NAME.hide) } diff --git a/src/main/events/_name.js b/src/main/events/_name.js index ea4c5154..4492dc62 100644 --- a/src/main/events/_name.js +++ b/src/main/events/_name.js @@ -14,6 +14,8 @@ exports.mainWindow = { ready_to_show: 'ready_to_show', show: 'show', hide: 'hide', + focus: 'focus', + blur: 'blur', } exports.tray = { diff --git a/src/main/modules/tray.js b/src/main/modules/tray.js index d6c081cc..1ba4b286 100644 --- a/src/main/modules/tray.js +++ b/src/main/modules/tray.js @@ -1,5 +1,5 @@ const { app, Tray, Menu, nativeImage } = require('electron') -// const { isWin } = require('../../common/utils') +const { isWin } = require('@common/utils') const { tray: TRAY_EVENT_NAME, common: COMMON_EVENT_NAME, mainWindow: MAIN_WINDOW_NAME } = require('../events/_name') const path = require('path') let isEnableTray = null @@ -40,6 +40,14 @@ global.lx_event.mainWindow.on(MAIN_WINDOW_NAME.ready_to_show, () => { global.lx_event.mainWindow.on(MAIN_WINDOW_NAME.show, () => { createMenu(global.modules.tray) }) +if (!isWin) { + global.lx_event.mainWindow.on(MAIN_WINDOW_NAME.focus, () => { + createMenu(global.modules.tray) + }) + global.lx_event.mainWindow.on(MAIN_WINDOW_NAME.blur, () => { + createMenu(global.modules.tray) + }) +} global.lx_event.mainWindow.on(MAIN_WINDOW_NAME.hide, () => { createMenu(global.modules.tray) }) @@ -75,24 +83,27 @@ const destroyTray = () => { const createMenu = tray => { if (!global.modules.tray) return let menu = [] - global.modules.mainWindow && menu.push(global.modules.mainWindow.isVisible() - ? { - label: '隐藏主界面', - click() { - global.modules.mainWindow.hide() - }, - } - : { - label: '显示主界面', - click() { - if (!global.modules.mainWindow) return - if (!global.modules.mainWindow.isVisible()) { - global.modules.mainWindow.show() - } - global.modules.mainWindow.restore() - global.modules.mainWindow.focus() - }, - }) + if (global.modules.mainWindow) { + const isShow = global.modules.mainWindow.isVisible() && (isWin ? true : global.modules.mainWindow.isFocused()) + menu.push(isShow + ? { + label: '隐藏主界面', + click() { + global.modules.mainWindow.hide() + }, + } + : { + label: '显示主界面', + click() { + if (!global.modules.mainWindow) return + if (!global.modules.mainWindow.isVisible()) { + global.modules.mainWindow.show() + } + global.modules.mainWindow.restore() + global.modules.mainWindow.focus() + }, + }) + } menu.push(global.appSetting.desktopLyric.enable ? { label: '关闭桌面歌词', diff --git a/src/main/rendererEvents/winEvent.js b/src/main/rendererEvents/winEvent.js index 6e983023..205d82fe 100644 --- a/src/main/rendererEvents/winEvent.js +++ b/src/main/rendererEvents/winEvent.js @@ -26,6 +26,11 @@ module.exports = mainWindow => { // }) mainWindow.on('focus', () => { mainSend(mainWindow, ipcMainWindowNames.focus) + global.lx_event.mainWindow.focus() + }) + + mainWindow.on('blur', () => { + global.lx_event.mainWindow.blur() }) mainWindow.once('ready-to-show', () => {