UNPKG

2.69 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const Styles_1 = require("./Styles");
4const Colors_1 = require("./Colors");
5const COLOR_PRIMARY_DARK_KEY = 'colorPrimaryDark';
6const WINDOW_TRANSLUCENT_STATUS = 'android:windowTranslucentStatus';
7const WINDOW_LIGHT_STATUS_BAR = 'android:windowLightStatusBar';
8function getStatusBarColor(config) {
9 var _a;
10 return ((_a = config.androidStatusBar) === null || _a === void 0 ? void 0 : _a.backgroundColor) || 'translucent';
11}
12exports.getStatusBarColor = getStatusBarColor;
13function getStatusBarStyle(config) {
14 var _a;
15 return ((_a = config.androidStatusBar) === null || _a === void 0 ? void 0 : _a.barStyle) || 'light-content';
16}
17exports.getStatusBarStyle = getStatusBarStyle;
18async function setStatusBarConfig(config, projectDirectory) {
19 let hexString = getStatusBarColor(config);
20 let statusBarStyle = getStatusBarStyle(config);
21 const stylesPath = await Styles_1.getProjectStylesXMLPathAsync(projectDirectory);
22 const colorsPath = await Colors_1.getProjectColorsXMLPathAsync(projectDirectory);
23 if (!colorsPath || !stylesPath) {
24 return false;
25 }
26 let stylesJSON = await Styles_1.readStylesXMLAsync(stylesPath);
27 let colorsJSON = await Colors_1.readColorsXMLAsync(colorsPath);
28 let styleItemToAdd = [{ _: '', $: { name: '' } }];
29 if (hexString === 'translucent') {
30 // translucent status bar set in theme
31 styleItemToAdd[0]._ = 'true';
32 styleItemToAdd[0].$.name = WINDOW_TRANSLUCENT_STATUS;
33 }
34 else {
35 // Need to add a color key to colors.xml to use in styles.xml
36 let colorItemToAdd = [{ _: hexString, $: { name: COLOR_PRIMARY_DARK_KEY } }];
37 colorsJSON = Colors_1.setColorItem(colorItemToAdd, colorsJSON);
38 styleItemToAdd[0]._ = `@color/${COLOR_PRIMARY_DARK_KEY}`;
39 styleItemToAdd[0].$.name = COLOR_PRIMARY_DARK_KEY;
40 }
41 // Default is light-content, don't need to do anything to set it
42 if (statusBarStyle === 'dark-content') {
43 let statusBarStyleItem = [{ _: 'true', $: { name: WINDOW_LIGHT_STATUS_BAR } }];
44 stylesJSON = Styles_1.setStylesItem(statusBarStyleItem, stylesJSON);
45 }
46 stylesJSON = Styles_1.setStylesItem(styleItemToAdd, stylesJSON);
47 try {
48 await Colors_1.writeColorsXMLAsync(colorsPath, colorsJSON);
49 await Styles_1.writeStylesXMLAsync(stylesPath, stylesJSON);
50 }
51 catch (e) {
52 throw new Error(`Error setting Android status bar config. Cannot write colors.xml to ${colorsPath}, or styles.xml to ${stylesPath}.`);
53 }
54 return true;
55}
56exports.setStatusBarConfig = setStatusBarConfig;
57//# sourceMappingURL=StatusBar.js.map
\No newline at end of file