inputBox.ts 20.6 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!./inputBox';
A
Alex Dima 已提交
7

8
import * as nls from 'vs/nls';
A
Alex Dima 已提交
9 10
import * as Bal from 'vs/base/browser/browser';
import * as dom from 'vs/base/browser/dom';
11
import { MarkdownRenderOptions } from 'vs/base/browser/markdownRenderer';
12
import { renderFormattedText, renderText } from 'vs/base/browser/formattedTextRenderer';
13
import * as aria from 'vs/base/browser/ui/aria/aria';
J
Johannes Rieken 已提交
14 15 16
import { IAction } from 'vs/base/common/actions';
import { ActionBar } from 'vs/base/browser/ui/actionbar/actionbar';
import { IContextViewProvider, AnchorAlignment } from 'vs/base/browser/ui/contextview/contextview';
M
Matt Bierner 已提交
17
import { Event, Emitter } from 'vs/base/common/event';
J
Johannes Rieken 已提交
18
import { Widget } from 'vs/base/browser/ui/widget';
B
Benjamin Pasero 已提交
19
import { Color } from 'vs/base/common/color';
20
import { mixin } from 'vs/base/common/objects';
21
import { HistoryNavigator } from 'vs/base/common/history';
22
import { IHistoryNavigationWidget } from 'vs/base/browser/history';
J
Joao Moreno 已提交
23 24 25
import { ScrollableElement } from 'vs/base/browser/ui/scrollbar/scrollableElement';
import { ScrollbarVisibility } from 'vs/base/common/scrollable';
import { domEvent } from 'vs/base/browser/event';
A
Alex Dima 已提交
26

J
Joao Moreno 已提交
27
const $ = dom.$;
E
Erich Gamma 已提交
28

B
Benjamin Pasero 已提交
29
export interface IInputOptions extends IInputBoxStyles {
M
Matt Bierner 已提交
30 31 32 33 34
	readonly placeholder?: string;
	readonly ariaLabel?: string;
	readonly type?: string;
	readonly validationOptions?: IInputValidationOptions;
	readonly flexibleHeight?: boolean;
35
	readonly flexibleWidth?: boolean;
J
Joao Moreno 已提交
36
	readonly flexibleMaxHeight?: number;
37
	readonly actions?: ReadonlyArray<IAction>;
E
Erich Gamma 已提交
38 39
}

40
export interface IInputBoxStyles {
41 42 43 44 45 46 47 48 49 50 51 52
	readonly inputBackground?: Color;
	readonly inputForeground?: Color;
	readonly inputBorder?: Color;
	readonly inputValidationInfoBorder?: Color;
	readonly inputValidationInfoBackground?: Color;
	readonly inputValidationInfoForeground?: Color;
	readonly inputValidationWarningBorder?: Color;
	readonly inputValidationWarningBackground?: Color;
	readonly inputValidationWarningForeground?: Color;
	readonly inputValidationErrorBorder?: Color;
	readonly inputValidationErrorBackground?: Color;
	readonly inputValidationErrorForeground?: Color;
53 54
}

E
Erich Gamma 已提交
55
export interface IInputValidator {
M
Matt Bierner 已提交
56
	(value: string): IMessage | null;
E
Erich Gamma 已提交
57 58 59
}

export interface IMessage {
M
Matt Bierner 已提交
60 61 62
	readonly content: string;
	readonly formatContent?: boolean; // defaults to false
	readonly type?: MessageType;
E
Erich Gamma 已提交
63 64 65
}

export interface IInputValidationOptions {
M
Matt Bierner 已提交
66
	validation?: IInputValidator;
E
Erich Gamma 已提交
67 68
}

69
export const enum MessageType {
E
Erich Gamma 已提交
70 71 72 73 74 75 76 77 78 79
	INFO = 1,
	WARNING = 2,
	ERROR = 3
}

export interface IRange {
	start: number;
	end: number;
}

80 81
const defaultOpts = {
	inputBackground: Color.fromHex('#3C3C3C'),
82
	inputForeground: Color.fromHex('#CCCCCC'),
B
Benjamin Pasero 已提交
83 84 85 86 87 88
	inputValidationInfoBorder: Color.fromHex('#55AAFF'),
	inputValidationInfoBackground: Color.fromHex('#063B49'),
	inputValidationWarningBorder: Color.fromHex('#B89500'),
	inputValidationWarningBackground: Color.fromHex('#352A05'),
	inputValidationErrorBorder: Color.fromHex('#BE1100'),
	inputValidationErrorBackground: Color.fromHex('#5A1D1D')
89 90
};

