2020-11-24 19:18:59 +00:00
|
|
|
import { LitElement } from 'lit-element';
|
2020-11-23 20:41:26 +00:00
|
|
|
import { DomTools } from './domtools.classes.domtools';
|
|
|
|
import * as plugins from './domtools.plugins';
|
|
|
|
|
|
|
|
export class ThemeManager {
|
|
|
|
public domtoolsRef: DomTools;
|
2020-11-24 19:18:59 +00:00
|
|
|
|
2020-11-24 19:48:14 +00:00
|
|
|
public goBrightBoolean = false;
|
2020-11-25 15:42:55 +00:00
|
|
|
public preferredColorSchemeMediaMatch = window.matchMedia('(prefers-color-scheme: light)');
|
2020-11-23 20:41:26 +00:00
|
|
|
|
|
|
|
constructor(domtoolsRefArg: DomTools) {
|
|
|
|
this.domtoolsRef = domtoolsRefArg;
|
2020-11-25 15:42:55 +00:00
|
|
|
|
|
|
|
// lets take care of elements being added that need to know of the current theme
|
2020-11-24 19:18:59 +00:00
|
|
|
this.domtoolsRef.elementInstrumenter.connectedElements.eventSubject.subscribe(async eventData => {
|
|
|
|
await this.setThemeStatusForElement(eventData.payload, this.goBrightBoolean);
|
|
|
|
});
|
2020-11-25 15:42:55 +00:00
|
|
|
|
|
|
|
// lets care
|
|
|
|
this.goBrightBoolean = this.preferredColorSchemeMediaMatch.matches;
|
|
|
|
this.preferredColorSchemeMediaMatch.addEventListener('change', eventArg => {
|
|
|
|
this.goBrightBoolean = eventArg.matches;
|
|
|
|
this.updateAllConnectedElements();
|
|
|
|
});
|
2020-11-24 19:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private async setThemeStatusForElement (elementArg: LitElement, goBrightBool: boolean) {
|
|
|
|
const goBright = (elementArg as any).goBright;
|
|
|
|
if (typeof goBright === 'boolean') {
|
|
|
|
(elementArg as any).goBright = goBrightBool;
|
|
|
|
}
|
2020-11-23 20:41:26 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 19:23:06 +00:00
|
|
|
private async updateAllConnectedElements() {
|
2020-11-30 20:03:05 +00:00
|
|
|
document.body.style.background = this.goBrightBoolean ? '#fff' : '#000';
|
2020-11-23 20:41:26 +00:00
|
|
|
this.domtoolsRef.elementInstrumenter.forEachelement(async elementArg => {
|
2020-11-24 19:23:06 +00:00
|
|
|
await this.setThemeStatusForElement(elementArg, this.goBrightBoolean);
|
2020-11-23 20:41:26 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-24 19:23:06 +00:00
|
|
|
public goBright() {
|
|
|
|
this.goBrightBoolean = true;
|
|
|
|
this.updateAllConnectedElements();
|
|
|
|
}
|
|
|
|
|
2020-11-23 20:41:26 +00:00
|
|
|
public goDark() {
|
2020-11-24 19:23:06 +00:00
|
|
|
this.goBrightBoolean = false;
|
|
|
|
this.updateAllConnectedElements();
|
|
|
|
}
|
|
|
|
|
|
|
|
public toggleDarkBright() {
|
|
|
|
this.goBrightBoolean = !this.goBrightBoolean;
|
|
|
|
this.updateAllConnectedElements();
|
2020-11-23 20:41:26 +00:00
|
|
|
}
|
|
|
|
}
|