Add webContents listeners to modals (#2247)

* Add webContents listeners to modals

* Fix tests
This commit is contained in:
Devin Binnie
2022-09-01 09:41:06 -04:00
committed by GitHub
parent 6d550a8a16
commit 4c968dc747
5 changed files with 47 additions and 15 deletions

View File

@@ -15,6 +15,14 @@ jest.mock('electron', () => ({
},
}));
jest.mock('common/config', () => ({
teams: [],
}));
jest.mock('main/views/webContentEvents', () => ({
addWebContentsEventListeners: jest.fn(),
}));
jest.mock('./modalView', () => ({
ModalView: jest.fn(),
}));

View File

@@ -19,8 +19,10 @@ import {
GET_MODAL_UNCLOSEABLE,
RESIZE_MODAL,
} from 'common/communication';
import Config from 'common/config';
import {getAdjustedWindowBoundaries} from 'main/utils';
import WebContentsEventManager from 'main/views/webContentEvents';
import WindowManager from 'main/windows/windowManager';
import {ModalView} from './modalView';
@@ -87,6 +89,7 @@ export class ModalManager {
if (index === 0) {
WindowManager.sendToRenderer(MODAL_OPEN);
modal.show(undefined, Boolean(withDevTools));
WebContentsEventManager.addWebContentsEventListeners(modal.view.webContents, () => Config.teams.concat());
} else {
WindowManager.sendToRenderer(MODAL_CLOSE);
modal.hide();

View File

@@ -277,7 +277,7 @@ export class ViewManager {
log.error(`Couldn't find a view with the name ${viewName}`);
return;
}
WebContentsEventManager.addWebContentsEventListeners(view, this.getServers);
WebContentsEventManager.addMattermostViewEventListeners(view, this.getServers);
}
finishLoading = (server: string) => {

View File

@@ -32,6 +32,10 @@ jest.mock('../windows/windowManager', () => ({
showMainWindow: jest.fn(),
}));
jest.mock('common/config', () => ({
spellcheck: true,
}));
jest.mock('common/utils/url', () => ({
parseURL: (url) => {
try {

View File

@@ -6,6 +6,7 @@ import log from 'electron-log';
import {TeamWithTabs} from 'types/config';
import Config from 'common/config';
import urlUtils from 'common/utils/url';
import ContextMenu from 'main/contextMenu';
@@ -221,9 +222,31 @@ export class WebContentsEventManager {
}
};
addWebContentsEventListeners = (mmview: MattermostView, getServersFunction: () => TeamWithTabs[]) => {
const contents = mmview.view.webContents;
addMattermostViewEventListeners = (mmview: MattermostView, getServersFunction: () => TeamWithTabs[]) => {
this.addWebContentsEventListeners(
mmview.view.webContents,
getServersFunction,
(contents: WebContents) => {
contents.on('page-title-updated', mmview.handleTitleUpdate);
contents.on('page-favicon-updated', mmview.handleFaviconUpdate);
contents.on('update-target-url', mmview.handleUpdateTarget);
contents.on('did-navigate', mmview.handleDidNavigate);
},
(contents: WebContents) => {
contents.removeListener('page-title-updated', mmview.handleTitleUpdate);
contents.removeListener('page-favicon-updated', mmview.handleFaviconUpdate);
contents.removeListener('update-target-url', mmview.handleUpdateTarget);
contents.removeListener('did-navigate', mmview.handleDidNavigate);
},
);
};
addWebContentsEventListeners = (
contents: WebContents,
getServersFunction: () => TeamWithTabs[],
addListeners?: (contents: WebContents) => void,
removeListeners?: (contents: WebContents) => void,
) => {
// initialize custom login tracking
this.customLogins[contents.id] = {
inProgress: false,
@@ -244,34 +267,28 @@ export class WebContentsEventManager {
const didStartNavigation = this.generateDidStartNavigation(getServersFunction);
contents.on('did-start-navigation', didStartNavigation as (e: Event, u: string) => void);
const spellcheck = mmview.options.webPreferences?.spellcheck;
const spellcheck = Config.useSpellChecker;
const newWindow = this.generateNewWindowListener(getServersFunction, spellcheck);
contents.setWindowOpenHandler(newWindow);
contents.on('page-title-updated', mmview.handleTitleUpdate);
contents.on('page-favicon-updated', mmview.handleFaviconUpdate);
contents.on('update-target-url', mmview.handleUpdateTarget);
contents.on('did-navigate', mmview.handleDidNavigate);
addListeners?.(contents);
const removeListeners = () => {
const removeWebContentsListeners = () => {
try {
contents.removeListener('will-navigate', willNavigate as (e: Event, u: string) => void);
contents.removeListener('did-start-navigation', didStartNavigation as (e: Event, u: string) => void);
contents.removeListener('page-title-updated', mmview.handleTitleUpdate);
contents.removeListener('page-favicon-updated', mmview.handleFaviconUpdate);
contents.removeListener('update-target-url', mmview.handleUpdateTarget);
contents.removeListener('did-navigate', mmview.handleDidNavigate);
removeListeners?.(contents);
} catch (e) {
log.error(`Error while trying to detach listeners, this might be ok if the process crashed: ${e}`);
}
};
this.listeners[contents.id] = removeListeners;
this.listeners[contents.id] = removeWebContentsListeners;
contents.once('render-process-gone', (event, details) => {
if (details.reason !== 'clean-exit') {
log.error('Renderer process for a webcontent is no longer available:', details.reason);
}
removeListeners();
removeWebContentsListeners();
});
};
}