A
Alex Dima 已提交
91
export class InputBox extends Widget {
M
Matt Bierner 已提交
92
	private contextViewProvider?: IContextViewProvider;
93
	element: HTMLElement;
E
Erich Gamma 已提交
94
	private input: HTMLInputElement;
M
Matt Bierner 已提交
95
	private actionbar?: ActionBar;
E
Erich Gamma 已提交
96
	private options: IInputOptions;
M
Matt Bierner 已提交
97
	private message: IMessage | null;
E
Erich Gamma 已提交
98 99
	private placeholder: string;
	private ariaLabel: string;
M
Matt Bierner 已提交
100
	private validation?: IInputValidator;
J
Joao Moreno 已提交
101
	private state: 'idle' | 'open' | 'closed' = 'idle';
J
Joao Moreno 已提交
102 103 104 105 106 107

	private mirror: HTMLElement | undefined;
	private cachedHeight: number | undefined;
	private cachedContentHeight: number | undefined;
	private maxHeight: number = Number.POSITIVE_INFINITY;
	private scrollableElement: ScrollableElement | undefined;
M
Matt Bierner 已提交
108

109 110 111 112 113 114 115 116 117 118 119 120 121
	private inputBackground?: Color;
	private inputForeground?: Color;
	private inputBorder?: Color;

	private inputValidationInfoBorder?: Color;
	private inputValidationInfoBackground?: Color;
	private inputValidationInfoForeground?: Color;
	private inputValidationWarningBorder?: Color;
	private inputValidationWarningBackground?: Color;
	private inputValidationWarningForeground?: Color;
	private inputValidationErrorBorder?: Color;
	private inputValidationErrorBackground?: Color;
	private inputValidationErrorForeground?: Color;
122

A
Alex Dima 已提交
123
	private _onDidChange = this._register(new Emitter<string>());
124
	public readonly onDidChange: Event<string> = this._onDidChange.event;
A
Alex Dima 已提交
125 126

