diff --git a/electron/renderer/src/components/App.jsx b/electron/renderer/src/components/App.jsx index c0d013405af..87cae3472e3 100644 --- a/electron/renderer/src/components/App.jsx +++ b/electron/renderer/src/components/App.jsx @@ -53,6 +53,8 @@ class App extends React.Component { if (accountId) { this.props.switchAccount(accountId); } + + event.target.focus(); } initiateSSO(event) { diff --git a/electron/src/main.ts b/electron/src/main.ts index 8d20cdc6564..5442e5d5e06 100644 --- a/electron/src/main.ts +++ b/electron/src/main.ts @@ -252,7 +252,7 @@ const showMainWindow = async (mainWindowState: WindowStateKeeper.State) => { main.flashFrame(false); }); - main.on('blur', () => systemMenu.unregisterShortcuts()); + main.on('blur', () => systemMenu.unregisterGlobalShortcuts()); main.on('page-title-updated', () => tray.showUnreadCount(main)); @@ -269,7 +269,7 @@ const showMainWindow = async (mainWindowState: WindowStateKeeper.State) => { main.hide(); } } - systemMenu.unregisterShortcuts(); + systemMenu.unregisterGlobalShortcuts(); }); main.webContents.on('crashed', event => { diff --git a/electron/src/menu/system.ts b/electron/src/menu/system.ts index a41472a9d61..3a4c21bbdd5 100644 --- a/electron/src/menu/system.ts +++ b/electron/src/menu/system.ts @@ -19,10 +19,12 @@ import autoLaunch = require('auto-launch'); import {Menu, MenuItemConstructorOptions, dialog, globalShortcut, ipcMain, shell} from 'electron'; +import * as path from 'path'; import {EVENT_TYPE} from '../lib/eventType'; import {WebViewFocus} from '../lib/webViewFocus'; import * as locale from '../locale/locale'; +import {getLogger} from '../logging/getLogger'; import * as lifecycle from '../runtime/lifecycle'; import {config} from '../settings/config'; import {settings} from '../settings/ConfigurationPersistence'; @@ -34,7 +36,7 @@ import * as EnvironmentUtil from '../runtime/EnvironmentUtil'; const launchCmd = process.env.APPIMAGE || process.execPath; -let menu: Menu; +const logger = getLogger(path.basename(__filename)); const launcher = new autoLaunch({ isHidden: true, @@ -437,26 +439,25 @@ export const createMenu = (isFullScreen: boolean): Menu => { } processMenu(menuTemplate, locale.getCurrent()); - menu = Menu.buildFromTemplate(menuTemplate); - - return menu; + return Menu.buildFromTemplate(menuTemplate); }; export const registerShortcuts = (): void => { // Global mute shortcut - globalShortcut.register('CmdOrCtrl+Alt+M', () => + const muteShortcut = 'CmdOrCtrl+Alt+M'; + logger.info(`Registering global mute shortcut "${muteShortcut}" ...`); + globalShortcut.register(muteShortcut, () => WindowManager.sendActionToPrimaryWindow(EVENT_TYPE.UI.SYSTEM_MENU, EVENT_TYPE.CONVERSATION.TOGGLE_MUTE), ); // Global account switching shortcut - const switchAccountShortcut = ['CmdOrCtrl', 'Super']; const accountLimit = config.maximumAccounts; - for (const shortcut of switchAccountShortcut) { - for (let accountId = 0; accountId < accountLimit; accountId++) { - globalShortcut.register(`${shortcut}+${accountId + 1}`, () => - WindowManager.sendActionToPrimaryWindow(EVENT_TYPE.ACTION.SWITCH_ACCOUNT, accountId), - ); - } + for (let accountId = 0; accountId < accountLimit; accountId++) { + logger.info(`Registering global account switching shortcut "CmdOrCtrl+${accountId + 1}" ...`); + globalShortcut.register(`CmdOrCtrl+${accountId + 1}`, () => { + logger.info(`Switching to account "${accountId}" ...`); + WindowManager.sendActionToPrimaryWindow(EVENT_TYPE.ACTION.SWITCH_ACCOUNT, accountId); + }); } }; @@ -473,6 +474,7 @@ export const toggleMenuBar = (): void => { } }; -export const unregisterShortcuts = (): void => { +export const unregisterGlobalShortcuts = (): void => { + logger.info('Unregistering all global shortcuts ...'); globalShortcut.unregisterAll(); }; diff --git a/electron/src/renderer/preload-app.ts b/electron/src/renderer/preload-app.ts index afbc6507cc1..49a6e199439 100644 --- a/electron/src/renderer/preload-app.ts +++ b/electron/src/renderer/preload-app.ts @@ -64,6 +64,10 @@ const subscribeToMainProcessEvents = () => { const webviews = document.querySelectorAll('webview'); webviews.forEach(webview => webview.reload()); }); + + ipcRenderer.on(EVENT_TYPE.ACTION.SWITCH_ACCOUNT, (event: CustomEvent, accountIndex: number) => { + window.dispatchEvent(new CustomEvent(EVENT_TYPE.ACTION.SWITCH_ACCOUNT, {detail: {accountIndex}})); + }); }; const setupIpcInterface = (): void => { @@ -106,10 +110,6 @@ const addDragRegion = (): void => { setupIpcInterface(); subscribeToMainProcessEvents(); -ipcRenderer.on(EVENT_TYPE.ACTION.SWITCH_ACCOUNT, (event: CustomEvent, accountIndex: number) => { - window.dispatchEvent(new CustomEvent(EVENT_TYPE.ACTION.SWITCH_ACCOUNT, {detail: {accountIndex}})); -}); - window.addEventListener('DOMContentLoaded', addDragRegion); window.addEventListener('focus', () => { diff --git a/electron/src/runtime/lifecycle.ts b/electron/src/runtime/lifecycle.ts index ece9c4d5717..3c4e4fdc654 100644 --- a/electron/src/runtime/lifecycle.ts +++ b/electron/src/runtime/lifecycle.ts @@ -17,7 +17,7 @@ * */ -import {app, ipcMain} from 'electron'; +import {app, globalShortcut, ipcMain} from 'electron'; import * as path from 'path'; import {EVENT_TYPE} from '../lib/eventType'; @@ -58,8 +58,13 @@ export const checkSingleInstance = () => { // Using exit instead of quit for the time being // see: https://github.com/electron/electron/issues/8862#issuecomment-294303518 export const quit = () => { - logger.info('Quitting the app ...'); + logger.info('Initiating app quit ...'); settings.persistToFile(); + + logger.info('Unregistering all global shortcuts ...'); + globalShortcut.unregisterAll(); + + logger.info('Exiting ...'); app.exit(); }; diff --git a/electron/src/window/WindowManager.ts b/electron/src/window/WindowManager.ts index 65597588ff9..1e3efaf1888 100644 --- a/electron/src/window/WindowManager.ts +++ b/electron/src/window/WindowManager.ts @@ -20,7 +20,6 @@ import {BrowserWindow, app} from 'electron'; import * as path from 'path'; -import {WebViewFocus} from '../lib/webViewFocus'; import {getLogger} from '../logging/getLogger'; const logger = getLogger(path.basename(__filename)); @@ -58,15 +57,9 @@ export const sendActionToPrimaryWindow = (channel: string, ...args: any[]): void const primaryWindow = getPrimaryWindow(); if (primaryWindow) { - logger.info(`Sending action "${channel}" to window with ID "${primaryWindow.id}":`, ...args); - const focusedWebContents = WebViewFocus.getFocusedWebContents(); - if (focusedWebContents) { - logger.info('Got focusedWebContents:', focusedWebContents.id); - focusedWebContents.send(channel, ...args); - } else { - logger.info('Got no focusedWebContents, using primaryWindow webContents:', primaryWindow.webContents.id); - primaryWindow.webContents.send(channel, ...args); - } + logger.info(`Sending action "${channel}" to window with ID "${primaryWindow.id}":`, {args}); + logger.info('Got no focusedWebContents, using primaryWindow webContents:', primaryWindow.webContents.id); + primaryWindow.webContents.send(channel, ...args); } };