xref: /aosp_15_r20/external/perfetto/ui/src/core/workspace_manager.ts (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1// Copyright (C) 2024 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import {assertTrue} from '../base/logging';
16import {Workspace, WorkspaceManager} from '../public/workspace';
17import {raf} from './raf_scheduler';
18
19const DEFAULT_WORKSPACE_NAME = 'Default Workspace';
20
21export class WorkspaceManagerImpl implements WorkspaceManager {
22  private _workspaces: Workspace[] = [];
23  private _currentWorkspace: Workspace;
24
25  constructor() {
26    // TS compiler cannot see that we are indirectly initializing
27    // _currentWorkspace via resetWorkspaces(), hence the re-assignment.
28    this._currentWorkspace = this.createEmptyWorkspace(DEFAULT_WORKSPACE_NAME);
29  }
30
31  createEmptyWorkspace(title: string): Workspace {
32    const workspace = new Workspace();
33    workspace.title = title;
34    workspace.onchange = () => raf.scheduleFullRedraw();
35    this._workspaces.push(workspace);
36    return workspace;
37  }
38
39  switchWorkspace(workspace: Workspace): void {
40    // If this fails the workspace doesn't come from createEmptyWorkspace().
41    assertTrue(this._workspaces.includes(workspace));
42    this._currentWorkspace = workspace;
43  }
44
45  get all(): ReadonlyArray<Workspace> {
46    return this._workspaces;
47  }
48
49  get currentWorkspace() {
50    return this._currentWorkspace;
51  }
52}
53