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 {FixedStringFormatter} from 'trace/tree_node/formatters';
19import {Operation} from 'trace/tree_node/operations/operation';
20import {UiPropertyTreeNode} from 'viewers/common/ui_property_tree_node';
21
22export class UpdateTransitionChangesNames
23  implements Operation<UiPropertyTreeNode>
24{
25  constructor(
26    private readonly layerIdToName: Map<number, string>,
27    private readonly windowTokenToTitle: Map<string, string>,
28  ) {}
29
30  apply(node: UiPropertyTreeNode): void {
31    assertDefined(node.getChildByName('wmData'))
32      .getChildByName('targets')
33      ?.getAllChildren()
34      .forEach((target) => {
35        const layerId = target.getChildByName('layerId');
36        if (layerId) {
37          const layerIdValue = Number(layerId.getValue());
38          const layerName = this.layerIdToName.get(layerIdValue);
39          if (layerName) {
40            layerId.setFormatter(
41              new FixedStringFormatter(`${layerIdValue} ${layerName}`),
42            );
43          }
44        }
45
46        const windowId = target.getChildByName('windowId');
47        if (windowId) {
48          const windowIdString = windowId.getValue().toString(16);
49          const windowTitle = this.windowTokenToTitle.get(windowIdString);
50          windowId.setFormatter(
51            new FixedStringFormatter(
52              windowTitle
53                ? `0x${windowIdString} (${windowTitle})`
54                : `0x${windowIdString}`,
55            ),
56          );
57        }
58      });
59  }
60}
61