mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-01-22 20:18:30 +03:00
Add custom theme support
This commit is contained in:
parent
7fb8d9b050
commit
90c61eeff6
@ -32,7 +32,11 @@ from frigate.config.camera.updater import (
|
||||
CameraConfigUpdateEnum,
|
||||
CameraConfigUpdateTopic,
|
||||
)
|
||||
<<<<<<< HEAD
|
||||
from frigate.ffmpeg_presets import FFMPEG_HWACCEL_VAAPI, _gpu_selector
|
||||
=======
|
||||
from frigate.const import THEMES_DIR
|
||||
>>>>>>> a3cdd1b1 (Add custom theme support)
|
||||
from frigate.models import Event, Timeline
|
||||
from frigate.stats.prometheus import get_metrics, update_metrics
|
||||
from frigate.util.builtin import (
|
||||
@ -183,6 +187,23 @@ def config(request: Request):
|
||||
|
||||
return JSONResponse(content=config)
|
||||
|
||||
@router.get("/config/themes")
|
||||
def config_themes():
|
||||
themes_dir = THEMES_DIR
|
||||
|
||||
if not os.path.isdir(themes_dir):
|
||||
return JSONResponse(content=[])
|
||||
|
||||
themes: list[str] = []
|
||||
for name in sorted(os.listdir(themes_dir)):
|
||||
if not name.lower().endswith(".css"):
|
||||
continue
|
||||
|
||||
full_path = os.path.join(themes_dir, name)
|
||||
if os.path.isfile(full_path):
|
||||
themes.append(name)
|
||||
|
||||
return JSONResponse(content=themes)
|
||||
|
||||
@router.get("/config/raw_paths", dependencies=[Depends(require_role(["admin"]))])
|
||||
def config_raw_paths(request: Request):
|
||||
|
||||
@ -5,6 +5,7 @@ INSTALL_DIR = "/opt/frigate"
|
||||
CONFIG_DIR = "/config"
|
||||
DEFAULT_DB_PATH = f"{CONFIG_DIR}/frigate.db"
|
||||
MODEL_CACHE_DIR = f"{CONFIG_DIR}/model_cache"
|
||||
THEMES_DIR = f"{CONFIG_DIR}/themes"
|
||||
BASE_DIR = "/media/frigate"
|
||||
CLIPS_DIR = f"{BASE_DIR}/clips"
|
||||
EXPORT_DIR = f"{BASE_DIR}/exports"
|
||||
|
||||
@ -487,11 +487,11 @@ export default function GeneralSettings({ className }: GeneralSettingsProps) {
|
||||
{scheme === colorScheme ? (
|
||||
<>
|
||||
<IoColorPalette className="mr-2 size-4 rotate-0 scale-100 transition-all" />
|
||||
{t(friendlyColorSchemeName(scheme))}
|
||||
{friendlyColorSchemeName(scheme, t)}
|
||||
</>
|
||||
) : (
|
||||
<span className="ml-6 mr-2">
|
||||
{t(friendlyColorSchemeName(scheme))}
|
||||
{friendlyColorSchemeName(scheme, t)}
|
||||
</span>
|
||||
)}
|
||||
</MenuItem>
|
||||
|
||||
@ -21,11 +21,25 @@ export const colorSchemes: ColorScheme[] = [
|
||||
|
||||
// Helper function to generate friendly color scheme names
|
||||
// eslint-disable-next-line react-refresh/only-export-components
|
||||
export const friendlyColorSchemeName = (className: string): string => {
|
||||
const words = className.split("-").slice(1); // Exclude the first word (e.g., 'theme')
|
||||
return "menu.theme." + words.join("");
|
||||
export const friendlyColorSchemeName = (
|
||||
className: string,
|
||||
t?: (key: string, options?: any) => string
|
||||
): string => {
|
||||
const words = className.split("-").slice(1);
|
||||
const key = "menu.theme." + words.join("");
|
||||
|
||||
if (!t) {
|
||||
return key;
|
||||
}
|
||||
|
||||
const fallback = words
|
||||
.join(" ")
|
||||
.replace(/\b\w/g, (char) => char.toUpperCase());
|
||||
|
||||
return t(key, { defaultValue: fallback });
|
||||
};
|
||||
|
||||
|
||||
type ThemeProviderProps = {
|
||||
children: React.ReactNode;
|
||||
defaultTheme?: Theme;
|
||||
@ -97,6 +111,44 @@ export function ThemeProvider({
|
||||
//console.log(localStorage.getItem(storageKey));
|
||||
const root = window.document.documentElement;
|
||||
|
||||
if (!(window as any).__frigateThemesLoaded) {
|
||||
(window as any).__frigateThemesLoaded = true;
|
||||
|
||||
fetch("/api/config/themes")
|
||||
.then((res) => (res.ok ? res.json() : []))
|
||||
.then((files: string[]) => {
|
||||
files.forEach((file) => {
|
||||
if (!file.endsWith(".css")) {
|
||||
return;
|
||||
}
|
||||
|
||||
const baseName = file.replace(/\.css$/, "");
|
||||
const className = baseName.startsWith("theme-")
|
||||
? baseName
|
||||
: `theme-${baseName}`;
|
||||
|
||||
if (!colorSchemes.includes(className as ColorScheme)) {
|
||||
// runtime extension is intentional
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
colorSchemes.push(className);
|
||||
}
|
||||
|
||||
if (
|
||||
!document.querySelector(`link[data-theme="${className}"]`)
|
||||
) {
|
||||
const link = document.createElement("link");
|
||||
link.rel = "stylesheet";
|
||||
link.href = `/config/themes/${file}`;
|
||||
link.dataset.theme = className;
|
||||
document.head.appendChild(link);
|
||||
}
|
||||
});
|
||||
})
|
||||
.catch(() => {
|
||||
});
|
||||
}
|
||||
|
||||
root.classList.remove("light", "dark", "system", ...colorSchemes);
|
||||
|
||||
root.classList.add(theme, colorScheme);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user