mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-20 15:19:16 +00:00
* Fix system-to-dark theme race * Cover peer windows in theme race fix * Exercise production theme sync path * fix(settings): announce all changed appearance fields over IPC Theme sync only broadcast STORAGE_KEY_THEME, so receivers that hit the stale-storage race for UI theme or accent kept the old value. Emit a keyed notify for each appearance field that actually changed so the reducer race guard covers light/dark UI theme ids and accent state too. * fix(settings): keep current appearance fields across keyed IPC Sequential notifies for one multi-field appearance change were re-reading non-announced keys from still-stale storage, so a later UI-theme IPC could revert an earlier theme update. Preserve in-memory current values for non-matching keys, compose IPC applies via a ref, and cover the multi-notify sequence in tests.
177 lines
5.0 KiB
TypeScript
177 lines
5.0 KiB
TypeScript
import {
|
|
STORAGE_KEY_ACCENT_MODE,
|
|
STORAGE_KEY_COLOR,
|
|
STORAGE_KEY_THEME,
|
|
STORAGE_KEY_UI_THEME_DARK,
|
|
STORAGE_KEY_UI_THEME_LIGHT,
|
|
} from '../../infrastructure/config/storageKeys';
|
|
import {
|
|
isValidHslToken,
|
|
isValidTheme,
|
|
isValidUiThemeId,
|
|
} from './settingsStateDefaults';
|
|
|
|
export type AppearanceState = {
|
|
theme: "light" | "dark" | "system";
|
|
lightUiThemeId: string;
|
|
darkUiThemeId: string;
|
|
accentMode: "theme" | "custom";
|
|
customAccent: string;
|
|
};
|
|
|
|
export type AppearanceRenderSnapshot = {
|
|
theme: AppearanceState["theme"];
|
|
resolvedTheme: "light" | "dark";
|
|
} & Omit<AppearanceState, "theme">;
|
|
|
|
export type AppearanceSyncEvent = {
|
|
key: string;
|
|
value: unknown;
|
|
};
|
|
|
|
export type StoredAppearanceValues = {
|
|
theme: unknown;
|
|
lightUiThemeId: unknown;
|
|
darkUiThemeId: unknown;
|
|
accentMode: unknown;
|
|
customAccent: unknown;
|
|
};
|
|
|
|
export type AppearanceStorageEventResolution = {
|
|
handled: boolean;
|
|
next: AppearanceState;
|
|
};
|
|
|
|
export function hasPersistedAppearanceChanged(
|
|
previous: AppearanceRenderSnapshot,
|
|
current: AppearanceRenderSnapshot,
|
|
): boolean {
|
|
return previous.theme !== current.theme
|
|
|| previous.lightUiThemeId !== current.lightUiThemeId
|
|
|| previous.darkUiThemeId !== current.darkUiThemeId
|
|
|| previous.accentMode !== current.accentMode
|
|
|| previous.customAccent !== current.customAccent;
|
|
}
|
|
|
|
export function resolveIncomingAppearanceValue<T>(
|
|
incoming: AppearanceSyncEvent | undefined,
|
|
key: string,
|
|
storedValue: T,
|
|
currentValue: T,
|
|
isValid: (value: unknown) => value is T,
|
|
): T {
|
|
if (incoming?.key === key && isValid(incoming.value)) {
|
|
return incoming.value;
|
|
}
|
|
// Keyed IPC updates only trust the announced key. Non-matching fields keep
|
|
// the in-memory current value so sequential notifies for one multi-field
|
|
// change cannot clobber each other with a still-stale storage read.
|
|
// Full rehydrate (no incoming) continues to prefer shared storage.
|
|
if (incoming) {
|
|
return currentValue;
|
|
}
|
|
return storedValue;
|
|
}
|
|
|
|
export function resolveAppearanceSyncState(
|
|
current: AppearanceState,
|
|
stored: StoredAppearanceValues,
|
|
incoming?: AppearanceSyncEvent,
|
|
): AppearanceState {
|
|
const theme = resolveIncomingAppearanceValue(
|
|
incoming,
|
|
STORAGE_KEY_THEME,
|
|
stored.theme,
|
|
current.theme,
|
|
isValidTheme,
|
|
);
|
|
const lightUiThemeId = resolveIncomingAppearanceValue(
|
|
incoming,
|
|
STORAGE_KEY_UI_THEME_LIGHT,
|
|
stored.lightUiThemeId,
|
|
current.lightUiThemeId,
|
|
(value): value is string => typeof value === 'string' && isValidUiThemeId('light', value),
|
|
);
|
|
const darkUiThemeId = resolveIncomingAppearanceValue(
|
|
incoming,
|
|
STORAGE_KEY_UI_THEME_DARK,
|
|
stored.darkUiThemeId,
|
|
current.darkUiThemeId,
|
|
(value): value is string => typeof value === 'string' && isValidUiThemeId('dark', value),
|
|
);
|
|
const accentMode = resolveIncomingAppearanceValue(
|
|
incoming,
|
|
STORAGE_KEY_ACCENT_MODE,
|
|
stored.accentMode,
|
|
current.accentMode,
|
|
(value): value is AppearanceState['accentMode'] => value === 'theme' || value === 'custom',
|
|
);
|
|
const customAccent = resolveIncomingAppearanceValue(
|
|
incoming,
|
|
STORAGE_KEY_COLOR,
|
|
stored.customAccent,
|
|
current.customAccent,
|
|
(value): value is string => typeof value === 'string' && isValidHslToken(value),
|
|
);
|
|
|
|
return {
|
|
theme: isValidTheme(theme) ? theme : current.theme,
|
|
lightUiThemeId: typeof lightUiThemeId === 'string' && isValidUiThemeId('light', lightUiThemeId)
|
|
? lightUiThemeId
|
|
: current.lightUiThemeId,
|
|
darkUiThemeId: typeof darkUiThemeId === 'string' && isValidUiThemeId('dark', darkUiThemeId)
|
|
? darkUiThemeId
|
|
: current.darkUiThemeId,
|
|
accentMode: accentMode === 'theme' || accentMode === 'custom' ? accentMode : current.accentMode,
|
|
customAccent: typeof customAccent === 'string' && isValidHslToken(customAccent)
|
|
? customAccent.trim()
|
|
: current.customAccent,
|
|
};
|
|
}
|
|
|
|
export function resolveAppearanceStorageEvent(
|
|
current: AppearanceState,
|
|
key: string | null,
|
|
newValue: string | null,
|
|
): AppearanceStorageEventResolution {
|
|
if (key === STORAGE_KEY_THEME) {
|
|
return {
|
|
handled: true,
|
|
next: newValue && isValidTheme(newValue) ? { ...current, theme: newValue } : current,
|
|
};
|
|
}
|
|
if (key === STORAGE_KEY_UI_THEME_LIGHT) {
|
|
return {
|
|
handled: true,
|
|
next: newValue && isValidUiThemeId('light', newValue)
|
|
? { ...current, lightUiThemeId: newValue }
|
|
: current,
|
|
};
|
|
}
|
|
if (key === STORAGE_KEY_UI_THEME_DARK) {
|
|
return {
|
|
handled: true,
|
|
next: newValue && isValidUiThemeId('dark', newValue)
|
|
? { ...current, darkUiThemeId: newValue }
|
|
: current,
|
|
};
|
|
}
|
|
if (key === STORAGE_KEY_ACCENT_MODE) {
|
|
return {
|
|
handled: true,
|
|
next: newValue === 'theme' || newValue === 'custom'
|
|
? { ...current, accentMode: newValue }
|
|
: current,
|
|
};
|
|
}
|
|
if (key === STORAGE_KEY_COLOR) {
|
|
return {
|
|
handled: true,
|
|
next: newValue && isValidHslToken(newValue)
|
|
? { ...current, customAccent: newValue.trim() }
|
|
: current,
|
|
};
|
|
}
|
|
return { handled: false, next: current };
|
|
}
|