@@ -7,14 +7,13 @@ import 'vs/css!./media/keybindings';
77import * as nls from 'vs/nls' ;
88import { OS } from 'vs/base/common/platform' ;
99import { TPromise } from 'vs/base/common/winjs.base' ;
10- import { Disposable } from 'vs/base/common/lifecycle' ;
10+ import { Disposable , dispose , toDisposable , IDisposable } from 'vs/base/common/lifecycle' ;
1111import { Event , Emitter } from 'vs/base/common/event' ;
1212import { KeybindingLabel } from 'vs/base/browser/ui/keybindingLabel/keybindingLabel' ;
1313import { Widget } from 'vs/base/browser/ui/widget' ;
1414import { ResolvedKeybinding , KeyCode } from 'vs/base/common/keyCodes' ;
1515import * as dom from 'vs/base/browser/dom' ;
16- import { InputBox , IInputOptions } from 'vs/base/browser/ui/inputbox/inputBox' ;
17- import { IKeyboardEvent } from 'vs/base/browser/keyboardEvent' ;
16+ import { IKeyboardEvent , StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent' ;
1817import { FastDomNode , createFastDomNode } from 'vs/base/browser/fastDomNode' ;
1918import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding' ;
2019import { IContextViewService } from 'vs/platform/contextview/browser/contextView' ;
@@ -24,16 +23,20 @@ import { attachInputBoxStyler, attachStylerCallback } from 'vs/platform/theme/co
2423import { IThemeService } from 'vs/platform/theme/common/themeService' ;
2524import { editorWidgetBackground , widgetShadow } from 'vs/platform/theme/common/colorRegistry' ;
2625import { ScrollType } from 'vs/editor/common/editorCommon' ;
26+ import { SearchWidget , SearchOptions } from 'vs/workbench/parts/preferences/browser/preferencesWidgets' ;
2727
28- class KeybindingInputWidget extends Widget {
28+ export interface KeybindingsSearchOptions extends SearchOptions {
29+ recordEnter ?: boolean ;
30+ }
2931
30- private readonly inputBox : InputBox ;
32+ export class KeybindingsSearchWidget extends SearchWidget {
3133
32- private _acceptChords : boolean ;
3334 private _firstPart : ResolvedKeybinding ;
3435 private _chordPart : ResolvedKeybinding ;
3536 private _inputValue : string ;
3637
38+ private recordDisposables : IDisposable [ ] = [ ] ;
39+
3740 private _onKeybinding = this . _register ( new Emitter < [ ResolvedKeybinding , ResolvedKeybinding ] > ( ) ) ;
3841 public readonly onKeybinding : Event < [ ResolvedKeybinding , ResolvedKeybinding ] > = this . _onKeybinding . event ;
3942
@@ -46,25 +49,36 @@ class KeybindingInputWidget extends Widget {
4649 private _onBlur = this . _register ( new Emitter < void > ( ) ) ;
4750 public readonly onBlur : Event < void > = this . _onBlur . event ;
4851
49- constructor ( parent : HTMLElement , private options : IInputOptions ,
50- @IContextViewService private contextViewService : IContextViewService ,
52+ constructor ( parent : HTMLElement , options : SearchOptions ,
53+ @IContextViewService contextViewService : IContextViewService ,
5154 @IKeybindingService private keybindingService : IKeybindingService ,
55+ @IInstantiationService instantiationService : IInstantiationService ,
5256 @IThemeService themeService : IThemeService
5357 ) {
54- super ( ) ;
55- this . inputBox = this . _register ( new InputBox ( parent , this . contextViewService , this . options ) ) ;
58+ super ( parent , options , contextViewService , instantiationService , themeService ) ;
5659 this . _register ( attachInputBoxStyler ( this . inputBox , themeService ) ) ;
57- this . onkeydown ( this . inputBox . inputElement , e => this . _onKeyDown ( e ) ) ;
58- this . onblur ( this . inputBox . inputElement , ( e ) => this . _onBlur . fire ( ) ) ;
60+ this . _register ( toDisposable ( ( ) => this . stopRecordingKeys ( ) ) ) ;
61+
62+ this . _reset ( ) ;
63+ }
5964
60- this . oninput ( this . inputBox . inputElement , ( e ) => {
65+ clear ( ) : void {
66+ this . _reset ( ) ;
67+ super . clear ( ) ;
68+ }
69+
70+ startRecordingKeys ( ) : void {
71+ this . recordDisposables . push ( dom . addDisposableListener ( this . inputBox . inputElement , dom . EventType . KEY_DOWN , ( e : KeyboardEvent ) => this . _onKeyDown ( new StandardKeyboardEvent ( e ) ) ) ) ;
72+ this . recordDisposables . push ( dom . addDisposableListener ( this . inputBox . inputElement , dom . EventType . BLUR , ( ) => this . _onBlur . fire ( ) ) ) ;
73+ this . recordDisposables . push ( dom . addDisposableListener ( this . inputBox . inputElement , dom . EventType . INPUT , ( ) => {
6174 // Prevent other characters from showing up
6275 this . setInputValue ( this . _inputValue ) ;
63- } ) ;
76+ } ) ) ;
77+ }
6478
65- this . _acceptChords = true ;
66- this . _firstPart = null ;
67- this . _chordPart = null ;
79+ stopRecordingKeys ( ) : void {
80+ this . _reset ( ) ;
81+ dispose ( this . recordDisposables ) ;
6882 }
6983
7084 public setInputValue ( value : string ) : void {
@@ -76,15 +90,16 @@ class KeybindingInputWidget extends Widget {
7690 this . inputBox . focus ( ) ;
7791 }
7892
79- public reset ( ) {
93+ private _reset ( ) {
8094 this . _firstPart = null ;
8195 this . _chordPart = null ;
8296 }
8397
8498 private _onKeyDown ( keyboardEvent : IKeyboardEvent ) : void {
8599 keyboardEvent . preventDefault ( ) ;
86100 keyboardEvent . stopPropagation ( ) ;
87- if ( keyboardEvent . equals ( KeyCode . Enter ) ) {
101+ const options = this . options as KeybindingsSearchOptions ;
102+ if ( ! options . recordEnter && keyboardEvent . equals ( KeyCode . Enter ) ) {
88103 this . _onEnter . fire ( ) ;
89104 return ;
90105 }
@@ -99,20 +114,16 @@ class KeybindingInputWidget extends Widget {
99114 const keybinding = this . keybindingService . resolveKeyboardEvent ( keyboardEvent ) ;
100115 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 ] } ` ;
101116
102- if ( this . _acceptChords ) {
103- const hasFirstPart = ( this . _firstPart && this . _firstPart . getDispatchParts ( ) [ 0 ] !== null ) ;
104- const hasChordPart = ( this . _chordPart && this . _chordPart . getDispatchParts ( ) [ 0 ] !== null ) ;
105- if ( hasFirstPart && hasChordPart ) {
106- // Reset
107- this . _firstPart = keybinding ;
108- this . _chordPart = null ;
109- } else if ( ! hasFirstPart ) {
110- this . _firstPart = keybinding ;
111- } else {
112- this . _chordPart = keybinding ;
113- }
114- } else {
117+ const hasFirstPart = ( this . _firstPart && this . _firstPart . getDispatchParts ( ) [ 0 ] !== null ) ;
118+ const hasChordPart = ( this . _chordPart && this . _chordPart . getDispatchParts ( ) [ 0 ] !== null ) ;
119+ if ( hasFirstPart && hasChordPart ) {
120+ // Reset
115121 this . _firstPart = keybinding ;
122+ this . _chordPart = null ;
123+ } else if ( ! hasFirstPart ) {
124+ this . _firstPart = keybinding ;
125+ } else {
126+ this . _chordPart = keybinding ;
116127 }
117128
118129 let value = '' ;
@@ -135,7 +146,7 @@ export class DefineKeybindingWidget extends Widget {
135146 private static readonly HEIGHT = 110 ;
136147
137148 private _domNode : FastDomNode < HTMLElement > ;
138- private _keybindingInputWidget : KeybindingInputWidget ;
149+ private _keybindingInputWidget : KeybindingsSearchWidget ;
139150 private _outputNode : HTMLElement ;
140151 private _showExistingKeybindingsNode : HTMLElement ;
141152
@@ -168,7 +179,7 @@ export class DefineKeybindingWidget extends Widget {
168179 }
169180
170181 define ( ) : TPromise < string > {
171- this . _keybindingInputWidget . reset ( ) ;
182+ this . _keybindingInputWidget . clear ( ) ;
172183 return new TPromise < string > ( ( c , e ) => {
173184 if ( ! this . _isVisible ) {
174185 this . _isVisible = true ;
@@ -232,7 +243,8 @@ export class DefineKeybindingWidget extends Widget {
232243 }
233244 } ) ) ;
234245
235- this . _keybindingInputWidget = this . _register ( this . instantiationService . createInstance ( KeybindingInputWidget , this . _domNode . domNode , { ariaLabel : message } ) ) ;
246+ this . _keybindingInputWidget = this . _register ( this . instantiationService . createInstance ( KeybindingsSearchWidget , this . _domNode . domNode , { ariaLabel : message } ) ) ;
247+ this . _keybindingInputWidget . startRecordingKeys ( ) ;
236248 this . _register ( this . _keybindingInputWidget . onKeybinding ( keybinding => this . onKeybinding ( keybinding ) ) ) ;
237249 this . _register ( this . _keybindingInputWidget . onEnter ( ( ) => this . hide ( ) ) ) ;
238250 this . _register ( this . _keybindingInputWidget . onEscape ( ( ) => this . onCancel ( ) ) ) ;
0 commit comments