keybindingWidgets.ts 10.7 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!./media/keybindings';
import * as nls from 'vs/nls';
8
import { OS } from 'vs/base/common/platform';
9
import { TPromise } from 'vs/base/common/winjs.base';
10
import { Disposable } from 'vs/base/common/lifecycle';
M
Matt Bierner 已提交
11
import { Event, Emitter } from 'vs/base/common/event';
12
import { KeybindingLabel } from 'vs/base/browser/ui/keybindingLabel/keybindingLabel';
13 14 15 16 17 18 19 20 21
import { Widget } from 'vs/base/browser/ui/widget';
import { ResolvedKeybinding, KeyCode } from 'vs/base/common/keyCodes';
import * as dom from 'vs/base/browser/dom';
import { InputBox, IInputOptions } from 'vs/base/browser/ui/inputbox/inputBox';
import { IKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { FastDomNode, createFastDomNode } from 'vs/base/browser/fastDomNode';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { IContextViewService } from 'vs/platform/contextview/browser/contextView';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
22
import { ICodeEditor, IOverlayWidget, IOverlayWidgetPosition } from 'vs/editor/browser/editorBrowser';
23
import { attachInputBoxStyler, attachStylerCallback } from 'vs/platform/theme/common/styler';
B
Benjamin Pasero 已提交
24
import { IThemeService } from 'vs/platform/theme/common/themeService';
25
import { editorWidgetBackground, widgetShadow } from 'vs/platform/theme/common/colorRegistry';
26
import { ScrollType } from 'vs/editor/common/editorCommon';
27 28 29

class KeybindingInputWidget extends Widget {

A
Alex Dima 已提交
30
	private readonly inputBox: InputBox;
31

32 33 34
	private _acceptChords: boolean;
	private _firstPart: ResolvedKeybinding;
	private _chordPart: ResolvedKeybinding;
A
Alex Dima 已提交
35
	private _inputValue: string;
36 37 38

	private _onKeybinding = this._register(new Emitter<[ResolvedKeybinding, ResolvedKeybinding]>());
	public readonly onKeybinding: Event<[ResolvedKeybinding, ResolvedKeybinding]> = this._onKeybinding.event;
39 40 41 42 43 44 45

	private _onEnter = this._register(new Emitter<void>());
	public readonly onEnter: Event<void> = this._onEnter.event;

	private _onEscape = this._register(new Emitter<void>());
	public readonly onEscape: Event<void> = this._onEscape.event;

A
Alex Dima 已提交
46 47 48
	private _onBlur = this._register(new Emitter<void>());
	public readonly onBlur: Event<void> = this._onBlur.event;

49 50
	constructor(parent: HTMLElement, private options: IInputOptions,
		@IContextViewService private contextViewService: IContextViewService,
B
Benjamin Pasero 已提交
51 52
		@IKeybindingService private keybindingService: IKeybindingService,
		@IThemeService themeService: IThemeService
53 54 55
	) {
		super();
		this.inputBox = this._register(new InputBox(parent, this.contextViewService, this.options));
B
Benjamin Pasero 已提交
56
		this._register(attachInputBoxStyler(this.inputBox, themeService));
57
		this.onkeydown(this.inputBox.inputElement, e => this._onKeyDown(e));
A
Alex Dima 已提交
58 59 60 61 62 63 64
		this.onblur(this.inputBox.inputElement, (e) => this._onBlur.fire());

		this.oninput(this.inputBox.inputElement, (e) => {
			// Prevent other characters from showing up
			this.setInputValue(this._inputValue);
		});

65 66 67 68 69
		this._acceptChords = true;
		this._firstPart = null;
		this._chordPart = null;
	}

A
Alex Dima 已提交
70 71 72 73 74 75 76 77 78
	public setInputValue(value: string): void {
		this._inputValue = value;
		this.inputBox.value = this._inputValue;
	}

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

S
Sandeep Somavarapu 已提交
79 80 81 82 83
	public reset() {
		this._firstPart = null;
		this._chordPart = null;
	}

84
	private _onKeyDown(keyboardEvent: IKeyboardEvent): void {
85 86
		keyboardEvent.preventDefault();
		keyboardEvent.stopPropagation();
A
Alex Dima 已提交
87 88 89 90 91 92 93
		if (keyboardEvent.equals(KeyCode.Enter)) {
			this._onEnter.fire();
			return;
		}
		if (keyboardEvent.equals(KeyCode.Escape)) {
			this._onEscape.fire();
			return;
94 95 96 97 98
		}
		this.printKeybinding(keyboardEvent);
	}

	private printKeybinding(keyboardEvent: IKeyboardEvent): void {
99
		const keybinding = this.keybindingService.resolveKeyboardEvent(keyboardEvent);
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
		const info = `code: ${keyboardEvent.browserEvent.code}, keyCode: ${keyboardEvent.browserEvent.keyCode}, key: ${keyboardEvent.browserEvent.key} => UI: ${keybinding.getAriaLabel()}, user settings: ${keybinding.getUserSettingsLabel()}, dispatch: ${keybinding.getDispatchParts()[0]}`;

		if (this._acceptChords) {
			const hasFirstPart = (this._firstPart && this._firstPart.getDispatchParts()[0] !== null);
			const hasChordPart = (this._chordPart && this._chordPart.getDispatchParts()[0] !== null);
			if (hasFirstPart && hasChordPart) {
				// Reset
				this._firstPart = keybinding;
				this._chordPart = null;
			} else if (!hasFirstPart) {
				this._firstPart = keybinding;
			} else {
				this._chordPart = keybinding;
			}
		} else {
			this._firstPart = keybinding;
		}

		let value = '';
		if (this._firstPart) {
			value = this._firstPart.getUserSettingsLabel();
		}
		if (this._chordPart) {
			value = value + ' ' + this._chordPart.getUserSettingsLabel();
		}
A
Alex Dima 已提交
125
		this.setInputValue(value);
126 127 128

		this.inputBox.inputElement.title = info;
		this._onKeybinding.fire([this._firstPart, this._chordPart]);
129 130 131 132 133
	}
}

export class DefineKeybindingWidget extends Widget {

134 135
	private static readonly WIDTH = 400;
	private static readonly HEIGHT = 90;
136 137 138 139 140

	private _domNode: FastDomNode<HTMLElement>;
	private _keybindingInputWidget: KeybindingInputWidget;
	private _outputNode: HTMLElement;

141 142
	private _firstPart: ResolvedKeybinding = null;
	private _chordPart: ResolvedKeybinding = null;
143 144 145 146
	private _isVisible: boolean = false;

	private _onHide = this._register(new Emitter<void>());

147 148 149
	private _onDidChange = this._register(new Emitter<[ResolvedKeybinding, ResolvedKeybinding]>());
	public onDidChange: Event<[ResolvedKeybinding, ResolvedKeybinding]> = this._onDidChange.event;

150 151 152 153
	constructor(
		parent: HTMLElement,
		@IInstantiationService private instantiationService: IInstantiationService,
		@IThemeService private themeService: IThemeService
154 155
	) {
		super();
156 157 158 159 160 161 162 163
		this.create();
		if (parent) {
			dom.append(parent, this._domNode.domNode);
		}
	}

	get domNode(): HTMLElement {
		return this._domNode.domNode;
164 165 166
	}

	define(): TPromise<string> {
S
Sandeep Somavarapu 已提交
167
		this._keybindingInputWidget.reset();
168
		return new TPromise<string>((c, e) => {
169 170 171 172
			if (!this._isVisible) {
				this._isVisible = true;
				this._domNode.setDisplay('block');

173 174
				this._firstPart = null;
				this._chordPart = null;
A
Alex Dima 已提交
175
				this._keybindingInputWidget.setInputValue('');
176
				dom.clearNode(this._outputNode);
A
Alex Dima 已提交
177
				this._keybindingInputWidget.focus();
178 179
			}
			const disposable = this._onHide.event(() => {
180 181 182 183 184 185
				if (this._firstPart) {
					let r = this._firstPart.getUserSettingsLabel();
					if (this._chordPart) {
						r = r + ' ' + this._chordPart.getUserSettingsLabel();
					}
					c(r);
186 187 188 189 190 191 192 193
				} else {
					c(null);
				}
				disposable.dispose();
			});
		});
	}

194
	layout(layout: dom.Dimension): void {
195 196 197 198 199 200 201
		let top = Math.round((layout.height - DefineKeybindingWidget.HEIGHT) / 2);
		this._domNode.setTop(top);

		let left = Math.round((layout.width - DefineKeybindingWidget.WIDTH) / 2);
		this._domNode.setLeft(left);
	}

202
	private create(): void {
203 204 205 206 207
		this._domNode = createFastDomNode(document.createElement('div'));
		this._domNode.setDisplay('none');
		this._domNode.setClassName('defineKeybindingWidget');
		this._domNode.setWidth(DefineKeybindingWidget.WIDTH);
		this._domNode.setHeight(DefineKeybindingWidget.HEIGHT);
S
Sandeep Somavarapu 已提交
208
		dom.append(this._domNode.domNode, dom.$('.message', null, nls.localize('defineKeybinding.initial', "Press desired key combination and then press ENTER.")));
209

210
		this._register(attachStylerCallback(this.themeService, { editorWidgetBackground, widgetShadow }, colors => {
211 212 213 214 215
			if (colors.editorWidgetBackground) {
				this._domNode.domNode.style.backgroundColor = colors.editorWidgetBackground.toString();
			} else {
				this._domNode.domNode.style.backgroundColor = null;
			}
216 217 218 219 220 221 222 223

			if (colors.widgetShadow) {
				this._domNode.domNode.style.boxShadow = `0 2px 8px ${colors.widgetShadow}`;
			} else {
				this._domNode.domNode.style.boxShadow = null;
			}
		}));

224
		this._keybindingInputWidget = this._register(this.instantiationService.createInstance(KeybindingInputWidget, this._domNode.domNode, {}));
225
		this._register(this._keybindingInputWidget.onKeybinding(keybinding => { this.printKeybinding(keybinding); this._onDidChange.fire(keybinding); }));
226 227
		this._register(this._keybindingInputWidget.onEnter(() => this.hide()));
		this._register(this._keybindingInputWidget.onEscape(() => this.onCancel()));
A
Alex Dima 已提交
228
		this._register(this._keybindingInputWidget.onBlur(() => this.onCancel()));
229

230
		this._outputNode = dom.append(this._domNode.domNode, dom.$('.output'));
231 232
	}

233 234 235 236
	private printKeybinding(keybinding: [ResolvedKeybinding, ResolvedKeybinding]): void {
		const [firstPart, chordPart] = keybinding;
		this._firstPart = firstPart;
		this._chordPart = chordPart;
237
		dom.clearNode(this._outputNode);
238
		new KeybindingLabel(this._outputNode, OS).set(this._firstPart, null);
239
		if (this._chordPart) {
240 241
			this._outputNode.appendChild(document.createTextNode(nls.localize('defineKeybinding.chordsTo', "chord to")));
			new KeybindingLabel(this._outputNode, OS).set(this._chordPart, null);
242
		}
243 244
	}

245 246 247 248 249 250 251 252 253 254 255
	public printConflicts(numConflicts: number): void {
		let outputString: string = nls.localize('defineKeybinding.conflicts', "Conflict(s)");
		if (numConflicts > 0) {
			outputString = '(' + numConflicts + ' ' + outputString + ')';
		}
		else {
			outputString = '';
		}
		this._outputNode.appendChild(document.createTextNode(outputString));
	}

256
	private onCancel(): void {
257 258
		this._firstPart = null;
		this._chordPart = null;
259 260 261 262
		this.hide();
	}

	private hide(): void {
263
		this._domNode.setDisplay('none');
264 265 266
		this._isVisible = false;
		this._onHide.fire();
	}
267 268 269 270
}

export class DefineKeybindingOverlayWidget extends Disposable implements IOverlayWidget {

271
	private static readonly ID = 'editor.contrib.defineKeybindingWidget';
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303

	private readonly _widget: DefineKeybindingWidget;

	constructor(private _editor: ICodeEditor,
		@IInstantiationService instantiationService: IInstantiationService
	) {
		super();

		this._widget = instantiationService.createInstance(DefineKeybindingWidget, null);
		this._editor.addOverlayWidget(this);
	}

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

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

	public getPosition(): IOverlayWidgetPosition {
		return {
			preference: null
		};
	}

	public dispose(): void {
		this._editor.removeOverlayWidget(this);
		super.dispose();
	}

	public start(): TPromise<string> {
304
		this._editor.revealPositionInCenterIfOutsideViewport(this._editor.getPosition(), ScrollType.Smooth);
305
		const layoutInfo = this._editor.getLayoutInfo();
306
		this._widget.layout(new dom.Dimension(layoutInfo.width, layoutInfo.height));
307 308
		return this._widget.define();
	}
A
Alex Dima 已提交
309
}