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

'use strict';

import 'vs/css!./findWidget';
A
Alex Dima 已提交
9 10 11 12 13 14 15 16 17

import * as nls from 'vs/nls';
import * as Errors from 'vs/base/common/errors';
import * as DomUtils from 'vs/base/browser/dom';
import {IContextViewProvider} from 'vs/base/browser/ui/contextview/contextview';
import {InputBox, IMessage as InputBoxMessage} from 'vs/base/browser/ui/inputbox/inputBox';
import {FindInput} from 'vs/base/browser/ui/findinput/findInput';
import * as EditorBrowser from 'vs/editor/browser/editorBrowser';
import * as EditorCommon from 'vs/editor/common/editorCommon';
A
Alex Dima 已提交
18
import {MATCHES_LIMIT, FIND_IDS} from 'vs/editor/contrib/find/common/findModel';
E
Erich Gamma 已提交
19
import {CommonKeybindings} from 'vs/base/common/keyCodes';
20
import {IKeybindingService} from 'vs/platform/keybinding/common/keybindingService';
A
Alex Dima 已提交
21
import {INewFindReplaceState, FindReplaceStateChangedEvent, FindReplaceState} from 'vs/editor/contrib/find/common/findState';
A
Alex Dima 已提交
22
import {Widget} from 'vs/base/browser/ui/widget';
E
Erich Gamma 已提交
23 24 25 26 27 28

export interface IFindController {
	replace(): void;
	replaceAll(): void;
}

29 30 31 32 33 34 35 36 37 38 39
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_TOGGLE_SELECTION_FIND_TITLE = nls.localize('label.toggleSelectionFind', "Find in selection");
const NLS_CLOSE_BTN_LABEL = nls.localize('label.closeButton', "Close");
const NLS_REPLACE_INPUT_LABEL = nls.localize('label.replace', "Replace");
const NLS_REPLACE_INPUT_PLACEHOLDER = nls.localize('placeholder.replace', "Replace");
const NLS_REPLACE_BTN_LABEL = nls.localize('label.replaceButton', "Replace");
const NLS_REPLACE_ALL_BTN_LABEL = nls.localize('label.replaceAllButton', "Replace All");
const NLS_TOGGLE_REPLACE_MODE_BTN_LABEL = nls.localize('label.toggleReplaceButton', "Toggle Replace mode");
A
Alex Dima 已提交
40
const NLS_MATCHES_COUNT_LIMIT_TITLE = nls.localize('title.matchesCountLimit', "Only the first 999 results are highlighted, but all find operations work on the entire text.");
41

A
Alex Dima 已提交
42
export class FindWidget extends Widget implements EditorBrowser.IOverlayWidget {
E
Erich Gamma 已提交
43 44 45 46 47 48

