simpleFindWidget.ts 10.8 KB
Newer Older
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  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!./simpleFindWidget';
import * as nls from 'vs/nls';
A
Alex Dima 已提交
8
import * as dom from 'vs/base/browser/dom';
9
import { FindInput, IFindInputStyles } from 'vs/base/browser/ui/findinput/findInput';
10
import { Widget } from 'vs/base/browser/ui/widget';
11
import { Delayer } from 'vs/base/common/async';
12
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
A
Alex Dima 已提交
13
import { FindReplaceState } from 'vs/editor/contrib/find/findState';
M
mgquan@myseneca.ca 已提交
14
import { IMessage as InputBoxMessage } from 'vs/base/browser/ui/inputbox/inputBox';
A
Alex Dima 已提交
15 16
import { SimpleButton } from 'vs/editor/contrib/find/findWidget';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
17
import { IContextViewService } from 'vs/platform/contextview/browser/contextView';
18
import { editorWidgetBackground, inputActiveOptionBorder, inputActiveOptionBackground, inputBackground, inputBorder, inputForeground, inputValidationErrorBackground, inputValidationErrorBorder, inputValidationErrorForeground, inputValidationInfoBackground, inputValidationInfoBorder, inputValidationInfoForeground, inputValidationWarningBackground, inputValidationWarningBorder, inputValidationWarningForeground, widgetShadow } from 'vs/platform/theme/common/colorRegistry';
A
Alex Dima 已提交
19
import { ITheme, registerThemingParticipant } from 'vs/platform/theme/common/themeService';
20
import { ContextScopedFindInput } from 'vs/platform/browser/contextScopedHistoryWidget';
21 22 23 24 25 26 27 28

const NLS_FIND_INPUT_LABEL = nls.localize('label.find', "Find");
const NLS_FIND_INPUT_PLACEHOLDER = nls.localize('placeholder.find', "Find");
const NLS_PREVIOUS_MATCH_BTN_LABEL = nls.localize('label.previousMatchButton', "Previous match");
const NLS_NEXT_MATCH_BTN_LABEL = nls.localize('label.nextMatchButton', "Next match");
const NLS_CLOSE_BTN_LABEL = nls.localize('label.closeButton', "Close");

