From ac75ad0e458da37821f8dd59f7bd4ee6da6d4c77 Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Mon, 18 May 2020 22:35:43 -0700 Subject: [PATCH] Move webview's use of setIgnoreMenuShortcuts to main processes Part of #95955 Switches from calling `setIgnoreMenuShortcuts` on the renderer to calling it on the main thread --- src/vs/code/electron-main/app.ts | 12 +++++++- .../electron-browser/webviewElement.ts | 28 ++++++++----------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/vs/code/electron-main/app.ts b/src/vs/code/electron-main/app.ts index 25300327917e12..9857b79b455aac 100644 --- a/src/vs/code/electron-main/app.ts +++ b/src/vs/code/electron-main/app.ts @@ -3,7 +3,7 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { app, ipcMain as ipc, systemPreferences, shell, Event, contentTracing, protocol, powerMonitor, IpcMainEvent, BrowserWindow, dialog, session } from 'electron'; +import { app, ipcMain as ipc, systemPreferences, shell, Event, contentTracing, protocol, powerMonitor, IpcMainEvent, BrowserWindow, dialog, session, webContents } from 'electron'; import { IProcessEnvironment, isWindows, isMacintosh } from 'vs/base/common/platform'; import { WindowsMainService } from 'vs/platform/windows/electron-main/windowsMainService'; import { IWindowOpenable } from 'vs/platform/windows/common/windows'; @@ -296,6 +296,16 @@ export class CodeApplication extends Disposable { ipc.on('vscode:reloadWindow', (event: IpcMainEvent) => event.sender.reload()); + ipc.on('vscode:webview.setIgnoreMenuShortcuts', (_event: IpcMainEvent, webContentsId: number, enabled: boolean) => { + const contents = webContents.fromId(webContentsId); + if (!contents) { + throw new Error(`Invalid webContentsId: ${webContentsId}`); + } + if (!contents.isDestroyed()) { + contents.setIgnoreMenuShortcuts(enabled); + } + }); + // Some listeners after window opened (async () => { await this.lifecycleMainService.when(LifecycleMainPhase.AfterWindowOpen); diff --git a/src/vs/workbench/contrib/webview/electron-browser/webviewElement.ts b/src/vs/workbench/contrib/webview/electron-browser/webviewElement.ts index bdffe18a853d04..2bdcb06ebe03e4 100644 --- a/src/vs/workbench/contrib/webview/electron-browser/webviewElement.ts +++ b/src/vs/workbench/contrib/webview/electron-browser/webviewElement.ts @@ -184,26 +184,23 @@ class WebviewPortMappingProvider extends Disposable { class WebviewKeyboardHandler { - private readonly _webviews = new Set(); + private readonly _webviews = new Set(); private readonly _isUsingNativeTitleBars: boolean; constructor(configurationService: IConfigurationService) { this._isUsingNativeTitleBars = configurationService.getValue('window.titleBarStyle') === 'native'; } - public add( - webviewHandle: WebviewTagHandle, - ): IDisposable { - this._webviews.add(webviewHandle); + public add(webview: WebviewTag): IDisposable { + this._webviews.add(webview); const disposables = new DisposableStore(); + if (this.shouldToggleMenuShortcutsEnablement) { - disposables.add(webviewHandle.onFirstLoad(() => { - this.setIgnoreMenuShortcutsForWebview(webviewHandle, true); - })); + this.setIgnoreMenuShortcutsForWebview(webview, true); } - disposables.add(addDisposableListener(webviewHandle.webview, 'ipc-message', (event) => { + disposables.add(addDisposableListener(webview, 'ipc-message', (event) => { switch (event.channel) { case 'did-focus': this.setIgnoreMenuShortcuts(true); @@ -217,7 +214,7 @@ class WebviewKeyboardHandler { return toDisposable(() => { disposables.dispose(); - this._webviews.delete(webviewHandle); + this._webviews.delete(webview); }); } @@ -231,12 +228,9 @@ class WebviewKeyboardHandler { } } - private setIgnoreMenuShortcutsForWebview(webview: WebviewTagHandle, value: boolean) { + private setIgnoreMenuShortcutsForWebview(webview: WebviewTag, value: boolean) { if (this.shouldToggleMenuShortcutsEnablement) { - const contents = webview.webContents; - if (!contents?.isDestroyed()) { - contents?.setIgnoreMenuShortcuts(value); - } + ipcRenderer.send('vscode:webview.setIgnoreMenuShortcuts', webview.getWebContentsId(), value); } } } @@ -290,7 +284,9 @@ export class ElectronWebviewBasedWebview extends BaseWebview impleme tunnelService, )); - this._register(ElectronWebviewBasedWebview.getWebviewKeyboardHandler(configurationService).add(webviewAndContents)); + this._register(addDisposableListener(this.element!, 'did-start-loading', once(() => { + this._register(ElectronWebviewBasedWebview.getWebviewKeyboardHandler(configurationService).add(this.element!)); + }))); this._domReady = new Promise(resolve => { const subscription = this._register(this.on(WebviewMessageChannels.webviewReady, () => {