	private _onDidHeightChange = this._register(new Emitter<number>());
127
	public readonly onDidHeightChange: Event<number> = this._onDidHeightChange.event;
A
Alex Dima 已提交
128

M
Matt Bierner 已提交
129
	constructor(container: HTMLElement, contextViewProvider: IContextViewProvider | undefined, options?: IInputOptions) {
E
Erich Gamma 已提交
130 131 132 133
		super();

		this.contextViewProvider = contextViewProvider;
		this.options = options || Object.create(null);
134
		mixin(this.options, defaultOpts, false);
E
Erich Gamma 已提交
135 136 137
		this.message = null;
		this.placeholder = this.options.placeholder || '';
		this.ariaLabel = this.options.ariaLabel || '';
138

139 140
		this.inputBackground = this.options.inputBackground;
		this.inputForeground = this.options.inputForeground;
B
Benjamin Pasero 已提交
141
		this.inputBorder = this.options.inputBorder;
E
Erich Gamma 已提交
142

B
Benjamin Pasero 已提交
143 144
		this.inputValidationInfoBorder = this.options.inputValidationInfoBorder;
		this.inputValidationInfoBackground = this.options.inputValidationInfoBackground;
145
		this.inputValidationInfoForeground = this.options.inputValidationInfoForeground;
B
Benjamin Pasero 已提交
146 147
		this.inputValidationWarningBorder = this.options.inputValidationWarningBorder;
		this.inputValidationWarningBackground = this.options.inputValidationWarningBackground;
148
		this.inputValidationWarningForeground = this.options.inputValidationWarningForeground;
B
Benjamin Pasero 已提交
149 150
		this.inputValidationErrorBorder = this.options.inputValidationErrorBorder;
		this.inputValidationErrorBackground = this.options.inputValidationErrorBackground;
151
		this.inputValidationErrorForeground = this.options.inputValidationErrorForeground;
152

E
Erich Gamma 已提交
153 154 155 156 157 158
		if (this.options.validationOptions) {
			this.validation = this.options.validationOptions.validation;
		}

		this.element = dom.append(container, $('.monaco-inputbox.idle'));

A
Alex Dima 已提交
159
		let tagName = this.options.flexibleHeight ? 'textarea' : 'input';
E
Erich Gamma 已提交
160

A
Alex Dima 已提交
161
		let wrapper = dom.append(this.element, $('.wrapper'));
J
Joao Moreno 已提交
162
		this.input = dom.append(wrapper, $(tagName + '.input.empty'));
E
Erich Gamma 已提交
163 164 165
		this.input.setAttribute('autocorrect', 'off');
		this.input.setAttribute('autocapitalize', 'off');
		this.input.setAttribute('spellcheck', 'false');
166

167 168
		this.onfocus(this.input, () => dom.addClass(this.element, 'synthetic-focus'));
		this.onblur(this.input, () => dom.removeClass(this.element, 'synthetic-focus'));
E
Erich Gamma 已提交
169 170

		if (this.options.flexibleHeight) {
J
Joao Moreno 已提交
171 172
			this.maxHeight = typeof this.options.flexibleMaxHeight === 'number' ? this.options.flexibleMaxHeight : Number.POSITIVE_INFINITY;

E
Erich Gamma 已提交
173
			this.mirror = dom.append(wrapper, $('div.mirror'));
J
Joao Moreno 已提交
174
			this.mirror.innerHTML = '&nbsp;';
J
Joao Moreno 已提交
175 176

			this.scrollableElement = new ScrollableElement(this.element, { vertical: ScrollbarVisibility.Auto });
177 178 179 180

			if (this.options.flexibleWidth) {
				this.input.setAttribute('wrap', 'off');
				this.mirror.style.whiteSpace = 'pre';
P
Peng Lyu 已提交
181
				this.mirror.style.wordWrap = 'initial';
182 183
			}

J
Joao Moreno 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197
			dom.append(container, this.scrollableElement.getDomNode());
			this._register(this.scrollableElement);

			// from ScrollableElement to DOM
			this._register(this.scrollableElement.onScroll(e => this.input.scrollTop = e.scrollTop));

			const onSelectionChange = Event.filter(domEvent(document, 'selectionchange'), () => {
				const selection = document.getSelection();
				return !!selection && selection.anchorNode === wrapper;
			});

			// from DOM to ScrollableElement
			this._register(onSelectionChange(this.updateScrollDimensions, this));
			this._register(this.onDidHeightChange(this.updateScrollDimensions, this));
E
Erich Gamma 已提交
198 199 200 201 202 203 204 205 206 207
		} else {
			this.input.type = this.options.type || 'text';
			this.input.setAttribute('wrap', 'off');
		}

		if (this.ariaLabel) {
			this.input.setAttribute('aria-label', this.ariaLabel);
		}

		if (this.placeholder) {
208
			this.setPlaceHolder(this.placeholder);
E
Erich Gamma 已提交
209 210
		}

A
Alex Dima 已提交
211 212 213
		this.oninput(this.input, () => this.onValueChange());
		this.onblur(this.input, () => this.onBlur());
		this.onfocus(this.input, () => this.onFocus());
E
Erich Gamma 已提交
214 215

		// Add placeholder shim for IE because IE decides to hide the placeholder on focus (we dont want that!)
A
Alex Dima 已提交
216
		if (this.placeholder && Bal.isIE) {
A
Alex Dima 已提交
217
			this.onclick(this.input, (e) => {
E
Erich Gamma 已提交
218 219
				dom.EventHelper.stop(e, true);
				this.input.focus();
A
Alex Dima 已提交
220
			});
E
Erich Gamma 已提交
221 222
		}

J
Joao Moreno 已提交
223
		setTimeout(() => this.updateMirror(), 0);
E
Erich Gamma 已提交
224 225 226

		// Support actions
		if (this.options.actions) {
A
Alex Dima 已提交
227
			this.actionbar = this._register(new ActionBar(this.element));
E
Erich Gamma 已提交
228 229
			this.actionbar.push(this.options.actions, { icon: true, label: false });
		}
B
Benjamin Pasero 已提交
230

231
		this.applyStyles();
E
Erich Gamma 已提交
232 233 234 235 236 237 238 239 240 241
	}

	private onBlur(): void {
		this._hideMessage();
	}

