All files / src/lib/themes themeManager.ts

30.18% Statements 16/53
5% Branches 1/20
14.28% Functions 1/7
30.18% Lines 16/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 1151x 1x 1x 1x 1x   1x 1x 1x 1x 1x       1x         1x         1x                             1x         1x 1x                                                                                                                                          
import { getGlobalPluginsPath } from "lib/pluginManager/globalPlugins";
import darkTheme from "ui/theme/darkTheme";
import darkThemeWin from "ui/theme/darkThemeWin";
import lightTheme from "ui/theme/lightTheme";
import lightThemeWin from "ui/theme/lightThemeWin";
import { ThemeInterface } from "ui/theme/ThemeInterface";
import { glob } from "lib/helpers/glob";
import { relative } from "path";
import { readJSON } from "fs-extra";
import merge from "lodash/merge";
import cloneDeep from "lodash/cloneDeep";
 
type ThemeId = "dark" | "light";
 
const themes: Record<ThemeId, ThemeInterface> = {
  light: lightTheme,
  dark: darkTheme,
};
 
const windowsThemes: Record<ThemeId, ThemeInterface> = {
  light: lightThemeWin,
  dark: darkThemeWin,
};
 
const loadThemePlugin = async (
  path: string,
): Promise<(JSON & { name: string; type: unknown }) | null> => {
  try {
    const theme = await readJSON(path);
    if (!theme.name) {
      throw new Error("Theme is missing name");
    }
    return theme;
  } catch (e) {
    console.error("Unable to load theme", e);
  }
  return null;
};
 
export class ThemeManager {
  systemThemes: Record<ThemeId, ThemeInterface>;
  pluginThemes: Record<string, ThemeInterface>;
 
  constructor(platform: string) {
    this.systemThemes = platform === "darwin" ? themes : windowsThemes;
    this.pluginThemes = {};
  }
 
  async loadPluginThemes() {
    this.pluginThemes = {};
    const globalPluginsPath = getGlobalPluginsPath();
    const pluginPaths = await glob("**/theme.json", {
      cwd: globalPluginsPath,
      absolute: true,
    });
    for (const path of pluginPaths) {
      const theme = await loadThemePlugin(path);
      if (theme) {
        const id = relative(globalPluginsPath, path);
        const baseTheme =
          theme.type === "dark"
            ? this.systemThemes.dark
            : this.systemThemes.light;
        const mergedTheme = merge(cloneDeep(baseTheme), theme);
        this.pluginThemes[id] = mergedTheme;
      }
    }
  }
 
  async loadPluginTheme(path: string) {
    const globalPluginsPath = getGlobalPluginsPath();
    const theme = await loadThemePlugin(path);
    if (theme) {
      const id = relative(globalPluginsPath, path);
      const baseTheme =
        theme.type === "dark"
          ? this.systemThemes.dark
          : this.systemThemes.light;
      const mergedTheme = merge(cloneDeep(baseTheme), theme);
      this.pluginThemes[id] = mergedTheme;
      return mergedTheme;
    }
  }
 
  getTheme(themeId: string, systemShouldUseDarkColors: boolean) {
    // Use plugin theme if available
    const pluginTheme = this.pluginThemes[themeId];
    if (pluginTheme) {
      return pluginTheme;
    }
 
    // If light/dark set manually
    if (themeId === "light") {
      return this.systemThemes[themeId];
    } else if (themeId === "dark") {
      return this.systemThemes[themeId];
    }
 
    // Fallback to system theme
    if (systemShouldUseDarkColors) {
      return this.systemThemes["dark"];
    }
    return this.systemThemes["light"];
  }
 
  getPluginThemes() {
    return Object.entries(this.pluginThemes).map(([id, theme]) => {
      return {
        id,
        name: theme.name,
      };
    });
  }
}