diff --git a/src/app/features/scene/README.md b/src/app/features/scene/README.md index ff3440bf..f1e0d3ea 100644 --- a/src/app/features/scene/README.md +++ b/src/app/features/scene/README.md @@ -88,7 +88,7 @@ duration, with the humidity as the fallback when the sun is low (`ridge/weatherF ## The scene view -`sceneView.ts` owns whether the fullscreen view is open and which submenu is up (main-chunk safe). The entry is `SceneViewEntry` (`SkyStateControl.tsx`), the icon button in the navigator and sign-in headers; it preloads the view's chunk on hover or focus. Opening pushes one history entry (so the browser Back button, Escape and Return are the same close), sets `html[data-scene-view]` and makes `#root` inert and hidden while it stays mounted, so the chat keeps its scroll and composer. `BackgroundScene.tsx` mounts the lazy `view/SceneView.tsx` in `#portalContainer`. +`sceneView.ts` owns whether the fullscreen view is open and which submenu is up (main-chunk safe). The entry is `SceneViewEntry` (`SceneViewEntry.tsx`), the icon button in the navigator and sign-in headers; it preloads the view's chunk on hover or focus. Opening pushes one history entry (so the browser Back button, Escape and Return are the same close), sets `html[data-scene-view]` and makes `#root` inert and hidden while it stays mounted, so the chat keeps its scroll and composer. `SceneViewHost.tsx` (mounted by `BackgroundScene.tsx`) loads `view/SceneView.tsx` as a chunk of its own and mounts it in `#portalContainer`; until it has loaded it stands Return and Escape there, a failed load closes the view, and once the view is gone it gives the focus back to the entry (`restoreEntryFocus`). The view is a full-viewport turning surface and, top left, a toolbar rail (Return, View, Time, Sky, Place; every icon `PhoneHit`) beside one panel at a time. View (`view/ViewSubmenu.tsx`): rotation mode, speed (bounded by `view.ts` `maxSpeed` for the mode) and the signed parallax offset (`offsetRange`), each written to `cameraState.ts` on release. Time (`view/TimeSubmenu.tsx`): the sky menu's moments and free moment, unchanged, to `skyState.ts` `setSkyChoice`. Sky (`view/SkySubmenu.tsx`): the constellation lines (`setConstellationLines`, the one setter). Place (`view/PlaceSubmenu.tsx`): the place shown and every data credit (terrain, stars, lines, and the weather only where the feed serves the place; ruling ridge-view-credits-home); the carousel is cut ridge-places. Return calls `setCamera` with the chosen mode, which ends a hold. diff --git a/src/app/features/scene/SceneViewHost.test.tsx b/src/app/features/scene/SceneViewHost.test.tsx index b6edb435..a322b01e 100644 --- a/src/app/features/scene/SceneViewHost.test.tsx +++ b/src/app/features/scene/SceneViewHost.test.tsx @@ -9,11 +9,13 @@ const held = vi.hoisted(() => ({ offered: true, view: undefined as unknown, closed: 0, + restored: 0, + sets: [] as unknown[], loads: [] as Array<(m: unknown) => void>, })); vi.mock('react', async (importOriginal) => { const actual = await importOriginal(); - const useState = () => [held.view, () => undefined]; + const useState = () => [held.view, (v: unknown) => held.sets.push(v)]; const useEffect = (fn: () => unknown) => { held.effects.push(fn); }; @@ -28,6 +30,9 @@ vi.mock('./sceneView', () => ({ closeSceneView: () => { held.closed += 1; }, + restoreEntryFocus: () => { + held.restored += 1; + }, loadSceneViewOrClose: () => new Promise((res) => held.loads.push(res)), })); @@ -47,6 +52,8 @@ beforeEach(() => { held.offered = true; held.view = undefined; held.closed = 0; + held.restored = 0; + held.sets.length = 0; held.loads.length = 0; vi.stubGlobal('document', { getElementById: () => ({}) }); }); @@ -94,6 +101,42 @@ describe('the view while its code loads', () => { }); }); +describe('the host and the focus', () => { + it('gives the entry its focus once the view is closed, and never while it is open', () => { + SceneViewHost(); + held.effects.forEach((run) => run()); + expect(held.restored).toBe(0); + held.effects.length = 0; + held.open = false; + SceneViewHost(); + held.effects.forEach((run) => run()); + expect(held.restored).toBe(1); + }); +}); + +describe('the view code arriving', () => { + const run = () => { + SceneViewHost(); + return held.effects.map((e) => e()).filter((c): c is () => void => typeof c === 'function'); + }; + it('is mounted when it arrives while the view is open', async () => { + const View = () => null; + run(); + held.loads[0]({ default: View }); + await Promise.resolve(); + await Promise.resolve(); + expect(held.sets).toEqual([{ View }]); + }); + it('is dropped when it arrives after the view closed again', async () => { + const cleanups = run(); + cleanups.forEach((c) => c()); + held.loads[0]({ default: () => null }); + await Promise.resolve(); + await Promise.resolve(); + expect(held.sets).toEqual([]); + }); +}); + describe('the host', () => { it('renders nothing and loads nothing while the view is closed', () => { held.open = false; diff --git a/src/app/features/scene/SceneViewHost.tsx b/src/app/features/scene/SceneViewHost.tsx index 6724081b..ba299a25 100644 --- a/src/app/features/scene/SceneViewHost.tsx +++ b/src/app/features/scene/SceneViewHost.tsx @@ -3,7 +3,7 @@ import { createPortal } from 'react-dom'; import { Icon, IconButton, Icons } from 'folds'; import { PhoneHit } from '../../styles/PhoneHit.css'; import { useSceneViewOffered, useSceneViewState } from '../../hooks/useSceneView'; -import { closeSceneView, loadSceneViewOrClose } from './sceneView'; +import { closeSceneView, loadSceneViewOrClose, restoreEntryFocus } from './sceneView'; import * as css from './sceneShell.css'; /** @@ -46,6 +46,11 @@ export function SceneViewHost() { const [view, setView] = useState<{ View: ComponentType } | undefined>(); const shown = open && offered; + // The view is gone from the page (its focus trap with it) once `open` is false: only now can the entry take the focus and keep it. + useEffect(() => { + if (!open) restoreEntryFocus(); + }, [open]); + useEffect(() => { if (open && !offered) closeSceneView(); }, [open, offered]); diff --git a/src/app/features/scene/sceneView.test.ts b/src/app/features/scene/sceneView.test.ts index fab9625f..798c9aa5 100644 --- a/src/app/features/scene/sceneView.test.ts +++ b/src/app/features/scene/sceneView.test.ts @@ -7,6 +7,7 @@ import { getSceneView, loadSceneView, loadSceneViewOrClose, + restoreEntryFocus, openSceneView, setSubmenu, subscribeSceneView, @@ -88,7 +89,7 @@ describe('the scene view', () => { expect(attrs.has(SCENE_VIEW_ATTRIBUTE)).toBe(true); }); - it('the Return icon closes it, takes its entry off the stack, frees the app and gives the focus back to the entry', () => { + it('the Return icon closes it, takes its entry off the stack, frees the app and owes the focus to the entry', () => { openSceneView(); closeSceneView(); expect(getSceneView().open).toBe(false); @@ -96,12 +97,11 @@ describe('the scene view', () => { expect(back).toBe(1); expect(rootAttrs.size).toBe(0); expect(attrs.size).toBe(0); - expect(entryEl.focused).toBe(1); closeSceneView(); expect(back).toBe(1); }); - it("the browser's Back button closes it (the entry is already gone), once, and the focus returns", () => { + it("the browser's Back button closes it (the entry is already gone), once", () => { openSceneView(); const seen = vi.fn(); subscribeSceneView(seen); @@ -111,7 +111,6 @@ describe('the scene view', () => { expect(back).toBe(1); // that one was the browser's; the close did not go back again expect(seen).toHaveBeenCalledTimes(1); expect(rootAttrs.size).toBe(0); - expect(entryEl.focused).toBe(1); }); it('shows one submenu at a time, and choosing the one shown closes its panel', () => { @@ -161,3 +160,34 @@ describe('the view code failing to load', () => { expect(getSceneView().open).toBe(true); }); }); + +describe('the focus after a close', () => { + const closes: Array<[string, () => void]> = [ + ['Return', () => closeSceneView()], + ['Escape (the view calls closeSceneView)', () => closeSceneView()], + ['Back', () => window.history.back()], + ]; + it.each(closes)( + '%s: the close does not touch the focus (the trap is still mounted); restoreEntryFocus gives it to the entry, once', + (_name, close) => { + openSceneView(); + close(); + expect(getSceneView().open).toBe(false); + expect(entryEl.focused).toBe(0); + restoreEntryFocus(); + expect(entryEl.focused).toBe(1); + restoreEntryFocus(); + expect(entryEl.focused).toBe(1); + } + ); + + it('nothing is owed while the view is open, and a reopen forgets an unclaimed focus', () => { + openSceneView(); + restoreEntryFocus(); + expect(entryEl.focused).toBe(0); + closeSceneView(); + openSceneView(); + restoreEntryFocus(); + expect(entryEl.focused).toBe(0); + }); +}); diff --git a/src/app/features/scene/sceneView.ts b/src/app/features/scene/sceneView.ts index 56c72f0d..00027d7e 100644 --- a/src/app/features/scene/sceneView.ts +++ b/src/app/features/scene/sceneView.ts @@ -20,8 +20,10 @@ export const SCENE_VIEW_HISTORY_MARK = 'ratatoskrSceneView'; let state: SceneViewState = { open: false, submenu: undefined }; const listeners = new Set<() => void>(); -/** The control that opened the view, which takes the focus back when it closes. */ +/** The control that opened the view. */ let entry: HTMLElement | null = null; +/** The control that is owed the focus: set by a close, taken by `restoreEntryFocus` once the view's focus trap is gone. */ +let owed: HTMLElement | null = null; /** Whether this view pushed a history entry that is still on top of the stack. */ let pushed = false; @@ -51,6 +53,7 @@ const onPopState = (): void => { export function openSceneView(): void { if (state.open) return; entry = document.activeElement instanceof HTMLElement ? document.activeElement : null; + owed = null; try { window.history.pushState( { ...(window.history.state ?? {}), [SCENE_VIEW_HISTORY_MARK]: true }, @@ -66,7 +69,7 @@ export function openSceneView(): void { set({ open: true, submenu: undefined }); } -/** Everything closing does, whichever way it came: the app live again, the rotation a manual turn held resumed, the focus back where it was. */ +/** Everything closing does, whichever way it came: the app live again, the rotation a manual turn held resumed, the focus owed back to the entry (`restoreEntryFocus`). */ function finish(): void { if (!state.open) return; window.removeEventListener('popstate', onPopState); @@ -74,10 +77,20 @@ function finish(): void { root()?.removeAttribute('inert'); // Picking the mode already chosen ends a hold (cameraState.ts): the scene behind the chat turns again from where the viewer left it. setCamera({ mode: getCamera().mode }); - const back = entry; + owed = entry; entry = null; set({ open: false, submenu: undefined }); - back?.focus?.(); +} + +/** + * Give the focus back to the control that opened the view. The one owner of that focus, called by the host once the view has unmounted: while the view's focus + * trap is still mounted it pulls focus back to Return, and its unmount then drops it to the body. Nothing is owed while the view is open or once it has been + * restored, so a call at any other time changes nothing. + */ +export function restoreEntryFocus(): void { + const to = owed; + owed = null; + to?.focus?.(); } /** Return: close the view, and take its history entry off the stack when it is still the top one. */ diff --git a/src/app/features/scene/view/PlaceSubmenu.test.tsx b/src/app/features/scene/view/PlaceSubmenu.test.tsx index b2beeeae..30be0a52 100644 --- a/src/app/features/scene/view/PlaceSubmenu.test.tsx +++ b/src/app/features/scene/view/PlaceSubmenu.test.tsx @@ -34,11 +34,11 @@ describe('the credits (ruling ridge-view-credits-home: Place alone)', () => { expect(t).toContain(LINES_CREDIT); }); - it('Sky carries none of them', () => { - const t = text(SkySubmenu()); - expect(t).not.toContain(STARS_CREDIT); - expect(t).not.toContain(LINES_CREDIT); - expect(t).not.toMatch(/Data|Datenquelle/); + it('Sky carries only the lines switch and its note: no credit of any wording', () => { + expect(strings(SkySubmenu()).filter((x) => /\s/.test(x))).toEqual([ + 'Constellation lines', + 'Drawn over the stars of a night sky.', + ]); }); it("the weather's credit shows where the feed serves the place, and only there", () => { diff --git a/src/app/features/scene/view/SceneView.test.tsx b/src/app/features/scene/view/SceneView.test.tsx index b776e05e..9d6a24f9 100644 --- a/src/app/features/scene/view/SceneView.test.tsx +++ b/src/app/features/scene/view/SceneView.test.tsx @@ -1,3 +1,5 @@ +import { readFileSync } from 'node:fs'; +import { join } from 'node:path'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { PhoneHit } from '../../../styles/PhoneHit.css'; import SceneView, { Rail, SUBMENUS } from './SceneView'; @@ -224,3 +226,16 @@ describe('one finger on the phone (ruling ridge-view-phone-one-finger)', () => { expect(held.turner).toEqual([['down', 100]]); }); }); + +describe('the chrome styles (a vanilla-extract rule cannot be read back, so the source is)', () => { + const css = readFileSync(join(__dirname, 'SceneView.css.ts'), 'utf8'); + it('the fade rule uses the constant the test above bounds', () => { + expect(css).toMatch(/data-turning="true"] &`]: { opacity: TURNING_CHROME_OPACITY }/); + }); + it('the chrome takes no pointer itself, so a drag over its gaps turns the view; its plates do', () => { + const chrome = css.slice(css.indexOf('export const Chrome'), css.indexOf('const Plate')); + expect(chrome).toContain("pointerEvents: 'none'"); + const plate = css.slice(css.indexOf('const Plate'), css.indexOf('export const Rail')); + expect(plate).toContain("pointerEvents: 'auto'"); + }); +}); diff --git a/src/app/hooks/useSceneView.test.ts b/src/app/hooks/useSceneView.test.ts new file mode 100644 index 00000000..6b75a9c8 --- /dev/null +++ b/src/app/hooks/useSceneView.test.ts @@ -0,0 +1,30 @@ +import { afterEach, describe, expect, it, vi } from 'vitest'; +import { useSceneViewOffered } from './useSceneView'; + +const held = vi.hoisted(() => ({ config: undefined as unknown })); +vi.mock('./useClientConfig', () => ({ useClientConfig: () => ({ backgroundScene: held.config }) })); + +const device = (nav: Record) => vi.stubGlobal('navigator', nav); +afterEach(() => { + vi.unstubAllGlobals(); +}); + +describe('whether the scene view is offered', () => { + it('is not with the tier off or absent, and is with any tier on', () => { + device({}); + held.config = undefined; + expect(useSceneViewOffered()).toBe(false); + held.config = { tier: 'off' }; + expect(useSceneViewOffered()).toBe(false); + for (const tier of ['still', 'low', 'full']) { + held.config = { tier }; + expect(useSceneViewOffered()).toBe(true); + } + }); + + it('follows the tier the device allows, not the one configured: no device ceiling turns a configured tier off', () => { + device({ connection: { saveData: true } }); + held.config = { tier: 'full' }; + expect(useSceneViewOffered()).toBe(true); // lowered to still, still a scene + }); +});