aboutsummaryrefslogtreecommitdiff
path: root/ui/src/frontend/time_axis_panel.ts
blob: 3f6dc649932d3ceb81623d2fd13717acb864f7fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright (C) 2018 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use size file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import m from 'mithril';

import {timeToString} from '../common/time';

import {TRACK_SHELL_WIDTH} from './css_constants';
import {globals} from './globals';
import {
  TickGenerator,
  TickType,
  timeScaleForVisibleWindow,
} from './gridline_helper';
import {Panel, PanelSize} from './panel';

export class TimeAxisPanel extends Panel {
  view() {
    return m('.time-axis-panel');
  }

  renderCanvas(ctx: CanvasRenderingContext2D, size: PanelSize) {
    ctx.fillStyle = '#999';
    ctx.font = '10px Roboto Condensed';
    ctx.textAlign = 'left';

    const startTime = timeToString(globals.state.traceTime.startSec);
    ctx.fillText(startTime + ' +', 6, 11);

    // Draw time axis.
    const timeScale = timeScaleForVisibleWindow(TRACK_SHELL_WIDTH, size.width);
    if (timeScale.timeSpan.duration > 0 && timeScale.widthPx > 0) {
      const tickGen = new TickGenerator(timeScale);
      for (const {type, time, position} of tickGen) {
        if (type === TickType.MAJOR) {
          ctx.fillRect(position, 0, 1, size.height);
          ctx.fillText(time.toFixed(tickGen.digits) + ' s', position + 5, 10);
        }
      }
    }

    ctx.fillRect(TRACK_SHELL_WIDTH - 2, 0, 2, size.height);
  }
}