1*6dbdd20aSAndroid Build Coastguard Worker// Copyright (C) 2019 The Android Open Source Project 2*6dbdd20aSAndroid Build Coastguard Worker// 3*6dbdd20aSAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License"); 4*6dbdd20aSAndroid Build Coastguard Worker// you may not use this file except in compliance with the License. 5*6dbdd20aSAndroid Build Coastguard Worker// You may obtain a copy of the License at 6*6dbdd20aSAndroid Build Coastguard Worker// 7*6dbdd20aSAndroid Build Coastguard Worker// http://www.apache.org/licenses/LICENSE-2.0 8*6dbdd20aSAndroid Build Coastguard Worker// 9*6dbdd20aSAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software 10*6dbdd20aSAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS, 11*6dbdd20aSAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*6dbdd20aSAndroid Build Coastguard Worker// See the License for the specific language governing permissions and 13*6dbdd20aSAndroid Build Coastguard Worker// limitations under the License. 14*6dbdd20aSAndroid Build Coastguard Worker 15*6dbdd20aSAndroid Build Coastguard Worker// This code can be used in unittests where we can't read CSS variables. 16*6dbdd20aSAndroid Build Coastguard Worker// Also we cannot have global constructors beacause when the javascript is 17*6dbdd20aSAndroid Build Coastguard Worker// loaded, the CSS might not be ready yet. 18*6dbdd20aSAndroid Build Coastguard Workerexport let TRACK_SHELL_WIDTH = 100; 19*6dbdd20aSAndroid Build Coastguard Workerexport let SIDEBAR_WIDTH = 100; 20*6dbdd20aSAndroid Build Coastguard Workerexport let TRACK_BORDER_COLOR = '#ffc0cb'; 21*6dbdd20aSAndroid Build Coastguard Workerexport let TOPBAR_HEIGHT = 48; 22*6dbdd20aSAndroid Build Coastguard Workerexport let SELECTION_STROKE_COLOR = '#00344596'; 23*6dbdd20aSAndroid Build Coastguard Workerexport let SELECTION_FILL_COLOR = '#8398e64d'; 24*6dbdd20aSAndroid Build Coastguard Workerexport let OVERVIEW_TIMELINE_NON_VISIBLE_COLOR = '#c8c8c8cc'; 25*6dbdd20aSAndroid Build Coastguard Workerexport let DEFAULT_DETAILS_CONTENT_HEIGHT = 280; 26*6dbdd20aSAndroid Build Coastguard Workerexport let BACKGROUND_COLOR = '#ffffff'; 27*6dbdd20aSAndroid Build Coastguard Workerexport let FOREGROUND_COLOR = '#222'; 28*6dbdd20aSAndroid Build Coastguard Workerexport let COLLAPSED_BACKGROUND = '#ffffff'; 29*6dbdd20aSAndroid Build Coastguard Workerexport let EXPANDED_BACKGROUND = '#ffffff'; 30*6dbdd20aSAndroid Build Coastguard Worker 31*6dbdd20aSAndroid Build Coastguard Workerexport function initCssConstants() { 32*6dbdd20aSAndroid Build Coastguard Worker TRACK_SHELL_WIDTH = getCssNum('--track-shell-width') ?? TRACK_SHELL_WIDTH; 33*6dbdd20aSAndroid Build Coastguard Worker SIDEBAR_WIDTH = getCssNum('--sidebar-width') ?? SIDEBAR_WIDTH; 34*6dbdd20aSAndroid Build Coastguard Worker TRACK_BORDER_COLOR = getCssStr('--track-border-color') ?? TRACK_BORDER_COLOR; 35*6dbdd20aSAndroid Build Coastguard Worker TOPBAR_HEIGHT = getCssNum('--topbar-height') ?? TOPBAR_HEIGHT; 36*6dbdd20aSAndroid Build Coastguard Worker SELECTION_STROKE_COLOR = 37*6dbdd20aSAndroid Build Coastguard Worker getCssStr('--selection-stroke-color') ?? SELECTION_STROKE_COLOR; 38*6dbdd20aSAndroid Build Coastguard Worker SELECTION_FILL_COLOR = 39*6dbdd20aSAndroid Build Coastguard Worker getCssStr('--selection-fill-color') ?? SELECTION_FILL_COLOR; 40*6dbdd20aSAndroid Build Coastguard Worker OVERVIEW_TIMELINE_NON_VISIBLE_COLOR = 41*6dbdd20aSAndroid Build Coastguard Worker getCssStr('--overview-timeline-non-visible-color') ?? 42*6dbdd20aSAndroid Build Coastguard Worker OVERVIEW_TIMELINE_NON_VISIBLE_COLOR; 43*6dbdd20aSAndroid Build Coastguard Worker DEFAULT_DETAILS_CONTENT_HEIGHT = 44*6dbdd20aSAndroid Build Coastguard Worker getCssNum('--details-content-height') ?? DEFAULT_DETAILS_CONTENT_HEIGHT; 45*6dbdd20aSAndroid Build Coastguard Worker BACKGROUND_COLOR = getCssStr('--main-background-color') ?? BACKGROUND_COLOR; 46*6dbdd20aSAndroid Build Coastguard Worker FOREGROUND_COLOR = getCssStr('--main-foreground-color') ?? FOREGROUND_COLOR; 47*6dbdd20aSAndroid Build Coastguard Worker COLLAPSED_BACKGROUND = 48*6dbdd20aSAndroid Build Coastguard Worker getCssStr('--collapsed-background') ?? COLLAPSED_BACKGROUND; 49*6dbdd20aSAndroid Build Coastguard Worker EXPANDED_BACKGROUND = 50*6dbdd20aSAndroid Build Coastguard Worker getCssStr('--expanded-background') ?? EXPANDED_BACKGROUND; 51*6dbdd20aSAndroid Build Coastguard Worker} 52*6dbdd20aSAndroid Build Coastguard Worker 53*6dbdd20aSAndroid Build Coastguard Workerfunction getCssStr(prop: string): string | undefined { 54*6dbdd20aSAndroid Build Coastguard Worker if (typeof window === 'undefined') return undefined; 55*6dbdd20aSAndroid Build Coastguard Worker const body = window.document.body; 56*6dbdd20aSAndroid Build Coastguard Worker const value = window.getComputedStyle(body).getPropertyValue(prop); 57*6dbdd20aSAndroid Build Coastguard Worker // Note: getPropertyValue() returns an empty string if not set 58*6dbdd20aSAndroid Build Coastguard Worker // https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration/getPropertyValue#return_value 59*6dbdd20aSAndroid Build Coastguard Worker return value === '' ? undefined : value; 60*6dbdd20aSAndroid Build Coastguard Worker} 61*6dbdd20aSAndroid Build Coastguard Worker 62*6dbdd20aSAndroid Build Coastguard Workerfunction getCssNum(prop: string): number | undefined { 63*6dbdd20aSAndroid Build Coastguard Worker const str = getCssStr(prop); 64*6dbdd20aSAndroid Build Coastguard Worker if (str === undefined) return undefined; 65*6dbdd20aSAndroid Build Coastguard Worker const match = str.match(/^\W*(\d+)px(|\!important')$/); 66*6dbdd20aSAndroid Build Coastguard Worker if (!match) throw Error(`Could not parse CSS property "${str}" as a number`); 67*6dbdd20aSAndroid Build Coastguard Worker return Number(match[1]); 68*6dbdd20aSAndroid Build Coastguard Worker} 69