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 | |
| 17 | import {moveTrack} from '../common/actions'; |
| 18 | import {TrackState} from '../common/state'; |
| 19 | |
| 20 | import {globals} from './globals'; |
| 21 | import {drawGridLines} from './gridline_helper'; |
| 22 | import {quietDispatch} from './mithril_helpers'; |
| 23 | import {Panel} from './panel'; |
| 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 | |
| 31 | const TrackShell = { |
| 32 | view({attrs}) { |
| 33 | return m( |
| 34 | '.track-shell', |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 35 | m('h1', attrs.trackState.name), |
Hector Dearman | 3ae7eb7 | 2018-08-14 17:51:14 +0100 | [diff] [blame] | 36 | m(TrackMoveButton, { |
| 37 | direction: 'up', |
| 38 | trackId: attrs.trackState.id, |
| 39 | }), |
| 40 | m(TrackMoveButton, { |
| 41 | direction: 'down', |
| 42 | trackId: attrs.trackState.id, |
| 43 | })); |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 44 | }, |
| 45 | } as m.Component<{trackState: TrackState}>; |
| 46 | |
| 47 | const TrackContent = { |
| 48 | view({attrs}) { |
| 49 | return m('.track-content', { |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 50 | onmousemove: (e: MouseEvent) => { |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 51 | attrs.track.onMouseMove({x: e.layerX, y: e.layerY}); |
Primiano Tucci | f30cd9c | 2018-08-13 01:53:26 +0200 | [diff] [blame] | 52 | globals.rafScheduler.scheduleOneRedraw(); |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 53 | }, |
| 54 | onmouseout: () => { |
| 55 | attrs.track.onMouseOut(); |
Primiano Tucci | f30cd9c | 2018-08-13 01:53:26 +0200 | [diff] [blame] | 56 | globals.rafScheduler.scheduleOneRedraw(); |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 57 | }, |
| 58 | }, ); |
| 59 | } |
| 60 | } as m.Component<{track: Track}>; |
| 61 | |
| 62 | const TrackComponent = { |
| 63 | view({attrs}) { |
| 64 | return m('.track', [ |
| 65 | m(TrackShell, {trackState: attrs.trackState}), |
| 66 | m(TrackContent, {track: attrs.track}) |
Hector Dearman | 3ae7eb7 | 2018-08-14 17:51:14 +0100 | [diff] [blame] | 67 | ]); |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 68 | } |
| 69 | } as m.Component<{trackState: TrackState, track: Track}>; |
| 70 | |
| 71 | const TrackMoveButton = { |
| 72 | view({attrs}) { |
| 73 | return m( |
| 74 | 'i.material-icons.track-move-icons', |
| 75 | { |
| 76 | onclick: quietDispatch(moveTrack(attrs.trackId, attrs.direction)), |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 77 | }, |
| 78 | attrs.direction === 'up' ? 'arrow_upward_alt' : 'arrow_downward_alt'); |
| 79 | } |
| 80 | } as m.Component<{ |
| 81 | direction: 'up' | 'down', |
| 82 | trackId: string, |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 83 | }, |
| 84 | {}>; |
| 85 | |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame^] | 86 | export class TrackPanel extends Panel { |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 87 | private track: Track; |
| 88 | constructor(public trackState: TrackState) { |
| 89 | // TODO: Since ES6 modules are asynchronous and it is conceivable that we |
| 90 | // want to load a track implementation on demand, we should not rely here on |
| 91 | // the fact that the track is already registered. We should show some |
| 92 | // default content until a track implementation is found. |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame^] | 93 | super(); |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 94 | const trackCreator = trackRegistry.get(this.trackState.kind); |
| 95 | this.track = trackCreator.create(this.trackState); |
| 96 | } |
| 97 | |
Primiano Tucci | 9b5f13e | 2018-08-10 00:36:19 +0100 | [diff] [blame] | 98 | getHeight(): number { |
| 99 | return this.track.getHeight(); |
| 100 | } |
| 101 | |
Primiano Tucci | f30cd9c | 2018-08-13 01:53:26 +0200 | [diff] [blame] | 102 | updateDom(dom: HTMLElement): void { |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 103 | // TODO: Let tracks render DOM in the content area. |
| 104 | m.render( |
| 105 | dom, |
| 106 | m(TrackComponent, {trackState: this.trackState, track: this.track})); |
| 107 | } |
| 108 | |
| 109 | renderCanvas(ctx: CanvasRenderingContext2D) { |
| 110 | ctx.translate(TRACK_SHELL_WIDTH, 0); |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 111 | drawGridLines( |
| 112 | ctx, |
| 113 | globals.frontendLocalState.timeScale, |
Primiano Tucci | f30cd9c | 2018-08-13 01:53:26 +0200 | [diff] [blame] | 114 | globals.frontendLocalState.visibleWindowTime, |
Primiano Tucci | 9b5f13e | 2018-08-10 00:36:19 +0100 | [diff] [blame] | 115 | this.track.getHeight()); |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 116 | |
Deepanjan Roy | 9d95a25 | 2018-08-09 10:10:19 -0400 | [diff] [blame] | 117 | this.track.renderCanvas(ctx); |
| 118 | } |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame^] | 119 | } |