From 3ea23704247e064507f571122c6829fc8fbd3bda Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 26 Nov 2026 11:34:43 +0100 Subject: [PATCH] scene view: sceneView.ts owns whether the view is open and which submenu One history entry per open, Back and Return the same close, #root inert and hidden while open, the held rotation resumed through cameraState on close. Co-Authored-By: Claude Sonnet 5.5 --- src/app/features/scene/sceneView.test.ts | 132 +++++++++++++++++++++++ src/app/features/scene/sceneView.ts | 93 ++++++++++++++++ 2 files changed, 225 insertions(+) create mode 100644 src/app/features/scene/sceneView.test.ts create mode 100644 src/app/features/scene/sceneView.ts diff --git a/src/app/features/scene/sceneView.test.ts b/src/app/features/scene/sceneView.test.ts new file mode 100644 index 00000000..41bfec69 --- /dev/null +++ b/src/app/features/scene/sceneView.test.ts @@ -0,0 +1,132 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import { getCamera, holdCamera, isHeld, resetCamera } from './cameraState'; +import { + SCENE_VIEW_ATTRIBUTE, + SCENE_VIEW_HISTORY_MARK, + closeSceneView, + getSceneView, + openSceneView, + setSubmenu, + subscribeSceneView, +} from './sceneView'; + +// No DOM here: the few things the module touches (, #root, the focused element, the history stack and the window's popstate) are stand-ins. +class Entry { + focused = 0; + focus() { + this.focused += 1; + } +} +const entryEl = new Entry(); +let attrs: Set; +let rootAttrs: Set; +let stack: Array | null>; +let pops: Set<() => void>; +let back: number; + +beforeEach(() => { + attrs = new Set(); + rootAttrs = new Set(); + stack = [{ idx: 0 }]; + pops = new Set(); + back = 0; + entryEl.focused = 0; + vi.stubGlobal('HTMLElement', Entry); + vi.stubGlobal('document', { + activeElement: entryEl, + documentElement: { + setAttribute: (k: string) => attrs.add(k), + removeAttribute: (k: string) => attrs.delete(k), + }, + getElementById: (id: string) => + id === 'root' + ? { setAttribute: (k: string) => rootAttrs.add(k), removeAttribute: (k: string) => rootAttrs.delete(k) } + : null, + }); + vi.stubGlobal('history', { + get state() { + return stack[stack.length - 1]; + }, + pushState: (s: Record) => stack.push(s), + back: () => { + back += 1; + stack.pop(); + [...pops].forEach((cb) => cb()); + }, + }); + vi.stubGlobal('window', { + addEventListener: (t: string, cb: () => void) => t === 'popstate' && pops.add(cb), + removeEventListener: (t: string, cb: () => void) => t === 'popstate' && pops.delete(cb), + }); + vi.stubGlobal('localStorage', { getItem: () => null, setItem: () => undefined, removeItem: () => undefined }); + resetCamera(); +}); + +afterEach(() => { + closeSceneView(); + vi.unstubAllGlobals(); +}); + +describe('the scene view', () => { + it('opens with one history entry that keeps what the router keeps, the app inert and the page marked', () => { + openSceneView(); + openSceneView(); + expect(getSceneView()).toEqual({ open: true, submenu: undefined }); + expect(stack).toHaveLength(2); + expect(stack[1]).toEqual({ idx: 0, [SCENE_VIEW_HISTORY_MARK]: true }); + expect(rootAttrs.has('inert')).toBe(true); + 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', () => { + openSceneView(); + closeSceneView(); + expect(getSceneView().open).toBe(false); + expect(stack).toHaveLength(1); + 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", () => { + openSceneView(); + const seen = vi.fn(); + subscribeSceneView(seen); + history.back(); + expect(getSceneView().open).toBe(false); + expect(stack).toHaveLength(1); + 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', () => { + setSubmenu('view'); + expect(getSceneView().submenu).toBeUndefined(); // closed: nothing to show + openSceneView(); + setSubmenu('view'); + expect(getSceneView().submenu).toBe('view'); + setSubmenu('time'); + expect(getSceneView().submenu).toBe('time'); + setSubmenu('time'); + expect(getSceneView().submenu).toBeUndefined(); + setSubmenu('sky'); + closeSceneView(); + openSceneView(); + expect(getSceneView().submenu).toBeUndefined(); + }); + + it('Return resumes a rotation a manual turn held, through the camera store', () => { + openSceneView(); + holdCamera(0); + expect(isHeld()).toBe(true); + const mode = getCamera().mode; + closeSceneView(); + expect(isHeld()).toBe(false); + expect(getCamera().mode).toBe(mode); + }); +}); diff --git a/src/app/features/scene/sceneView.ts b/src/app/features/scene/sceneView.ts new file mode 100644 index 00000000..7c215418 --- /dev/null +++ b/src/app/features/scene/sceneView.ts @@ -0,0 +1,93 @@ +// The scene view: whether the fullscreen view of the scene is open, and which submenu it shows. The one owner of both (ruling ridge-scene-view-fullscreen). Opening +// pushes one history entry, so the browser's Back button, Escape and the Return icon are all the same act, closing; while it is open the app (#root) is inert and +// hidden but stays mounted, so the chat keeps its scroll and its composer. Closing is Return: the rotation a manual turn held resumes through `cameraState` +// (ruling ridge-view-turn-handback). The view's controls write the stores that own what they set (cameraState.ts, skyState.ts) and hold no copy of them. +// Main-chunk safe (no React, no ridge code), as skyState.ts and cameraState.ts are. +import { getCamera, setCamera } from './cameraState'; + +export type Submenu = 'view' | 'time' | 'sky' | 'place'; + +export interface SceneViewState { + open: boolean; + /** The submenu whose panel is up, if any: one at a time. */ + submenu: Submenu | undefined; +} + +/** Set on while the view is open: the shell is hidden and the view is the only thing over the scene. */ +export const SCENE_VIEW_ATTRIBUTE = 'data-scene-view'; +/** The mark the view's history entry carries beside whatever the router keeps in `history.state`. */ +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. */ +let entry: HTMLElement | null = null; +/** Whether this view pushed a history entry that is still on top of the stack. */ +let pushed = false; + +export const getSceneView = (): SceneViewState => state; + +export const subscribeSceneView = (cb: () => void): (() => void) => { + listeners.add(cb); + return () => { + listeners.delete(cb); + }; +}; + +const set = (next: SceneViewState): void => { + state = next; + [...listeners].forEach((cb) => cb()); +}; + +const root = (): HTMLElement | null => document.getElementById('root'); + +/** Browser Back (or Forward over the entry) while the view is open: it is Return, and the entry is already gone. */ +const onPopState = (): void => { + pushed = false; + finish(); +}; + +/** Open the view: one history entry, the app inert and hidden behind it. A second call while open changes nothing. */ +export function openSceneView(): void { + if (state.open) return; + entry = document.activeElement instanceof HTMLElement ? document.activeElement : null; + try { + history.pushState({ ...(history.state ?? {}), [SCENE_VIEW_HISTORY_MARK]: true }, ''); + pushed = true; + } catch { + pushed = false; // no history to push to: Return and Escape still close it + } + window.addEventListener('popstate', onPopState); + document.documentElement.setAttribute(SCENE_VIEW_ATTRIBUTE, ''); + root()?.setAttribute('inert', ''); + 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. */ +function finish(): void { + if (!state.open) return; + window.removeEventListener('popstate', onPopState); + document.documentElement.removeAttribute(SCENE_VIEW_ATTRIBUTE); + 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; + entry = null; + set({ open: false, submenu: undefined }); + back?.focus?.(); +} + +/** Return: close the view, and take its history entry off the stack when it is still the top one. */ +export function closeSceneView(): void { + if (!state.open) return; + const wasPushed = pushed; + finish(); // takes the popstate listener off first, so the back below does not close twice + if (wasPushed && history.state?.[SCENE_VIEW_HISTORY_MARK]) history.back(); + pushed = false; +} + +/** Show a submenu's panel, or none; choosing the one already shown closes its panel. */ +export function setSubmenu(submenu: Submenu | undefined): void { + if (!state.open) return; + set({ open: true, submenu: submenu === state.submenu ? undefined : submenu }); +}