menuItemActionItem.ts 6.4 KB
Newer Older
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

J
Johannes Rieken 已提交
8 9
import { localize } from 'vs/nls';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
10
import { IMenu, MenuItemAction, IMenuActionOptions } from 'vs/platform/actions/common/actions';
J
Johannes Rieken 已提交
11
import { IMessageService } from 'vs/platform/message/common/message';
12
import Severity from 'vs/base/common/severity';
J
Johannes Rieken 已提交
13 14 15 16 17
import { IAction } from 'vs/base/common/actions';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { ActionItem, Separator } from 'vs/base/browser/ui/actionbar/actionbar';
import { domEvent } from 'vs/base/browser/event';
import { Emitter } from 'vs/base/common/event';
I
isidor 已提交
18 19
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { memoize } from 'vs/base/common/decorators';
20

I
isidor 已提交
21
class AltKeyEmitter extends Emitter<boolean> {
22 23 24 25

	private _subscriptions: IDisposable[] = [];
	private _isPressed: boolean;

I
isidor 已提交
26
	private constructor(contextMenuService: IContextMenuService) {
27 28 29 30 31 32
		super();

		this._subscriptions.push(domEvent(document.body, 'keydown')(e => this.isPressed = e.altKey));
		this._subscriptions.push(domEvent(document.body, 'keyup')(e => this.isPressed = false));
		this._subscriptions.push(domEvent(document.body, 'mouseleave')(e => this.isPressed = false));
		this._subscriptions.push(domEvent(document.body, 'blur')(e => this.isPressed = false));
I
isidor 已提交
33 34
		// Workaround since we do not get any events while a context menu is shown
		this._subscriptions.push(contextMenuService.onDidContextMenu(() => this.isPressed = false));
35 36 37 38 39 40 41 42 43 44 45
	}

	get isPressed(): boolean {
		return this._isPressed;
	}

	set isPressed(value: boolean) {
		this._isPressed = value;
		this.fire(this._isPressed);
	}

I
isidor 已提交
46 47 48 49 50
	@memoize
	static getInstance(contextMenuService: IContextMenuService) {
		return new AltKeyEmitter(contextMenuService);
	}

51 52 53 54
	dispose() {
		super.dispose();
		this._subscriptions = dispose(this._subscriptions);
	}
I
isidor 已提交
55
}
56

I
isidor 已提交
57
export function fillInActions(menu: IMenu, options: IMenuActionOptions, target: IAction[] | { primary: IAction[]; secondary: IAction[]; }, contextMenuService: IContextMenuService, isPrimaryGroup: (group: string) => boolean = group => group === 'navigation'): void {
58
	const groups = menu.getActions(options);
59
	if (groups.length === 0) {
60 61
		return;
	}
I
isidor 已提交
62
	const altKey = AltKeyEmitter.getInstance(contextMenuService);
63

64
	for (let tuple of groups) {
65
		let [group, actions] = tuple;
I
isidor 已提交
66
		if (altKey.isPressed) {
67 68 69
			actions = actions.map(a => !!a.alt ? a.alt : a);
		}

J
Joao Moreno 已提交
70
		if (isPrimaryGroup(group)) {
71 72 73 74 75 76 77 78 79 80

			const head = Array.isArray<IAction>(target) ? target : target.primary;

			// split contributed actions at the point where order
			// changes form lt zero to gte
			let pivot = 0;
			for (; pivot < actions.length; pivot++) {
				if ((<MenuItemAction>actions[pivot]).order >= 0) {
					break;
				}
81
			}
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
			// prepend contributed actions with order lte zero
			head.unshift(...actions.slice(0, pivot));

			// find the first separator which marks the end of the
			// navigation group - might be the whole array length
			let sep = 0;
			while (sep < head.length) {
				if (head[sep] instanceof Separator) {
					break;
				}
				sep++;
			}
			// append contributed actions with order gt zero
			head.splice(sep, 0, ...actions.slice(pivot));

97
		} else {
J
Joao Moreno 已提交
98 99 100 101
			const to = Array.isArray<IAction>(target) ? target : target.secondary;

			if (to.length > 0) {
				to.push(new Separator());
102
			}
J
Joao Moreno 已提交
103 104

			to.push(...actions);
105 106 107 108 109
		}
	}
}


I
isidor 已提交
110
export function createActionItem(action: IAction, keybindingService: IKeybindingService, messageService: IMessageService, contextMenuService: IContextMenuService): ActionItem {
111
	if (action instanceof MenuItemAction) {
I
isidor 已提交
112
		return new MenuItemActionItem(action, keybindingService, messageService, contextMenuService);
113
	}
114
	return undefined;
115 116
}

117
export class MenuItemActionItem extends ActionItem {
118

119
	private _wantsAltCommand: boolean = false;
120 121 122

	constructor(
		action: MenuItemAction,
123
		@IKeybindingService private _keybindingService: IKeybindingService,
I
isidor 已提交
124 125
		@IMessageService protected _messageService: IMessageService,
		@IContextMenuService private _contextMenuService: IContextMenuService
126
	) {
127
		super(undefined, action, { icon: !!action.class, label: !action.class });
128 129
	}

130
	protected get _commandAction(): IAction {
131
		return this._wantsAltCommand && (<MenuItemAction>this._action).alt || this._action;
132 133 134 135 136 137
	}

	onClick(event: MouseEvent): void {
		event.preventDefault();
		event.stopPropagation();

138 139
		this.actionRunner.run(this._commandAction)
			.done(undefined, err => this._messageService.show(Severity.Error, err));
140 141 142 143 144
	}

	render(container: HTMLElement): void {
		super.render(container);

145 146
		let mouseOver = false;
		let altDown = false;
147

148 149 150 151
		const updateAltState = () => {
			const wantsAltCommand = mouseOver && altDown;
			if (wantsAltCommand !== this._wantsAltCommand) {
				this._wantsAltCommand = wantsAltCommand;
152 153 154
				this._updateLabel();
				this._updateTooltip();
				this._updateClass();
155 156
			}
		};
157

I
isidor 已提交
158
		this._callOnDispose.push(AltKeyEmitter.getInstance(this._contextMenuService).event(value => {
159 160 161 162 163 164 165 166 167 168 169 170
			altDown = value;
			updateAltState();
		}));

		this._callOnDispose.push(domEvent(container, 'mouseleave')(_ => {
			mouseOver = false;
			updateAltState();
		}));

		this._callOnDispose.push(domEvent(container, 'mouseenter')(e => {
			mouseOver = true;
			updateAltState();
171 172 173 174 175
		}));
	}

	_updateLabel(): void {
		if (this.options.label) {
176
			this.$e.text(this._commandAction.label);
177 178 179 180 181
		}
	}

	_updateTooltip(): void {
		const element = this.$e.getHTMLElement();
182
		const keybinding = this._keybindingService.lookupKeybinding(this._commandAction.id);
183
		const keybindingLabel = keybinding && keybinding.getLabel();
184 185

		element.title = keybindingLabel
186 187
			? localize('titleAndKb', "{0} ({1})", this._commandAction.label, keybindingLabel)
			: this._commandAction.label;
188 189 190 191 192
	}

	_updateClass(): void {
		if (this.options.icon) {
			const element = this.$e.getHTMLElement();
193
			if (this._commandAction !== this._action) {
194 195 196
				element.classList.remove(this._action.class);
			} else if ((<MenuItemAction>this._action).alt) {
				element.classList.remove((<MenuItemAction>this._action).alt.class);
197
			}
198
			element.classList.add('icon', this._commandAction.class);
199 200 201
		}
	}
}