mirror of
https://github.com/bitwarden/browser
synced 2025-12-11 05:43:41 +00:00
* refactor: update clientType enum * refactor: update deviceType filename * refactor: update encryptedExportType filename * refactor: update encryptionType filename * refactor: update eventType filename * refactor: update fieldType filename * refactor: update fileUploadType filename * refactor: update hashPurpose filename * refactor: update htmlStorageLocation filename * refactor: update kdfType filename * refactor: update keySuffixOptions filename * refactor: update linkedIdType filename * refactor: update logLevelType filename * refactor: update nativeMessagingVersion filename * refactor: update notificationType filename * refactor: update productType filename * refactor: update secureNoteType filename * refactor: update stateVersion filename * refactor: update storageLocation filename * refactor: update themeType filename * refactor: update uriMatchType filename * fix: update kdfType classes missed in initial pass, refs AC-1266 * fix: missing import update for device-type * refactor: add barrel file for enums and update pathed import statements, refs AC-1266 * fix: incorrect import statements for web, refs AC-1266 * fix: missed import statement updates (browser), refs AC-1266 * fix: missed import statement changes (cli), refs AC-1266 * fix: missed import statement changes (desktop), refs AC-1266 * fix: prettier, refs AC-1266 * refactor: (libs) update relative paths to use barrel file, refs AC-1266 * fix: missed find/replace import statements for SecureNoteType, refs AC-1266 * refactor: apply .enum suffix to enums folder and modify leftover relative paths, refs AC-1266 * fix: find/replace errors for native-messaging-version, refs AC-1266
73 lines
2.6 KiB
TypeScript
73 lines
2.6 KiB
TypeScript
import { DOCUMENT } from "@angular/common";
|
|
import { Inject, Injectable } from "@angular/core";
|
|
import { BehaviorSubject, filter, fromEvent, Observable } from "rxjs";
|
|
|
|
import { StateService } from "@bitwarden/common/abstractions/state.service";
|
|
import { ThemeType } from "@bitwarden/common/enums";
|
|
|
|
import { WINDOW } from "../injection-tokens";
|
|
|
|
import { Theme } from "./theme";
|
|
import { ThemeBuilder } from "./themeBuilder";
|
|
import { AbstractThemingService } from "./theming.service.abstraction";
|
|
|
|
@Injectable()
|
|
export class ThemingService implements AbstractThemingService {
|
|
private _theme = new BehaviorSubject<ThemeBuilder | null>(null);
|
|
theme$: Observable<Theme> = this._theme.pipe(filter((x) => x !== null));
|
|
|
|
constructor(
|
|
private stateService: StateService,
|
|
@Inject(WINDOW) private window: Window,
|
|
@Inject(DOCUMENT) private document: Document
|
|
) {
|
|
this.monitorThemeChanges();
|
|
}
|
|
|
|
async monitorThemeChanges(): Promise<void> {
|
|
this._theme.next(
|
|
new ThemeBuilder(await this.stateService.getTheme(), await this.getSystemTheme())
|
|
);
|
|
this.monitorConfiguredThemeChanges();
|
|
this.monitorSystemThemeChanges();
|
|
}
|
|
|
|
updateSystemTheme(systemTheme: ThemeType): void {
|
|
this._theme.next(this._theme.getValue().updateSystemTheme(systemTheme));
|
|
}
|
|
|
|
async updateConfiguredTheme(theme: ThemeType): Promise<void> {
|
|
await this.stateService.setTheme(theme);
|
|
this._theme.next(this._theme.getValue().updateConfiguredTheme(theme));
|
|
}
|
|
|
|
protected monitorConfiguredThemeChanges(): void {
|
|
this.theme$.subscribe((theme: Theme) => {
|
|
this.document.documentElement.classList.remove(
|
|
"theme_" + ThemeType.Light,
|
|
"theme_" + ThemeType.Dark,
|
|
"theme_" + ThemeType.Nord,
|
|
"theme_" + ThemeType.SolarizedDark
|
|
);
|
|
this.document.documentElement.classList.add("theme_" + theme.effectiveTheme);
|
|
});
|
|
}
|
|
|
|
// We use a media match query for monitoring the system theme on web and browser, but this doesn't work for electron apps on Linux.
|
|
// In desktop we override these methods to track systemTheme with the electron renderer instead, which works for all OSs.
|
|
protected async getSystemTheme(): Promise<ThemeType> {
|
|
return this.window.matchMedia("(prefers-color-scheme: dark)").matches
|
|
? ThemeType.Dark
|
|
: ThemeType.Light;
|
|
}
|
|
|
|
protected monitorSystemThemeChanges(): void {
|
|
fromEvent<MediaQueryListEvent>(
|
|
this.window.matchMedia("(prefers-color-scheme: dark)"),
|
|
"change"
|
|
).subscribe((event) => {
|
|
this.updateSystemTheme(event.matches ? ThemeType.Dark : ThemeType.Light);
|
|
});
|
|
}
|
|
}
|