Merge pull request #3638 from matrix-org/dbkr/dedup_theming_code

Remove getBaseTheme
This commit is contained in:
David Baker 2019-11-21 09:44:37 +00:00 committed by GitHub
commit 04641a0f87
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -127,28 +127,14 @@ function getCustomTheme(themeName) {
return customTheme;
}
/**
* Gets the underlying theme name for the given theme. This is usually the theme or
* CSS resource that the theme relies upon to load.
* @param {string} theme The theme name to get the base of.
* @returns {string} The base theme (typically "light" or "dark").
*/
export function getBaseTheme(theme) {
if (!theme) return "light";
if (theme.startsWith("custom-")) {
const customTheme = getCustomTheme(theme.substr(7));
return customTheme.is_dark ? "dark-custom" : "light-custom";
}
return theme; // it's probably a base theme
}
/**
* Called whenever someone changes the theme
* Async function that returns once the theme has been set
* (ie. the CSS has been loaded)
*
* @param {string} theme new theme
*/
export function setTheme(theme) {
export async function setTheme(theme) {
if (!theme) {
const themeWatcher = new ThemeWatcher();
theme = themeWatcher.getEffectiveTheme();
@ -190,6 +176,7 @@ export function setTheme(theme) {
styleElements[stylesheetName].disabled = false;
return new Promise((resolve) => {
const switchTheme = function() {
// we re-enable our theme here just in case we raced with another
// theme set request as per https://github.com/vector-im/riot-web/issues/5601.
@ -201,6 +188,7 @@ export function setTheme(theme) {
a.disabled = true;
});
Tinter.setTheme(theme);
resolve();
};
// turns out that Firefox preloads the CSS for link elements with
@ -224,4 +212,5 @@ export function setTheme(theme) {
styleElements[stylesheetName].onload = undefined;
switchTheme();
}
});
}