1/*
2 * Copyright (C) 2023 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 {FunctionUtils} from 'common/function_utils';
18import {WinscopeEvent} from 'messaging/winscope_event';
19import {EmitEvent} from 'messaging/winscope_event_emitter';
20import {Trace} from 'trace/trace';
21import {Traces} from 'trace/traces';
22import {TRACE_INFO} from 'trace/trace_info';
23import {TraceType} from 'trace/trace_type';
24import {HierarchyTreeNode} from 'trace/tree_node/hierarchy_tree_node';
25import {NotifyHierarchyViewCallbackType} from 'viewers/common/abstract_hierarchy_viewer_presenter';
26import {ViewerEvents} from 'viewers/common/viewer_events';
27import {View, Viewer, ViewType} from 'viewers/viewer';
28import {Presenter} from './presenter';
29import {UiData} from './ui_data';
30
31export class ViewerViewCapture implements Viewer {
32  static readonly DEPENDENCIES: TraceType[] = [TraceType.VIEW_CAPTURE];
33
34  private readonly traces: Traces;
35  private readonly htmlElement: HTMLElement;
36  private readonly presenter: Presenter;
37  private readonly view: View;
38  private emitAppEvent: EmitEvent = FunctionUtils.DO_NOTHING_ASYNC;
39
40  constructor(traces: Traces, storage: Storage) {
41    this.traces = traces;
42    this.htmlElement = document.createElement('viewer-view-capture');
43    const notifyViewCallback = (uiData: UiData) => {
44      (this.htmlElement as any).inputData = uiData;
45    };
46    this.presenter = new Presenter(
47      traces,
48      storage,
49      notifyViewCallback as NotifyHierarchyViewCallbackType,
50    );
51    this.presenter.addEventListeners(this.htmlElement);
52
53    this.htmlElement.addEventListener(
54      ViewerEvents.MiniRectsDblClick,
55      async (event) => {
56        await this.presenter.onMiniRectsDoubleClick();
57      },
58    );
59
60    this.view = new View(
61      ViewType.TAB,
62      this.getTraces(),
63      this.htmlElement,
64      TRACE_INFO[TraceType.VIEW_CAPTURE].name,
65    );
66  }
67
68  setEmitEvent(callback: EmitEvent) {
69    this.presenter.setEmitEvent(callback);
70  }
71
72  async onWinscopeEvent(event: WinscopeEvent) {
73    await this.presenter.onAppEvent(event);
74  }
75
76  getViews(): View[] {
77    return [this.view];
78  }
79
80  getTraces(): Array<Trace<HierarchyTreeNode>> {
81    return this.presenter.getTraces();
82  }
83}
84