blob: 9329053d77907e56e100b4cfe53e7aa5a8fbd7c8 [file] [log] [blame]
Deepanjan Royabd79aa2018-08-28 07:29:15 -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
Deepanjan Roy1f658fe2018-09-11 08:38:17 -040017import {assertExists, assertTrue} from '../base/logging';
Deepanjan Royabd79aa2018-08-28 07:29:15 -040018
19import {globals} from './globals';
Deepanjan Roy9a906ed2018-09-20 11:06:00 -040020import {isPanelVNode, Panel, PanelSize} from './panel';
21import {
22 debugNow,
23 perfDebug,
24 perfDisplay,
25 RunningStatistics,
26 runningStatStr
27} from './perf';
Deepanjan Royabd79aa2018-08-28 07:29:15 -040028
29/**
30 * If the panel container scrolls, the backing canvas height is
31 * SCROLLING_CANVAS_OVERDRAW_FACTOR * parent container height.
32 */
33const SCROLLING_CANVAS_OVERDRAW_FACTOR = 2;
34
Deepanjan Roy4fa0ecc2018-10-03 16:10:46 -040035// We need any here so we can accept vnodes with arbitrary attrs.
36// tslint:disable-next-line:no-any
37export type AnyAttrsVnode = m.Vnode<any, {}>;
38
Deepanjan Roy1f658fe2018-09-11 08:38:17 -040039interface Attrs {
Deepanjan Roy4fa0ecc2018-10-03 16:10:46 -040040 panels: AnyAttrsVnode[];
Deepanjan Royabd79aa2018-08-28 07:29:15 -040041 doesScroll: boolean;
42}
43
Deepanjan Roy1f658fe2018-09-11 08:38:17 -040044export class PanelContainer implements m.ClassComponent<Attrs> {
45 // These values are updated with proper values in oncreate.
46 private parentWidth = 0;
47 private parentHeight = 0;
48 private scrollTop = 0;
49 private panelHeights: number[] = [];
50 private totalPanelHeight = 0;
51 private canvasHeight = 0;
Deepanjan Royabd79aa2018-08-28 07:29:15 -040052
Deepanjan Roy9a906ed2018-09-20 11:06:00 -040053 private panelPerfStats = new WeakMap<Panel, RunningStatistics>();
54 private perfStats = {
55 totalPanels: 0,
56 panelsOnCanvas: 0,
57 renderStats: new RunningStatistics(10),
58 };
59
Deepanjan Roy1a225c22018-10-04 07:33:02 -040060 // Attrs received in the most recent mithril redraw. We receive a new vnode
61 // with new attrs on every redraw, and we cache it here so that resize
62 // listeners and canvas redraw callbacks can access it.
63 private attrs: Attrs;
Deepanjan Roy1f658fe2018-09-11 08:38:17 -040064
Deepanjan Roy1f658fe2018-09-11 08:38:17 -040065 private ctx?: CanvasRenderingContext2D;
66
67 private onResize: () => void = () => {};
68 private parentOnScroll: () => void = () => {};
69 private canvasRedrawer: () => void;
70
Deepanjan Roy1a225c22018-10-04 07:33:02 -040071 get canvasOverdrawFactor() {
72 return this.attrs.doesScroll ? SCROLLING_CANVAS_OVERDRAW_FACTOR : 1;
73 }
74
Deepanjan Roy1f658fe2018-09-11 08:38:17 -040075 constructor(vnode: m.CVnode<Attrs>) {
Deepanjan Roy1a225c22018-10-04 07:33:02 -040076 this.attrs = vnode.attrs;
Deepanjan Roy1f658fe2018-09-11 08:38:17 -040077 this.canvasRedrawer = () => this.redrawCanvas();
Deepanjan Royabd79aa2018-08-28 07:29:15 -040078 globals.rafScheduler.addRedrawCallback(this.canvasRedrawer);
Deepanjan Roy9a906ed2018-09-20 11:06:00 -040079 perfDisplay.addContainer(this);
Deepanjan Roy1f658fe2018-09-11 08:38:17 -040080 }
Deepanjan Royabd79aa2018-08-28 07:29:15 -040081
Deepanjan Roy1f658fe2018-09-11 08:38:17 -040082 oncreate(vnodeDom: m.CVnodeDOM<Attrs>) {
Deepanjan Royabd79aa2018-08-28 07:29:15 -040083 // Save the canvas context in the state.
84 const canvas =
85 vnodeDom.dom.querySelector('.main-canvas') as HTMLCanvasElement;
86 const ctx = canvas.getContext('2d');
87 if (!ctx) {
88 throw Error('Cannot create canvas context');
89 }
90 this.ctx = ctx;
91
Deepanjan Roy1f658fe2018-09-11 08:38:17 -040092 const clientRect =
93 assertExists(vnodeDom.dom.parentElement).getBoundingClientRect();
94 this.parentWidth = clientRect.width;
95 this.parentHeight = clientRect.height;
96
Deepanjan Roy1a225c22018-10-04 07:33:02 -040097 this.readPanelHeightsFromDom(vnodeDom.dom);
Deepanjan Roy1f658fe2018-09-11 08:38:17 -040098 (vnodeDom.dom as HTMLElement).style.height = `${this.totalPanelHeight}px`;
99
Deepanjan Roy1a225c22018-10-04 07:33:02 -0400100 this.updateCanvasDimensions();
101 this.repositionCanvas();
Deepanjan Royabd79aa2018-08-28 07:29:15 -0400102
103 // Save the resize handler in the state so we can remove it later.
104 // TODO: Encapsulate resize handling better.
105 this.onResize = () => {
Deepanjan Roy1a225c22018-10-04 07:33:02 -0400106 this.readParentSizeFromDom(vnodeDom.dom);
107 this.updateCanvasDimensions();
108 this.repositionCanvas();
Deepanjan Roy112ff6a2018-09-10 08:31:43 -0400109 globals.rafScheduler.scheduleFullRedraw();
Deepanjan Royabd79aa2018-08-28 07:29:15 -0400110 };
111
112 // Once ResizeObservers are out, we can stop accessing the window here.
113 window.addEventListener('resize', this.onResize);
114
Deepanjan Roy1f658fe2018-09-11 08:38:17 -0400115 // TODO(dproy): Handle change in doesScroll attribute.
Deepanjan Roy1a225c22018-10-04 07:33:02 -0400116 if (this.attrs.doesScroll) {
Deepanjan Royabd79aa2018-08-28 07:29:15 -0400117 this.parentOnScroll = () => {
Deepanjan Roy1a225c22018-10-04 07:33:02 -0400118 this.scrollTop = assertExists(vnodeDom.dom.parentElement).scrollTop;
119 this.repositionCanvas();
Deepanjan Royf190cb22018-08-28 10:43:07 -0400120 globals.rafScheduler.scheduleRedraw();
Deepanjan Royabd79aa2018-08-28 07:29:15 -0400121 };
122 vnodeDom.dom.parentElement!.addEventListener(
123 'scroll', this.parentOnScroll, {passive: true});
124 }
Deepanjan Roy1f658fe2018-09-11 08:38:17 -0400125 }
Deepanjan Royabd79aa2018-08-28 07:29:15 -0400126
Deepanjan Roy1f658fe2018-09-11 08:38:17 -0400127 onremove({attrs, dom}: m.CVnodeDOM<Attrs>) {
Deepanjan Royabd79aa2018-08-28 07:29:15 -0400128 window.removeEventListener('resize', this.onResize);
129 globals.rafScheduler.removeRedrawCallback(this.canvasRedrawer);
130 if (attrs.doesScroll) {
131 dom.parentElement!.removeEventListener('scroll', this.parentOnScroll);
132 }
Deepanjan Roy9a906ed2018-09-20 11:06:00 -0400133 perfDisplay.removeContainer(this);
Deepanjan Roy1f658fe2018-09-11 08:38:17 -0400134 }
Deepanjan Royabd79aa2018-08-28 07:29:15 -0400135
Deepanjan Roy1f658fe2018-09-11 08:38:17 -0400136 view({attrs}: m.CVnode<Attrs>) {
Deepanjan Roy1f658fe2018-09-11 08:38:17 -0400137 this.attrs = attrs;
Deepanjan Roy9a906ed2018-09-20 11:06:00 -0400138 const renderPanel = (panel: m.Vnode) => perfDebug() ?
139 m('.panel', panel, m('.debug-panel-border')) :
140 m('.panel', panel);
141
Deepanjan Royabd79aa2018-08-28 07:29:15 -0400142 return m(
143 '.scroll-limiter',
Deepanjan Roy1f658fe2018-09-11 08:38:17 -0400144 m('canvas.main-canvas'),
Deepanjan Roy9a906ed2018-09-20 11:06:00 -0400145 attrs.panels.map(renderPanel));
Deepanjan Royabd79aa2018-08-28 07:29:15 -0400146 }
Deepanjan Roy1f658fe2018-09-11 08:38:17 -0400147
148 onupdate(vnodeDom: m.CVnodeDOM<Attrs>) {
Deepanjan Roy1a225c22018-10-04 07:33:02 -0400149 const totalPanelHeightChanged = this.readPanelHeightsFromDom(vnodeDom.dom);
150 const parentSizeChanged = this.readParentSizeFromDom(vnodeDom.dom);
Deepanjan Roy1f658fe2018-09-11 08:38:17 -0400151
Deepanjan Roy1a225c22018-10-04 07:33:02 -0400152 if (totalPanelHeightChanged) {
Deepanjan Roy1f658fe2018-09-11 08:38:17 -0400153 (vnodeDom.dom as HTMLElement).style.height = `${this.totalPanelHeight}px`;
154 }
155
Deepanjan Roy1a225c22018-10-04 07:33:02 -0400156 const canvasSizeShouldChange =
157 this.attrs.doesScroll ? parentSizeChanged : totalPanelHeightChanged;
158 if (canvasSizeShouldChange) {
159 this.updateCanvasDimensions();
160 this.repositionCanvas();
Deepanjan Roy1f658fe2018-09-11 08:38:17 -0400161 }
162 }
163
Deepanjan Roy1a225c22018-10-04 07:33:02 -0400164 private updateCanvasDimensions() {
165 this.canvasHeight = this.attrs.doesScroll ?
166 this.parentHeight * this.canvasOverdrawFactor :
167 this.totalPanelHeight;
Deepanjan Roy1f658fe2018-09-11 08:38:17 -0400168 const ctx = assertExists(this.ctx);
Deepanjan Roy1a225c22018-10-04 07:33:02 -0400169 const canvas = assertExists(ctx.canvas);
Deepanjan Roy1f658fe2018-09-11 08:38:17 -0400170 canvas.style.height = `${this.canvasHeight}px`;
171 const dpr = window.devicePixelRatio;
172 ctx.canvas.width = this.parentWidth * dpr;
173 ctx.canvas.height = this.canvasHeight * dpr;
174 ctx.scale(dpr, dpr);
175 }
176
Deepanjan Roy1a225c22018-10-04 07:33:02 -0400177 private repositionCanvas() {
178 const canvas = assertExists(assertExists(this.ctx).canvas);
179 const canvasYStart = this.scrollTop - this.getCanvasOverdrawHeightPerSide();
180 canvas.style.transform = `translateY(${canvasYStart}px)`;
181 }
182
183 /**
184 * Reads dimensions of parent node. Returns true if read dimensions are
185 * different from what was cached in the state.
186 */
187 private readParentSizeFromDom(dom: Element): boolean {
188 const oldWidth = this.parentWidth;
189 const oldHeight = this.parentHeight;
190 const clientRect = assertExists(dom.parentElement).getBoundingClientRect();
191 this.parentWidth = clientRect.width;
192 this.parentHeight = clientRect.height;
193 return this.parentHeight !== oldHeight || this.parentWidth !== oldWidth;
194 }
195
196 /**
197 * Reads dimensions of panels. Returns true if total panel height is different
198 * from what was cached in state.
199 */
200 private readPanelHeightsFromDom(dom: Element): boolean {
Deepanjan Roy1f658fe2018-09-11 08:38:17 -0400201 const prevHeight = this.totalPanelHeight;
202 this.panelHeights = [];
203 this.totalPanelHeight = 0;
204
Deepanjan Roy1a225c22018-10-04 07:33:02 -0400205 const panels = dom.querySelectorAll('.panel');
206 assertTrue(panels.length === this.attrs.panels.length);
Deepanjan Roy1f658fe2018-09-11 08:38:17 -0400207 for (let i = 0; i < panels.length; i++) {
208 const height = panels[i].getBoundingClientRect().height;
209 this.panelHeights[i] = height;
210 this.totalPanelHeight += height;
211 }
212
213 return this.totalPanelHeight !== prevHeight;
214 }
215
Deepanjan Roy1f658fe2018-09-11 08:38:17 -0400216 private overlapsCanvas(yStart: number, yEnd: number) {
217 return yEnd > 0 && yStart < this.canvasHeight;
218 }
219
220 private redrawCanvas() {
Deepanjan Roy9a906ed2018-09-20 11:06:00 -0400221 const redrawStart = debugNow();
Deepanjan Roy1f658fe2018-09-11 08:38:17 -0400222 if (!this.ctx) return;
223 this.ctx.clearRect(0, 0, this.parentWidth, this.canvasHeight);
224 const canvasYStart = this.scrollTop - this.getCanvasOverdrawHeightPerSide();
225
226 let panelYStart = 0;
227 const panels = assertExists(this.attrs).panels;
228 assertTrue(panels.length === this.panelHeights.length);
Deepanjan Roy9a906ed2018-09-20 11:06:00 -0400229 let totalOnCanvas = 0;
Deepanjan Roy1f658fe2018-09-11 08:38:17 -0400230 for (let i = 0; i < panels.length; i++) {
231 const panel = panels[i];
232 const panelHeight = this.panelHeights[i];
233 const yStartOnCanvas = panelYStart - canvasYStart;
234
235 if (!this.overlapsCanvas(yStartOnCanvas, yStartOnCanvas + panelHeight)) {
236 panelYStart += panelHeight;
237 continue;
238 }
239
Deepanjan Roy9a906ed2018-09-20 11:06:00 -0400240 totalOnCanvas++;
241
Deepanjan Roy1f658fe2018-09-11 08:38:17 -0400242 if (!isPanelVNode(panel)) {
243 throw Error('Vnode passed to panel container is not a panel');
244 }
245
246 this.ctx.save();
247 this.ctx.translate(0, yStartOnCanvas);
248 const clipRect = new Path2D();
249 const size = {width: this.parentWidth, height: panelHeight};
250 clipRect.rect(0, 0, size.width, size.height);
251 this.ctx.clip(clipRect);
Deepanjan Roy9a906ed2018-09-20 11:06:00 -0400252 const beforeRender = debugNow();
Deepanjan Roy1f658fe2018-09-11 08:38:17 -0400253 panel.state.renderCanvas(this.ctx, size, panel);
Deepanjan Roy9a906ed2018-09-20 11:06:00 -0400254 this.updatePanelStats(
255 i, panel.state, debugNow() - beforeRender, this.ctx, size);
Deepanjan Roy1f658fe2018-09-11 08:38:17 -0400256 this.ctx.restore();
257 panelYStart += panelHeight;
258 }
Deepanjan Roy9a906ed2018-09-20 11:06:00 -0400259 const redrawDur = debugNow() - redrawStart;
260 this.updatePerfStats(redrawDur, panels.length, totalOnCanvas);
261 }
262
263 private updatePanelStats(
264 panelIndex: number, panel: Panel, renderTime: number,
265 ctx: CanvasRenderingContext2D, size: PanelSize) {
266 if (!perfDebug()) return;
267 let renderStats = this.panelPerfStats.get(panel);
268 if (renderStats === undefined) {
269 renderStats = new RunningStatistics();
270 this.panelPerfStats.set(panel, renderStats);
271 }
272 renderStats.addValue(renderTime);
273
274 const statW = 300;
275 ctx.fillStyle = 'hsl(97, 100%, 96%)';
276 ctx.fillRect(size.width - statW, size.height - 20, statW, 20);
277 ctx.fillStyle = 'hsla(122, 77%, 22%)';
278 const statStr = `Panel ${panelIndex + 1} | ` + runningStatStr(renderStats);
279 ctx.fillText(statStr, size.width - statW, size.height - 10);
280 }
281
282 private updatePerfStats(
283 renderTime: number, totalPanels: number, panelsOnCanvas: number) {
284 if (!perfDebug()) return;
285 this.perfStats.renderStats.addValue(renderTime);
286 this.perfStats.totalPanels = totalPanels;
287 this.perfStats.panelsOnCanvas = panelsOnCanvas;
288 }
289
290 renderPerfStats(index: number) {
291 assertTrue(perfDebug());
292 return [m(
293 'section',
294 m('div', `Panel Container ${index + 1}`),
295 m('div',
296 `${this.perfStats.totalPanels} panels, ` +
297 `${this.perfStats.panelsOnCanvas} on canvas.`),
298 m('div', runningStatStr(this.perfStats.renderStats)), )];
Deepanjan Roy1f658fe2018-09-11 08:38:17 -0400299 }
300
301 private getCanvasOverdrawHeightPerSide() {
302 const overdrawHeight = (this.canvasOverdrawFactor - 1) * this.parentHeight;
303 return overdrawHeight / 2;
304 }
305}