1// Copyright (C) 2019 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// This code can be used in unittests where we can't read CSS variables.
16// Also we cannot have global constructors beacause when the javascript is
17// loaded, the CSS might not be ready yet.
18export let TRACK_SHELL_WIDTH = 100;
19export let SIDEBAR_WIDTH = 100;
20export let TRACK_BORDER_COLOR = '#ffc0cb';
21export let TOPBAR_HEIGHT = 48;
22
23export function initCssConstants() {
24  TRACK_SHELL_WIDTH = getCssNum('--track-shell-width') || TRACK_SHELL_WIDTH;
25  SIDEBAR_WIDTH = getCssNum('--sidebar-width') || SIDEBAR_WIDTH;
26  TRACK_BORDER_COLOR = getCssStr('--track-border-color') || TRACK_BORDER_COLOR;
27  TOPBAR_HEIGHT = getCssNum('--topbar-height') || TOPBAR_HEIGHT;
28}
29
30function getCssStr(prop: string): string|undefined {
31  if (typeof window === 'undefined') return undefined;
32  const body = window.document.body;
33  return window.getComputedStyle(body).getPropertyValue(prop);
34}
35
36function getCssNum(prop: string): number|undefined {
37  const str = getCssStr(prop);
38  if (str === undefined) return undefined;
39  const match = str.match(/^\W*(\d+)px$/);
40  if (!match) throw Error(`Could not parse CSS property "${str}" as a number`);
41  return Number(match[1]);
42}
43