findWidget.ts 20.1 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 18 19 20

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 {StandardKeyboardEvent} from 'vs/base/browser/keyboardEvent';
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';
import {FindIds} from 'vs/editor/contrib/find/common/findModel';
import {disposeAll, IDisposable} from 'vs/base/common/lifecycle';
E
Erich Gamma 已提交
21
import {CommonKeybindings} from 'vs/base/common/keyCodes';
22
import {IKeybindingService} from 'vs/platform/keybinding/common/keybindingService';
A
Alex Dima 已提交
23
import {INewFindReplaceState, FindReplaceStateChangedEvent, FindReplaceState} from 'vs/editor/contrib/find/common/findState';
E
Erich Gamma 已提交
24 25 26 27 28 29

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

30 31 32 33 34 35 36 37 38 39 40 41
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 已提交
42
export class FindWidget 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 62 63 64 65
	private _toggleReplaceBtn: SimpleButton;
	private _prevBtn: SimpleButton;
	private _nextBtn: SimpleButton;
	private _toggleSelectionFind: Checkbox;
	private _closeBtn: SimpleButton;
	private _replaceBtn: SimpleButton;
	private _replaceAllBtn: SimpleButton;
E
Erich Gamma 已提交
66

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

A
Alex Dima 已提交
71
	private _toDispose: IDisposable[];
E
Erich Gamma 已提交
72

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

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

		this._isVisible = false;
		this._isReplaceVisible = false;
		this._isReplaceEnabled = false;
		this._toDispose = [];

A
Alex Dima 已提交
93
		this._toDispose.push(this._state.addChangeListener((e) => this._onStateChanged(e)));
E
Erich Gamma 已提交
94 95 96

		this._buildDomNode();

A
Alex Dima 已提交
97 98 99
		this.focusTracker = DomUtils.trackFocus(this._findInput.inputBox.inputElement);
		this.focusTracker.addFocusListener(() => this._reseedFindScope());
		this._toDispose.push(this.focusTracker);
E
Erich Gamma 已提交
100

A
Alex Dima 已提交
101 102 103
		this._toDispose.push({
			dispose: () => {
				this._findInput.destroy();
E
Erich Gamma 已提交
104 105
			}
		});
A
Alex Dima 已提交
106 107 108
		this._toDispose.push(this._replaceInputBox);

		this._codeEditor.addOverlayWidget(this);
E
Erich Gamma 已提交
109 110 111
	}

	public dispose(): void {
A
Alex Dima 已提交
112 113 114 115 116 117 118 119 120
		this._toDispose = disposeAll(this._toDispose);
	}

	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);
E
Erich Gamma 已提交
147

A
Alex Dima 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
			let findInputIsNonEmpty = (this._state.searchString.length > 0);
			this._prevBtn.setEnabled(findInputIsNonEmpty);
			this._nextBtn.setEnabled(findInputIsNonEmpty);
			this._replaceBtn.setEnabled(findInputIsNonEmpty);
			this._replaceAllBtn.setEnabled(findInputIsNonEmpty);
		}
		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) {
				this._enableReplace();
			} else {
				this._disableReplace();
			}
		}
		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) {
				this._toggleSelectionFind.checkbox.checked = true;
			} else {
				this._toggleSelectionFind.checkbox.checked = false;
			}
			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);
E
Erich Gamma 已提交
191 192 193 194 195
		}
	}

	// ----- Public

196 197 198 199 200 201 202 203 204 205 206 207
	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();
	}

A
Alex Dima 已提交
208
	private _enableReplace(): void {
E
Erich Gamma 已提交
209 210 211 212 213 214 215 216 217 218 219
		this._isReplaceEnabled = true;
		if (!this._codeEditor.getConfiguration().readOnly && !this._isReplaceVisible) {
			this._replaceInputBox.enable();
			this._isReplaceVisible = true;
			DomUtils.addClass(this._domNode, 'replaceToggled');
			this._toggleReplaceBtn.toggleClass('collapse', false);
			this._toggleReplaceBtn.toggleClass('expand', true);
			this._toggleReplaceBtn.setExpanded(true);
		}
	}

A
Alex Dima 已提交
220
	private _disableReplace(): void {
E
Erich Gamma 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233
		this._isReplaceEnabled = false;
		if (this._isReplaceVisible) {
			this._replaceInputBox.disable();
			DomUtils.removeClass(this._domNode, 'replaceToggled');
			this._toggleReplaceBtn.toggleClass('expand', false);
			this._toggleReplaceBtn.toggleClass('collapse', true);
			this._toggleReplaceBtn.setExpanded(false);
			this._isReplaceVisible = false;
		}
	}

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

