Skip to content

Commit

Permalink
Fix issue with Cmd+W closing the window with opened tabs (microsoft#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
benibenj authored and andremmsilva committed May 26, 2024
1 parent 4b72645 commit 9c6dae2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
64 changes: 63 additions & 1 deletion src/vs/workbench/browser/contextkeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Event } from 'vs/base/common/event';
import { Disposable } from 'vs/base/common/lifecycle';
import { IContextKeyService, IContextKey, setConstant as setConstantContextKey } from 'vs/platform/contextkey/common/contextkey';
import { InputFocusedContext, IsMacContext, IsLinuxContext, IsWindowsContext, IsWebContext, IsMacNativeContext, IsDevelopmentContext, IsIOSContext, ProductQualityContext, IsMobileContext } from 'vs/platform/contextkey/common/contextkeys';
import { SplitEditorsVertically, InEditorZenModeContext, AuxiliaryBarVisibleContext, SideBarVisibleContext, PanelAlignmentContext, PanelMaximizedContext, PanelVisibleContext, EmbedderIdentifierContext, EditorTabsVisibleContext, IsMainEditorCenteredLayoutContext, MainEditorAreaVisibleContext, DirtyWorkingCopiesContext, EmptyWorkspaceSupportContext, EnterMultiRootWorkspaceSupportContext, HasWebFileSystemAccess, IsMainWindowFullscreenContext, OpenFolderWorkspaceSupportContext, RemoteNameContext, VirtualWorkspaceContext, WorkbenchStateContext, WorkspaceFolderCountContext, PanelPositionContext, TemporaryWorkspaceContext, TitleBarVisibleContext, TitleBarStyleContext, IsAuxiliaryWindowFocusedContext } from 'vs/workbench/common/contextkeys';
import { SplitEditorsVertically, InEditorZenModeContext, AuxiliaryBarVisibleContext, SideBarVisibleContext, PanelAlignmentContext, PanelMaximizedContext, PanelVisibleContext, EmbedderIdentifierContext, EditorTabsVisibleContext, IsMainEditorCenteredLayoutContext, MainEditorAreaVisibleContext, DirtyWorkingCopiesContext, EmptyWorkspaceSupportContext, EnterMultiRootWorkspaceSupportContext, HasWebFileSystemAccess, IsMainWindowFullscreenContext, OpenFolderWorkspaceSupportContext, RemoteNameContext, VirtualWorkspaceContext, WorkbenchStateContext, WorkspaceFolderCountContext, PanelPositionContext, TemporaryWorkspaceContext, TitleBarVisibleContext, TitleBarStyleContext, IsAuxiliaryWindowFocusedContext, ActiveEditorGroupEmptyContext, ActiveEditorGroupIndexContext, ActiveEditorGroupLastContext, ActiveEditorGroupLockedContext, MultipleEditorGroupsContext, EditorsVisibleContext } from 'vs/workbench/common/contextkeys';
import { trackFocus, addDisposableListener, EventType, onDidRegisterWindow, getActiveWindow } from 'vs/base/browser/dom';
import { preferredSideBySideGroupDirection, GroupDirection, IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
Expand All @@ -24,12 +24,21 @@ import { IProductService } from 'vs/platform/product/common/productService';
import { getTitleBarStyle } from 'vs/platform/window/common/window';
import { mainWindow } from 'vs/base/browser/window';
import { isFullscreen, onDidChangeFullscreen } from 'vs/base/browser/browser';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';

export class WorkbenchContextKeysHandler extends Disposable {
private inputFocusedContext: IContextKey<boolean>;

private dirtyWorkingCopiesContext: IContextKey<boolean>;

private activeEditorGroupEmpty: IContextKey<boolean>;
private activeEditorGroupIndex: IContextKey<number>;
private activeEditorGroupLast: IContextKey<boolean>;
private activeEditorGroupLocked: IContextKey<boolean>;
private multipleEditorGroupsContext: IContextKey<boolean>;

private editorsVisibleContext: IContextKey<boolean>;

private splitEditorsVerticallyContext: IContextKey<boolean>;

private workbenchStateContext: IContextKey<string>;
Expand Down Expand Up @@ -64,6 +73,7 @@ export class WorkbenchContextKeysHandler extends Disposable {
@IWorkbenchEnvironmentService private readonly environmentService: IWorkbenchEnvironmentService,
@IProductService private readonly productService: IProductService,
@IEditorGroupsService private readonly editorGroupService: IEditorGroupsService,
@IEditorService private readonly editorService: IEditorService,
@IWorkbenchLayoutService private readonly layoutService: IWorkbenchLayoutService,
@IPaneCompositePartService private readonly paneCompositeService: IPaneCompositePartService,
@IWorkingCopyService private readonly workingCopyService: IWorkingCopyService,
Expand Down Expand Up @@ -98,6 +108,16 @@ export class WorkbenchContextKeysHandler extends Disposable {
ProductQualityContext.bindTo(this.contextKeyService).set(this.productService.quality || '');
EmbedderIdentifierContext.bindTo(this.contextKeyService).set(productService.embedderIdentifier);

// Editor Groups
this.activeEditorGroupEmpty = ActiveEditorGroupEmptyContext.bindTo(this.contextKeyService);
this.activeEditorGroupIndex = ActiveEditorGroupIndexContext.bindTo(this.contextKeyService);
this.activeEditorGroupLast = ActiveEditorGroupLastContext.bindTo(this.contextKeyService);
this.activeEditorGroupLocked = ActiveEditorGroupLockedContext.bindTo(this.contextKeyService);
this.multipleEditorGroupsContext = MultipleEditorGroupsContext.bindTo(this.contextKeyService);

// Editors
this.editorsVisibleContext = EditorsVisibleContext.bindTo(this.contextKeyService);

// Working Copies
this.dirtyWorkingCopiesContext = DirtyWorkingCopiesContext.bindTo(this.contextKeyService);
this.dirtyWorkingCopiesContext.set(this.workingCopyService.hasDirty);
Expand Down Expand Up @@ -183,8 +203,18 @@ export class WorkbenchContextKeysHandler extends Disposable {
private registerListeners(): void {
this.editorGroupService.whenReady.then(() => {
this.updateEditorAreaContextKeys();
this.updateEditorGroupContextKeys();
this.updateVisiblePanesContextKeys();
});

this._register(this.editorService.onDidActiveEditorChange(() => this.updateEditorGroupContextKeys()));
this._register(this.editorService.onDidVisibleEditorsChange(() => this.updateVisiblePanesContextKeys()));
this._register(this.editorGroupService.onDidAddGroup(() => this.updateEditorGroupContextKeys()));
this._register(this.editorGroupService.onDidRemoveGroup(() => this.updateEditorGroupContextKeys()));
this._register(this.editorGroupService.onDidChangeGroupIndex(() => this.updateEditorGroupContextKeys()));
this._register(this.editorGroupService.onDidChangeActiveGroup(() => this.updateEditorGroupsContextKeys()));
this._register(this.editorGroupService.onDidChangeGroupLocked(() => this.updateEditorGroupsContextKeys()));

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

this._register(Event.runAndSubscribe(onDidRegisterWindow, ({ window, disposables }) => disposables.add(addDisposableListener(window, EventType.FOCUS_IN, () => this.updateInputContextKeys(window.document), true)), { window: mainWindow, disposables: this._store }));
Expand Down Expand Up @@ -227,6 +257,38 @@ export class WorkbenchContextKeysHandler extends Disposable {
this._register(this.workingCopyService.onDidChangeDirty(workingCopy => this.dirtyWorkingCopiesContext.set(workingCopy.isDirty() || this.workingCopyService.hasDirty)));
}

private updateVisiblePanesContextKeys(): void {
const visibleEditorPanes = this.editorService.visibleEditorPanes;
if (visibleEditorPanes.length > 0) {
this.editorsVisibleContext.set(true);
} else {
this.editorsVisibleContext.reset();
}
}

private updateEditorGroupContextKeys(): void {
if (!this.editorService.activeEditor) {
this.activeEditorGroupEmpty.set(true);
} else {
this.activeEditorGroupEmpty.reset();
}
this.updateEditorGroupsContextKeys();
}

private updateEditorGroupsContextKeys(): void {
const groupCount = this.editorGroupService.count;
if (groupCount > 1) {
this.multipleEditorGroupsContext.set(true);
} else {
this.multipleEditorGroupsContext.reset();
}

const activeGroup = this.editorGroupService.activeGroup;
this.activeEditorGroupIndex.set(activeGroup.index + 1); // not zero-indexed
this.activeEditorGroupLast.set(activeGroup.index === groupCount - 1);
this.activeEditorGroupLocked.set(activeGroup.isLocked);
}

private updateEditorAreaContextKeys(): void {
this.editorTabsVisibleContext.set(this.editorGroupService.partOptions.showTabs === 'multiple');
}
Expand Down
6 changes: 3 additions & 3 deletions src/vs/workbench/browser/parts/editor/editorGroupView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ export class EditorGroupView extends Themable implements IEditorGroupView {

const groupActiveEditorAvailableEditorIds = this.editorPartsView.bind(ActiveEditorAvailableEditorIdsContext, this);
const groupActiveEditorCanSplitInGroupContext = this.editorPartsView.bind(ActiveEditorCanSplitInGroupContext, this);
const sideBySideEditorContext = this.editorPartsView.bind(SideBySideEditorActiveContext, this);
const groupActiveEditorIsSideBySideEditorContext = this.editorPartsView.bind(SideBySideEditorActiveContext, this);

const activeEditorListener = this._register(new MutableDisposable());

Expand All @@ -286,15 +286,15 @@ export class EditorGroupView extends Themable implements IEditorGroupView {

if (activeEditor) {
groupActiveEditorCanSplitInGroupContext.set(activeEditor.hasCapability(EditorInputCapabilities.CanSplitInGroup));
sideBySideEditorContext.set(activeEditor.typeId === SideBySideEditorInput.ID);
groupActiveEditorIsSideBySideEditorContext.set(activeEditor.typeId === SideBySideEditorInput.ID);

groupActiveEditorDirtyContext.set(activeEditor.isDirty() && !activeEditor.isSaving());
activeEditorListener.value = activeEditor.onDidChangeDirty(() => {
groupActiveEditorDirtyContext.set(activeEditor.isDirty() && !activeEditor.isSaving());
});
} else {
groupActiveEditorCanSplitInGroupContext.set(false);
sideBySideEditorContext.set(false);
groupActiveEditorIsSideBySideEditorContext.set(false);
groupActiveEditorDirtyContext.set(false);
}

Expand Down

0 comments on commit 9c6dae2

Please sign in to comment.