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
Binary file not shown.
@@ -59,6 +59,8 @@ class SettingsApiGoldenTest(unittest.TestCase):
"show_thumbnails": False,
"preferred_startup_path_left": None,
"preferred_startup_path_right": None,
"selected_theme": "default",
"selected_color_mode": "dark",
},
)
@@ -75,6 +77,8 @@ class SettingsApiGoldenTest(unittest.TestCase):
"show_thumbnails": False,
"preferred_startup_path_left": "storage1/docs",
"preferred_startup_path_right": None,
"selected_theme": "default",
"selected_color_mode": "dark",
},
)
@@ -96,6 +100,8 @@ class SettingsApiGoldenTest(unittest.TestCase):
"show_thumbnails": True,
"preferred_startup_path_left": "storage1/docs",
"preferred_startup_path_right": "storage1/docs",
"selected_theme": "default",
"selected_color_mode": "dark",
},
)
self.assertEqual(
@@ -104,6 +110,8 @@ class SettingsApiGoldenTest(unittest.TestCase):
"show_thumbnails": True,
"preferred_startup_path_left": "storage1/docs",
"preferred_startup_path_right": "storage1/docs",
"selected_theme": "default",
"selected_color_mode": "dark",
},
)
@@ -113,6 +121,8 @@ class SettingsApiGoldenTest(unittest.TestCase):
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()["preferred_startup_path_left"], "storage1/docs")
self.assertEqual(response.json()["preferred_startup_path_right"], None)
self.assertEqual(response.json()["selected_theme"], "default")
self.assertEqual(response.json()["selected_color_mode"], "dark")
def test_settings_preferred_startup_path_right_persistence(self) -> None:
response = self._request("POST", "/api/settings", {"preferred_startup_path_right": "storage1/docs"})
@@ -120,6 +130,8 @@ class SettingsApiGoldenTest(unittest.TestCase):
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()["preferred_startup_path_left"], None)
self.assertEqual(response.json()["preferred_startup_path_right"], "storage1/docs")
self.assertEqual(response.json()["selected_theme"], "default")
self.assertEqual(response.json()["selected_color_mode"], "dark")
def test_settings_preferred_startup_path_empty_string_resets_only_left_to_null(self) -> None:
self._request(
@@ -135,6 +147,34 @@ class SettingsApiGoldenTest(unittest.TestCase):
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()["preferred_startup_path_left"], None)
self.assertEqual(response.json()["preferred_startup_path_right"], "storage1/docs")
self.assertEqual(response.json()["selected_theme"], "default")
self.assertEqual(response.json()["selected_color_mode"], "dark")
def test_settings_selected_theme_persistence(self) -> None:
response = self._request("POST", "/api/settings", {"selected_theme": "default"})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()["selected_theme"], "default")
self.assertEqual(response.json()["selected_color_mode"], "dark")
def test_settings_selected_color_mode_persistence(self) -> None:
response = self._request("POST", "/api/settings", {"selected_color_mode": "light"})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()["selected_theme"], "default")
self.assertEqual(response.json()["selected_color_mode"], "light")
def test_settings_rejects_invalid_selected_theme(self) -> None:
response = self._request("POST", "/api/settings", {"selected_theme": "unknown"})
self.assertEqual(response.status_code, 400)
self.assertEqual(response.json()["error"]["code"], "invalid_request")
def test_settings_rejects_invalid_selected_color_mode(self) -> None:
response = self._request("POST", "/api/settings", {"selected_color_mode": "sepia"})
self.assertEqual(response.status_code, 400)
self.assertEqual(response.json()["error"]["code"], "invalid_request")
def test_settings_preferred_startup_path_left_rejects_file_path(self) -> None:
response = self._request("POST", "/api/settings", {"preferred_startup_path_left": "storage1/file.txt"})
@@ -65,14 +65,19 @@ class UiSmokeGoldenTest(unittest.TestCase):
self.assertIn('id="rename-input"', body)
self.assertIn('id="rename-apply-btn"', body)
self.assertIn('id="settings-general-tab"', body)
self.assertIn('id="settings-interface-tab"', body)
self.assertIn('id="settings-logs-tab"', body)
self.assertIn('id="settings-show-thumbnails"', body)
self.assertIn("Show thumbnails", body)
self.assertIn('id="settings-selected-theme"', body)
self.assertIn("Theme", body)
self.assertNotIn('id="settings-selected-color-mode"', body)
self.assertIn('id="settings-startup-path-left"', body)
self.assertIn('id="settings-startup-path-right"', body)
self.assertIn("Preferred startup path (left)", body)
self.assertIn("Preferred startup path (right)", body)
self.assertIn('id="settings-general-save-btn"', body)
self.assertIn('id="settings-interface-save-btn"', body)
self.assertIn('id="settings-logs-list"', body)
self.assertIn('id="viewer-content"', body)
self.assertIn('id="editor-modal"', body)
@@ -119,7 +124,9 @@ class UiSmokeGoldenTest(unittest.TestCase):
self.assertTrue((static_root / "style.css").exists())
app_js = (static_root / "app.js").read_text(encoding="utf-8")
self.assertIn('currentPath: "/Volumes"', app_js)
self.assertIn('const THEME_STORAGE_KEY = "webmanager-theme"', app_js)
self.assertIn('selectedTheme: "default"', app_js)
self.assertIn('selectedColorMode: "dark"', app_js)
self.assertIn('function effectiveThemeKey(theme, colorMode)', app_js)
self.assertIn("document.documentElement.dataset.theme", app_js)
self.assertIn('document.getElementById("theme-toggle").onclick = toggleTheme;', app_js)
self.assertIn('document.getElementById("settings-btn").onclick = () => openSettings("general");', app_js)
@@ -127,12 +134,19 @@ class UiSmokeGoldenTest(unittest.TestCase):
self.assertIn('await loadSettings();', app_js)
self.assertIn('settings.showThumbnailsInput.onchange = handleShowThumbnailsChange;', app_js)
self.assertIn('settings.generalSaveButton.onclick = handlePreferredStartupPathSave;', app_js)
self.assertIn('settings.interfaceSaveButton.onclick = handleInterfaceSave;', app_js)
self.assertIn('preferredStartupPathLeft', app_js)
self.assertIn('preferredStartupPathRight', app_js)
self.assertIn('selected_theme', app_js)
self.assertIn('selected_color_mode', app_js)
self.assertNotIn("localStorage", app_js)
self.assertNotIn("THEME_STORAGE_KEY", app_js)
self.assertIn('preferred_startup_path_left', app_js)
self.assertIn('preferred_startup_path_right', app_js)
self.assertIn('paneState("left").currentPath = settingsState.preferredStartupPathLeft || "/Volumes";', app_js)
self.assertIn('paneState("right").currentPath = settingsState.preferredStartupPathRight || "/Volumes";', app_js)
self.assertIn('applyTheme(settingsState.selectedTheme, settingsState.selectedColorMode);', app_js)
self.assertIn('settings.interfaceTab.onclick = () => setSettingsTab("interface");', app_js)
self.assertIn('"/api/settings"', app_js)
self.assertIn('`/api/files/thumbnail?', app_js)
self.assertIn("function iconTypeForEntry(entry)", app_js)
@@ -180,8 +194,8 @@ class UiSmokeGoldenTest(unittest.TestCase):
self.assertIn("function rootKeyFromPath(path)", app_js)
self.assertIn("function isNestedPath(sourcePath, destinationPath)", app_js)
style_css = (static_root / "style.css").read_text(encoding="utf-8")
self.assertIn(':root[data-theme="dark"]', style_css)
self.assertIn(':root[data-theme="light"]', style_css)
self.assertIn(':root[data-theme="default-dark"]', style_css)
self.assertIn(':root[data-theme="default-light"]', style_css)
self.assertIn('#theme-toggle', style_css)
self.assertIn('.settings-card', style_css)
self.assertIn('.settings-tabs', style_css)