xref: /aosp_15_r20/development/tools/winscope/src/viewers/common/ui_tree_utils.ts (revision 90c8c64db3049935a07c6143d7fd006e26f8ecca)
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 {
18  PropertySource,
19  PropertyTreeNode,
20} from 'trace/tree_node/property_tree_node';
21import {TreeNode} from 'trace/tree_node/tree_node';
22import {StringFilterPredicate} from 'viewers/common/string_filter_predicate';
23import {DiffType} from './diff_type';
24import {UiHierarchyTreeNode} from './ui_hierarchy_tree_node';
25import {UiPropertyTreeNode} from './ui_property_tree_node';
26
27export type TreeNodeFilter = (node: TreeNode) => boolean;
28
29export class UiTreeUtils {
30  static isHighlighted(item: TreeNode, highlighted: string): boolean {
31    return highlighted === item.id;
32  }
33
34  static isVisible: TreeNodeFilter = (node: TreeNode) => {
35    return (
36      node instanceof UiHierarchyTreeNode &&
37      node.getEagerPropertyByName('isComputedVisible')?.getValue()
38    );
39  };
40
41  static makeIsNotDefaultFilter(allowList: string[]): TreeNodeFilter {
42    return (node: TreeNode) => {
43      return (
44        node instanceof UiPropertyTreeNode &&
45        (node.source !== PropertySource.DEFAULT ||
46          allowList.includes(node.name))
47      );
48    };
49  }
50
51  static isNotCalculated: TreeNodeFilter = (node: TreeNode) => {
52    return (
53      node instanceof UiPropertyTreeNode &&
54      node.source !== PropertySource.CALCULATED
55    );
56  };
57
58  static makeNodeFilter(predicate: StringFilterPredicate): TreeNodeFilter {
59    return (node: TreeNode) => {
60      return (
61        predicate(node.id) ||
62        (node instanceof PropertyTreeNode && predicate(node.formattedValue()))
63      );
64    };
65  }
66
67  static makeIdMatchFilter(targetId: string): TreeNodeFilter {
68    return (node: TreeNode) => node.id === targetId;
69  }
70
71  static makeDenyListFilterByName(denylist: string[]): TreeNodeFilter {
72    return (node: TreeNode) => !denylist.includes(node.name);
73  }
74
75  static shouldGetProperties(node: UiHierarchyTreeNode): boolean {
76    return !node.isOldNode() || node.getDiff() === DiffType.DELETED;
77  }
78}
79