From 46c4bc1d78e671cd09d4d5b04adfe971d58e6337 Mon Sep 17 00:00:00 2001 From: piotrpekala7 <31202938+piotrpekala7@users.noreply.github.com> Date: Wed, 17 Feb 2021 04:03:02 +0100 Subject: [PATCH 1/4] Fix for theming --- angular.json | 11 +-- src/app/app.module.ts | 4 +- src/app/services/theme.service.ts | 47 ++++++----- src/theme-light.scss | 70 ---------------- src/theme.scss | 135 ++++++++++++++++++++++-------- 5 files changed, 128 insertions(+), 139 deletions(-) delete mode 100644 src/theme-light.scss diff --git a/angular.json b/angular.json index 6eebf3ab..3be730c1 100644 --- a/angular.json +++ b/angular.json @@ -47,16 +47,7 @@ "node_modules/bootstrap/dist/css/bootstrap.min.css", "node_modules/notosans-fontface/css/notosans-fontface.min.css", "src/styles.scss", - { - "inject": true, - "input": "src/theme.scss", - "bundleName": "theme-default-dark" - }, - { - "inject": true, - "input": "src/theme-light.scss", - "bundleName": "theme-default" - } + "src/theme.scss" ], "scripts": [] }, diff --git a/src/app/app.module.ts b/src/app/app.module.ts index af8366e4..8aafeb5e 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -284,6 +284,7 @@ import { UpdatesService } from './services/updates.service'; import { ReportIssueComponent } from './components/help/report-issue/report-issue.component'; import { AngularReactBrowserModule } from '@angular-react/core'; import { FabDialogModule, FabButtonModule } from '@angular-react/fabric'; +import { OverlayContainer, OverlayModule } from '@angular/cdk/overlay'; @NgModule({ declarations: [ @@ -492,7 +493,8 @@ import { FabDialogModule, FabButtonModule } from '@angular-react/fabric'; DragDropModule, NgxChildProcessModule, MATERIAL_IMPORTS, - NgCircleProgressModule.forRoot() + NgCircleProgressModule.forRoot(), + OverlayModule ], providers: [ SettingsService, diff --git a/src/app/services/theme.service.ts b/src/app/services/theme.service.ts index e7869ba2..c2cb934a 100644 --- a/src/app/services/theme.service.ts +++ b/src/app/services/theme.service.ts @@ -1,6 +1,7 @@ import { Injectable, RendererFactory2, Renderer2, Inject, EventEmitter } from '@angular/core'; import { Observable, BehaviorSubject, combineLatest } from 'rxjs'; import { DOCUMENT } from '@angular/common'; +import { OverlayContainer} from '@angular/cdk/overlay'; @Injectable({ providedIn: 'root' }) export class ThemeService { @@ -17,18 +18,19 @@ export class ThemeService { constructor( rendererFactory: RendererFactory2, + private overlayContainer: OverlayContainer, @Inject(DOCUMENT) document: Document ) { - this.head = document.head; - this._renderer = rendererFactory.createRenderer(null, null); - this.theme$ = combineLatest(this._mainTheme$, this._darkMode$); - this.theme$.subscribe(async ([mainTheme, darkMode]) => { - const cssExt = '.css'; - const cssFilename = darkMode ? mainTheme + '-dark' + cssExt : mainTheme + cssExt; - await this.loadCss(cssFilename); - if (this.themeLinks.length == 2) - this._renderer.removeChild(this.head, this.themeLinks.shift()); - }) + // this.head = document.head; + // this._renderer = rendererFactory.createRenderer(null, null); + // this.theme$ = combineLatest(this._mainTheme$, this._darkMode$); + // this.theme$.subscribe(async ([mainTheme, darkMode]) => { + // const cssExt = '.css'; + // const cssFilename = darkMode ? mainTheme + '-dark' + cssExt : mainTheme + cssExt; + // await this.loadCss(cssFilename); + // if (this.themeLinks.length == 2) + // this._renderer.removeChild(this.head, this.themeLinks.shift()); + // }) } getActualTheme() { @@ -40,17 +42,20 @@ export class ThemeService { } setDarkMode(value: boolean) { - this._darkMode$.next(value); - localStorage.removeItem('theme'); - if (value) { - this.savedTheme = 'dark'; - this.themeChanged.emit(this.savedTheme); - localStorage.setItem('theme', 'dark'); - } else { - this.savedTheme = 'light'; - this.themeChanged.emit(this.savedTheme); - localStorage.setItem('theme', 'light'); - } + if (value) this.overlayContainer.getContainerElement().classList.add('dark-theme'); + if (!value) this.overlayContainer.getContainerElement().classList.add('light-theme'); + + // this._darkMode$.next(value); + // localStorage.removeItem('theme'); + // if (value) { + // this.savedTheme = 'dark'; + // this.themeChanged.emit(this.savedTheme); + // localStorage.setItem('theme', 'dark'); + // } else { + // this.savedTheme = 'light'; + // this.themeChanged.emit(this.savedTheme); + // localStorage.setItem('theme', 'light'); + // } } private async loadCss(filename: string) { diff --git a/src/theme-light.scss b/src/theme-light.scss deleted file mode 100644 index 29585d89..00000000 --- a/src/theme-light.scss +++ /dev/null @@ -1,70 +0,0 @@ -@import '~@angular/material/theming'; -@import '~material-design-icons/iconfont/material-icons.css'; -@import '~typeface-roboto/index.css'; - -// Include non-theme styles for core. -@include mat-core(); - -// Define a theme. -$primary: mat-palette($mat-cyan, 700, 500, 900); -$accent: mat-palette($mat-blue, A200, A100, A700); - -$theme: mat-light-theme($primary, $accent); - -$light-palette: mat-palette($mat-blue, 900, A100, A400); -$light-background: mat-light-theme($light-palette, $primary); - -$mat-light-theme-background: ( - status-bar: black, - app-bar: map_get($mat-blue, 900), - background: white, - hover: rgba(white, 0.04), - card: white, - dialog: white, - disabled-button: $white-12-opacity, - raised-button: white, - focused-button: $white-6-opacity, - selected-button: map_get($mat-blue, 900), - selected-disabled-button: map_get($mat-blue, 800), - disabled-button-toggle: black, - unselected-chip: map_get($mat-blue, 700), - disabled-list-option: black -); - -$theme: map-merge( - $theme, - ( - background: $mat-light-theme-background - ) -); - -@include mat-core-theme($theme); -@include mat-autocomplete-theme($theme); -@include mat-button-theme($theme); -@include mat-button-toggle-theme($theme); -@include mat-card-theme($theme); -@include mat-checkbox-theme($theme); -@include mat-chips-theme($theme); -@include mat-table-theme($theme); -@include mat-datepicker-theme($theme); -@include mat-dialog-theme($theme); -@include mat-expansion-panel-theme($theme); -@include mat-form-field-theme($theme); -@include mat-grid-list-theme($theme); -@include mat-icon-theme($theme); -@include mat-input-theme($theme); -@include mat-list-theme($theme); -@include mat-menu-theme($theme); -@include mat-paginator-theme($theme); -@include mat-progress-bar-theme($theme); -@include mat-progress-spinner-theme($theme); -@include mat-radio-theme($theme); -@include mat-select-theme($theme); -@include mat-sidenav-theme($theme); -@include mat-slide-toggle-theme($theme); -@include mat-slider-theme($theme); -@include mat-stepper-theme($theme); -@include mat-tabs-theme($theme); -@include mat-toolbar-theme($light-background); -@include mat-tooltip-theme($theme); -@include mat-snack-bar-theme($theme); diff --git a/src/theme.scss b/src/theme.scss index fcd9f957..9ff7e407 100644 --- a/src/theme.scss +++ b/src/theme.scss @@ -1,16 +1,13 @@ @import '~@angular/material/theming'; @import '~material-design-icons/iconfont/material-icons.css'; @import '~typeface-roboto/index.css'; - -// Include non-theme styles for core. @include mat-core(); -// Define a theme. $primary: mat-palette($mat-cyan, 700, 500, 900); $accent: mat-palette($mat-blue-grey, A200, A100, A700); -$theme: mat-dark-theme($primary, $accent); - +// Dark theme +$dark-theme: mat-dark-theme($primary, $accent); $dark-palette: mat-palette($mat-blue-grey, 900, A100, A400); $dark-background: mat-dark-theme($dark-palette, $primary); @@ -31,40 +28,104 @@ $mat-dark-theme-background: ( disabled-list-option: black ); -$theme: map-merge( - $theme, +$dark-theme: map-merge( + $dark-theme, ( background: $mat-dark-theme-background ) ); -@include mat-core-theme($theme); -@include mat-autocomplete-theme($theme); -@include mat-button-theme($theme); -@include mat-button-toggle-theme($theme); -@include mat-card-theme($theme); -@include mat-checkbox-theme($theme); -@include mat-chips-theme($theme); -@include mat-table-theme($theme); -@include mat-datepicker-theme($theme); -@include mat-dialog-theme($theme); -@include mat-expansion-panel-theme($theme); -@include mat-form-field-theme($theme); -@include mat-grid-list-theme($theme); -@include mat-icon-theme($theme); -@include mat-input-theme($theme); -@include mat-list-theme($theme); -@include mat-menu-theme($theme); -@include mat-paginator-theme($theme); -@include mat-progress-bar-theme($theme); -@include mat-progress-spinner-theme($theme); -@include mat-radio-theme($theme); -@include mat-select-theme($theme); -@include mat-sidenav-theme($theme); -@include mat-slide-toggle-theme($theme); -@include mat-slider-theme($theme); -@include mat-stepper-theme($theme); -@include mat-tabs-theme($theme); -@include mat-toolbar-theme($dark-background); -@include mat-tooltip-theme($theme); -@include mat-snack-bar-theme($theme); +.dark-theme { + @include mat-core-theme($dark-theme); + @include mat-autocomplete-theme($dark-theme); + @include mat-button-theme($dark-theme); + @include mat-button-toggle-theme($dark-theme); + @include mat-card-theme($dark-theme); + @include mat-checkbox-theme($dark-theme); + @include mat-chips-theme($dark-theme); + @include mat-table-theme($dark-theme); + @include mat-datepicker-theme($dark-theme); + @include mat-dialog-theme($dark-theme); + @include mat-expansion-panel-theme($dark-theme); + @include mat-form-field-theme($dark-theme); + @include mat-grid-list-theme($dark-theme); + @include mat-icon-theme($dark-theme); + @include mat-input-theme($dark-theme); + @include mat-list-theme($dark-theme); + @include mat-menu-theme($dark-theme); + @include mat-paginator-theme($dark-theme); + @include mat-progress-bar-theme($dark-theme); + @include mat-progress-spinner-theme($dark-theme); + @include mat-radio-theme($dark-theme); + @include mat-select-theme($dark-theme); + @include mat-sidenav-theme($dark-theme); + @include mat-slide-toggle-theme($dark-theme); + @include mat-slider-theme($dark-theme); + @include mat-stepper-theme($dark-theme); + @include mat-tabs-theme($dark-theme); + @include mat-toolbar-theme($dark-background); + @include mat-tooltip-theme($dark-theme); + @include mat-snack-bar-theme($dark-theme); +} + +// Light theme +$light-theme: mat-light-theme($primary, $accent); +$light-palette: mat-palette($mat-blue, 900, A100, A400); +$light-background: mat-light-theme($light-palette, $primary); + +$mat-light-theme-background: ( + status-bar: black, + app-bar: map_get($mat-blue, 900), + background: white, + hover: rgba(white, 0.04), + card: white, + dialog: white, + disabled-button: $white-12-opacity, + raised-button: white, + focused-button: $white-6-opacity, + selected-button: map_get($mat-blue, 900), + selected-disabled-button: map_get($mat-blue, 800), + disabled-button-toggle: black, + unselected-chip: map_get($mat-blue, 700), + disabled-list-option: black +); + +$light-theme: map-merge( + $light-theme, + ( + background: $mat-light-theme-background + ) +); + +.light-theme { + @include mat-core-theme($light-theme); + @include mat-autocomplete-theme($light-theme); + @include mat-button-theme($light-theme); + @include mat-button-toggle-theme($light-theme); + @include mat-card-theme($light-theme); + @include mat-checkbox-theme($light-theme); + @include mat-chips-theme($light-theme); + @include mat-table-theme($light-theme); + @include mat-datepicker-theme($light-theme); + @include mat-dialog-theme($light-theme); + @include mat-expansion-panel-theme($light-theme); + @include mat-form-field-theme($light-theme); + @include mat-grid-list-theme($light-theme); + @include mat-icon-theme($light-theme); + @include mat-input-theme($light-theme); + @include mat-list-theme($light-theme); + @include mat-menu-theme($light-theme); + @include mat-paginator-theme($light-theme); + @include mat-progress-bar-theme($light-theme); + @include mat-progress-spinner-theme($light-theme); + @include mat-radio-theme($light-theme); + @include mat-select-theme($light-theme); + @include mat-sidenav-theme($light-theme); + @include mat-slide-toggle-theme($light-theme); + @include mat-slider-theme($light-theme); + @include mat-stepper-theme($light-theme); + @include mat-tabs-theme($light-theme); + @include mat-toolbar-theme($light-background); + @include mat-tooltip-theme($light-theme); + @include mat-snack-bar-theme($light-theme); +} From 91844ee7702d2c02cda3091643e1a093362b8dc4 Mon Sep 17 00:00:00 2001 From: piotrpekala7 <31202938+piotrpekala7@users.noreply.github.com> Date: Wed, 17 Feb 2021 16:28:00 +0100 Subject: [PATCH 2/4] Rewriting theme-service --- src/app/app.component.ts | 22 ++++-- .../notification-box.component.ts | 6 +- .../project-map/project-map.component.html | 2 +- .../project-map/project-map.component.scss | 4 ++ .../project-map/project-map.component.ts | 13 +++- src/app/services/theme.service.ts | 67 ++++--------------- 6 files changed, 49 insertions(+), 65 deletions(-) diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 401e4efa..ae1eba45 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, HostBinding, OnInit } from '@angular/core'; import { MatIconRegistry } from '@angular/material/icon'; import { DomSanitizer } from '@angular/platform-browser'; import { ElectronService } from 'ngx-electron'; @@ -6,6 +6,7 @@ import { SettingsService } from './services/settings.service'; import { ThemeService } from './services/theme.service'; import { Router, NavigationStart, NavigationEnd, NavigationCancel, NavigationError } from '@angular/router'; import { ProgressService } from './common/progress/progress.service'; +import { OverlayContainer} from '@angular/cdk/overlay'; @Component({ selector: 'app-root', @@ -14,6 +15,7 @@ import { ProgressService } from './common/progress/progress.service'; }) export class AppComponent implements OnInit { constructor( + private overlayContainer: OverlayContainer, iconReg: MatIconRegistry, sanitizer: DomSanitizer, private settingsService: SettingsService, @@ -30,18 +32,24 @@ export class AppComponent implements OnInit { }); } + @HostBinding('class') componentCssClass; + ngOnInit(): void { if (this.electronService.isElectronApp) { this.settingsService.subscribe(settings => { this.electronService.ipcRenderer.send('settings.changed', settings); }); } - let theme = localStorage.getItem('theme'); - if (theme === 'light') { - this.themeService.setDarkMode(false); - } else { - this.themeService.setDarkMode(true); - } + + this.applyTheme(this.themeService.savedTheme + '-theme'); + this.themeService.themeChanged.subscribe((event: string) => { + this.applyTheme(event); + }); + } + + applyTheme(theme: string) { + this.overlayContainer.getContainerElement().classList.add(theme); + this.componentCssClass = theme; } checkEvent(routerEvent) : void { diff --git a/src/app/components/notification-box/notification-box.component.ts b/src/app/components/notification-box/notification-box.component.ts index e2768f54..6814cf5c 100644 --- a/src/app/components/notification-box/notification-box.component.ts +++ b/src/app/components/notification-box/notification-box.component.ts @@ -46,7 +46,11 @@ export class NotificationBoxComponent implements OnInit, OnDestroy { // if (!this.location.path().includes('nodes') && !(adbutler == today)) this.startTimer(); if (!this.location.path().includes('nodes')) this.startTimer(); - this.themeService.getActualTheme() === 'light' ? this.isLightThemeEnabled = true : this.isLightThemeEnabled = false; + this.themeService.getActualTheme() === 'light' ? this.isLightThemeEnabled = true : this.isLightThemeEnabled = false; + + this.themeService.themeChanged.subscribe((value: string) => { + this.themeService.getActualTheme() === 'light' ? this.isLightThemeEnabled = true : this.isLightThemeEnabled = false; + }); } ngAfterViewInit() { diff --git a/src/app/components/project-map/project-map.component.html b/src/app/components/project-map/project-map.component.html index 44d2b469..60a8942b 100644 --- a/src/app/components/project-map/project-map.component.html +++ b/src/app/components/project-map/project-map.component.html @@ -172,7 +172,7 @@
- +