Skip to content

Commit

Permalink
aux window - reduce use of global document and prefer ownerDocument
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Oct 17, 2023
1 parent e9b6a77 commit f6bc90d
Show file tree
Hide file tree
Showing 48 changed files with 112 additions and 85 deletions.
2 changes: 1 addition & 1 deletion src/vs/base/browser/ui/breadcrumbs/breadcrumbsWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export class BreadcrumbsWidget {
}

isDOMFocused(): boolean {
let candidate = document.activeElement;
let candidate = this._domNode.ownerDocument.activeElement;
while (candidate) {
if (this._domNode === candidate) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/vs/base/browser/ui/button/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ export class Button extends Disposable implements IButton {
}

hasFocus(): boolean {
return this._element === document.activeElement;
return this._element === this._element.ownerDocument.activeElement;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/vs/base/browser/ui/contextview/contextview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ export class ContextView extends Disposable {
private onDOMEvent(e: Event, onCapture: boolean): void {
if (this.delegate) {
if (this.delegate.onDOMEvent) {
this.delegate.onDOMEvent(e, <HTMLElement>document.activeElement);
this.delegate.onDOMEvent(e, <HTMLElement>this.view.ownerDocument.activeElement);
} else if (onCapture && !DOM.isAncestor(<HTMLElement>e.target, this.container)) {
this.hide();
}
Expand Down
6 changes: 3 additions & 3 deletions src/vs/base/browser/ui/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,7 @@ export class Dialog extends Disposable {
}

async show(): Promise<IDialogResult> {
const window = getWindow(this.container);
this.focusToReturn = window.document.activeElement as HTMLElement;
this.focusToReturn = this.container.ownerDocument.activeElement as HTMLElement;

return new Promise<IDialogResult>((resolve) => {
clearNode(this.buttonsContainer);
Expand Down Expand Up @@ -229,6 +228,7 @@ export class Dialog extends Disposable {
});

// Handle keyboard events globally: Tab, Arrow-Left/Right
const window = getWindow(this.container);
this._register(addDisposableListener(window, 'keydown', e => {
const evt = new StandardKeyboardEvent(e);

Expand Down Expand Up @@ -269,7 +269,7 @@ export class Dialog extends Disposable {
const links = this.messageContainer.querySelectorAll('a');
for (const link of links) {
focusableElements.push(link);
if (link === document.activeElement) {
if (link === link.ownerDocument.activeElement) {
focusedIndex = focusableElements.length - 1;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/vs/base/browser/ui/findinput/findInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export class FindInput extends Widget {
const indexes = [this.caseSensitive.domNode, this.wholeWords.domNode, this.regex.domNode];
this.onkeydown(this.domNode, (event: IKeyboardEvent) => {
if (event.equals(KeyCode.LeftArrow) || event.equals(KeyCode.RightArrow) || event.equals(KeyCode.Escape)) {
const index = indexes.indexOf(<HTMLElement>document.activeElement);
const index = indexes.indexOf(<HTMLElement>this.domNode.ownerDocument.activeElement);
if (index >= 0) {
let newIndex: number = -1;
if (event.equals(KeyCode.RightArrow)) {
Expand Down
2 changes: 1 addition & 1 deletion src/vs/base/browser/ui/findinput/replaceInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export class ReplaceInput extends Widget {
const indexes = [this.preserveCase.domNode];
this.onkeydown(this.domNode, (event: IKeyboardEvent) => {
if (event.equals(KeyCode.LeftArrow) || event.equals(KeyCode.RightArrow) || event.equals(KeyCode.Escape)) {
const index = indexes.indexOf(<HTMLElement>document.activeElement);
const index = indexes.indexOf(<HTMLElement>this.domNode.ownerDocument.activeElement);
if (index >= 0) {
let newIndex: number = -1;
if (event.equals(KeyCode.RightArrow)) {
Expand Down
4 changes: 2 additions & 2 deletions src/vs/base/browser/ui/inputbox/inputBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ export class InputBox extends Widget {
}

public hasFocus(): boolean {
return document.activeElement === this.input;
return this.input.ownerDocument.activeElement === this.input;
}

public select(range: IRange | null = null): void {
Expand Down Expand Up @@ -628,7 +628,7 @@ export class HistoryInputBox extends InputBox implements IHistoryNavigationWidge
if (options.showHistoryHint && options.showHistoryHint() && !this.placeholder.endsWith(NLS_PLACEHOLDER_HISTORY_HINT_SUFFIX) && !this.placeholder.endsWith(NLS_PLACEHOLDER_HISTORY_HINT_SUFFIX_IN_PARENS) && this.history.getHistory().length) {
const suffix = this.placeholder.endsWith(')') ? NLS_PLACEHOLDER_HISTORY_HINT_SUFFIX : NLS_PLACEHOLDER_HISTORY_HINT_SUFFIX_IN_PARENS;
const suffixedPlaceholder = this.placeholder + suffix;
if (options.showPlaceholderOnFocus && document.activeElement !== this.input) {
if (options.showPlaceholderOnFocus && this.input.ownerDocument.activeElement !== this.input) {
this.placeholder = suffixedPlaceholder;
}
else {
Expand Down
3 changes: 2 additions & 1 deletion src/vs/base/browser/ui/list/listPaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ export class PagedList<T> implements IDisposable {
}

isDOMFocused(): boolean {
return this.list.getHTMLElement() === document.activeElement;
const element = this.getHTMLElement();
return element === element.ownerDocument.activeElement;
}

domFocus(): void {
Expand Down
2 changes: 1 addition & 1 deletion src/vs/base/browser/ui/list/listWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1880,7 +1880,7 @@ export class List<T> implements ISpliceable<T>, IDisposable {
}

isDOMFocused(): boolean {
return this.view.domNode === document.activeElement;
return this.view.domNode === this.view.domNode.ownerDocument.activeElement;
}

getHTMLElement(): HTMLElement {
Expand Down
4 changes: 2 additions & 2 deletions src/vs/base/browser/ui/splitview/paneview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ export class PaneView extends Disposable {

private focusPrevious(): void {
const headers = this.getPaneHeaderElements();
const index = headers.indexOf(document.activeElement as HTMLElement);
const index = headers.indexOf(this.element.ownerDocument.activeElement as HTMLElement);

if (index === -1) {
return;
Expand All @@ -656,7 +656,7 @@ export class PaneView extends Disposable {

private focusNext(): void {
const headers = this.getPaneHeaderElements();
const index = headers.indexOf(document.activeElement as HTMLElement);
const index = headers.indexOf(this.element.ownerDocument.activeElement as HTMLElement);

if (index === -1) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/vs/base/browser/ui/toggle/toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ export class Checkbox extends Widget {
}

hasFocus(): boolean {
return this.domNode === document.activeElement;
return this.domNode === this.domNode.ownerDocument.activeElement;
}

protected applyStyles(): void {
Expand Down
3 changes: 2 additions & 1 deletion src/vs/base/browser/ui/tree/abstractTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1781,7 +1781,8 @@ export abstract class AbstractTree<T, TFilterData, TRef> implements IDisposable
}

isDOMFocused(): boolean {
return this.getHTMLElement() === document.activeElement;
const element = this.getHTMLElement();
return element === element.ownerDocument.activeElement;
}

layout(height?: number, width?: number): void {
Expand Down
2 changes: 1 addition & 1 deletion src/vs/platform/contextview/browser/contextMenuHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class ContextMenuHandler {
return; // Don't render an empty context menu
}

this.focusToReturn = document.activeElement as HTMLElement;
this.focusToReturn = getActiveElement() as HTMLElement;

let menu: Menu | undefined;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function registerAndCreateHistoryNavigationContext(scopedContextKeyServic
};

// Check for currently being focused
if (widget.element === document.activeElement) {
if (widget.element === widget.element.ownerDocument.activeElement) {
onDidFocus();
}

Expand Down
3 changes: 2 additions & 1 deletion src/vs/platform/list/browser/listService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ export class ListService implements IListService {
this.lists.push(registeredList);

// Check for currently being focused
if (widget.getHTMLElement() === document.activeElement) {
const element = widget.getHTMLElement();
if (element === element.ownerDocument.activeElement) {
this.setLastFocusedList(widget);
}

Expand Down
3 changes: 2 additions & 1 deletion src/vs/platform/quickinput/browser/quickInputController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,8 @@ export class QuickInputController extends Disposable {
return;
}

const focusChanged = !dom.isAncestor(document.activeElement, this.ui?.container ?? null);
const container = this.ui?.container;
const focusChanged = container && !dom.isAncestor(container.ownerDocument.activeElement, container);
this.controller = null;
this.onHideEmitter.fire();
this.getUI().container.style.display = 'none';
Expand Down
5 changes: 3 additions & 2 deletions src/vs/workbench/browser/actions/listCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ function ensureDOMFocus(widget: ListWidget | undefined): void {
// DOM focus is within another focusable control within the
// list/tree item. therefor we should ensure that the
// list/tree has DOM focus again after the command ran.
if (widget && widget.getHTMLElement() !== document.activeElement) {
widget.domFocus();
const element = widget?.getHTMLElement();
if (element && element !== element.ownerDocument.activeElement) {
widget?.domFocus();
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/vs/workbench/browser/actions/windowActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { CommandsRegistry } from 'vs/platform/commands/common/commands';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { isFolderBackupInfo, isWorkspaceBackupInfo } from 'vs/platform/backup/common/backup';
import { getActiveElement } from 'vs/base/browser/dom';

export const inRecentFilesPickerContextKey = 'inRecentFilesPicker';

Expand Down Expand Up @@ -406,10 +407,9 @@ class BlurAction extends Action2 {
}

run(): void {
const el = document.activeElement;

if (el instanceof HTMLElement) {
el.blur();
const activeElement = getActiveElement();
if (activeElement instanceof HTMLElement) {
activeElement.blur();
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/vs/workbench/browser/contextkeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export class WorkbenchContextKeysHandler extends Disposable {

this._register(this.editorGroupService.onDidChangeEditorPartOptions(() => this.updateEditorAreaContextKeys()));

this._register(Event.runAndSubscribe(onDidRegisterWindow, ({ window, disposableStore }) => disposableStore.add(addDisposableListener(window, EventType.FOCUS_IN, () => this.updateInputContextKeys(), true)), { window, disposableStore: this._store }));
this._register(Event.runAndSubscribe(onDidRegisterWindow, ({ window, disposableStore }) => disposableStore.add(addDisposableListener(window, EventType.FOCUS_IN, () => this.updateInputContextKeys(window.document), true)), { window, disposableStore: this._store }));

this._register(this.contextService.onDidChangeWorkbenchState(() => this.updateWorkbenchStateContextKey()));
this._register(this.contextService.onDidChangeWorkspaceFolders(() => {
Expand Down Expand Up @@ -329,17 +329,17 @@ export class WorkbenchContextKeysHandler extends Disposable {
this.activeEditorGroupLocked.set(activeGroup.isLocked);
}

private updateInputContextKeys(): void {
private updateInputContextKeys(ownerDocument: Document): void {

function activeElementIsInput(): boolean {
return !!document.activeElement && (document.activeElement.tagName === 'INPUT' || document.activeElement.tagName === 'TEXTAREA');
return !!ownerDocument.activeElement && (ownerDocument.activeElement.tagName === 'INPUT' || ownerDocument.activeElement.tagName === 'TEXTAREA');
}

const isInputFocused = activeElementIsInput();
this.inputFocusedContext.set(isInputFocused);

if (isInputFocused) {
const tracker = trackFocus(document.activeElement as HTMLElement);
const tracker = trackFocus(ownerDocument.activeElement as HTMLElement);
Event.once(tracker.onDidBlur)(() => {
this.inputFocusedContext.set(activeElementIsInput());

Expand Down
11 changes: 7 additions & 4 deletions src/vs/workbench/browser/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1001,14 +1001,17 @@ export abstract class Layout extends Disposable implements IWorkbenchLayoutServi
}

hasFocus(part: Parts): boolean {
const activeElement = document.activeElement;
if (!activeElement) {
const container = this.getContainer(part);
if (!container) {
return false;
}

const container = this.getContainer(part);
const activeElement = container.ownerDocument.activeElement;
if (!activeElement) {
return false;
}

return !!container && isAncestorUsingFlowTo(activeElement, container);
return isAncestorUsingFlowTo(activeElement, container);
}

focusPart(part: Parts): void {
Expand Down
4 changes: 2 additions & 2 deletions src/vs/workbench/browser/parts/editor/editorGroupView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ export class EditorGroupView extends Themable implements IEditorGroupView {
options.sticky = this.model.isSticky(activeEditor); // preserve sticky state
options.preserveFocus = true; // handle focus after editor is opened

const activeElement = document.activeElement;
const activeElement = this.editorContainer.ownerDocument.activeElement;

// Show active editor (intentionally not using async to keep
// `restoreEditors` from executing in same stack)
Expand All @@ -501,7 +501,7 @@ export class EditorGroupView extends Themable implements IEditorGroupView {
// stolen accidentally on startup when the user already
// clicked somewhere.

if (this.groupsView.activeGroup === this && activeElement === document.activeElement) {
if (this.groupsView.activeGroup === this && activeElement === this.editorContainer.ownerDocument.activeElement) {
this.focus();
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/browser/parts/editor/editorPanes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ export class EditorPanes extends Disposable {
const pane = this.doShowEditorPane(descriptor);

// Remember current active element for deciding to restore focus later
const activeElement = document.activeElement;
const activeElement = this.editorPanesParent.ownerDocument.activeElement;

// Apply input to pane
const { changed, cancelled } = await this.doSetInput(pane, editor, options, context);
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/browser/parts/statusbar/statusbarModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export class StatusbarViewModel extends Disposable {
}

private getFocusedEntry(): IStatusbarViewModelEntry | undefined {
return this._entries.find(entry => isAncestor(document.activeElement, entry.container));
return this._entries.find(entry => isAncestor(entry.container.ownerDocument.activeElement, entry.container));
}

private focusEntry(delta: number, restartPosition: number): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,8 @@ export class AccessibleView extends Disposable {
}
}));
disposableStore.add(this._editorWidget.onDidBlurEditorWidget(() => {
if (document.activeElement !== this._toolbar.getElement()) {
const element = this._toolbar.getElement();
if (element.ownerDocument.activeElement !== element) {
this._contextViewService.hideContextView();
}
}));
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/contrib/chat/browser/chatQuick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class QuickChatService extends Disposable implements IQuickChatService {
if (!widget) {
return false;
}
return dom.isAncestor(document.activeElement, widget);
return dom.isAncestor(widget.ownerDocument.activeElement, widget);
}

toggle(providerId?: string, query?: string | undefined): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
import { CommentContextKeys } from 'vs/workbench/contrib/comments/common/commentContextKeys';
import { CommentCommandId } from 'vs/workbench/contrib/comments/common/commentCommandIds';
import { ToggleTabFocusModeAction } from 'vs/editor/contrib/toggleTabFocusMode/browser/toggleTabFocusMode';
import { getActiveElement } from 'vs/base/browser/dom';

Registry.as<IConfigurationRegistry>(ConfigurationExtensions.Configuration).registerConfiguration({
id: 'comments',
Expand Down Expand Up @@ -118,7 +119,7 @@ export class CommentsAccessibilityHelpProvider implements IAccessibleContentProv
return strings.format(noKbMsg, commandId);
}
provideContent(): string {
this._element = document.activeElement as HTMLElement;
this._element = getActiveElement() as HTMLElement;
const content: string[] = [];
content.push(this._descriptionForCommand(ToggleTabFocusModeAction.ID, CommentAccessibilityHelpNLS.introWidget, CommentAccessibilityHelpNLS.introWidgetNoKb) + '\n');
content.push(CommentAccessibilityHelpNLS.commentCommands);
Expand Down
3 changes: 2 additions & 1 deletion src/vs/workbench/contrib/comments/browser/commentsView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ export class CommentsPanel extends FilterViewPane implements ICommentsView {
}

public override focus(): void {
if (this.tree && this.tree.getHTMLElement() === document.activeElement) {
const element = this.tree?.getHTMLElement();
if (element && element === element.ownerDocument.activeElement) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/contrib/debug/browser/debugHover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ export class DebugHoverWidget implements IContentWidget {
return;
}

if (dom.isAncestor(document.activeElement, this.domNode)) {
if (dom.isAncestor(this.domNode.ownerDocument.activeElement, this.domNode)) {
this.editor.focus();
}
this._isVisible = false;
Expand Down
6 changes: 5 additions & 1 deletion src/vs/workbench/contrib/debug/browser/exceptionWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ export class ExceptionWidget extends ZoneWidget {
}

override hasFocus(): boolean {
return dom.isAncestor(document.activeElement, this.container);
if (!this.container) {
return false;
}

return dom.isAncestor(this.container.ownerDocument.activeElement, this.container);
}
}
Loading

0 comments on commit f6bc90d

Please sign in to comment.