From 69d712e5fb1f895003cb5e17b59f61eb1754bd3f Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Sun, 16 Mar 2025 13:41:29 +0100 Subject: [PATCH] Theme detection + Improve theme picker + warning Upstream changes helped a little Signed-off-by: Minecon724 --- modules/theme/theme.go | 80 +++++++++++++++++++++++++ options/locale/locale_en-US.ini | 1 + routers/web/user/setting/profile.go | 15 +++++ templates/user/settings/appearance.tmpl | 10 ++-- 4 files changed, 101 insertions(+), 5 deletions(-) create mode 100644 modules/theme/theme.go diff --git a/modules/theme/theme.go b/modules/theme/theme.go new file mode 100644 index 0000000000..762926d78f --- /dev/null +++ b/modules/theme/theme.go @@ -0,0 +1,80 @@ +package theme + +import ( + "fmt" + "strings" + + "code.gitea.io/gitea/modules/assetfs" + "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/public" + "code.gitea.io/gitea/modules/setting" + + "golang.org/x/sync/singleflight" +) + +const ( + key string = "load-themes" +) + +var ( + group singleflight.Group + assetFs *assetfs.LayeredFS + loaded bool +) + +// LoadThemes loads installed themes +// +// Note that you can't just run this during init, because webpack mightn't have completed yet. +// Hence, we're loading on first demand. +func LoadThemes() error { + if loaded { + return nil + } + + if assetFs == nil { + assetFs = public.AssetFS() + } + + _, err, _ := group.Do(key, func() (interface{}, error) { + themes, err := loadThemesInner(assetFs) + + if err != nil { + group.Forget(key) + } else { + setting.UI.Themes = themes + loaded = true + } + + return nil, err + }) + + return err +} + +func loadThemesInner(assetFs *assetfs.LayeredFS) ([]string, error) { + entries, err := assetFs.ListFiles("assets/css") + + if err != nil { + return nil, err + } + + var themes []string + + for _, entry := range entries { + if !(strings.HasPrefix(entry, "theme-") && strings.HasSuffix(entry, ".css")) { + continue + } + + theme := entry[6 : len(entry)-4] + themes = append(themes, theme) + + log.Info("Found theme: %s", theme) + } + + if len(themes) > 0 { + log.Info("Loaded %d themes", len(themes)) + return themes, nil + } else { + return nil, fmt.Errorf("no themes found") + } +} diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 2a8c789385..3c39c99c4f 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -829,6 +829,7 @@ manage_themes = Default theme manage_openid = OpenID addresses email_desc = Your primary email address will be used for notifications, password recovery and, provided that it is not hidden, web-based Git operations. theme_desc = This theme will be used for the web interface when you are logged in. +theme_warning = WARNING: Themes are made by third parties. Click to read more. primary = Primary activated = Activated requires_activation = Requires activation diff --git a/routers/web/user/setting/profile.go b/routers/web/user/setting/profile.go index 271621872f..178cfc9d59 100644 --- a/routers/web/user/setting/profile.go +++ b/routers/web/user/setting/profile.go @@ -24,6 +24,7 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/optional" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/theme" "code.gitea.io/gitea/modules/translation" "code.gitea.io/gitea/modules/typesniffer" "code.gitea.io/gitea/modules/util" @@ -329,12 +330,26 @@ func Repos(ctx *context.Context) { func Appearance(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("settings.appearance") ctx.Data["PageIsSettingsAppearance"] = true + + err := theme.LoadThemes() + + if err != nil { + ctx.ServerError("Failed to load themes", err) + return + } + ctx.Data["AllThemes"] = setting.UI.Themes ctx.Data["ThemeName"] = func(themeName string) string { fullThemeName := "themes.names." + themeName if ctx.Locale.HasKey(fullThemeName) { return ctx.Locale.TrString(fullThemeName) } + + themeName = strings.ReplaceAll(themeName, "-", " ") + + themeName = strings.ToLower(themeName) + themeName = strings.Title(themeName) + return themeName } diff --git a/templates/user/settings/appearance.tmpl b/templates/user/settings/appearance.tmpl index df4d6f3999..be2b8d9962 100644 --- a/templates/user/settings/appearance.tmpl +++ b/templates/user/settings/appearance.tmpl @@ -7,15 +7,15 @@