diff --git a/dooit/utils/css_manager.py b/dooit/utils/css_manager.py index ba8ea417..00fe34f2 100644 --- a/dooit/utils/css_manager.py +++ b/dooit/utils/css_manager.py @@ -5,16 +5,46 @@ dooit_cache_path = Path(user_cache_dir("dooit")) + def generate_random_id(): return uuid4().hex + class CssManager: - css_file: Path = dooit_cache_path / "dooit.tcss" + base_css: Path = Path(__file__).parent.parent.parent / "ui" / "styles.tcss" stylesheets: Path = dooit_cache_path / "stylesheets" + css_file: Path = dooit_cache_path / "dooit.tcss" + theme_css_file: Path = dooit_cache_path / "theme.tcss" + base_css_file: Path = dooit_cache_path / "base.tcss" + + def _create_files(self): + if not self.stylesheets.exists(): + self.stylesheets.mkdir( + parents=True, + exist_ok=True, + ) + + if not self.theme_css_file.exists(): + self.theme_css_file.touch() + + if not self.base_css_file.exists(): + self.base_css_file.touch() + def refresh_css(self): + self._create_files() + css = "" + # setup theme variables + with open(self.theme_css_file, "r") as f: + css = css + "\n" + f.read() + + # setup base variables + with open(self.base_css_file, "r") as f: + css = css + "\n" + f.read() + + # inject extra stylesheets for sheet in self.stylesheets.iterdir(): with open(sheet, "r") as f: css = css + "\n" + f.read() @@ -22,9 +52,7 @@ def refresh_css(self): self.write(css) def set_theme(self, theme: DooitThemeBase): - theme_file = self.stylesheets / "theme.tcss" - - with open(theme_file, "w") as f: + with open(self.theme_css_file, "w") as f: f.write(theme.to_css()) self.refresh_css() @@ -36,6 +64,7 @@ def inject_css(self, css: str) -> str: with open(css_file, "w") as f: f.write(css) + self.refresh_css() return uuid def uninject_css(self, _id: str) -> bool: