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

import 'vs/css!./actionbar';
7
import * as platform from 'vs/base/common/platform';
8
import * as nls from 'vs/nls';
9
import { Disposable, dispose } from 'vs/base/common/lifecycle';
10
import { SelectBox, ISelectOptionItem, ISelectBoxOptions } from 'vs/base/browser/ui/selectBox/selectBox';
I
isidor 已提交
11
import { IAction, IActionRunner, Action, IActionChangeEvent, ActionRunner, IRunEvent } from 'vs/base/common/actions';
12 13
import * as DOM from 'vs/base/browser/dom';
import * as types from 'vs/base/common/types';
14
import { EventType, Gesture } from 'vs/base/browser/touch';
J
Johannes Rieken 已提交
15
import { StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent';
16
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
17
import { IContextViewProvider } from 'vs/base/browser/ui/contextview/contextview';
M
Matt Bierner 已提交
18
import { Event, Emitter } from 'vs/base/common/event';
M
Matt Bierner 已提交
19
import { asArray } from 'vs/base/common/arrays';
E
Erich Gamma 已提交
20

I
isidor 已提交
21
export interface IActionItem {
M
Matt Bierner 已提交
22
	actionRunner: IActionRunner;
B
Benjamin Pasero 已提交
23 24 25
	setActionContext(context: any): void;
	render(element: HTMLElement): void;
	isEnabled(): boolean;
I
isidor 已提交
26
	focus(fromRight?: boolean): void;
B
Benjamin Pasero 已提交
27 28
	blur(): void;
	dispose(): void;
E
Erich Gamma 已提交
29 30
}

31 32
export interface IBaseActionItemOptions {
	draggable?: boolean;
33
	isMenu?: boolean;
34 35
}

36
export class BaseActionItem extends Disposable implements IActionItem {
E
Erich Gamma 已提交
37

M
Matt Bierner 已提交
38
	element?: HTMLElement;
39 40
	_context: any;
	_action: IAction;
B
Benjamin Pasero 已提交
41 42

	private _actionRunner: IActionRunner;
E
Erich Gamma 已提交
43

44
	constructor(context: any, action: IAction, protected options?: IBaseActionItemOptions) {
B
Benjamin Pasero 已提交
45 46
		super();

E
Erich Gamma 已提交
47 48 49
		this._context = context || this;
		this._action = action;

B
Benjamin Pasero 已提交
50
		if (action instanceof Action) {
B
Benjamin Pasero 已提交
51 52
			this._register(action.onDidChange(event => {
				if (!this.element) {
E
Erich Gamma 已提交
53 54 55 56
					// we have not been rendered yet, so there
					// is no point in updating the UI
					return;
				}
57 58

				this.handleActionChangeEvent(event);
59 60 61
			}));
		}
	}
E
Erich Gamma 已提交
62

63
	private handleActionChangeEvent(event: IActionChangeEvent): void {
R
Rob Lourens 已提交
64
		if (event.enabled !== undefined) {
65
			this.updateEnabled();
66
		}
67

R
Rob Lourens 已提交
68
		if (event.checked !== undefined) {
69
			this.updateChecked();
70
		}
71

R
Rob Lourens 已提交
72
		if (event.class !== undefined) {
73
			this.updateClass();
74
		}
75

R
Rob Lourens 已提交
76
		if (event.label !== undefined) {
77 78
			this.updateLabel();
			this.updateTooltip();
79
		}
80

R
Rob Lourens 已提交
81
		if (event.tooltip !== undefined) {
82
			this.updateTooltip();
E
Erich Gamma 已提交
83 84 85
		}
	}

86
	set actionRunner(actionRunner: IActionRunner) {
E
Erich Gamma 已提交
87 88 89
		this._actionRunner = actionRunner;
	}

90
	get actionRunner(): IActionRunner {
E
Erich Gamma 已提交
91 92 93
		return this._actionRunner;
	}

94
	getAction(): IAction {
E
Erich Gamma 已提交
95 96 97
		return this._action;
	}

98
	isEnabled(): boolean {
E
Erich Gamma 已提交
99 100 101
		return this._action.enabled;
	}

102
	setActionContext(newContext: any): void {
E
Erich Gamma 已提交
103 104 105
		this._context = newContext;
	}

106
	render(container: HTMLElement): void {
B
Benjamin Pasero 已提交
107
		this.element = container;
108
		Gesture.addTarget(container);
E
Erich Gamma 已提交
109

110 111 112 113 114
		const enableDragging = this.options && this.options.draggable;
		if (enableDragging) {
			container.draggable = true;
		}

B
Benjamin Pasero 已提交
115
		this._register(DOM.addDisposableListener(this.element, EventType.Tap, e => this.onClick(e)));
E
Erich Gamma 已提交
116

B
Benjamin Pasero 已提交
117
		this._register(DOM.addDisposableListener(this.element, DOM.EventType.MOUSE_DOWN, e => {
118
			if (!enableDragging) {
J
Joao Moreno 已提交
119
				DOM.EventHelper.stop(e, true); // do not run when dragging is on because that would disable it
120 121
			}

M
Matt Bierner 已提交
122
			if (this._action.enabled && e.button === 0 && this.element) {
B
Benjamin Pasero 已提交
123
				DOM.addClass(this.element, 'active');
E
Erich Gamma 已提交
124
			}
B
Benjamin Pasero 已提交
125
		}));
126

B
Benjamin Pasero 已提交
127
		this._register(DOM.addDisposableListener(this.element, DOM.EventType.CLICK, e => {
J
Johannes Rieken 已提交
128
			DOM.EventHelper.stop(e, true);
129 130 131 132 133 134 135 136 137 138 139
			// See https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Interact_with_the_clipboard
			// > Writing to the clipboard
			// > You can use the "cut" and "copy" commands without any special
			// permission if you are using them in a short-lived event handler
			// for a user action (for example, a click handler).

			// => to get the Copy and Paste context menu actions working on Firefox,
			// there should be no timeout here
			if (this.options && this.options.isMenu) {
				this.onClick(e);
			} else {
140
				platform.setImmediate(() => this.onClick(e));
141
			}
B
Benjamin Pasero 已提交
142
		}));
E
Erich Gamma 已提交
143

J
Joao Moreno 已提交
144 145 146 147
		this._register(DOM.addDisposableListener(this.element, DOM.EventType.DBLCLICK, e => {
			DOM.EventHelper.stop(e, true);
		}));

B
Benjamin Pasero 已提交
148
		[DOM.EventType.MOUSE_UP, DOM.EventType.MOUSE_OUT].forEach(event => {
M
Matt Bierner 已提交
149
			this._register(DOM.addDisposableListener(this.element!, event, e => {
B
Benjamin Pasero 已提交
150
				DOM.EventHelper.stop(e);
M
Matt Bierner 已提交
151
				DOM.removeClass(this.element!, 'active');
B
Benjamin Pasero 已提交
152
			}));
E
Erich Gamma 已提交
153 154 155
		});
	}

156
	onClick(event: DOM.EventLike): void {
B
Benjamin Pasero 已提交
157
		DOM.EventHelper.stop(event, true);
J
Joao Moreno 已提交
158

159
		let context: any;
S
SteVen Batten 已提交
160
		if (types.isUndefinedOrNull(this._context)) {
161 162 163
			context = event;
		} else {
			context = this._context;
S
SteVen Batten 已提交
164 165 166 167

			if (types.isObject(context)) {
				context.event = event;
			}
168 169
		}

170
		this._actionRunner.run(this._action, context);
E
Erich Gamma 已提交
171 172
	}

173
	focus(): void {
B
Benjamin Pasero 已提交
174 175 176
		if (this.element) {
			this.element.focus();
			DOM.addClass(this.element, 'focused');
177
		}
E
Erich Gamma 已提交
178 179
	}

180
	blur(): void {
B
Benjamin Pasero 已提交
181 182 183
		if (this.element) {
			this.element.blur();
			DOM.removeClass(this.element, 'focused');
184
		}
E
Erich Gamma 已提交
185 186
	}

187
	protected updateEnabled(): void {
E
Erich Gamma 已提交
188 189 190
		// implement in subclass
	}

191
	protected updateLabel(): void {
E
Erich Gamma 已提交
192 193 194
		// implement in subclass
	}

195
	protected updateTooltip(): void {
E
Erich Gamma 已提交
196 197 198
		// implement in subclass
	}

199
	protected updateClass(): void {
E
Erich Gamma 已提交
200 201 202
		// implement in subclass
	}

203
	protected updateChecked(): void {
E
Erich Gamma 已提交
204 205 206
		// implement in subclass
	}

207
	dispose(): void {
B
Benjamin Pasero 已提交
208
		if (this.element) {
A
Alex Dima 已提交
209
			DOM.removeNode(this.element);
M
Matt Bierner 已提交
210
			this.element = undefined;
E
Erich Gamma 已提交
211 212
		}

B
Benjamin Pasero 已提交
213
		super.dispose();
E
Erich Gamma 已提交
214 215 216
	}
}

B
Benjamin Pasero 已提交
217
export class Separator extends Action {
E
Erich Gamma 已提交
218

219
	static readonly ID = 'vs.actions.separator';
E
Erich Gamma 已提交
220

B
Benjamin Pasero 已提交
221
	constructor(label?: string) {
E
Erich Gamma 已提交
222 223
		super(Separator.ID, label, label ? 'separator text' : 'separator');
		this.checked = false;
224
		this.radio = false;
E
Erich Gamma 已提交
225 226 227 228
		this.enabled = false;
	}
}

229
export interface IActionItemOptions extends IBaseActionItemOptions {
B
Benjamin Pasero 已提交
230 231
	icon?: boolean;
	label?: boolean;
A
Alex Dima 已提交
232
	keybinding?: string | null;
E
Erich Gamma 已提交
233 234 235 236
}

export class ActionItem extends BaseActionItem {

B
Benjamin Pasero 已提交
237
	protected label: HTMLElement;
J
Johannes Rieken 已提交
238
	protected options: IActionItemOptions;
B
Benjamin Pasero 已提交
239

M
Matt Bierner 已提交
240
	private cssClass?: string;
E
Erich Gamma 已提交
241

B
Benjamin Pasero 已提交
242
	constructor(context: any, action: IAction, options: IActionItemOptions = {}) {
243
		super(context, action, options);
E
Erich Gamma 已提交
244 245 246 247 248 249 250

		this.options = options;
		this.options.icon = options.icon !== undefined ? options.icon : false;
		this.options.label = options.label !== undefined ? options.label : true;
		this.cssClass = '';
	}

251
	render(container: HTMLElement): void {
E
Erich Gamma 已提交
252 253
		super.render(container);

M
Matt Bierner 已提交
254 255 256
		if (this.element) {
			this.label = DOM.append(this.element, DOM.$('a.action-label'));
		}
257
		if (this._action.id === Separator.ID) {
258
			this.label.setAttribute('role', 'presentation'); // A separator is a presentation item
A
Alex Dima 已提交
259
		} else {
260
			if (this.options.isMenu) {
B
Benjamin Pasero 已提交
261
				this.label.setAttribute('role', 'menuitem');
262
			} else {
B
Benjamin Pasero 已提交
263
				this.label.setAttribute('role', 'button');
264
			}
A
Alex Dima 已提交
265
		}
E
Erich Gamma 已提交
266

M
Matt Bierner 已提交
267
		if (this.options.label && this.options.keybinding && this.element) {
B
Benjamin Pasero 已提交
268
			DOM.append(this.element, DOM.$('span.keybinding')).textContent = this.options.keybinding;
E
Erich Gamma 已提交
269 270
		}

271 272 273 274 275
		this.updateClass();
		this.updateLabel();
		this.updateTooltip();
		this.updateEnabled();
		this.updateChecked();
E
Erich Gamma 已提交
276 277
	}

278
	focus(): void {
E
Erich Gamma 已提交
279
		super.focus();
280

B
Benjamin Pasero 已提交
281
		this.label.focus();
E
Erich Gamma 已提交
282 283
	}

284
	updateLabel(): void {
E
Erich Gamma 已提交
285
		if (this.options.label) {
B
Benjamin Pasero 已提交
286
			this.label.textContent = this.getAction().label;
E
Erich Gamma 已提交
287 288 289
		}
	}

290
	updateTooltip(): void {
291
		let title: string | null = null;
E
Erich Gamma 已提交
292 293 294 295 296 297 298 299

		if (this.getAction().tooltip) {
			title = this.getAction().tooltip;

		} else if (!this.options.label && this.getAction().label && this.options.icon) {
			title = this.getAction().label;

			if (this.options.keybinding) {
B
Benjamin Pasero 已提交
300
				title = nls.localize({ key: 'titleLabel', comment: ['action title', 'action keybinding'] }, "{0} ({1})", title, this.options.keybinding);
E
Erich Gamma 已提交
301 302 303 304
			}
		}

		if (title) {
B
Benjamin Pasero 已提交
305
			this.label.title = title;
E
Erich Gamma 已提交
306 307 308
		}
	}

309
	updateClass(): void {
E
Erich Gamma 已提交
310
		if (this.cssClass) {
B
Benjamin Pasero 已提交
311
			DOM.removeClasses(this.label, this.cssClass);
E
Erich Gamma 已提交
312
		}
313

E
Erich Gamma 已提交
314 315
		if (this.options.icon) {
			this.cssClass = this.getAction().class;
B
Benjamin Pasero 已提交
316
			DOM.addClass(this.label, 'icon');
E
Erich Gamma 已提交
317
			if (this.cssClass) {
B
Benjamin Pasero 已提交
318
				DOM.addClasses(this.label, this.cssClass);
E
Erich Gamma 已提交
319
			}
320 321

			this.updateEnabled();
E
Erich Gamma 已提交
322
		} else {
B
Benjamin Pasero 已提交
323
			DOM.removeClass(this.label, 'icon');
E
Erich Gamma 已提交
324 325 326
		}
	}

327
	updateEnabled(): void {
B
Benjamin Pasero 已提交
328
		if (this.getAction().enabled) {
329
			this.label.removeAttribute('aria-disabled');
M
Matt Bierner 已提交
330 331 332
			if (this.element) {
				DOM.removeClass(this.element, 'disabled');
			}
B
Benjamin Pasero 已提交
333 334
			DOM.removeClass(this.label, 'disabled');
			this.label.tabIndex = 0;
E
Erich Gamma 已提交
335
		} else {
336
			this.label.setAttribute('aria-disabled', 'true');
M
Matt Bierner 已提交
337 338 339
			if (this.element) {
				DOM.addClass(this.element, 'disabled');
			}
B
Benjamin Pasero 已提交
340 341
			DOM.addClass(this.label, 'disabled');
			DOM.removeTabIndexAndUpdateFocus(this.label);
E
Erich Gamma 已提交
342 343 344
		}
	}

345
	updateChecked(): void {
B
Benjamin Pasero 已提交
346
		if (this.getAction().checked) {
B
Benjamin Pasero 已提交
347
			DOM.addClass(this.label, 'checked');
E
Erich Gamma 已提交
348
		} else {
B
Benjamin Pasero 已提交
349
			DOM.removeClass(this.label, 'checked');
E
Erich Gamma 已提交
350 351 352 353
		}
	}
}

354
export const enum ActionsOrientation {
J
Joao Moreno 已提交
355 356 357 358
	HORIZONTAL,
	HORIZONTAL_REVERSE,
	VERTICAL,
	VERTICAL_REVERSE,
E
Erich Gamma 已提交
359 360
}

361 362 363 364 365
export interface ActionTrigger {
	keys: KeyCode[];
	keyDown: boolean;
}

E
Erich Gamma 已提交
366
export interface IActionItemProvider {
367
	(action: IAction): IActionItem | undefined;
E
Erich Gamma 已提交
368 369 370
}

export interface IActionBarOptions {
B
Benjamin Pasero 已提交
371 372 373
	orientation?: ActionsOrientation;
	context?: any;
	actionItemProvider?: IActionItemProvider;
B
Benjamin Pasero 已提交
374
	actionRunner?: IActionRunner;
375
	ariaLabel?: string;
J
Joao Moreno 已提交
376
	animated?: boolean;
377
	triggerKeys?: ActionTrigger;
E
Erich Gamma 已提交
378 379
}

380
const defaultOptions: IActionBarOptions = {
E
Erich Gamma 已提交
381
	orientation: ActionsOrientation.HORIZONTAL,
382 383 384 385 386
	context: null,
	triggerKeys: {
		keys: [KeyCode.Enter, KeyCode.Space],
		keyDown: false
	}
E
Erich Gamma 已提交
387 388 389
};

export interface IActionOptions extends IActionItemOptions {
B
Benjamin Pasero 已提交
390
	index?: number;
E
Erich Gamma 已提交
391 392
}

393
export class ActionBar extends Disposable implements IActionRunner {
E
Erich Gamma 已提交
394

395
	options: IActionBarOptions;
396

M
Matt Bierner 已提交
397
	private _actionRunner: IActionRunner;
E
Erich Gamma 已提交
398 399 400
	private _context: any;

	// Items
401
	items: IActionItem[];
M
Matt Bierner 已提交
402
	protected focusedItem?: number;
403
	private focusTracker: DOM.IFocusTracker;
E
Erich Gamma 已提交
404 405

	// Elements
406
	domNode: HTMLElement;
S
SteVen Batten 已提交
407
	protected actionsList: HTMLElement;
E
Erich Gamma 已提交
408

409 410 411 412 413 414 415 416 417 418 419
	private _onDidBlur = this._register(new Emitter<void>());
	get onDidBlur(): Event<void> { return this._onDidBlur.event; }

	private _onDidCancel = this._register(new Emitter<void>());
	get onDidCancel(): Event<void> { return this._onDidCancel.event; }

	private _onDidRun = this._register(new Emitter<IRunEvent>());
	get onDidRun(): Event<IRunEvent> { return this._onDidRun.event; }

	private _onDidBeforeRun = this._register(new Emitter<IRunEvent>());
	get onDidBeforeRun(): Event<IRunEvent> { return this._onDidBeforeRun.event; }
I
isidor 已提交
420

421
	constructor(container: HTMLElement, options: IActionBarOptions = defaultOptions) {
B
Benjamin Pasero 已提交
422 423
		super();

E
Erich Gamma 已提交
424 425 426
		this.options = options;
		this._context = options.context;

S
SteVen Batten 已提交
427 428 429 430
		if (!this.options.triggerKeys) {
			this.options.triggerKeys = defaultOptions.triggerKeys;
		}

M
Matt Bierner 已提交
431 432 433
		if (this.options.actionRunner) {
			this._actionRunner = this.options.actionRunner;
		} else {
B
Benjamin Pasero 已提交
434
			this._actionRunner = new ActionRunner();
B
Benjamin Pasero 已提交
435
			this._register(this._actionRunner);
E
Erich Gamma 已提交
436 437
		}

B
Benjamin Pasero 已提交
438 439
		this._register(this._actionRunner.onDidRun(e => this._onDidRun.fire(e)));
		this._register(this._actionRunner.onDidBeforeRun(e => this._onDidBeforeRun.fire(e)));
E
Erich Gamma 已提交
440 441 442 443 444 445 446

		this.items = [];
		this.focusedItem = undefined;

		this.domNode = document.createElement('div');
		this.domNode.className = 'monaco-action-bar';

J
Joao Moreno 已提交
447 448 449 450
		if (options.animated !== false) {
			DOM.addClass(this.domNode, 'animated');
		}

J
Joao Moreno 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
		let previousKey: KeyCode;
		let nextKey: KeyCode;

		switch (this.options.orientation) {
			case ActionsOrientation.HORIZONTAL:
				previousKey = KeyCode.LeftArrow;
				nextKey = KeyCode.RightArrow;
				break;
			case ActionsOrientation.HORIZONTAL_REVERSE:
				previousKey = KeyCode.RightArrow;
				nextKey = KeyCode.LeftArrow;
				this.domNode.className += ' reverse';
				break;
			case ActionsOrientation.VERTICAL:
				previousKey = KeyCode.UpArrow;
				nextKey = KeyCode.DownArrow;
				this.domNode.className += ' vertical';
				break;
			case ActionsOrientation.VERTICAL_REVERSE:
				previousKey = KeyCode.DownArrow;
				nextKey = KeyCode.UpArrow;
				this.domNode.className += ' vertical reverse';
				break;
E
Erich Gamma 已提交
474 475
		}

B
Benjamin Pasero 已提交
476
		this._register(DOM.addDisposableListener(this.domNode, DOM.EventType.KEY_DOWN, e => {
477
			const event = new StandardKeyboardEvent(e);
B
Benjamin Pasero 已提交
478
			let eventHandled = true;
E
Erich Gamma 已提交
479

J
Joao Moreno 已提交
480
			if (event.equals(previousKey)) {
E
Erich Gamma 已提交
481
				this.focusPrevious();
J
Joao Moreno 已提交
482
			} else if (event.equals(nextKey)) {
E
Erich Gamma 已提交
483
				this.focusNext();
A
Alexandru Dima 已提交
484
			} else if (event.equals(KeyCode.Escape)) {
E
Erich Gamma 已提交
485
				this.cancel();
486 487 488 489 490
			} else if (this.isTriggerKeyEvent(event)) {
				// Staying out of the else branch even if not triggered
				if (this.options.triggerKeys && this.options.triggerKeys.keyDown) {
					this.doTrigger(event);
				}
E
Erich Gamma 已提交
491 492 493 494
			} else {
				eventHandled = false;
			}

B
Benjamin Pasero 已提交
495
			if (eventHandled) {
E
Erich Gamma 已提交
496 497 498
				event.preventDefault();
				event.stopPropagation();
			}
B
Benjamin Pasero 已提交
499
		}));
E
Erich Gamma 已提交
500

B
Benjamin Pasero 已提交
501
		this._register(DOM.addDisposableListener(this.domNode, DOM.EventType.KEY_UP, e => {
502
			const event = new StandardKeyboardEvent(e);
E
Erich Gamma 已提交
503

504
			// Run action on Enter/Space
505
			if (this.isTriggerKeyEvent(event)) {
M
Matt Bierner 已提交
506
				if (this.options.triggerKeys && !this.options.triggerKeys.keyDown) {
507 508 509
					this.doTrigger(event);
				}

E
Erich Gamma 已提交
510 511 512
				event.preventDefault();
				event.stopPropagation();
			}
513 514

			// Recompute focused item
A
Alexandru Dima 已提交
515
			else if (event.equals(KeyCode.Tab) || event.equals(KeyMod.Shift | KeyCode.Tab)) {
516 517
				this.updateFocusedItem();
			}
B
Benjamin Pasero 已提交
518
		}));
E
Erich Gamma 已提交
519

B
Benjamin Pasero 已提交
520 521
		this.focusTracker = this._register(DOM.trackFocus(this.domNode));
		this._register(this.focusTracker.onDidBlur(() => {
B
Benjamin Pasero 已提交
522
			if (document.activeElement === this.domNode || !DOM.isAncestor(document.activeElement, this.domNode)) {
I
isidor 已提交
523
				this._onDidBlur.fire();
524 525
				this.focusedItem = undefined;
			}
526
		}));
527

B
Benjamin Pasero 已提交
528
		this._register(this.focusTracker.onDidFocus(() => this.updateFocusedItem()));
E
Erich Gamma 已提交
529 530 531

		this.actionsList = document.createElement('ul');
		this.actionsList.className = 'actions-container';
S
SteVen Batten 已提交
532 533
		this.actionsList.setAttribute('role', 'toolbar');

534 535 536 537
		if (this.options.ariaLabel) {
			this.actionsList.setAttribute('aria-label', this.options.ariaLabel);
		}

E
Erich Gamma 已提交
538 539
		this.domNode.appendChild(this.actionsList);

540
		container.appendChild(this.domNode);
E
Erich Gamma 已提交
541 542
	}

543
	setAriaLabel(label: string): void {
544 545
		if (label) {
			this.actionsList.setAttribute('aria-label', label);
B
Benjamin Pasero 已提交
546
		} else {
547 548 549 550
			this.actionsList.removeAttribute('aria-label');
		}
	}

551 552 553 554 555 556 557 558 559 560 561
	private isTriggerKeyEvent(event: StandardKeyboardEvent): boolean {
		let ret = false;
		if (this.options.triggerKeys) {
			this.options.triggerKeys.keys.forEach(keyCode => {
				ret = ret || event.equals(keyCode);
			});
		}

		return ret;
	}

562 563
	private updateFocusedItem(): void {
		for (let i = 0; i < this.actionsList.children.length; i++) {
564
			const elem = this.actionsList.children[i];
565 566 567 568 569 570 571
			if (DOM.isAncestor(document.activeElement, elem)) {
				this.focusedItem = i;
				break;
			}
		}
	}

572
	get context(): any {
E
Erich Gamma 已提交
573 574 575
		return this._context;
	}

576
	set context(context: any) {
E
Erich Gamma 已提交
577 578 579 580
		this._context = context;
		this.items.forEach(i => i.setActionContext(context));
	}

M
Matt Bierner 已提交
581
	get actionRunner(): IActionRunner {
E
Erich Gamma 已提交
582 583 584
		return this._actionRunner;
	}

M
Matt Bierner 已提交
585
	set actionRunner(actionRunner: IActionRunner) {
E
Erich Gamma 已提交
586 587 588 589 590 591
		if (actionRunner) {
			this._actionRunner = actionRunner;
			this.items.forEach(item => item.actionRunner = actionRunner);
		}
	}

592
	getContainer(): HTMLElement {
593
		return this.domNode;
E
Erich Gamma 已提交
594 595
	}

596
	push(arg: IAction | IAction[], options: IActionOptions = {}): void {
M
Matt Bierner 已提交
597
		const actions: IAction[] = asArray(arg);
E
Erich Gamma 已提交
598

B
Benjamin Pasero 已提交
599
		let index = types.isNumber(options.index) ? options.index : null;
E
Erich Gamma 已提交
600

B
Benjamin Pasero 已提交
601
		actions.forEach((action: IAction) => {
602
			const actionItemElement = document.createElement('li');
E
Erich Gamma 已提交
603 604 605
			actionItemElement.className = 'action-item';
			actionItemElement.setAttribute('role', 'presentation');

606
			// Prevent native context menu on actions
B
Benjamin Pasero 已提交
607
			this._register(DOM.addDisposableListener(actionItemElement, DOM.EventType.CONTEXT_MENU, (e: DOM.EventLike) => {
608 609
				e.preventDefault();
				e.stopPropagation();
B
Benjamin Pasero 已提交
610
			}));
611

612
			let item: IActionItem | undefined;
E
Erich Gamma 已提交
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627

			if (this.options.actionItemProvider) {
				item = this.options.actionItemProvider(action);
			}

			if (!item) {
				item = new ActionItem(this.context, action, options);
			}

			item.actionRunner = this._actionRunner;
			item.setActionContext(this.context);
			item.render(actionItemElement);

			if (index === null || index < 0 || index >= this.actionsList.children.length) {
				this.actionsList.appendChild(actionItemElement);
628
				this.items.push(item);
E
Erich Gamma 已提交
629
			} else {
630 631 632
				this.actionsList.insertBefore(actionItemElement, this.actionsList.children[index]);
				this.items.splice(index, 0, item);
				index++;
E
Erich Gamma 已提交
633 634 635 636
			}
		});
	}

637
	getWidth(index: number): number {
638
		if (index >= 0 && index < this.actionsList.children.length) {
M
Matt Bierner 已提交
639 640 641 642
			const item = this.actionsList.children.item(index);
			if (item) {
				return item.clientWidth;
			}
643 644 645 646 647
		}

		return 0;
	}

648
	getHeight(index: number): number {
649
		if (index >= 0 && index < this.actionsList.children.length) {
M
Matt Bierner 已提交
650 651 652 653
			const item = this.actionsList.children.item(index);
			if (item) {
				return item.clientHeight;
			}
654 655 656 657 658
		}

		return 0;
	}

659
	pull(index: number): void {
P
Pine Wu 已提交
660
		if (index >= 0 && index < this.items.length) {
P
Pine Wu 已提交
661
			this.actionsList.removeChild(this.actionsList.childNodes[index]);
662
			dispose(this.items.splice(index, 1));
P
Pine Wu 已提交
663 664 665
		}
	}

666 667
	clear(): void {
		this.items = dispose(this.items);
B
Benjamin Pasero 已提交
668
		DOM.clearNode(this.actionsList);
E
Erich Gamma 已提交
669 670
	}

671
	length(): number {
E
Erich Gamma 已提交
672 673 674
		return this.items.length;
	}

675
	isEmpty(): boolean {
E
Erich Gamma 已提交
676 677 678
		return this.items.length === 0;
	}

S
SteVen Batten 已提交
679 680 681 682
	focus(index?: number): void;
	focus(selectFirst?: boolean): void;
	focus(arg?: any): void {
		let selectFirst: boolean = false;
R
Rob Lourens 已提交
683
		let index: number | undefined = undefined;
S
SteVen Batten 已提交
684 685 686 687 688 689 690 691
		if (arg === undefined) {
			selectFirst = true;
		} else if (typeof arg === 'number') {
			index = arg;
		} else if (typeof arg === 'boolean') {
			selectFirst = arg;
		}

E
Erich Gamma 已提交
692
		if (selectFirst && typeof this.focusedItem === 'undefined') {
S
SteVen Batten 已提交
693 694 695 696
			// Focus the first enabled item
			this.focusedItem = this.items.length - 1;
			this.focusNext();
		} else {
S
SteVen Batten 已提交
697 698 699 700
			if (index !== undefined) {
				this.focusedItem = index;
			}

S
SteVen Batten 已提交
701
			this.updateFocus();
E
Erich Gamma 已提交
702 703 704
		}
	}

S
SteVen Batten 已提交
705
	protected focusNext(): void {
E
Erich Gamma 已提交
706 707 708 709
		if (typeof this.focusedItem === 'undefined') {
			this.focusedItem = this.items.length - 1;
		}

710
		const startIndex = this.focusedItem;
B
Benjamin Pasero 已提交
711
		let item: IActionItem;
E
Erich Gamma 已提交
712 713 714 715 716 717 718 719 720 721 722 723 724

		do {
			this.focusedItem = (this.focusedItem + 1) % this.items.length;
			item = this.items[this.focusedItem];
		} while (this.focusedItem !== startIndex && !item.isEnabled());

		if (this.focusedItem === startIndex && !item.isEnabled()) {
			this.focusedItem = undefined;
		}

		this.updateFocus();
	}

S
SteVen Batten 已提交
725
	protected focusPrevious(): void {
E
Erich Gamma 已提交
726 727 728 729
		if (typeof this.focusedItem === 'undefined') {
			this.focusedItem = 0;
		}

730
		const startIndex = this.focusedItem;
B
Benjamin Pasero 已提交
731
		let item: IActionItem;
E
Erich Gamma 已提交
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746

		do {
			this.focusedItem = this.focusedItem - 1;

			if (this.focusedItem < 0) {
				this.focusedItem = this.items.length - 1;
			}

			item = this.items[this.focusedItem];
		} while (this.focusedItem !== startIndex && !item.isEnabled());

		if (this.focusedItem === startIndex && !item.isEnabled()) {
			this.focusedItem = undefined;
		}

I
isidor 已提交
747
		this.updateFocus(true);
E
Erich Gamma 已提交
748 749
	}

S
SteVen Batten 已提交
750
	protected updateFocus(fromRight?: boolean): void {
E
Erich Gamma 已提交
751
		if (typeof this.focusedItem === 'undefined') {
752
			this.actionsList.focus();
E
Erich Gamma 已提交
753 754
		}

B
Benjamin Pasero 已提交
755
		for (let i = 0; i < this.items.length; i++) {
756 757
			const item = this.items[i];
			const actionItem = item;
E
Erich Gamma 已提交
758

B
Benjamin Pasero 已提交
759
			if (i === this.focusedItem) {
760 761 762 763
				if (types.isFunction(actionItem.isEnabled)) {
					if (actionItem.isEnabled() && types.isFunction(actionItem.focus)) {
						actionItem.focus(fromRight);
					} else {
764
						this.actionsList.focus();
765
					}
E
Erich Gamma 已提交
766 767
				}
			} else {
B
Benjamin Pasero 已提交
768
				if (types.isFunction(actionItem.blur)) {
E
Erich Gamma 已提交
769 770 771 772 773 774
					actionItem.blur();
				}
			}
		}
	}

B
Benjamin Pasero 已提交
775
	private doTrigger(event: StandardKeyboardEvent): void {
B
Benjamin Pasero 已提交
776
		if (typeof this.focusedItem === 'undefined') {
777
			return; //nothing to focus
E
Erich Gamma 已提交
778 779 780
		}

		// trigger action
781
		const actionItem = this.items[this.focusedItem];
I
isidor 已提交
782 783
		if (actionItem instanceof BaseActionItem) {
			const context = (actionItem._context === null || actionItem._context === undefined) ? event : actionItem._context;
784
			this.run(actionItem._action, context);
I
isidor 已提交
785
		}
E
Erich Gamma 已提交
786 787
	}

B
Benjamin Pasero 已提交
788
	private cancel(): void {
B
Benjamin Pasero 已提交
789
		if (document.activeElement instanceof HTMLElement) {
790
			(<HTMLElement>document.activeElement).blur(); // remove focus from focused action
B
Benjamin Pasero 已提交
791 792
		}

I
isidor 已提交
793
		this._onDidCancel.fire();
E
Erich Gamma 已提交
794 795
	}

J
Johannes Rieken 已提交
796
	run(action: IAction, context?: any): Promise<void> {
M
Matt Bierner 已提交
797
		return this._actionRunner.run(action, context);
E
Erich Gamma 已提交
798 799
	}

800
	dispose(): void {
M
Matt Bierner 已提交
801 802
		dispose(this.items);
		this.items = [];
E
Erich Gamma 已提交
803

A
Alex Dima 已提交
804
		DOM.removeNode(this.getContainer());
B
Benjamin Pasero 已提交
805 806

		super.dispose();
E
Erich Gamma 已提交
807 808 809 810
	}
}

export class SelectActionItem extends BaseActionItem {
811
	protected selectBox: SelectBox;
E
Erich Gamma 已提交
812

813
	constructor(ctx: any, action: IAction, options: ISelectOptionItem[], selected: number, contextViewProvider: IContextViewProvider, selectBoxOptions?: ISelectBoxOptions) {
E
Erich Gamma 已提交
814
		super(ctx, action);
B
Benjamin Pasero 已提交
815

M
Matt Bierner 已提交
816
		this.selectBox = new SelectBox(options, selected, contextViewProvider, undefined, selectBoxOptions);
E
Erich Gamma 已提交
817

B
Benjamin Pasero 已提交
818
		this._register(this.selectBox);
E
Erich Gamma 已提交
819 820 821
		this.registerListeners();
	}

822 823
	setOptions(options: ISelectOptionItem[], selected?: number): void {
		this.selectBox.setOptions(options, selected);
E
Erich Gamma 已提交
824 825
	}

826
	select(index: number): void {
I
isidor 已提交
827 828 829
		this.selectBox.select(index);
	}

E
Erich Gamma 已提交
830
	private registerListeners(): void {
B
Benjamin Pasero 已提交
831
		this._register(this.selectBox.onDidSelect(e => {
S
Sandeep Somavarapu 已提交
832
			this.actionRunner.run(this._action, this.getActionContext(e.selected, e.index));
E
Erich Gamma 已提交
833 834 835
		}));
	}

S
Sandeep Somavarapu 已提交
836
	protected getActionContext(option: string, index: number) {
I
isidor 已提交
837 838 839
		return option;
	}

840
	focus(): void {
I
isidor 已提交
841 842
		if (this.selectBox) {
			this.selectBox.focus();
843 844 845
		}
	}

846
	blur(): void {
I
isidor 已提交
847 848
		if (this.selectBox) {
			this.selectBox.blur();
849 850 851
		}
	}

852
	render(container: HTMLElement): void {
I
isidor 已提交
853
		this.selectBox.render(container);
E
Erich Gamma 已提交
854
	}
855
}