46 lines
1.5 KiB
Python
46 lines
1.5 KiB
Python
from typing import Optional
|
|
from PyQt6.QtWidgets import QApplication
|
|
from .theme import Theme, StyleSheet
|
|
|
|
class ThemeManager:
|
|
_instance: Optional['ThemeManager'] = None
|
|
_current_theme = Theme.DARK
|
|
|
|
def __new__(cls):
|
|
if cls._instance is None:
|
|
cls._instance = super(ThemeManager, cls).__new__(cls)
|
|
return cls._instance
|
|
|
|
@classmethod
|
|
def get_instance(cls) -> 'ThemeManager':
|
|
if cls._instance is None:
|
|
cls._instance = cls()
|
|
return cls._instance
|
|
|
|
@property
|
|
def current_theme(self) -> Theme:
|
|
return self._current_theme
|
|
|
|
def switch_theme(self, theme: Theme):
|
|
"""Переключение темы приложения"""
|
|
self._current_theme = theme
|
|
self.apply_theme()
|
|
|
|
def toggle_theme(self):
|
|
"""Переключение между светлой и темной темой"""
|
|
new_theme = Theme.LIGHT if self._current_theme == Theme.DARK else Theme.DARK
|
|
self.switch_theme(new_theme)
|
|
|
|
def apply_theme(self):
|
|
"""Применение текущей темы ко всему приложению"""
|
|
if not QApplication.instance():
|
|
return
|
|
|
|
# Применяем базовые стили
|
|
QApplication.instance().setStyleSheet(
|
|
StyleSheet.get_base_styles(self._current_theme)
|
|
)
|
|
|
|
def get_style(self, style_name: str) -> str:
|
|
"""Получение конкретного стиля"""
|
|
return StyleSheet.get_base_styles(self._current_theme) |