xref: /aosp_15_r20/development/tools/winscope/src/viewers/viewer_window_manager/presenter.ts (revision 90c8c64db3049935a07c6143d7fd006e26f8ecca)
1/*
2 * Copyright (C) 2022 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 {PersistentStoreProxy} from 'common/persistent_store_proxy';
18import {Store} from 'common/store';
19import {Trace} from 'trace/trace';
20import {Traces} from 'trace/traces';
21import {TraceType} from 'trace/trace_type';
22import {HierarchyTreeNode} from 'trace/tree_node/hierarchy_tree_node';
23import {
24  AbstractHierarchyViewerPresenter,
25  NotifyHierarchyViewCallbackType,
26} from 'viewers/common/abstract_hierarchy_viewer_presenter';
27import {VISIBLE_CHIP} from 'viewers/common/chip';
28import {DisplayIdentifier} from 'viewers/common/display_identifier';
29import {HierarchyPresenter} from 'viewers/common/hierarchy_presenter';
30import {PropertiesPresenter} from 'viewers/common/properties_presenter';
31import {RectsPresenter} from 'viewers/common/rects_presenter';
32import {TextFilter} from 'viewers/common/text_filter';
33import {UiHierarchyTreeNode} from 'viewers/common/ui_hierarchy_tree_node';
34import {UI_RECT_FACTORY} from 'viewers/common/ui_rect_factory';
35import {UserOptions} from 'viewers/common/user_options';
36import {UiRect} from 'viewers/components/rects/ui_rect';
37import {UpdateDisplayNames} from './operations/update_display_names';
38import {UiData} from './ui_data';
39
40export class Presenter extends AbstractHierarchyViewerPresenter<UiData> {
41  static readonly DENYLIST_PROPERTY_NAMES = [
42    'name',
43    'children',
44    'dpiX',
45    'dpiY',
46  ];
47
48  protected override hierarchyPresenter = new HierarchyPresenter(
49    PersistentStoreProxy.new<UserOptions>(
50      'WmHierarchyOptions',
51      {
52        showDiff: {
53          name: 'Show diff',
54          enabled: false,
55          isUnavailable: false,
56        },
57        showOnlyVisible: {
58          name: 'Show only',
59          chip: VISIBLE_CHIP,
60          enabled: false,
61        },
62        simplifyNames: {
63          name: 'Simplify names',
64          enabled: true,
65        },
66        flat: {
67          name: 'Flat',
68          enabled: false,
69        },
70      },
71      this.storage,
72    ),
73    new TextFilter(),
74    Presenter.DENYLIST_PROPERTY_NAMES,
75    true,
76    false,
77    this.getEntryFormattedTimestamp,
78    [[TraceType.WINDOW_MANAGER, [new UpdateDisplayNames()]]],
79  );
80  protected override rectsPresenter = new RectsPresenter(
81    PersistentStoreProxy.new<UserOptions>(
82      'WmRectsOptions',
83      {
84        ignoreRectShowState: {
85          name: 'Ignore',
86          icon: 'visibility',
87          enabled: false,
88        },
89        showOnlyVisible: {
90          name: 'Show only',
91          chip: VISIBLE_CHIP,
92          enabled: false,
93        },
94      },
95      this.storage,
96    ),
97    (tree: HierarchyTreeNode) => UI_RECT_FACTORY.makeUiRects(tree),
98    this.getDisplays,
99    this.convertRectIdtoContainerName,
100  );
101  protected override propertiesPresenter = new PropertiesPresenter(
102    PersistentStoreProxy.new<UserOptions>(
103      'WmPropertyOptions',
104      {
105        showDiff: {
106          name: 'Show diff',
107          enabled: false,
108          isUnavailable: false,
109        },
110        showDefaults: {
111          name: 'Show defaults',
112          enabled: false,
113          tooltip: `If checked, shows the value of all properties.
114Otherwise, hides all properties whose value is
115the default for its data type.`,
116        },
117      },
118      this.storage,
119    ),
120    new TextFilter(),
121    Presenter.DENYLIST_PROPERTY_NAMES,
122  );
123  protected override multiTraceType = undefined;
124
125  constructor(
126    trace: Trace<HierarchyTreeNode>,
127    traces: Traces,
128    storage: Readonly<Store>,
129    notifyViewCallback: NotifyHierarchyViewCallbackType<UiData>,
130  ) {
131    super(trace, traces, storage, notifyViewCallback, new UiData());
132  }
133
134  override async onHighlightedNodeChange(item: UiHierarchyTreeNode) {
135    await this.applyHighlightedNodeChange(item);
136    this.refreshUIData();
137  }
138
139  override async onHighlightedIdChange(newId: string) {
140    await this.applyHighlightedIdChange(newId);
141    this.refreshUIData();
142  }
143
144  protected override getOverrideDisplayName(
145    selected: [Trace<HierarchyTreeNode>, HierarchyTreeNode],
146  ): string | undefined {
147    if (!selected[1].isRoot()) {
148      return undefined;
149    }
150    return this.hierarchyPresenter
151      .getCurrentHierarchyTreeNames(selected[0])
152      ?.at(0);
153  }
154
155  protected override keepCalculated(tree: HierarchyTreeNode): boolean {
156    return false;
157  }
158
159  protected override refreshUIData() {
160    this.refreshHierarchyViewerUiData();
161  }
162
163  private getDisplays(rects: UiRect[]): DisplayIdentifier[] {
164    const ids: DisplayIdentifier[] = [];
165    rects.forEach((rect: UiRect) => {
166      if (!rect.isDisplay) return;
167      const displayName = rect.label.slice(10, rect.label.length);
168      ids.push({
169        displayId: rect.id,
170        groupId: rect.groupId,
171        name: displayName,
172        isActive: rect.isActiveDisplay,
173      });
174    });
175    return ids.sort();
176  }
177
178  private convertRectIdtoContainerName(id: string) {
179    const parts = id.split(' ');
180    return parts.slice(2).join(' ');
181  }
182}
183