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