mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
Initial support for colorblindness-friendly themes (#30625)
Initial support for #25680 This PR only adds some simple styles from GitHub, it is big enough and it focuses on adding the necessary framework-level supports. More styles could be fine-tuned later.
This commit is contained in:
@@ -230,6 +230,7 @@ func Contexter() func(next http.Handler) http.Handler {
|
||||
|
||||
// HasError returns true if error occurs in form validation.
|
||||
// Attention: this function changes ctx.Data and ctx.Flash
|
||||
// If HasError is called, then before Redirect, the error message should be stored by ctx.Flash.Error(ctx.GetErrMsg()) again.
|
||||
func (ctx *Context) HasError() bool {
|
||||
hasErr, ok := ctx.Data["HasError"]
|
||||
if !ok {
|
||||
|
@@ -11,7 +11,6 @@ import (
|
||||
|
||||
auth_model "code.gitea.io/gitea/models/auth"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/web/middleware"
|
||||
"code.gitea.io/gitea/services/context"
|
||||
@@ -273,7 +272,7 @@ func (f *AddEmailForm) Validate(req *http.Request, errs binding.Errors) binding.
|
||||
|
||||
// UpdateThemeForm form for updating a users' theme
|
||||
type UpdateThemeForm struct {
|
||||
Theme string `binding:"Required;MaxSize(30)"`
|
||||
Theme string `binding:"Required;MaxSize(255)"`
|
||||
}
|
||||
|
||||
// Validate validates the field
|
||||
@@ -282,20 +281,6 @@ func (f *UpdateThemeForm) Validate(req *http.Request, errs binding.Errors) bindi
|
||||
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
|
||||
}
|
||||
|
||||
// IsThemeExists checks if the theme is a theme available in the config.
|
||||
func (f UpdateThemeForm) IsThemeExists() bool {
|
||||
var exists bool
|
||||
|
||||
for _, v := range setting.UI.Themes {
|
||||
if strings.EqualFold(v, f.Theme) {
|
||||
exists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return exists
|
||||
}
|
||||
|
||||
// ChangePasswordForm form for changing password
|
||||
type ChangePasswordForm struct {
|
||||
OldPassword string `form:"old_password" binding:"MaxSize(255)"`
|
||||
|
74
services/webtheme/webtheme.go
Normal file
74
services/webtheme/webtheme.go
Normal file
@@ -0,0 +1,74 @@
|
||||
// Copyright 2024 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package webtheme
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"code.gitea.io/gitea/modules/container"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/public"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
)
|
||||
|
||||
var (
|
||||
availableThemes []string
|
||||
availableThemesSet container.Set[string]
|
||||
themeOnce sync.Once
|
||||
)
|
||||
|
||||
func initThemes() {
|
||||
availableThemes = nil
|
||||
defer func() {
|
||||
availableThemesSet = container.SetOf(availableThemes...)
|
||||
if !availableThemesSet.Contains(setting.UI.DefaultTheme) {
|
||||
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)
|
||||
}
|
||||
}()
|
||||
cssFiles, err := public.AssetFS().ListFiles("/assets/css")
|
||||
if err != nil {
|
||||
log.Error("Failed to list themes: %v", err)
|
||||
availableThemes = []string{setting.UI.DefaultTheme}
|
||||
return
|
||||
}
|
||||
var foundThemes []string
|
||||
for _, name := range cssFiles {
|
||||
name, ok := strings.CutPrefix(name, "theme-")
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
name, ok = strings.CutSuffix(name, ".css")
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
foundThemes = append(foundThemes, name)
|
||||
}
|
||||
if len(setting.UI.Themes) > 0 {
|
||||
allowedThemes := container.SetOf(setting.UI.Themes...)
|
||||
for _, theme := range foundThemes {
|
||||
if allowedThemes.Contains(theme) {
|
||||
availableThemes = append(availableThemes, theme)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
availableThemes = foundThemes
|
||||
}
|
||||
sort.Strings(availableThemes)
|
||||
if len(availableThemes) == 0 {
|
||||
setting.LogStartupProblem(1, log.ERROR, "No theme candidate in asset files, but Gitea requires there should be at least one usable theme")
|
||||
availableThemes = []string{setting.UI.DefaultTheme}
|
||||
}
|
||||
}
|
||||
|
||||
func GetAvailableThemes() []string {
|
||||
themeOnce.Do(initThemes)
|
||||
return availableThemes
|
||||
}
|
||||
|
||||
func IsThemeAvailable(name string) bool {
|
||||
themeOnce.Do(initThemes)
|
||||
return availableThemesSet.Contains(name)
|
||||
}
|
Reference in New Issue
Block a user