	private static ID = 'editor.contrib.findWidget';
	private static PART_WIDTH = 275;
	private static FIND_INPUT_AREA_WIDTH = FindWidget.PART_WIDTH - 54;
	private static REPLACE_INPUT_AREA_WIDTH = FindWidget.FIND_INPUT_AREA_WIDTH;

A
Alex Dima 已提交
49 50
	private _codeEditor: EditorBrowser.ICodeEditor;
	private _state: FindReplaceState;
E
Erich Gamma 已提交
51
	private _controller: IFindController;
A
Alex Dima 已提交
52
	private _contextViewProvider: IContextViewProvider;
53
	private _keybindingService: IKeybindingService;
E
Erich Gamma 已提交
54

A
Alex Dima 已提交
55 56 57
	private _domNode: HTMLElement;
	private _findInput: FindInput;
	private _replaceInputBox: InputBox;
E
Erich Gamma 已提交
58

A
Alex Dima 已提交
59 60 61
	private _toggleReplaceBtn: SimpleButton;
	private _prevBtn: SimpleButton;
	private _nextBtn: SimpleButton;
62
	private _toggleSelectionFind: SimpleCheckbox;
A
Alex Dima 已提交
63 64 65
	private _closeBtn: SimpleButton;
	private _replaceBtn: SimpleButton;
	private _replaceAllBtn: SimpleButton;
E
Erich Gamma 已提交
66

A
Alex Dima 已提交
67 68
	private _isVisible: boolean;
	private _isReplaceVisible: boolean;
E
Erich Gamma 已提交
69

A
Alex Dima 已提交
70
	private focusTracker: DomUtils.IFocusTracker;
E
Erich Gamma 已提交
71

72
	constructor(
A
Alex Dima 已提交
73 74 75 76 77
		codeEditor: EditorBrowser.ICodeEditor,
		controller: IFindController,
		state: FindReplaceState,
		contextViewProvider: IContextViewProvider,
		keybindingService: IKeybindingService
78
	) {
A
Alex Dima 已提交
79
		super();
E
Erich Gamma 已提交
80 81
		this._codeEditor = codeEditor;
		this._controller = controller;
A
Alex Dima 已提交
82
		this._state = state;
E
Erich Gamma 已提交
83
		this._contextViewProvider = contextViewProvider;
84
		this._keybindingService = keybindingService;
E
Erich Gamma 已提交
85 86 87 88

		this._isVisible = false;
		this._isReplaceVisible = false;

A
Alex Dima 已提交
89
		this._register(this._state.addChangeListener((e) => this._onStateChanged(e)));
E
Erich Gamma 已提交
90 91

		this._buildDomNode();
92
		this._updateButtons();
E
Erich Gamma 已提交
93

A
Alex Dima 已提交
94
		this.focusTracker = this._register(DomUtils.trackFocus(this._findInput.inputBox.inputElement));
A
Alex Dima 已提交
95 96
		this.focusTracker.addFocusListener(() => this._reseedFindScope());

97 98
		this._register(this._codeEditor.addListener2(EditorCommon.EventType.ConfigurationChanged, (e:EditorCommon.IConfigurationChangedEvent) => {
			if (e.readOnly) {
99 100 101 102 103 104 105 106 107 108
				if (this._codeEditor.getConfiguration().readOnly) {
					// Hide replace part if editor becomes read only
					this._state.change({ isReplaceRevealed: false }, false);
				}
				this._updateButtons();
			}
		}));
		this._register(this._codeEditor.addListener2(EditorCommon.EventType.CursorSelectionChanged, () => {
			if (this._isVisible) {
				this._updateToggleSelectionFindButton();
109 110 111
			}
		}));

A
Alex Dima 已提交
112
		this._codeEditor.addOverlayWidget(this);
E
Erich Gamma 已提交
113 114
	}

A
Alex Dima 已提交
115 116 117 118 119 120
	private _reseedFindScope(): void {
		let selection = this._codeEditor.getSelection();
		if (selection.startLineNumber !== selection.endLineNumber) {
			// Reseed find scope
			this._state.change({ searchScope: selection }, true);
		}
E
Erich Gamma 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
	}

	// ----- IOverlayWidget API

	public getId(): string {
		return FindWidget.ID;
	}

	public getDomNode(): HTMLElement {
		return this._domNode;
	}

