|
| 1 | +// Copyright 2025 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package misc |
| 5 | + |
| 6 | +import ( |
| 7 | + "net/http" |
| 8 | + |
| 9 | + "code.gitea.io/gitea/modules/optional" |
| 10 | + "code.gitea.io/gitea/modules/templates" |
| 11 | + "code.gitea.io/gitea/modules/util" |
| 12 | + "code.gitea.io/gitea/modules/web/middleware" |
| 13 | + "code.gitea.io/gitea/services/context" |
| 14 | + user_service "code.gitea.io/gitea/services/user" |
| 15 | + "code.gitea.io/gitea/services/webtheme" |
| 16 | +) |
| 17 | + |
| 18 | +func WebThemeList(ctx *context.Context) { |
| 19 | + curWebTheme := ctx.TemplateContext.CurrentWebTheme() |
| 20 | + renderUtils := templates.NewRenderUtils(ctx) |
| 21 | + allThemes := webtheme.GetAvailableThemes() |
| 22 | + |
| 23 | + var results []map[string]any |
| 24 | + for _, theme := range allThemes { |
| 25 | + results = append(results, map[string]any{ |
| 26 | + "name": renderUtils.RenderThemeItem(theme, 14), |
| 27 | + "value": theme.InternalName, |
| 28 | + "class": "item js-aria-clickable" + util.Iif(theme.InternalName == curWebTheme.InternalName, " selected", ""), |
| 29 | + }) |
| 30 | + } |
| 31 | + ctx.JSON(http.StatusOK, map[string]any{"results": results}) |
| 32 | +} |
| 33 | + |
| 34 | +func WebThemeApply(ctx *context.Context) { |
| 35 | + themeName := ctx.FormString("theme") |
| 36 | + middleware.SetSiteCookie(ctx.Resp, "gitea_theme", themeName, 0) |
| 37 | + if ctx.Doer != nil { |
| 38 | + opts := &user_service.UpdateOptions{Theme: optional.Some(themeName)} |
| 39 | + _ = user_service.UpdateUser(ctx, ctx.Doer, opts) |
| 40 | + } |
| 41 | +} |
0 commit comments