Skip to content

Commit cb8e8a9

Browse files
authored
Closing aux window focuses wrong "main" window (fix microsoft#199372) (microsoft#199527)
* Closing aux window focuses wrong "main" window (fix microsoft#199372) * .
1 parent 79bdee6 commit cb8e8a9

File tree

5 files changed

+27
-9
lines changed

5 files changed

+27
-9
lines changed

src/vs/workbench/browser/parts/editor/auxiliaryEditorPart.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { EditorPart } from 'vs/workbench/browser/parts/editor/editorPart';
1919
import { IAuxiliaryTitlebarPart } from 'vs/workbench/browser/parts/titlebar/titlebarPart';
2020
import { WindowTitle } from 'vs/workbench/browser/parts/titlebar/windowTitle';
2121
import { IAuxiliaryWindowOpenOptions, IAuxiliaryWindowService } from 'vs/workbench/services/auxiliaryWindow/browser/auxiliaryWindowService';
22-
import { IAuxiliaryEditorPart } from 'vs/workbench/services/editor/common/editorGroupsService';
22+
import { GroupsOrder, IAuxiliaryEditorPart } from 'vs/workbench/services/editor/common/editorGroupsService';
2323
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
2424
import { IHostService } from 'vs/workbench/services/host/browser/host';
2525
import { IWorkbenchLayoutService } from 'vs/workbench/services/layout/browser/layoutService';
@@ -180,12 +180,12 @@ class AuxiliaryEditorPartImpl extends EditorPart implements IAuxiliaryEditorPart
180180
super(editorPartsView, `workbench.parts.auxiliaryEditor.${id}`, groupsLabel, true, instantiationService, themeService, configurationService, storageService, layoutService, hostService, contextKeyService);
181181
}
182182

183-
override removeGroup(group: number | IEditorGroupView, preserveFocus?: boolean | undefined): void {
183+
override removeGroup(group: number | IEditorGroupView, preserveFocus?: boolean): void {
184184

185185
// Close aux window when last group removed
186186
const groupView = this.assertGroupView(group);
187187
if (this.count === 1 && this.activeGroup === groupView) {
188-
this.doClose(false /* do not merge any groups to main part */);
188+
this.doRemoveLastGroup(preserveFocus);
189189
}
190190

191191
// Otherwise delegate to parent implementation
@@ -194,6 +194,23 @@ class AuxiliaryEditorPartImpl extends EditorPart implements IAuxiliaryEditorPart
194194
}
195195
}
196196

197+
private doRemoveLastGroup(preserveFocus?: boolean): void {
198+
const restoreFocus = !preserveFocus && this.shouldRestoreFocus(this.container);
199+
200+
// Activate next group
201+
const mostRecentlyActiveGroups = this.editorPartsView.getGroups(GroupsOrder.MOST_RECENTLY_ACTIVE);
202+
const nextActiveGroup = mostRecentlyActiveGroups[1]; // [0] will be the current group we are about to dispose
203+
if (nextActiveGroup) {
204+
nextActiveGroup.groupsView.activateGroup(nextActiveGroup);
205+
206+
if (restoreFocus) {
207+
nextActiveGroup.focus();
208+
}
209+
}
210+
211+
this.doClose(false /* do not merge any groups to main part */);
212+
}
213+
197214
protected override saveState(): void {
198215
return; // TODO support auxiliary editor state
199216
}

src/vs/workbench/browser/parts/editor/editor.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ export interface IEditorPartsView {
181181
readonly activeGroup: IEditorGroupView;
182182
readonly groups: IEditorGroupView[];
183183
getGroup(identifier: GroupIdentifier): IEditorGroupView | undefined;
184+
getGroups(order?: GroupsOrder): IEditorGroupView[];
184185

185186
readonly count: number;
186187

src/vs/workbench/browser/parts/editor/editorPart.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export class EditorPart extends Part implements IEditorPart, IEditorGroupsView {
141141
private readonly groupViews = new Map<GroupIdentifier, IEditorGroupView>();
142142
private mostRecentActiveGroups: GroupIdentifier[] = [];
143143

144-
private container: HTMLElement | undefined;
144+
protected container: HTMLElement | undefined;
145145

146146
private scopedInstantiationService!: IInstantiationService;
147147

@@ -540,7 +540,7 @@ export class EditorPart extends Part implements IEditorPart, IEditorGroupsView {
540540
return { size: serializedNode.size };
541541
}
542542

543-
private shouldRestoreFocus(target: Element | undefined): boolean {
543+
protected shouldRestoreFocus(target: Element | undefined): boolean {
544544
if (!target) {
545545
return false;
546546
}

src/vs/workbench/electron-sandbox/window.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { localize } from 'vs/nls';
77
import { URI } from 'vs/base/common/uri';
88
import { onUnexpectedError } from 'vs/base/common/errors';
99
import { equals } from 'vs/base/common/objects';
10-
import { EventType, EventHelper, addDisposableListener, ModifierKeyEmitter, getActiveElement, getActiveWindow, hasWindow, getWindow, getWindowById } from 'vs/base/browser/dom';
10+
import { EventType, EventHelper, addDisposableListener, ModifierKeyEmitter, getActiveElement, getActiveWindow, hasWindow, getWindow, getWindowById, getWindowId } from 'vs/base/browser/dom';
1111
import { Separator, WorkbenchActionExecutedClassification, WorkbenchActionExecutedEvent } from 'vs/base/common/actions';
1212
import { IFileService } from 'vs/platform/files/common/files';
1313
import { EditorResourceAccessor, IUntitledTextResourceEditorInput, SideBySideEditor, pathsToEditors, IResourceDiffEditorInput, IUntypedEditorInput, IEditorPane, isResourceEditorInput, IResourceMergeEditorInput } from 'vs/workbench/common/editor';
@@ -706,7 +706,7 @@ export class NativeWindow extends BaseWindow {
706706
originalWindowFocus();
707707

708708
if (getActiveWindow() !== mainWindow) {
709-
that.nativeHostService.focusWindow({ targetWindowId: that.nativeHostService.windowId });
709+
that.nativeHostService.focusWindow({ targetWindowId: getWindowId(mainWindow) });
710710
}
711711
};
712712
}

src/vs/workbench/services/host/electron-sandbox/nativeHostService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { Disposable } from 'vs/base/common/lifecycle';
1414
import { NativeHostService } from 'vs/platform/native/common/nativeHostService';
1515
import { INativeWorkbenchEnvironmentService } from 'vs/workbench/services/environment/electron-sandbox/environmentService';
1616
import { IMainProcessService } from 'vs/platform/ipc/common/mainProcessService';
17-
import { disposableWindowInterval, getActiveDocument, getWindowsCount, hasWindow, onDidRegisterWindow } from 'vs/base/browser/dom';
17+
import { disposableWindowInterval, getActiveDocument, getWindowId, getWindowsCount, hasWindow, onDidRegisterWindow } from 'vs/base/browser/dom';
1818
import { memoize } from 'vs/base/common/decorators';
1919
import { isAuxiliaryWindow } from 'vs/base/browser/window';
2020

@@ -164,7 +164,7 @@ class WorkbenchHostService extends Disposable implements IHostService {
164164
focus(targetWindow: Window, options?: { force: boolean }): Promise<void> {
165165
return this.nativeHostService.focusWindow({
166166
force: options?.force,
167-
targetWindowId: isAuxiliaryWindow(targetWindow) ? targetWindow.vscodeWindowId : this.nativeHostService.windowId
167+
targetWindowId: getWindowId(targetWindow)
168168
});
169169
}
170170

0 commit comments

Comments
 (0)