From 62469028a6dce9db5b7c4639be7f447f4ab0e35c Mon Sep 17 00:00:00 2001 From: SteVen Batten <6561887+sbatten@users.noreply.github.com> Date: Wed, 20 Jul 2022 21:20:23 -0700 Subject: [PATCH] make system context menu code more understandable (#155747) * removing unnecessary enablements refs #155276 * add some comments * address feedback, bring back enablements but comment them --- src/vs/platform/windows/electron-main/window.ts | 9 +++++++++ .../parts/titlebar/titlebarPart.ts | 17 ++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/vs/platform/windows/electron-main/window.ts b/src/vs/platform/windows/electron-main/window.ts index 85fc9e0c27e12..6c64ff8c565e1 100644 --- a/src/vs/platform/windows/electron-main/window.ts +++ b/src/vs/platform/windows/electron-main/window.ts @@ -292,12 +292,21 @@ export class CodeWindow extends Disposable implements ICodeWindow { // Windows Custom System Context Menu // See https://github.com/electron/electron/issues/24893 + // + // The purpose of this is to allow for the context menu in the Windows Title Bar + // + // Currently, all mouse events in the title bar are captured by the OS + // thus we need to capture them here with a window hook specific to Windows + // and then forward them to the correct window. if (isWindows && useCustomTitleStyle) { + // https://docs.microsoft.com/en-us/windows/win32/menurc/wm-initmenu const WM_INITMENU = 0x0116; + // This sets up a listener for the window hook. This is a Windows-only API provided by electron. this._win.hookWindowMessage(WM_INITMENU, () => { const [x, y] = this._win.getPosition(); const cursorPos = screen.getCursorScreenPoint(); + // This is necessary to make sure the native system context menu does not show up. this._win.setEnabled(false); this._win.setEnabled(true); diff --git a/src/vs/workbench/electron-sandbox/parts/titlebar/titlebarPart.ts b/src/vs/workbench/electron-sandbox/parts/titlebar/titlebarPart.ts index 4720f73a32ac8..f6f5b60e313e3 100644 --- a/src/vs/workbench/electron-sandbox/parts/titlebar/titlebarPart.ts +++ b/src/vs/workbench/electron-sandbox/parts/titlebar/titlebarPart.ts @@ -207,7 +207,22 @@ export class TitlebarPart extends BrowserTitleBarPart { } const zoomFactor = getZoomFactor(); - this.onContextMenu(new MouseEvent('mouseup', { clientX: x / zoomFactor, clientY: y / zoomFactor }), MenuId.TitleBarContext); + const boundingRect = this.rootContainer.getBoundingClientRect(); + const eventPosition = { x, y }; + const relativeCoordinates = { x, y }; + // When comparing the coordinates with the title bar, account for zoom level if not using counter zoom. + if (!this.useCounterZoom) { + relativeCoordinates.x /= zoomFactor; + relativeCoordinates.y /= zoomFactor; + } + + // Don't trigger the menu if the click is not over the title bar + if (relativeCoordinates.x < boundingRect.left || relativeCoordinates.x > boundingRect.right || + relativeCoordinates.y < boundingRect.top || relativeCoordinates.y > boundingRect.bottom) { + return; + } + + this.onContextMenu(new MouseEvent('mouseup', { clientX: eventPosition.x / zoomFactor, clientY: eventPosition.y / zoomFactor }), MenuId.TitleBarContext); })); }