From cd2bc7b7c8c1e9d0396c2f1a9aa52afa5f57038e Mon Sep 17 00:00:00 2001 From: Konstantinos Spartalis Date: Tue, 10 Mar 2026 00:33:33 +0200 Subject: [PATCH 1/7] working --- .../mvc/app/views/layouts/default.volt | 5 ++ src/opnsense/www/js/opnsense_theme_auto.js | 55 +++++++++++++++++++ src/www/head.inc | 6 ++ src/www/system_general.php | 11 +++- 4 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 src/opnsense/www/js/opnsense_theme_auto.js diff --git a/src/opnsense/mvc/app/views/layouts/default.volt b/src/opnsense/mvc/app/views/layouts/default.volt index 97b3d11041..2d2830515e 100644 --- a/src/opnsense/mvc/app/views/layouts/default.volt +++ b/src/opnsense/mvc/app/views/layouts/default.volt @@ -207,6 +207,11 @@ + + {% if theme_name == 'opnsense-auto' %} + + {% endif %} +
diff --git a/src/opnsense/www/js/opnsense_theme_auto.js b/src/opnsense/www/js/opnsense_theme_auto.js new file mode 100644 index 0000000000..de3c3f0b89 --- /dev/null +++ b/src/opnsense/www/js/opnsense_theme_auto.js @@ -0,0 +1,55 @@ + /* + * Copyright (C) 2026 Konstantinos Spartalis + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +(function() { + if (window.opnsenseAutoThemeEnabled || document.cookie.indexOf('opnsense_theme=opnsense-auto') !== -1) { + + function updateTheme() { + var isDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches; + var activeTheme = isDark ? 'opnsense-dark' : 'opnsense'; + + var links = document.getElementsByTagName('link'); + for (var i = 0; i < links.length; i++) { + if (links[i].href && links[i].href.indexOf('/ui/themes/') !== -1) { + links[i].href = links[i].href.replace(/\/ui\/themes\/[^\/]+\//, '/ui/themes/' + activeTheme + '/'); + } + } + + var imgs = document.getElementsByTagName('img'); + for (var j = 0; j < imgs.length; j++) { + if (imgs[j].src && imgs[j].src.indexOf('/ui/themes/') !== -1) { + imgs[j].src = imgs[j].src.replace(/\/ui\/themes\/[^\/]+\//, '/ui/themes/' + activeTheme + '/'); + } + } + } + + updateTheme(); + + if (window.matchMedia) { + window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', updateTheme); + } + } +})(); \ No newline at end of file diff --git a/src/www/head.inc b/src/www/head.inc index 9a34549412..7f2c8c841a 100644 --- a/src/www/head.inc +++ b/src/www/head.inc @@ -32,6 +32,12 @@ $pagetitle .= html_safe(sprintf(' | %s.%s', $config['system']['hostname'], $conf + + + + + +