Skip to content

Commit

Permalink
make system context menu code more understandable (#155747)
Browse files Browse the repository at this point in the history
* removing unnecessary enablements
refs #155276

* add some comments

* address feedback, bring back enablements
but comment them
  • Loading branch information
sbatten committed Jul 21, 2022
1 parent 9776b9d commit 6246902
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/vs/platform/windows/electron-main/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
17 changes: 16 additions & 1 deletion src/vs/workbench/electron-sandbox/parts/titlebar/titlebarPart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}));
}

Expand Down

0 comments on commit 6246902

Please sign in to comment.