2019-10-01 16:21:22 +03:00
|
|
|
/*
|
|
|
|
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
|
2019-10-02 12:05:57 +03:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2019-10-01 16:21:22 +03:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {_t} from "./languageHandler";
|
|
|
|
|
2020-07-14 19:23:06 +03:00
|
|
|
export const DEFAULT_THEME = "light";
|
2019-10-01 16:21:22 +03:00
|
|
|
import Tinter from "./Tinter";
|
2020-05-28 15:55:07 +03:00
|
|
|
import SettingsStore from "./settings/SettingsStore";
|
|
|
|
import ThemeWatcher from "./settings/watchers/ThemeWatcher";
|
2019-11-20 16:41:06 +03:00
|
|
|
|
2019-10-01 16:21:22 +03:00
|
|
|
export function enumerateThemes() {
|
|
|
|
const BUILTIN_THEMES = {
|
2020-06-11 14:27:09 +03:00
|
|
|
"light": _t("Light"),
|
|
|
|
"dark": _t("Dark"),
|
2019-10-01 16:21:22 +03:00
|
|
|
};
|
|
|
|
const customThemes = SettingsStore.getValue("custom_themes");
|
|
|
|
const customThemeNames = {};
|
|
|
|
for (const {name} of customThemes) {
|
|
|
|
customThemeNames[`custom-${name}`] = name;
|
|
|
|
}
|
|
|
|
return Object.assign({}, customThemeNames, BUILTIN_THEMES);
|
|
|
|
}
|
|
|
|
|
2020-06-23 18:54:17 +03:00
|
|
|
function clearCustomTheme() {
|
|
|
|
// remove all css variables, we assume these are there because of the custom theme
|
|
|
|
const inlineStyleProps = Object.values(document.body.style);
|
|
|
|
for (const prop of inlineStyleProps) {
|
|
|
|
if (prop.startsWith("--")) {
|
|
|
|
document.body.style.removeProperty(prop);
|
|
|
|
}
|
|
|
|
}
|
2020-06-23 18:54:38 +03:00
|
|
|
const customFontFaceStyle = document.querySelector("head > style[title='custom-theme-font-faces']");
|
|
|
|
if (customFontFaceStyle) {
|
|
|
|
customFontFaceStyle.remove();
|
|
|
|
}
|
2020-06-23 18:54:17 +03:00
|
|
|
}
|
|
|
|
|
2020-06-24 15:54:14 +03:00
|
|
|
const allowedFontFaceProps = [
|
|
|
|
"font-display",
|
|
|
|
"font-family",
|
|
|
|
"font-stretch",
|
|
|
|
"font-style",
|
|
|
|
"font-weight",
|
|
|
|
"font-variant",
|
|
|
|
"font-feature-settings",
|
|
|
|
"font-variation-settings",
|
|
|
|
"src",
|
2020-06-24 15:58:41 +03:00
|
|
|
"unicode-range",
|
2020-06-24 15:54:14 +03:00
|
|
|
];
|
|
|
|
|
2020-06-23 18:54:38 +03:00
|
|
|
function generateCustomFontFaceCSS(faces) {
|
2020-06-24 15:54:14 +03:00
|
|
|
return faces.map(face => {
|
|
|
|
const src = face.src && face.src.map(srcElement => {
|
|
|
|
let format;
|
|
|
|
if (srcElement.format) {
|
|
|
|
format = `format("${srcElement.format}")`;
|
|
|
|
}
|
|
|
|
if (srcElement.url) {
|
|
|
|
return `url("${srcElement.url}") ${format}`;
|
|
|
|
} else if (srcElement.local) {
|
|
|
|
return `local("${srcElement.local}") ${format}`;
|
|
|
|
}
|
|
|
|
return "";
|
2020-06-23 18:54:38 +03:00
|
|
|
}).join(", ");
|
2020-06-24 15:54:14 +03:00
|
|
|
const props = Object.keys(face).filter(prop => allowedFontFaceProps.includes(prop));
|
|
|
|
const body = props.map(prop => {
|
|
|
|
let value;
|
|
|
|
if (prop === "src") {
|
|
|
|
value = src;
|
|
|
|
} else if (prop === "font-family") {
|
|
|
|
value = `"${face[prop]}"`;
|
|
|
|
} else {
|
|
|
|
value = face[prop];
|
|
|
|
}
|
|
|
|
return `${prop}: ${value}`;
|
|
|
|
}).join(";");
|
|
|
|
return `@font-face {${body}}`;
|
2020-06-23 18:54:38 +03:00
|
|
|
}).join("\n");
|
|
|
|
}
|
2020-04-27 18:31:22 +03:00
|
|
|
|
2019-10-01 18:00:29 +03:00
|
|
|
function setCustomThemeVars(customTheme) {
|
|
|
|
const {style} = document.body;
|
2020-04-27 18:31:22 +03:00
|
|
|
|
2020-06-23 18:52:54 +03:00
|
|
|
function setCSSColorVariable(name, hexColor, doPct = true) {
|
2020-04-27 18:31:22 +03:00
|
|
|
style.setProperty(`--${name}`, hexColor);
|
|
|
|
if (doPct) {
|
2020-04-27 20:39:07 +03:00
|
|
|
// uses #rrggbbaa to define the color with alpha values at 0%, 15% and 50%
|
2019-10-01 18:00:29 +03:00
|
|
|
style.setProperty(`--${name}-0pct`, hexColor + "00");
|
2020-04-27 18:30:28 +03:00
|
|
|
style.setProperty(`--${name}-15pct`, hexColor + "26");
|
2019-10-01 18:00:29 +03:00
|
|
|
style.setProperty(`--${name}-50pct`, hexColor + "7F");
|
|
|
|
}
|
|
|
|
}
|
2020-04-27 18:31:22 +03:00
|
|
|
|
|
|
|
if (customTheme.colors) {
|
|
|
|
for (const [name, value] of Object.entries(customTheme.colors)) {
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
for (let i = 0; i < value.length; i += 1) {
|
2020-06-23 18:52:54 +03:00
|
|
|
setCSSColorVariable(`${name}_${i}`, value[i], false);
|
2020-04-27 18:31:22 +03:00
|
|
|
}
|
|
|
|
} else {
|
2020-06-23 18:52:54 +03:00
|
|
|
setCSSColorVariable(name, value);
|
2020-04-27 18:31:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-23 18:53:16 +03:00
|
|
|
if (customTheme.fonts) {
|
|
|
|
const {fonts} = customTheme;
|
2020-06-23 18:54:38 +03:00
|
|
|
if (fonts.faces) {
|
|
|
|
const css = generateCustomFontFaceCSS(fonts.faces);
|
|
|
|
const style = document.createElement("style");
|
|
|
|
style.setAttribute("title", "custom-theme-font-faces");
|
|
|
|
style.setAttribute("type", "text/css");
|
|
|
|
style.appendChild(document.createTextNode(css));
|
|
|
|
document.head.appendChild(style);
|
|
|
|
}
|
2020-06-23 18:53:16 +03:00
|
|
|
if (fonts.general) {
|
|
|
|
style.setProperty("--font-family", fonts.general);
|
|
|
|
}
|
|
|
|
if (fonts.monospace) {
|
|
|
|
style.setProperty("--font-family-monospace", fonts.monospace);
|
|
|
|
}
|
|
|
|
}
|
2019-10-01 18:00:29 +03:00
|
|
|
}
|
|
|
|
|
2020-06-08 18:24:08 +03:00
|
|
|
export function getCustomTheme(themeName) {
|
2019-10-01 16:21:22 +03:00
|
|
|
// set css variables
|
|
|
|
const customThemes = SettingsStore.getValue("custom_themes");
|
|
|
|
if (!customThemes) {
|
|
|
|
throw new Error(`No custom themes set, can't set custom theme "${themeName}"`);
|
|
|
|
}
|
|
|
|
const customTheme = customThemes.find(t => t.name === themeName);
|
|
|
|
if (!customTheme) {
|
|
|
|
const knownNames = customThemes.map(t => t.name).join(", ");
|
|
|
|
throw new Error(`Can't find custom theme "${themeName}", only know ${knownNames}`);
|
|
|
|
}
|
2019-10-01 18:00:29 +03:00
|
|
|
return customTheme;
|
2019-10-01 16:21:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called whenever someone changes the theme
|
2019-11-20 18:45:32 +03:00
|
|
|
* Async function that returns once the theme has been set
|
|
|
|
* (ie. the CSS has been loaded)
|
2019-10-01 16:21:22 +03:00
|
|
|
*
|
|
|
|
* @param {string} theme new theme
|
|
|
|
*/
|
2019-11-20 18:45:32 +03:00
|
|
|
export async function setTheme(theme) {
|
2019-10-01 16:21:22 +03:00
|
|
|
if (!theme) {
|
2019-11-20 16:41:06 +03:00
|
|
|
const themeWatcher = new ThemeWatcher();
|
|
|
|
theme = themeWatcher.getEffectiveTheme();
|
2019-10-01 16:21:22 +03:00
|
|
|
}
|
2020-06-23 18:54:17 +03:00
|
|
|
clearCustomTheme();
|
2019-10-01 16:21:22 +03:00
|
|
|
let stylesheetName = theme;
|
|
|
|
if (theme.startsWith("custom-")) {
|
2019-10-01 18:00:29 +03:00
|
|
|
const customTheme = getCustomTheme(theme.substr(7));
|
|
|
|
stylesheetName = customTheme.is_dark ? "dark-custom" : "light-custom";
|
|
|
|
setCustomThemeVars(customTheme);
|
2019-10-01 16:21:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// look for the stylesheet elements.
|
|
|
|
// styleElements is a map from style name to HTMLLinkElement.
|
|
|
|
const styleElements = Object.create(null);
|
|
|
|
let a;
|
|
|
|
for (let i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
|
|
|
|
const href = a.getAttribute("href");
|
|
|
|
// shouldn't we be using the 'title' tag rather than the href?
|
2021-03-26 18:43:59 +03:00
|
|
|
const match = href && href.match(/^bundles\/.*\/theme-(.*)\.css$/);
|
2019-10-01 16:21:22 +03:00
|
|
|
if (match) {
|
|
|
|
styleElements[match[1]] = a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(stylesheetName in styleElements)) {
|
|
|
|
throw new Error("Unknown theme " + stylesheetName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// disable all of them first, then enable the one we want. Chrome only
|
|
|
|
// bothers to do an update on a true->false transition, so this ensures
|
|
|
|
// that we get exactly one update, at the right time.
|
|
|
|
//
|
|
|
|
// ^ This comment was true when we used to use alternative stylesheets
|
|
|
|
// for the CSS. Nowadays we just set them all as disabled in index.html
|
|
|
|
// and enable them as needed. It might be cleaner to disable them all
|
|
|
|
// at the same time to prevent loading two themes simultaneously and
|
|
|
|
// having them interact badly... but this causes a flash of unstyled app
|
|
|
|
// which is even uglier. So we don't.
|
|
|
|
|
|
|
|
styleElements[stylesheetName].disabled = false;
|
|
|
|
|
2019-11-20 18:45:32 +03:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
const switchTheme = function() {
|
|
|
|
// we re-enable our theme here just in case we raced with another
|
2020-08-03 18:02:26 +03:00
|
|
|
// theme set request as per https://github.com/vector-im/element-web/issues/5601.
|
2019-11-20 18:45:32 +03:00
|
|
|
// We could alternatively lock or similar to stop the race, but
|
|
|
|
// this is probably good enough for now.
|
|
|
|
styleElements[stylesheetName].disabled = false;
|
|
|
|
Object.values(styleElements).forEach((a) => {
|
|
|
|
if (a == styleElements[stylesheetName]) return;
|
|
|
|
a.disabled = true;
|
|
|
|
});
|
2020-06-23 18:54:57 +03:00
|
|
|
const bodyStyles = global.getComputedStyle(document.body);
|
2020-05-06 18:23:06 +03:00
|
|
|
if (bodyStyles.backgroundColor) {
|
|
|
|
document.querySelector('meta[name="theme-color"]').content = bodyStyles.backgroundColor;
|
|
|
|
}
|
2019-11-20 18:45:32 +03:00
|
|
|
Tinter.setTheme(theme);
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
// turns out that Firefox preloads the CSS for link elements with
|
|
|
|
// the disabled attribute, but Chrome doesn't.
|
|
|
|
|
|
|
|
let cssLoaded = false;
|
|
|
|
|
|
|
|
styleElements[stylesheetName].onload = () => {
|
|
|
|
switchTheme();
|
|
|
|
};
|
|
|
|
|
|
|
|
for (let i = 0; i < document.styleSheets.length; i++) {
|
|
|
|
const ss = document.styleSheets[i];
|
|
|
|
if (ss && ss.href === styleElements[stylesheetName].href) {
|
|
|
|
cssLoaded = true;
|
|
|
|
break;
|
|
|
|
}
|
2019-10-01 16:21:22 +03:00
|
|
|
}
|
|
|
|
|
2019-11-20 18:45:32 +03:00
|
|
|
if (cssLoaded) {
|
|
|
|
styleElements[stylesheetName].onload = undefined;
|
|
|
|
switchTheme();
|
|
|
|
}
|
|
|
|
});
|
2019-10-01 16:21:22 +03:00
|
|
|
}
|