• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 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  private nextNodeDfs: this | undefined;
29  private prevNodeDfs: this | undefined;
30
31  static from(
32    node: HierarchyTreeNode,
33    parent?: UiHierarchyTreeNode,
34  ): UiHierarchyTreeNode {
35    const displayNode = new UiHierarchyTreeNode(
36      node.id,
37      node.name,
38      (node as any).propertiesProvider,
39    );
40    const rects = node.getRects();
41    if (rects) displayNode.setRects(rects);
42
43    if (parent) displayNode.setParent(parent);
44
45    const zParent = node.getZParent();
46    if (zParent) displayNode.setZParent(zParent);
47
48    node
49      .getRelativeChildren()
50      .forEach((zChild) => displayNode.addRelativeChild(zChild));
51
52    node.getAllChildren().forEach((child) => {
53      displayNode.addOrReplaceChild(
54        UiHierarchyTreeNode.from(child, displayNode),
55      );
56    });
57    node.getWarnings().forEach((warning) => displayNode.addWarning(warning));
58
59    return displayNode;
60  }
61
62  setDiff(diff: DiffType): void {
63    this.diff = diff;
64  }
65
66  getDiff(): DiffType {
67    return this.diff;
68  }
69
70  heading(): string | undefined {
71    return this.showHeading ? this.id.split(' ')[0].split('.')[0] : undefined;
72  }
73
74  setShowHeading(value: boolean) {
75    this.showHeading = value;
76  }
77
78  setDisplayName(name: string) {
79    this.displayName = name;
80  }
81
82  getDisplayName(): string {
83    return this.displayName;
84  }
85
86  addChip(chip: Chip): void {
87    this.chips.push(chip);
88  }
89
90  getChips(): Chip[] {
91    return this.chips;
92  }
93
94  setIsOldNode(value: boolean) {
95    this.isOldNodeInternal = value;
96  }
97
98  isOldNode() {
99    return this.isOldNodeInternal;
100  }
101
102  getNextDfs(): this | undefined {
103    return this.nextNodeDfs;
104  }
105
106  getPrevDfs(): this | undefined {
107    return this.prevNodeDfs;
108  }
109
110  assignDfsOrder() {
111    if (!this.isRoot()) {
112      console.warn('Attempted to assign DFS order from non-root node.');
113      return;
114    }
115
116    let prev: this | undefined;
117    this.forEachNodeDfs((node) => {
118      if (prev) {
119        prev.setNextDfs(node);
120        node.setPrevDfs(prev);
121      }
122      prev = node;
123    });
124  }
125
126  private setNextDfs(node: this) {
127    this.nextNodeDfs = node;
128  }
129
130  private setPrevDfs(node: this) {
131    this.prevNodeDfs = node;
132  }
133}
134