/** * Copyright (c) 2021 OpenLens Authors * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ import { comparer, computed, makeObservable, observable, reaction } from "mobx"; import { autoBind, Singleton } from "./utils"; import { UserStore } from "../common/user-store"; import logger from "../common/logger"; import lensDarkThemeJson from "./themes/lens-dark.json"; import lensLightThemeJson from "./themes/lens-light.json"; import type { SelectOption } from "./components/select"; import type { MonacoEditorProps } from "./components/monaco-editor"; import { defaultTheme } from "../common/vars"; import { camelCase } from "lodash"; export type ThemeId = string; export interface Theme { name: string; type: "dark" | "light"; colors: Record; description: string; author: string; monacoTheme: MonacoEditorProps["theme"]; } export class ThemeStore extends Singleton { private terminalColorPrefix = "terminal"; // bundled themes from `themes/${themeId}.json` private themes = observable.map({ "lens-dark": lensDarkThemeJson as Theme, "lens-light": lensLightThemeJson as Theme, }); @computed get activeThemeId(): ThemeId { return UserStore.getInstance().colorTheme; } @computed get terminalThemeId(): ThemeId { return UserStore.getInstance().terminalTheme; } @computed get activeTheme(): Theme { return this.themes.get(this.activeThemeId) ?? this.themes.get(defaultTheme); } @computed get terminalColors(): [string, string][] { const theme = this.themes.get(this.terminalThemeId) ?? this.activeTheme; return Object .entries(theme.colors) .filter(([name]) => name.startsWith(this.terminalColorPrefix)); } // Replacing keys stored in styles to format accepted by terminal // E.g. terminalBrightBlack -> brightBlack @computed get xtermColors(): Record { return Object.fromEntries( this.terminalColors.map(([name, color]) => [ camelCase(name.replace(this.terminalColorPrefix, "")), color, ]), ); } @computed get themeOptions(): SelectOption[] { return Array.from(this.themes).map(([themeId, theme]) => ({ label: theme.name, value: themeId, })); } constructor() { super(); makeObservable(this); autoBind(this); // auto-apply active theme reaction(() => ({ themeId: this.activeThemeId, terminalThemeId: this.terminalThemeId, }), ({ themeId }) => { try { this.applyTheme(themeId); } catch (err) { logger.error(err); UserStore.getInstance().resetTheme(); } }, { fireImmediately: true, equals: comparer.shallow, }); } getThemeById(themeId: ThemeId): Theme { return this.themes.get(themeId); } protected applyTheme(themeId: ThemeId) { const theme = this.getThemeById(themeId); const colors = Object.entries({ ...theme.colors, ...Object.fromEntries(this.terminalColors), }); colors.forEach(([name, value]) => { document.documentElement.style.setProperty(`--${name}`, value); }); // Adding universal theme flag which can be used in component styles document.body.classList.toggle("theme-light", theme.type === "light"); } }