export abstract class SimpleFindWidget extends Widget {
L
Logan Ramos 已提交
29
	protected readonly _findInput: FindInput;
30 31
	private readonly _domNode: HTMLElement;
	private readonly _innerDomNode: HTMLElement;
32
	private _isVisible: boolean = false;
33 34 35
	private readonly _focusTracker: dom.IFocusTracker;
	private readonly _findInputFocusTracker: dom.IFocusTracker;
	private readonly _updateHistoryDelayer: Delayer<void>;
36 37 38
	private prevBtn: SimpleButton;
	private nextBtn: SimpleButton;
	private foundMatch: boolean;
L
Logan Ramos 已提交
39
	private readonly _invertDefaultDirection: boolean | undefined;
40

41
	constructor(
S
#50583  
Sandeep Somavarapu 已提交
42
		@IContextViewService private readonly _contextViewService: IContextViewService,
43 44
		@IContextKeyService contextKeyService: IContextKeyService,
		private readonly _state: FindReplaceState = new FindReplaceState(),
L
Logan Ramos 已提交
45 46
		showOptionButtons?: boolean,
		invertDefaultDirection?: boolean
47 48
	) {
		super();
M
Matt Bierner 已提交
49

L
Logan Ramos 已提交
50
		this._invertDefaultDirection = invertDefaultDirection;
S
#50583  
Sandeep Somavarapu 已提交
51
		this._findInput = this._register(new ContextScopedFindInput(null, this._contextViewService, {
52 53
			label: NLS_FIND_INPUT_LABEL,
			placeholder: NLS_FIND_INPUT_PLACEHOLDER,
M
mgquan@myseneca.ca 已提交
54
			validation: (value: string): InputBoxMessage | null => {
M
updates  
mgquan@myseneca.ca 已提交
55
				if (value.length === 0 || !this._findInput.getRegex()) {
M
mgquan@myseneca.ca 已提交
56 57 58
					return null;
				}
				try {
M
updates  
mgquan@myseneca.ca 已提交
59
					/* tslint:disable-next-line:no-unused-expression */
M
mgquan@myseneca.ca 已提交
60 61 62
					new RegExp(value);
					return null;
				} catch (e) {
63
					this.foundMatch = false;
D
Daniel Imms 已提交
64
					this._updateButtons();
M
mgquan@myseneca.ca 已提交
65 66 67
					return { content: e.message };
				}
			}
68
		}, contextKeyService, showOptionButtons));
69

70 71 72
		// Find History with update delayer
		this._updateHistoryDelayer = new Delayer<void>(500);

73
		this.oninput(this._findInput.domNode, (e) => {
74
			this.foundMatch = this.onInputChanged();
D
Daniel Imms 已提交
75
			this._updateButtons();
76
			this._delayedUpdateHistory();
77 78
		});

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
		this._findInput.setRegex(!!this._state.isRegex);
		this._findInput.setCaseSensitive(!!this._state.matchCase);
		this._findInput.setWholeWords(!!this._state.wholeWord);

		this._register(this._findInput.onDidOptionChange(() => {
			this._state.change({
				isRegex: this._findInput.getRegex(),
				wholeWord: this._findInput.getWholeWords(),
				matchCase: this._findInput.getCaseSensitive()
			}, true);
		}));

		this._register(this._state.onFindReplaceStateChange(() => {
			this._findInput.setRegex(this._state.isRegex);
			this._findInput.setWholeWords(this._state.wholeWord);
			this._findInput.setCaseSensitive(this._state.matchCase);
		}));

97 98
		this._register(this._findInput.onKeyDown((e) => {
			if (e.equals(KeyCode.Enter)) {
L
Logan Ramos 已提交
99 100
				// Flip the direction search goes in the terminal case so it matches other terminals
				this.find(this._invertDefaultDirection ? true : false);
101 102 103 104 105
				e.preventDefault();
				return;
			}

			if (e.equals(KeyMod.Shift | KeyCode.Enter)) {
L
Logan Ramos 已提交
106
				this.find(this._invertDefaultDirection ? false : true);
107 108 109 110 111
				e.preventDefault();
				return;
			}
		}));

D
Daniel Imms 已提交
112
		this.prevBtn = this._register(new SimpleButton({
113 114 115 116
			label: NLS_PREVIOUS_MATCH_BTN_LABEL,
			className: 'previous',
			onTrigger: () => {
				this.find(true);
M
Matt Bierner 已提交
117
			}
118
		}));
119

D
Daniel Imms 已提交
120
		this.nextBtn = this._register(new SimpleButton({
121 122 123 124
			label: NLS_NEXT_MATCH_BTN_LABEL,
			className: 'next',
			onTrigger: () => {
				this.find(false);
M
Matt Bierner 已提交
125
			}
126
		}));
127

128
		const closeBtn = this._register(new SimpleButton({
129 130 131 132
			label: NLS_CLOSE_BTN_LABEL,
			className: 'close-fw',
			onTrigger: () => {
				this.hide();
M
Matt Bierner 已提交
133
			}
134
		}));
135

136 137 138
		this._innerDomNode = document.createElement('div');
		this._innerDomNode.classList.add('simple-find-part');
		this._innerDomNode.appendChild(this._findInput.domNode);
139 140
		this._innerDomNode.appendChild(this.prevBtn.domNode);
		this._innerDomNode.appendChild(this.nextBtn.domNode);
141 142 143
		this._innerDomNode.appendChild(closeBtn.domNode);

		// _domNode wraps _innerDomNode, ensuring that
144
		this._domNode = document.createElement('div');
145 146
		this._domNode.classList.add('simple-find-part-wrapper');
		this._domNode.appendChild(this._innerDomNode);
147

148
		this.onkeyup(this._innerDomNode, e => {
149 150 151 152 153 154 155
			if (e.equals(KeyCode.Escape)) {
				this.hide();
				e.preventDefault();
				return;
			}
		});

156
		this._focusTracker = this._register(dom.trackFocus(this._innerDomNode));
157 158
		this._register(this._focusTracker.onDidFocus(this.onFocusTrackerFocus.bind(this)));
		this._register(this._focusTracker.onDidBlur(this.onFocusTrackerBlur.bind(this)));
159

160 161 162 163
		this._findInputFocusTracker = this._register(dom.trackFocus(this._findInput.domNode));
		this._register(this._findInputFocusTracker.onDidFocus(this.onFindInputFocusTrackerFocus.bind(this)));
		this._register(this._findInputFocusTracker.onDidBlur(this.onFindInputFocusTrackerBlur.bind(this)));

164
		this._register(dom.addDisposableListener(this._innerDomNode, 'click', (event) => {
165 166 167 168
			event.stopPropagation();
		}));
	}

169
	protected abstract onInputChanged(): boolean;
170 171 172 173 174
	protected abstract find(previous: boolean): void;
	protected abstract onFocusTrackerFocus(): void;
	protected abstract onFocusTrackerBlur(): void;
	protected abstract onFindInputFocusTrackerFocus(): void;
	protected abstract onFindInputFocusTrackerBlur(): void;
C
cleidigh 已提交
175

176 177 178 179
	protected get inputValue() {
		return this._findInput.getValue();
	}

180
	public get focusTracker(): dom.IFocusTracker {
181
		return this._focusTracker;
182 183
	}

M
Matt Bierner 已提交
184
	public updateTheme(theme: ITheme): void {
185
		const inputStyles: IFindInputStyles = {
186
			inputActiveOptionBorder: theme.getColor(inputActiveOptionBorder),
187
			inputActiveOptionBackground: theme.getColor(inputActiveOptionBackground),
188 189 190 191 192 193 194 195 196 197 198 199
			inputBackground: theme.getColor(inputBackground),
			inputForeground: theme.getColor(inputForeground),
			inputBorder: theme.getColor(inputBorder),
			inputValidationInfoBackground: theme.getColor(inputValidationInfoBackground),
			inputValidationInfoForeground: theme.getColor(inputValidationInfoForeground),
			inputValidationInfoBorder: theme.getColor(inputValidationInfoBorder),
			inputValidationWarningBackground: theme.getColor(inputValidationWarningBackground),
			inputValidationWarningForeground: theme.getColor(inputValidationWarningForeground),
			inputValidationWarningBorder: theme.getColor(inputValidationWarningBorder),
			inputValidationErrorBackground: theme.getColor(inputValidationErrorBackground),
			inputValidationErrorForeground: theme.getColor(inputValidationErrorForeground),
			inputValidationErrorBorder: theme.getColor(inputValidationErrorBorder)
200 201 202 203
		};
		this._findInput.style(inputStyles);
	}

M
Matt Bierner 已提交
204
	dispose() {
205 206 207 208 209 210 211
		super.dispose();

		if (this._domNode && this._domNode.parentElement) {
			this._domNode.parentElement.removeChild(this._domNode);
		}
	}

M
Matt Bierner 已提交
212
	public getDomNode() {
213 214 215
		return this._domNode;
	}

216
	public reveal(initialInput?: string): void {
217 218 219 220
		if (initialInput) {
			this._findInput.setValue(initialInput);
		}

221
		if (this._isVisible) {
222 223 224 225 226
			this._findInput.select();
			return;
		}

		this._isVisible = true;
D
Daniel Imms 已提交
227
		this._updateButtons();
228 229

		setTimeout(() => {
230
			dom.addClass(this._innerDomNode, 'visible');
231
			dom.addClass(this._innerDomNode, 'visible-transition');
232
			this._innerDomNode.setAttribute('aria-hidden', 'false');
233
			this._findInput.select();
234 235 236
		}, 0);
	}

237 238 239 240 241 242 243 244 245
	public show(initialInput?: string): void {
		if (initialInput && !this._isVisible) {
			this._findInput.setValue(initialInput);
		}

		this._isVisible = true;

		setTimeout(() => {
			dom.addClass(this._innerDomNode, 'visible');
246
			dom.addClass(this._innerDomNode, 'visible-transition');
247 248 249 250
			this._innerDomNode.setAttribute('aria-hidden', 'false');
		}, 0);
	}

251 252
	public hide(): void {
		if (this._isVisible) {
253
			dom.removeClass(this._innerDomNode, 'visible-transition');
254
			this._innerDomNode.setAttribute('aria-hidden', 'true');
255 256
			// Need to delay toggling visibility until after Transition, then visibility hidden - removes from tabIndex list
			setTimeout(() => {
257
				this._isVisible = false;
D
Daniel Imms 已提交
258
				this._updateButtons();
259 260
				dom.removeClass(this._innerDomNode, 'visible');
			}, 200);
261 262
		}
	}
263 264 265 266 267 268