A
Alex Dima 已提交
234
		let handled = false;
E
Erich Gamma 已提交
235 236

		if (e.equals(CommonKeybindings.ENTER)) {
A
Alex Dima 已提交
237
			this._codeEditor.getAction(FindIds.NEXT_MATCH_FIND_ACTION_ID).run().done(null, Errors.onUnexpectedError);
E
Erich Gamma 已提交
238 239
			handled = true;
		} else if (e.equals(CommonKeybindings.SHIFT_ENTER)) {
A
Alex Dima 已提交
240
			this._codeEditor.getAction(FindIds.PREVIOUS_MATCH_FIND_ACTION_ID).run().done(null, Errors.onUnexpectedError);
E
Erich Gamma 已提交
241 242 243 244 245 246 247 248
			handled = true;
		} else if (e.equals(CommonKeybindings.TAB)) {
			if (this._isReplaceVisible) {
				this._replaceInputBox.focus();
			} else {
				this._findInput.focusOnCaseSensitive();
			}
			handled = true;
249 250 251
		} else if (e.equals(CommonKeybindings.CTRLCMD_DOWN_ARROW)) {
			this._codeEditor.focus();
			handled = true;
E
Erich Gamma 已提交
252 253 254 255 256
		}

		if (handled) {
			e.preventDefault();
		} else {
A
Alex Dima 已提交
257
			// getValue() is not updated right away
E
Erich Gamma 已提交
258
			setTimeout(() => {
A
Alex Dima 已提交
259
				this._state.change({ searchString: this._findInput.getValue() }, true);
E
Erich Gamma 已提交
260 261 262 263 264 265
			}, 10);
		}
	}

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

A
Alex Dima 已提交
266
		let handled = false;
E
Erich Gamma 已提交
267 268 269 270 271 272 273 274 275 276

		if (e.equals(CommonKeybindings.ENTER)) {
			this._controller.replace();
			handled = true;
		} else if (e.equals(CommonKeybindings.CTRLCMD_ENTER)) {
			this._controller.replaceAll();
			handled = true;
		} else if (e.equals(CommonKeybindings.TAB)) {
			this._findInput.focusOnCaseSensitive();
			handled = true;
277 278 279
		} else if (e.equals(CommonKeybindings.CTRLCMD_DOWN_ARROW)) {
			this._codeEditor.focus();
			handled = true;
E
Erich Gamma 已提交
280 281 282 283 284 285
		}

		if (handled) {
			e.preventDefault();
		} else {
			setTimeout(() => {
A
Alex Dima 已提交
286
				this._state.change({ replaceString: this._replaceInputBox.value }, false);
E
Erich Gamma 已提交
287 288 289 290
			}, 10);
		}
	}

A
Alex Dima 已提交
291
	// ----- initialization
E
Erich Gamma 已提交
292

293 294 295 296 297 298 299 300
	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 已提交
