From 4537390895ad644093b255b5f8f66f10b294b324 Mon Sep 17 00:00:00 2001 From: Alexandru Dima Date: Tue, 4 Mar 2025 17:18:57 +0100 Subject: [PATCH] Add a workaround for browser bug (#242459) * Add a workaround for browser bug exposed by the steps at #12384 * refresh focus state before returning whether focused or not --------- Co-authored-by: Aiday Marlen Kyzy --- .../editContext/native/nativeEditContext.ts | 3 ++- .../native/nativeEditContextUtils.ts | 26 +++++++++++++------ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/vs/editor/browser/controller/editContext/native/nativeEditContext.ts b/src/vs/editor/browser/controller/editContext/native/nativeEditContext.ts index 76a5bd52fe5..cee5e3f9df9 100644 --- a/src/vs/editor/browser/controller/editContext/native/nativeEditContext.ts +++ b/src/vs/editor/browser/controller/editContext/native/nativeEditContext.ts @@ -258,7 +258,8 @@ export class NativeEditContext extends AbstractEditContext { } public isFocused(): boolean { - return this._focusTracker.isFocused || (getActiveWindow().document.activeElement === this.textArea.domNode); + this.refreshFocusState(); + return this._focusTracker.isFocused; } public focus(): void { diff --git a/src/vs/editor/browser/controller/editContext/native/nativeEditContextUtils.ts b/src/vs/editor/browser/controller/editContext/native/nativeEditContextUtils.ts index b3166de0437..4c4667b613c 100644 --- a/src/vs/editor/browser/controller/editContext/native/nativeEditContextUtils.ts +++ b/src/vs/editor/browser/controller/editContext/native/nativeEditContextUtils.ts @@ -3,7 +3,7 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { addDisposableListener, getActiveWindow } from '../../../../../base/browser/dom.js'; +import { addDisposableListener, getActiveElement, getShadowRoot } from '../../../../../base/browser/dom.js'; import { IDisposable, Disposable } from '../../../../../base/common/lifecycle.js'; export interface ITypeData { @@ -21,8 +21,12 @@ export class FocusTracker extends Disposable { private readonly _onFocusChange: (newFocusValue: boolean) => void, ) { super(); - this._register(addDisposableListener(this._domNode, 'focus', () => this._handleFocusedChanged(true))); - this._register(addDisposableListener(this._domNode, 'blur', () => this._handleFocusedChanged(false))); + this._register(addDisposableListener(this._domNode, 'focus', () => { + this.refreshFocusState(); + })); + this._register(addDisposableListener(this._domNode, 'blur', () => { + this._handleFocusedChanged(false); + })); } private _handleFocusedChanged(focused: boolean): void { @@ -34,15 +38,21 @@ export class FocusTracker extends Disposable { } public focus(): void { - // fixes: https://github.com/microsoft/vscode/issues/228147 - // Immediately call this method in order to directly set the field isFocused to true so the textInputFocus context key is evaluated correctly - this._handleFocusedChanged(true); this._domNode.focus(); + this.refreshFocusState(); } public refreshFocusState(): void { - const focused = this._domNode === getActiveWindow().document.activeElement; - this._handleFocusedChanged(focused); + + let activeElement: Element | null = null; + const shadowRoot = getShadowRoot(this._domNode); + if (shadowRoot) { + activeElement = shadowRoot.activeElement; + } else { + activeElement = getActiveElement(); + } + + this._handleFocusedChanged(activeElement === this._domNode); } get isFocused(): boolean {