	private onFocus(): void {
		this._showMessage();
	}

242
	public setPlaceHolder(placeHolder: string): void {
J
Joao Moreno 已提交
243
		this.placeholder = placeHolder;
J
Joao Moreno 已提交
244 245
		this.input.setAttribute('placeholder', placeHolder);
		this.input.title = placeHolder;
E
Erich Gamma 已提交
246 247
	}

248 249 250
	public setAriaLabel(label: string): void {
		this.ariaLabel = label;

J
Joao Moreno 已提交
251 252 253 254
		if (label) {
			this.input.setAttribute('aria-label', this.ariaLabel);
		} else {
			this.input.removeAttribute('aria-label');
255 256 257
		}
	}

I
isidor 已提交
258
	public get mirrorElement(): HTMLElement | undefined {
259 260 261
		return this.mirror;
	}

E
Erich Gamma 已提交
262 263 264 265
	public get inputElement(): HTMLInputElement {
		return this.input;
	}

266
	public get value(): string {
E
Erich Gamma 已提交
267 268 269
		return this.input.value;
	}

270
	public set value(newValue: string) {
E
Erich Gamma 已提交
271 272 273 274 275 276 277
		if (this.input.value !== newValue) {
			this.input.value = newValue;
			this.onValueChange();
		}
	}

	public get height(): number {
J
Joao Moreno 已提交
278
		return typeof this.cachedHeight === 'number' ? this.cachedHeight : dom.getTotalHeight(this.element);
E
Erich Gamma 已提交
279 280 281 282 283 284 285 286 287 288 289
	}

	public focus(): void {
		this.input.focus();
	}

	public blur(): void {
		this.input.blur();
	}

	public hasFocus(): boolean {
290
		return document.activeElement === this.input;
E
Erich Gamma 已提交
291 292
	}

293
	public select(range: IRange | null = null): void {
E
Erich Gamma 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
		this.input.select();

		if (range) {
			this.input.setSelectionRange(range.start, range.end);
		}
	}

	public enable(): void {
		this.input.removeAttribute('disabled');
	}

	public disable(): void {
		this.input.disabled = true;
		this._hideMessage();
	}

310
	public setEnabled(enabled: boolean): void {
311 312 313 314 315 316 317
		if (enabled) {
			this.enable();
		} else {
			this.disable();
		}
	}

318
	public get width(): number {
E
Erich Gamma 已提交
319 320 321
		return dom.getTotalWidth(this.input);
	}

322
	public set width(width: number) {
323 324 325 326 327 328 329 330 331 332 333 334 335
		if (this.options.flexibleHeight && this.options.flexibleWidth) {
			// textarea with horizontal scrolling
			let horizontalPadding = 0;
			if (this.mirror) {
				const paddingLeft = parseFloat(this.mirror.style.paddingLeft || '') || 0;
				const paddingRight = parseFloat(this.mirror.style.paddingRight || '') || 0;
				horizontalPadding = paddingLeft + paddingRight;
			}
			this.input.style.width = (width - horizontalPadding) + 'px';
		} else {
			this.input.style.width = width + 'px';
		}

336 337 338
		if (this.mirror) {
			this.mirror.style.width = width + 'px';
		}
E
Erich Gamma 已提交
339 340
	}

341 342 343 344 345 346 347 348 349 350 351 352
	public set paddingRight(paddingRight: number) {
		if (this.options.flexibleHeight && this.options.flexibleWidth) {
			this.input.style.width = `calc(100% - ${paddingRight}px)`;
		} else {
			this.input.style.paddingRight = paddingRight + 'px';
		}

		if (this.mirror) {
			this.mirror.style.paddingRight = paddingRight + 'px';
		}
	}

J
Joao Moreno 已提交
353
	private updateScrollDimensions(): void {
354
		if (typeof this.cachedContentHeight !== 'number' || typeof this.cachedHeight !== 'number' || !this.scrollableElement) {
J
Joao Moreno 已提交
355 356 357 358 359 360 361
			return;
		}

		const scrollHeight = this.cachedContentHeight;
		const height = this.cachedHeight;
		const scrollTop = this.input.scrollTop;

362 363
		this.scrollableElement.setScrollDimensions({ scrollHeight, height });
		this.scrollableElement.setScrollPosition({ scrollTop });
364 365
	}

366
	public showMessage(message: IMessage, force?: boolean): void {
E
Erich Gamma 已提交
367 368 369 370 371 372 373 374
		this.message = message;

		dom.removeClass(this.element, 'idle');
		dom.removeClass(this.element, 'info');
		dom.removeClass(this.element, 'warning');
		dom.removeClass(this.element, 'error');
		dom.addClass(this.element, this.classForType(message.type));

375
		const styles = this.stylesForType(this.message.type);
376
		this.element.style.border = styles.border ? `1px solid ${styles.border}` : '';
377

378 379 380 381 382 383 384 385 386 387 388 389
		// ARIA Support
		let alertText: string;
		if (message.type === MessageType.ERROR) {
			alertText = nls.localize('alertErrorMessage', "Error: {0}", message.content);
		} else if (message.type === MessageType.WARNING) {
			alertText = nls.localize('alertWarningMessage', "Warning: {0}", message.content);
		} else {
			alertText = nls.localize('alertInfoMessage', "Info: {0}", message.content);
		}

		aria.alert(alertText);

E
Erich Gamma 已提交
390 391 392 393 394 395 396 397 398 399 400 401 402 403
		if (this.hasFocus() || force) {
			this._showMessage();
		}
	}

