Skip to content

Commit 6b5563c

Browse files
authored
Support selecting theme on the footer (go-gitea#35741)
Fixes: go-gitea#27576
1 parent cddff73 commit 6b5563c

33 files changed

+254
-59
lines changed

modules/setting/session.go

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

1111
"code.gitea.io/gitea/modules/json"
1212
"code.gitea.io/gitea/modules/log"
13+
"code.gitea.io/gitea/modules/util"
1314
)
1415

1516
// SessionConfig defines Session settings
@@ -49,10 +50,8 @@ func loadSessionFrom(rootCfg ConfigProvider) {
4950
checkOverlappedPath("[session].PROVIDER_CONFIG", SessionConfig.ProviderConfig)
5051
}
5152
SessionConfig.CookieName = sec.Key("COOKIE_NAME").MustString("i_like_gitea")
52-
SessionConfig.CookiePath = AppSubURL
53-
if SessionConfig.CookiePath == "" {
54-
SessionConfig.CookiePath = "/"
55-
}
53+
// HINT: INSTALL-PAGE-COOKIE-INIT: the cookie system is not properly initialized on the Install page, so there is no CookiePath
54+
SessionConfig.CookiePath = util.IfZero(AppSubURL, "/")
5655
SessionConfig.Secure = sec.Key("COOKIE_SECURE").MustBool(strings.HasPrefix(strings.ToLower(AppURL), "https://"))
5756
SessionConfig.Gclifetime = sec.Key("GC_INTERVAL_TIME").MustInt64(86400)
5857
SessionConfig.Maxlifetime = sec.Key("SESSION_LIFE_TIME").MustInt64(86400)