	public getPosition(): EditorBrowser.IOverlayWidgetPosition {
		if (this._isVisible) {
			return {
				preference: EditorBrowser.OverlayWidgetPositionPreference.TOP_RIGHT_CORNER
			};
		}
		return null;
	}

A
Alex Dima 已提交
142
	// ----- React to state changes
E
Erich Gamma 已提交
143

A
Alex Dima 已提交
144 145 146
	private _onStateChanged(e:FindReplaceStateChangedEvent): void {
		if (e.searchString) {
			this._findInput.setValue(this._state.searchString);
147
			this._updateButtons();
A
Alex Dima 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160
		}
		if (e.replaceString) {
			this._replaceInputBox.value = this._state.replaceString;
		}
		if (e.isRevealed) {
			if (this._state.isRevealed) {
				this._reveal(true);
			} else {
				this._hide(true);
			}
		}
		if (e.isReplaceRevealed) {
			if (this._state.isReplaceRevealed) {
161 162 163 164
				if (!this._codeEditor.getConfiguration().readOnly && !this._isReplaceVisible) {
					this._isReplaceVisible = true;
					this._updateButtons();
				}
A
Alex Dima 已提交
165
			} else {
166 167 168 169
				if (this._isReplaceVisible) {
					this._isReplaceVisible = false;
					this._updateButtons();
				}
A
Alex Dima 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182
			}
		}
		if (e.isRegex) {
			this._findInput.setRegex(this._state.isRegex);
		}
		if (e.wholeWord) {
			this._findInput.setWholeWords(this._state.wholeWord);
		}
		if (e.matchCase) {
			this._findInput.setCaseSensitive(this._state.matchCase);
		}
		if (e.searchScope) {
			if (this._state.searchScope) {
183
				this._toggleSelectionFind.checked = true;
A
Alex Dima 已提交
184
			} else {
185
				this._toggleSelectionFind.checked = false;
A
Alex Dima 已提交
186 187 188 189 190 191
			}
			this._updateToggleSelectionFindButton();
		}
		if (e.searchString || e.matchesCount) {
			let showRedOutline = (this._state.searchString.length > 0 && this._state.matchesCount === 0);
			DomUtils.toggleClass(this._domNode, 'no-results', showRedOutline);
A
Alex Dima 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

			let showMatchesCount = (this._state.searchString.length > 0);

			let matchesCount:string = String(this._state.matchesCount);
			let matchesCountTitle = '';
			if (this._state.matchesCount >= MATCHES_LIMIT) {
				matchesCountTitle = NLS_MATCHES_COUNT_LIMIT_TITLE;
				matchesCount += '+';
			}

			this._findInput.setMatchCountState({
				isVisible: showMatchesCount,
				count: matchesCount,
				title: matchesCountTitle
			});
E
Erich Gamma 已提交
207 208 209
		}
	}

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
	// ----- actions

	/**
	 * If 'selection find' is ON we should not disable the button (its function is to cancel 'selection find').
	 * If 'selection find' is OFF we enable the button only if there is a multi line selection.
	 */
	private _updateToggleSelectionFindButton(): void {
		let selection = this._codeEditor.getSelection();
		let isMultiLineSelection = selection ? (selection.startLineNumber !== selection.endLineNumber) : false;
		let isChecked = this._toggleSelectionFind.checked;

		this._toggleSelectionFind.setEnabled(this._isVisible && (isChecked || isMultiLineSelection));
	}

	private _updateButtons(): void {
		this._findInput.setEnabled(this._isVisible);
		this._replaceInputBox.setEnabled(this._isVisible && this._isReplaceVisible);
		this._updateToggleSelectionFindButton();
		this._closeBtn.setEnabled(this._isVisible);

		let findInputIsNonEmpty = (this._state.searchString.length > 0);
		this._prevBtn.setEnabled(this._isVisible && findInputIsNonEmpty);
		this._nextBtn.setEnabled(this._isVisible && findInputIsNonEmpty);
		this._replaceBtn.setEnabled(this._isVisible && this._isReplaceVisible && findInputIsNonEmpty);
		this._replaceAllBtn.setEnabled(this._isVisible && this._isReplaceVisible && findInputIsNonEmpty);

		DomUtils.toggleClass(this._domNode, 'replaceToggled', this._isReplaceVisible);
		this._toggleReplaceBtn.toggleClass('collapse', !this._isReplaceVisible);
		this._toggleReplaceBtn.toggleClass('expand', this._isReplaceVisible);
		this._toggleReplaceBtn.setExpanded(this._isReplaceVisible);

		let canReplace = !this._codeEditor.getConfiguration().readOnly;
		this._toggleReplaceBtn.setEnabled(this._isVisible && canReplace);
	}

