feat(ui): OLED dark mode variant (#1560)
This commit is contained in:
@@ -67,6 +67,7 @@ describe('AppConfigService', () => {
|
||||
vi.spyOn(window, 'getComputedStyle').mockReturnValue(computedStyle);
|
||||
root.classList.remove('dark');
|
||||
delete root.dataset['appTheme'];
|
||||
delete root.dataset['oledDarkMode'];
|
||||
|
||||
TestBed.resetTestingModule();
|
||||
TestBed.configureTestingModule({
|
||||
@@ -84,6 +85,7 @@ describe('AppConfigService', () => {
|
||||
rootStyle.cssText = '';
|
||||
root.classList.remove('dark');
|
||||
delete root.dataset['appTheme'];
|
||||
delete root.dataset['oledDarkMode'];
|
||||
vi.restoreAllMocks();
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
@@ -93,6 +95,7 @@ describe('AppConfigService', () => {
|
||||
themePreference: 'grimmory',
|
||||
appearancePreference: 'system',
|
||||
customPrimary: 'orange',
|
||||
oledDarkMode: false,
|
||||
});
|
||||
expect(localStorageMock.getItem('appConfigState')).toBeNull();
|
||||
expect(root.dataset['appTheme']).toBe('grimmory');
|
||||
@@ -145,6 +148,7 @@ describe('AppConfigService', () => {
|
||||
themePreference: 'grimmory',
|
||||
appearancePreference: 'system',
|
||||
customPrimary: 'orange',
|
||||
oledDarkMode: false,
|
||||
});
|
||||
expect(root.dataset['appTheme']).toBe('grimmory');
|
||||
expect(root.classList.contains('dark')).toBe(false);
|
||||
@@ -152,6 +156,23 @@ describe('AppConfigService', () => {
|
||||
themePreference: 'grimmory',
|
||||
appearancePreference: 'system',
|
||||
customPrimary: 'orange',
|
||||
oledDarkMode: false,
|
||||
}));
|
||||
});
|
||||
|
||||
it('applies OLED mode only when the effective appearance is dark', () => {
|
||||
service.setOledDarkMode(true);
|
||||
|
||||
expect(root.dataset['oledDarkMode']).toBeUndefined();
|
||||
|
||||
service.setAppearancePreference('dark');
|
||||
|
||||
expect(root.classList.contains('dark')).toBe(true);
|
||||
expect(root.dataset['oledDarkMode']).toBe('true');
|
||||
|
||||
service.setAppearancePreference('light');
|
||||
|
||||
expect(root.classList.contains('dark')).toBe(false);
|
||||
expect(root.dataset['oledDarkMode']).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -22,6 +22,8 @@ type StoredAppState = Partial<AppState> & {
|
||||
surface?: unknown;
|
||||
};
|
||||
|
||||
type AppStatePatch = Partial<AppState>;
|
||||
|
||||
interface LoadedAppState {
|
||||
state: AppState;
|
||||
shouldPersist: boolean;
|
||||
@@ -33,8 +35,10 @@ interface LoadedAppState {
|
||||
export class AppConfigService {
|
||||
private readonly STORAGE_KEY = 'appConfigState';
|
||||
readonly themes = APP_THEME_OPTIONS;
|
||||
private readonly appStateSignal = signal<AppState>({});
|
||||
private readonly appStateSignal = signal<AppState>(this.withDefaults({}));
|
||||
private readonly effectiveAppearanceSignal = signal<'light' | 'dark'>('dark');
|
||||
readonly appState = this.appStateSignal.asReadonly();
|
||||
readonly effectiveAppearance = this.effectiveAppearanceSignal.asReadonly();
|
||||
document = inject(DOCUMENT);
|
||||
platformId = inject(PLATFORM_ID);
|
||||
faviconService = inject(FaviconService);
|
||||
@@ -61,7 +65,11 @@ export class AppConfigService {
|
||||
this.updateAppState({appearancePreference});
|
||||
}
|
||||
|
||||
private updateAppState(patch: AppState): void {
|
||||
setOledDarkMode(oledDarkMode: boolean): void {
|
||||
this.updateAppState({oledDarkMode});
|
||||
}
|
||||
|
||||
private updateAppState(patch: AppStatePatch): void {
|
||||
const state = this.withDefaults({
|
||||
...this.appStateSignal(),
|
||||
...patch,
|
||||
@@ -103,6 +111,7 @@ export class AppConfigService {
|
||||
themePreference: state.themePreference,
|
||||
appearancePreference: state.appearancePreference,
|
||||
customPrimary: state.customPrimary,
|
||||
oledDarkMode: state.oledDarkMode,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -110,22 +119,23 @@ export class AppConfigService {
|
||||
return 'preset' in state || 'primary' in state || 'surface' in state;
|
||||
}
|
||||
|
||||
private withDefaults(state: AppState): AppState {
|
||||
private withDefaults(state: AppStatePatch): AppState {
|
||||
return {
|
||||
themePreference: this.resolveThemePreference(state.themePreference),
|
||||
appearancePreference: this.resolveAppearancePreference(state.appearancePreference),
|
||||
customPrimary: this.resolveCustomPrimary(state.customPrimary),
|
||||
oledDarkMode: state.oledDarkMode === true,
|
||||
};
|
||||
}
|
||||
|
||||
private resolveCustomPrimary(customPrimary: AppState['customPrimary']): CustomPrimary {
|
||||
private resolveCustomPrimary(customPrimary: AppStatePatch['customPrimary']): CustomPrimary {
|
||||
if (customPrimary && CUSTOM_PRIMARY_OPTIONS.includes(customPrimary)) {
|
||||
return customPrimary;
|
||||
}
|
||||
return DEFAULT_CUSTOM_PRIMARY;
|
||||
}
|
||||
|
||||
private resolveThemePreference(themePreference: AppState['themePreference']): AppTheme {
|
||||
private resolveThemePreference(themePreference: AppStatePatch['themePreference']): AppTheme {
|
||||
if (themePreference && this.themes.some((option) => option.name === themePreference)) {
|
||||
return themePreference;
|
||||
}
|
||||
@@ -133,7 +143,7 @@ export class AppConfigService {
|
||||
return DEFAULT_APP_THEME;
|
||||
}
|
||||
|
||||
private resolveAppearancePreference(appearancePreference: AppState['appearancePreference']): AppearancePreference {
|
||||
private resolveAppearancePreference(appearancePreference: AppStatePatch['appearancePreference']): AppearancePreference {
|
||||
if (appearancePreference === 'light' || appearancePreference === 'dark' || appearancePreference === 'system') {
|
||||
return appearancePreference;
|
||||
}
|
||||
@@ -152,12 +162,12 @@ export class AppConfigService {
|
||||
|
||||
private saveAppState(state: AppState): void {
|
||||
if (isPlatformBrowser(this.platformId)) {
|
||||
localStorage.setItem(this.STORAGE_KEY, JSON.stringify(this.withDefaults(state)));
|
||||
localStorage.setItem(this.STORAGE_KEY, JSON.stringify(state));
|
||||
}
|
||||
}
|
||||
|
||||
applyCurrentTheme(): void {
|
||||
this.applyAppState(this.withDefaults(this.appStateSignal()));
|
||||
this.applyAppState(this.appStateSignal());
|
||||
}
|
||||
|
||||
private applyAppState(state: AppState): void {
|
||||
@@ -177,8 +187,14 @@ export class AppConfigService {
|
||||
const appearancePreference = this.resolveAppearancePreference(state.appearancePreference);
|
||||
const effective = this.effectiveAppearancePreference(appearancePreference);
|
||||
|
||||
this.effectiveAppearanceSignal.set(effective);
|
||||
root.dataset['appTheme'] = theme;
|
||||
root.classList.toggle('dark', effective === 'dark');
|
||||
if (state.oledDarkMode && effective === 'dark') {
|
||||
root.dataset['oledDarkMode'] = 'true';
|
||||
} else {
|
||||
delete root.dataset['oledDarkMode'];
|
||||
}
|
||||
root.style.setProperty('color-scheme', effective);
|
||||
this.applyCustomPrimary(root, theme, this.resolveCustomPrimary(state.customPrimary));
|
||||
this.syncSystemSchemeListener(appearancePreference);
|
||||
|
||||
Reference in New Issue
Block a user