	protected _delayedUpdateHistory() {
		this._updateHistoryDelayer.trigger(this._updateHistory.bind(this));
	}

	protected _updateHistory() {
S
Sandeep Somavarapu 已提交
269
		this._findInput.inputBox.addToHistory();
270
	}
271 272 273 274 275 276 277 278 279 280 281 282

	protected _getRegexValue(): boolean {
		return this._findInput.getRegex();
	}

	protected _getWholeWordValue(): boolean {
		return this._findInput.getWholeWords();
	}

	protected _getCaseSensitiveValue(): boolean {
		return this._findInput.getCaseSensitive();
	}
283

D
Daniel Imms 已提交
284
	private _updateButtons() {
285 286 287 288
		let hasInput = this.inputValue.length > 0;
		this.prevBtn.setEnabled(this._isVisible && hasInput && this.foundMatch);
		this.nextBtn.setEnabled(this._isVisible && hasInput && this.foundMatch);
	}
289 290 291 292 293 294 295 296 297
}

// theming
registerThemingParticipant((theme, collector) => {
	const findWidgetBGColor = theme.getColor(editorWidgetBackground);
	if (findWidgetBGColor) {
		collector.addRule(`.monaco-workbench .simple-find-part { background-color: ${findWidgetBGColor} !important; }`);
	}

M
Matt Bierner 已提交
298
	const widgetShadowColor = theme.getColor(widgetShadow);
299 300 301
	if (widgetShadowColor) {
		collector.addRule(`.monaco-workbench .simple-find-part { box-shadow: 0 2px 8px ${widgetShadowColor}; }`);
	}
302
});