Skip to content

Add theme selector button to footer #27576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions modules/templates/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ func NewFuncMap() template.FuncMap {
}
return user.Theme
},
"ThemeIcon": func(theme string) string {
if theme == "gitea-dark" {
return "octicon-moon"
} else if theme == "gitea-light" {
return "octicon-sun"
} else if theme == "gitea-auto" {
return "gitea-eclipse"
} else {
return "octicon-paintbrush"
}
Comment on lines +142 to +150
Copy link
Member

@hiifong hiifong Oct 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if theme == "gitea-dark" {
return "octicon-moon"
} else if theme == "gitea-light" {
return "octicon-sun"
} else if theme == "gitea-auto" {
return "gitea-eclipse"
} else {
return "octicon-paintbrush"
}
switch theme {
case "gitea-dark":
return "octicon-moon"
case "gitea-light":
return "octicon-sun"
case "gitea-auto":
return "gitea-eclips"
default:
return "octicon-paintbrush"
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the switch way of writing, but gitea doesn't seem to have a normalized way of writing this, so it's the same with either one of them.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I will go with the JSON config, making this switch unnecessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-> Improve theme display #30671

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that seems nice, waiting for that PR here.

},
"Themes": func() []string {
return setting.UI.Themes
},
"NotificationSettings": func() map[string]any {
return map[string]any{
"MinTimeout": int(setting.UI.Notification.MinTimeout / time.Millisecond),
Expand Down
1 change: 1 addition & 0 deletions public/assets/img/svg/gitea-eclipse.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ func registerRoutes(m *web.Route) {
})
addWebhookEditRoutes()
}, webhooksEnabled)
}, reqSignIn, ctxDataSet("PageIsUserSettings", true, "AllThemes", setting.UI.Themes, "EnablePackages", setting.Packages.Enabled))
}, reqSignIn, ctxDataSet("PageIsUserSettings", true, "EnablePackages", setting.Packages.Enabled))

m.Group("/user", func() {
m.Get("/activate", auth.Activate)
Expand Down
11 changes: 11 additions & 0 deletions templates/base/footer_content.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@
{{end}}
</div>
<div class="right-links" role="group" aria-label="{{ctx.Locale.Tr "aria.footer.links"}}">
<div class="ui dropdown upward">
{{$currentTheme := ThemeName .SignedUser}}
<span class="flex-text-inline">{{svg (ThemeIcon $currentTheme) 14}} {{$currentTheme}}</span>
<div class="menu theme-menu">
{{range Themes}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we do that, I think it might also be a good idea to sort the themes alphabetically beforehand (once, on Gitea startup).
Otherwise, we might confuse users if the order changes because the admin changed the value of the setting.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sorting is a good idea, can add. Not sure if really warranted to store in-memory because sorting a small array is fast, but can do.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, we store the array already, so I don't see why it would consume extra memory?

Copy link
Member Author

@silverwind silverwind Oct 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, we can just sort setting.UI.Themes after config parsing, so no additional memory needed.

<a data-value="{{.}}" class="item {{if eq $currentTheme .}}active selected{{end}}">
{{svg (ThemeIcon .) 14}} {{.}}
</a>
{{end}}
</div>
</div>
<div class="ui dropdown upward language">
<span class="flex-text-inline">{{svg "octicon-globe" 14}} {{ctx.Locale.LangName}}</span>
<div class="menu language-menu">
Expand Down
16 changes: 6 additions & 10 deletions templates/user/settings/appearance.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,17 @@

<form class="ui form" action="{{.Link}}/theme" method="post">
{{.CsrfTokenHtml}}
{{$currentTheme := ThemeName .SignedUser}}
<div class="field">
<label for="ui">{{ctx.Locale.Tr "settings.ui"}}</label>
<div class="ui selection dropdown" id="ui">
<input name="theme" type="hidden" value="{{.SignedUser.Theme}}">
<input name="theme" type="hidden" value="{{$currentTheme}}">
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
<div class="text">
{{range $i,$a := .AllThemes}}
{{if eq $.SignedUser.Theme $a}}{{$a}}{{end}}
{{end}}
</div>

<div class="text">{{$currentTheme}}</div>
<div class="menu">
{{range $i,$a := .AllThemes}}
<div class="item{{if eq $.SignedUser.Theme $a}} active selected{{end}}" data-value="{{$a}}">
{{$a}}
{{range Themes}}
<div class="item{{if eq $currentTheme .}} active selected{{end}}" data-value="{{.}}">
{{.}}
</div>
{{end}}
</div>
Expand Down
8 changes: 4 additions & 4 deletions web_src/css/home.css
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@
justify-content: center;
}

.page-footer .right-links > a {
.page-footer .right-links > * + * {
border-left: 1px solid var(--color-secondary-dark-1);
padding-left: 8px;
margin-left: 5px;
padding-left: 7px;
margin-left: 7px;
}

.page-footer .ui.dropdown.language .menu {
.page-footer .ui.dropdown .menu {
max-height: min(500px, calc(100vh - 60px));
overflow-y: auto;
margin-bottom: 10px;
Expand Down
19 changes: 11 additions & 8 deletions web_src/js/features/common-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {htmlEscape} from 'escape-goat';
import {showTemporaryTooltip} from '../modules/tippy.js';
import {confirmModal} from './comp/ConfirmModal.js';
import {showErrorToast} from '../modules/toast.js';
import {request} from '../modules/fetch.js';
import {request, POST} from '../modules/fetch.js';

const {appUrl, appSubUrl, csrfToken, i18n} = window.config;

Expand All @@ -35,15 +35,18 @@ export function initHeadNavbarContentToggle() {
});
}

export function initFootLanguageMenu() {
function linkLanguageAction() {
const $this = $(this);
$.get($this.data('url')).always(() => {
export function initFooterMenus() {
$('.language-menu a[lang]').on('click', function() {
$.get($(this).data('url')).always(() => {
window.location.reload();
});
}

$('.language-menu a[lang]').on('click', linkLanguageAction);
});
$('.theme-menu .item').on('click', async (e) => {
const res = await POST(`${appSubUrl}/user/settings/appearance/theme`, {
data: new URLSearchParams({theme: e.target.getAttribute('data-value')}),
});
if (res.ok) window.location.reload();
});
}


Expand Down
4 changes: 2 additions & 2 deletions web_src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {
initCommitStatuses,
} from './features/repo-commit.js';
import {
initFootLanguageMenu,
initFooterMenus,
initGlobalButtonClickOnEnter,
initGlobalButtons,
initGlobalCommon,
Expand Down Expand Up @@ -111,7 +111,7 @@ onDomReady(() => {
initInstall();

initHeadNavbarContentToggle();
initFootLanguageMenu();
initFooterMenus();

initCommentContent();
initContextPopups();
Expand Down
1 change: 1 addition & 0 deletions web_src/svg/gitea-eclipse.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.