Dynamic theme loading
Signed-off-by: Minecon724 <minecon724@noreply.git.m724.eu>
This commit is contained in:
parent
afcfa5e582
commit
53c8df0d96
2 changed files with 90 additions and 0 deletions
80
modules/theme/theme.go
Normal file
80
modules/theme/theme.go
Normal file
|
@ -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")
|
||||
}
|
||||
}
|
|
@ -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"
|
||||
|
@ -323,6 +324,15 @@ func Repos(ctx *context.Context) {
|
|||
|
||||
// Appearance render user's appearance settings
|
||||
func Appearance(ctx *context.Context) {
|
||||
err := theme.LoadThemes()
|
||||
|
||||
if err != nil {
|
||||
ctx.ServerError("Failed to load themes", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Data["AllThemes"] = setting.UI.Themes
|
||||
|
||||
ctx.Data["Title"] = ctx.Tr("settings.appearance")
|
||||
ctx.Data["PageIsSettingsAppearance"] = true
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue