Support for detection of system's theme
This commit is contained in:
parent
538f69235f
commit
baf820bcdb
2 changed files with 97 additions and 1 deletions
44
src/Ui/media/ZeroSiteTheme.coffee
Normal file
44
src/Ui/media/ZeroSiteTheme.coffee
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
DARK = "(prefers-color-scheme: dark)"
|
||||||
|
LIGHT = "(prefers-color-scheme: light)"
|
||||||
|
|
||||||
|
mqDark = window.matchMedia(DARK)
|
||||||
|
mqLight = window.matchMedia(LIGHT)
|
||||||
|
|
||||||
|
|
||||||
|
changeColorScheme = (theme) ->
|
||||||
|
zeroframe.cmd "userGetGlobalSettings", [], (user_settings) ->
|
||||||
|
if user_settings.theme != theme
|
||||||
|
user_settings.theme = theme
|
||||||
|
zeroframe.cmd "userSetGlobalSettings", [user_settings]
|
||||||
|
|
||||||
|
location.reload()
|
||||||
|
|
||||||
|
return
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
displayNotification = ({matches, media}) ->
|
||||||
|
if !matches
|
||||||
|
return
|
||||||
|
|
||||||
|
zeroframe.cmd "wrapperNotification", ["info", "Your system's theme has been changed.<br>Please reload site to use it."]
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
detectColorScheme = ->
|
||||||
|
if mqDark.matches
|
||||||
|
changeColorScheme("dark")
|
||||||
|
else if mqLight.matches
|
||||||
|
changeColorScheme("light")
|
||||||
|
|
||||||
|
mqDark.addListener(displayNotification)
|
||||||
|
mqLight.addListener(displayNotification)
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
zeroframe.cmd "userGetGlobalSettings", [], (user_settings) ->
|
||||||
|
if user_settings.use_system_theme == true
|
||||||
|
detectColorScheme()
|
||||||
|
|
||||||
|
return
|
|
@ -2046,3 +2046,55 @@ $.extend( $.easing,
|
||||||
window.zeroframe = new WrapperZeroFrame(window.wrapper);
|
window.zeroframe = new WrapperZeroFrame(window.wrapper);
|
||||||
|
|
||||||
}).call(this);
|
}).call(this);
|
||||||
|
|
||||||
|
|
||||||
|
/* ---- src/Ui/media/ZeroSiteTheme.coffee ---- */
|
||||||
|
|
||||||
|
|
||||||
|
(function() {
|
||||||
|
var DARK, LIGHT, changeColorScheme, detectColorScheme, displayNotification, mqDark, mqLight;
|
||||||
|
|
||||||
|
DARK = "(prefers-color-scheme: dark)";
|
||||||
|
|
||||||
|
LIGHT = "(prefers-color-scheme: light)";
|
||||||
|
|
||||||
|
mqDark = window.matchMedia(DARK);
|
||||||
|
|
||||||
|
mqLight = window.matchMedia(LIGHT);
|
||||||
|
|
||||||
|
changeColorScheme = function(theme) {
|
||||||
|
zeroframe.cmd("userGetGlobalSettings", [], function(user_settings) {
|
||||||
|
if (user_settings.theme !== theme) {
|
||||||
|
user_settings.theme = theme;
|
||||||
|
zeroframe.cmd("userSetGlobalSettings", [user_settings]);
|
||||||
|
location.reload();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
displayNotification = function(arg) {
|
||||||
|
var matches, media;
|
||||||
|
matches = arg.matches, media = arg.media;
|
||||||
|
if (!matches) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
zeroframe.cmd("wrapperNotification", ["info", "Your system's theme has been changed.<br>Please reload site to use it."]);
|
||||||
|
};
|
||||||
|
|
||||||
|
detectColorScheme = function() {
|
||||||
|
if (mqDark.matches) {
|
||||||
|
changeColorScheme("dark");
|
||||||
|
} else if (mqLight.matches) {
|
||||||
|
changeColorScheme("light");
|
||||||
|
}
|
||||||
|
mqDark.addListener(displayNotification);
|
||||||
|
mqLight.addListener(displayNotification);
|
||||||
|
};
|
||||||
|
|
||||||
|
zeroframe.cmd("userGetGlobalSettings", [], function(user_settings) {
|
||||||
|
if (user_settings.use_system_theme === true) {
|
||||||
|
detectColorScheme();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
}).call(this);
|
||||||
|
|
Loading…
Reference in a new issue