[MM-46634] Force window to be focused after navigation on notification click (#3051)
This commit is contained in:
parent
fd8212a756
commit
923df7c187
|
@ -4,8 +4,8 @@
|
|||
'use strict';
|
||||
import notMockedCP from 'child_process';
|
||||
|
||||
import type {BrowserWindow, WebContents} from 'electron';
|
||||
import {Notification as NotMockedNotification, shell, app} from 'electron';
|
||||
import type {BrowserWindow, IpcMain, IpcMainEvent, WebContents} from 'electron';
|
||||
import {Notification as NotMockedNotification, shell, app, ipcMain as NotMockedIpcMain} from 'electron';
|
||||
import {getDoNotDisturb as notMockedGetDarwinDoNotDisturb} from 'macos-notification-state';
|
||||
import {getFocusAssist as notMockedGetFocusAssist} from 'windows-focus-assist';
|
||||
|
||||
|
@ -28,6 +28,7 @@ const Config = jest.mocked(notMockedConfig);
|
|||
const MainWindow = jest.mocked(notMockedMainWindow);
|
||||
const localizeMessage = jest.mocked(notMockedLocalizeMessage);
|
||||
const cp = jest.mocked(notMockedCP);
|
||||
const ipcMain = jest.mocked(NotMockedIpcMain);
|
||||
|
||||
const mentions: Array<{body: string; value: any}> = [];
|
||||
|
||||
|
@ -69,6 +70,10 @@ jest.mock('electron', () => {
|
|||
bounce: jest.fn(),
|
||||
},
|
||||
},
|
||||
ipcMain: {
|
||||
on: jest.fn(),
|
||||
off: jest.fn(),
|
||||
},
|
||||
Notification: NotificationMock,
|
||||
shell: {
|
||||
showItemInFolder: jest.fn(),
|
||||
|
@ -273,7 +278,12 @@ describe('main/notifications', () => {
|
|||
});
|
||||
});
|
||||
|
||||
it('should switch view when clicking on notification', async () => {
|
||||
it('should switch view when clicking on notification, but after the navigation has happened', async () => {
|
||||
let listener: (event: IpcMainEvent, ...args: any[]) => void;
|
||||
ipcMain.on.mockImplementation((channel: string, cb: (event: IpcMainEvent, ...args: any[]) => void): IpcMain => {
|
||||
listener = cb;
|
||||
return ipcMain;
|
||||
});
|
||||
await NotificationManager.displayMention(
|
||||
'click_test',
|
||||
'mention_click_body',
|
||||
|
@ -286,6 +296,12 @@ describe('main/notifications', () => {
|
|||
);
|
||||
const mention = mentions.find((m) => m.body === 'mention_click_body');
|
||||
mention?.value.click();
|
||||
expect(MainWindow.show).not.toHaveBeenCalled();
|
||||
expect(ViewManager.showById).not.toHaveBeenCalledWith('server_id');
|
||||
|
||||
// @ts-expect-error "Set by the click handler"
|
||||
listener?.({} as unknown as IpcMainEvent);
|
||||
|
||||
expect(MainWindow.show).toHaveBeenCalled();
|
||||
expect(ViewManager.showById).toHaveBeenCalledWith('server_id');
|
||||
});
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
import {app, shell, Notification} from 'electron';
|
||||
import {app, shell, Notification, ipcMain} from 'electron';
|
||||
import isDev from 'electron-is-dev';
|
||||
import {getDoNotDisturb as getDarwinDoNotDisturb} from 'macos-notification-state';
|
||||
|
||||
import {PLAY_SOUND, NOTIFICATION_CLICKED} from 'common/communication';
|
||||
import {PLAY_SOUND, NOTIFICATION_CLICKED, BROWSER_HISTORY_PUSH} from 'common/communication';
|
||||
import Config from 'common/config';
|
||||
import {Logger} from 'common/log';
|
||||
|
||||
|
@ -70,11 +70,16 @@ class NotificationManager {
|
|||
log.debug('notification click', serverName, mention.uId);
|
||||
|
||||
this.allActiveNotifications.delete(mention.uId);
|
||||
MainWindow.show();
|
||||
if (serverName) {
|
||||
|
||||
// Show the window after navigation has finished to avoid the focus handler
|
||||
// being called before the current channel has updated
|
||||
const focus = () => {
|
||||
MainWindow.show();
|
||||
ViewManager.showById(view.id);
|
||||
webcontents.send(NOTIFICATION_CLICKED, channelId, teamId, url);
|
||||
}
|
||||
ipcMain.off(BROWSER_HISTORY_PUSH, focus);
|
||||
};
|
||||
ipcMain.on(BROWSER_HISTORY_PUSH, focus);
|
||||
webcontents.send(NOTIFICATION_CLICKED, channelId, teamId, url);
|
||||
});
|
||||
|
||||
mention.on('close', () => {
|
||||
|
|
Loading…
Reference in a new issue