Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 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 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 | |
| 15 | import * as m from 'mithril'; |
| 16 | |
Hector Dearman | a926f4e | 2018-09-17 13:33:30 +0100 | [diff] [blame] | 17 | import {moveTrack, toggleTrackPinned} from '../common/actions'; |
| 18 | import {Action} from '../common/actions'; |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 19 | import {TrackState} from '../common/state'; |
| 20 | |
| 21 | import {globals} from './globals'; |
| 22 | import {drawGridLines} from './gridline_helper'; |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 23 | import {Panel, PanelSize} from './panel'; |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 24 | import {Track} from './track'; |
| 25 | import {trackRegistry} from './track_registry'; |
| 26 | |
Hector Dearman | 3ae7eb7 | 2018-08-14 17:51:14 +0100 | [diff] [blame] | 27 | // 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 Tucci | dc3dbcb | 2018-08-10 00:34:33 +0100 | [diff] [blame] | 29 | export const TRACK_SHELL_WIDTH = 300; |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 30 | |
Hector Dearman | a926f4e | 2018-09-17 13:33:30 +0100 | [diff] [blame] | 31 | function isPinned(id: string) { |
| 32 | return globals.state.pinnedTracks.indexOf(id) !== -1; |
| 33 | } |
| 34 | |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 35 | const TrackShell = { |
| 36 | view({attrs}) { |
| 37 | return m( |
| 38 | '.track-shell', |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 39 | m('h1', attrs.trackState.name), |
Hector Dearman | a926f4e | 2018-09-17 13:33:30 +0100 | [diff] [blame] | 40 | m(TrackButton, { |
| 41 | action: moveTrack(attrs.trackState.id, 'up'), |
| 42 | i: 'arrow_upward_alt', |
Hector Dearman | 3ae7eb7 | 2018-08-14 17:51:14 +0100 | [diff] [blame] | 43 | }), |
Hector Dearman | a926f4e | 2018-09-17 13:33:30 +0100 | [diff] [blame] | 44 | 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 Dearman | 3ae7eb7 | 2018-08-14 17:51:14 +0100 | [diff] [blame] | 51 | })); |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 52 | }, |
| 53 | } as m.Component<{trackState: TrackState}>; |
| 54 | |
| 55 | const TrackContent = { |
| 56 | view({attrs}) { |
| 57 | return m('.track-content', { |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 58 | onmousemove: (e: MouseEvent) => { |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 59 | attrs.track.onMouseMove({x: e.layerX, y: e.layerY}); |
Deepanjan Roy | f190cb2 | 2018-08-28 10:43:07 -0400 | [diff] [blame] | 60 | globals.rafScheduler.scheduleRedraw(); |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 61 | }, |
| 62 | onmouseout: () => { |
| 63 | attrs.track.onMouseOut(); |
Deepanjan Roy | f190cb2 | 2018-08-28 10:43:07 -0400 | [diff] [blame] | 64 | globals.rafScheduler.scheduleRedraw(); |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 65 | }, |
| 66 | }, ); |
| 67 | } |
| 68 | } as m.Component<{track: Track}>; |
| 69 | |
| 70 | const TrackComponent = { |
| 71 | view({attrs}) { |
| 72 | return m('.track', [ |
| 73 | m(TrackShell, {trackState: attrs.trackState}), |
| 74 | m(TrackContent, {track: attrs.track}) |
Hector Dearman | 3ae7eb7 | 2018-08-14 17:51:14 +0100 | [diff] [blame] | 75 | ]); |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 76 | } |
| 77 | } as m.Component<{trackState: TrackState, track: Track}>; |
| 78 | |
Hector Dearman | a926f4e | 2018-09-17 13:33:30 +0100 | [diff] [blame] | 79 | const TrackButton = { |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 80 | view({attrs}) { |
| 81 | return m( |
Hector Dearman | a926f4e | 2018-09-17 13:33:30 +0100 | [diff] [blame] | 82 | 'i.material-icons.track-button', |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 83 | { |
Hector Dearman | a926f4e | 2018-09-17 13:33:30 +0100 | [diff] [blame] | 84 | onclick: () => globals.dispatch(attrs.action), |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 85 | }, |
Hector Dearman | a926f4e | 2018-09-17 13:33:30 +0100 | [diff] [blame] | 86 | attrs.i); |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 87 | } |
| 88 | } as m.Component<{ |
Hector Dearman | a926f4e | 2018-09-17 13:33:30 +0100 | [diff] [blame] | 89 | action: Action, |
| 90 | i: string, |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 91 | }, |
Hector Dearman | a926f4e | 2018-09-17 13:33:30 +0100 | [diff] [blame] | 92 | {}>; |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 93 | |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 94 | interface TrackPanelAttrs { |
| 95 | id: string; |
| 96 | } |
| 97 | |
| 98 | export class TrackPanel extends Panel<TrackPanelAttrs> { |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 99 | private track: Track; |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 100 | private trackState: TrackState; |
| 101 | constructor(vnode: m.CVnode<TrackPanelAttrs>) { |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 102 | super(); |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 103 | this.trackState = globals.state.tracks[vnode.attrs.id]; |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 104 | const trackCreator = trackRegistry.get(this.trackState.kind); |
| 105 | this.track = trackCreator.create(this.trackState); |
| 106 | } |
| 107 | |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 108 | 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 Tucci | 9b5f13e | 2018-08-10 00:36:19 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 123 | renderCanvas(ctx: CanvasRenderingContext2D, size: PanelSize) { |
Deepanjan Roy | 9a906ed | 2018-09-20 11:06:00 -0400 | [diff] [blame^] | 124 | ctx.save(); |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 125 | ctx.translate(TRACK_SHELL_WIDTH, 0); |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 126 | drawGridLines( |
| 127 | ctx, |
| 128 | globals.frontendLocalState.timeScale, |
Primiano Tucci | f30cd9c | 2018-08-13 01:53:26 +0200 | [diff] [blame] | 129 | globals.frontendLocalState.visibleWindowTime, |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 130 | size.height); |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 131 | |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 132 | this.track.renderCanvas(ctx); |
Deepanjan Roy | 9a906ed | 2018-09-20 11:06:00 -0400 | [diff] [blame^] | 133 | ctx.restore(); |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 134 | } |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 135 | } |