MOBILE-3670 statusbar: Change status bar color on loading site
parent
2cd17b1f29
commit
c8899dde03
|
@ -43,7 +43,7 @@ export class AddonRemoteThemesProvider {
|
|||
protected stylesEls: {[siteId: string]: { element: HTMLStyleElement; hash: string }} = {};
|
||||
|
||||
constructor() {
|
||||
this.logger = CoreLogger.getInstance('AddonRemoteThemesProvider');
|
||||
this.logger = CoreLogger.getInstance('AddonRemoteThemes');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -363,6 +363,9 @@ export class AddonRemoteThemesProvider {
|
|||
// Adding styles to a style element automatically enables it. Disable it again.
|
||||
if (disabled) {
|
||||
this.disableElement(this.stylesEls[siteId].element, true);
|
||||
} else {
|
||||
// Set StatusBar properties.
|
||||
CoreApp.setStatusBarColor();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -622,8 +622,16 @@ export class CoreAppProvider {
|
|||
*/
|
||||
setStatusBarColor(color?: string): void {
|
||||
if (!color) {
|
||||
// Get the default color to reset it.
|
||||
color = getComputedStyle(document.documentElement).getPropertyValue('--core-header-toolbar-background').trim();
|
||||
// Get the default color to change it.
|
||||
const element = document.querySelector('ion-header ion-toolbar');
|
||||
if (element) {
|
||||
color = getComputedStyle(element).getPropertyValue('--background').trim();
|
||||
} else {
|
||||
// Fallback, it won't always work.
|
||||
color = getComputedStyle(document.body).getPropertyValue('--core-header-toolbar-background').trim();
|
||||
}
|
||||
|
||||
color = CoreColors.getColorHex(color);
|
||||
}
|
||||
|
||||
// Make darker on Android.
|
||||
|
@ -631,6 +639,8 @@ export class CoreAppProvider {
|
|||
color = CoreColors.darker(color);
|
||||
}
|
||||
|
||||
this.logger.debug(`Set status bar color ${color}`);
|
||||
|
||||
const useLightText = CoreColors.isWhiteContrastingBetter(color);
|
||||
const statusBar = StatusBar.instance;
|
||||
statusBar.backgroundColorByHexString(color);
|
||||
|
|
|
@ -52,6 +52,29 @@ export class CoreColors {
|
|||
return CoreColors.RGBToHex(components);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the hex code from any color css type (ie named).
|
||||
*
|
||||
* @param color Color in any format.
|
||||
* @returns Color in hex format.
|
||||
*/
|
||||
static getColorHex(color: string): string {
|
||||
const d = document.createElement('div');
|
||||
d.style.color = color;
|
||||
document.body.appendChild(d);
|
||||
|
||||
// Color in RGB .
|
||||
const rgba = getComputedStyle(d).color.match(/\d+/g)!.map((a) => parseInt(a, 10));
|
||||
|
||||
const hex = [0,1,2].map(
|
||||
(idx) => this.componentToHex(rgba[idx]),
|
||||
).join('');
|
||||
|
||||
document.body.removeChild(d);
|
||||
|
||||
return '#'+hex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the luma of a color.
|
||||
*
|
||||
|
@ -109,9 +132,7 @@ export class CoreColors {
|
|||
* @return Hexadec of the color component.
|
||||
*/
|
||||
protected static componentToHex(c: number): string {
|
||||
const hex = c.toString(16);
|
||||
|
||||
return hex.length == 1 ? '0' + hex : hex;
|
||||
return ('0' + c.toString(16)).slice(-2);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue