Skip to content

Commit d3cf886

Browse files
committed
Move webview's use of setIgnoreMenuShortcuts to main processes
Part of microsoft#95955 Switches from calling `setIgnoreMenuShortcuts` on the renderer to calling it on the main thread
1 parent 3bd9d75 commit d3cf886

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

src/vs/code/electron-main/app.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { app, ipcMain as ipc, systemPreferences, shell, Event, contentTracing, protocol, powerMonitor, IpcMainEvent, BrowserWindow, dialog, session } from 'electron';
6+
import { app, ipcMain as ipc, systemPreferences, shell, Event, contentTracing, protocol, powerMonitor, IpcMainEvent, BrowserWindow, dialog, session, webContents } from 'electron';
77
import { IProcessEnvironment, isWindows, isMacintosh } from 'vs/base/common/platform';
88
import { WindowsMainService } from 'vs/platform/windows/electron-main/windowsMainService';
99
import { IWindowOpenable } from 'vs/platform/windows/common/windows';
@@ -296,6 +296,16 @@ export class CodeApplication extends Disposable {
296296

297297
ipc.on('vscode:reloadWindow', (event: IpcMainEvent) => event.sender.reload());
298298

299+
ipc.on('vscode:webview.setIgnoreMenuShortcuts', (_event: IpcMainEvent, webContentsId: number, enabled: boolean) => {
300+
const contents = webContents.fromId(webContentsId);
301+
if (!contents) {
302+
throw new Error(`Invalid webContentsId: ${webContentsId}`);
303+
}
304+
if (!contents.isDestroyed()) {
305+
contents.setIgnoreMenuShortcuts(enabled);
306+
}
307+
});
308+
299309
// Some listeners after window opened
300310
(async () => {
301311
await this.lifecycleMainService.when(LifecycleMainPhase.AfterWindowOpen);

src/vs/workbench/contrib/webview/electron-browser/webviewElement.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -184,26 +184,23 @@ class WebviewPortMappingProvider extends Disposable {
184184

185185
class WebviewKeyboardHandler {
186186

187-
private readonly _webviews = new Set<WebviewTagHandle>();
187+
private readonly _webviews = new Set<WebviewTag>();
188188
private readonly _isUsingNativeTitleBars: boolean;
189189

190190
constructor(configurationService: IConfigurationService) {
191191
this._isUsingNativeTitleBars = configurationService.getValue<string>('window.titleBarStyle') === 'native';
192192
}
193193

194-
public add(
195-
webviewHandle: WebviewTagHandle,
196-
): IDisposable {
197-
this._webviews.add(webviewHandle);
194+
public add(webview: WebviewTag): IDisposable {
195+
this._webviews.add(webview);
198196

199197
const disposables = new DisposableStore();
198+
200199
if (this.shouldToggleMenuShortcutsEnablement) {
201-
disposables.add(webviewHandle.onFirstLoad(() => {
202-
this.setIgnoreMenuShortcutsForWebview(webviewHandle, true);
203-
}));
200+
this.setIgnoreMenuShortcutsForWebview(webview, true);
204201
}
205202

206-
disposables.add(addDisposableListener(webviewHandle.webview, 'ipc-message', (event) => {
203+
disposables.add(addDisposableListener(webview, 'ipc-message', (event) => {
207204
switch (event.channel) {
208205
case 'did-focus':
209206
this.setIgnoreMenuShortcuts(true);
@@ -217,7 +214,7 @@ class WebviewKeyboardHandler {
217214

218215
return toDisposable(() => {
219216
disposables.dispose();
220-
this._webviews.delete(webviewHandle);
217+
this._webviews.delete(webview);
221218
});
222219
}
223220

@@ -231,12 +228,9 @@ class WebviewKeyboardHandler {
231228
}
232229
}
233230

234-
private setIgnoreMenuShortcutsForWebview(webview: WebviewTagHandle, value: boolean) {
231+
private setIgnoreMenuShortcutsForWebview(webview: WebviewTag, value: boolean) {
235232
if (this.shouldToggleMenuShortcutsEnablement) {
236-
const contents = webview.webContents;
237-
if (!contents?.isDestroyed()) {
238-
contents?.setIgnoreMenuShortcuts(value);
239-
}
233+
ipcRenderer.send('vscode:webview.setIgnoreMenuShortcuts', webview.getWebContentsId(), value);
240234
}
241235
}
242236
}
@@ -290,7 +284,9 @@ export class ElectronWebviewBasedWebview extends BaseWebview<WebviewTag> impleme
290284
tunnelService,
291285
));
292286

293-
this._register(ElectronWebviewBasedWebview.getWebviewKeyboardHandler(configurationService).add(webviewAndContents));
287+
this._register(addDisposableListener(this.element!, 'did-start-loading', once(() => {
288+
this._register(ElectronWebviewBasedWebview.getWebviewKeyboardHandler(configurationService).add(this.element!));
289+
})));
294290

295291
this._domReady = new Promise(resolve => {
296292
const subscription = this._register(this.on(WebviewMessageChannels.webviewReady, () => {

0 commit comments

Comments
 (0)