Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -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'; |
Isabelle Taylor | a16dec2 | 2019-12-03 16:34:13 +0000 | [diff] [blame] | 16 | import {TimestampedAreaSelection} from 'src/common/state'; |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 17 | |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 18 | import {assertExists, assertTrue} from '../base/logging'; |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 19 | |
Isabelle Taylor | a16dec2 | 2019-12-03 16:34:13 +0000 | [diff] [blame] | 20 | import {TOPBAR_HEIGHT, TRACK_SHELL_WIDTH} from './css_constants'; |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 21 | import {globals} from './globals'; |
Hector Dearman | 191741d | 2020-06-09 16:00:40 +0100 | [diff] [blame] | 22 | import {isPanelVNode, Panel, PanelSize, PanelVNode} from './panel'; |
Deepanjan Roy | 9a906ed | 2018-09-20 11:06:00 -0400 | [diff] [blame] | 23 | import { |
| 24 | debugNow, |
| 25 | perfDebug, |
| 26 | perfDisplay, |
| 27 | RunningStatistics, |
| 28 | runningStatStr |
| 29 | } from './perf'; |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * If the panel container scrolls, the backing canvas height is |
| 33 | * SCROLLING_CANVAS_OVERDRAW_FACTOR * parent container height. |
| 34 | */ |
Hector Dearman | 0d82591 | 2019-02-15 10:33:37 +0000 | [diff] [blame] | 35 | const SCROLLING_CANVAS_OVERDRAW_FACTOR = 1.2; |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 36 | |
Deepanjan Roy | 4fa0ecc | 2018-10-03 16:10:46 -0400 | [diff] [blame] | 37 | // We need any here so we can accept vnodes with arbitrary attrs. |
| 38 | // tslint:disable-next-line:no-any |
| 39 | export type AnyAttrsVnode = m.Vnode<any, {}>; |
| 40 | |
Isabelle Taylor | a16dec2 | 2019-12-03 16:34:13 +0000 | [diff] [blame] | 41 | export interface Attrs { |
Deepanjan Roy | 4fa0ecc | 2018-10-03 16:10:46 -0400 | [diff] [blame] | 42 | panels: AnyAttrsVnode[]; |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 43 | doesScroll: boolean; |
Isabelle Taylor | f41b7c7 | 2019-07-24 13:55:43 +0100 | [diff] [blame] | 44 | kind: 'TRACKS'|'OVERVIEW'|'DETAILS'; |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 45 | } |
| 46 | |
Isabelle Taylor | a16dec2 | 2019-12-03 16:34:13 +0000 | [diff] [blame] | 47 | interface PanelPosition { |
Isabelle Taylor | 9b4a81d | 2020-01-31 11:11:02 +0000 | [diff] [blame] | 48 | id: string; |
Isabelle Taylor | a16dec2 | 2019-12-03 16:34:13 +0000 | [diff] [blame] | 49 | height: number; |
| 50 | width: number; |
| 51 | x: number; |
| 52 | y: number; |
| 53 | } |
| 54 | |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 55 | export class PanelContainer implements m.ClassComponent<Attrs> { |
| 56 | // These values are updated with proper values in oncreate. |
| 57 | private parentWidth = 0; |
| 58 | private parentHeight = 0; |
| 59 | private scrollTop = 0; |
Isabelle Taylor | a16dec2 | 2019-12-03 16:34:13 +0000 | [diff] [blame] | 60 | private panelPositions: PanelPosition[] = []; |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 61 | private totalPanelHeight = 0; |
| 62 | private canvasHeight = 0; |
Isabelle Taylor | a16dec2 | 2019-12-03 16:34:13 +0000 | [diff] [blame] | 63 | private prevAreaSelection?: TimestampedAreaSelection; |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 64 | |
Deepanjan Roy | 9a906ed | 2018-09-20 11:06:00 -0400 | [diff] [blame] | 65 | private panelPerfStats = new WeakMap<Panel, RunningStatistics>(); |
| 66 | private perfStats = { |
| 67 | totalPanels: 0, |
| 68 | panelsOnCanvas: 0, |
| 69 | renderStats: new RunningStatistics(10), |
| 70 | }; |
| 71 | |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame] | 72 | // Attrs received in the most recent mithril redraw. We receive a new vnode |
| 73 | // with new attrs on every redraw, and we cache it here so that resize |
| 74 | // listeners and canvas redraw callbacks can access it. |
| 75 | private attrs: Attrs; |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 76 | |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 77 | private ctx?: CanvasRenderingContext2D; |
| 78 | |
| 79 | private onResize: () => void = () => {}; |
| 80 | private parentOnScroll: () => void = () => {}; |
| 81 | private canvasRedrawer: () => void; |
| 82 | |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame] | 83 | get canvasOverdrawFactor() { |
| 84 | return this.attrs.doesScroll ? SCROLLING_CANVAS_OVERDRAW_FACTOR : 1; |
| 85 | } |
| 86 | |
Isabelle Taylor | a16dec2 | 2019-12-03 16:34:13 +0000 | [diff] [blame] | 87 | getPanelsInRegion(startX: number, endX: number, startY: number, endY: number): |
| 88 | AnyAttrsVnode[] { |
| 89 | const minX = Math.min(startX, endX); |
| 90 | const maxX = Math.max(startX, endX); |
| 91 | const minY = Math.min(startY, endY); |
| 92 | const maxY = Math.max(startY, endY); |
| 93 | const panels: AnyAttrsVnode[] = []; |
| 94 | for (let i = 0; i < this.panelPositions.length; i++) { |
| 95 | const pos = this.panelPositions[i]; |
| 96 | const realPosX = pos.x - TRACK_SHELL_WIDTH; |
| 97 | if (realPosX + pos.width >= minX && realPosX <= maxX && |
Isabelle Taylor | 9b4a81d | 2020-01-31 11:11:02 +0000 | [diff] [blame] | 98 | pos.y + pos.height >= minY && pos.y <= maxY && |
| 99 | this.attrs.panels[i].attrs.selectable) { |
Isabelle Taylor | a16dec2 | 2019-12-03 16:34:13 +0000 | [diff] [blame] | 100 | panels.push(this.attrs.panels[i]); |
| 101 | } |
| 102 | } |
| 103 | return panels; |
| 104 | } |
| 105 | |
| 106 | handleAreaSelection() { |
| 107 | const selection = globals.frontendLocalState.selectedArea; |
| 108 | const area = selection.area; |
| 109 | if ((this.prevAreaSelection && |
| 110 | this.prevAreaSelection.lastUpdate >= selection.lastUpdate) || |
| 111 | area === undefined || |
| 112 | globals.frontendLocalState.areaY.start === undefined || |
Isabelle Taylor | 4255fd5 | 2020-02-21 14:43:39 +0000 | [diff] [blame] | 113 | globals.frontendLocalState.areaY.end === undefined || |
| 114 | this.panelPositions.length === 0) { |
Isabelle Taylor | a16dec2 | 2019-12-03 16:34:13 +0000 | [diff] [blame] | 115 | return; |
| 116 | } |
Isabelle Taylor | 9b4a81d | 2020-01-31 11:11:02 +0000 | [diff] [blame] | 117 | // Only get panels from the current panel container if the selection began |
| 118 | // in this container. |
| 119 | const panelContainerTop = this.panelPositions[0].y; |
| 120 | const panelContainerBottom = |
| 121 | this.panelPositions[this.panelPositions.length - 1].y + |
| 122 | this.panelPositions[this.panelPositions.length - 1].height; |
| 123 | if (globals.frontendLocalState.areaY.start + TOPBAR_HEIGHT < |
| 124 | panelContainerTop || |
| 125 | globals.frontendLocalState.areaY.start + TOPBAR_HEIGHT > |
| 126 | panelContainerBottom) { |
| 127 | return; |
| 128 | } |
| 129 | |
Isabelle Taylor | a16dec2 | 2019-12-03 16:34:13 +0000 | [diff] [blame] | 130 | // The Y value is given from the top of the pan and zoom region, we want it |
| 131 | // from the top of the panel container. The parent offset corrects that. |
| 132 | const panels = this.getPanelsInRegion( |
| 133 | globals.frontendLocalState.timeScale.timeToPx(area.startSec), |
| 134 | globals.frontendLocalState.timeScale.timeToPx(area.endSec), |
| 135 | globals.frontendLocalState.areaY.start + TOPBAR_HEIGHT, |
| 136 | globals.frontendLocalState.areaY.end + TOPBAR_HEIGHT); |
| 137 | // Get the track ids from the panels. |
| 138 | const tracks = []; |
| 139 | for (const panel of panels) { |
| 140 | if (panel.attrs.id !== undefined) { |
| 141 | tracks.push(panel.attrs.id); |
| 142 | continue; |
| 143 | } |
| 144 | if (panel.attrs.trackGroupId !== undefined) { |
| 145 | const trackGroup = globals.state.trackGroups[panel.attrs.trackGroupId]; |
| 146 | // Only select a track group and all child tracks if it is closed. |
| 147 | if (trackGroup.collapsed) { |
| 148 | tracks.push(panel.attrs.trackGroupId); |
| 149 | for (const track of trackGroup.tracks) { |
| 150 | tracks.push(track); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | globals.frontendLocalState.selectArea(area.startSec, area.endSec, tracks); |
| 156 | this.prevAreaSelection = globals.frontendLocalState.selectedArea; |
| 157 | } |
| 158 | |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 159 | constructor(vnode: m.CVnode<Attrs>) { |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame] | 160 | this.attrs = vnode.attrs; |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 161 | this.canvasRedrawer = () => this.redrawCanvas(); |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 162 | globals.rafScheduler.addRedrawCallback(this.canvasRedrawer); |
Deepanjan Roy | 9a906ed | 2018-09-20 11:06:00 -0400 | [diff] [blame] | 163 | perfDisplay.addContainer(this); |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 164 | } |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 165 | |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 166 | oncreate(vnodeDom: m.CVnodeDOM<Attrs>) { |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 167 | // Save the canvas context in the state. |
| 168 | const canvas = |
| 169 | vnodeDom.dom.querySelector('.main-canvas') as HTMLCanvasElement; |
| 170 | const ctx = canvas.getContext('2d'); |
| 171 | if (!ctx) { |
| 172 | throw Error('Cannot create canvas context'); |
| 173 | } |
| 174 | this.ctx = ctx; |
| 175 | |
Isabelle Taylor | a16dec2 | 2019-12-03 16:34:13 +0000 | [diff] [blame] | 176 | this.readParentSizeFromDom(vnodeDom.dom); |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame] | 177 | this.readPanelHeightsFromDom(vnodeDom.dom); |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 178 | |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame] | 179 | this.updateCanvasDimensions(); |
| 180 | this.repositionCanvas(); |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 181 | |
| 182 | // Save the resize handler in the state so we can remove it later. |
| 183 | // TODO: Encapsulate resize handling better. |
| 184 | this.onResize = () => { |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame] | 185 | this.readParentSizeFromDom(vnodeDom.dom); |
| 186 | this.updateCanvasDimensions(); |
| 187 | this.repositionCanvas(); |
Deepanjan Roy | 112ff6a | 2018-09-10 08:31:43 -0400 | [diff] [blame] | 188 | globals.rafScheduler.scheduleFullRedraw(); |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | // Once ResizeObservers are out, we can stop accessing the window here. |
| 192 | window.addEventListener('resize', this.onResize); |
| 193 | |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 194 | // TODO(dproy): Handle change in doesScroll attribute. |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame] | 195 | if (this.attrs.doesScroll) { |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 196 | this.parentOnScroll = () => { |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame] | 197 | this.scrollTop = assertExists(vnodeDom.dom.parentElement).scrollTop; |
| 198 | this.repositionCanvas(); |
Deepanjan Roy | f190cb2 | 2018-08-28 10:43:07 -0400 | [diff] [blame] | 199 | globals.rafScheduler.scheduleRedraw(); |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 200 | }; |
| 201 | vnodeDom.dom.parentElement!.addEventListener( |
| 202 | 'scroll', this.parentOnScroll, {passive: true}); |
| 203 | } |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 204 | } |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 205 | |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 206 | onremove({attrs, dom}: m.CVnodeDOM<Attrs>) { |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 207 | window.removeEventListener('resize', this.onResize); |
| 208 | globals.rafScheduler.removeRedrawCallback(this.canvasRedrawer); |
| 209 | if (attrs.doesScroll) { |
| 210 | dom.parentElement!.removeEventListener('scroll', this.parentOnScroll); |
| 211 | } |
Deepanjan Roy | 9a906ed | 2018-09-20 11:06:00 -0400 | [diff] [blame] | 212 | perfDisplay.removeContainer(this); |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 213 | } |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 214 | |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 215 | view({attrs}: m.CVnode<Attrs>) { |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 216 | this.attrs = attrs; |
Deepanjan Roy | 9a906ed | 2018-09-20 11:06:00 -0400 | [diff] [blame] | 217 | const renderPanel = (panel: m.Vnode) => perfDebug() ? |
| 218 | m('.panel', panel, m('.debug-panel-border')) : |
Isabelle Taylor | 1acfa2b | 2019-05-17 17:26:40 +0100 | [diff] [blame] | 219 | m('.panel', {key: panel.key}, panel); |
Deepanjan Roy | 9a906ed | 2018-09-20 11:06:00 -0400 | [diff] [blame] | 220 | |
Isabelle Taylor | f4e0f68 | 2019-11-20 10:47:49 +0000 | [diff] [blame] | 221 | return [ |
| 222 | m( |
| 223 | '.scroll-limiter', |
| 224 | m('canvas.main-canvas'), |
| 225 | ), |
| 226 | m('.panels', attrs.panels.map(renderPanel)) |
| 227 | ]; |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 228 | } |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 229 | |
| 230 | onupdate(vnodeDom: m.CVnodeDOM<Attrs>) { |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame] | 231 | const totalPanelHeightChanged = this.readPanelHeightsFromDom(vnodeDom.dom); |
| 232 | const parentSizeChanged = this.readParentSizeFromDom(vnodeDom.dom); |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame] | 233 | const canvasSizeShouldChange = |
Isabelle Taylor | f41b7c7 | 2019-07-24 13:55:43 +0100 | [diff] [blame] | 234 | parentSizeChanged || !this.attrs.doesScroll && totalPanelHeightChanged; |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame] | 235 | if (canvasSizeShouldChange) { |
| 236 | this.updateCanvasDimensions(); |
| 237 | this.repositionCanvas(); |
Isabelle Taylor | f41b7c7 | 2019-07-24 13:55:43 +0100 | [diff] [blame] | 238 | if (this.attrs.kind === 'TRACKS') { |
| 239 | globals.frontendLocalState.timeScale.setLimitsPx( |
| 240 | 0, this.parentWidth - TRACK_SHELL_WIDTH); |
| 241 | } |
| 242 | this.redrawCanvas(); |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame] | 246 | private updateCanvasDimensions() { |
Hector Dearman | afa51b2 | 2018-12-11 16:38:30 +0000 | [diff] [blame] | 247 | this.canvasHeight = Math.floor( |
| 248 | this.attrs.doesScroll ? this.parentHeight * this.canvasOverdrawFactor : |
| 249 | this.totalPanelHeight); |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 250 | const ctx = assertExists(this.ctx); |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame] | 251 | const canvas = assertExists(ctx.canvas); |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 252 | canvas.style.height = `${this.canvasHeight}px`; |
Hector Dearman | 4f8ea6a | 2020-01-17 14:52:34 +0000 | [diff] [blame] | 253 | |
| 254 | // If're we're non-scrolling canvas and the scroll-limiter should always |
| 255 | // have the same height. Enforce this by explicitly setting the height. |
| 256 | if (!this.attrs.doesScroll) { |
| 257 | const scrollLimiter = canvas.parentElement; |
| 258 | if (scrollLimiter) { |
| 259 | scrollLimiter.style.height = `${this.canvasHeight}px`; |
| 260 | } |
| 261 | } |
| 262 | |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 263 | const dpr = window.devicePixelRatio; |
Isabelle Taylor | ef90242 | 2020-01-24 10:35:55 +0000 | [diff] [blame] | 264 | ctx.canvas.width = this.parentWidth * dpr; |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 265 | ctx.canvas.height = this.canvasHeight * dpr; |
| 266 | ctx.scale(dpr, dpr); |
| 267 | } |
| 268 | |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame] | 269 | private repositionCanvas() { |
| 270 | const canvas = assertExists(assertExists(this.ctx).canvas); |
Hector Dearman | afa51b2 | 2018-12-11 16:38:30 +0000 | [diff] [blame] | 271 | const canvasYStart = |
| 272 | Math.floor(this.scrollTop - this.getCanvasOverdrawHeightPerSide()); |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame] | 273 | canvas.style.transform = `translateY(${canvasYStart}px)`; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Reads dimensions of parent node. Returns true if read dimensions are |
| 278 | * different from what was cached in the state. |
| 279 | */ |
| 280 | private readParentSizeFromDom(dom: Element): boolean { |
| 281 | const oldWidth = this.parentWidth; |
| 282 | const oldHeight = this.parentHeight; |
| 283 | const clientRect = assertExists(dom.parentElement).getBoundingClientRect(); |
Isabelle Taylor | ef90242 | 2020-01-24 10:35:55 +0000 | [diff] [blame] | 284 | // On non-MacOS if there is a solid scroll bar it can cover important |
| 285 | // pixels, reduce the size of the canvas so it doesn't overlap with |
| 286 | // the scroll bar. |
| 287 | this.parentWidth = |
| 288 | clientRect.width - globals.frontendLocalState.getScrollbarWidth(); |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame] | 289 | this.parentHeight = clientRect.height; |
| 290 | return this.parentHeight !== oldHeight || this.parentWidth !== oldWidth; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Reads dimensions of panels. Returns true if total panel height is different |
| 295 | * from what was cached in state. |
| 296 | */ |
| 297 | private readPanelHeightsFromDom(dom: Element): boolean { |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 298 | const prevHeight = this.totalPanelHeight; |
Isabelle Taylor | a16dec2 | 2019-12-03 16:34:13 +0000 | [diff] [blame] | 299 | this.panelPositions = []; |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 300 | this.totalPanelHeight = 0; |
| 301 | |
Isabelle Taylor | f4e0f68 | 2019-11-20 10:47:49 +0000 | [diff] [blame] | 302 | const panels = dom.parentElement!.querySelectorAll('.panel'); |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame] | 303 | assertTrue(panels.length === this.attrs.panels.length); |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 304 | for (let i = 0; i < panels.length; i++) { |
Hector Dearman | 191741d | 2020-06-09 16:00:40 +0100 | [diff] [blame] | 305 | const rect = panels[i].getBoundingClientRect(); |
Isabelle Taylor | 9b4a81d | 2020-01-31 11:11:02 +0000 | [diff] [blame] | 306 | const id = this.attrs.panels[i].attrs.id || |
| 307 | this.attrs.panels[i].attrs.trackGroupId; |
Isabelle Taylor | a16dec2 | 2019-12-03 16:34:13 +0000 | [diff] [blame] | 308 | this.panelPositions[i] = |
Isabelle Taylor | 9b4a81d | 2020-01-31 11:11:02 +0000 | [diff] [blame] | 309 | {id, height: rect.height, width: rect.width, x: rect.x, y: rect.y}; |
Isabelle Taylor | a16dec2 | 2019-12-03 16:34:13 +0000 | [diff] [blame] | 310 | this.totalPanelHeight += rect.height; |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | return this.totalPanelHeight !== prevHeight; |
| 314 | } |
| 315 | |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 316 | private overlapsCanvas(yStart: number, yEnd: number) { |
| 317 | return yEnd > 0 && yStart < this.canvasHeight; |
| 318 | } |
| 319 | |
| 320 | private redrawCanvas() { |
Deepanjan Roy | 9a906ed | 2018-09-20 11:06:00 -0400 | [diff] [blame] | 321 | const redrawStart = debugNow(); |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 322 | if (!this.ctx) return; |
| 323 | this.ctx.clearRect(0, 0, this.parentWidth, this.canvasHeight); |
Hector Dearman | f74cf22 | 2018-10-31 16:22:29 +0000 | [diff] [blame] | 324 | const canvasYStart = |
Hector Dearman | afa51b2 | 2018-12-11 16:38:30 +0000 | [diff] [blame] | 325 | Math.floor(this.scrollTop - this.getCanvasOverdrawHeightPerSide()); |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 326 | |
Isabelle Taylor | 9b4a81d | 2020-01-31 11:11:02 +0000 | [diff] [blame] | 327 | this.handleAreaSelection(); |
Isabelle Taylor | a16dec2 | 2019-12-03 16:34:13 +0000 | [diff] [blame] | 328 | |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 329 | let panelYStart = 0; |
| 330 | const panels = assertExists(this.attrs).panels; |
Isabelle Taylor | a16dec2 | 2019-12-03 16:34:13 +0000 | [diff] [blame] | 331 | assertTrue(panels.length === this.panelPositions.length); |
Deepanjan Roy | 9a906ed | 2018-09-20 11:06:00 -0400 | [diff] [blame] | 332 | let totalOnCanvas = 0; |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 333 | for (let i = 0; i < panels.length; i++) { |
| 334 | const panel = panels[i]; |
Isabelle Taylor | a16dec2 | 2019-12-03 16:34:13 +0000 | [diff] [blame] | 335 | const panelHeight = this.panelPositions[i].height; |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 336 | const yStartOnCanvas = panelYStart - canvasYStart; |
| 337 | |
| 338 | if (!this.overlapsCanvas(yStartOnCanvas, yStartOnCanvas + panelHeight)) { |
| 339 | panelYStart += panelHeight; |
| 340 | continue; |
| 341 | } |
| 342 | |
Deepanjan Roy | 9a906ed | 2018-09-20 11:06:00 -0400 | [diff] [blame] | 343 | totalOnCanvas++; |
| 344 | |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 345 | if (!isPanelVNode(panel)) { |
Hector Dearman | 191741d | 2020-06-09 16:00:40 +0100 | [diff] [blame] | 346 | throw new Error('Vnode passed to panel container is not a panel'); |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 347 | } |
| 348 | |
Hector Dearman | 191741d | 2020-06-09 16:00:40 +0100 | [diff] [blame] | 349 | // TODO(hjd): This cast should be unnecessary given the type guard above. |
| 350 | const p = panel as PanelVNode<{}>; |
| 351 | |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 352 | this.ctx.save(); |
| 353 | this.ctx.translate(0, yStartOnCanvas); |
| 354 | const clipRect = new Path2D(); |
| 355 | const size = {width: this.parentWidth, height: panelHeight}; |
| 356 | clipRect.rect(0, 0, size.width, size.height); |
| 357 | this.ctx.clip(clipRect); |
Deepanjan Roy | 9a906ed | 2018-09-20 11:06:00 -0400 | [diff] [blame] | 358 | const beforeRender = debugNow(); |
Hector Dearman | 191741d | 2020-06-09 16:00:40 +0100 | [diff] [blame] | 359 | p.state.renderCanvas(this.ctx, size, p); |
Deepanjan Roy | 9a906ed | 2018-09-20 11:06:00 -0400 | [diff] [blame] | 360 | this.updatePanelStats( |
Hector Dearman | 191741d | 2020-06-09 16:00:40 +0100 | [diff] [blame] | 361 | i, p.state, debugNow() - beforeRender, this.ctx, size); |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 362 | this.ctx.restore(); |
| 363 | panelYStart += panelHeight; |
| 364 | } |
Isabelle Taylor | 01254f7 | 2020-01-27 15:29:14 +0000 | [diff] [blame] | 365 | |
| 366 | this.drawTopLayerOnCanvas(); |
Deepanjan Roy | 9a906ed | 2018-09-20 11:06:00 -0400 | [diff] [blame] | 367 | const redrawDur = debugNow() - redrawStart; |
| 368 | this.updatePerfStats(redrawDur, panels.length, totalOnCanvas); |
| 369 | } |
| 370 | |
Isabelle Taylor | 01254f7 | 2020-01-27 15:29:14 +0000 | [diff] [blame] | 371 | // The panels each draw on the canvas but some details need to be drawn across |
| 372 | // the whole canvas rather than per panel. |
| 373 | private drawTopLayerOnCanvas() { |
| 374 | if (!this.ctx) return; |
Isabelle Taylor | 01254f7 | 2020-01-27 15:29:14 +0000 | [diff] [blame] | 375 | const selection = globals.frontendLocalState.selectedArea; |
| 376 | const area = selection.area; |
| 377 | if (area === undefined || |
| 378 | globals.frontendLocalState.areaY.start === undefined || |
| 379 | globals.frontendLocalState.areaY.end === undefined || |
| 380 | !globals.frontendLocalState.selectingArea) { |
| 381 | return; |
| 382 | } |
Isabelle Taylor | 9b4a81d | 2020-01-31 11:11:02 +0000 | [diff] [blame] | 383 | if (this.panelPositions.length === 0 || area.tracks.length === 0) return; |
| 384 | |
| 385 | // Find the minY and maxY of the selected tracks in this panel container. |
| 386 | const panelContainerTop = this.panelPositions[0].y; |
| 387 | const panelContainerBottom = |
| 388 | this.panelPositions[this.panelPositions.length - 1].y + |
| 389 | this.panelPositions[this.panelPositions.length - 1].height; |
| 390 | let selectedTracksMinY = panelContainerBottom; |
| 391 | let selectedTracksMaxY = panelContainerTop; |
| 392 | let trackFromCurrentContainerSelected = false; |
| 393 | for (let i = 0; i < this.panelPositions.length; i++) { |
| 394 | if (area.tracks.includes(this.panelPositions[i].id)) { |
| 395 | trackFromCurrentContainerSelected = true; |
| 396 | selectedTracksMinY = |
| 397 | Math.min(selectedTracksMinY, this.panelPositions[i].y); |
| 398 | selectedTracksMaxY = Math.max( |
| 399 | selectedTracksMaxY, |
| 400 | this.panelPositions[i].y + this.panelPositions[i].height); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | // No box should be drawn if there are no selected tracks in the current |
| 405 | // container. |
| 406 | if (!trackFromCurrentContainerSelected) { |
| 407 | return; |
| 408 | } |
| 409 | |
| 410 | const startX = globals.frontendLocalState.timeScale.timeToPx(area.startSec); |
| 411 | const endX = globals.frontendLocalState.timeScale.timeToPx(area.endSec); |
| 412 | // To align with where to draw on the canvas subtract the first panel Y. |
| 413 | selectedTracksMinY -= panelContainerTop; |
| 414 | selectedTracksMaxY -= panelContainerTop; |
Isabelle Taylor | 01254f7 | 2020-01-27 15:29:14 +0000 | [diff] [blame] | 415 | this.ctx.save(); |
| 416 | this.ctx.strokeStyle = 'rgba(52,69,150)'; |
| 417 | this.ctx.lineWidth = 1; |
| 418 | const canvasYStart = |
| 419 | Math.floor(this.scrollTop - this.getCanvasOverdrawHeightPerSide()); |
| 420 | this.ctx.translate(TRACK_SHELL_WIDTH, -canvasYStart); |
Isabelle Taylor | 01254f7 | 2020-01-27 15:29:14 +0000 | [diff] [blame] | 421 | this.ctx.strokeRect( |
Isabelle Taylor | 9b4a81d | 2020-01-31 11:11:02 +0000 | [diff] [blame] | 422 | startX, |
| 423 | selectedTracksMaxY, |
| 424 | endX - startX, |
| 425 | selectedTracksMinY - selectedTracksMaxY); |
Isabelle Taylor | 01254f7 | 2020-01-27 15:29:14 +0000 | [diff] [blame] | 426 | this.ctx.restore(); |
| 427 | } |
| 428 | |
Deepanjan Roy | 9a906ed | 2018-09-20 11:06:00 -0400 | [diff] [blame] | 429 | private updatePanelStats( |
| 430 | panelIndex: number, panel: Panel, renderTime: number, |
| 431 | ctx: CanvasRenderingContext2D, size: PanelSize) { |
| 432 | if (!perfDebug()) return; |
| 433 | let renderStats = this.panelPerfStats.get(panel); |
| 434 | if (renderStats === undefined) { |
| 435 | renderStats = new RunningStatistics(); |
| 436 | this.panelPerfStats.set(panel, renderStats); |
| 437 | } |
| 438 | renderStats.addValue(renderTime); |
| 439 | |
| 440 | const statW = 300; |
| 441 | ctx.fillStyle = 'hsl(97, 100%, 96%)'; |
| 442 | ctx.fillRect(size.width - statW, size.height - 20, statW, 20); |
| 443 | ctx.fillStyle = 'hsla(122, 77%, 22%)'; |
| 444 | const statStr = `Panel ${panelIndex + 1} | ` + runningStatStr(renderStats); |
| 445 | ctx.fillText(statStr, size.width - statW, size.height - 10); |
| 446 | } |
| 447 | |
| 448 | private updatePerfStats( |
| 449 | renderTime: number, totalPanels: number, panelsOnCanvas: number) { |
| 450 | if (!perfDebug()) return; |
| 451 | this.perfStats.renderStats.addValue(renderTime); |
| 452 | this.perfStats.totalPanels = totalPanels; |
| 453 | this.perfStats.panelsOnCanvas = panelsOnCanvas; |
| 454 | } |
| 455 | |
| 456 | renderPerfStats(index: number) { |
| 457 | assertTrue(perfDebug()); |
| 458 | return [m( |
| 459 | 'section', |
| 460 | m('div', `Panel Container ${index + 1}`), |
| 461 | m('div', |
| 462 | `${this.perfStats.totalPanels} panels, ` + |
| 463 | `${this.perfStats.panelsOnCanvas} on canvas.`), |
| 464 | m('div', runningStatStr(this.perfStats.renderStats)), )]; |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | private getCanvasOverdrawHeightPerSide() { |
| 468 | const overdrawHeight = (this.canvasOverdrawFactor - 1) * this.parentHeight; |
| 469 | return overdrawHeight / 2; |
| 470 | } |
| 471 | } |