feat: theme

This commit is contained in:
kodi
2026-03-12 18:26:29 +01:00
parent 939a7fd191
commit ab83ee3f20
13 changed files with 374 additions and 34 deletions
+4
View File
@@ -98,12 +98,16 @@ class SettingsResponse(BaseModel):
show_thumbnails: bool
preferred_startup_path_left: str | None = None
preferred_startup_path_right: str | None = None
selected_theme: str
selected_color_mode: str
class SettingsUpdateRequest(BaseModel):
show_thumbnails: bool | None = None
preferred_startup_path_left: str | None = None
preferred_startup_path_right: str | None = None
selected_theme: str | None = None
selected_color_mode: str | None = None
class TaskListItem(BaseModel):
@@ -1,10 +1,15 @@
from __future__ import annotations
from backend.app.api.errors import AppError
from backend.app.api.schemas import SettingsResponse, SettingsUpdateRequest
from backend.app.db.settings_repository import SettingsRepository
from backend.app.security.path_guard import PathGuard
VALID_THEMES = {"default"}
VALID_COLOR_MODES = {"dark", "light"}
class SettingsService:
def __init__(self, repository: SettingsRepository, path_guard: PathGuard):
self._repository = repository
@@ -15,10 +20,14 @@ class SettingsService:
preferred_left = self._as_optional_str(values.get("preferred_startup_path_left"))
preferred_right = self._as_optional_str(values.get("preferred_startup_path_right"))
legacy_preferred = self._as_optional_str(values.get("preferred_startup_path"))
selected_theme = self._normalize_theme(values.get("selected_theme"))
selected_color_mode = self._normalize_color_mode(values.get("selected_color_mode"))
return SettingsResponse(
show_thumbnails=self._as_bool(values.get("show_thumbnails"), default=False),
preferred_startup_path_left=preferred_left or legacy_preferred,
preferred_startup_path_right=preferred_right,
selected_theme=selected_theme,
selected_color_mode=selected_color_mode,
)
def update_settings(self, request: SettingsUpdateRequest) -> SettingsResponse:
@@ -31,6 +40,12 @@ class SettingsService:
if request.preferred_startup_path_right is not None:
self._set_directory_setting("preferred_startup_path_right", request.preferred_startup_path_right)
if request.selected_theme is not None:
self._repository.set_setting("selected_theme", self._validate_theme(request.selected_theme))
if request.selected_color_mode is not None:
self._repository.set_setting("selected_color_mode", self._validate_color_mode(request.selected_color_mode))
return self.get_settings()
def _set_directory_setting(self, key: str, value: str) -> None:
@@ -53,3 +68,35 @@ class SettingsService:
return None
normalized = value.strip()
return normalized or None
@staticmethod
def _normalize_theme(value: str | None) -> str:
normalized = (value or "").strip()
return normalized if normalized in VALID_THEMES else "default"
@staticmethod
def _normalize_color_mode(value: str | None) -> str:
normalized = (value or "").strip().lower()
return normalized if normalized in VALID_COLOR_MODES else "dark"
@staticmethod
def _validate_theme(value: str) -> str:
normalized = value.strip()
if normalized not in VALID_THEMES:
raise AppError(
status_code=400,
code="invalid_request",
message="Theme must be one of: default",
)
return normalized
@staticmethod
def _validate_color_mode(value: str) -> str:
normalized = value.strip().lower()
if normalized not in VALID_COLOR_MODES:
raise AppError(
status_code=400,
code="invalid_request",
message="Color mode must be one of: dark, light",
)
return normalized