Skip to content

Commit c4a2695

Browse files
committed
Add automatic theme discovery
Introduce a new special value '*' for ui.THEMES which will automatically discover installed themes in bindata, custom folder and regular files. Ref: #11118
1 parent 6891b90 commit c4a2695

File tree

12 files changed

+111
-15
lines changed

12 files changed

+111
-15
lines changed

custom/conf/app.example.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ MAX_DISPLAY_FILE_SIZE = 8388608
178178
SHOW_USER_EMAIL = true
179179
; Set the default theme for the Gitea install
180180
DEFAULT_THEME = gitea
181-
; All available themes. Allow users select personalized themes regardless of the value of `DEFAULT_THEME`.
182-
THEMES = gitea,arc-green
181+
; Comma-separated list of enabled themes. If set to '*' will automatically discover installed themes
182+
THEMES = *
183183
;All available reactions users can choose on issues/prs and comments.
184184
;Values can be emoji alias (:smile:) or a unicode emoji.
185185
;For custom reactions, add a tightly cropped square image to public/emoji/img/reaction_name.png

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,8 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
128128
- `MEMBERS_PAGING_NUM`: **20**: Number of members that are shown in organization members.
129129
- `FEED_MAX_COMMIT_NUM`: **5**: Number of maximum commits shown in one activity feed.
130130
- `GRAPH_MAX_COMMIT_NUM`: **100**: Number of maximum commits shown in the commit graph.
131-
- `DEFAULT_THEME`: **gitea**: \[gitea, arc-green\]: Set the default theme for the Gitea install.
132-
- `THEMES`: **gitea,arc-green**: All available themes. Allow users select personalized themes
133-
regardless of the value of `DEFAULT_THEME`.
131+
- `DEFAULT_THEME`: **gitea**: Set the default theme for the Gitea install.
132+
- `THEMES`: **\***: Comma-separated list of enabled themes. If set to `*` will automatically discover installed themes.
134133
- `REACTIONS`: All available reactions users can choose on issues/prs and comments
135134
Values can be emoji alias (:smile:) or a unicode emoji.
136135
For custom reactions, add a tightly cropped square image to public/emoji/img/reaction_name.png

docs/content/doc/advanced/customizing-gitea.en-us.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,4 +333,4 @@ A full list of supported emoji's is at [emoji list](https://gitea.com/gitea/gite
333333
## Customizing the look of Gitea
334334

