Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: show toast before applying system color scheme #964

Merged
merged 19 commits into from
Apr 11, 2022
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 78 additions & 41 deletions app/assets/javascripts/services/themeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ import {
CreateDecryptedLocalStorageContextPayload,
InternalEventBus,
} from '@standardnotes/snjs';
import {
dismissToast,
ToastType,
addTimedToast,
} from '@standardnotes/stylekit';

const CACHED_THEMES_KEY = 'cachedThemes';
const CachedThemesKey = 'cachedThemes';
const TimeBeforeApplyingColorScheme = 5;
const DefaultThemeIdentifier = 'Default';

export class ThemeManager extends ApplicationService {
private activeThemes: UuidString[] = [];
Expand All @@ -39,7 +46,7 @@ export class ThemeManager extends ApplicationService {
this.deactivateAllThemes();
this.activeThemes = [];
this.application?.removeValue(
CACHED_THEMES_KEY,
CachedThemesKey,
StorageValueModes.Nonwrapped
);
break;
Expand Down Expand Up @@ -71,19 +78,17 @@ export class ThemeManager extends ApplicationService {
false
);

if (useDeviceThemeSettings === this.lastUseDeviceThemeSettings) {
return;
if (useDeviceThemeSettings !== this.lastUseDeviceThemeSettings) {
this.lastUseDeviceThemeSettings = useDeviceThemeSettings;
}

this.lastUseDeviceThemeSettings = useDeviceThemeSettings;
if (useDeviceThemeSettings) {
const prefersDarkColorScheme = window.matchMedia(
'(prefers-color-scheme: dark)'
);

const prefersDarkColorScheme = window.matchMedia(
'(prefers-color-scheme: dark)'
);
this.setThemeAsPerColorScheme(
useDeviceThemeSettings,
prefersDarkColorScheme.matches
);
this.setThemeAsPerColorScheme(prefersDarkColorScheme.matches);
}
}

get webApplication() {
Expand Down Expand Up @@ -141,37 +146,59 @@ export class ThemeManager extends ApplicationService {
}

private colorSchemeEventHandler(event: MediaQueryListEvent) {
this.setThemeAsPerColorScheme(
this.lastUseDeviceThemeSettings,
event.matches
this.setThemeAsPerColorScheme(event.matches);
}

private showColorSchemeToast(setThemeCallback: () => void) {
const [toastId, intervalId] = addTimedToast(
{
type: ToastType.Regular,
message: (timeRemaining) =>
`Applying system color scheme in ${timeRemaining}s...`,
actions: [
{
label: 'Keep current theme',
handler: () => {
dismissToast(toastId);
clearInterval(intervalId);
},
},
{
label: 'Apply now',
handler: () => {
dismissToast(toastId);
clearInterval(intervalId);
setThemeCallback();
},
},
],
},
setThemeCallback,
TimeBeforeApplyingColorScheme
);
}

private setThemeAsPerColorScheme(
useDeviceThemeSettings: boolean,
prefersDarkColorScheme: boolean
) {
if (useDeviceThemeSettings) {
const preference = prefersDarkColorScheme
? PrefKey.AutoDarkThemeIdentifier
: PrefKey.AutoLightThemeIdentifier;
const themes = this.application.items.getDisplayableItems<SNTheme>(
ContentType.Theme
);
private setThemeAsPerColorScheme(prefersDarkColorScheme: boolean) {
const preference = prefersDarkColorScheme
? PrefKey.AutoDarkThemeIdentifier
: PrefKey.AutoLightThemeIdentifier;

const enableDefaultTheme = () => {
const activeTheme = themes.find(
(theme) => theme.active && !theme.isLayerable()
);
if (activeTheme) this.application.mutator.toggleTheme(activeTheme);
};

const themeIdentifier = this.application.getPreference(
preference,
'Default'
) as string;
if (themeIdentifier === 'Default') {
enableDefaultTheme();
const themes = this.application.items.getDisplayableItems(
ContentType.Theme
) as SNTheme[];

const activeTheme = themes.find(
(theme) => theme.active && !theme.isLayerable()
);

const themeIdentifier = this.application.getPreference(
preference,
DefaultThemeIdentifier
) as string;

const setTheme = () => {
if (themeIdentifier === DefaultThemeIdentifier && activeTheme) {
this.application.mutator.toggleTheme(activeTheme);
} else {
const theme = themes.find(
(theme) => theme.package_info.identifier === themeIdentifier
Expand All @@ -180,6 +207,16 @@ export class ThemeManager extends ApplicationService {
this.application.mutator.toggleTheme(theme);
}
}
};

const isPreferredThemeNotActive =
activeTheme?.identifier !== themeIdentifier;

const isDefaultThemePreferredAndNotActive =
themeIdentifier === DefaultThemeIdentifier && activeTheme;

if (isPreferredThemeNotActive || isDefaultThemePreferredAndNotActive) {
this.showColorSchemeToast(setTheme);
}
}

Expand Down Expand Up @@ -279,15 +316,15 @@ export class ThemeManager extends ApplicationService {
});

return this.application.setValue(
CACHED_THEMES_KEY,
CachedThemesKey,
mapped,
StorageValueModes.Nonwrapped
);
}

private async getCachedThemes() {
const cachedThemes = (await this.application.getValue(
CACHED_THEMES_KEY,
CachedThemesKey,
StorageValueModes.Nonwrapped
)) as SNTheme[];
if (cachedThemes) {
Expand Down