Skip to content

Commit b6d934b

Browse files
committed
add config option default_theme_dark
Signed-off-by: Sven Nierlein <[email protected]>
1 parent 7b128fa commit b6d934b

File tree

8 files changed

+62
-40
lines changed

8 files changed

+62
-40
lines changed

Changes

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ This file documents the revision history for the Monitoring Webinterface Thruk.
22

33
next:
44
- improve logs performance when using logcache
5+
- improve switching light/dark mode automatically
6+
- add config option default_theme_dark
57
- fix minemap missing services if user is only authorized for service but not the host
68
- fix back button for embedded links, like pnp or grafana
79
- Panorama:

docs/documentation/configuration.asciidoc

+11-2
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,20 @@ ex.:
267267
=== default_theme
268268

269269
Default theme to use for all users. Must be a valid sub directory in
270-
the themes folder.
270+
the `themes_path` folder.
271271

272272
ex.:
273273

274-
default_theme = Thruk
274+
default_theme = Light
275+
276+
277+
=== default_theme_dark
278+
279+
Default theme to use for if the user prefers a dark theme.
280+
281+
ex.:
282+
283+
default_theme_dark = Dark
275284

276285

277286
=== first_day_of_week

lib/Thruk/Action/AddDefaults.pm

+11-10
Original file line numberDiff line numberDiff line change
@@ -79,23 +79,24 @@ sub begin {
7979
# use pager?
8080
Thruk::Utils::set_paging_steps($c, $c->config->{'paging_steps'});
8181

82+
###############################
8283
# which theme?
8384
my $available_themes = Thruk::Base::array2hash($c->config->{'themes'});
84-
my($param_theme, $cookie_theme);
85-
if( $c->req->parameters->{'theme'} ) {
86-
$param_theme = $c->req->parameters->{'theme'};
87-
}
88-
elsif($c->cookies('thruk_theme') ) {
89-
my $theme_cookie = $c->cookies('thruk_theme');
90-
$cookie_theme = $theme_cookie if(defined $theme_cookie && defined $available_themes->{$theme_cookie});
85+
my $theme = $c->req->parameters->{'theme'} || $c->cookies('thruk_theme') || 'auto:';
86+
if($theme =~ m/^auto:(.*)/mx) {
87+
if($1 eq 'dark') {
88+
$theme = $c->config->{'default_theme_dark'};
89+
} else {
90+
$theme = $c->config->{'default_theme'};
91+
}
9192
}
92-
my $theme = $param_theme || $cookie_theme || $c->config->{'default_theme'};
9393
$theme = $c->config->{'default_theme'} unless defined $available_themes->{$theme};
9494
$theme = 'Light' unless defined $available_themes->{$theme};
9595
$theme = $c->config->{'themes'}->[0] unless defined $available_themes->{$theme};
96-
$c->stash->{'theme'} = $theme || 'None';
97-
$c->stash->{'cookie_theme'} = $cookie_theme // '';
96+
$c->stash->{'theme'} = $theme || 'Broken';
97+
$c->stash->{'cookie_theme'} = $c->cookies('thruk_theme') // '';
9898

99+
###############################
99100
if(exists $c->req->parameters->{'noheader'}) {
100101
$c->req->parameters->{'hidetop'} = 1;
101102
}

lib/Thruk/Config.pm

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ my $base_defaults = {
7676
'group_paging_grid' => '*5, 10, 50, all',
7777
'group_paging_summary' => '*10, 50, 100, all',
7878
'default_theme' => 'Light',
79+
'default_theme_dark' => 'Dark',
7980
'datetime_format' => '%Y-%m-%d %H:%M:%S',
8081
'datetime_format_long' => '%a %b %e %H:%M:%S %Z %Y',
8182
'datetime_format_today' => '%H:%M:%S',

root/thruk/javascript/thruk-3.12.js

+20-6
Original file line numberDiff line numberDiff line change
@@ -1077,20 +1077,34 @@ function preventDblEvents(el) {
10771077
return false;
10781078
}
10791079

1080-
function switchTheme(sel, store) {
1081-
theme = sel;
1080+
function switchTheme(sel) {
10821081
if(sel && sel.tagName) {
1083-
theme = jQuery(sel).val();
1082+
sel = jQuery(sel).val();
1083+
}
1084+
if(is_array(sel)) {
1085+
sel = sel[0];
10841086
}
1085-
if(is_array(theme)) {
1086-
theme = theme[0];
1087+
1088+
var auto = "";
1089+
if(sel.match(/^auto:/)) {
1090+
sel = theme_default;
1091+
auto = "auto:light";
1092+
if(isDarkModeEnabled()) {
1093+
sel = theme_default_dark;
1094+
auto = "auto:dark";
1095+
}
1096+
cookieSave('thruk_theme', auto);
10871097
}
1098+
if(theme == sel) {
1099+
return;
1100+
}
1101+
theme = sel;
10881102
if(theme.match(/dark/i)) {
10891103
jQuery("BODY").addClass("dark");
10901104
} else {
10911105
jQuery("BODY").removeClass("dark");
10921106
}
1093-
if(store !== false) {
1107+
if(!auto) {
10941108
cookieSave('thruk_theme', theme);
10951109
}
10961110
jQuery("LINK.maintheme").attr("href", url_prefix+"themes/"+theme+"/stylesheets/"+theme+".css");

templates/_common_js.tt

+10-15
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
var product_prefix = '[% product_prefix %]';
1717
var cookie_path = '[% cookie_path %]';
1818
var theme = '[% theme %]';
19-
var themes = [% json_encode(c.config.themes) %];
19+
var theme_default = '[% c.config.default_theme %]';
20+
var theme_default_dark = '[% c.config.default_theme_dark %]';
2021
var has_expire_acks = [% IF has_expire_acks %]true[% ELSE %]false[% END %];
2122
var fav_counter = [% IF fav_counter +%]true[% ELSE %]false[% END %];
2223
var perf_bar_mode = '[% perf_bar_mode %]';
@@ -83,20 +84,14 @@
8384

8485
// switch to dark theme if prefered by the user
8586
var cookieTheme = readCookie('thruk_theme');
86-
if(!cookieTheme && isDarkModeEnabled()) {
87-
// user has not set any theme yet and prefers dark mode
88-
if(jQuery.inArray(themes, "Dark")) {
89-
switchTheme("Dark", false);
90-
} else {
91-
// use first theme which has dark in name
92-
jQuery.each(themes, function(i, name) {
93-
if(name.match(/dark/i)) {
94-
switchTheme(name, false);
95-
return false;
96-
}
97-
});
98-
}
87+
if(!cookieTheme || cookieTheme.match(/^auto:/)) {
88+
switchTheme(cookieTheme);
89+
}
90+
// watch for changes
91+
if(window.matchMedia) {
92+
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => {
93+
switchTheme(cookieTheme);
94+
});
9995
}
100-
10196
-->
10297
</script>

templates/_header_prefs.tt

+5-6
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,21 @@
1717
<th class="align-middle"><i class="uil uil-brush-alt"></i></th>
1818
<th class="align-middle">Theme</th>
1919
<td class="flex gap-x-1">
20-
[% IF c.config.themes.size > 4 %]
20+
[% IF c.config.themes.size > 1 %]
2121
<select name="theme" id="pref_theme" class="w-40" onchange="switchTheme(this)">
22+
<option value="auto:"[% IF !cookie_theme || cookie_theme.match('/^auto:/') %] selected[% END %]>system default (auto.)</option>
2223
[% FOR t = c.config.themes %]
23-
<option value="[% t | html %]"[% IF cookie_theme && t == theme %] selected[% END %]>[% t | html %]</option>
24+
<option value="[% t | html %]"[% IF cookie_theme && t == cookie_theme %] selected[% END %]>[% t | html %]</option>
2425
[% END %]
2526
</select>
2627
[% ELSE %]
2728
<div class="radiogroup min-w-[18rem]">
29+
<input type="radio" name="theme" value="auto:" id="theme-auto" onchange="switchTheme('auto:')" [% IF !cookie_theme || cookie_theme.match('/^auto:/') %]checked[% END %]><label for="theme-auto" title="Use system default theme.&#013;Switches automatically between light/dark theme based on desktop preferences.">auto.</label>
2830
[% FOR t = c.config.themes %]
29-
<input type="radio" name="theme" value="[% t | html %]" id="theme-[% t | html %]" onchange="switchTheme('[% t | html %]')" [% IF cookie_theme && t == theme %]checked[% END %]><label for="theme-[% t | html %]">[% t | html %]</label>
31+
<input type="radio" name="theme" value="[% t | html %]" id="theme-[% t | html %]" onchange="switchTheme('[% t | html %]')" [% IF cookie_theme && t == cookie_theme %]checked[% END %]><label for="theme-[% t | html %]">[% t | html %]</label>
3032
[% END %]
3133
</div>
3234
[% END %]
33-
<button class="iconOnly" onclick="cookieRemove('thruk_theme'); reloadPage(50, true); return false;" title="use system default theme">
34-
<i class="uil uil-cloud-times"></i>
35-
</button>
3635
</td>
3736
</tr>
3837
[% IF show_sounds %]

thruk.conf

+2-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ ssl_verify_hostnames=1
208208

209209
######################################
210210
# themes
211-
default_theme = Light
211+
default_theme = Light
212+
default_theme_dark = Dark
212213

213214
######################################
214215
# path used for cookies. Do not change unless you have weird url rewrites which

0 commit comments

Comments
 (0)