menuItemActionItem.ts 5.6 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';
18

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
const _altKey = new class extends Emitter<boolean> {

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

	constructor() {
		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));
	}

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

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

	dispose() {
		super.dispose();
		this._subscriptions = dispose(this._subscriptions);
	}
};
47

48 49
export function fillInActions(menu: IMenu, options: IMenuActionOptions, target: IAction[] | { primary: IAction[]; secondary: IAction[]; }, isPrimaryGroup: (group: string) => boolean = group => group === 'navigation'): void {
	const groups = menu.getActions(options);
50
	if (groups.length === 0) {
51 52 53
		return;
	}

54
	for (let tuple of groups) {
55
		let [group, actions] = tuple;
56 57 58 59
		if (_altKey.isPressed) {
			actions = actions.map(a => !!a.alt ? a.alt : a);
		}

J
Joao Moreno 已提交
60
		if (isPrimaryGroup(group)) {
61 62 63 64 65 66 67 68 69 70

			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;
				}
71
			}
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
			// 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));

87
		} else {
J
Joao Moreno 已提交
88 89 90 91
			const to = Array.isArray<IAction>(target) ? target : target.secondary;

			if (to.length > 0) {
				to.push(new Separator());
92
			}
J
Joao Moreno 已提交
93 94

			to.push(...actions);
95 96 97 98 99
		}
	}
}


100
export function createActionItem(action: IAction, keybindingService: IKeybindingService, messageService: IMessageService): ActionItem {
101
	if (action instanceof MenuItemAction) {
102
		return new MenuItemActionItem(action, keybindingService, messageService);
103
	}
104
	return undefined;
105 106
}

107
export class MenuItemActionItem extends ActionItem {
108

109
	private _wantsAltCommand: boolean = false;
110 111 112

	constructor(
		action: MenuItemAction,
113
		@IKeybindingService private _keybindingService: IKeybindingService,
114
		@IMessageService protected _messageService: IMessageService
115
	) {
116
		super(undefined, action, { icon: !!action.class, label: !action.class });
117 118
	}

119
	protected get _commandAction(): IAction {
120
		return this._wantsAltCommand && (<MenuItemAction>this._action).alt || this._action;
121 122 123 124 125 126
	}

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

127 128
		this.actionRunner.run(this._commandAction)
			.done(undefined, err => this._messageService.show(Severity.Error, err));
129 130 131 132 133
	}

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

134 135
		let mouseOver = false;
		let altDown = false;
136

137 138 139 140
		const updateAltState = () => {
			const wantsAltCommand = mouseOver && altDown;
			if (wantsAltCommand !== this._wantsAltCommand) {
				this._wantsAltCommand = wantsAltCommand;
141 142 143
				this._updateLabel();
				this._updateTooltip();
				this._updateClass();
144 145
			}
		};
146

147 148 149 150 151 152 153 154 155 156 157 158 159
		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();
160 161 162 163 164
		}));
	}

	_updateLabel(): void {
		if (this.options.label) {
165
			this.$e.text(this._commandAction.label);
166 167 168 169 170
		}
	}

	_updateTooltip(): void {
		const element = this.$e.getHTMLElement();
171
		const keybinding = this._keybindingService.lookupKeybinding(this._commandAction.id);
172
		const keybindingLabel = keybinding && keybinding.getLabel();
173 174

		element.title = keybindingLabel
175 176
			? localize('titleAndKb', "{0} ({1})", this._commandAction.label, keybindingLabel)
			: this._commandAction.label;
177 178 179 180 181
	}

	_updateClass(): void {
		if (this.options.icon) {
			const element = this.$e.getHTMLElement();
182
			if (this._commandAction !== this._action) {
183 184 185
				element.classList.remove(this._action.class);
			} else if ((<MenuItemAction>this._action).alt) {
				element.classList.remove((<MenuItemAction>this._action).alt.class);
186
			}
187
			element.classList.add('icon', this._commandAction.class);
188 189 190
		}
	}
}