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 {Transformer} from 'app/components/timeline/mini-timeline/transformer';
18import {Segment} from 'app/components/timeline/segment';
19import {TimeRange} from 'common/time';
20import {Trace} from 'trace/trace';
21import {MiniTimelineDrawerOutput} from './mini_timeline_drawer_output';
22
23export type TimelineTraces = Map<Trace<object>, TimelineTrace>;
24
25export interface TimelineTrace {
26  points: number[];
27  segments: Segment[];
28  activePoint: number | undefined;
29  activeSegment: Segment | undefined;
30}
31
32export class MiniCanvasDrawerData {
33  constructor(
34    public selectedPosition: number,
35    public selection: Segment,
36    private timelineTracesGetter: () => Promise<TimelineTraces>,
37    public transformer: Transformer,
38    public bookmarks: number[],
39  ) {}
40
41  private traces: TimelineTraces | undefined = undefined;
42
43  async getTimelineTraces(): Promise<TimelineTraces> {
44    if (this.traces === undefined) {
45      this.traces = await this.timelineTracesGetter();
46    }
47    return this.traces;
48  }
49
50  toOutput(): MiniTimelineDrawerOutput {
51    return new MiniTimelineDrawerOutput(
52      this.transformer.untransform(this.selectedPosition),
53      new TimeRange(
54        this.transformer.untransform(this.selection.from),
55        this.transformer.untransform(this.selection.to),
56      ),
57    );
58  }
59}
60