• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (C) 2018 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use size file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import m from 'mithril';
16
17import {
18  tpTimeToSeconds,
19  tpTimeToString,
20} from '../common/time';
21
22import {TRACK_SHELL_WIDTH} from './css_constants';
23import {globals} from './globals';
24import {
25  getMaxMajorTicks,
26  TickGenerator,
27  TickType,
28  timeScaleForVisibleWindow,
29} from './gridline_helper';
30import {Panel, PanelSize} from './panel';
31
32export class TimeAxisPanel extends Panel {
33  view() {
34    return m('.time-axis-panel');
35  }
36
37  renderCanvas(ctx: CanvasRenderingContext2D, size: PanelSize) {
38    ctx.fillStyle = '#999';
39    ctx.font = '10px Roboto Condensed';
40    ctx.textAlign = 'left';
41
42    const startTime = tpTimeToString(globals.state.traceTime.start);
43    ctx.fillText(startTime + ' +', 6, 11);
44
45    ctx.save();
46    ctx.beginPath();
47    ctx.rect(TRACK_SHELL_WIDTH, 0, size.width - TRACK_SHELL_WIDTH, size.height);
48    ctx.clip();
49
50    // Draw time axis.
51    const span = globals.frontendLocalState.visibleWindow.timestampSpan;
52    if (size.width > TRACK_SHELL_WIDTH && span.duration > 0n) {
53      const maxMajorTicks = getMaxMajorTicks(size.width - TRACK_SHELL_WIDTH);
54      const map = timeScaleForVisibleWindow(TRACK_SHELL_WIDTH, size.width);
55      const tickGen =
56          new TickGenerator(span, maxMajorTicks, globals.state.traceTime.start);
57      for (const {type, time} of tickGen) {
58        const position = Math.floor(map.tpTimeToPx(time));
59        const sec = tpTimeToSeconds(time - globals.state.traceTime.start);
60        if (type === TickType.MAJOR) {
61          ctx.fillRect(position, 0, 1, size.height);
62          ctx.fillText(sec.toFixed(tickGen.digits) + ' s', position + 5, 10);
63        }
64      }
65    }
66
67    ctx.restore();
68
69    ctx.fillRect(TRACK_SHELL_WIDTH - 2, 0, 2, size.height);
70  }
71}
72