From 7a1ded34b29ae88f642eda85d2f66eb6879265b2 Mon Sep 17 00:00:00 2001 From: Kiran Niranjan Date: Tue, 16 Jul 2019 10:59:50 +0530 Subject: [PATCH] fix: ELECTRON-1380 (Show pop-up menu only on main window) (#732) * ELECTRON-1380 - Show pop-up menu only on main window * ELECTRON-1380 - Fix unit tests --- spec/mainApiHandler.spec.ts | 2 ++ src/app/main-api-handler.ts | 7 ++++--- src/app/window-actions.ts | 4 ++-- src/app/window-utils.ts | 3 +-- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/spec/mainApiHandler.spec.ts b/spec/mainApiHandler.spec.ts index 53357065..b31c1e28 100644 --- a/spec/mainApiHandler.spec.ts +++ b/spec/mainApiHandler.spec.ts @@ -55,6 +55,7 @@ jest.mock('../src/app/window-utils', () => { showBadgeCount: jest.fn(), showPopupMenu: jest.fn(), updateLocale: jest.fn(), + windowExists: jest.fn( () => true), }; }); @@ -243,6 +244,7 @@ describe('main api handler', () => { it('should call `popupMenu` correctly', () => { const fromWebContentsMocked = { isDestroyed: jest.fn(), + winName: apiName.mainWindowName, }; const spy = jest.spyOn(utils, 'showPopupMenu'); const value = { diff --git a/src/app/main-api-handler.ts b/src/app/main-api-handler.ts index ad5da42d..d294c8c6 100644 --- a/src/app/main-api-handler.ts +++ b/src/app/main-api-handler.ts @@ -10,7 +10,7 @@ import { memoryMonitor } from './memory-monitor'; import { protocolHandler } from './protocol-handler'; import { screenSnippet } from './screen-snippet-handler'; import { activate, handleKeyPress } from './window-actions'; -import { windowHandler } from './window-handler'; +import { ICustomBrowserWindow, windowHandler } from './window-handler'; import { downloadManagerAction, isValidWindow, @@ -19,6 +19,7 @@ import { showBadgeCount, showPopupMenu, updateLocale, + windowExists, } from './window-utils'; /** @@ -94,8 +95,8 @@ ipcMain.on(apiName.symphonyApi, (event: Electron.Event, arg: IApiArgs) => { } break; case apiCmds.popupMenu: { - const browserWin = BrowserWindow.fromWebContents(event.sender); - if (browserWin && !browserWin.isDestroyed()) { + const browserWin = BrowserWindow.fromWebContents(event.sender) as ICustomBrowserWindow; + if (browserWin && windowExists(browserWin) && browserWin.winName === apiName.mainWindowName) { showPopupMenu({ window: browserWin }); } break; diff --git a/src/app/window-actions.ts b/src/app/window-actions.ts index e8a93802..e0faff3e 100644 --- a/src/app/window-actions.ts +++ b/src/app/window-actions.ts @@ -149,8 +149,8 @@ export const handleKeyPress = (key: number): void => { if (isMac) { return; } - const browserWin = BrowserWindow.getFocusedWindow(); - if (browserWin && !browserWin.isDestroyed()) { + const browserWin = BrowserWindow.getFocusedWindow() as ICustomBrowserWindow; + if (browserWin && windowExists(browserWin) && browserWin.winName === apiName.mainWindowName) { logger.info(`window-actions: popping up menu by alt key action`); showPopupMenu({ window: browserWin }); } diff --git a/src/app/window-utils.ts b/src/app/window-utils.ts index c050d2c3..4a6b34a4 100644 --- a/src/app/window-utils.ts +++ b/src/app/window-utils.ts @@ -5,7 +5,6 @@ import * as filesize from 'filesize'; import * as fs from 'fs'; import * as path from 'path'; import { format, parse } from 'url'; -import { apiName } from '../common/api-interface'; import { isDevEnv, isMac } from '../common/env'; import { i18n, LocaleType } from '../common/i18n'; @@ -252,7 +251,7 @@ export const updateLocale = (locale: LocaleType): void => { */ export const showPopupMenu = (opts: Electron.PopupOptions): void => { const mainWindow = windowHandler.getMainWindow(); - if (mainWindow && windowExists(mainWindow) && isValidWindow(mainWindow) && mainWindow.winName === apiName.mainWindowName) { + if (mainWindow && windowExists(mainWindow) && isValidWindow(mainWindow)) { const coordinates = windowHandler.isCustomTitleBar ? { x: 20, y: 15 } : { x: 10, y: -20 }; const { x, y } = mainWindow.isFullScreen() ? { x: 0, y: 0 } : coordinates; const popupOpts = { window: mainWindow, x, y };