	private _reveal(animate:boolean): void {
		if (!this._isVisible) {
			this._isVisible = true;

			this._updateButtons();

			setTimeout(() => {
				DomUtils.addClass(this._domNode, 'visible');
				if (!animate) {
					DomUtils.addClass(this._domNode, 'noanimation');
					setTimeout(() => {
						DomUtils.removeClass(this._domNode, 'noanimation');
					}, 200);
				}
			}, 0);
			this._codeEditor.layoutOverlayWidget(this);
		}
	}

	private _hide(focusTheEditor:boolean): void {
		if (this._isVisible) {
			this._isVisible = false;

			this._updateButtons();

			DomUtils.removeClass(this._domNode, 'visible');
			if (focusTheEditor) {
				this._codeEditor.focus();
			}
			this._codeEditor.layoutOverlayWidget(this);
		}
	}

E
Erich Gamma 已提交
278 279
	// ----- Public

280 281 282 283 284 285 286 287 288 289 290 291
	public focusFindInput(): void {
		this._findInput.select();
		// Edge browser requires focus() in addition to select()
		this._findInput.focus();
	}

	public focusReplaceInput(): void {
		this._replaceInputBox.select();
		// Edge browser requires focus() in addition to select()
		this._replaceInputBox.focus();
	}

E
Erich Gamma 已提交
292 293
	private _onFindInputKeyDown(e:DomUtils.IKeyboardEvent): void {

294 295 296 297 298
		switch (e.asKeybinding()) {
			case CommonKeybindings.ENTER:
				this._codeEditor.getAction(FIND_IDS.NextMatchFindAction).run().done(null, Errors.onUnexpectedError);
				e.preventDefault();
				return;
E
Erich Gamma 已提交
299

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
			case CommonKeybindings.SHIFT_ENTER:
				this._codeEditor.getAction(FIND_IDS.PreviousMatchFindAction).run().done(null, Errors.onUnexpectedError);
				e.preventDefault();
				return;

			case CommonKeybindings.TAB:
				if (this._isReplaceVisible) {
					this._replaceInputBox.focus();
				} else {
					this._findInput.focusOnCaseSensitive();
				}
				e.preventDefault();
				return;

			case CommonKeybindings.CTRLCMD_DOWN_ARROW:
				this._codeEditor.focus();
				e.preventDefault();
				return;
E
Erich Gamma 已提交
318 319 320 321 322
		}
	}

