• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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';
17
18const DEFAULT_WORKSPACE_NAME = 'Default Workspace';
19
20export class WorkspaceManagerImpl implements WorkspaceManager {
21  readonly defaultWorkspace = new Workspace();
22  private _workspaces: Workspace[] = [];
23  private _currentWorkspace: Workspace;
24
25  constructor() {
26    this.defaultWorkspace.title = DEFAULT_WORKSPACE_NAME;
27    this.defaultWorkspace.userEditable = false;
28    this._currentWorkspace = this.defaultWorkspace;
29  }
30
31  createEmptyWorkspace(title: string): Workspace {
32    const workspace = new Workspace();
33    workspace.title = title;
34    this._workspaces.push(workspace);
35    return workspace;
36  }
37
38  removeWorkspace(ws: Workspace) {
39    if (ws === this.currentWorkspace) {
40      this._currentWorkspace = this.defaultWorkspace;
41    }
42    this._workspaces = this._workspaces.filter((w) => w !== ws);
43  }
44
45  switchWorkspace(workspace: Workspace): void {
46    // If this fails the workspace doesn't come from createEmptyWorkspace().
47    assertTrue(
48      this._workspaces.includes(workspace) ||
49        workspace === this.defaultWorkspace,
50    );
51    this._currentWorkspace = workspace;
52  }
53
54  get all(): ReadonlyArray<Workspace> {
55    return [this.defaultWorkspace].concat(this._workspaces);
56  }
57
58  get currentWorkspace() {
59    return this._currentWorkspace;
60  }
61}
62