From 2ca44c967e0dad65391845bd5fee12ab0ba6b4aa Mon Sep 17 00:00:00 2001 From: Jason Kulatunga Date: Sat, 4 Jun 2022 20:32:12 -0700 Subject: [PATCH] simplify darkmode ui toggle. --- .../@treo/services/config/config.service.ts | 15 ------------ .../src/app/core/config/app.config.ts | 6 +---- .../dashboard-settings.component.html | 23 ++++++++----------- .../dashboard-settings.component.ts | 5 ++-- .../src/app/layout/layout.component.ts | 17 +++++++++++++- 5 files changed, 29 insertions(+), 37 deletions(-) diff --git a/webapp/frontend/src/@treo/services/config/config.service.ts b/webapp/frontend/src/@treo/services/config/config.service.ts index 278b506..5e5d1d0 100644 --- a/webapp/frontend/src/@treo/services/config/config.service.ts +++ b/webapp/frontend/src/@treo/services/config/config.service.ts @@ -13,15 +13,12 @@ export class TreoConfigService { // Private private _config: BehaviorSubject; - private systemPrefersDark: boolean; /** * Constructor */ constructor(@Inject(TREO_APP_CONFIG) defaultConfig: any) { - this.systemPrefersDark = window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches; - let currentScrutinyConfig = defaultConfig let localConfigStr = localStorage.getItem(SCRUTINY_CONFIG_LOCAL_STORAGE_KEY) @@ -30,9 +27,6 @@ export class TreoConfigService let localConfig = JSON.parse(localConfigStr) currentScrutinyConfig = Object.assign({}, localConfig, currentScrutinyConfig) // make sure defaults are available if missing from localStorage. } - - currentScrutinyConfig.theme = this.determineTheme(currentScrutinyConfig); - // Set the private defaults this._config = new BehaviorSubject(currentScrutinyConfig); } @@ -53,8 +47,6 @@ export class TreoConfigService //Store the config in localstorage localStorage.setItem(SCRUTINY_CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config)); - config.theme = this.determineTheme(config); - // Execute the observable this._config.next(config); } @@ -69,13 +61,6 @@ export class TreoConfigService // @ Private methods // ----------------------------------------------------------------------------------------------------- - /** - * Checks if theme should be set to dark based on config & system settings - */ - private determineTheme(config:AppConfig): string { - return (config.themeUseSystem && this.systemPrefersDark) ? "dark" : config.theme; - } - // ----------------------------------------------------------------------------------------------------- // @ Public methods // ----------------------------------------------------------------------------------------------------- diff --git a/webapp/frontend/src/app/core/config/app.config.ts b/webapp/frontend/src/app/core/config/app.config.ts index f2ef91b..ba39423 100644 --- a/webapp/frontend/src/app/core/config/app.config.ts +++ b/webapp/frontend/src/app/core/config/app.config.ts @@ -1,7 +1,7 @@ import { Layout } from "app/layout/layout.types"; // Theme type -export type Theme = "light" | "dark"; +export type Theme = "light" | "dark" | "system"; /** * AppConfig interface. Update this interface to strictly type your config @@ -17,8 +17,6 @@ export interface AppConfig dashboardSort: string; temperatureUnit: string; - - themeUseSystem: boolean; } /** @@ -37,7 +35,5 @@ export const appConfig: AppConfig = { dashboardSort: "status", temperatureUnit: "celsius", - - themeUseSystem: true, }; diff --git a/webapp/frontend/src/app/layout/common/dashboard-settings/dashboard-settings.component.html b/webapp/frontend/src/app/layout/common/dashboard-settings/dashboard-settings.component.html index 156ce37..a10d550 100644 --- a/webapp/frontend/src/app/layout/common/dashboard-settings/dashboard-settings.component.html +++ b/webapp/frontend/src/app/layout/common/dashboard-settings/dashboard-settings.component.html @@ -2,19 +2,16 @@
-
- Use system settings for dark mode -

- Theme: - - - light_mode - - - dark_mode - - -

+
+ + + Dark Mode + + System + Dark + Light + +
diff --git a/webapp/frontend/src/app/layout/common/dashboard-settings/dashboard-settings.component.ts b/webapp/frontend/src/app/layout/common/dashboard-settings/dashboard-settings.component.ts index 37ce10c..e56ffc9 100644 --- a/webapp/frontend/src/app/layout/common/dashboard-settings/dashboard-settings.component.ts +++ b/webapp/frontend/src/app/layout/common/dashboard-settings/dashboard-settings.component.ts @@ -14,7 +14,6 @@ export class DashboardSettingsComponent implements OnInit { dashboardDisplay: string; dashboardSort: string; temperatureUnit: string; - themeUseSystem: boolean; theme: string; // Private @@ -37,7 +36,6 @@ export class DashboardSettingsComponent implements OnInit { this.dashboardDisplay = config.dashboardDisplay; this.dashboardSort = config.dashboardSort; this.temperatureUnit = config.temperatureUnit; - this.themeUseSystem = config.themeUseSystem; this.theme = config.theme; }); @@ -45,11 +43,12 @@ export class DashboardSettingsComponent implements OnInit { } saveSettings(): void { + + const newSettings = { dashboardDisplay: this.dashboardDisplay, dashboardSort: this.dashboardSort, temperatureUnit: this.temperatureUnit, - themeUseSystem: this.themeUseSystem, theme: this.theme } this._configService.config = newSettings diff --git a/webapp/frontend/src/app/layout/layout.component.ts b/webapp/frontend/src/app/layout/layout.component.ts index 602e62d..8196f09 100644 --- a/webapp/frontend/src/app/layout/layout.component.ts +++ b/webapp/frontend/src/app/layout/layout.component.ts @@ -23,6 +23,7 @@ export class LayoutComponent implements OnInit, OnDestroy // Private private _unsubscribeAll: Subject; + private systemPrefersDark: boolean; /** * Constructor @@ -43,6 +44,9 @@ export class LayoutComponent implements OnInit, OnDestroy { // Set the private defaults this._unsubscribeAll = new Subject(); + + this.systemPrefersDark = window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches; + } // ----------------------------------------------------------------------------------------------------- @@ -66,7 +70,7 @@ export class LayoutComponent implements OnInit, OnDestroy this.theme = config.theme; // Update the selected theme class name on body - const themeName = 'treo-theme-' + config.theme; + const themeName = 'treo-theme-' + this.determineTheme(config); this._document.body.classList.forEach((className) => { if ( className.startsWith('treo-theme-') && className !== themeName ) { @@ -105,6 +109,17 @@ export class LayoutComponent implements OnInit, OnDestroy // @ Private methods // ----------------------------------------------------------------------------------------------------- + /** + * Checks if theme should be set to dark based on config & system settings + */ + private determineTheme(config:AppConfig): string { + if (config.theme === 'system') { + return this.systemPrefersDark ? 'dark' : 'light' + } else { + return config.theme + } + } + /** * Update the selected layout */