	private _onReplaceInputKeyDown(e:DomUtils.IKeyboardEvent): void {

323 324 325 326 327
		switch (e.asKeybinding()) {
			case CommonKeybindings.ENTER:
				this._controller.replace();
				e.preventDefault();
				return;
E
Erich Gamma 已提交
328

329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
			case CommonKeybindings.CTRLCMD_ENTER:
				this._controller.replaceAll();
				e.preventDefault();
				return;

			case CommonKeybindings.TAB:
				this._findInput.focusOnCaseSensitive();
				e.preventDefault();
				return;

			case CommonKeybindings.SHIFT_TAB:
				this._findInput.focus();
				e.preventDefault();
				return;

			case CommonKeybindings.CTRLCMD_DOWN_ARROW:
				this._codeEditor.focus();
				e.preventDefault();
				return;
E
Erich Gamma 已提交
348 349 350
		}
	}

A
Alex Dima 已提交
351
	// ----- initialization
E
Erich Gamma 已提交
352

353 354 355 356 357 358 359 360
	private _keybindingLabelFor(actionId:string): string {
		let keybindings = this._keybindingService.lookupKeybindings(actionId);
		if (keybindings.length === 0) {
			return '';
		}
		return ' (' + this._keybindingService.getLabelFor(keybindings[0]) + ')';
	}

E
Erich Gamma 已提交
361 362
	private _buildFindPart(): HTMLElement {
		// Find input
A
Alex Dima 已提交
363
		this._findInput = this._register(new FindInput(null, this._contextViewProvider, {
E
Erich Gamma 已提交
364
			width: FindWidget.FIND_INPUT_AREA_WIDTH,
365 366
			label: NLS_FIND_INPUT_LABEL,
			placeholder: NLS_FIND_INPUT_PLACEHOLDER,
A
Alex Dima 已提交
367 368 369
			appendCaseSensitiveLabel: this._keybindingLabelFor(FIND_IDS.ToggleCaseSensitiveCommand),
			appendWholeWordsLabel: this._keybindingLabelFor(FIND_IDS.ToggleWholeWordCommand),
			appendRegexLabel: this._keybindingLabelFor(FIND_IDS.ToggleRegexCommand),
A
Alex Dima 已提交
370
			validation: (value:string): InputBoxMessage => {
E
Erich Gamma 已提交
371 372 373 374 375 376 377
				if (value.length === 0) {
					return null;
				}
				if (!this._findInput.getRegex()) {
					return null;
				}
				try {
A
Alex Dima 已提交
378
					let r = new RegExp(value);
E
Erich Gamma 已提交
379 380 381 382 383
					return null;
				} catch (e) {
					return { content: e.message };
				}
			}
A
Alex Dima 已提交
384 385
		}));
		this._register(this._findInput.onKeyDown((e) => this._onFindInputKeyDown(e)));
386 387 388
		this._register(this._findInput.onInput(() => {
			this._state.change({ searchString: this._findInput.getValue() }, true);
		}));
A
Alex Dima 已提交
389
		this._register(this._findInput.onDidOptionChange(() => {
A
Alex Dima 已提交
390 391 392 393 394
			this._state.change({
				isRegex: this._findInput.getRegex(),
				wholeWord: this._findInput.getWholeWords(),
				matchCase: this._findInput.getCaseSensitive()
			}, true);
A
Alex Dima 已提交
395
		}));
396 397 398 399 400 401 402 403
		this._register(this._findInput.onCaseSensitiveKeyDown((e) => {
			if (e.equals(CommonKeybindings.SHIFT_TAB)) {
				if (this._isReplaceVisible) {
					this._replaceInputBox.focus();
					e.preventDefault();
				}
			}
		}));
E
Erich Gamma 已提交
404 405

		// Previous button
A
Alex Dima 已提交
406
		this._prevBtn = this._register(new SimpleButton({
A
Alex Dima 已提交
407
			label: NLS_PREVIOUS_MATCH_BTN_LABEL + this._keybindingLabelFor(FIND_IDS.PreviousMatchFindAction),
A
Alex Dima 已提交
408 409
			className: 'previous',
			onTrigger: () => {
A
Alex Dima 已提交
410
				this._codeEditor.getAction(FIND_IDS.PreviousMatchFindAction).run().done(null, Errors.onUnexpectedError);
A
Alex Dima 已提交
411 412
			},
			onKeyDown: (e) => {}
A
Alex Dima 已提交
413
		}));
E
Erich Gamma 已提交
414 415

		// Next button
A
Alex Dima 已提交
416
		this._nextBtn = this._register(new SimpleButton({
A
Alex Dima 已提交
417
			label: NLS_NEXT_MATCH_BTN_LABEL + this._keybindingLabelFor(FIND_IDS.NextMatchFindAction),
A
Alex Dima 已提交
418 419
			className: 'next',
			onTrigger: () => {
A
Alex Dima 已提交
420
				this._codeEditor.getAction(FIND_IDS.NextMatchFindAction).run().done(null, Errors.onUnexpectedError);
A
Alex Dima 已提交
421 422
			},
			onKeyDown: (e) => {}
A
Alex Dima 已提交
423
		}));
E
Erich Gamma 已提交
424

A
Alex Dima 已提交
425
		let findPart = document.createElement('div');
E
Erich Gamma 已提交
426 427 428 429 430 431
		findPart.className = 'find-part';
		findPart.appendChild(this._findInput.domNode);
		findPart.appendChild(this._prevBtn.domNode);
		findPart.appendChild(this._nextBtn.domNode);

		// Toggle selection button
432 433 434 435 436 437 438 439 440
		this._toggleSelectionFind = this._register(new SimpleCheckbox({
			parent: findPart,
			title: NLS_TOGGLE_SELECTION_FIND_TITLE,
			onChange: () => {
				if (this._toggleSelectionFind.checked) {
					this._reseedFindScope();
				} else {
					this._state.change({ searchScope: null }, true);
				}
E
Erich Gamma 已提交
441 442 443 444
			}
		}));

		// Close button
A
Alex Dima 已提交
445
		this._closeBtn = this._register(new SimpleButton({
A
Alex Dima 已提交
446
			label: NLS_CLOSE_BTN_LABEL + this._keybindingLabelFor(FIND_IDS.CloseFindWidgetCommand),
A
Alex Dima 已提交
447 448 449 450 451
			className: 'close-fw',
			onTrigger: () => {
				this._state.change({ isRevealed: false }, false);
			},
			onKeyDown: (e) => {
A
Alex Dima 已提交
452 453
				if (e.equals(CommonKeybindings.TAB)) {
					if (this._isReplaceVisible) {
454 455 456 457 458
						if (this._replaceBtn.isEnabled()) {
							this._replaceBtn.focus();
						} else {
							this._codeEditor.focus();
						}
A
Alex Dima 已提交
459 460
						e.preventDefault();
					}
A
Alex Dima 已提交
461
				}
E
Erich Gamma 已提交
462
			}
A
Alex Dima 已提交
463
		}));
E
Erich Gamma 已提交
464 465 466 467 468 469 470 471

		findPart.appendChild(this._closeBtn.domNode);

		return findPart;
	}