modules/svg/svg.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ func MockIcon(icon string) func() {
5858

5959
// RenderHTML renders icons - arguments icon name (string), size (int), class (string)
6060
func RenderHTML(icon string, others ...any) template.HTML {
61+
if icon == "" {
62+
return ""
63+
}
6164
size, class := gitea_html.ParseSizeAndClass(defaultSize, "", others...)
6265
if svgStr, ok := svgIcons[icon]; ok {
6366
// the code is somewhat hacky, but it just works, because the SVG contents are all normalized

modules/templates/helper.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"strings"
1313
"time"
1414

15-
user_model "code.gitea.io/gitea/models/user"
1615
"code.gitea.io/gitea/modules/base"
1716
"code.gitea.io/gitea/modules/htmlutil"
1817
"code.gitea.io/gitea/modules/markup"
@@ -21,7 +20,6 @@ import (
2120
"code.gitea.io/gitea/modules/templates/eval"
2221
"code.gitea.io/gitea/modules/util"
2322
"code.gitea.io/gitea/services/gitdiff"
24-
"code.gitea.io/gitea/services/webtheme"
2523
)
2624

2725
// NewFuncMap returns functions for injecting to templates
@@ -130,7 +128,6 @@ func NewFuncMap() template.FuncMap {
130128
"DisableWebhooks": func() bool {
131129
return setting.DisableWebhooks
132130
},
133-
"UserThemeName": userThemeName,
134131
"NotificationSettings": func() map[string]any {
135132
return map[string]any{
136133
"MinTimeout": int(setting.UI.Notification.MinTimeout / time.Millisecond),
@@ -217,16 +214,6 @@ func evalTokens(tokens ...any) (any, error) {
217214
return n.Value, err
218215
}
219216

220-
func userThemeName(user *user_model.User) string {
221-
if user == nil || user.Theme == "" {
222-
return setting.UI.DefaultTheme
223-
}
224-
if webtheme.IsThemeAvailable(user.Theme) {
225-
return user.Theme
226-
}
227-
return setting.UI.DefaultTheme
228-
}
229-
230217
func isQueryParamEmpty(v any) bool {
231218
return v == nil || v == false || v == 0 || v == int64(0) || v == ""
232219
}

modules/templates/util_render.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ import (
2323
"code.gitea.io/gitea/modules/markup/markdown"
2424
"code.gitea.io/gitea/modules/reqctx"
2525
"code.gitea.io/gitea/modules/setting"
26+
"code.gitea.io/gitea/modules/svg"
2627
"code.gitea.io/gitea/modules/translation"
2728
"code.gitea.io/gitea/modules/util"
29+
"code.gitea.io/gitea/services/webtheme"
2830
)
2931

3032
type RenderUtils struct {
@@ -259,3 +261,18 @@ func (ut *RenderUtils) RenderLabels(labels []*issues_model.Label, repoLink strin
259261
htmlCode += "</span>"
260262
return template.HTML(htmlCode)
261263
}
264+
265+
func (ut *RenderUtils) RenderThemeItem(info *webtheme.ThemeMetaInfo, iconSize int) template.HTML {
266+
svgName := "octicon-paintbrush"
267+
switch info.ColorScheme {
268+
case "dark":
269+
svgName = "octicon-moon"
270+
case "light":
271+
svgName = "octicon-sun"
272+
case "auto":
273+
svgName = "gitea-eclipse"
274+
}
275+
icon := svg.RenderHTML(svgName, iconSize)
276+
extraIcon := svg.RenderHTML(info.GetExtraIconName(), iconSize)
277+
return htmlutil.HTMLFormat(`<div class="theme-menu-item" data-tooltip-content="%s">%s %s %s</div>`, info.GetDescription(), icon, info.DisplayName, extraIcon)
278+
}

modules/web/middleware/cookie.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"code.gitea.io/gitea/modules/session"
1313
"code.gitea.io/gitea/modules/setting"
14+
"code.gitea.io/gitea/modules/util"
1415
)
1516

1617
// SetRedirectToCookie convenience function to set the RedirectTo cookie consistently
@@ -39,11 +40,13 @@ func SetSiteCookie(resp http.ResponseWriter, name, value string, maxAge int) {
3940
// These are more specific than cookies without a trailing /, so
4041
// we need to delete these if they exist.
4142
deleteLegacySiteCookie(resp, name)
43+
44+
// HINT: INSTALL-PAGE-COOKIE-INIT: the cookie system is not properly initialized on the Install page, so there is no CookiePath
4245
cookie := &http.Cookie{
4346
Name: name,
4447
Value: url.QueryEscape(value),
4548
MaxAge: maxAge,
46-
Path: setting.SessionConfig.CookiePath,
49+
Path: util.IfZero(setting.SessionConfig.CookiePath, "/"),
4750
Domain: setting.SessionConfig.Domain,
4851
Secure: setting.SessionConfig.Secure,
4952
HttpOnly: true,

public/assets/img/svg/gitea-colorblind-redgreen.svg

Lines changed: 1 addition & 0 deletions
Loading

public/assets/img/svg/gitea-eclipse.svg

Lines changed: 1 addition & 0 deletions
Loading

routers/common/errpage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func RenderPanicErrorPage(w http.ResponseWriter, req *http.Request, err any) {
3535
httpcache.SetCacheControlInHeader(w.Header(), &httpcache.CacheControlOptions{NoTransform: true})
3636
w.Header().Set(`X-Frame-Options`, setting.CORSConfig.XFrameOptions)
3737

38-
tmplCtx := context.TemplateContext{}
38+
tmplCtx := context.NewTemplateContext(req.Context(), req)
3939
tmplCtx["Locale"] = middleware.Locale(w, req)
4040
ctxData := middleware.GetContextData(req.Context())
4141

routers/common/qos.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func renderServiceUnavailable(w http.ResponseWriter, req *http.Request) {
133133
return
134134
}
135135

136-
tmplCtx := giteacontext.TemplateContext{}
136+
tmplCtx := giteacontext.NewTemplateContext(req.Context(), req)
137137
tmplCtx["Locale"] = middleware.Locale(w, req)
138138
ctxData := middleware.GetContextData(req.Context())
139139
err := templates.HTMLRenderer().HTML(w, http.StatusServiceUnavailable, tplStatus503, ctxData, tmplCtx)

routers/install/routes.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"code.gitea.io/gitea/modules/web"
1515
"code.gitea.io/gitea/routers/common"
1616
"code.gitea.io/gitea/routers/web/healthcheck"
17+
"code.gitea.io/gitea/routers/web/misc"
1718
"code.gitea.io/gitea/services/forms"
1819
)
1920

@@ -32,7 +33,11 @@ func Routes() *web.Router {
3233
r.Get("/", Install) // it must be on the root, because the "install.js" use the window.location to replace the "localhost" AppURL
3334
r.Post("/", web.Bind(forms.InstallForm{}), SubmitInstall)
3435
r.Get("/post-install", InstallDone)
36+
37+
r.Get("/-/web-theme/list", misc.WebThemeList)
38+
r.Post("/-/web-theme/apply", misc.WebThemeApply)
3539
r.Get("/api/healthz", healthcheck.Check)
40+
3641
r.NotFound(installNotFound)
3742

3843
base.Mount("", r)

0 commit comments

Comments
 (0)