actionbar.ts 21.5 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, IDisposable } 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';
E
Erich Gamma 已提交
19

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

29
export interface IBaseActionViewItemOptions {
30
	draggable?: boolean;
31
	isMenu?: boolean;
32 33
}

34
export class BaseActionViewItem extends Disposable implements IActionViewItem {
E
Erich Gamma 已提交
35

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

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

42
	constructor(context: any, action: IAction, protected options?: IBaseActionViewItemOptions) {
B
Benjamin Pasero 已提交
43 44
		super();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

B
Benjamin Pasero 已提交
125
		this._register(DOM.addDisposableListener(this.element, DOM.EventType.CLICK, e => {
J
Johannes Rieken 已提交
126
			DOM.EventHelper.stop(e, true);
127 128 129 130 131 132 133 134 135 136 137
			// 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 {
138
				platform.setImmediate(() => this.onClick(e));
139
			}
B
Benjamin Pasero 已提交
140
		}));
E
Erich Gamma 已提交
141

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

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

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

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

			if (types.isObject(context)) {
				context.event = event;
			}
166 167
		}

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

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

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

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

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

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

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

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

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

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

B
Benjamin Pasero 已提交
215
export class Separator extends Action {
E
Erich Gamma 已提交
216

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

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

227
export interface IActionViewItemOptions extends IBaseActionViewItemOptions {
B
Benjamin Pasero 已提交
228 229
	icon?: boolean;
	label?: boolean;
A
Alex Dima 已提交
230
	keybinding?: string | null;
E
Erich Gamma 已提交
231 232
}

233
export class ActionViewItem extends BaseActionViewItem {
E
Erich Gamma 已提交
234

B
Benjamin Pasero 已提交
235
	protected label: HTMLElement;
236
	protected options: IActionViewItemOptions;
B
Benjamin Pasero 已提交
237

M
Matt Bierner 已提交
238
	private cssClass?: string;
E
Erich Gamma 已提交
239

240
	constructor(context: any, action: IAction, options: IActionViewItemOptions = {}) {
241
		super(context, action, options);
E
Erich Gamma 已提交
242 243 244 245 246 247 248

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

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

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

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

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

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

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

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

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

		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 已提交
298
				title = nls.localize({ key: 'titleLabel', comment: ['action title', 'action keybinding'] }, "{0} ({1})", title, this.options.keybinding);
E
Erich Gamma 已提交
299 300 301 302
			}
		}

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

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

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

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

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

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

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

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

364 365
export interface IActionViewItemProvider {
	(action: IAction): IActionViewItem | undefined;
E
Erich Gamma 已提交
366 367 368
}

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

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

387
export interface IActionOptions extends IActionViewItemOptions {
B
Benjamin Pasero 已提交
388
	index?: number;
E
Erich Gamma 已提交
389 390
}

391
export class ActionBar extends Disposable implements IActionRunner {
E
Erich Gamma 已提交
392

393
	options: IActionBarOptions;
394

M
Matt Bierner 已提交
395
	private _actionRunner: IActionRunner;
E
Erich Gamma 已提交
396 397
	private _context: any;

398 399
	// View Items
	viewItems: IActionViewItem[];
M
Matt Bierner 已提交
400
	protected focusedItem?: number;
401
	private focusTracker: DOM.IFocusTracker;
E
Erich Gamma 已提交
402 403

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

407 408 409 410 411 412 413 414 415 416 417
	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 已提交
418

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

E
Erich Gamma 已提交
422 423 424
		this.options = options;
		this._context = options.context;

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

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

B
Benjamin Pasero 已提交
436 437
		this._register(this._actionRunner.onDidRun(e => this._onDidRun.fire(e)));
		this._register(this._actionRunner.onDidBeforeRun(e => this._onDidBeforeRun.fire(e)));
E
Erich Gamma 已提交
438

439
		this.viewItems = [];
E
Erich Gamma 已提交
440 441 442 443 444
		this.focusedItem = undefined;

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

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

J
Joao Moreno 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
		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 已提交
472 473
		}

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

J
Joao Moreno 已提交
478
			if (event.equals(previousKey)) {
E
Erich Gamma 已提交
479
				this.focusPrevious();
J
Joao Moreno 已提交
480
			} else if (event.equals(nextKey)) {
E
Erich Gamma 已提交
481
				this.focusNext();
A
Alexandru Dima 已提交
482
			} else if (event.equals(KeyCode.Escape)) {
E
Erich Gamma 已提交
483
				this.cancel();
484 485 486 487 488
			} 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 已提交
489 490 491 492
			} else {
				eventHandled = false;
			}

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

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

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

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

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

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

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

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

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

E
Erich Gamma 已提交
536 537
		this.domNode.appendChild(this.actionsList);

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

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

549 550 551 552 553 554 555 556 557 558 559
	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;
	}

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

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

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

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

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

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

594 595
	push(arg: IAction | ReadonlyArray<IAction>, options: IActionOptions = {}): void {
		const actions: ReadonlyArray<IAction> = Array.isArray(arg) ? arg : [arg];
E
Erich Gamma 已提交
596

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

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

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

610
			let item: IActionViewItem | undefined;
E
Erich Gamma 已提交
611

612 613
			if (this.options.actionViewItemProvider) {
				item = this.options.actionViewItemProvider(action);
E
Erich Gamma 已提交
614 615 616
			}

			if (!item) {
617
				item = new ActionViewItem(this.context, action, options);
E
Erich Gamma 已提交
618 619 620 621
			}

			item.actionRunner = this._actionRunner;
			item.setActionContext(this.context);
622
			item.render(actionViewItemElement);
E
Erich Gamma 已提交
623 624

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

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

		return 0;
	}

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

		return 0;
	}

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

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

669
	length(): number {
670
		return this.viewItems.length;
E
Erich Gamma 已提交
671 672
	}

673
	isEmpty(): boolean {
674
		return this.viewItems.length === 0;
E
Erich Gamma 已提交
675 676
	}

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

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

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

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

708
		const startIndex = this.focusedItem;
709
		let item: IActionViewItem;
E
Erich Gamma 已提交
710 711

		do {
712 713
			this.focusedItem = (this.focusedItem + 1) % this.viewItems.length;
			item = this.viewItems[this.focusedItem];
E
Erich Gamma 已提交
714 715 716 717 718 719 720 721 722
		} while (this.focusedItem !== startIndex && !item.isEnabled());

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

		this.updateFocus();
	}

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

728
		const startIndex = this.focusedItem;
729
		let item: IActionViewItem;
E
Erich Gamma 已提交
730 731 732 733 734

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

			if (this.focusedItem < 0) {
735
				this.focusedItem = this.viewItems.length - 1;
E
Erich Gamma 已提交
736 737
			}

738
			item = this.viewItems[this.focusedItem];
E
Erich Gamma 已提交
739 740 741 742 743 744
		} while (this.focusedItem !== startIndex && !item.isEnabled());

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

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

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

753 754 755
		for (let i = 0; i < this.viewItems.length; i++) {
			const item = this.viewItems[i];
			const actionViewItem = item;
E
Erich Gamma 已提交
756

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

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

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

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

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

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

798
	dispose(): void {
799 800
		dispose(this.viewItems);
		this.viewItems = [];
E
Erich Gamma 已提交
801

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

		super.dispose();
E
Erich Gamma 已提交
805 806 807
	}
}

808
export class SelectActionViewItem extends BaseActionViewItem {
809
	protected selectBox: SelectBox;
E
Erich Gamma 已提交
810

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

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

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

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

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

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

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

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

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

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