	private _buildReplacePart(): HTMLElement {
		// Replace input
A
Alex Dima 已提交
472
		let replaceInput = document.createElement('div');
E
Erich Gamma 已提交
473 474
		replaceInput.className = 'replace-input';
		replaceInput.style.width = FindWidget.REPLACE_INPUT_AREA_WIDTH + 'px';
A
Alex Dima 已提交
475
		this._replaceInputBox = this._register(new InputBox(replaceInput, null, {
476 477
			ariaLabel: NLS_REPLACE_INPUT_LABEL,
			placeholder: NLS_REPLACE_INPUT_PLACEHOLDER
A
Alex Dima 已提交
478
		}));
E
Erich Gamma 已提交
479

A
Alex Dima 已提交
480
		this._register(DomUtils.addStandardDisposableListener(this._replaceInputBox.inputElement, 'keydown', (e) => this._onReplaceInputKeyDown(e)));
481 482 483
		this._register(DomUtils.addStandardDisposableListener(this._replaceInputBox.inputElement, 'input', (e) => {
			this._state.change({ replaceString: this._replaceInputBox.value }, false);
		}));
E
Erich Gamma 已提交
484 485

		// Replace one button
A
Alex Dima 已提交
486
		this._replaceBtn = this._register(new SimpleButton({
A
Alex Dima 已提交
487 488 489 490 491
			label: NLS_REPLACE_BTN_LABEL,
			className: 'replace',
			onTrigger: () => {
				this._controller.replace();
			},
A
Alex Dima 已提交
492 493 494 495 496 497 498
			onKeyDown: (e) => {
				if (e.equals(CommonKeybindings.SHIFT_TAB)) {
					this._closeBtn.focus();
					e.preventDefault();
				}
			}
		}));
E
Erich Gamma 已提交
499 500

		// Replace all button
A
Alex Dima 已提交
501
		this._replaceAllBtn = this._register(new SimpleButton({
A
Alex Dima 已提交
502 503 504 505 506 507
			label: NLS_REPLACE_ALL_BTN_LABEL,
			className: 'replace-all',
			onTrigger: () => {
				this._controller.replaceAll();
			},
			onKeyDown: (e) => {}
A
Alex Dima 已提交
508
		}));
E
Erich Gamma 已提交
509

A
Alex Dima 已提交
510
		let replacePart = document.createElement('div');
E
Erich Gamma 已提交
511 512 513 514 515 516 517 518 519 520
		replacePart.className = 'replace-part';
		replacePart.appendChild(replaceInput);
		replacePart.appendChild(this._replaceBtn.domNode);
		replacePart.appendChild(this._replaceAllBtn.domNode);

		return replacePart;
	}

