Skip to content

Commit 36347f3

Browse files
committed
better theme handling
1 parent 52dcda5 commit 36347f3

File tree

6 files changed

+28
-31
lines changed

6 files changed

+28
-31
lines changed

modules/setting/config_provider.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,22 +318,22 @@ func mustMapSetting(rootCfg ConfigProvider, sectionName string, setting any) {
318318
// StartupProblems contains the messages for various startup problems, including: setting option, file/folder, etc
319319
var StartupProblems []string
320320

321-
func logStartupProblem(skip int, level log.Level, format string, args ...any) {
321+
func LogStartupProblem(skip int, level log.Level, format string, args ...any) {
322322
msg := fmt.Sprintf(format, args...)
323323
log.Log(skip+1, level, "%s", msg)
324324
StartupProblems = append(StartupProblems, msg)
325325
}
326326

327327
func deprecatedSetting(rootCfg ConfigProvider, oldSection, oldKey, newSection, newKey, version string) {
328328
if rootCfg.Section(oldSection).HasKey(oldKey) {
329-
logStartupProblem(1, log.ERROR, "Deprecation: config option `[%s].%s` presents, please use `[%s].%s` instead because this fallback will be/has been removed in %s", oldSection, oldKey, newSection, newKey, version)
329+
LogStartupProblem(1, log.ERROR, "Deprecation: config option `[%s].%s` presents, please use `[%s].%s` instead because this fallback will be/has been removed in %s", oldSection, oldKey, newSection, newKey, version)
330330
}
331331
}
332332

333333
// deprecatedSettingDB add a hint that the configuration has been moved to database but still kept in app.ini
334334
func deprecatedSettingDB(rootCfg ConfigProvider, oldSection, oldKey string) {
335335
if rootCfg.Section(oldSection).HasKey(oldKey) {
336-
logStartupProblem(1, log.ERROR, "Deprecation: config option `[%s].%s` presents but it won't take effect because it has been moved to admin panel -> config setting", oldSection, oldKey)
336+
LogStartupProblem(1, log.ERROR, "Deprecation: config option `[%s].%s` presents but it won't take effect because it has been moved to admin panel -> config setting", oldSection, oldKey)
337337
}
338338
}
339339

modules/setting/oauth2.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func GetGeneralTokenSigningSecret() []byte {
174174
}
175175
if generalSigningSecret.CompareAndSwap(old, &jwtSecret) {
176176
// FIXME: in main branch, the signing token should be refactored (eg: one unique for LFS/OAuth2/etc ...)
177-
logStartupProblem(1, log.WARN, "OAuth2 is not enabled, unable to use a persistent signing secret, a new one is generated, which is not persistent between restarts and cluster nodes")
177+
LogStartupProblem(1, log.WARN, "OAuth2 is not enabled, unable to use a persistent signing secret, a new one is generated, which is not persistent between restarts and cluster nodes")
178178
return jwtSecret
179179
}
180180
return *generalSigningSecret.Load()

modules/setting/setting.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ var configuredPaths = make(map[string]string)
235235
func checkOverlappedPath(name, path string) {
236236
// TODO: some paths shouldn't overlap (storage.xxx.path), while some could (data path is the base path for storage path)
237237
if targetName, ok := configuredPaths[path]; ok && targetName != name {
238-
logStartupProblem(1, log.ERROR, "Configured path %q is used by %q and %q at the same time. The paths must be unique to prevent data loss.", path, targetName, name)
238+
LogStartupProblem(1, log.ERROR, "Configured path %q is used by %q and %q at the same time. The paths must be unique to prevent data loss.", path, targetName, name)
239239
}
240240
configuredPaths[path] = name
241241
}

routers/web/user/setting/profile.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,13 @@ func Repos(ctx *context.Context) {
319319
func Appearance(ctx *context.Context) {
320320
ctx.Data["Title"] = ctx.Tr("settings.appearance")
321321
ctx.Data["PageIsSettingsAppearance"] = true
322-
ctx.Data["AllThemes"] = webtheme.GetAvailableThemes()
322+
323+
allThemes := webtheme.GetAvailableThemes()
324+
if webtheme.IsThemeAvailable(setting.UI.DefaultTheme) {
325+
allThemes = util.SliceRemoveAll(allThemes, setting.UI.DefaultTheme)
326+
allThemes = append([]string{setting.UI.DefaultTheme}, allThemes...) // move the default theme to the top
327+
}
328+
ctx.Data["AllThemes"] = allThemes
323329

324330
var hiddenCommentTypes *big.Int
325331
val, err := user_model.GetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyHiddenCommentTypes)

services/webtheme/webtheme.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package webtheme
55

66
import (
7+
"sort"
78
"strings"
89
"sync"
910

@@ -23,6 +24,9 @@ func initThemes() {
2324
availableThemes = nil
2425
defer func() {
2526
availableThemesSet = container.SetOf(availableThemes...)
27+
if !availableThemesSet.Contains(setting.UI.DefaultTheme) {
28+
setting.LogStartupProblem(1, log.ERROR, "Default theme %q is not available, please correct the '[ui].DEFAULT_THEME' setting in the config file", setting.UI.DefaultTheme)
29+
}
2630
}()
2731
cssFiles, err := public.AssetFS().ListFiles("/assets/css")
2832
if err != nil {
@@ -52,8 +56,9 @@ func initThemes() {
5256
} else {
5357
availableThemes = foundThemes
5458
}
59+
sort.Strings(availableThemes)
5560
if len(availableThemes) == 0 {
56-
log.Error("No theme candidate, but gitea requires there should be at least one usable theme")
61+
setting.LogStartupProblem(1, log.ERROR, "No theme candidate in asset files, but Gitea requires there should be at least one usable theme")
5762
availableThemes = []string{setting.UI.DefaultTheme}
5863
}
5964
}

templates/user/settings/appearance.tmpl

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,20 @@
1010
<div class="item">
1111
{{ctx.Locale.Tr "settings.theme_desc"}}
1212
</div>
13-
14-
<form class="ui form" action="{{.Link}}/theme" method="post">
15-
{{.CsrfTokenHtml}}
13+
<form class="ui form" action="{{.Link}}/theme" method="post">
14+
{{.CsrfTokenHtml}}
1615
<div class="field">
1716
<label for="ui">{{ctx.Locale.Tr "settings.ui"}}</label>
18-
<div class="ui selection dropdown" id="ui">
19-
<input name="theme" type="hidden" value="{{.SignedUser.Theme}}">
20-
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
21-
<div class="text">
22-
{{range $i,$a := .AllThemes}}
23-
{{if eq $.SignedUser.Theme $a}}{{$a}}{{end}}
24-
{{end}}
25-
</div>
26-
27-
<div class="menu">
28-
{{range $i,$a := .AllThemes}}
29-
<div class="item{{if eq $.SignedUser.Theme $a}} active selected{{end}}" data-value="{{$a}}">
30-
{{$a}}
31-
</div>
17+
<select name="theme" class="ui dropdown">
18+
{{range $theme := .AllThemes}}
19+
<option value="{{$theme}}" {{Iif (eq $.SignedUser.Theme $theme) "selected"}}>{{$theme}}</option>
3220
{{end}}
33-
</div>
34-
</div>
21+
</select>
3522
</div>
36-
37-
<div class="field">
38-
<button class="ui primary button">{{ctx.Locale.Tr "settings.update_theme"}}</button>
39-
</div>
40-
</form>
23+
<div class="field">
24+
<button class="ui primary button">{{ctx.Locale.Tr "settings.update_theme"}}</button>
25+
</div>
26+
</form>
4127
</div>
4228
</div>
4329

0 commit comments

Comments
 (0)