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 {assertDefined} from 'common/assert_utils';
18import {TraceRect} from 'trace/trace_rect';
19import {HierarchyTreeNode} from 'trace/tree_node/hierarchy_tree_node';
20import {UiRect} from 'viewers/components/rects/types2d';
21import {UiRectBuilder} from 'viewers/components/rects/ui_rect_builder';
22
23class UiRectFactory {
24  makeUiRects(
25    hierarchyRoot: HierarchyTreeNode,
26    viewCapturePackageNames: string[] = [],
27  ): UiRect[] {
28    const traceRects = this.extractRects(hierarchyRoot);
29    return traceRects.map((traceRect) => {
30      return new UiRectBuilder()
31        .setX(traceRect.x)
32        .setY(traceRect.y)
33        .setWidth(traceRect.w)
34        .setHeight(traceRect.h)
35        .setLabel(traceRect.name)
36        .setTransform(traceRect.transform)
37        .setIsVisible(traceRect.isVisible)
38        .setIsDisplay(traceRect.isDisplay)
39        .setId(traceRect.id)
40        .setGroupId(traceRect.groupId)
41        .setIsVirtual(traceRect.isVirtual)
42        .setIsClickable(!traceRect.isDisplay)
43        .setCornerRadius(traceRect.cornerRadius)
44        .setHasContent(
45          viewCapturePackageNames.includes(
46            traceRect.name.substring(0, traceRect.name.indexOf('/')),
47          ),
48        )
49        .setDepth(traceRect.depth)
50        .setOpacity(traceRect.opacity)
51        .build();
52    });
53  }
54
55  makeVcUiRects(hierarchyRoot: HierarchyTreeNode, groupId: number): UiRect[] {
56    const traceRects = this.extractRects(hierarchyRoot).filter((traceRect) => {
57      return traceRect.h > 0 && traceRect.w > 0;
58    });
59    return traceRects.map((traceRect) => {
60      return new UiRectBuilder()
61        .setX(traceRect.x)
62        .setY(traceRect.y)
63        .setWidth(traceRect.w)
64        .setHeight(traceRect.h)
65        .setLabel('')
66        .setTransform(traceRect.transform)
67        .setIsVisible(traceRect.isVisible)
68        .setIsDisplay(traceRect.isDisplay)
69        .setId(traceRect.id)
70        .setGroupId(groupId)
71        .setIsVirtual(traceRect.isVirtual)
72        .setIsClickable(true)
73        .setCornerRadius(traceRect.cornerRadius)
74        .setHasContent(traceRect.isVisible)
75        .setDepth(assertDefined(traceRect.depth))
76        .setOpacity(traceRect.opacity)
77        .build();
78    });
79  }
80
81  private extractRects(hierarchyRoot: HierarchyTreeNode): TraceRect[] {
82    const rects: TraceRect[] = [];
83
84    hierarchyRoot.forEachNodeDfs((node) => {
85      const nodeRects = node.getRects();
86      if (nodeRects && nodeRects.length > 0) {
87        rects.push(...nodeRects);
88      }
89    });
90
91    return rects;
92  }
93}
94
95export const UI_RECT_FACTORY = new UiRectFactory();
96