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

'use strict';

import {localize} from 'vs/nls';
import {IKeybindingService} from 'vs/platform/keybinding/common/keybindingService';
10
import {IMenu, MenuItemAction} from 'vs/platform/actions/common/actions';
11
import {IAction} from 'vs/base/common/actions';
12
import {IDisposable, dispose} from 'vs/base/common/lifecycle';
13
import {ActionItem, Separator} from 'vs/base/browser/ui/actionbar/actionbar';
14
import {domEvent} from 'vs/base/browser/event';
15
import {Emitter} from 'vs/base/common/event';
16

17 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

export function fillInActions(menu: IMenu, target: IAction[] | { primary: IAction[]; secondary: IAction[];}): void {
	const actions = menu.getActions();
	if (actions.length === 0) {
		return;
	}

	for (let tuple of actions) {
		let [group, actions] = tuple;
		if (group === 'navigation') {
			if (Array.isArray<IAction>(target)) {
				target.unshift(...actions);
			} else {
				target.primary.unshift(...actions);
			}
		} else {
			if (Array.isArray<IAction>(target)) {
				target.push(new Separator(), ...actions);
			} else {
				target.secondary.push(new Separator(), ...actions);
			}
		}
	}
}


43 44 45 46 47 48
export function createActionItem(action: IAction, keybindingService: IKeybindingService): ActionItem {
	if (action instanceof MenuItemAction) {
		return new MenuItemActionItem(action, keybindingService);
	}
}

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

const _altKey = new class extends Emitter<boolean> {

	private _subscriptions: IDisposable[] = [];

	constructor() {
		super({
			onFirstListenerAdd: () => {
				domEvent(document.body, 'keydown')(this._key, this, this._subscriptions);
				domEvent(document.body, 'keyup')(this._key, this, this._subscriptions);
			},
			onLastListenerRemove: () => {
				this._subscriptions = dispose(this._subscriptions);
			}
		});
	}

	private _key(e: KeyboardEvent) {
		this.fire(e.type === 'keydown' && e.altKey);
	}
};

71 72 73 74 75 76 77 78 79 80 81
class MenuItemActionItem extends ActionItem {

	private _altKeyDown: boolean = false;

	constructor(
		action: MenuItemAction,
		@IKeybindingService private _keybindingService: IKeybindingService
	) {
		super(undefined, action, { icon: !!action.command.iconClass, label: !action.command.iconClass });
	}

82
	private get _command() {
83 84 85 86 87 88 89 90 91 92 93 94 95 96
		const {command, altCommand} = <MenuItemAction>this._action;
		return this._altKeyDown && altCommand || command;
	}

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

		(<MenuItemAction>this._action).run(this._altKeyDown).done(undefined, console.error);
	}

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

97
		let altSubscription: IDisposable;
98 99 100 101 102 103 104
		let mouseOver: boolean;
		this._callOnDispose.push(domEvent(container, 'mouseleave')(_ => {
			if (!this._altKeyDown) {
				dispose(altSubscription);
			}
			mouseOver = false;
		}));
105
		this._callOnDispose.push(domEvent(container, 'mouseenter')(e => {
106
			mouseOver = true;
107
			altSubscription = _altKey.event(value => {
108 109 110
				if (!mouseOver) {
					dispose(altSubscription);
				}
111
				this._altKeyDown = value;
112 113 114
				this._updateLabel();
				this._updateTooltip();
				this._updateClass();
115
			});
116 117 118 119 120
		}));
	}

	_updateLabel(): void {
		if (this.options.label) {
121
			this.$e.text(this._command.title);
122 123 124 125 126
		}
	}

	_updateTooltip(): void {
		const element = this.$e.getHTMLElement();
127
		const keybinding = this._keybindingService.lookupKeybindings(this._command.id)[0];
128 129 130
		const keybindingLabel = keybinding && this._keybindingService.getLabelFor(keybinding);

		element.title = keybindingLabel
131 132
			? localize('titleAndKb', "{0} ({1})", this._command.title, keybindingLabel)
			: this._command.title;
133 134 135 136 137
	}

	_updateClass(): void {
		if (this.options.icon) {
			const element = this.$e.getHTMLElement();
138 139 140 141 142 143 144
			const {command, altCommand} = (<MenuItemAction>this._action);
			if (this._command !== command) {
				element.classList.remove(command.iconClass);
			} else if (altCommand) {
				element.classList.remove(altCommand.iconClass);
			}
			element.classList.add('icon', this._command.iconClass);
145 146 147
		}
	}
}