menuItemActionItem.ts 5.3 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 10 11
import { localize } from 'vs/nls';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { IMenu, MenuItemAction } from 'vs/platform/actions/common/actions';
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';
18

19

J
Joao Moreno 已提交
20
export function fillInActions(menu: IMenu, context: any, target: IAction[] | { primary: IAction[]; secondary: IAction[]; }, isPrimaryGroup: (group: string) => boolean = group => group === 'navigation'): void {
21
	const groups = menu.getActions(context);
22
	if (groups.length === 0) {
23 24 25
		return;
	}

26
	for (let tuple of groups) {
27
		let [group, actions] = tuple;
J
Joao Moreno 已提交
28
		if (isPrimaryGroup(group)) {
29 30 31 32 33 34 35 36 37 38

			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;
				}
39
			}
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
			// 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));

55 56 57 58 59 60 61 62 63 64 65
		} else {
			if (Array.isArray<IAction>(target)) {
				target.push(new Separator(), ...actions);
			} else {
				target.secondary.push(new Separator(), ...actions);
			}
		}
	}
}


66
export function createActionItem(action: IAction, keybindingService: IKeybindingService, messageService: IMessageService): ActionItem {
67
	if (action instanceof MenuItemAction) {
68
		return new MenuItemActionItem(action, keybindingService, messageService);
69
	}
70
	return undefined;
71 72
}

73 74 75 76 77 78

const _altKey = new class extends Emitter<boolean> {

	private _subscriptions: IDisposable[] = [];

	constructor() {
79 80 81 82 83
		super();

		this._subscriptions.push(domEvent(document.body, 'keydown')(e => this.fire(e.altKey)));
		this._subscriptions.push(domEvent(document.body, 'keyup')(e => this.fire(false)));
		this._subscriptions.push(domEvent(document.body, 'mouseleave')(e => this.fire(false)));
84
		this._subscriptions.push(domEvent(document.body, 'blur')(e => this.fire(false)));
85 86
	}

87 88 89
	dispose() {
		super.dispose();
		this._subscriptions = dispose(this._subscriptions);
90 91 92
	}
};

93 94
class MenuItemActionItem extends ActionItem {

95
	private _wantsAltCommand: boolean = false;
96 97 98

	constructor(
		action: MenuItemAction,
99 100
		@IKeybindingService private _keybindingService: IKeybindingService,
		@IMessageService private _messageService: IMessageService
101
	) {
102
		super(undefined, action, { icon: !!action.class, label: !action.class });
103 104
	}

105
	private get _command() {
106
		return this._wantsAltCommand && (<MenuItemAction>this._action).alt || this._action;
107 108 109 110 111 112
	}

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

113
		this._command.run().done(undefined, err => this._messageService.show(Severity.Error, err));
114 115 116 117 118
	}

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

119 120
		let mouseOver = false;
		let altDown = false;
121

122 123 124 125
		const updateAltState = () => {
			const wantsAltCommand = mouseOver && altDown;
			if (wantsAltCommand !== this._wantsAltCommand) {
				this._wantsAltCommand = wantsAltCommand;
126 127 128
				this._updateLabel();
				this._updateTooltip();
				this._updateClass();
129 130
			}
		};
131

132 133 134 135 136 137 138 139 140 141 142 143 144
		this._callOnDispose.push(_altKey.event(value => {
			altDown = value;
			updateAltState();
		}));

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

		this._callOnDispose.push(domEvent(container, 'mouseenter')(e => {
			mouseOver = true;
			updateAltState();
145 146 147 148 149
		}));
	}

	_updateLabel(): void {
		if (this.options.label) {
150
			this.$e.text(this._command.label);
151 152 153 154 155
		}
	}

	_updateTooltip(): void {
		const element = this.$e.getHTMLElement();
B
Benjamin Pasero 已提交
156
		const [keybinding] = this._keybindingService.lookupKeybindings(this._command.id);
157 158 159
		const keybindingLabel = keybinding && this._keybindingService.getLabelFor(keybinding);

		element.title = keybindingLabel
160 161
			? localize('titleAndKb', "{0} ({1})", this._command.label, keybindingLabel)
			: this._command.label;
162 163 164 165 166
	}

	_updateClass(): void {
		if (this.options.icon) {
			const element = this.$e.getHTMLElement();
167 168 169 170
			if (this._command !== this._action) {
				element.classList.remove(this._action.class);
			} else if ((<MenuItemAction>this._action).alt) {
				element.classList.remove((<MenuItemAction>this._action).alt.class);
171
			}
172
			element.classList.add('icon', this._command.class);
173 174 175
		}
	}
}