Path: blob/main/src/vs/workbench/browser/parts/visibleViewContainersTracker.ts
5241 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { Disposable, DisposableMap } from '../../../base/common/lifecycle.js';6import { Emitter, Event } from '../../../base/common/event.js';7import { IViewDescriptorService, ViewContainerLocation } from '../../common/views.js';89/**10* Tracks the number of visible view containers at a given location.11* A view container is considered visible if it has active views (activeViewDescriptors.length > 0).12* Fires an event when the number of visible containers changes.13*/14export class VisibleViewContainersTracker extends Disposable {1516private readonly viewContainerModelListeners = this._register(new DisposableMap<string>());1718private readonly _onDidChange = this._register(new Emitter<{ before: number; after: number }>());19readonly onDidChange: Event<{ before: number; after: number }> = this._onDidChange.event;2021private _visibleCount: number = 0;2223constructor(24private readonly location: ViewContainerLocation,25@IViewDescriptorService private readonly viewDescriptorService: IViewDescriptorService26) {27super();2829this.registerListeners();30this.initializeViewContainerListeners();31this.updateVisibleCount();32}3334/**35* Returns the current number of visible view containers at this location.36*/37get visibleCount(): number {38return this._visibleCount;39}4041private registerListeners(): void {42// Track view container additions/removals43this._register(this.viewDescriptorService.onDidChangeViewContainers(({ added, removed }) => {44// Add listeners for new view containers45for (const { container, location } of added) {46if (location === this.location) {47this.addViewContainerModelListener(container.id);48}49}50// Remove listeners for removed view containers51for (const { container, location } of removed) {52if (location === this.location) {53this.viewContainerModelListeners.deleteAndDispose(container.id);54}55}5657const relevantChange = [...added, ...removed].some(({ location }) => location === this.location);58if (relevantChange) {59this.updateVisibleCount();60}61}));6263// Track container location changes64this._register(this.viewDescriptorService.onDidChangeContainerLocation(({ viewContainer, from, to }) => {65// Update listeners when container moves66if (from === this.location) {67this.viewContainerModelListeners.deleteAndDispose(viewContainer.id);68}69if (to === this.location) {70this.addViewContainerModelListener(viewContainer.id);71}7273if (from === this.location || to === this.location) {74this.updateVisibleCount();75}76}));77}7879private initializeViewContainerListeners(): void {80// Initialize listeners for existing view containers81for (const container of this.viewDescriptorService.getViewContainersByLocation(this.location)) {82this.addViewContainerModelListener(container.id);83}84}8586private addViewContainerModelListener(containerId: string): void {87const container = this.viewDescriptorService.getViewContainerById(containerId);88if (container) {89const model = this.viewDescriptorService.getViewContainerModel(container);90const listener = model.onDidChangeActiveViewDescriptors(() => this.updateVisibleCount());91this.viewContainerModelListeners.set(containerId, listener);92}93}9495private updateVisibleCount(): void {96const viewContainers = this.viewDescriptorService.getViewContainersByLocation(this.location);97const visibleViewContainers = viewContainers.filter(container =>98this.viewDescriptorService.getViewContainerModel(container).activeViewDescriptors.length > 099);100101const newCount = visibleViewContainers.length;102if (this._visibleCount !== newCount) {103const before = this._visibleCount;104this._visibleCount = newCount;105this._onDidChange.fire({ before, after: newCount });106}107}108}109110111