From 0476d3efb83354644f3351dcafa4e478e2b48dcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillermo=20Vay=C3=A1?= Date: Fri, 24 Sep 2021 15:30:17 +0200 Subject: [PATCH] remove global shortcuts (#1740) * remove global shortcuts * Add global shortcuts only when the app is in focus Co-authored-by: Devin Binnie --- src/main/main.ts | 13 +------------ src/main/windows/mainWindow.ts | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/main/main.ts b/src/main/main.ts index 19329592..49e3d78a 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -7,7 +7,7 @@ import fs from 'fs'; import path from 'path'; -import electron, {BrowserWindow, globalShortcut, IpcMainEvent, IpcMainInvokeEvent, Rectangle} from 'electron'; +import electron, {BrowserWindow, IpcMainEvent, IpcMainInvokeEvent, Rectangle} from 'electron'; import isDev from 'electron-is-dev'; import installExtension, {REACT_DEVELOPER_TOOLS} from 'electron-devtools-installer'; import log from 'electron-log'; @@ -41,7 +41,6 @@ import { SHOW_EDIT_SERVER_MODAL, SHOW_REMOVE_SERVER_MODAL, UPDATE_SHORTCUT_MENU, - OPEN_TEAMS_DROPDOWN, UPDATE_LAST_ACTIVE, GET_AVAILABLE_SPELL_CHECKER_LANGUAGES, } from 'common/communication'; @@ -798,16 +797,6 @@ function initializeAfterAppReady() { // is the requesting url trusted? callback(urlUtils.isTrustedURL(requestingURL, config.teams)); }); - - globalShortcut.register(`${process.platform === 'darwin' ? 'Cmd+Ctrl' : 'Ctrl+Shift'}+S`, () => { - ipcMain.emit(OPEN_TEAMS_DROPDOWN); - }); - - if (process.platform === 'linux') { - globalShortcut.registerAll(['Alt+F', 'Alt+E', 'Alt+V', 'Alt+H', 'Alt+W', 'Alt+P'], () => { - // do nothing because we want to supress the menu popping up - }); - } } // diff --git a/src/main/windows/mainWindow.ts b/src/main/windows/mainWindow.ts index 3731fbeb..07a33490 100644 --- a/src/main/windows/mainWindow.ts +++ b/src/main/windows/mainWindow.ts @@ -6,13 +6,13 @@ import fs from 'fs'; import path from 'path'; import os from 'os'; -import {app, BrowserWindow, BrowserWindowConstructorOptions, ipcMain} from 'electron'; +import {app, BrowserWindow, BrowserWindowConstructorOptions, globalShortcut, ipcMain} from 'electron'; import log from 'electron-log'; import {CombinedConfig} from 'types/config'; import {SavedWindowState} from 'types/mainWindow'; -import {SELECT_NEXT_TAB, SELECT_PREVIOUS_TAB, GET_FULL_SCREEN_STATUS} from 'common/communication'; +import {SELECT_NEXT_TAB, SELECT_PREVIOUS_TAB, GET_FULL_SCREEN_STATUS, OPEN_TEAMS_DROPDOWN} from 'common/communication'; import {DEFAULT_WINDOW_HEIGHT, DEFAULT_WINDOW_WIDTH, MINIMUM_WINDOW_HEIGHT, MINIMUM_WINDOW_WIDTH} from 'common/utils/constants'; import * as Validator from '../Validator'; @@ -174,6 +174,21 @@ function createMainWindow(config: CombinedConfig, options: {linuxAppIcon: string } }); + // Only add shortcuts when window is in focus + mainWindow.on('focus', () => { + if (process.platform === 'linux') { + globalShortcut.registerAll(['Alt+F', 'Alt+E', 'Alt+V', 'Alt+H', 'Alt+W', 'Alt+P'], () => { + // do nothing because we want to supress the menu popping up + }); + } + globalShortcut.register(`${process.platform === 'darwin' ? 'Cmd+Ctrl' : 'Ctrl+Shift'}+S`, () => { + ipcMain.emit(OPEN_TEAMS_DROPDOWN); + }); + }); + mainWindow.on('blur', () => { + globalShortcut.unregisterAll(); + }); + const contextMenu = new ContextMenu({}, mainWindow); contextMenu.reload();