blob: d2e121d5c3c00c67306c3b178ee814c50965752d [file] [log] [blame]
Deepanjan Roy9d95a252018-08-09 10:10:19 -04001// 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 this 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 * as m from 'mithril';
16
17import {moveTrack} from '../common/actions';
18import {TrackState} from '../common/state';
19
20import {globals} from './globals';
21import {drawGridLines} from './gridline_helper';
Deepanjan Roy1f658fe2018-09-11 08:38:17 -040022import {Panel, PanelSize} from './panel';
Deepanjan Roy9d95a252018-08-09 10:10:19 -040023import {Track} from './track';
24import {trackRegistry} from './track_registry';
25
Hector Dearman3ae7eb72018-08-14 17:51:14 +010026// TODO(hjd): We should remove the constant where possible.
27// If any uses can't be removed we should read this constant from CSS.
Primiano Tuccidc3dbcb2018-08-10 00:34:33 +010028export const TRACK_SHELL_WIDTH = 300;
Deepanjan Roy9d95a252018-08-09 10:10:19 -040029
30const TrackShell = {
31 view({attrs}) {
32 return m(
33 '.track-shell',
Deepanjan Roy9d95a252018-08-09 10:10:19 -040034 m('h1', attrs.trackState.name),
Hector Dearman3ae7eb72018-08-14 17:51:14 +010035 m(TrackMoveButton, {
36 direction: 'up',
37 trackId: attrs.trackState.id,
38 }),
39 m(TrackMoveButton, {
40 direction: 'down',
41 trackId: attrs.trackState.id,
42 }));
Deepanjan Roy9d95a252018-08-09 10:10:19 -040043 },
44} as m.Component<{trackState: TrackState}>;
45
46const TrackContent = {
47 view({attrs}) {
48 return m('.track-content', {
Deepanjan Roy9d95a252018-08-09 10:10:19 -040049 onmousemove: (e: MouseEvent) => {
Deepanjan Roy9d95a252018-08-09 10:10:19 -040050 attrs.track.onMouseMove({x: e.layerX, y: e.layerY});
Deepanjan Royf190cb22018-08-28 10:43:07 -040051 globals.rafScheduler.scheduleRedraw();
Deepanjan Roy9d95a252018-08-09 10:10:19 -040052 },
53 onmouseout: () => {
54 attrs.track.onMouseOut();
Deepanjan Royf190cb22018-08-28 10:43:07 -040055 globals.rafScheduler.scheduleRedraw();
Deepanjan Roy9d95a252018-08-09 10:10:19 -040056 },
57 }, );
58 }
59} as m.Component<{track: Track}>;
60
61const TrackComponent = {
62 view({attrs}) {
63 return m('.track', [
64 m(TrackShell, {trackState: attrs.trackState}),
65 m(TrackContent, {track: attrs.track})
Hector Dearman3ae7eb72018-08-14 17:51:14 +010066 ]);
Deepanjan Roy9d95a252018-08-09 10:10:19 -040067 }
68} as m.Component<{trackState: TrackState, track: Track}>;
69
70const TrackMoveButton = {
71 view({attrs}) {
72 return m(
73 'i.material-icons.track-move-icons',
74 {
Deepanjan Roy112ff6a2018-09-10 08:31:43 -040075 onclick: () =>
76 globals.dispatch(moveTrack(attrs.trackId, attrs.direction)),
Deepanjan Roy9d95a252018-08-09 10:10:19 -040077 },
78 attrs.direction === 'up' ? 'arrow_upward_alt' : 'arrow_downward_alt');
79 }
80} as m.Component<{
81 direction: 'up' | 'down',
82 trackId: string,
Deepanjan Roy9d95a252018-08-09 10:10:19 -040083},
84 {}>;
85
Deepanjan Roy1f658fe2018-09-11 08:38:17 -040086interface TrackPanelAttrs {
87 id: string;
88}
89
90export class TrackPanel extends Panel<TrackPanelAttrs> {
Deepanjan Roy9d95a252018-08-09 10:10:19 -040091 private track: Track;
Deepanjan Roy1f658fe2018-09-11 08:38:17 -040092 private trackState: TrackState;
93 constructor(vnode: m.CVnode<TrackPanelAttrs>) {
Deepanjan Royabd79aa2018-08-28 07:29:15 -040094 super();
Deepanjan Roy1f658fe2018-09-11 08:38:17 -040095 this.trackState = globals.state.tracks[vnode.attrs.id];
Deepanjan Roy9d95a252018-08-09 10:10:19 -040096 const trackCreator = trackRegistry.get(this.trackState.kind);
97 this.track = trackCreator.create(this.trackState);
98 }
99
Deepanjan Roy1f658fe2018-09-11 08:38:17 -0400100 view() {
101 return m(
102 '.track',
103 {
104 style: {
105 height: `${this.track.getHeight()}px`,
106 }
107 },
108 [
109 m(TrackShell, {trackState: this.trackState}),
110 m(TrackContent, {track: this.track})
111 ]);
112 return m(TrackComponent, {trackState: this.trackState, track: this.track});
Primiano Tucci9b5f13e2018-08-10 00:36:19 +0100113 }
114
Deepanjan Roy1f658fe2018-09-11 08:38:17 -0400115 renderCanvas(ctx: CanvasRenderingContext2D, size: PanelSize) {
Deepanjan Roy9d95a252018-08-09 10:10:19 -0400116 ctx.translate(TRACK_SHELL_WIDTH, 0);
Deepanjan Roy9d95a252018-08-09 10:10:19 -0400117 drawGridLines(
118 ctx,
119 globals.frontendLocalState.timeScale,
Primiano Tuccif30cd9c2018-08-13 01:53:26 +0200120 globals.frontendLocalState.visibleWindowTime,
Deepanjan Roy1f658fe2018-09-11 08:38:17 -0400121 size.height);
Deepanjan Roy9d95a252018-08-09 10:10:19 -0400122
Deepanjan Roy9d95a252018-08-09 10:10:19 -0400123 this.track.renderCanvas(ctx);
124 }
Deepanjan Royabd79aa2018-08-28 07:29:15 -0400125}