From 9141f44ebb1a8139ea5c7b45756efedfe7eda482 Mon Sep 17 00:00:00 2001 From: Joel Collins Date: Thu, 30 Jan 2020 16:46:40 +0000 Subject: [PATCH] Option to follow system dark theme --- src/App.vue | 51 +++++++++++++++++-- .../settingsComponents/appSettings.vue | 26 ++++++---- src/store.js | 2 +- 3 files changed, 64 insertions(+), 15 deletions(-) diff --git a/src/App.vue b/src/App.vue index faf10b8b..d97cd99e 100644 --- a/src/App.vue +++ b/src/App.vue @@ -27,22 +27,67 @@ export default { }, data: function() { - return {}; + return { + systemDark: undefined, + themeObserver: undefined + }; }, computed: { + isSystemDark: function() { + if ( + window.matchMedia && + window.matchMedia("(prefers-color-scheme: dark)").matches + ) { + return true; + } else { + return false; + } + }, handleTheme: function() { + var isDark = false; + if (this.$store.state.globalSettings.appTheme == "dark") { + isDark = true; + } else if (this.$store.state.globalSettings.appTheme == "system") { + if (this.systemDark) { + isDark = true; + } + } return { - "uk-light": this.$store.state.globalSettings.darkMode, - "uk-background-secondary": this.$store.state.globalSettings.darkMode + "uk-light": isDark, + "uk-background-secondary": isDark }; } }, + mounted() { + // Query CSS dark theme preference + var mql = window.matchMedia("(prefers-color-scheme: dark)"); + // Check for system dark theme when mounted + if (mql.matches) { + this.systemDark = true; + } + // Create a theme observer to watch for changes + this.themeObserver = mql.addListener(e => { + if (e.matches) { + this.systemDark = true; + } else { + this.systemDark = false; + } + }); + }, + created: function() { window.addEventListener("beforeunload", this.handleExit); }, + beforeDestroy: function() { + // Disconnect the theme observer + if (this.themeObserver) { + this.themeObserver.disconnect(); + } + }, + methods: { handleExit: function() { console.log("Triggered beforeunload"); diff --git a/src/components/controlComponents/settingsComponents/appSettings.vue b/src/components/controlComponents/settingsComponents/appSettings.vue index 55e8fd37..08ac791f 100644 --- a/src/components/controlComponents/settingsComponents/appSettings.vue +++ b/src/components/controlComponents/settingsComponents/appSettings.vue @@ -1,10 +1,14 @@ @@ -19,26 +23,26 @@ export default { }, computed: { - darkMode: { + appTheme: { get() { - return this.$store.state.globalSettings.darkMode; + return this.$store.state.globalSettings.appTheme; }, set(value) { - this.$store.commit("changeSetting", ["darkMode", value]); + this.$store.commit("changeSetting", ["appTheme", value]); } } }, watch: { - darkMode() { - console.log("Saving darkmode setting"); - this.setLocalStorageObj("darkMode", this.darkMode); + appTheme() { + console.log("Saving appTheme setting"); + this.setLocalStorageObj("appTheme", this.appTheme); } }, mounted() { // Try loading settings from localStorage. If null, don't change. - this.darkMode = this.getLocalStorageObj("darkMode") || this.darkMode; + this.appTheme = this.getLocalStorageObj("appTheme") || this.appTheme; } }; diff --git a/src/store.js b/src/store.js index 45ecd47f..48934e7c 100644 --- a/src/store.js +++ b/src/store.js @@ -14,7 +14,7 @@ export default new Vuex.Store({ disableStream: false, autoGpuPreview: false, trackWindow: true, - darkMode: false + appTheme: "system" } },