	public hideMessage(): void {
		this.message = null;

		dom.removeClass(this.element, 'info');
		dom.removeClass(this.element, 'warning');
		dom.removeClass(this.element, 'error');
		dom.addClass(this.element, 'idle');

		this._hideMessage();
404
		this.applyStyles();
E
Erich Gamma 已提交
405 406 407 408 409 410 411
	}

	public isInputValid(): boolean {
		return !!this.validation && !this.validation(this.value);
	}

	public validate(): boolean {
412
		let errorMsg: IMessage | null = null;
E
Erich Gamma 已提交
413 414

		if (this.validation) {
T
Till Salinger 已提交
415
			errorMsg = this.validation(this.value);
E
Erich Gamma 已提交
416

T
Till Salinger 已提交
417 418 419 420 421
			if (errorMsg) {
				this.inputElement.setAttribute('aria-invalid', 'true');
				this.showMessage(errorMsg);
			}
			else if (this.inputElement.hasAttribute('aria-invalid')) {
422
				this.inputElement.removeAttribute('aria-invalid');
E
Erich Gamma 已提交
423 424 425 426
				this.hideMessage();
			}
		}

T
Till Salinger 已提交
427
		return !errorMsg;
E
Erich Gamma 已提交
428 429
	}

430
	public stylesForType(type: MessageType | undefined): { border: Color | undefined; background: Color | undefined; foreground: Color | undefined } {
431
		switch (type) {
432 433 434
			case MessageType.INFO: return { border: this.inputValidationInfoBorder, background: this.inputValidationInfoBackground, foreground: this.inputValidationInfoForeground };
			case MessageType.WARNING: return { border: this.inputValidationWarningBorder, background: this.inputValidationWarningBackground, foreground: this.inputValidationWarningForeground };
			default: return { border: this.inputValidationErrorBorder, background: this.inputValidationErrorBackground, foreground: this.inputValidationErrorForeground };
435 436 437
		}
	}

M
Matt Bierner 已提交
438
	private classForType(type: MessageType | undefined): string {
E
Erich Gamma 已提交
439 440 441 442 443 444 445 446 447 448 449 450
		switch (type) {
			case MessageType.INFO: return 'info';
			case MessageType.WARNING: return 'warning';
			default: return 'error';
		}
	}

	private _showMessage(): void {
		if (!this.contextViewProvider || !this.message) {
			return;
		}

A
Alex Dima 已提交
451 452
		let div: HTMLElement;
		let layout = () => div.style.width = dom.getTotalWidth(this.element) + 'px';
E
Erich Gamma 已提交
453 454 455

		this.contextViewProvider.showContextView({
			getAnchor: () => this.element,
A
Alex Dima 已提交
456
			anchorAlignment: AnchorAlignment.RIGHT,
E
Erich Gamma 已提交
457
			render: (container: HTMLElement) => {
M
Matt Bierner 已提交
458 459 460 461
				if (!this.message) {
					return null;
				}

E
Erich Gamma 已提交
462 463 464
				div = dom.append(container, $('.monaco-inputbox-container'));
				layout();

465
				const renderOptions: MarkdownRenderOptions = {
466
					inline: true,
467
					className: 'monaco-inputbox-message'
E
Erich Gamma 已提交
468 469
				};

470
				const spanElement = (this.message.formatContent
471
					? renderFormattedText(this.message.content, renderOptions)
472
					: renderText(this.message.content, renderOptions));
E
Erich Gamma 已提交
473
				dom.addClass(spanElement, this.classForType(this.message.type));
474 475

				const styles = this.stylesForType(this.message.type);
476
				spanElement.style.backgroundColor = styles.background ? styles.background.toString() : '';
477
				spanElement.style.color = styles.foreground ? styles.foreground.toString() : null;
478
				spanElement.style.border = styles.border ? `1px solid ${styles.border}` : '';
479

E
Erich Gamma 已提交
480
				dom.append(div, spanElement);
481

E
Erich Gamma 已提交
482 483
				return null;
			},
S
SteVen Batten 已提交
484
			onHide: () => {
S
SteVen Batten 已提交
485
				this.state = 'closed';
S
SteVen Batten 已提交
486
			},
E
Erich Gamma 已提交
487 488
			layout: layout
		});
S
SteVen Batten 已提交
489 490

		this.state = 'open';
E
Erich Gamma 已提交
491 492 493
	}

