From 5c0eb20cb446f5027d91f334d0cf6a669011e41c Mon Sep 17 00:00:00 2001 From: Ajay Bura Date: Sat, 9 Jul 2022 18:08:35 +0530 Subject: [PATCH] Follow system theme by default --- .github/PULL_REQUEST_TEMPLATE.md | 2 +- src/app/organisms/settings/Settings.jsx | 34 ++++++++++--------- src/client/state/settings.js | 45 ++++++++++++++----------- src/index.jsx | 2 +- 4 files changed, 46 insertions(+), 37 deletions(-) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index a8a04e7..1292f1d 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,4 +1,4 @@ - + ### Description diff --git a/src/app/organisms/settings/Settings.jsx b/src/app/organisms/settings/Settings.jsx index 82b948a..b0f45f4 100644 --- a/src/app/organisms/settings/Settings.jsx +++ b/src/app/organisms/settings/Settings.jsx @@ -57,23 +57,25 @@ function AppearanceSection() { )} content={Use light or dark mode based on the system settings.} /> - {!settings.useSystemTheme && ( - settings.setTheme(index)} - /> - )} - /> + { + if (settings.useSystemTheme) toggleSystemTheme(); + settings.setTheme(index); + updateState({}); + }} + /> )} + />
Room messages diff --git a/src/client/state/settings.js b/src/client/state/settings.js index 0f476ef..32f55fc 100644 --- a/src/client/state/settings.js +++ b/src/client/state/settings.js @@ -48,31 +48,43 @@ class Settings extends EventEmitter { return this.themes[this.themeIndex]; } - setTheme(themeIndex) { - const appBody = document.getElementById('appBody'); - - appBody.classList.remove('system-theme'); + _clearTheme() { + document.body.classList.remove('system-theme'); this.themes.forEach((themeName) => { if (themeName === '') return; - appBody.classList.remove(themeName); + document.body.classList.remove(themeName); }); - // If use system theme is enabled - // we will override current theme choice with system theme + } + + applyTheme() { + this._clearTheme(); if (this.useSystemTheme) { - appBody.classList.add('system-theme'); - } else if (this.themes[themeIndex] !== '') { - appBody.classList.add(this.themes[themeIndex]); + document.body.classList.add('system-theme'); + } else if (this.themes[this.themeIndex]) { + document.body.classList.add(this.themes[this.themeIndex]); } - setSettings('themeIndex', themeIndex); + } + + setTheme(themeIndex) { this.themeIndex = themeIndex; + setSettings('themeIndex', this.themeIndex); + this.applyTheme(); + } + + toggleUseSystemTheme() { + this.useSystemTheme = !this.useSystemTheme; + setSettings('useSystemTheme', this.useSystemTheme); + this.applyTheme(); + + this.emit(cons.events.settings.SYSTEM_THEME_TOGGLED, this.useSystemTheme); } getUseSystemTheme() { if (typeof this.useSystemTheme === 'boolean') return this.useSystemTheme; const settings = getSettings(); - if (settings === null) return false; - if (typeof settings.useSystemTheme === 'undefined') return false; + if (settings === null) return true; + if (typeof settings.useSystemTheme === 'undefined') return true; return settings.useSystemTheme; } @@ -138,12 +150,7 @@ class Settings extends EventEmitter { setter(action) { const actions = { [cons.actions.settings.TOGGLE_SYSTEM_THEME]: () => { - this.useSystemTheme = !this.useSystemTheme; - - setSettings('useSystemTheme', this.useSystemTheme); - this.setTheme(this.themeIndex); - - this.emit(cons.events.settings.SYSTEM_THEME_TOGGLED, this.useSystemTheme); + this.toggleUseSystemTheme(); }, [cons.actions.settings.TOGGLE_MARKDOWN]: () => { this.isMarkdown = !this.isMarkdown; diff --git a/src/index.jsx b/src/index.jsx index 69fff62..55f8656 100644 --- a/src/index.jsx +++ b/src/index.jsx @@ -7,7 +7,7 @@ import settings from './client/state/settings'; import App from './app/pages/App'; -settings.setTheme(settings.getThemeIndex()); +settings.applyTheme(); ReactDom.render( ,