|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { Sequencer } from '../../../../base/common/async.js'; |
| 7 | +import { Disposable } from '../../../../base/common/lifecycle.js'; |
| 8 | +import { ResourceMap } from '../../../../base/common/map.js'; |
| 9 | +import { autorun, derivedOpts, IObservable, runOnChange } from '../../../../base/common/observable.js'; |
| 10 | +import { isEqual } from '../../../../base/common/resources.js'; |
| 11 | +import { URI } from '../../../../base/common/uri.js'; |
| 12 | +import { IConfigurationService } from '../../../../platform/configuration/common/configuration.js'; |
| 13 | +import { observableConfigValue } from '../../../../platform/observable/common/platformObservableUtils.js'; |
| 14 | +import { IWorkspaceContextService } from '../../../../platform/workspace/common/workspace.js'; |
| 15 | +import { IWorkbenchContribution } from '../../../../workbench/common/contributions.js'; |
| 16 | +import { IEditorGroupsService, IEditorWorkingSet } from '../../../../workbench/services/editor/common/editorGroupsService.js'; |
| 17 | +import { IEditorService } from '../../../../workbench/services/editor/common/editorService.js'; |
| 18 | +import { IWorkbenchLayoutService, Parts } from '../../../../workbench/services/layout/browser/layoutService.js'; |
| 19 | +import { SessionStatus } from '../../../services/sessions/common/session.js'; |
| 20 | +import { IActiveSession, ISessionsManagementService } from '../../../services/sessions/common/sessionsManagement.js'; |
| 21 | + |
| 22 | +export class SessionWorkingSetController extends Disposable implements IWorkbenchContribution { |
| 23 | + |
| 24 | + static readonly ID = 'workbench.contrib.sessionsWorkingSetController'; |
| 25 | + |
| 26 | + private readonly _useModalConfigObs: IObservable<'off' | 'some' | 'all'>; |
| 27 | + private readonly _workingSets = new ResourceMap<IEditorWorkingSet>(); |
| 28 | + private readonly _workingSetSequencer = new Sequencer(); |
| 29 | + |
| 30 | + constructor( |
| 31 | + @IConfigurationService private readonly _configurationService: IConfigurationService, |
| 32 | + @ISessionsManagementService private readonly _sessionManagementService: ISessionsManagementService, |
| 33 | + @IEditorGroupsService private readonly _editorGroupsService: IEditorGroupsService, |
| 34 | + @IEditorService private readonly _editorService: IEditorService, |
| 35 | + @IWorkspaceContextService private readonly _workspaceContextService: IWorkspaceContextService, |
| 36 | + @IWorkbenchLayoutService private readonly _layoutService: IWorkbenchLayoutService, |
| 37 | + ) { |
| 38 | + super(); |
| 39 | + |
| 40 | + this._useModalConfigObs = observableConfigValue<'off' | 'some' | 'all'>('workbench.editor.useModal', 'all', this._configurationService); |
| 41 | + |
| 42 | + const activeSession = derivedOpts<IActiveSession | undefined>({ |
| 43 | + equalsFn: ((a, b) => isEqual(a?.resource, b?.resource)) |
| 44 | + }, reader => { |
| 45 | + return this._sessionManagementService.activeSession.read(reader); |
| 46 | + }); |
| 47 | + |
| 48 | + this._register(autorun(reader => { |
| 49 | + const _useModalConfig = this._useModalConfigObs.read(reader); |
| 50 | + if (_useModalConfig === 'all') { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + // Session changed (save) |
| 55 | + reader.store.add(runOnChange(activeSession, (_, previousSession) => { |
| 56 | + if (!previousSession || previousSession.status.read(undefined) === SessionStatus.Untitled) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + this._saveWorkingSet(previousSession.resource); |
| 61 | + })); |
| 62 | + |
| 63 | + // Workspace folders changes (apply) |
| 64 | + reader.store.add(this._workspaceContextService.onDidChangeWorkspaceFolders(() => { |
| 65 | + const activeSessionResource = activeSession.read(undefined)?.resource; |
| 66 | + if (!activeSessionResource) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + void this._applyWorkingSet(activeSessionResource); |
| 71 | + })); |
| 72 | + })); |
| 73 | + } |
| 74 | + |
| 75 | + private _saveWorkingSet(sessionResource: URI): void { |
| 76 | + const existingWorkingSet = this._workingSets.get(sessionResource); |
| 77 | + if (existingWorkingSet) { |
| 78 | + this._editorGroupsService.deleteWorkingSet(existingWorkingSet); |
| 79 | + } |
| 80 | + |
| 81 | + const workingSet = this._editorGroupsService.saveWorkingSet(`session-working-set:${sessionResource.toString()}`); |
| 82 | + this._workingSets.set(sessionResource, workingSet); |
| 83 | + } |
| 84 | + |
| 85 | + private async _applyWorkingSet(sessionResource: URI): Promise<void> { |
| 86 | + const workingSet: IEditorWorkingSet | 'empty' = this._workingSets.get(sessionResource) ?? 'empty'; |
| 87 | + const preserveFocus = this._layoutService.hasFocus(Parts.PANEL_PART); |
| 88 | + |
| 89 | + return this._workingSetSequencer.queue(async () => { |
| 90 | + const applied = await this._editorGroupsService.applyWorkingSet(workingSet, { preserveFocus }); |
| 91 | + if (applied && this._editorService.visibleEditors.length > 0) { |
| 92 | + this._layoutService.setPartHidden(false, Parts.EDITOR_PART); |
| 93 | + } |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + override dispose(): void { |
| 98 | + for (const [, workingSet] of this._workingSets) { |
| 99 | + this._editorGroupsService.deleteWorkingSet(workingSet); |
| 100 | + } |
| 101 | + |
| 102 | + super.dispose(); |
| 103 | + } |
| 104 | +} |
0 commit comments