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-06-28 12:28:19 +05:30
|
|
|
import type {ILogItem} from "../../logging/types";
|
2022-07-17 16:08:02 +05:30
|
|
|
import { ThemeManifest } from "../types/theme";
|
2022-05-11 14:58:14 +05:30
|
|
|
import type {Platform} from "./Platform.js";
|
2022-06-28 12:28:19 +05:30
|
|
|
import {ThemeBuilder} from "./ThemeBuilder";
|
2022-05-11 14:58:14 +05:30
|
|
|
|
2022-05-26 23:35:09 +05:30
|
|
|
type NormalVariant = {
|
|
|
|
id: string;
|
|
|
|
cssLocation: string;
|
2022-06-28 12:28:19 +05:30
|
|
|
variables?: any;
|
2022-05-26 23:35:09 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
type DefaultVariant = {
|
2022-06-28 12:28:19 +05:30
|
|
|
dark: NormalVariant & {
|
2022-05-30 11:48:02 +05:30
|
|
|
variantName: string;
|
2022-05-26 23:35:09 +05:30
|
|
|
};
|
2022-06-28 12:28:19 +05:30
|
|
|
light: NormalVariant & {
|
2022-05-30 11:48:02 +05:30
|
|
|
variantName: string;
|
2022-05-26 23:35:09 +05:30
|
|
|
};
|
2022-06-28 12:28:19 +05:30
|
|
|
default: NormalVariant & {
|
2022-06-05 20:49:05 +05:30
|
|
|
variantName: string;
|
|
|
|
};
|
2022-05-26 23:35:09 +05:30
|
|
|
}
|
2022-05-30 11:49:45 +05:30
|
|
|
|
2022-06-28 12:28:19 +05:30
|
|
|
export type ThemeInformation = NormalVariant | DefaultVariant;
|
2022-05-26 23:35:09 +05:30
|
|
|
|
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;
|
2022-05-26 23:35:09 +05:30
|
|
|
private _themeMapping: Record<string, ThemeInformation>;
|
2022-06-28 12:28:19 +05:30
|
|
|
private _themeBuilder: ThemeBuilder;
|
2022-05-11 14:58:14 +05:30
|
|
|
|
|
|
|
constructor(platform: Platform) {
|
|
|
|
this._platform = platform;
|
|
|
|
}
|
|
|
|
|
2022-06-07 11:57:57 +05:30
|
|
|
async init(manifestLocations: string[], log?: ILogItem): Promise<void> {
|
2022-06-28 12:28:19 +05:30
|
|
|
const idToManifest = new Map();
|
2022-06-07 11:57:57 +05:30
|
|
|
await this._platform.logger.wrapOrRun(log, "ThemeLoader.init", async (log) => {
|
|
|
|
this._themeMapping = {};
|
2022-06-12 17:05:31 +05:30
|
|
|
const results = await Promise.all(
|
|
|
|
manifestLocations.map( location => this._platform.request(location, { method: "GET", format: "json", cache: true, }).response())
|
|
|
|
);
|
2022-06-28 12:28:19 +05:30
|
|
|
results.forEach(({ body }, i) => idToManifest.set(body.id, { manifest: body, location: manifestLocations[i] }));
|
2022-07-15 15:05:50 +05:30
|
|
|
this._themeBuilder = new ThemeBuilder(this._platform, idToManifest, this.preferredColorScheme);
|
|
|
|
for (let i = 0; i < results.length; ++i) {
|
|
|
|
const { body } = results[i];
|
2022-07-17 17:35:29 +05:30
|
|
|
try {
|
|
|
|
if (body.extends) {
|
|
|
|
await this._themeBuilder.populateDerivedTheme(body);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this._populateThemeMap(body, manifestLocations[i], log);
|
|
|
|
}
|
2022-06-28 12:28:19 +05:30
|
|
|
}
|
2022-07-17 17:35:29 +05:30
|
|
|
catch(e) {
|
|
|
|
console.error(e);
|
2022-06-28 12:28:19 +05:30
|
|
|
}
|
2022-07-15 15:05:50 +05:30
|
|
|
}
|
2022-07-17 17:44:21 +05:30
|
|
|
//Add the default-theme as an additional option to the mapping
|
|
|
|
const defaultThemeId = this.getDefaultTheme();
|
|
|
|
if (defaultThemeId) {
|
|
|
|
const themeDetails = this._findThemeDetailsFromId(defaultThemeId);
|
|
|
|
if (themeDetails) {
|
|
|
|
this._themeMapping["Default"] = { id: "default", cssLocation: themeDetails.cssLocation };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.log({ l: "Default Theme", theme: defaultThemeId});
|
|
|
|
log.log({ l: "Preferred colorscheme", scheme: this.preferredColorScheme === ColorSchemePreference.Dark ? "dark" : "light" });
|
|
|
|
log.log({ l: "Result", themeMapping: this._themeMapping });
|
2022-06-28 12:28:19 +05:30
|
|
|
Object.assign(this._themeMapping, this._themeBuilder.themeMapping);
|
2022-06-07 11:57:57 +05:30
|
|
|
});
|
2022-05-11 14:58:14 +05:30
|
|
|
}
|
|
|
|
|
2022-07-17 16:08:02 +05:30
|
|
|
private _populateThemeMap(manifest: ThemeManifest, manifestLocation: string, log: ILogItem) {
|
2022-06-07 11:57:57 +05:30
|
|
|
log.wrap("populateThemeMap", (l) => {
|
|
|
|
/*
|
|
|
|
After build has finished, the source section of each theme manifest
|
|
|
|
contains `built-assets` which is a mapping from the theme-id to
|
|
|
|
cssLocation of theme
|
|
|
|
*/
|
|
|
|
const builtAssets: Record<string, string> = manifest.source?.["built-assets"];
|
|
|
|
const themeName = manifest.name;
|
2022-07-17 16:08:02 +05:30
|
|
|
if (!themeName) {
|
|
|
|
throw new Error(`Theme name not found in manifest at ${manifestLocation}`);
|
|
|
|
}
|
2022-06-07 11:57:57 +05:30
|
|
|
let defaultDarkVariant: any = {}, defaultLightVariant: any = {};
|
2022-06-20 20:35:06 +05:30
|
|
|
for (let [themeId, cssLocation] of Object.entries(builtAssets)) {
|
2022-07-11 12:40:24 +05:30
|
|
|
try {
|
|
|
|
/**
|
|
|
|
* This cssLocation is relative to the location of the manifest file.
|
|
|
|
* So we first need to resolve it relative to the root of this hydrogen instance.
|
|
|
|
*/
|
|
|
|
cssLocation = new URL(cssLocation, new URL(manifestLocation, window.location.origin)).href;
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
continue;
|
|
|
|
}
|
2022-06-07 11:57:57 +05:30
|
|
|
const variant = themeId.match(/.+-(.+)/)?.[1];
|
2022-07-17 16:08:02 +05:30
|
|
|
const variantDetails = manifest.values?.variants[variant!];
|
|
|
|
if (!variantDetails) {
|
|
|
|
throw new Error(`Variant ${variant} is missing in manifest at ${manifestLocation}`);
|
|
|
|
}
|
|
|
|
const { name: variantName, default: isDefault, dark } = variantDetails;
|
2022-06-07 11:57:57 +05:30
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
const defaultVariant = this.preferredColorScheme === ColorSchemePreference.Dark ? defaultDarkVariant : defaultLightVariant;
|
|
|
|
this._themeMapping[themeName] = { dark: defaultDarkVariant, light: defaultLightVariant, default: defaultVariant };
|
|
|
|
}
|
|
|
|
else {
|
2022-05-31 20:21:18 +05:30
|
|
|
/**
|
2022-06-07 11:57:57 +05:30
|
|
|
* 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-31 20:21:18 +05:30
|
|
|
}
|
2022-06-07 11:57:57 +05:30
|
|
|
});
|
2022-05-31 20:21:18 +05:30
|
|
|
}
|
|
|
|
|
2022-06-06 17:20:16 +05:30
|
|
|
setTheme(themeName: string, themeVariant?: "light" | "dark" | "default", log?: ILogItem) {
|
2022-06-05 20:49:05 +05:30
|
|
|
this._platform.logger.wrapOrRun(log, { l: "change theme", name: themeName, variant: themeVariant }, () => {
|
2022-06-28 12:28:19 +05:30
|
|
|
let cssLocation: string, variables: Record<string, string>;
|
2022-06-05 20:49:05 +05:30
|
|
|
let themeDetails = this._themeMapping[themeName];
|
|
|
|
if ("id" in themeDetails) {
|
|
|
|
cssLocation = themeDetails.cssLocation;
|
2022-06-28 12:28:19 +05:30
|
|
|
variables = themeDetails.variables;
|
2022-06-05 20:49:05 +05:30
|
|
|
}
|
|
|
|
else {
|
2022-06-06 17:20:16 +05:30
|
|
|
if (!themeVariant) {
|
|
|
|
throw new Error("themeVariant is undefined!");
|
|
|
|
}
|
|
|
|
cssLocation = themeDetails[themeVariant].cssLocation;
|
2022-06-28 12:28:19 +05:30
|
|
|
variables = themeDetails[themeVariant].variables;
|
2022-06-05 20:49:05 +05:30
|
|
|
}
|
|
|
|
this._platform.replaceStylesheet(cssLocation);
|
2022-06-28 12:28:19 +05:30
|
|
|
if (variables) {
|
|
|
|
this._themeBuilder.injectCSSVariables(variables);
|
|
|
|
}
|
2022-07-15 15:05:50 +05:30
|
|
|
else {
|
|
|
|
this._themeBuilder.removePreviousCSSVariables();
|
|
|
|
}
|
2022-06-05 20:49:05 +05:30
|
|
|
this._platform.settingsStorage.setString("theme-name", themeName);
|
|
|
|
if (themeVariant) {
|
|
|
|
this._platform.settingsStorage.setString("theme-variant", themeVariant);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this._platform.settingsStorage.remove("theme-variant");
|
2022-05-18 16:09:09 +05:30
|
|
|
}
|
2022-06-02 15:05:43 +05:30
|
|
|
});
|
2022-05-11 14:58:14 +05:30
|
|
|
}
|
|
|
|
|
2022-06-12 16:52:21 +05:30
|
|
|
/** Maps theme display name to theme information */
|
2022-05-26 23:35:09 +05:30
|
|
|
get themeMapping(): Record<string, ThemeInformation> {
|
|
|
|
return this._themeMapping;
|
2022-05-11 14:58:14 +05:30
|
|
|
}
|
|
|
|
|
2022-06-05 20:49:05 +05:30
|
|
|
async getActiveTheme(): Promise<{themeName: string, themeVariant?: string}> {
|
2022-06-06 12:20:06 +05:30
|
|
|
let themeName = await this._platform.settingsStorage.getString("theme-name");
|
|
|
|
let themeVariant = await this._platform.settingsStorage.getString("theme-variant");
|
|
|
|
if (!themeName || !this._themeMapping[themeName]) {
|
|
|
|
themeName = "Default" in this._themeMapping ? "Default" : Object.keys(this._themeMapping)[0];
|
|
|
|
if (!this._themeMapping[themeName][themeVariant]) {
|
|
|
|
themeVariant = "default" in this._themeMapping[themeName] ? "default" : undefined;
|
|
|
|
}
|
|
|
|
}
|
2022-06-05 20:49:05 +05:30
|
|
|
return { themeName, themeVariant };
|
2022-05-27 14:23:38 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
getDefaultTheme(): string | undefined {
|
2022-06-02 15:05:43 +05:30
|
|
|
switch (this.preferredColorScheme) {
|
|
|
|
case ColorSchemePreference.Dark:
|
2022-06-06 12:19:48 +05:30
|
|
|
return this._platform.config["defaultTheme"]?.dark;
|
2022-06-02 15:05:43 +05:30
|
|
|
case ColorSchemePreference.Light:
|
2022-06-06 12:19:48 +05:30
|
|
|
return this._platform.config["defaultTheme"]?.light;
|
2022-05-18 18:56:28 +05:30
|
|
|
}
|
2022-05-11 14:58:14 +05:30
|
|
|
}
|
2022-05-26 23:35:09 +05:30
|
|
|
|
2022-06-05 20:49:05 +05:30
|
|
|
private _findThemeDetailsFromId(themeId: string): {themeName: string, cssLocation: string, variant?: string} | undefined {
|
|
|
|
for (const [themeName, themeData] of Object.entries(this._themeMapping)) {
|
2022-05-26 23:35:09 +05:30
|
|
|
if ("id" in themeData && themeData.id === themeId) {
|
2022-06-05 20:49:05 +05:30
|
|
|
return { themeName, cssLocation: themeData.cssLocation };
|
2022-05-26 23:35:09 +05:30
|
|
|
}
|
|
|
|
else if ("light" in themeData && themeData.light?.id === themeId) {
|
2022-06-05 20:49:05 +05:30
|
|
|
return { themeName, cssLocation: themeData.light.cssLocation, variant: "light" };
|
2022-05-26 23:35:09 +05:30
|
|
|
}
|
|
|
|
else if ("dark" in themeData && themeData.dark?.id === themeId) {
|
2022-06-05 20:49:05 +05:30
|
|
|
return { themeName, cssLocation: themeData.dark.cssLocation, variant: "dark" };
|
2022-05-26 23:35:09 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-02 15:05:43 +05:30
|
|
|
|
2022-06-14 19:27:18 +05:30
|
|
|
get preferredColorScheme(): ColorSchemePreference | undefined {
|
2022-06-02 15:05:43 +05:30
|
|
|
if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
|
|
|
|
return ColorSchemePreference.Dark;
|
|
|
|
}
|
|
|
|
else if (window.matchMedia("(prefers-color-scheme: light)").matches) {
|
|
|
|
return ColorSchemePreference.Light;
|
|
|
|
}
|
|
|
|
}
|
2022-05-11 14:58:14 +05:30
|
|
|
}
|