	private _buildDomNode(): void {
		// Find part
A
Alex Dima 已提交
521
		let findPart = this._buildFindPart();
E
Erich Gamma 已提交
522 523

		// Replace part
A
Alex Dima 已提交
524
		let replacePart = this._buildReplacePart();
E
Erich Gamma 已提交
525 526

		// Toggle replace button
A
Alex Dima 已提交
527
		this._toggleReplaceBtn = this._register(new SimpleButton({
A
Alex Dima 已提交
528 529 530 531 532 533
			label: NLS_TOGGLE_REPLACE_MODE_BTN_LABEL,
			className: 'toggle left',
			onTrigger: () => {
				this._state.change({ isReplaceRevealed: !this._isReplaceVisible }, true);
			},
			onKeyDown: (e) => {}
A
Alex Dima 已提交
534
		}));
E
Erich Gamma 已提交
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
		this._toggleReplaceBtn.toggleClass('expand', this._isReplaceVisible);
		this._toggleReplaceBtn.toggleClass('collapse', !this._isReplaceVisible);
		this._toggleReplaceBtn.setExpanded(this._isReplaceVisible);

		// Widget
		this._domNode = document.createElement('div');
		this._domNode.className = 'editor-widget find-widget';
		this._domNode.setAttribute('aria-hidden', 'false');

		this._domNode.appendChild(this._toggleReplaceBtn.domNode);
		this._domNode.appendChild(findPart);
		this._domNode.appendChild(replacePart);
	}
}

550 551 552 553 554 555 556
interface ISimpleCheckboxOpts {
	parent: HTMLElement;
	title: string;
	onChange: () => void;
}

class SimpleCheckbox extends Widget {
A
Alex Dima 已提交
557 558

	private static _COUNTER = 0;
E
Erich Gamma 已提交
559

560
	private _opts: ISimpleCheckboxOpts;
A
Alex Dima 已提交
561 562 563
	private _domNode: HTMLElement;
	private _checkbox: HTMLInputElement;
	private _label: HTMLLabelElement;
E
Erich Gamma 已提交
564

565
	constructor(opts:ISimpleCheckboxOpts) {
A
Alex Dima 已提交
566
		super();
567 568
		this._opts = opts;

E
Erich Gamma 已提交
569 570
		this._domNode = document.createElement('div');
		this._domNode.className = 'monaco-checkbox';
571
		this._domNode.title = this._opts.title;
E
Erich Gamma 已提交
572 573 574 575

		this._checkbox = document.createElement('input');
		this._checkbox.type = 'checkbox';
		this._checkbox.className = 'checkbox';
576
		this._checkbox.id = 'checkbox-' + SimpleCheckbox._COUNTER++;
E
Erich Gamma 已提交
577

A
Alex Dima 已提交
578 579
		this._label = document.createElement('label');
		this._label.className = 'label';
E
Erich Gamma 已提交
580
		// Connect the label and the checkbox. Checkbox will get checked when the label recieves a click.
A
Alex Dima 已提交
581
		this._label.htmlFor = this._checkbox.id;
E
Erich Gamma 已提交
582 583

		this._domNode.appendChild(this._checkbox);
A
Alex Dima 已提交
584
		this._domNode.appendChild(this._label);
E
Erich Gamma 已提交
585

586 587 588 589 590
		this._opts.parent.appendChild(this._domNode);

		this.onchange(this._checkbox, (e) => {
			this._opts.onChange();
		});
E
Erich Gamma 已提交
591 592 593 594 595 596
	}

