| 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 { SIDEBAR_AUTO_COLLAPSE_WIDTH } from "../lib/workspaceLayout"; |
| 11 | import { detectBrowserPlatform } from "../lib/desktopPlatform"; |
| 12 | import type { DesktopPlatform } from "../lib/desktopPlatform"; |
| 13 | |
| 14 | function initialViewportSize(): { width: number; height: number } { |
| 15 | if (typeof window === "undefined") return { width: 1440, height: 720 }; |
| 16 | return { width: window.innerWidth, height: window.innerHeight }; |
| 17 | } |
| 18 | |
| 19 | type WindowChromeState = { |
| 20 | platform: DesktopPlatform; |
| 21 | viewportWidth: number; |
| 22 | viewportHeight: number; |
| 23 | mainWindowMaximised: boolean; |
| 24 | narrowSidebarExpanded: boolean; |
| 25 | }; |
| 26 | |
| 27 | export const useWindowChromeStore = create<WindowChromeState>(() => { |
| 28 | const viewport = initialViewportSize(); |
| 29 | return { |
| 30 | platform: detectBrowserPlatform(), |
| 31 | viewportWidth: viewport.width, |
| 32 | viewportHeight: viewport.height, |
| 33 | mainWindowMaximised: false, |
| 34 | narrowSidebarExpanded: false, |
| 35 | }; |
| 36 | }); |
| 37 | |
| 38 | export const setDesktopPlatform = (platform: DesktopPlatform): void => { |
| 39 | useWindowChromeStore.setState({ platform }); |
| 40 | }; |
| 41 | |
| 42 | export const setViewportSize = (width: number, height: number): void => { |
| 43 | useWindowChromeStore.setState((current) => |
| 44 | current.viewportWidth === width && current.viewportHeight === height ? current : { |
| 45 | viewportWidth: width, viewportHeight: height, |
| 46 | narrowSidebarExpanded: (current.viewportWidth < SIDEBAR_AUTO_COLLAPSE_WIDTH) === (width < SIDEBAR_AUTO_COLLAPSE_WIDTH) |
| 47 | ? current.narrowSidebarExpanded : false, |
| 48 | }, |
| 49 | ); |
| 50 | }; |
| 51 | |
| 52 | export const setMainWindowMaximised = (maximised: boolean): void => { |
| 53 | useWindowChromeStore.setState((current) => |
| 54 | current.mainWindowMaximised === maximised ? current : { mainWindowMaximised: maximised }, |
| 55 | ); |
| 56 | }; |
| 57 |