contextmenuService.ts 7.6 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import { IAction, IActionRunner, ActionRunner, WorkbenchActionExecutedEvent, WorkbenchActionExecutedClassification } from 'vs/base/common/actions';
J
Johannes Rieken 已提交
7
import { Separator } from 'vs/base/browser/ui/actionbar/actionbar';
8
import * as dom from 'vs/base/browser/dom';
9
import { IContextMenuService, IContextViewService } from 'vs/platform/contextview/browser/contextView';
J
Johannes Rieken 已提交
10 11
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
12
import { getZoomFactor } from 'vs/base/browser/browser';
B
Benjamin Pasero 已提交
13
import { unmnemonicLabel } from 'vs/base/common/labels';
M
Matt Bierner 已提交
14
import { Event, Emitter } from 'vs/base/common/event';
15
import { INotificationService } from 'vs/platform/notification/common/notification';
16
import { IContextMenuDelegate, ContextSubMenu, IContextMenuEvent } from 'vs/base/browser/contextmenu';
17
import { once } from 'vs/base/common/functional';
B
Benjamin Pasero 已提交
18
import { Disposable } from 'vs/base/common/lifecycle';
19
import { IContextMenuItem } from 'vs/base/parts/contextmenu/common/contextmenu';
20
import { popup } from 'vs/base/parts/contextmenu/electron-sandbox/contextmenu';
21 22 23 24 25 26 27
import { getTitleBarStyle } from 'vs/platform/windows/common/windows';
import { isMacintosh } from 'vs/base/common/platform';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { ContextMenuService as HTMLContextMenuService } from 'vs/platform/contextview/browser/contextMenuService';
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
S
SteVen Batten 已提交
28
import { stripCodicons } from 'vs/base/common/codicons';
B
Benjamin Pasero 已提交
29 30

export class ContextMenuService extends Disposable implements IContextMenuService {
E
Erich Gamma 已提交
31

32
	declare readonly _serviceBrand: undefined;
33

34 35 36 37 38 39 40 41 42 43 44
	get onDidContextMenu(): Event<void> { return this.impl.onDidContextMenu; }

	private impl: IContextMenuService;

	constructor(
		@INotificationService notificationService: INotificationService,
		@ITelemetryService telemetryService: ITelemetryService,
		@IKeybindingService keybindingService: IKeybindingService,
		@IConfigurationService configurationService: IConfigurationService,
		@IEnvironmentService environmentService: IEnvironmentService,
		@IContextViewService contextViewService: IContextViewService,
B
Benjamin Pasero 已提交
45
		@IThemeService themeService: IThemeService
46 47 48 49 50
	) {
		super();

		// Custom context menu: Linux/Windows if custom title is enabled
		if (!isMacintosh && getTitleBarStyle(configurationService, environmentService) === 'custom') {
B
Benjamin Pasero 已提交
51
			this.impl = new HTMLContextMenuService(telemetryService, notificationService, contextViewService, keybindingService, themeService);
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
		}

		// Native context menu: otherwise
		else {
			this.impl = new NativeContextMenuService(notificationService, telemetryService, keybindingService);
		}
	}

	showContextMenu(delegate: IContextMenuDelegate): void {
		this.impl.showContextMenu(delegate);
	}
}

