1/*
2 * Copyright 2024, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import {HierarchyTreeNode} from 'trace/tree_node/hierarchy_tree_node';
18import {Chip} from './chip';
19import {DiffNode} from './diff_node';
20import {DiffType} from './diff_type';
21
22export class UiHierarchyTreeNode extends HierarchyTreeNode implements DiffNode {
23  private chips: Chip[] = [];
24  private diff: DiffType = DiffType.NONE;
25  private displayName: string = this.name;
26  private isOldNodeInternal = false;
27  private showHeading = true;
28
29  static from(
30    node: HierarchyTreeNode,
31    parent?: UiHierarchyTreeNode,
32  ): UiHierarchyTreeNode {
33    const displayNode = new UiHierarchyTreeNode(
34      node.id,
35      node.name,
36      (node as any).propertiesProvider,
37    );
38    const rects = node.getRects();
39    if (rects) displayNode.setRects(rects);
40
41    if (parent) displayNode.setParent(parent);
42
43    const zParent = node.getZParent();
44    if (zParent) displayNode.setZParent(zParent);
45
46    node.getAllChildren().forEach((child) => {
47      displayNode.addOrReplaceChild(
48        UiHierarchyTreeNode.from(child, displayNode),
49      );
50    });
51    return displayNode;
52  }
53
54  setDiff(diff: DiffType): void {
55    this.diff = diff;
56  }
57
58  getDiff(): DiffType {
59    return this.diff;
60  }
61
62  heading(): string | undefined {
63    return this.showHeading ? this.id.split(' ')[0].split('.')[0] : undefined;
64  }
65
66  setShowHeading(value: boolean) {
67    this.showHeading = value;
68  }
69
70  setDisplayName(name: string) {
71    this.displayName = name;
72  }
73
74  getDisplayName(): string {
75    return this.displayName;
76  }
77
78  addChip(chip: Chip): void {
79    this.chips.push(chip);
80  }
81
82  getChips(): Chip[] {
83    return this.chips;
84  }
85
86  setIsOldNode(value: boolean) {
87    this.isOldNodeInternal = value;
88  }
89
90  isOldNode() {
91    return this.isOldNodeInternal;
92  }
93}
94