| 1 | // windowChrome owns the desktop shell's native chrome state — detected |
| 2 | // desktop platform, viewport geometry and the main-window maximised flag — as |
| 3 | // a selectable store rather than App-local useState. Runtime wiring (platform |
| 4 | // probe, resize listener, maximised sync) lives in the app-runtime |
| 5 | // WindowChromeLifecycle/useNativeWindowController modules; components only |
| 6 | // read slices, which keeps every chrome consumer on one source of truth |
| 7 | // without prop drilling and without duplicating listeners per region. |
| 8 | |
| 9 | import { create } from "zustand"; |
| 10 | import { detectBrowserPlatform } from "../lib/desktopPlatform"; |
| 11 | import type { DesktopPlatform } from "../lib/desktopPlatform"; |
| 12 | |
| 13 | function initialViewportSize(): { width: number; height: number } { |
| 14 | if (typeof window === "undefined") return { width: 1440, height: 720 }; |
| 15 | return { width: window.innerWidth, height: window.innerHeight }; |
| 16 | } |
| 17 | |
| 18 | type WindowChromeState = { |
| 19 | platform: DesktopPlatform; |
| 20 | viewportWidth: number; |
| 21 | viewportHeight: number; |
| 22 | mainWindowMaximised: boolean; |
| 23 | }; |
| 24 | |
| 25 | export const useWindowChromeStore = create<WindowChromeState>(() => { |
| 26 | const viewport = initialViewportSize(); |
| 27 | return { |
| 28 | platform: detectBrowserPlatform(), |
| 29 | viewportWidth: viewport.width, |
| 30 | viewportHeight: viewport.height, |
| 31 | mainWindowMaximised: false, |
| 32 | }; |
| 33 | }); |
| 34 | |
| 35 | export const setDesktopPlatform = (platform: DesktopPlatform): void => { |
| 36 | useWindowChromeStore.setState({ platform }); |
| 37 | }; |
| 38 | |
| 39 | export const setViewportSize = (width: number, height: number): void => { |
| 40 | useWindowChromeStore.setState((current) => |
| 41 | current.viewportWidth === width && current.viewportHeight === height ? current : { viewportWidth: width, viewportHeight: height }, |
| 42 | ); |
| 43 | }; |
| 44 | |
| 45 | export const setMainWindowMaximised = (maximised: boolean): void => { |
| 46 | useWindowChromeStore.setState((current) => |
| 47 | current.mainWindowMaximised === maximised ? current : { mainWindowMaximised: maximised }, |
| 48 | ); |
| 49 | }; |
| 50 |