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 <amarlenkyzy@microsoft.com>
This commit is contained in:
Alexandru Dima
2025-03-04 17:18:57 +01:00
committed by GitHub
parent ba2edd3016
commit 4537390895
2 changed files with 20 additions and 9 deletions
@@ -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 {
@@ -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 {