	private _hideMessage(): void {
S
SteVen Batten 已提交
494
		if (!this.contextViewProvider) {
E
Erich Gamma 已提交
495 496 497
			return;
		}

S
SteVen Batten 已提交
498 499 500
		if (this.state === 'open') {
			this.contextViewProvider.hideContextView();
		}
E
Erich Gamma 已提交
501

S
SteVen Batten 已提交
502
		this.state = 'idle';
E
Erich Gamma 已提交
503 504 505
	}

	private onValueChange(): void {
A
Alex Dima 已提交
506
		this._onDidChange.fire(this.value);
E
Erich Gamma 已提交
507 508

		this.validate();
J
Joao Moreno 已提交
509
		this.updateMirror();
J
Joao Moreno 已提交
510
		dom.toggleClass(this.input, 'empty', !this.value);
J
Joao Moreno 已提交
511

M
Matt Bierner 已提交
512
		if (this.state === 'open' && this.contextViewProvider) {
J
Joao Moreno 已提交
513 514
			this.contextViewProvider.layout();
		}
E
Erich Gamma 已提交
515 516
	}

J
Joao Moreno 已提交
517 518 519 520 521
	private updateMirror(): void {
		if (!this.mirror) {
			return;
		}

J
Joao Moreno 已提交
522
		const value = this.value;
J
Joao Moreno 已提交
523 524 525 526 527 528 529 530 531 532
		const lastCharCode = value.charCodeAt(value.length - 1);
		const suffix = lastCharCode === 10 ? ' ' : '';
		const mirrorTextContent = value + suffix;

		if (mirrorTextContent) {
			this.mirror.textContent = value + suffix;
		} else {
			this.mirror.innerHTML = '&nbsp;';
		}

J
Joao Moreno 已提交
533 534 535
		this.layout();
	}

536
	public style(styles: IInputBoxStyles): void {
537 538
		this.inputBackground = styles.inputBackground;
		this.inputForeground = styles.inputForeground;
B
Benjamin Pasero 已提交
539
		this.inputBorder = styles.inputBorder;
B
Benjamin Pasero 已提交
540

B
Benjamin Pasero 已提交
541
		this.inputValidationInfoBackground = styles.inputValidationInfoBackground;
542
		this.inputValidationInfoForeground = styles.inputValidationInfoForeground;
B
Benjamin Pasero 已提交
543 544
		this.inputValidationInfoBorder = styles.inputValidationInfoBorder;
		this.inputValidationWarningBackground = styles.inputValidationWarningBackground;
545
		this.inputValidationWarningForeground = styles.inputValidationWarningForeground;
B
Benjamin Pasero 已提交
546 547
		this.inputValidationWarningBorder = styles.inputValidationWarningBorder;
		this.inputValidationErrorBackground = styles.inputValidationErrorBackground;
548
		this.inputValidationErrorForeground = styles.inputValidationErrorForeground;
B
Benjamin Pasero 已提交
549
		this.inputValidationErrorBorder = styles.inputValidationErrorBorder;
550

551
		this.applyStyles();
B
Benjamin Pasero 已提交
552 553
	}

554
	protected applyStyles(): void {
555 556 557
		const background = this.inputBackground ? this.inputBackground.toString() : '';
		const foreground = this.inputForeground ? this.inputForeground.toString() : '';
		const border = this.inputBorder ? this.inputBorder.toString() : '';
J
Joao Moreno 已提交
558 559 560 561 562 563

		this.element.style.backgroundColor = background;
		this.element.style.color = foreground;
		this.input.style.backgroundColor = background;
		this.input.style.color = foreground;

564 565
		this.element.style.borderWidth = border ? '1px' : '';
		this.element.style.borderStyle = border ? 'solid' : '';
J
Joao Moreno 已提交
566
		this.element.style.borderColor = border;
B
Benjamin Pasero 已提交
567 568
	}

569
	public layout(): void {
E
Erich Gamma 已提交
570 571 572 573
		if (!this.mirror) {
			return;
		}

J
Joao Moreno 已提交
574 575
		const previousHeight = this.cachedContentHeight;
		this.cachedContentHeight = dom.getTotalHeight(this.mirror);
E
Erich Gamma 已提交
576

J
Joao Moreno 已提交
577 578
		if (previousHeight !== this.cachedContentHeight) {
			this.cachedHeight = Math.min(this.cachedContentHeight, this.maxHeight);
579
			this.input.style.height = this.cachedHeight + 'px';
J
Joao Moreno 已提交
580
			this._onDidHeightChange.fire(this.cachedContentHeight);
581
		}
E
Erich Gamma 已提交
582 583
	}

584 585 586 587 588 589 590 591 592 593 594 595 596
	public insertAtCursor(text: string): void {
		const inputElement = this.inputElement;
		const start = inputElement.selectionStart;
		const end = inputElement.selectionEnd;
		const content = inputElement.value;

		if (start !== null && end !== null) {
			this.value = content.substr(0, start) + text + content.substr(end);
			inputElement.setSelectionRange(start + 1, start + 1);
			this.layout();
		}
	}

E
Erich Gamma 已提交
597 598 599 600
	public dispose(): void {
		this._hideMessage();

		this.message = null;
J
Joao Moreno 已提交
601 602 603 604

		if (this.actionbar) {
			this.actionbar.dispose();
		}
E
Erich Gamma 已提交
605 606 607

		super.dispose();
	}
608 609 610 611 612 613
}