335335
As of version 1.6.0 Gitea has built-in themes. The two built-in themes are, the default theme `gitea`, and a dark theme `arc-green`. To change the look of your Gitea install change the value of `DEFAULT_THEME` in the [ui](https://docs.gitea.io/en-us/config-cheat-sheet/#ui-ui) section of `app.ini` to another one of the available options.
336-
As of version 1.8.0 Gitea also has per-user themes. The list of themes a user can choose from can be configured with the `THEMES` value in the [ui](https://docs.gitea.io/en-us/config-cheat-sheet/#ui-ui) section of `app.ini` (defaults to `gitea` and `arc-green`, light and dark respectively)
336+
As of version 1.8.0 Gitea also has per-user themes. The list of themes a user can choose from can be configured with the `THEMES` value in the [ui](https://docs.gitea.io/en-us/config-cheat-sheet/#ui-ui) section of `app.ini` (defaults to automatic discovery which is fine unless you want to disable certain themes)

docs/content/doc/help/faq.en-us.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,7 @@ Use [Fail2Ban]({{ relref "doc/usage/fail2ban-setup.md" >}}) to monitor and stop
167167
Gitea supports two official themes right now, `gitea` and `arc-green` (`light` and `dark` respectively)
168168
To add your own theme, currently the only way is to provide a complete theme (not just color overrides)
169169

170-
As an example, let's say our theme is `arc-blue` (this is a real theme, and can be found [in this issue](https://github.com/go-gitea/gitea/issues/6011))
171-
Name the `.css` file `theme-arc-blue.css` and add it to your custom folder in `custom/pulic/css`
172-
Allow users to use it by adding `arc-blue` to the list of `THEMES` in your `app.ini`
170+
As an example, let's say our theme is `arc-blue` (this is a real theme, and can be found [in this issue](https://github.com/go-gitea/gitea/issues/6011)). Name the `.css` file `theme-arc-blue.css` and add it to your custom folder in `custom/public/css`. If `THEMES` is set to `*` (the default), it will be available after Gitea is restarted.
173171

174172
## SSHD vs built-in SSH
175173
SSHD is the built-in SSH server on most Unix systems.

modules/auth/user_form.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111

1212
"code.gitea.io/gitea/modules/setting"
13+
"code.gitea.io/gitea/modules/themes"
1314

1415
"gitea.com/macaron/binding"
1516
"gitea.com/macaron/macaron"
@@ -251,11 +252,11 @@ func (f *UpdateThemeForm) Validate(ctx *macaron.Context, errs binding.Errors) bi
251252
return validate(errs, ctx.Data, f, ctx.Locale)
252253
}
253254

254-
// IsThemeExists checks if the theme is a theme available in the config.
255+
// IsThemeExists checks if a theme is available
255256
func (f UpdateThemeForm) IsThemeExists() bool {
256257
var exists bool
257258

258-
for _, v := range setting.UI.Themes {
259+
for _, v := range themes.Themes {
259260
if strings.EqualFold(v, f.Theme) {
260261
exists = true
261262
break

modules/setting/setting.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ var (
213213
ThemeColorMetaTag: `#6cc644`,
214214
MaxDisplayFileSize: 8388608,
215215
DefaultTheme: `gitea`,
216-
Themes: []string{`gitea`, `arc-green`},
216+
Themes: []string{`*`},
217217
Reactions: []string{`+1`, `-1`, `laugh`, `hooray`, `confused`, `heart`, `rocket`, `eyes`},
218218
Notification: struct {
219219
MinTimeout time.Duration

modules/themes/discover_bindata.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build bindata
6+
7+
package themes
8+
9+
import (
10+
"path"
11+
"path/filepath"
12+
13+
"code.gitea.io/gitea/modules/public"
14+
"code.gitea.io/gitea/modules/setting"
15+
16+
"github.com/gobwas/glob"
17+
)
18+
19+
// Discover locates installed themes
20+
func Discover() []string {
21+
themes := []string{"gitea"}
22+
23+
glob := glob.MustCompile("css/theme-*.css")
24+
for _, file := range public.AssetNames() {
25+
if glob.Match(file) {
26+
filename := path.Base(file)
27+
themes = append(themes, filename[6:len(filename)-4]) // chop off "theme-" and ".css"
28+
}
29+
}
30+
31+
customFiles, _ := filepath.Glob(path.Join(setting.CustomPath, "public", "css", "theme-*.css"))
32+
for _, file := range customFiles {
33+
filename := path.Base(file)
34+
themes = append(themes, filename[6:len(filename)-4])
35+
}
36+
37+
return themes
38+
}

modules/themes/discover_nobindata.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build !bindata
6+
7+
package themes
8+
9+
import (
10+
"path"
11+
"path/filepath"
12+
13+
"code.gitea.io/gitea/modules/setting"
14+
)
15+
16+
// Discover locates installed themes
17+
func Discover() []string {
18+
themes := []string{"gitea"}
19+
20+
staticFiles, _ := filepath.Glob(path.Join(setting.StaticRootPath, "public", "css", "theme-*.css"))
21+
for _, file := range staticFiles {
22+
filename := path.Base(file)
23+
themes = append(themes, filename[6:len(filename)-4]) // chop off "theme-" and ".css"
24+
}
25+
26+
customFiles, _ := filepath.Glob(path.Join(setting.CustomPath, "public", "css", "theme-*.css"))
27+
for _, file := range customFiles {
28+
filename := path.Base(file)
29+
themes = append(themes, filename[6:len(filename)-4])
30+
}
31+
32+
return themes
33+
}

modules/themes/themes.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package themes
6+
7+
import (
8+
"reflect"
9+
10+
"code.gitea.io/gitea/modules/setting"
11+
)
12+
13+
// Themes lists available themes
14+
var Themes []string
15+
16+
// Init initializes theme-related variables
17+
func Init() {
18+
if reflect.DeepEqual(setting.UI.Themes, []string{"*"}) {
19+
Themes = Discover()
20+
} else {
21+
Themes = setting.UI.Themes
22+
}
23+
}

routers/init.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"code.gitea.io/gitea/modules/setting"
3030
"code.gitea.io/gitea/modules/ssh"
3131
"code.gitea.io/gitea/modules/task"
32+
"code.gitea.io/gitea/modules/themes"
3233
"code.gitea.io/gitea/modules/webhook"
3334
"code.gitea.io/gitea/services/mailer"
3435
mirror_service "code.gitea.io/gitea/services/mirror"
@@ -124,6 +125,8 @@ func GlobalInit(ctx context.Context) {
124125
// Setup i18n
125126
InitLocales()
126127

128+
themes.Init()
129+
127130
log.Info("%s", unknwoni18n.Tr("en-US", "admin.dashboard.delete_repo_archives"))
128131

129132
NewServices()

routers/routes/routes.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"code.gitea.io/gitea/modules/public"
2323
"code.gitea.io/gitea/modules/setting"
2424
"code.gitea.io/gitea/modules/templates"
25+
"code.gitea.io/gitea/modules/themes"
2526
"code.gitea.io/gitea/modules/validation"
2627
"code.gitea.io/gitea/routers"
2728
"code.gitea.io/gitea/routers/admin"
@@ -403,7 +404,7 @@ func RegisterRoutes(m *macaron.Macaron) {
403404
m.Get("/repos", userSetting.Repos)
404405
}, reqSignIn, func(ctx *context.Context) {
405406
ctx.Data["PageIsUserSettings"] = true
406-
ctx.Data["AllThemes"] = setting.UI.Themes
407+
ctx.Data["Themes"] = themes.Themes
407408
})
408409

409410
m.Group("/user", func() {

templates/user/settings/account.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,13 @@
146146
<input name="theme" type="hidden" value="{{.SignedUser.Theme}}">
147147
<i class="dropdown icon"></i>
148148
<div class="text">
149-
{{range $i,$a := .AllThemes}}
149+
{{range $i,$a := .Themes}}
150150
{{if eq $.SignedUser.Theme $a}}{{$a}}{{end}}
151151
{{end}}
152152
</div>
153153

154154
<div class="menu">
155-
{{range $i,$a := .AllThemes}}
155+
{{range $i,$a := .Themes}}
156156
<div class="item{{if eq $.SignedUser.Theme $a}} active selected{{end}}" data-value="{{$a}}">
157157
{{$a}}
158158
</div>

0 commit comments

Comments
 (0)