Skip to content
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

Added automated dark / follow system settings mode #1806

Open
wants to merge 6 commits into
base: v2-develop
Choose a base branch
from
Open
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
32 changes: 30 additions & 2 deletions api/classes/organizr.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,32 @@ public function setTheme($theme = null, $rootPath = '')
$theme = $theme ?? $this->config['theme'];
return '<link id="theme" href="' . $rootPath . 'css/themes/' . $theme . '.css?v=' . $this->fileHash . '" rel="stylesheet">';
}

public function setScheme()
{
return '
<script src="https://cdn.jsdelivr.net/npm/js-cookie/dist/js.cookie.min.js"></script>
<script>
// code to set the `color_scheme` cookie
let $color_scheme = Cookies.get("color_scheme"), $auto_dark_mode = ' . (($this->config['autoDarkMode']) ? 'true' : 'false') . ';
function get_color_scheme() {
return (window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches) ? "dark" : "light";
}
function update_color_scheme(override) {
if (typeof override == "undefined") override = "";
Cookies.set("color_scheme", get_color_scheme());
if ($auto_dark_mode) changeStyle(get_color_scheme());
if (override != "") changeStyle(override);
}
// read & compare cookie `color-scheme`
if ((typeof $color_scheme === "undefined") || (get_color_scheme() != $color_scheme))
update_color_scheme();
// detect changes and change the cookie
if (window.matchMedia)
window.matchMedia("(prefers-color-scheme: dark)").addListener( update_color_scheme );
</script>
';
}

public function pluginFiles($type, $settings = false, $rootPath = '')
{
Expand Down Expand Up @@ -1807,7 +1833,9 @@ public function getCustomizeAppearance()
$this->settingsOption('color', 'buttonColor', ['label' => 'Button Color']),
$this->settingsOption('color', 'buttonTextColor', ['label' => 'Button Text Color']),
$this->settingsOption('select', 'theme', ['label' => 'Theme', 'class' => 'themeChanger', 'options' => $this->getThemes()]),
$this->settingsOption('select', 'style', ['label' => 'Style', 'class' => 'styleChanger', 'options' => [['name' => 'Light', 'value' => 'light'], ['name' => 'Dark', 'value' => 'dark'], ['name' => 'Horizontal', 'value' => 'horizontal']]]),
$this->settingsOption('select', 'style', ['label' => 'Style', 'class' => 'styleChanger', 'attr' => $this->config['autoDarkMode'] ? 'disabled' : '','options' => [['name' => 'Light', 'value' => 'light'], ['name' => 'Dark', 'value' => 'dark'], ['name' => 'Horizontal', 'value' => 'horizontal']]]),
$this->settingsOption('blank'),
$this->settingsOption('switch', 'autoDarkMode', ['label' => 'Automatic Dark Mode','class' => $this->config['autoDarkMode'] ? 'on' : '','help' => 'Follows system settings. Style setting will be ignored.', 'attr' => 'onchange="$(this).toggleClass(\'on\'); if ($(this).hasClass(\'on\')){ $auto_dark_mode = true; update_color_scheme(); $(\'.styleChanger[name=style]\').prop(\'disabled\',true); } else { $auto_dark_mode = false; update_color_scheme(\''.$this->config['style'].'\'); $(\'.styleChanger[name=style]\').prop(\'disabled\',false);}"']),
],
'Notifications' => [
$this->settingsOption('select', 'notificationBackbone', ['label' => 'Type', 'class' => 'notifyChanger', 'options' => $this->notificationTypesOptions()]),
Expand Down Expand Up @@ -6739,4 +6767,4 @@ protected function processQueries(array $request, $migration = false)
return count($request) > 1 ? $results : $results[$firstKey];
}

}
}
1 change: 1 addition & 0 deletions api/config/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
'lockScreen' => false,
'theme' => 'Organizr',
'style' => 'dark',
'autoDarkMode' => false,
'plexURL' => '',
'plexTabURL' => '',
'plexToken' => '',
Expand Down
3 changes: 2 additions & 1 deletion api/v2/routes/root.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
$GLOBALS['api']['response']['data']['branch'] = $Organizr->config['branch'];
$GLOBALS['api']['response']['data']['theme'] = $Organizr->config['theme'];
$GLOBALS['api']['response']['data']['style'] = $Organizr->config['style'];
$GLOBALS['api']['response']['data']['darkmode'] = $Organizr->config['autoDarkMode'];
$GLOBALS['api']['response']['data']['version'] = $Organizr->version;
$GLOBALS['api']['response']['data']['settings'] = $Organizr->organizrSpecialSettings();
$GLOBALS['api']['response']['data']['plugins'] = $Organizr->pluginGlobalList();
Expand All @@ -138,4 +139,4 @@
return $response
->withHeader('Content-Type', 'application/json;charset=UTF-8')
->withStatus($GLOBALS['responseCode']);
});
});
3 changes: 2 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ class="fa fa-check text-success"></i>&nbsp;
echo $Organizr->formKey();
echo $Organizr->loadCalendarJS();
echo $Organizr->CBPFWTabs();
echo $Organizr->setScheme();
?>
</body>

</html>
</html>
3 changes: 2 additions & 1 deletion js/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11558,6 +11558,7 @@ function launch(){
appearance:json.data.appearance,
theme:json.data.theme,
style:json.data.style,
darkMode:json.data.darkmode,
version:json.data.version
};
// Add element to signal activeInfo Ready
Expand All @@ -11570,7 +11571,7 @@ function launch(){
checkMessage();
errorPage();
uriRedirect();
changeStyle(activeInfo.style);
changeStyle((activeInfo.darkMode) ? $color_scheme : activeInfo.style);
changeTheme(activeInfo.theme);
setSSO();
checkToken();
Expand Down