Skip to content

Commit

Permalink
fix: Focus account after switching (WEBAPP-5736) (#3205)
Browse files Browse the repository at this point in the history
  • Loading branch information
ffflorian authored Nov 4, 2019
1 parent 315fc98 commit 105b97b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 31 deletions.
2 changes: 2 additions & 0 deletions electron/renderer/src/components/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class App extends React.Component {
if (accountId) {
this.props.switchAccount(accountId);
}

event.target.focus();
}

initiateSSO(event) {
Expand Down
4 changes: 2 additions & 2 deletions electron/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -269,7 +269,7 @@ const showMainWindow = async (mainWindowState: WindowStateKeeper.State) => {
main.hide();
}
}
systemMenu.unregisterShortcuts();
systemMenu.unregisterGlobalShortcuts();
});

main.webContents.on('crashed', event => {
Expand Down
28 changes: 15 additions & 13 deletions electron/src/menu/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
Expand Down Expand Up @@ -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);
});
}
};

Expand All @@ -473,6 +474,7 @@ export const toggleMenuBar = (): void => {
}
};

export const unregisterShortcuts = (): void => {
export const unregisterGlobalShortcuts = (): void => {
logger.info('Unregistering all global shortcuts ...');
globalShortcut.unregisterAll();
};
8 changes: 4 additions & 4 deletions electron/src/renderer/preload-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ const subscribeToMainProcessEvents = () => {
const webviews = document.querySelectorAll<WebviewTag>('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 => {
Expand Down Expand Up @@ -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', () => {
Expand Down
9 changes: 7 additions & 2 deletions electron/src/runtime/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
};

Expand Down
13 changes: 3 additions & 10 deletions electron/src/window/WindowManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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);
}
};

Expand Down

0 comments on commit 105b97b

Please sign in to comment.