Skip to content

Commit b4094b2

Browse files
committed
Allow disabling theme picker
1 parent b294839 commit b4094b2

File tree

5 files changed

+62
-39
lines changed

5 files changed

+62
-39
lines changed

docs/configuration.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,10 +389,12 @@ Example:
389389

390390
```yaml
391391
theme:
392+
# This will be the default theme
392393
background-color: 100 20 10
393394
primary-color: 40 90 40
394395
contrast-multiplier: 1.1
395396
397+
disable-picker: false
396398
presets:
397399
gruvbox-dark:
398400
background-color: 0 0 16
@@ -421,6 +423,7 @@ If you don't want to spend time configuring your own theme, there are [several a
421423
| contrast-multiplier | number | no | 1 |
422424
| text-saturation-multiplier | number | no | 1 |
423425
| custom-css-file | string | no | |
426+
| disable-picker | bool | false | |
424427
| presets | object | no | |
425428

426429
#### `light`
@@ -466,8 +469,11 @@ theme:
466469
>
467470
> In addition, you can also use the `css-class` property which is available on every widget to set custom class names for individual widgets.
468471

472+
#### `disable-picker`
473+
When set to `true` hides the theme picker and disables the abiltity to switch between themes. All users who previously picked a non-default theme will be switched over to the default theme.
474+
469475
#### `presets`
470-
Define additional theme presets that can be selected from the theme switcher on the page. For each preset, you can specify the same properties as for the default theme, such as `background-color`, `primary-color`, `positive-color`, `negative-color`, `contrast-multiplier`, etc., except for the `custom-css-file` property.
476+
Define additional theme presets that can be selected from the theme picker on the page. For each preset, you can specify the same properties as for the default theme, such as `background-color`, `primary-color`, `positive-color`, `negative-color`, `contrast-multiplier`, etc., except for the `custom-css-file` property.
471477

472478
Example:
473479

internal/glance/config.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ type config struct {
4747

4848
Theme struct {
4949
themeProperties `yaml:",inline"`
50-
CustomCSSFile string `yaml:"custom-css-file"`
51-
Presets orderedYAMLMap[string, *themeProperties] `yaml:"presets"`
50+
CustomCSSFile string `yaml:"custom-css-file"`
51+
52+
DisablePicker bool `yaml:"disable-picker"`
53+
Presets orderedYAMLMap[string, *themeProperties] `yaml:"presets"`
5254
} `yaml:"theme"`
5355

5456
Branding struct {

internal/glance/glance.go

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -101,35 +101,37 @@ func newApplication(c *config) (*application, error) {
101101
// Init themes
102102
//
103103

104-
themeKeys := make([]string, 0, 2)
105-
themeProps := make([]*themeProperties, 0, 2)
106-
107-
defaultDarkTheme, ok := config.Theme.Presets.Get("default-dark")
108-
if ok && !config.Theme.SameAs(defaultDarkTheme) || !config.Theme.SameAs(&themeProperties{}) {
109-
themeKeys = append(themeKeys, "default-dark")
110-
themeProps = append(themeProps, &themeProperties{})
111-
}
112-
113-
themeKeys = append(themeKeys, "default-light")
114-
themeProps = append(themeProps, &themeProperties{
115-
Light: true,
116-
BackgroundColor: &hslColorField{240, 13, 95},
117-
PrimaryColor: &hslColorField{230, 100, 30},
118-
NegativeColor: &hslColorField{0, 70, 50},
119-
ContrastMultiplier: 1.3,
120-
TextSaturationMultiplier: 0.5,
121-
})
104+
if !config.Theme.DisablePicker {
105+
themeKeys := make([]string, 0, 2)
106+
themeProps := make([]*themeProperties, 0, 2)
107+
108+
defaultDarkTheme, ok := config.Theme.Presets.Get("default-dark")
109+
if ok && !config.Theme.SameAs(defaultDarkTheme) || !config.Theme.SameAs(&themeProperties{}) {
110+
themeKeys = append(themeKeys, "default-dark")
111+
themeProps = append(themeProps, &themeProperties{})
112+
}
122113

123-
themePresets, err := newOrderedYAMLMap(themeKeys, themeProps)
124-
if err != nil {
125-
return nil, fmt.Errorf("creating theme presets: %v", err)
126-
}
127-
config.Theme.Presets = *themePresets.Merge(&config.Theme.Presets)
114+
themeKeys = append(themeKeys, "default-light")
115+
themeProps = append(themeProps, &themeProperties{
116+
Light: true,
117+
BackgroundColor: &hslColorField{240, 13, 95},
118+
PrimaryColor: &hslColorField{230, 100, 30},
119+
NegativeColor: &hslColorField{0, 70, 50},
120+
ContrastMultiplier: 1.3,
121+
TextSaturationMultiplier: 0.5,
122+
})
123+
124+
themePresets, err := newOrderedYAMLMap(themeKeys, themeProps)
125+
if err != nil {
126+
return nil, fmt.Errorf("creating theme presets: %v", err)
127+
}
128+
config.Theme.Presets = *themePresets.Merge(&config.Theme.Presets)
128129

129-
for key, properties := range config.Theme.Presets.Items() {
130-
properties.Key = key
131-
if err := properties.init(); err != nil {
132-
return nil, fmt.Errorf("initializing preset theme %s: %v", key, err)
130+
for key, properties := range config.Theme.Presets.Items() {
131+
properties.Key = key
132+
if err := properties.init(); err != nil {
133+
return nil, fmt.Errorf("initializing preset theme %s: %v", key, err)
134+
}
133135
}
134136
}
135137

@@ -288,11 +290,13 @@ type templateData struct {
288290
func (a *application) populateTemplateRequestData(data *templateRequestData, r *http.Request) {
289291
theme := &a.Config.Theme.themeProperties
290292

291-
selectedTheme, err := r.Cookie("theme")
292-
if err == nil {
293-
preset, exists := a.Config.Theme.Presets.Get(selectedTheme.Value)
294-
if exists {
295-
theme = preset
293+
if !a.Config.Theme.DisablePicker {
294+
selectedTheme, err := r.Cookie("theme")
295+
if err == nil {
296+
preset, exists := a.Config.Theme.Presets.Get(selectedTheme.Value)
297+
if exists {
298+
theme = preset
299+
}
296300
}
297301
}
298302

@@ -436,7 +440,11 @@ func (a *application) server() (func() error, func() error) {
436440
mux.HandleFunc("GET /{page}", a.handlePageRequest)
437441

438442
mux.HandleFunc("GET /api/pages/{page}/content/{$}", a.handlePageContentRequest)
439-
mux.HandleFunc("POST /api/set-theme/{key}", a.handleThemeChangeRequest)
443+
444+
if !a.Config.Theme.DisablePicker {
445+
mux.HandleFunc("POST /api/set-theme/{key}", a.handleThemeChangeRequest)
446+
}
447+
440448
mux.HandleFunc("/api/widgets/{widget}/{path...}", a.handleWidgetRequest)
441449
mux.HandleFunc("GET /api/healthz", func(w http.ResponseWriter, _ *http.Request) {
442450
w.WriteHeader(http.StatusOK)

internal/glance/static/js/page.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -689,12 +689,15 @@ async function changeTheme(key, onChanged) {
689689
setTimeout(() => { tempStyle.remove(); }, 10);
690690
}
691691

692-
function initThemeSwitcher() {
692+
function initThemePicker() {
693+
const themeChoicesInMobileNav = find(".mobile-navigation .theme-choices");
694+
if (!themeChoicesInMobileNav) return;
695+
693696
const themeChoicesInHeader = find(".header-container .theme-choices");
694697

695698
if (themeChoicesInHeader) {
696699
themeChoicesInHeader.replaceWith(
697-
find(".mobile-navigation .theme-choices").cloneNode(true)
700+
themeChoicesInMobileNav.cloneNode(true)
698701
);
699702
}
700703

@@ -739,7 +742,7 @@ function initThemeSwitcher() {
739742
}
740743

741744
async function setupPage() {
742-
initThemeSwitcher();
745+
initThemePicker();
743746

744747
const pageElement = document.getElementById("page");
745748
const pageContentElement = document.getElementById("page-content");

internal/glance/templates/page.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<nav class="nav flex grow hide-scrollbars">
3434
{{ template "navigation-links" . }}
3535
</nav>
36+
{{ if not .App.Config.Theme.DisablePicker }}
3637
<div class="theme-picker self-center" data-popover-type="html" data-popover-position="below" data-popover-show-delay="0">
3738
<div class="current-theme-preview">
3839
{{ .Request.Theme.PreviewHTML }}
@@ -41,6 +42,7 @@
4142
<div class="theme-choices"></div>
4243
</div>
4344
</div>
45+
{{ end }}
4446
{{- if .App.RequiresAuth }}
4547
<a class="block self-center" href="{{ .App.Config.Server.BaseURL }}/logout" title="Logout">
4648
<svg class="logout-button" stroke="var(--color-text-subdue)" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5">
@@ -66,6 +68,7 @@
6668
</div>
6769

6870
<div class="mobile-navigation-actions flex flex-column margin-block-10">
71+
{{ if not .App.Config.Theme.DisablePicker }}
6972
<div class="theme-picker flex justify-between items-center" data-popover-type="html" data-popover-position="above" data-popover-show-delay="0" data-popover-hide-delay="100" data-popover-anchor=".current-theme-preview" data-popover-trigger="click">
7073
<div data-popover-html>
7174
<div class="theme-choices">
@@ -87,6 +90,7 @@
8790
</svg>
8891
</div>
8992
</div>
93+
{{ end }}
9094

9195
{{ if .App.RequiresAuth }}
9296
<a href="{{ .App.Config.Server.BaseURL }}/logout" class="flex justify-between items-center">

0 commit comments

Comments
 (0)