Theme detection + Improve theme picker + warning

Upstream changes helped a little

Signed-off-by: Minecon724 <minecon724@noreply.git.m724.eu>
This commit is contained in:
Minecon724 2025-03-16 13:41:29 +01:00
parent d40ff8c1c1
commit 69d712e5fb
Signed by: Minecon724
GPG key ID: A02E6E67AB961189
4 changed files with 101 additions and 5 deletions

80
modules/theme/theme.go Normal file
View 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")
}
}

View file

@ -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 = <strong>WARNING:</strong> Themes are made by third parties. <a href="/git724/git724/src/branch/master/THEMES.md">Click to read more.</a>
primary = Primary
activated = Activated
requires_activation = Requires activation

View file

@ -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
}

View file

@ -7,15 +7,15 @@
</h4>
<div class="ui attached segment">
<div class="ui email list">
<div class="item">
{{ctx.Locale.Tr "settings.theme_desc"}}
<div class="item tw-mb-4">
<p>{{ctx.Locale.Tr "settings.theme_desc"}}</p>
<p>{{ctx.Locale.Tr "settings.theme_warning"}}</p>
</div>
<form class="ui form" action="{{.Link}}/theme" method="post">
{{.CsrfTokenHtml}}
<div class="field">
<label for="ui">{{ctx.Locale.Tr "settings.ui"}}</label>
<div class="ui selection dropdown" id="ui">
<div class="ui search selection dropdown" id="ui">
<input name="theme" type="hidden" value="{{.SignedUser.Theme}}">
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
<div class="text">
@ -52,7 +52,7 @@
</div>
{{.CsrfTokenHtml}}
<div class="field">
<div class="ui language selection dropdown" id="language">
<div class="ui search language selection dropdown" id="language">
<input name="language" type="hidden" value="{{.SignedUser.Language}}">
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
<div class="text">{{range .AllLangs}}{{if eq $.SignedUser.Language .Lang}}{{.Name}}{{end}}{{end}}</div>