Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/browser/parts/visibleViewContainersTracker.ts
5241 views
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 { Disposable, DisposableMap } from '../../../base/common/lifecycle.js';
7
import { Emitter, Event } from '../../../base/common/event.js';
8
import { IViewDescriptorService, ViewContainerLocation } from '../../common/views.js';
9
10
/**
11
* Tracks the number of visible view containers at a given location.
12
* A view container is considered visible if it has active views (activeViewDescriptors.length > 0).
13
* Fires an event when the number of visible containers changes.
14
*/
15
export class VisibleViewContainersTracker extends Disposable {
16
17
private readonly viewContainerModelListeners = this._register(new DisposableMap<string>());
18
19
private readonly _onDidChange = this._register(new Emitter<{ before: number; after: number }>());
20
readonly onDidChange: Event<{ before: number; after: number }> = this._onDidChange.event;
21
22
private _visibleCount: number = 0;
23
24
constructor(
25
private readonly location: ViewContainerLocation,
26
@IViewDescriptorService private readonly viewDescriptorService: IViewDescriptorService
27
) {
28
super();
29
30
this.registerListeners();
31
this.initializeViewContainerListeners();
32
this.updateVisibleCount();
33
}
34
35
/**
36
* Returns the current number of visible view containers at this location.
37
*/
38
get visibleCount(): number {
39
return this._visibleCount;
40
}
41
42
private registerListeners(): void {
43
// Track view container additions/removals
44
this._register(this.viewDescriptorService.onDidChangeViewContainers(({ added, removed }) => {
45
// Add listeners for new view containers
46
for (const { container, location } of added) {
47
if (location === this.location) {
48
this.addViewContainerModelListener(container.id);
49
}
50
}
51
// Remove listeners for removed view containers
52
for (const { container, location } of removed) {
53
if (location === this.location) {
54
this.viewContainerModelListeners.deleteAndDispose(container.id);
55
}
56
}
57
58
const relevantChange = [...added, ...removed].some(({ location }) => location === this.location);
59
if (relevantChange) {
60
this.updateVisibleCount();
61
}
62
}));
63
64
// Track container location changes
65
this._register(this.viewDescriptorService.onDidChangeContainerLocation(({ viewContainer, from, to }) => {
66
// Update listeners when container moves
67
if (from === this.location) {
68
this.viewContainerModelListeners.deleteAndDispose(viewContainer.id);
69
}
70
if (to === this.location) {
71
this.addViewContainerModelListener(viewContainer.id);
72
}
73
74
if (from === this.location || to === this.location) {
75
this.updateVisibleCount();
76
}
77
}));
78
}
79
80
private initializeViewContainerListeners(): void {
81
// Initialize listeners for existing view containers
82
for (const container of this.viewDescriptorService.getViewContainersByLocation(this.location)) {
83
this.addViewContainerModelListener(container.id);
84
}
85
}
86
87
private addViewContainerModelListener(containerId: string): void {
88
const container = this.viewDescriptorService.getViewContainerById(containerId);
89
if (container) {
90
const model = this.viewDescriptorService.getViewContainerModel(container);
91
const listener = model.onDidChangeActiveViewDescriptors(() => this.updateVisibleCount());
92
this.viewContainerModelListeners.set(containerId, listener);
93
}
94
}
95
96
private updateVisibleCount(): void {
97
const viewContainers = this.viewDescriptorService.getViewContainersByLocation(this.location);
98
const visibleViewContainers = viewContainers.filter(container =>
99
this.viewDescriptorService.getViewContainerModel(container).activeViewDescriptors.length > 0
100
);
101
102
const newCount = visibleViewContainers.length;
103
if (this._visibleCount !== newCount) {
104
const before = this._visibleCount;
105
this._visibleCount = newCount;
106
this._onDidChange.fire({ before, after: newCount });
107
}
108
}
109
}
110
111