Skip to content

Commit

Permalink
Move webview's use of setIgnoreMenuShortcuts to main processes
Browse files Browse the repository at this point in the history
Part of #95955

Switches from calling `setIgnoreMenuShortcuts` on the renderer to calling it on the main thread
  • Loading branch information
mjbvz committed May 27, 2020
1 parent 040034f commit ac75ad0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
12 changes: 11 additions & 1 deletion src/vs/code/electron-main/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down
28 changes: 12 additions & 16 deletions src/vs/workbench/contrib/webview/electron-browser/webviewElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,26 +184,23 @@ class WebviewPortMappingProvider extends Disposable {

class WebviewKeyboardHandler {

private readonly _webviews = new Set<WebviewTagHandle>();
private readonly _webviews = new Set<WebviewTag>();
private readonly _isUsingNativeTitleBars: boolean;

constructor(configurationService: IConfigurationService) {
this._isUsingNativeTitleBars = configurationService.getValue<string>('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);
Expand All @@ -217,7 +214,7 @@ class WebviewKeyboardHandler {

return toDisposable(() => {
disposables.dispose();
this._webviews.delete(webviewHandle);
this._webviews.delete(webview);
});
}

Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -290,7 +284,9 @@ export class ElectronWebviewBasedWebview extends BaseWebview<WebviewTag> 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, () => {
Expand Down

0 comments on commit ac75ad0

Please sign in to comment.