hydrogen-web/src/platform/web/ThemeLoader.ts

198 lines
7.3 KiB
TypeScript
Raw Normal View History

2022-05-11 14:58:14 +05:30
/*
Copyright 2020 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2022-05-18 18:56:28 +05:30
import type {ILogItem} from "../../logging/types.js";
2022-05-11 14:58:14 +05:30
import type {Platform} from "./Platform.js";
type NormalVariant = {
id: string;
cssLocation: string;
};
type DefaultVariant = {
dark: {
id: string;
cssLocation: string;
2022-05-30 11:48:02 +05:30
variantName: string;
};
light: {
id: string;
cssLocation: string;
2022-05-30 11:48:02 +05:30
variantName: string;
};
}
2022-05-30 11:49:45 +05:30
type ThemeInformation = NormalVariant | DefaultVariant;
2022-06-02 15:05:43 +05:30
export enum ColorSchemePreference {
Dark,
Light
};
2022-05-11 14:58:14 +05:30
export class ThemeLoader {
private _platform: Platform;
private _themeMapping: Record<string, ThemeInformation>;
2022-05-11 14:58:14 +05:30
constructor(platform: Platform) {
this._platform = platform;
}
2022-05-11 15:03:32 +05:30
async init(manifestLocations: string[]): Promise<void> {
this._themeMapping = {};
2022-05-18 18:56:28 +05:30
for (const manifestLocation of manifestLocations) {
const { body } = await this._platform
.request(manifestLocation, {
method: "GET",
format: "json",
cache: true,
})
.response();
2022-05-31 20:21:18 +05:30
this._populateThemeMap(body);
2022-05-18 18:56:28 +05:30
/*
After build has finished, the source section of each theme manifest
contains `built-assets` which is a mapping from the theme-name to theme
2022-05-30 11:49:45 +05:30
information which includes the location of the CSS file.
(see type ThemeInformation above)
2022-05-18 18:56:28 +05:30
*/
2022-05-31 20:21:18 +05:30
// Object.assign(this._themeMapping, body["source"]["built-assets"]);
2022-05-27 14:24:44 +05:30
//Add the default-theme as an additional option to the mapping
const defaultThemeId = this.getDefaultTheme();
if (defaultThemeId) {
const cssLocation = this._findThemeLocationFromId(defaultThemeId);
if (cssLocation) {
this._themeMapping["Default"] = { id: "default", cssLocation };
}
}
2022-05-18 18:56:28 +05:30
}
2022-05-11 14:58:14 +05:30
}
2022-05-31 20:21:18 +05:30
private _populateThemeMap(manifest) {
const builtAssets: Record<string, string> = manifest.source?.["built-assets"];
const themeName = manifest.name;
let defaultDarkVariant: any = {}, defaultLightVariant: any = {};
for (const [themeId, cssLocation] of Object.entries(builtAssets)) {
const variant = themeId.match(/.+-(.+)/)?.[1];
const { name: variantName, default: isDefault, dark } = manifest.values.variants[variant!];
const themeDisplayName = `${themeName} ${variantName}`;
if (isDefault) {
/**
* This is a default variant!
* We'll add these to the themeMapping (separately) keyed with just the
* theme-name (i.e "Element" instead of "Element Dark").
* We need to be able to distinguish them from other variants!
*
* This allows us to render radio-buttons with "dark" and
* "light" options.
*/
const defaultVariant = dark ? defaultDarkVariant : defaultLightVariant;
defaultVariant.variantName = variantName;
defaultVariant.id = themeId
defaultVariant.cssLocation = cssLocation;
continue;
}
// Non-default variants are keyed in themeMapping with "theme_name variant_name"
// eg: "Element Dark"
this._themeMapping[themeDisplayName] = {
cssLocation,
id: themeId
};
2022-06-02 15:05:43 +05:30
}
2022-05-31 20:21:18 +05:30
if (defaultDarkVariant.id && defaultLightVariant.id) {
/**
* As mentioned above, if there's both a default dark and a default light variant,
* add them to themeMapping separately.
*/
this._themeMapping[themeName] = { dark: defaultDarkVariant, light: defaultLightVariant };
}
else {
/**
* If only one default variant is found (i.e only dark default or light default but not both),
* treat it like any other variant.
*/
const variant = defaultDarkVariant.id ? defaultDarkVariant : defaultLightVariant;
this._themeMapping[`${themeName} ${variant.variantName}`] = { id: variant.id, cssLocation: variant.cssLocation };
}
}
2022-05-30 11:58:17 +05:30
setTheme(themeId: string, log?: ILogItem) {
2022-06-02 15:05:43 +05:30
this._platform.logger.wrapOrRun(log, { l: "change theme", id: themeId }, () => {
2022-05-30 11:58:17 +05:30
const themeLocation = this._findThemeLocationFromId(themeId);
2022-05-18 16:09:09 +05:30
if (!themeLocation) {
2022-06-02 15:05:43 +05:30
throw new Error(`Cannot find theme location for theme "${themeId}"!`);
2022-05-18 16:09:09 +05:30
}
this._platform.replaceStylesheet(themeLocation);
2022-05-30 11:58:17 +05:30
this._platform.settingsStorage.setString("theme", themeId);
2022-06-02 15:05:43 +05:30
});
2022-05-11 14:58:14 +05:30
}
get themeMapping(): Record<string, ThemeInformation> {
return this._themeMapping;
2022-05-11 14:58:14 +05:30
}
async getActiveTheme(): Promise<string> {
2022-05-27 16:31:23 +05:30
const theme = await this._platform.settingsStorage.getString("theme") ?? "default";
2022-05-18 18:56:28 +05:30
if (theme) {
return theme;
}
2022-05-27 14:23:38 +05:30
throw new Error("Cannot find active theme!");
}
getDefaultTheme(): string | undefined {
2022-06-02 15:05:43 +05:30
switch (this.preferredColorScheme) {
case ColorSchemePreference.Dark:
return this._platform.config["defaultTheme"].dark;
case ColorSchemePreference.Light:
return this._platform.config["defaultTheme"].light;
2022-05-18 18:56:28 +05:30
}
2022-05-11 14:58:14 +05:30
}
2022-05-27 14:24:19 +05:30
private _findThemeLocationFromId(themeId: string): string | undefined {
for (const themeData of Object.values(this._themeMapping)) {
if ("id" in themeData && themeData.id === themeId) {
return themeData.cssLocation;
}
else if ("light" in themeData && themeData.light?.id === themeId) {
return themeData.light.cssLocation;
}
else if ("dark" in themeData && themeData.dark?.id === themeId) {
return themeData.dark.cssLocation;
}
}
}
2022-06-02 15:05:43 +05:30
get preferredColorScheme(): ColorSchemePreference {
if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
return ColorSchemePreference.Dark;
}
else if (window.matchMedia("(prefers-color-scheme: light)").matches) {
return ColorSchemePreference.Light;
}
throw new Error("Cannot find preferred colorscheme!");
}
async persistVariantToStorage(variant: string) {
await this._platform.settingsStorage.setString("theme-variant", variant);
}
async getCurrentVariant(): Promise<string> {
return await this._platform.settingsStorage.getString("theme-variant");
}
async removeVariantFromStorage(): Promise<void> {
await this._platform.settingsStorage.remove("theme-variant");
}
2022-05-11 14:58:14 +05:30
}