export interface IHistoryInputOptions extends IInputOptions {
	history: string[];
}

614
export class HistoryInputBox extends InputBox implements IHistoryNavigationWidget {
615 616 617

	private readonly history: HistoryNavigator<string>;

M
Matt Bierner 已提交
618
	constructor(container: HTMLElement, contextViewProvider: IContextViewProvider | undefined, options: IHistoryInputOptions) {
619 620 621 622
		super(container, contextViewProvider, options);
		this.history = new HistoryNavigator<string>(options.history, 100);
	}

S
Sandeep Somavarapu 已提交
623 624 625
	public addToHistory(): void {
		if (this.value && this.value !== this.getCurrentValue()) {
			this.history.add(this.value);
626 627 628 629 630 631 632
		}
	}

	public getHistory(): string[] {
		return this.history.getHistory();
	}

633
	public showNextValue(): void {
S
Sandeep Somavarapu 已提交
634 635 636 637
		if (!this.history.has(this.value)) {
			this.addToHistory();
		}

S
Sandeep Somavarapu 已提交
638 639 640 641 642
		let next = this.getNextValue();
		if (next) {
			next = next === this.value ? this.getNextValue() : next;
		}

643 644
		if (next) {
			this.value = next;
645
			aria.status(this.value);
646 647 648
		}
	}

649
	public showPreviousValue(): void {
S
Sandeep Somavarapu 已提交
650 651
		if (!this.history.has(this.value)) {
			this.addToHistory();
652
		}
S
Sandeep Somavarapu 已提交
653 654 655 656 657 658

		let previous = this.getPreviousValue();
		if (previous) {
			previous = previous === this.value ? this.getPreviousValue() : previous;
		}

659 660
		if (previous) {
			this.value = previous;
661
			aria.status(this.value);
662 663 664 665 666 667
		}
	}

	public clearHistory(): void {
		this.history.clear();
	}
S
Sandeep Somavarapu 已提交
668

M
Matt Bierner 已提交
669
	private getCurrentValue(): string | null {
S
Sandeep Somavarapu 已提交
670 671 672 673 674 675 676 677
		let currentValue = this.history.current();
		if (!currentValue) {
			currentValue = this.history.last();
			this.history.next();
		}
		return currentValue;
	}

M
Matt Bierner 已提交
678
	private getPreviousValue(): string | null {
S
Sandeep Somavarapu 已提交
679 680 681
		return this.history.previous() || this.history.first();
	}

M
Matt Bierner 已提交
682
	private getNextValue(): string | null {
S
Sandeep Somavarapu 已提交
683 684
		return this.history.next() || this.history.last();
	}
685
}