Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make system context menu code more understandable #155747

Merged
merged 3 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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