	public get domNode(): HTMLElement {
		return this._domNode;
	}

597 598 599 600 601 602
	public get checked(): boolean {
		return this._checkbox.checked;
	}

	public set checked(newValue:boolean) {
		this._checkbox.checked = newValue;
E
Erich Gamma 已提交
603 604 605 606 607 608
	}

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

609
	private enable(): void {
E
Erich Gamma 已提交
610 611 612
		this._checkbox.removeAttribute('disabled');
	}

613
	private disable(): void {
E
Erich Gamma 已提交
614 615
		this._checkbox.disabled = true;
	}
616 617 618 619 620 621 622 623

	public setEnabled(enabled:boolean): void {
		if (enabled) {
			this.enable();
		} else {
			this.disable();
		}
	}
E
Erich Gamma 已提交
624 625
}

A
Alex Dima 已提交
626 627 628 629 630 631
interface ISimpleButtonOpts {
	label: string;
	className: string;
	onTrigger: ()=>void;
	onKeyDown: (e:DomUtils.IKeyboardEvent)=>void;
}
E
Erich Gamma 已提交
632

A
Alex Dima 已提交
633
class SimpleButton extends Widget {
E
Erich Gamma 已提交
634

A
Alex Dima 已提交
635 636
	private _opts: ISimpleButtonOpts;
	private _domNode: HTMLElement;
E
Erich Gamma 已提交
637

A
Alex Dima 已提交
638
	constructor(opts:ISimpleButtonOpts) {
A
Alex Dima 已提交
639
		super();
A
Alex Dima 已提交
640
		this._opts = opts;
E
Erich Gamma 已提交
641 642

		this._domNode = document.createElement('div');
A
Alex Dima 已提交
643
		this._domNode.title = this._opts.label;
A
Alex Dima 已提交
644
		this._domNode.tabIndex = 0;
A
Alex Dima 已提交
645
		this._domNode.className = 'button ' + this._opts.className;
E
Erich Gamma 已提交
646
		this._domNode.setAttribute('role', 'button');
A
Alex Dima 已提交
647
		this._domNode.setAttribute('aria-label', this._opts.label);
E
Erich Gamma 已提交
648

A
Alex Dima 已提交
649
		this.onclick(this._domNode, (e) => {
A
Alex Dima 已提交
650
			this._opts.onTrigger();
E
Erich Gamma 已提交
651
			e.preventDefault();
A
Alex Dima 已提交
652 653
		});
		this.onkeydown(this._domNode, (e) => {
E
Erich Gamma 已提交
654
			if (e.equals(CommonKeybindings.SPACE) || e.equals(CommonKeybindings.ENTER)) {
A
Alex Dima 已提交
655
				this._opts.onTrigger();
E
Erich Gamma 已提交
656 657 658
				e.preventDefault();
				return;
			}
A
Alex Dima 已提交
659
			this._opts.onKeyDown(e);
A
Alex Dima 已提交
660
		});
E
Erich Gamma 已提交
661 662 663 664 665 666
	}

	public get domNode(): HTMLElement {
		return this._domNode;
	}

667 668 669 670
	public isEnabled(): boolean {
		return (this._domNode.tabIndex >= 0);
	}

E
Erich Gamma 已提交
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
	public focus(): void {
		this._domNode.focus();
	}

	public setEnabled(enabled:boolean): void {
		DomUtils.toggleClass(this._domNode, 'disabled', !enabled);
		this._domNode.setAttribute('aria-disabled', String(!enabled));
		this._domNode.tabIndex = enabled ? 0 : -1;
	}

	public setExpanded(expanded:boolean): void {
		this._domNode.setAttribute('aria-expanded', String(expanded));
	}

	public toggleClass(className:string, shouldHaveIt:boolean): void {
		DomUtils.toggleClass(this._domNode, className, shouldHaveIt);
	}
}