forgejo/modules/theme/theme.go
Minecon724 072688f419
Improve theme picker
Signed-off-by: Minecon724 <minecon724@noreply.git.m724.eu>
2025-03-16 13:41:29 +01:00

100 lines
1.8 KiB
Go

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 FriendlyThemes map[string]string
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
FriendlyThemes = convertToFriendly(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")
}
}
func convertToFriendly(themes []string) map[string]string {
themesFriendly := make(map[string]string)
for _, theme := range themes {
friendly := theme
friendly = strings.ReplaceAll(friendly, "-", " ")
friendly = strings.ToLower(friendly)
friendly = strings.Title(friendly)
themesFriendly[theme] = friendly
}
return themesFriendly
}