301 302
	private _buildFindPart(): HTMLElement {
		// Find input
A
Alex Dima 已提交
303
		this._findInput = new FindInput(null, this._contextViewProvider, {
E
Erich Gamma 已提交
304
			width: FindWidget.FIND_INPUT_AREA_WIDTH,
305 306
			label: NLS_FIND_INPUT_LABEL,
			placeholder: NLS_FIND_INPUT_PLACEHOLDER,
A
Alex Dima 已提交
307 308 309 310
			appendCaseSensitiveLabel: this._keybindingLabelFor(FindIds.TOGGLE_CASE_SENSITIVE_COMMAND_ID),
			appendWholeWordsLabel: this._keybindingLabelFor(FindIds.TOGGLE_WHOLE_WORD_COMMAND_ID),
			appendRegexLabel: this._keybindingLabelFor(FindIds.TOGGLE_REGEX_COMMAND_ID),
			validation: (value:string): InputBoxMessage => {
E
Erich Gamma 已提交
311 312 313 314 315 316 317 318 319 320 321 322 323 324
				if (value.length === 0) {
					return null;
				}
				if (!this._findInput.getRegex()) {
					return null;
				}
				try {
					new RegExp(value);
					return null;
				} catch (e) {
					return { content: e.message };
				}
			}
		}).on('keydown', (browserEvent:KeyboardEvent) => {
A
Alex Dima 已提交
325 326 327 328 329 330 331
			this._onFindInputKeyDown(new StandardKeyboardEvent(browserEvent));
		}).on(FindInput.OPTION_CHANGE, () => {
			this._state.change({
				isRegex: this._findInput.getRegex(),
				wholeWord: this._findInput.getWholeWords(),
				matchCase: this._findInput.getCaseSensitive()
			}, true);
E
Erich Gamma 已提交
332 333 334 335 336
		});

		this._findInput.disable();

		// Previous button
A
Alex Dima 已提交
337 338 339 340 341 342 343
		this._prevBtn = new SimpleButton({
			label: NLS_PREVIOUS_MATCH_BTN_LABEL + this._keybindingLabelFor(FindIds.PREVIOUS_MATCH_FIND_ACTION_ID),
			className: 'previous',
			onTrigger: () => {
				this._codeEditor.getAction(FindIds.PREVIOUS_MATCH_FIND_ACTION_ID).run().done(null, Errors.onUnexpectedError);
			},
			onKeyDown: (e) => {}
E
Erich Gamma 已提交
344 345 346 347
		});
		this._toDispose.push(this._prevBtn);

		// Next button
A
Alex Dima 已提交
348 349 350 351 352 353 354
		this._nextBtn = new SimpleButton({
			label: NLS_NEXT_MATCH_BTN_LABEL + this._keybindingLabelFor(FindIds.NEXT_MATCH_FIND_ACTION_ID),
			className: 'next',
			onTrigger: () => {
				this._codeEditor.getAction(FindIds.NEXT_MATCH_FIND_ACTION_ID).run().done(null, Errors.onUnexpectedError);
			},
			onKeyDown: (e) => {}
E
Erich Gamma 已提交
355 356 357
		});
		this._toDispose.push(this._nextBtn);

A
Alex Dima 已提交
358
		let findPart = document.createElement('div');
E
Erich Gamma 已提交
359 360 361 362 363 364
		findPart.className = 'find-part';
		findPart.appendChild(this._findInput.domNode);
		findPart.appendChild(this._prevBtn.domNode);
		findPart.appendChild(this._nextBtn.domNode);

		// Toggle selection button
365
		this._toggleSelectionFind = new Checkbox(findPart, NLS_TOGGLE_SELECTION_FIND_TITLE);
E
Erich Gamma 已提交
366 367 368
		this._toggleSelectionFind.disable();
		this._toDispose.push(DomUtils.addStandardDisposableListener(this._toggleSelectionFind.checkbox, 'change', (e) => {
			if (this._toggleSelectionFind.checkbox.checked) {
A
Alex Dima 已提交
369
				this._reseedFindScope();
E
Erich Gamma 已提交
370
			} else {
A
Alex Dima 已提交
371
				this._state.change({ searchScope: null }, true);
E
Erich Gamma 已提交
372 373 374 375 376 377 378 379
			}
		}));

		this._codeEditor.addListener(EditorCommon.EventType.CursorSelectionChanged, () => {
			this._updateToggleSelectionFindButton();
		});

		// Close button
A
Alex Dima 已提交
380 381 382 383 384 385 386 387 388 389 390
		this._closeBtn = new SimpleButton({
			label: NLS_CLOSE_BTN_LABEL + this._keybindingLabelFor(FindIds.CLOSE_FIND_WIDGET_COMMAND_ID),
			className: 'close-fw',
			onTrigger: () => {
				this._state.change({ isRevealed: false }, false);
			},
			onKeyDown: (e) => {
				if (this._isReplaceVisible) {
					this._replaceBtn.focus();
					e.preventDefault();
				}
E
Erich Gamma 已提交
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
			}
		});
		this._toDispose.push(this._closeBtn);

		findPart.appendChild(this._closeBtn.domNode);

		return findPart;
	}

	/**
	 * 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 {
		if (!this._isVisible) {
			return;
		}

		if (!this._toggleSelectionFind.checkbox.checked) {
A
Alex Dima 已提交
410
			let selection = this._codeEditor.getSelection();
E
Erich Gamma 已提交
411 412 413 414 415 416 417 418 419 420 421

			if (selection.startLineNumber === selection.endLineNumber) {
				this._toggleSelectionFind.disable();
			} else {
				this._toggleSelectionFind.enable();
			}
		}
	}

	private _buildReplacePart(): HTMLElement {
		// Replace input
A
Alex Dima 已提交
422
		let replaceInput = document.createElement('div');
E
Erich Gamma 已提交
423 424
		replaceInput.className = 'replace-input';
		replaceInput.style.width = FindWidget.REPLACE_INPUT_AREA_WIDTH + 'px';
A
Alex Dima 已提交
425
		this._replaceInputBox = new InputBox(replaceInput, null, {
426 427
			ariaLabel: NLS_REPLACE_INPUT_LABEL,
			placeholder: NLS_REPLACE_INPUT_PLACEHOLDER
E
Erich Gamma 已提交
428 429 430 431 432 433
		});

		this._toDispose.push(DomUtils.addStandardDisposableListener(this._replaceInputBox.inputElement, 'keydown', (e) => this._onReplaceInputKeyDown(e)));
		this._replaceInputBox.disable();

		// Replace one button
A
Alex Dima 已提交
434 435 436 437 438 439 440
		this._replaceBtn = new SimpleButton({
			label: NLS_REPLACE_BTN_LABEL,
			className: 'replace',
			onTrigger: () => {
				this._controller.replace();
			},
			onKeyDown: (e) => {}
E
Erich Gamma 已提交
441 442 443 444
		});
		this._toDispose.push(this._replaceBtn);

		// Replace all button
A
Alex Dima 已提交
445 446 447 448 449 450 451
		this._replaceAllBtn = new SimpleButton({
			label: NLS_REPLACE_ALL_BTN_LABEL,
			className: 'replace-all',
			onTrigger: () => {
				this._controller.replaceAll();
			},
			onKeyDown: (e) => {}
E
Erich Gamma 已提交
452 453 454
		});
		this._toDispose.push(this._replaceAllBtn);

A
Alex Dima 已提交
455
		let replacePart = document.createElement('div');
E
Erich Gamma 已提交
456 457 458 459 460 461 462 463 464 465
		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 已提交
466
		let findPart = this._buildFindPart();
E
Erich Gamma 已提交
467 468

		// Replace part
A
Alex Dima 已提交
469
		let replacePart = this._buildReplacePart();
E
Erich Gamma 已提交
470 471

		// Toggle replace button
A
Alex Dima 已提交
472 473 474 475 476 477 478
		this._toggleReplaceBtn = new SimpleButton({
			label: NLS_TOGGLE_REPLACE_MODE_BTN_LABEL,
			className: 'toggle left',
			onTrigger: () => {
				this._state.change({ isReplaceRevealed: !this._isReplaceVisible }, true);
			},
			onKeyDown: (e) => {}
E
Erich Gamma 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
		});
		this._toggleReplaceBtn.toggleClass('expand', this._isReplaceVisible);
		this._toggleReplaceBtn.toggleClass('collapse', !this._isReplaceVisible);
		this._toggleReplaceBtn.setExpanded(this._isReplaceVisible);
		this._toDispose.push(this._toggleReplaceBtn);

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

		if (!this._codeEditor.getConfiguration().readOnly) {
			DomUtils.addClass(this._domNode, 'can-replace');
		}

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

	// ----- actions

	private _reveal(animate:boolean): void {
		if (!this._isVisible) {
			this._findInput.enable();
			if (this._isReplaceVisible) {
				this._replaceInputBox.enable();
			}

			this._toggleSelectionFind.enable();
			this._closeBtn.setEnabled(true);

A
Alex Dima 已提交
511 512 513 514 515
			let findInputIsNonEmpty = (this._state.searchString.length > 0);
			this._prevBtn.setEnabled(findInputIsNonEmpty);
			this._nextBtn.setEnabled(findInputIsNonEmpty);
			this._replaceBtn.setEnabled(findInputIsNonEmpty);
			this._replaceAllBtn.setEnabled(findInputIsNonEmpty);
E
Erich Gamma 已提交
516 517

			this._isVisible = true;
A
Alex Dima 已提交
518
			setTimeout(() => {
E
Erich Gamma 已提交
519 520 521
				DomUtils.addClass(this._domNode, 'visible');
				if (!animate) {
					DomUtils.addClass(this._domNode, 'noanimation');
A
Alex Dima 已提交
522
					setTimeout(() => {
E
Erich Gamma 已提交
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
						DomUtils.removeClass(this._domNode, 'noanimation');
					}, 200);
				}
			}, 0);
			this._codeEditor.layoutOverlayWidget(this);
		}
	}

	private _hide(focusTheEditor:boolean): void {
		if (this._isVisible) {
			this._findInput.disable();
			this._replaceInputBox.disable();
			this._toggleSelectionFind.disable();
			this._closeBtn.setEnabled(false);

			this._prevBtn.setEnabled(false);
			this._nextBtn.setEnabled(false);
			this._replaceBtn.setEnabled(false);
			this._replaceAllBtn.setEnabled(false);

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

A
Alex Dima 已提交
553 554 555
export class Checkbox {

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

A
Alex Dima 已提交
557 558 559
	private _domNode: HTMLElement;
	private _checkbox: HTMLInputElement;
	private _label: HTMLLabelElement;
E
Erich Gamma 已提交
560 561 562 563 564 565 566 567 568

	constructor(parent: HTMLElement, title: string) {
		this._domNode = document.createElement('div');
		this._domNode.className = 'monaco-checkbox';
		this._domNode.title = title;

		this._checkbox = document.createElement('input');
		this._checkbox.type = 'checkbox';
		this._checkbox.className = 'checkbox';
A
Alex Dima 已提交
569
		this._checkbox.id = 'checkbox-' + Checkbox._COUNTER++;
E
Erich Gamma 已提交
570

A
Alex Dima 已提交
571 572
		this._label = document.createElement('label');
		this._label.className = 'label';
E
Erich Gamma 已提交
573
		// Connect the label and the checkbox. Checkbox will get checked when the label recieves a click.
A
Alex Dima 已提交
574
		this._label.htmlFor = this._checkbox.id;
E
Erich Gamma 已提交
575 576

		this._domNode.appendChild(this._checkbox);
A
Alex Dima 已提交
577
		this._domNode.appendChild(this._label);
E
Erich Gamma 已提交
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602

		parent.appendChild(this._domNode);
	}

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

	public get checkbox(): HTMLInputElement {
		return this._checkbox;
	}

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

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

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

A
Alex Dima 已提交
603 604 605 606 607 608
interface ISimpleButtonOpts {
	label: string;
	className: string;
	onTrigger: ()=>void;
	onKeyDown: (e:DomUtils.IKeyboardEvent)=>void;
}
E
Erich Gamma 已提交
609

A
Alex Dima 已提交
610
class SimpleButton implements IDisposable {
E
Erich Gamma 已提交
611

A
Alex Dima 已提交
612 613 614
	private _opts: ISimpleButtonOpts;
	private _domNode: HTMLElement;
	private _toDispose: IDisposable[];
E
Erich Gamma 已提交
615

A
Alex Dima 已提交
616 617
	constructor(opts:ISimpleButtonOpts) {
		this._opts = opts;
E
Erich Gamma 已提交
618 619

		this._domNode = document.createElement('div');
A
Alex Dima 已提交
620
		this._domNode.title = this._opts.label;
E
Erich Gamma 已提交
621
		this._domNode.tabIndex = -1;
A
Alex Dima 已提交
622
		this._domNode.className = 'button ' + this._opts.className;
E
Erich Gamma 已提交
623
		this._domNode.setAttribute('role', 'button');
A
Alex Dima 已提交
624
		this._domNode.setAttribute('aria-label', this._opts.label);
E
Erich Gamma 已提交
625 626 627

		this._toDispose = [];
		this._toDispose.push(DomUtils.addStandardDisposableListener(this._domNode, 'click', (e) => {
A
Alex Dima 已提交
628
			this._opts.onTrigger();
E
Erich Gamma 已提交
629 630 631 632
			e.preventDefault();
		}));
		this._toDispose.push(DomUtils.addStandardDisposableListener(this._domNode, 'keydown', (e) => {
			if (e.equals(CommonKeybindings.SPACE) || e.equals(CommonKeybindings.ENTER)) {
A
Alex Dima 已提交
633
				this._opts.onTrigger();
E
Erich Gamma 已提交
634 635 636
				e.preventDefault();
				return;
			}
A
Alex Dima 已提交
637
			this._opts.onKeyDown(e);
E
Erich Gamma 已提交
638 639 640 641
		}));
	}

	public dispose(): void {
A
Alex Dima 已提交
642
		this._toDispose = disposeAll(this._toDispose);
E
Erich Gamma 已提交
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
	}

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

	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);
	}
}