import json import os from typing import Any, Dict, List, Optional import app.utils.paths as paths class Theme: def __init__(self, name: str, colors: Dict[str, str]) -> None: self.name: str = name self.colors: Dict[str, str] = colors def get_color(self, element: str) -> str: return self.colors.get(element, "#FFFFFF") class ThemeManager: def __init__(self) -> None: theme_path: str = os.path.join(paths.get_data_dir(), "themes") base_theme_path: str = os.path.join(theme_path, "base.qss") with open(base_theme_path, "r", encoding="utf-8") as theme_file: self.base_sheet: str = theme_file.read() self.themes: List[Theme] = [] for theme_file in sorted(os.listdir(theme_path)): if not theme_file.endswith(".json"): continue with open(os.path.join(theme_path, theme_file), "r", encoding="utf-8") as file: theme_data: Dict[str, Any] = json.load(file) self.themes.append(Theme(theme_data["theme_name"], theme_data["colors"])) self.current_theme: Theme = self.themes[0] def set_theme(self, theme: str) -> None: if theme != self.current_theme.name: found_theme: Optional[Theme] = next( (available_theme for available_theme in self.themes if available_theme.name == theme), None, ) if found_theme: self.current_theme = found_theme def get_theme(self) -> Theme: return self.current_theme def get_themes(self) -> List[Theme]: return self.themes def get_sheet(self) -> str: sheet: str = self.base_sheet for color_name, color_value in self.current_theme.colors.items(): sheet = sheet.replace("{{" + color_name + "}}", color_value) return sheet