@@ -16,27 +16,28 @@ import { IKeyboardEvent } from 'vs/platform/keybinding/common/keybinding';
1616import { KeyCodeUtils , KeyCode } from 'vs/base/common/keyCodes' ;
1717import { IMacLinuxKeyboardMapping , MacLinuxKeyboardMapper } from 'vs/workbench/services/keybinding/common/macLinuxKeyboardMapper' ;
1818import { StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent' ;
19-
2019import { KeyboardLayoutProvider } from 'vs/workbench/services/keybinding/browser/keyboardLayoutProvider' ;
2120
22- export class BrowserKeymap {
23- public static readonly INSTANCE : BrowserKeymap = new BrowserKeymap ( ) ;
24-
25- private readonly _onDidChangeKeyboardLayout = new Emitter < void > ( ) ;
26- public readonly onDidChangeKeyboardLayout : Event < void > = this . _onDidChangeKeyboardLayout . event ;
27-
28- private readonly _onDidInitialized = new Emitter < void > ( ) ;
29- public readonly onDidInitialized : Event < void > = this . _onDidInitialized . event ;
30-
21+ export class BrowserKeyboardMapperFactory {
22+ public static readonly INSTANCE = new BrowserKeyboardMapperFactory ( ) ;
23+ private _layoutInfo : IKeyboardLayoutInfo | null ;
24+ private _rawMapping : IKeyboardMapping | null ;
25+ private _keyboardMapper : IKeyboardMapper | null ;
3126 private _initialized : boolean ;
27+ private readonly _onDidChangeKeyboardMapper = new Emitter < void > ( ) ;
28+ public readonly onDidChangeKeyboardMapper : Event < void > = this . _onDidChangeKeyboardMapper . event ;
3229
3330 private constructor ( ) {
31+ this . _layoutInfo = null ;
32+ this . _rawMapping = null ;
33+ this . _keyboardMapper = null ;
3434 this . _initialized = false ;
35+
3536 const platform = isWindows ? 'win' : isMacintosh ? 'darwin' : 'linux' ;
3637
3738 import ( 'vs/workbench/services/keybinding/browser/keyboardlayouts/layout.contribution.' + platform ) . then ( ( ) => {
3839 this . _initialized = true ;
39- this . _onDidInitialized . fire ( ) ;
40+ this . _onKeyboardLayoutChanged ( ) ;
4041 } ) ;
4142
4243 if ( ( navigator as any ) . keyboard && ( navigator as any ) . keyboard . addEventListener ) {
@@ -47,15 +48,62 @@ export class BrowserKeymap {
4748 return ;
4849 }
4950
50- this . _onDidChangeKeyboardLayout . fire ( ) ;
51+ this . _onKeyboardLayoutChanged ( ) ;
5152 } ) ;
5253 } ) ;
5354 }
55+ }
5456
57+ private _onKeyboardLayoutChanged ( ) : void {
58+ this . _updateKeyboardLayoutAsync ( this . _initialized ) ;
59+ }
5560
61+ private _updateKeyboardLayoutAsync ( initialized : boolean ) {
62+ if ( ! initialized ) {
63+ return ;
64+ }
65+
66+ this . getBrowserKeyMap ( ) . then ( keyMap => {
67+ KeyboardLayoutProvider . INSTANCE . setActive ( keyMap ) ;
68+ let currentKeyboardLayout = KeyboardLayoutProvider . INSTANCE . activeKeyboardLayout . layout ;
69+ this . _setKeyboardData ( currentKeyboardLayout , keyMap ) ;
70+ } ) ;
5671 }
5772
58- validateCurrentKeyboardMapping ( keyboardEvent : IKeyboardEvent ) : boolean {
73+ public getKeyboardMapper ( dispatchConfig : DispatchConfig ) : IKeyboardMapper {
74+ if ( ! this . _initialized ) {
75+ return new MacLinuxFallbackKeyboardMapper ( OS ) ;
76+ }
77+ if ( dispatchConfig === DispatchConfig . KeyCode ) {
78+ // Forcefully set to use keyCode
79+ return new MacLinuxFallbackKeyboardMapper ( OS ) ;
80+ }
81+ return this . _keyboardMapper ! ;
82+
83+ }
84+
85+ public getCurrentKeyboardLayout ( ) : IKeyboardLayoutInfo | null {
86+ if ( ! this . _initialized ) {
87+ return null ;
88+ }
89+ return this . _layoutInfo ;
90+ }
91+
92+ public validateCurrentKeyboardMapping ( keyboardEvent : IKeyboardEvent ) : void {
93+ if ( ! this . _initialized ) {
94+ return ;
95+ }
96+
97+ let isCurrentKeyboard = this . _validateCurrentKeyboardMapping ( keyboardEvent ) ;
98+
99+ if ( isCurrentKeyboard ) {
100+ return ;
101+ }
102+
103+ this . _updateKeyboardLayoutAsync ( true ) ;
104+ }
105+
106+ private _validateCurrentKeyboardMapping ( keyboardEvent : IKeyboardEvent ) : boolean {
59107 if ( ! this . _initialized ) {
60108 return true ;
61109 }
@@ -77,7 +125,7 @@ export class BrowserKeymap {
77125 return ;
78126 }
79127
80- this . _onDidChangeKeyboardLayout . fire ( ) ;
128+ this . _onKeyboardLayoutChanged ( ) ;
81129 } ) ;
82130 } , 350 ) ;
83131 }
@@ -104,113 +152,6 @@ export class BrowserKeymap {
104152 return true ;
105153 }
106154
107- getCurrentKeyboardLayout ( ) {
108- if ( ! this . _initialized ) {
109- return null ;
110- }
111-
112- return KeyboardLayoutProvider . INSTANCE . activeKeyboardLayout . layout ;
113- }
114-
115- async getBrowserKeyMap ( ) {
116- if ( ( navigator as any ) . keyboard ) {
117- return ( navigator as any ) . keyboard . getLayoutMap ( ) . then ( ( e : any ) => {
118- let ret : IKeyboardMapping = { } ;
119- for ( let key of e ) {
120- ret [ key [ 0 ] ] = {
121- 'value' : key [ 1 ] ,
122- 'withShift' : '' ,
123- 'withAltGr' : '' ,
124- 'withShiftAltGr' : ''
125- } ;
126- }
127-
128- return KeyboardLayoutProvider . INSTANCE . getMatchedKeyboardLayout ( ret ) . value ;
129- } ) ;
130- }
131-
132- return { } ;
133- }
134- }
135-
136- export class BrowserKeyboardMapperFactory {
137- public static readonly INSTANCE = new BrowserKeyboardMapperFactory ( ) ;
138- private _layoutInfo : IKeyboardLayoutInfo | null ;
139- private _rawMapping : IKeyboardMapping | null ;
140- private _keyboardMapper : IKeyboardMapper | null ;
141- private _initialized : boolean ;
142- private readonly _onDidChangeKeyboardMapper = new Emitter < void > ( ) ;
143- public readonly onDidChangeKeyboardMapper : Event < void > = this . _onDidChangeKeyboardMapper . event ;
144-
145- private readonly _onDidInitialized = new Emitter < void > ( ) ;
146- public readonly onDidInitialized : Event < void > = this . _onDidInitialized . event ;
147-
148- private constructor ( ) {
149- this . _layoutInfo = null ;
150- this . _rawMapping = null ;
151- this . _keyboardMapper = null ;
152- this . _initialized = false ;
153-
154- BrowserKeymap . INSTANCE . onDidInitialized ( ( ) => {
155- this . _initialized = true ;
156- this . _onKeyboardLayoutChanged ( ) ;
157- } ) ;
158-
159- BrowserKeymap . INSTANCE . onDidChangeKeyboardLayout ( ( ) => {
160- this . _onKeyboardLayoutChanged ( ) ;
161- } ) ;
162- }
163-
164- private _onKeyboardLayoutChanged ( ) : void {
165- if ( this . _initialized ) {
166- this . updateKeyboardLayoutAsync ( true ) ;
167- }
168- }
169-
170- private updateKeyboardLayoutAsync ( initialized : boolean ) {
171- if ( ! initialized ) {
172- return ;
173- }
174-
175- BrowserKeymap . INSTANCE . getBrowserKeyMap ( ) . then ( keyMap => {
176- KeyboardLayoutProvider . INSTANCE . setActive ( keyMap ) ;
177- this . _setKeyboardData ( BrowserKeymap . INSTANCE . getCurrentKeyboardLayout ( ) ! , keyMap ) ;
178- } ) ;
179- }
180-
181- public getKeyboardMapper ( dispatchConfig : DispatchConfig ) : IKeyboardMapper {
182- if ( ! this . _initialized ) {
183- return new MacLinuxFallbackKeyboardMapper ( OS ) ;
184- }
185- if ( dispatchConfig === DispatchConfig . KeyCode ) {
186- // Forcefully set to use keyCode
187- return new MacLinuxFallbackKeyboardMapper ( OS ) ;
188- }
189- return this . _keyboardMapper ! ;
190-
191- }
192-
193- public getCurrentKeyboardLayout ( ) : IKeyboardLayoutInfo | null {
194- if ( ! this . _initialized ) {
195- return null ;
196- }
197- return this . _layoutInfo ;
198- }
199-
200- public validateCurrentKeyboardMapping ( keyboardEvent : IKeyboardEvent ) : void {
201- if ( ! this . _initialized ) {
202- return ;
203- }
204-
205- let isCurrentKeyboard = BrowserKeymap . INSTANCE . validateCurrentKeyboardMapping ( keyboardEvent ) ;
206-
207- if ( isCurrentKeyboard ) {
208- return ;
209- }
210-
211- this . updateKeyboardLayoutAsync ( true ) ;
212- }
213-
214155 public getRawKeyboardMapping ( ) : IKeyboardMapping | null {
215156 if ( ! this . _initialized ) {
216157 return null ;
@@ -252,6 +193,26 @@ export class BrowserKeyboardMapperFactory {
252193
253194 return new MacLinuxKeyboardMapper ( isUSStandard , < IMacLinuxKeyboardMapping > rawMapping , OS ) ;
254195 }
196+
197+ async getBrowserKeyMap ( ) {
198+ if ( ( navigator as any ) . keyboard ) {
199+ return ( navigator as any ) . keyboard . getLayoutMap ( ) . then ( ( e : any ) => {
200+ let ret : IKeyboardMapping = { } ;
201+ for ( let key of e ) {
202+ ret [ key [ 0 ] ] = {
203+ 'value' : key [ 1 ] ,
204+ 'withShift' : '' ,
205+ 'withAltGr' : '' ,
206+ 'withShiftAltGr' : ''
207+ } ;
208+ }
209+
210+ return KeyboardLayoutProvider . INSTANCE . getMatchedKeyboardLayout ( ret ) . value ;
211+ } ) ;
212+ }
213+
214+ return { } ;
215+ }
255216}
256217
257218
0 commit comments