Release candidate a7: dev 6524621e into main #175
14 changed files with 207 additions and 162 deletions
|
|
@ -6,14 +6,14 @@ target = "ratatoskr"
|
|||
# tarball was built from, which is not this commit.
|
||||
source_stamp_environment = "RATATOSKR_BUILD_COMMIT"
|
||||
|
||||
# Release dist f562f8fae9a294e04354801538ed7a595133f480: the output of build-prod-dist.sh (live config check passed), packed
|
||||
# Release dist d62be29fd4b245bd1303454022ace86c4f762d02: the output of build-prod-dist.sh (live config check passed), packed
|
||||
# as dist/ at the top of one gzip tar. The tree carries the repo's own config.json; what is served is the operator's
|
||||
# carried file, which Idunn copies beside dist/ and the vhost aliases over the tree's (a vhost without that alias would
|
||||
# serve the repo config).
|
||||
[[external_inputs]]
|
||||
id = "dist-tarball"
|
||||
url = "https://eureka.gamecult.org/forge/GameCult/Ratatoskr/releases/download/dist-f562f8fa/ratatoskr-f562f8fa-dist.tar.gz"
|
||||
sha256 = "ec4f4fc3e64dfeb7ba6509b386d2a5efc318211f66de663c061e1240b1f610f0"
|
||||
url = "https://eureka.gamecult.org/forge/GameCult/Ratatoskr/releases/download/dist-d62be29f/ratatoskr-d62be29f-dist.tar.gz"
|
||||
sha256 = "dd04634f82d081004941c71545ba43b8d5c40dbf97fb5643d9f4f24940b6afe8"
|
||||
runner = "unpack"
|
||||
destination = "release/dist.tar.gz"
|
||||
|
||||
|
|
|
|||
|
|
@ -2,13 +2,11 @@ import React, { useEffect, useRef } from 'react';
|
|||
import { useClientConfig } from '../../hooks/useClientConfig';
|
||||
import { getFallbackSession } from '../../state/sessions';
|
||||
import { mountBackgroundScene } from './host';
|
||||
import { setSceneDrawn } from './sceneDrawn';
|
||||
import { readCapabilities, resolveTier } from './tier';
|
||||
import './sceneShell.css';
|
||||
import './sceneButtons.css';
|
||||
|
||||
/** Set on <html> while a scene runs, so signed-out screens can let it show through. */
|
||||
export const SCENE_ACTIVE_ATTRIBUTE = 'data-scene';
|
||||
|
||||
/**
|
||||
* The one host for the background scene, mounted once at the app root. Fixed behind everything
|
||||
* (z-index -1 sits above the body's background and below all normal content; the signed-out
|
||||
|
|
@ -24,17 +22,16 @@ export function BackgroundScene() {
|
|||
useEffect(() => {
|
||||
const canvas = canvasRef.current;
|
||||
if (!enabled || !canvas) return undefined;
|
||||
const root = document.documentElement;
|
||||
const dispose = mountBackgroundScene(config, readCapabilities(navigator), canvas, {
|
||||
loggedIn: !!getFallbackSession(),
|
||||
onActive: () => root.setAttribute(SCENE_ACTIVE_ATTRIBUTE, ''),
|
||||
onInactive: () => root.removeAttribute(SCENE_ACTIVE_ATTRIBUTE),
|
||||
onActive: () => setSceneDrawn(true),
|
||||
onInactive: () => setSceneDrawn(false),
|
||||
// eslint-disable-next-line no-console
|
||||
onFail: (e) => console.warn('Background scene unavailable:', e),
|
||||
});
|
||||
return () => {
|
||||
dispose();
|
||||
root.removeAttribute(SCENE_ACTIVE_ATTRIBUTE);
|
||||
setSceneDrawn(false);
|
||||
};
|
||||
}, [config, enabled]);
|
||||
|
||||
|
|
|
|||
37
src/app/features/scene/sceneDrawn.test.ts
Normal file
37
src/app/features/scene/sceneDrawn.test.ts
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import {
|
||||
SCENE_ACTIVE_ATTRIBUTE,
|
||||
getSceneDrawn,
|
||||
setSceneDrawn,
|
||||
subscribeSceneDrawn,
|
||||
} from './sceneDrawn';
|
||||
|
||||
const attrs = new Set<string>();
|
||||
vi.stubGlobal('document', {
|
||||
documentElement: {
|
||||
setAttribute: (n: string) => attrs.add(n),
|
||||
removeAttribute: (n: string) => attrs.delete(n),
|
||||
},
|
||||
});
|
||||
|
||||
afterEach(() => setSceneDrawn(false));
|
||||
|
||||
describe('sceneDrawn: one store owns the flag and html[data-scene]', () => {
|
||||
it('starts off, and on/off move the flag, the attribute and the subscribers together', () => {
|
||||
const cb = vi.fn();
|
||||
const off = subscribeSceneDrawn(cb);
|
||||
expect(getSceneDrawn()).toBe(false);
|
||||
setSceneDrawn(true);
|
||||
expect(getSceneDrawn()).toBe(true);
|
||||
expect(attrs.has(SCENE_ACTIVE_ATTRIBUTE)).toBe(true);
|
||||
setSceneDrawn(true);
|
||||
expect(cb).toHaveBeenCalledTimes(1);
|
||||
setSceneDrawn(false);
|
||||
expect(getSceneDrawn()).toBe(false);
|
||||
expect(attrs.has(SCENE_ACTIVE_ATTRIBUTE)).toBe(false);
|
||||
expect(cb).toHaveBeenCalledTimes(2);
|
||||
off();
|
||||
setSceneDrawn(true);
|
||||
expect(cb).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
});
|
||||
29
src/app/features/scene/sceneDrawn.ts
Normal file
29
src/app/features/scene/sceneDrawn.ts
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
// Whether a background scene is drawing a sky right now. BackgroundScene is the only writer: it sets this when the
|
||||
// ridge scene starts, and clears it when the scene stops, fails to start, or loses its WebGL context (a restored
|
||||
// context sets it again). This store also owns html[data-scene], so the attribute the CSS reads and the flag the theme
|
||||
// hooks read cannot disagree. The configured tier only says a scene may run; this says one does.
|
||||
|
||||
/** Set on <html> while a scene is drawn, so signed-out screens can let it show through. */
|
||||
export const SCENE_ACTIVE_ATTRIBUTE = 'data-scene';
|
||||
|
||||
let drawn = false;
|
||||
const listeners = new Set<() => void>();
|
||||
|
||||
export const getSceneDrawn = (): boolean => drawn;
|
||||
|
||||
export const subscribeSceneDrawn = (cb: () => void): (() => void) => {
|
||||
listeners.add(cb);
|
||||
return () => {
|
||||
listeners.delete(cb);
|
||||
};
|
||||
};
|
||||
|
||||
export function setSceneDrawn(on: boolean): void {
|
||||
if (typeof document !== 'undefined') {
|
||||
if (on) document.documentElement.setAttribute(SCENE_ACTIVE_ATTRIBUTE, '');
|
||||
else document.documentElement.removeAttribute(SCENE_ACTIVE_ATTRIBUTE);
|
||||
}
|
||||
if (on === drawn) return;
|
||||
drawn = on;
|
||||
[...listeners].forEach((cb) => cb());
|
||||
}
|
||||
|
|
@ -136,29 +136,18 @@ describe('userColorInk', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('sceneInk: an explicit theme wins over the auto ink', () => {
|
||||
it('is the sky state and viewport only while the sky theme is worn and a scene runs', () => {
|
||||
// auto, scene on, no theme of the person's own: the sky theme is worn.
|
||||
expect(sceneInk(sceneThemeId('auto', 'dusk', true, false), true, 'dusk', true)).toEqual({
|
||||
describe('sceneInk: the drawn sky decides the ink', () => {
|
||||
it('is the sky state and viewport while a scene runs, and nothing without one', () => {
|
||||
expect(sceneInk(sceneThemeId('dusk', true), true, 'dusk', true)).toEqual({
|
||||
state: 'dusk',
|
||||
phone: true,
|
||||
});
|
||||
// auto, but the person chose a theme: it wins, the theme's own ink stands.
|
||||
expect(sceneInk(sceneThemeId('auto', 'dusk', true, true), true, 'dusk', true)).toBeUndefined();
|
||||
// a fixed sky state does not override the person's theme either: the theme's own ink stands.
|
||||
expect(
|
||||
sceneInk(sceneThemeId('night', 'night', true, true), true, 'night', false)
|
||||
).toBeUndefined();
|
||||
// a fixed sky state with no theme of the person's own: the sky theme's ink applies.
|
||||
expect(sceneInk(sceneThemeId('night', 'night', true, false), true, 'night', false)).toEqual({
|
||||
expect(sceneInk(sceneThemeId('night', true), true, 'night', false)).toEqual({
|
||||
state: 'night',
|
||||
phone: false,
|
||||
});
|
||||
// no scene behind the page: nothing for the ink to be measured against.
|
||||
expect(
|
||||
sceneInk(sceneThemeId('auto', 'day', false, false), false, 'day', false)
|
||||
).toBeUndefined();
|
||||
expect(sceneInk(sceneThemeId('day', 'day', true, false), false, 'day', false)).toBeUndefined();
|
||||
// no scene behind the page: the person's theme and its ink stand.
|
||||
expect(sceneInk(sceneThemeId('day', false), false, 'day', false)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@
|
|||
// not a GPU; ratatoskr-sidebar-contrast-20261110/results.md, T3). These are the scene's own patches, not
|
||||
// the bare sky: the terrain under the timeline is what the ink sits on, and at night its snow (0.103, phone 0.149) is
|
||||
// lighter than the sky was when these were measured (0.0816).
|
||||
// Only a person whose theme is the sky's (no explicit theme of their own, sceneThemeId) wears this ink.
|
||||
// The page wears this ink whenever the drawn sky decides its theme (a scene is on, sceneThemeId).
|
||||
import chroma from 'chroma-js';
|
||||
import { AA, luminance } from '../../../sky/skyTokens.js';
|
||||
import { baked } from '../../../sky/baked';
|
||||
|
|
@ -25,8 +25,8 @@ export interface SkyInk {
|
|||
}
|
||||
|
||||
/**
|
||||
* The ink in force: only while the page wears the sky's theme (`themeId`, from sceneThemeId, so a person's explicit
|
||||
* theme, which that function lets win over 'auto', keeps its own ink) and a scene runs behind it. Otherwise undefined.
|
||||
* The ink in force: only while the page wears the sky's theme (`themeId`, from sceneThemeId, defined only while a
|
||||
* scene is on) and a scene runs behind it. With no scene the person's theme and its own ink stand: undefined.
|
||||
*/
|
||||
export const sceneInk = (
|
||||
themeId: SkyThemeId | undefined,
|
||||
|
|
|
|||
|
|
@ -14,53 +14,36 @@ import {
|
|||
setSkyClock,
|
||||
subscribe as subscribeSky,
|
||||
} from './skyState';
|
||||
import { sceneThemeId, skyThemeId } from './skyTheme';
|
||||
import { sceneThemeId } from './skyTheme';
|
||||
|
||||
const SKY = { 'midnight-sky': baked.midnight, 'noonday-sky': baked.noonday } as const;
|
||||
const r2 = (n: number) => n.toFixed(2);
|
||||
const fixed = (['day', 'dusk', 'night'] as const).flatMap((s) => {
|
||||
const id = skyThemeId(s);
|
||||
return id ? [[s, id] as const] : [];
|
||||
const fixed = (['day', 'dusk', 'night'] as const).flatMap((st) => {
|
||||
const id = sceneThemeId(st, true);
|
||||
return id ? [[st, id] as const] : [];
|
||||
});
|
||||
|
||||
describe('skyThemeId', () => {
|
||||
it('picks a theme for each explicit state', () => {
|
||||
expect(skyThemeId('day')).toBe('noonday-sky');
|
||||
expect(skyThemeId('dusk')).toBe('noonday-sky');
|
||||
expect(skyThemeId('night')).toBe('midnight-sky');
|
||||
describe('sceneThemeId, the theme the page wears', () => {
|
||||
it('with a scene on, the drawn sky gives its theme, whatever theme a person stored', () => {
|
||||
expect(sceneThemeId('day', true)).toBe('noonday-sky');
|
||||
expect(sceneThemeId('dusk', true)).toBe('noonday-sky');
|
||||
expect(sceneThemeId('night', true)).toBe('midnight-sky');
|
||||
});
|
||||
|
||||
it('picks none for auto, at any hour: auto never overrides a signed-in theme', () => {
|
||||
[0, 6, 12, 18, 23].forEach((h) => {
|
||||
it('auto draws the clock sky and that sky decides', () => {
|
||||
[0, 12, 23].forEach((h) => {
|
||||
setSkyClock(() => atStandardTime(h, 10));
|
||||
expect(skyThemeId('auto')).toBeUndefined();
|
||||
expect(sceneThemeId(getResolvedSkyState(), true)).toBe(
|
||||
h === 12 ? 'noonday-sky' : 'midnight-sky'
|
||||
);
|
||||
});
|
||||
setSkyClock();
|
||||
});
|
||||
|
||||
describe('sceneThemeId, the theme the page wears', () => {
|
||||
it('a fixed state picks its theme, whatever the scene, while the person chose no theme', () => {
|
||||
[true, false].forEach((scene) => {
|
||||
expect(sceneThemeId('day', 'night', scene, false)).toBe('noonday-sky');
|
||||
expect(sceneThemeId('night', 'day', scene, false)).toBe('midnight-sky');
|
||||
});
|
||||
});
|
||||
|
||||
it('a theme the person chose wins over every sky state, fixed or auto', () => {
|
||||
(['auto', 'day', 'dusk', 'night'] as const).forEach((state) =>
|
||||
[true, false].forEach((scene) =>
|
||||
expect(sceneThemeId(state, 'night', scene, true)).toBeUndefined()
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
it('auto follows the resolved sky only while a scene is on and the person chose no theme', () => {
|
||||
expect(sceneThemeId('auto', 'day', true, false)).toBe('noonday-sky');
|
||||
expect(sceneThemeId('auto', 'dusk', true, false)).toBe('noonday-sky');
|
||||
expect(sceneThemeId('auto', 'night', true, false)).toBe('midnight-sky');
|
||||
expect(sceneThemeId('auto', 'day', false, false)).toBeUndefined();
|
||||
expect(sceneThemeId('auto', 'day', true, true)).toBeUndefined();
|
||||
});
|
||||
it('with the scene off no sky is drawn, so the person theme stands', () => {
|
||||
(['day', 'dusk', 'night'] as const).forEach((st) =>
|
||||
expect(sceneThemeId(st, false)).toBeUndefined()
|
||||
);
|
||||
});
|
||||
|
||||
it('the resolved sky a hook subscribes to changes as the clock crosses, so theme classes follow', () => {
|
||||
|
|
@ -79,14 +62,6 @@ describe('skyThemeId', () => {
|
|||
vi.useRealTimers();
|
||||
setSkyClock();
|
||||
});
|
||||
|
||||
it('is what a wiring that follows the sky reads: the resolved state names a theme', () => {
|
||||
setSkyClock(() => atStandardTime(12));
|
||||
expect(skyThemeId(getResolvedSkyState())).toBe('noonday-sky');
|
||||
setSkyClock(() => atStandardTime(0));
|
||||
expect(skyThemeId(getResolvedSkyState())).toBe('midnight-sky');
|
||||
setSkyClock();
|
||||
});
|
||||
});
|
||||
|
||||
describe('the plate, where text cannot rest on bare sky', () => {
|
||||
|
|
|
|||
|
|
@ -1,16 +1,8 @@
|
|||
// Which theme a sky state lights the page with. 'auto' picks none: it never overrides a person's theme
|
||||
// (the stored one when signed in, Midnight Sky when signed out). A wiring that follows the sky when no theme
|
||||
// is stored reads `getResolvedSkyState` (skyState.ts) and passes that fixed state here.
|
||||
//
|
||||
// Day and Dusk take the dark ink of Noonday Sky, Night the light ink of Midnight Sky. Each state's
|
||||
// drawn sky is held to a luminance budget (ridge/toneMap.ts, bands in skyState.ts `FixedSkyState`) in which that ink's TEXT clears 4.5:1 on
|
||||
// bare sky: Day at least 0.2983 (dark ink), Night at most 0.0931 (light ink). Dusk's floor is higher, the bound of
|
||||
// the dark link (0.4554), so its link clears as well. skyTheme.test.ts pins the measured text and link ratios. Night's
|
||||
// link ink is not in the band, and the themes' link colours are derived outside this code (src/sky/skyTokens.js).
|
||||
// Midnight Sky's link is derived there against the lightest Night sky
|
||||
// (4.53 on it, 5.43 on the darkest). The plate (skyPlate.css.ts) is still in the shell, pending
|
||||
// its removal.
|
||||
import type { FixedSkyState, SkyState } from './skyState';
|
||||
// Which theme the drawn sky lights the page with. While a scene is on, the sky being drawn (the fixed pick, or the
|
||||
// clock's state under 'auto'; `useResolvedSkyState`) alone decides the theme and, with it, the ink: Day and Dusk
|
||||
// wear Noonday Sky, Night wears Midnight Sky. A person's own theme (the theme menu) stands only where no sky is
|
||||
// drawn, that is with the scene off.
|
||||
import type { FixedSkyState } from './skyState';
|
||||
|
||||
export type SkyThemeId = 'midnight-sky' | 'noonday-sky';
|
||||
|
||||
|
|
@ -20,21 +12,6 @@ const BY_STATE: Record<FixedSkyState, SkyThemeId> = {
|
|||
night: 'midnight-sky',
|
||||
};
|
||||
|
||||
export const skyThemeId = (state: SkyState): SkyThemeId | undefined =>
|
||||
state === 'auto' ? undefined : BY_STATE[state];
|
||||
|
||||
/**
|
||||
* The theme the page wears: a theme the person chose always wins, and picks none here. Otherwise a fixed sky state
|
||||
* picks its theme, and 'auto' follows the resolved sky (`resolved`, the clock's state) only while a scene is on.
|
||||
* Signed-out pages choose no theme, so they pass `userChoseTheme: false`.
|
||||
*/
|
||||
export const sceneThemeId = (
|
||||
state: SkyState,
|
||||
resolved: FixedSkyState,
|
||||
sceneOn: boolean,
|
||||
userChoseTheme: boolean
|
||||
): SkyThemeId | undefined => {
|
||||
if (userChoseTheme) return undefined;
|
||||
if (state !== 'auto') return BY_STATE[state];
|
||||
return sceneOn ? BY_STATE[resolved] : undefined;
|
||||
};
|
||||
/** The theme the page wears: the drawn sky's while a scene is on, otherwise none (the person's theme stands). */
|
||||
export const sceneThemeId = (resolved: FixedSkyState, sceneOn: boolean): SkyThemeId | undefined =>
|
||||
sceneOn ? BY_STATE[resolved] : undefined;
|
||||
|
|
|
|||
|
|
@ -82,7 +82,6 @@ function SelectTheme({ disabled }: { disabled?: boolean }) {
|
|||
const themes = useThemes();
|
||||
const themeNames = useThemeNames();
|
||||
const [themeId, setThemeId] = useSetting(settingsAtom, 'themeId');
|
||||
const [, setThemeChosen] = useSetting(settingsAtom, 'themeChosen');
|
||||
const [menuCords, setMenuCords] = useState<RectCords>();
|
||||
const selectedTheme = themes.find((theme) => theme.id === themeId) ?? LightTheme;
|
||||
|
||||
|
|
@ -92,7 +91,6 @@ function SelectTheme({ disabled }: { disabled?: boolean }) {
|
|||
|
||||
const handleThemeSelect = (theme: Theme) => {
|
||||
setThemeId(theme.id);
|
||||
setThemeChosen(true);
|
||||
setMenuCords(undefined);
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -6,8 +6,7 @@ import {
|
|||
type FixedSkyState,
|
||||
type SkyState,
|
||||
} from '../features/scene/skyState';
|
||||
import { readCapabilities, resolveTier } from '../features/scene/tier';
|
||||
import { useClientConfig } from './useClientConfig';
|
||||
import { getSceneDrawn, subscribeSceneDrawn } from '../features/scene/sceneDrawn';
|
||||
|
||||
/** The choice, as the control shows it ('auto' stays 'auto'). The sky being drawn is `useResolvedSkyState`. */
|
||||
export const useSkyState = (): SkyState => useSyncExternalStore(subscribe, getSkyState);
|
||||
|
|
@ -16,6 +15,5 @@ export const useSkyState = (): SkyState => useSyncExternalStore(subscribe, getSk
|
|||
export const useResolvedSkyState = (): FixedSkyState =>
|
||||
useSyncExternalStore(subscribe, getResolvedSkyState);
|
||||
|
||||
/** Whether a background scene runs on this device and config (the tier the scene host will mount). */
|
||||
export const useSceneOn = (): boolean =>
|
||||
resolveTier(useClientConfig().backgroundScene, readCapabilities(navigator)) !== 'off';
|
||||
/** Whether a background scene is drawing a sky right now (not merely configured): false on a failed or lost context. */
|
||||
export const useSceneOn = (): boolean => useSyncExternalStore(subscribeSceneDrawn, getSceneDrawn);
|
||||
|
|
|
|||
|
|
@ -1,40 +1,88 @@
|
|||
import { readFileSync } from 'fs';
|
||||
import { beforeAll, describe, expect, it, vi } from 'vitest';
|
||||
import { createElement } from 'react';
|
||||
import { renderToString } from 'react-dom/server';
|
||||
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
// settings.ts reads localStorage when it loads, and this suite runs without a DOM.
|
||||
vi.stubGlobal('localStorage', { getItem: () => null, setItem: () => undefined });
|
||||
let chosenTheme: typeof import('./useTheme').chosenTheme;
|
||||
let defaultThemeId: string;
|
||||
// settings.ts reads localStorage when it loads; these suites run without a DOM.
|
||||
const store: Record<string, string> = {};
|
||||
vi.stubGlobal('localStorage', {
|
||||
getItem: (k: string) => store[k] ?? null,
|
||||
setItem: (k: string, v: string) => {
|
||||
store[k] = v;
|
||||
},
|
||||
});
|
||||
// Stored before settings.ts loads: a theme of the person's, and the themeChosen key old settings carry.
|
||||
store['settings'] = JSON.stringify({
|
||||
themeId: 'dark-theme',
|
||||
useSystemTheme: false,
|
||||
themeChosen: true,
|
||||
});
|
||||
vi.stubGlobal('window', {
|
||||
matchMedia: () => ({ matches: false, addEventListener() {}, removeEventListener() {} }),
|
||||
});
|
||||
// The skyState hooks are useSyncExternalStore reads of these two sources; the server renderer needs a snapshot of
|
||||
// each, so the hooks read the same stores directly.
|
||||
vi.mock('./useSkyState', async () => {
|
||||
const drawn = await import('../features/scene/sceneDrawn');
|
||||
return {
|
||||
useResolvedSkyState: () => 'night',
|
||||
useSkyState: () => 'night',
|
||||
useSceneOn: () => drawn.getSceneDrawn(),
|
||||
};
|
||||
});
|
||||
// Style modules need a DOM to inject into; the class names are not what is under test.
|
||||
vi.mock('folds', () => ({ lightTheme: 'light' }));
|
||||
vi.mock('../../config.css', () => ({ onDarkFontWeight: 'dk', onLightFontWeight: 'lt' }));
|
||||
vi.mock('../../colors.css', () => ({
|
||||
butterTheme: 'b',
|
||||
darkTheme: 'd',
|
||||
midnightSkyTheme: 'm',
|
||||
noondaySkyTheme: 'n',
|
||||
silverTheme: 's',
|
||||
}));
|
||||
vi.mock('../../gamecult.css', () => ({}));
|
||||
vi.mock('../../sky.css', () => ({}));
|
||||
vi.mock('./useScreenSize', () => ({
|
||||
ScreenSize: { Mobile: 'm' },
|
||||
useScreenSizeContext: () => 'd',
|
||||
}));
|
||||
|
||||
let useActiveTheme: typeof import('./useTheme').useActiveTheme;
|
||||
let useSceneInk: typeof import('./useTheme').useSceneInk;
|
||||
let setSceneDrawn: typeof import('../features/scene/sceneDrawn').setSceneDrawn;
|
||||
beforeAll(async () => {
|
||||
({ chosenTheme } = await import('./useTheme'));
|
||||
({ defaultThemeId } = await import('../state/settings'));
|
||||
({ useActiveTheme, useSceneInk } = await import('./useTheme'));
|
||||
({ setSceneDrawn } = await import('../features/scene/sceneDrawn'));
|
||||
});
|
||||
|
||||
describe('chosenTheme: what counts as a theme the person chose', () => {
|
||||
it('is not a theme before anything was picked: no id saved, or the default saved', () => {
|
||||
expect(chosenTheme(false, undefined, undefined)).toBe(false);
|
||||
expect(chosenTheme(false, defaultThemeId, undefined)).toBe(false);
|
||||
expect(chosenTheme(false, defaultThemeId, false)).toBe(false);
|
||||
const read = () => {
|
||||
let out = '';
|
||||
const Probe = () => {
|
||||
out = `${useActiveTheme().id}|${JSON.stringify(useSceneInk() ?? null)}`;
|
||||
return null;
|
||||
};
|
||||
renderToString(createElement(Probe));
|
||||
return out;
|
||||
};
|
||||
|
||||
describe('the drawn sky decides the theme and ink; the stored theme stands where none is drawn', () => {
|
||||
beforeEach(() => {
|
||||
setSceneDrawn(false);
|
||||
});
|
||||
|
||||
it('is a pick of Midnight Sky made in the theme menu, which sets the flag', () => {
|
||||
expect(chosenTheme(false, defaultThemeId, true)).toBe(true);
|
||||
it('a scene running over a stored theme wears the sky theme and ink', () => {
|
||||
setSceneDrawn(true);
|
||||
expect(read()).toBe('midnight-sky|{"state":"night","phone":false}');
|
||||
});
|
||||
|
||||
it('is a saved id other than the default, with or without the flag', () => {
|
||||
expect(chosenTheme(false, 'dark-theme', undefined)).toBe(true);
|
||||
expect(chosenTheme(false, 'noonday-sky', true)).toBe(true);
|
||||
it('a scene that is not drawing (failed, or context lost) leaves the stored theme and its own ink', () => {
|
||||
expect(read()).toBe('dark-theme|null');
|
||||
});
|
||||
|
||||
it('is the system scheme', () => {
|
||||
expect(chosenTheme(true, defaultThemeId, undefined)).toBe(true);
|
||||
});
|
||||
|
||||
it('the theme menu sets the flag together with the id', () => {
|
||||
const tsx = readFileSync(
|
||||
`${process.cwd()}/src/app/features/settings/general/General.tsx`,
|
||||
'utf8'
|
||||
);
|
||||
expect(tsx).toMatch(/setThemeId\(theme\.id\);\s*setThemeChosen\(true\);/);
|
||||
it('a restored context wears the sky again', () => {
|
||||
setSceneDrawn(true);
|
||||
setSceneDrawn(false);
|
||||
expect(read()).toBe('dark-theme|null');
|
||||
setSceneDrawn(true);
|
||||
expect(read()).toBe('midnight-sky|{"state":"night","phone":false}');
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -10,11 +10,11 @@ import {
|
|||
} from '../../colors.css';
|
||||
import '../../gamecult.css';
|
||||
import '../../sky.css';
|
||||
import { defaultThemeId, settingsAtom } from '../state/settings';
|
||||
import { settingsAtom } from '../state/settings';
|
||||
import { useSetting } from '../state/hooks/settings';
|
||||
import { sceneThemeId } from '../features/scene/skyTheme';
|
||||
import { sceneInk, type SkyInk } from '../features/scene/skyInk';
|
||||
import { useResolvedSkyState, useSceneOn, useSkyState } from './useSkyState';
|
||||
import { useResolvedSkyState, useSceneOn } from './useSkyState';
|
||||
import { ScreenSize, useScreenSizeContext } from './useScreenSize';
|
||||
|
||||
export enum ThemeKind {
|
||||
|
|
@ -107,28 +107,13 @@ export const useSystemThemeKind = (): ThemeKind => {
|
|||
const toTheme = (id: ReturnType<typeof sceneThemeId>): Theme | undefined =>
|
||||
id === NoondaySkyTheme.id ? NoondaySkyTheme : id && MidnightSkyTheme;
|
||||
|
||||
/**
|
||||
* A theme the person chose: the system scheme, any pick made in the theme menu (`themeChosen`, which also covers
|
||||
* Midnight Sky, the default), or a saved id other than the default (picks from before the flag existed).
|
||||
*/
|
||||
export const chosenTheme = (useSystemTheme: boolean, themeId?: string, themeChosen?: boolean) =>
|
||||
useSystemTheme || themeChosen === true || (themeId !== undefined && themeId !== defaultThemeId);
|
||||
|
||||
/** The theme the sky asks for on a page with no theme of its own (signed out), or undefined for none. */
|
||||
/** The theme the drawn sky asks for while a scene is on, or undefined for none (signed-out pages then wear Midnight Sky). */
|
||||
export const useSkyTheme = (): Theme | undefined =>
|
||||
toTheme(sceneThemeId(useSkyState(), useResolvedSkyState(), useSceneOn(), false));
|
||||
toTheme(sceneThemeId(useResolvedSkyState(), useSceneOn()));
|
||||
|
||||
/** The sky theme the signed-in page wears, or undefined when the person's own theme wins. */
|
||||
const useSceneSkyThemeId = (): ReturnType<typeof sceneThemeId> => {
|
||||
const sky = useSkyState();
|
||||
const resolved = useResolvedSkyState();
|
||||
const sceneOn = useSceneOn();
|
||||
const [systemTheme] = useSetting(settingsAtom, 'useSystemTheme');
|
||||
const [themeId] = useSetting(settingsAtom, 'themeId');
|
||||
const [themeChosen] = useSetting(settingsAtom, 'themeChosen');
|
||||
// A person's own theme wins over the sky, fixed or 'auto'.
|
||||
return sceneThemeId(sky, resolved, sceneOn, chosenTheme(systemTheme, themeId, themeChosen));
|
||||
};
|
||||
/** The sky theme the page wears while a scene is on, or undefined when the person's own theme stands. */
|
||||
const useSceneSkyThemeId = (): ReturnType<typeof sceneThemeId> =>
|
||||
sceneThemeId(useResolvedSkyState(), useSceneOn());
|
||||
|
||||
/** The ink the page wears over the scene (`sceneInk`, skyInk.ts), or undefined when the theme's own ink stands. */
|
||||
export const useSceneInk = (): SkyInk | undefined => {
|
||||
|
|
|
|||
14
src/app/state/settings.test.ts
Normal file
14
src/app/state/settings.test.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import { describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const stored = JSON.stringify({ themeId: 'dark-theme', themeChosen: true });
|
||||
vi.stubGlobal('localStorage', { getItem: () => stored, setItem: () => undefined });
|
||||
|
||||
describe('settings load', () => {
|
||||
it('tolerates a themeChosen key stored by an older build: the rest loads, the key is inert', async () => {
|
||||
const { getSettings, defaultThemeId } = await import('./settings');
|
||||
const s = getSettings() as unknown as Record<string, unknown>;
|
||||
expect(s.themeId).toBe('dark-theme');
|
||||
expect(s.useSystemTheme).toBe(false);
|
||||
expect(defaultThemeId).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
|
@ -18,8 +18,6 @@ export enum MessageLayout {
|
|||
|
||||
export interface Settings {
|
||||
themeId?: string;
|
||||
/** Set when a theme is picked in the menu, so a picked default (Midnight Sky) reads as chosen. Absent until then. */
|
||||
themeChosen?: boolean;
|
||||
useSystemTheme: boolean;
|
||||
lightThemeId?: string;
|
||||
darkThemeId?: string;
|
||||
|
|
|
|||
Loading…
Reference in a new issue