• 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 {Registry} from '../base/registry';
16import {SidebarManager, SidebarMenuItem} from '../public/sidebar';
17
18export type SidebarMenuItemInternal = SidebarMenuItem & {
19  id: string; // A unique id generated by this class at registration time.
20};
21
22export class SidebarManagerImpl implements SidebarManager {
23  readonly enabled: boolean;
24  private _visible: boolean;
25  private lastId = 0;
26
27  readonly menuItems = new Registry<SidebarMenuItemInternal>((m) => m.id);
28
29  constructor(args: {disabled?: boolean; hidden?: boolean}) {
30    this.enabled = !args.disabled;
31    this._visible = !args.hidden;
32  }
33
34  addMenuItem(item: SidebarMenuItem): Disposable {
35    // Assign a unique id to every item. This simplifies the job of the mithril
36    // component that renders the sidebar.
37    const id = `sidebar_${++this.lastId}`;
38    const itemInt: SidebarMenuItemInternal = {...item, id};
39    return this.menuItems.register(itemInt);
40  }
41
42  public get visible() {
43    return this._visible;
44  }
45
46  public toggleVisibility() {
47    if (!this.enabled) return;
48    this._visible = !this._visible;
49  }
50}
51