class NativeContextMenuService extends Disposable implements IContextMenuService {

67
	declare readonly _serviceBrand: undefined;
68

B
Benjamin Pasero 已提交
69
	private _onDidContextMenu = this._register(new Emitter<void>());
70
	readonly onDidContextMenu: Event<void> = this._onDidContextMenu.event;
71

72
	constructor(
73 74 75
		@INotificationService private readonly notificationService: INotificationService,
		@ITelemetryService private readonly telemetryService: ITelemetryService,
		@IKeybindingService private readonly keybindingService: IKeybindingService
76
	) {
B
Benjamin Pasero 已提交
77
		super();
E
Erich Gamma 已提交
78 79
	}

B
Benjamin Pasero 已提交
80
	showContextMenu(delegate: IContextMenuDelegate): void {
81 82 83 84
		const actions = delegate.getActions();
		if (actions.length) {
			const onHide = once(() => {
				if (delegate.onHide) {
M
Matt Bierner 已提交
85
					delegate.onHide(false);
86 87
				}

88 89
				this._onDidContextMenu.fire();
			});
90

91 92 93
			const menu = this.createMenu(delegate, actions, onHide);
			const anchor = delegate.getAnchor();

B
Benjamin Pasero 已提交
94 95 96
			let x: number;
			let y: number;

97
			const zoom = getZoomFactor();
98
			if (dom.isHTMLElement(anchor)) {
99
				const elementPosition = dom.getDomNodePagePosition(anchor);
100 101 102

				x = elementPosition.left;
				y = elementPosition.top + elementPosition.height;
S
SteVen Batten 已提交
103 104 105 106 107 108 109

				// Shift macOS menus by a few pixels below elements
				// to account for extra padding on top of native menu
				// https://github.com/microsoft/vscode/issues/84231
				if (isMacintosh) {
					y += 4 / zoom;
				}
110
			} else {
B
Benjamin Pasero 已提交
111
				const pos: { x: number; y: number; } = anchor;
112 113
				x = pos.x + 1; /* prevent first item from being selected automatically under mouse */
				y = pos.y;
114
			}
115 116 117 118 119 120 121

			x *= zoom;
			y *= zoom;

			popup(menu, {
				x: Math.floor(x),
				y: Math.floor(y),
R
Rob Lourens 已提交
122
				positioningItem: delegate.autoSelectFirstItem ? 0 : undefined,
123
			}, () => onHide());
124
		}
125
	}
E
Erich Gamma 已提交
126

127
	private createMenu(delegate: IContextMenuDelegate, entries: ReadonlyArray<IAction | ContextSubMenu>, onHide: () => void): IContextMenuItem[] {
128
		const actionRunner = delegate.actionRunner || new ActionRunner();
I
isidor 已提交
129

130 131
		return entries.map(entry => this.createMenuItem(delegate, entry, actionRunner, onHide));
	}
I
isidor 已提交
132

133 134 135 136
	private createMenuItem(delegate: IContextMenuDelegate, entry: IAction | ContextSubMenu, actionRunner: IActionRunner, onHide: () => void): IContextMenuItem {

		// Separator
		if (entry instanceof Separator) {
137
			return { type: 'separator' };
138 139 140 141 142
		}

		// Submenu
		if (entry instanceof ContextSubMenu) {
			return {
S
SteVen Batten 已提交
143
				label: unmnemonicLabel(stripCodicons(entry.label)).trim(),
144
				submenu: this.createMenu(delegate, entry.entries, onHide)
145
			};
146 147 148 149
		}

		// Normal Menu Item
		else {
B
Benjamin Pasero 已提交
150 151 152 153 154 155 156 157 158
			let type: 'radio' | 'checkbox' | undefined = undefined;
			if (!!entry.checked) {
				if (typeof delegate.getCheckedActionsRepresentation === 'function') {
					type = delegate.getCheckedActionsRepresentation(entry);
				} else {
					type = 'checkbox';
				}
			}

159
			const item: IContextMenuItem = {
S
SteVen Batten 已提交
160
				label: unmnemonicLabel(stripCodicons(entry.label)).trim(),
B
Benjamin Pasero 已提交
161 162
				checked: !!entry.checked,
				type,
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
				enabled: !!entry.enabled,
				click: event => {

					// To preserve pre-electron-2.x behaviour, we first trigger
					// the onHide callback and then the action.
					// Fixes https://github.com/Microsoft/vscode/issues/45601
					onHide();

					// Run action which will close the menu
					this.runAction(actionRunner, entry, delegate, event);
				}
			};

			const keybinding = !!delegate.getKeyBinding ? delegate.getKeyBinding(entry) : this.keybindingService.lookupKeybinding(entry.id);
			if (keybinding) {
				const electronAccelerator = keybinding.getElectronAccelerator();
				if (electronAccelerator) {
					item.accelerator = electronAccelerator;
				} else {
					const label = keybinding.getLabel();
					if (label) {
						item.label = `${item.label} [${label}]`;
185 186
					}
				}
I
isidor 已提交
187 188
			}

189 190
			return item;
		}
I
isidor 已提交
191 192
	}

193
	private async runAction(actionRunner: IActionRunner, actionToRun: IAction, delegate: IContextMenuDelegate, event: IContextMenuEvent): Promise<void> {
194
		this.telemetryService.publicLog2<WorkbenchActionExecutedEvent, WorkbenchActionExecutedClassification>('workbenchActionExecuted', { id: actionToRun.id, from: 'contextMenu' });
E
Erich Gamma 已提交
195

S
SteVen Batten 已提交
196
		const context = delegate.getActionsContext ? delegate.getActionsContext(event) : undefined;
E
Erich Gamma 已提交
197

198 199 200 201 202 203 204 205
		const runnable = actionRunner.run(actionToRun, context);
		if (runnable) {
			try {
				await runnable;
			} catch (error) {
				this.notificationService.error(error);
			}
		}
E
Erich Gamma 已提交
206
	}
207
}
208

B
Benjamin Pasero 已提交
209
registerSingleton(IContextMenuService, ContextMenuService, true);