blob: f08b8682fac688bf14d711e98d6a8df94a31aa65 [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
Hector Dearmana926f4e2018-09-17 13:33:30 +010017import {moveTrack, toggleTrackPinned} from '../common/actions';
18import {Action} from '../common/actions';
Deepanjan Roy9d95a252018-08-09 10:10:19 -040019import {TrackState} from '../common/state';
20
21import {globals} from './globals';
22import {drawGridLines} from './gridline_helper';
Deepanjan Roy1f658fe2018-09-11 08:38:17 -040023import {Panel, PanelSize} from './panel';
Deepanjan Roy9d95a252018-08-09 10:10:19 -040024import {Track} from './track';
25import {trackRegistry} from './track_registry';
26
Hector Dearman3ae7eb72018-08-14 17:51:14 +010027// TODO(hjd): We should remove the constant where possible.
28// If any uses can't be removed we should read this constant from CSS.
Primiano Tuccidc3dbcb2018-08-10 00:34:33 +010029export const TRACK_SHELL_WIDTH = 300;
Deepanjan Roy9d95a252018-08-09 10:10:19 -040030
Hector Dearmana926f4e2018-09-17 13:33:30 +010031function isPinned(id: string) {
32 return globals.state.pinnedTracks.indexOf(id) !== -1;
33}
34
Deepanjan Roy9d95a252018-08-09 10:10:19 -040035const TrackShell = {
36 view({attrs}) {
37 return m(
38 '.track-shell',
Deepanjan Roy9d95a252018-08-09 10:10:19 -040039 m('h1', attrs.trackState.name),
Hector Dearmana926f4e2018-09-17 13:33:30 +010040 m(TrackButton, {
41 action: moveTrack(attrs.trackState.id, 'up'),
42 i: 'arrow_upward_alt',
Hector Dearman3ae7eb72018-08-14 17:51:14 +010043 }),
Hector Dearmana926f4e2018-09-17 13:33:30 +010044 m(TrackButton, {
45 action: moveTrack(attrs.trackState.id, 'down'),
46 i: 'arrow_downward_alt',
47 }),
48 m(TrackButton, {
49 action: toggleTrackPinned(attrs.trackState.id),
50 i: isPinned(attrs.trackState.id) ? 'star' : 'star_border',
Hector Dearman3ae7eb72018-08-14 17:51:14 +010051 }));
Deepanjan Roy9d95a252018-08-09 10:10:19 -040052 },
53} as m.Component<{trackState: TrackState}>;
54
55const TrackContent = {
56 view({attrs}) {
57 return m('.track-content', {
Deepanjan Roy9d95a252018-08-09 10:10:19 -040058 onmousemove: (e: MouseEvent) => {
Deepanjan Roy9d95a252018-08-09 10:10:19 -040059 attrs.track.onMouseMove({x: e.layerX, y: e.layerY});
Deepanjan Royf190cb22018-08-28 10:43:07 -040060 globals.rafScheduler.scheduleRedraw();
Deepanjan Roy9d95a252018-08-09 10:10:19 -040061 },
62 onmouseout: () => {
63 attrs.track.onMouseOut();
Deepanjan Royf190cb22018-08-28 10:43:07 -040064 globals.rafScheduler.scheduleRedraw();
Deepanjan Roy9d95a252018-08-09 10:10:19 -040065 },
66 }, );
67 }
68} as m.Component<{track: Track}>;
69
70const TrackComponent = {
71 view({attrs}) {
72 return m('.track', [
73 m(TrackShell, {trackState: attrs.trackState}),
74 m(TrackContent, {track: attrs.track})
Hector Dearman3ae7eb72018-08-14 17:51:14 +010075 ]);
Deepanjan Roy9d95a252018-08-09 10:10:19 -040076 }
77} as m.Component<{trackState: TrackState, track: Track}>;
78
Hector Dearmana926f4e2018-09-17 13:33:30 +010079const TrackButton = {
Deepanjan Roy9d95a252018-08-09 10:10:19 -040080 view({attrs}) {
81 return m(
Hector Dearmana926f4e2018-09-17 13:33:30 +010082 'i.material-icons.track-button',
Deepanjan Roy9d95a252018-08-09 10:10:19 -040083 {
Hector Dearmana926f4e2018-09-17 13:33:30 +010084 onclick: () => globals.dispatch(attrs.action),
Deepanjan Roy9d95a252018-08-09 10:10:19 -040085 },
Hector Dearmana926f4e2018-09-17 13:33:30 +010086 attrs.i);
Deepanjan Roy9d95a252018-08-09 10:10:19 -040087 }
88} as m.Component<{
Hector Dearmana926f4e2018-09-17 13:33:30 +010089 action: Action,
90 i: string,
Deepanjan Roy9d95a252018-08-09 10:10:19 -040091},
Hector Dearmana926f4e2018-09-17 13:33:30 +010092 {}>;
Deepanjan Roy9d95a252018-08-09 10:10:19 -040093
Deepanjan Roy1f658fe2018-09-11 08:38:17 -040094interface TrackPanelAttrs {
95 id: string;
96}
97
98export class TrackPanel extends Panel<TrackPanelAttrs> {
Deepanjan Roy9d95a252018-08-09 10:10:19 -040099 private track: Track;
Deepanjan Roy1f658fe2018-09-11 08:38:17 -0400100 private trackState: TrackState;
101 constructor(vnode: m.CVnode<TrackPanelAttrs>) {
Deepanjan Royabd79aa2018-08-28 07:29:15 -0400102 super();
Deepanjan Roy1f658fe2018-09-11 08:38:17 -0400103 this.trackState = globals.state.tracks[vnode.attrs.id];
Deepanjan Roy9d95a252018-08-09 10:10:19 -0400104 const trackCreator = trackRegistry.get(this.trackState.kind);
105 this.track = trackCreator.create(this.trackState);
106 }
107
Deepanjan Roy1f658fe2018-09-11 08:38:17 -0400108 view() {
109 return m(
110 '.track',
111 {
112 style: {
113 height: `${this.track.getHeight()}px`,
114 }
115 },
116 [
117 m(TrackShell, {trackState: this.trackState}),
118 m(TrackContent, {track: this.track})
119 ]);
120 return m(TrackComponent, {trackState: this.trackState, track: this.track});
Primiano Tucci9b5f13e2018-08-10 00:36:19 +0100121 }
122
Deepanjan Roy1f658fe2018-09-11 08:38:17 -0400123 renderCanvas(ctx: CanvasRenderingContext2D, size: PanelSize) {
Deepanjan Roy9a906ed2018-09-20 11:06:00 -0400124 ctx.save();
Deepanjan Roy9d95a252018-08-09 10:10:19 -0400125 ctx.translate(TRACK_SHELL_WIDTH, 0);
Deepanjan Roy9d95a252018-08-09 10:10:19 -0400126 drawGridLines(
127 ctx,
128 globals.frontendLocalState.timeScale,
Primiano Tuccif30cd9c2018-08-13 01:53:26 +0200129 globals.frontendLocalState.visibleWindowTime,
Deepanjan Roy1f658fe2018-09-11 08:38:17 -0400130 size.height);
Deepanjan Roy9d95a252018-08-09 10:10:19 -0400131
Deepanjan Roy9d95a252018-08-09 10:10:19 -0400132 this.track.renderCanvas(ctx);
Deepanjan Roy9a906ed2018-09-20 11:06:00 -0400133 ctx.restore();
Deepanjan Roy9d95a252018-08-09 10:10:19 -0400134 }
Deepanjan Royabd79aa2018-08-28 07:29:15 -0400135}