Skip to content

Commit 4cd0e32

Browse files
author
Matt Bierner
authored
Merge pull request microsoft#76148 from microsoft/dev/mjbvz/workaround-75090
Potential workaround for electron 4 eating mouse events in webviews
2 parents 3d5deba + 159a492 commit 4cd0e32

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

src/vs/workbench/contrib/webview/browser/pre/main.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,9 @@
410410
}
411411
};
412412

413+
/**
414+
* @param {HTMLIFrameElement} newFrame
415+
*/
413416
function hookupOnLoadHandlers(newFrame) {
414417
clearTimeout(loadTimeout);
415418
loadTimeout = undefined;
@@ -442,6 +445,29 @@
442445

443446
// Bubble out link clicks
444447
newFrame.contentWindow.addEventListener('click', handleInnerClick);
448+
449+
// Electron 4 eats mouseup events from inside webviews
450+
// https://github.com/microsoft/vscode/issues/75090
451+
// Try to fix this by rebroadcasting mouse moves and mouseups so that we can
452+
// emulate these on the main window
453+
if (!FAKE_LOAD) {
454+
let isMouseDown = false;
455+
456+
newFrame.contentWindow.addEventListener('mousedown', () => {
457+
isMouseDown = true;
458+
});
459+
460+
const tryDispatchSyntheticMouseEvent = (e) => {
461+
if (!isMouseDown) {
462+
host.postMessage('synthetic-mouse-event', { type: e.type, screenX: e.screenX, screenY: e.screenY, clientX: e.clientX, clientY: e.clientY });
463+
}
464+
};
465+
newFrame.contentWindow.addEventListener('mouseup', e => {
466+
tryDispatchSyntheticMouseEvent(e);
467+
isMouseDown = false;
468+
});
469+
newFrame.contentWindow.addEventListener('mousemove', tryDispatchSyntheticMouseEvent);
470+
}
445471
}
446472

447473
if (!FAKE_LOAD) {

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,18 @@ export class WebviewElement extends Disposable implements Webview {
391391
this._onDidClickLink.fire(URI.parse(uri));
392392
return;
393393

394+
case 'synthetic-mouse-event':
395+
{
396+
const rawEvent = event.args[0];
397+
const bounds = this._webview.getBoundingClientRect();
398+
window.dispatchEvent(new MouseEvent(rawEvent.type, {
399+
...rawEvent,
400+
clientX: rawEvent.clientX + bounds.left,
401+
clientY: rawEvent.clientY + bounds.top,
402+
}));
403+
return;
404+
}
405+
394406
case 'did-set-content':
395407
this._webview.style.flex = '';
396408
this._webview.style.width = '100%';

0 commit comments

Comments
 (0)