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

6
import * as nls from 'vs/nls';
7
import * as dom from 'vs/base/browser/dom';
8
import { Widget } from 'vs/base/browser/ui/widget';
9 10
import { Checkbox } from 'vs/base/browser/ui/checkbox/checkbox';
import { IContextViewProvider } from 'vs/base/browser/ui/contextview/contextview';
11
import { IInputValidator, HistoryInputBox } from 'vs/base/browser/ui/inputbox/inputBox';
12 13
import { IKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { KeyCode } from 'vs/base/common/keyCodes';
M
Matt Bierner 已提交
14
import { Event as CommonEvent, Emitter } from 'vs/base/common/event';
B
Benjamin Pasero 已提交
15
import { IThemeService } from 'vs/platform/theme/common/themeService';
B
Benjamin Pasero 已提交
16
import { attachInputBoxStyler, attachCheckboxStyler } from 'vs/platform/theme/common/styler';
17
import { ContextScopedHistoryInputBox } from 'vs/platform/browser/contextScopedHistoryWidget';
S
#50583  
Sandeep Somavarapu 已提交
18
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
19 20 21 22 23 24

export interface IOptions {
	placeholder?: string;
	width?: number;
	validation?: IInputValidator;
	ariaLabel?: string;
25
	history?: string[];
26 27
}

28
export class PatternInputWidget extends Widget {
29 30 31

	static OPTION_CHANGE: string = 'optionChange';

32
	inputFocusTracker!: dom.IFocusTracker;
33

34 35 36 37
	private width: number;
	private placeholder: string;
	private ariaLabel: string;

38 39
	private domNode!: HTMLElement;
	protected inputBox!: HistoryInputBox;
40

41
	private _onSubmit = this._register(new Emitter<boolean>());
R
Rob Lourens 已提交
42
	onSubmit: CommonEvent<boolean> = this._onSubmit.event;
43

44
	private _onCancel = this._register(new Emitter<boolean>());
R
Rob Lourens 已提交
45
	onCancel: CommonEvent<boolean> = this._onCancel.event;
46

47
	constructor(parent: HTMLElement, private contextViewProvider: IContextViewProvider, options: IOptions = Object.create(null),
S
#50583  
Sandeep Somavarapu 已提交
48
		@IThemeService protected themeService: IThemeService,
49
		@IContextKeyService private readonly contextKeyService: IContextKeyService
50
	) {
51
		super();
52 53 54 55
		this.width = options.width || 100;
		this.placeholder = options.placeholder || '';
		this.ariaLabel = options.ariaLabel || nls.localize('defaultLabel', "input");

56
		this.render(options);
57

58
		parent.appendChild(this.domNode);
59 60
	}

R
Rob Lourens 已提交
61
	dispose(): void {
62
		super.dispose();
63 64 65
		if (this.inputFocusTracker) {
			this.inputFocusTracker.dispose();
		}
66 67
	}

R
Rob Lourens 已提交
68
	setWidth(newWidth: number): void {
69 70 71 72 73 74
		this.width = newWidth;
		this.domNode.style.width = this.width + 'px';
		this.contextViewProvider.layout();
		this.setInputWidth();
	}

R
Rob Lourens 已提交
75
	getValue(): string {
76 77 78
		return this.inputBox.value;
	}

R
Rob Lourens 已提交
79
	setValue(value: string): void {
80 81 82 83 84
		if (this.inputBox.value !== value) {
			this.inputBox.value = value;
		}
	}

85

R
Rob Lourens 已提交
86
	select(): void {
87 88 89
		this.inputBox.select();
	}

R
Rob Lourens 已提交
90
	focus(): void {
91 92 93
		this.inputBox.focus();
	}

R
Rob Lourens 已提交
94
	inputHasFocus(): boolean {
95 96 97
		return this.inputBox.hasFocus();
	}

98
	private setInputWidth(): void {
99
		this.inputBox.width = this.width - this.getSubcontrolsWidth() - 2; // 2 for input box border
100 101 102
	}

	protected getSubcontrolsWidth(): number {
R
Rob Lourens 已提交
103
		return 0;
104 105
	}

R
Rob Lourens 已提交
106
	getHistory(): string[] {
107
		return this.inputBox.getHistory();
108 109
	}

R
Rob Lourens 已提交
110
	clearHistory(): void {
111
		this.inputBox.clearHistory();
112 113
	}

R
Rob Lourens 已提交
114
	onSearchSubmit(): void {
S
Sandeep Somavarapu 已提交
115
		this.inputBox.addToHistory();
116 117
	}

R
Rob Lourens 已提交
118
	showNextTerm() {
119
		this.inputBox.showNextValue();
120 121
	}

R
Rob Lourens 已提交
122
	showPreviousTerm() {
123
		this.inputBox.showPreviousValue();
124 125
	}

126
	private render(options: IOptions): void {
127 128
		this.domNode = document.createElement('div');
		this.domNode.style.width = this.width + 'px';
129
		dom.addClass(this.domNode, 'monaco-findInput');
130

S
#50583  
Sandeep Somavarapu 已提交
131
		this.inputBox = new ContextScopedHistoryInputBox(this.domNode, this.contextViewProvider, {
132 133 134
			placeholder: this.placeholder || '',
			ariaLabel: this.ariaLabel || '',
			validationOptions: {
135
				validation: undefined
136 137
			},
			history: options.history || []
S
#50583  
Sandeep Somavarapu 已提交
138
		}, this.contextKeyService);
B
Benjamin Pasero 已提交
139
		this._register(attachInputBoxStyler(this.inputBox, this.themeService));
140
		this.inputFocusTracker = dom.trackFocus(this.inputBox.inputElement);
141 142
		this.onkeyup(this.inputBox.inputElement, (keyboardEvent) => this.onInputKeyUp(keyboardEvent));

143
		const controls = document.createElement('div');
144
		controls.className = 'controls';
145
		this.renderSubcontrols(controls);
146 147

		this.domNode.appendChild(controls);
148 149 150
		this.setInputWidth();
	}

151
	protected renderSubcontrols(_controlsDiv: HTMLDivElement): void {
152
	}
153 154 155 156

	private onInputKeyUp(keyboardEvent: IKeyboardEvent) {
		switch (keyboardEvent.keyCode) {
			case KeyCode.Enter:
157
				this._onSubmit.fire(false);
158
				return;
159
			case KeyCode.Escape:
160
				this._onCancel.fire(false);
161
				return;
162 163 164 165
			default:
				return;
		}
	}
166 167 168 169
}

export class ExcludePatternInputWidget extends PatternInputWidget {

170
	constructor(parent: HTMLElement, contextViewProvider: IContextViewProvider, options: IOptions = Object.create(null),
S
#50583  
Sandeep Somavarapu 已提交
171 172
		@IThemeService themeService: IThemeService,
		@IContextKeyService contextKeyService: IContextKeyService
173
	) {
S
#50583  
Sandeep Somavarapu 已提交
174
		super(parent, contextViewProvider, options, themeService, contextKeyService);
175 176
	}

177
	private useExcludesAndIgnoreFilesBox!: Checkbox;
178

R
Rob Lourens 已提交
179
	dispose(): void {
180
		super.dispose();
181
		this.useExcludesAndIgnoreFilesBox.dispose();
182 183
	}

R
Rob Lourens 已提交
184
	useExcludesAndIgnoreFiles(): boolean {
185
		return this.useExcludesAndIgnoreFilesBox.checked;
186 187
	}

R
Rob Lourens 已提交
188
	setUseExcludesAndIgnoreFiles(value: boolean) {
189
		this.useExcludesAndIgnoreFilesBox.checked = value;
190 191 192
	}

	protected getSubcontrolsWidth(): number {
193
		return super.getSubcontrolsWidth() + this.useExcludesAndIgnoreFilesBox.width();
194 195 196
	}

	protected renderSubcontrols(controlsDiv: HTMLDivElement): void {
197
		this.useExcludesAndIgnoreFilesBox = this._register(new Checkbox({
198 199 200
			actionClassName: 'useExcludesAndIgnoreFiles',
			title: nls.localize('useExcludesAndIgnoreFilesDescription', "Use Exclude Settings and Ignore Files"),
			isChecked: true,
201 202 203 204
		}));
		this._register(this.useExcludesAndIgnoreFilesBox.onChange(viaKeyboard => {
			if (!viaKeyboard) {
				this.inputBox.focus();
205
			}
206
		}));
207
		this._register(attachCheckboxStyler(this.useExcludesAndIgnoreFilesBox, this.themeService));
208

209
		controlsDiv.appendChild(this.useExcludesAndIgnoreFilesBox.domNode);
210 211
		super.renderSubcontrols(controlsDiv);
	}
212
}