33 * Licensed under the MIT License. See License.txt in the project root for license information.
44 *--------------------------------------------------------------------------------------------*/
55
6- import { addDisposableListener , getActiveWindow } from '../../../../../base/browser/dom.js' ;
6+ import { addDisposableListener , getActiveElement , getShadowRoot } from '../../../../../base/browser/dom.js' ;
77import { IDisposable , Disposable } from '../../../../../base/common/lifecycle.js' ;
88
99export interface ITypeData {
@@ -21,8 +21,12 @@ export class FocusTracker extends Disposable {
2121 private readonly _onFocusChange : ( newFocusValue : boolean ) => void ,
2222 ) {
2323 super ( ) ;
24- this . _register ( addDisposableListener ( this . _domNode , 'focus' , ( ) => this . _handleFocusedChanged ( true ) ) ) ;
25- this . _register ( addDisposableListener ( this . _domNode , 'blur' , ( ) => this . _handleFocusedChanged ( false ) ) ) ;
24+ this . _register ( addDisposableListener ( this . _domNode , 'focus' , ( ) => {
25+ this . refreshFocusState ( ) ;
26+ } ) ) ;
27+ this . _register ( addDisposableListener ( this . _domNode , 'blur' , ( ) => {
28+ this . _handleFocusedChanged ( false ) ;
29+ } ) ) ;
2630 }
2731
2832 private _handleFocusedChanged ( focused : boolean ) : void {
@@ -34,15 +38,19 @@ export class FocusTracker extends Disposable {
3438 }
3539
3640 public focus ( ) : void {
37- // fixes: https://github.com/microsoft/vscode/issues/228147
38- // Immediately call this method in order to directly set the field isFocused to true so the textInputFocus context key is evaluated correctly
39- this . _handleFocusedChanged ( true ) ;
4041 this . _domNode . focus ( ) ;
42+ this . refreshFocusState ( ) ;
4143 }
4244
4345 public refreshFocusState ( ) : void {
44- const focused = this . _domNode === getActiveWindow ( ) . document . activeElement ;
45- this . _handleFocusedChanged ( focused ) ;
46+ let activeElement : Element | null = null ;
47+ const shadowRoot = getShadowRoot ( this . _domNode ) ;
48+ if ( shadowRoot ) {
49+ activeElement = shadowRoot . activeElement ;
50+ } else {
51+ activeElement = getActiveElement ( ) ;
52+ }
53+ this . _handleFocusedChanged ( activeElement === this . _domNode ) ;
4654 }
4755
4856 get isFocused ( ) : boolean {
0 commit comments