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// TODO(hjd): Dedupe these.
16const SLICE_HEIGHT = 32;
17const TRACK_PADDING = 5;
18
19/**
20 * Checker board the range [leftPx, rightPx].
21 */
22export function checkerboard(
23    ctx: CanvasRenderingContext2D, leftPx: number, rightPx: number): void {
24  const widthPx = rightPx - leftPx;
25  ctx.font = '12px Google Sans';
26  ctx.fillStyle = '#eee';
27  ctx.fillRect(leftPx, TRACK_PADDING, widthPx, SLICE_HEIGHT);
28  ctx.fillStyle = '#666';
29  ctx.textBaseline = 'alphabetic';
30  ctx.fillText(
31      'loading...',
32      leftPx + widthPx / 2,
33      TRACK_PADDING + SLICE_HEIGHT / 2,
34      widthPx);
35}
36
37/**
38 * Checker board everything between [startPx, endPx] except [leftPx, rightPx].
39 */
40export function checkerboardExcept(
41    ctx: CanvasRenderingContext2D,
42    startPx: number,
43    endPx: number,
44    leftPx: number,
45    rightPx: number): void {
46  // [leftPx, rightPx] doesn't overlap [startPx, endPx] at all:
47  if (rightPx <= startPx || leftPx >= endPx) {
48    checkerboard(ctx, startPx, endPx);
49    return;
50  }
51
52  // Checkerboard [startPx, leftPx]:
53  if (leftPx > startPx) {
54    checkerboard(ctx, startPx, leftPx);
55  }
56
57  // Checkerboard [rightPx, endPx]:
58  if (rightPx < endPx) {
59    checkerboard(ctx, rightPx, endPx);
60  }
61}
62