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 {DiffType} from './diff_type';
23import {UiHierarchyTreeNode} from './ui_hierarchy_tree_node';
24import {UiPropertyTreeNode} from './ui_property_tree_node';
25
26export type TreeNodeFilter = (node: TreeNode) => boolean;
27
28export class UiTreeUtils {
29  static isHighlighted(item: TreeNode, highlighted: string): boolean {
30    return highlighted === item.id;
31  }
32
33  static isVisible: TreeNodeFilter = (node: TreeNode) => {
34    return (
35      node instanceof UiHierarchyTreeNode &&
36      node.getEagerPropertyByName('isComputedVisible')?.getValue()
37    );
38  };
39
40  static isNotDefault: TreeNodeFilter = (node: TreeNode) => {
41    return (
42      node instanceof UiPropertyTreeNode &&
43      node.source !== PropertySource.DEFAULT
44    );
45  };
46
47  static isNotCalculated: TreeNodeFilter = (node: TreeNode) => {
48    return (
49      node instanceof UiPropertyTreeNode &&
50      node.source !== PropertySource.CALCULATED
51    );
52  };
53
54  static makeIdFilter(filterString: string): TreeNodeFilter {
55    const filter = (node: TreeNode) => {
56      const regex = new RegExp(filterString, 'i');
57      return filterString.length === 0 || regex.test(node.id);
58    };
59    return filter;
60  }
61
62  static makePropertyFilter(filterString: string): TreeNodeFilter {
63    const filter = (node: TreeNode) => {
64      const regex = new RegExp(filterString, 'i');
65      return (
66        filterString.length === 0 ||
67        regex.test(node.name) ||
68        (node instanceof PropertyTreeNode && regex.test(node.formattedValue()))
69      );
70    };
71    return filter;
72  }
73
74  static makeIdMatchFilter(targetId: string): TreeNodeFilter {
75    return (node: TreeNode) => node.id === targetId;
76  }
77
78  static makePropertyMatchFilter(targetValue: string): TreeNodeFilter {
79    return (node: TreeNode) => {
80      return (
81        node instanceof UiPropertyTreeNode &&
82        node.formattedValue() !== targetValue
83      );
84    };
85  }
86
87  static makeDenyListFilterByName(denylist: string[]): TreeNodeFilter {
88    return (node: TreeNode) => !denylist.includes(node.name);
89  }
90
91  static makeAllowListFilterById(allowlist: string[]): TreeNodeFilter {
92    return (node: TreeNode) => allowlist.includes(node.id);
93  }
94
95  static shouldGetProperties(node: UiHierarchyTreeNode): boolean {
96    return !node.isOldNode() || node.getDiff() === DiffType.DELETED;
97  }
98}
99