{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "window",
  "title": "Window",
  "description": "Draggable retro window with a striped title bar, close button and optional status bar. Drags snap to --pixel.",
  "dependencies": [
    "cn"
  ],
  "registryDependencies": [
    "@ui95/theme",
    "@ui95/button",
    "@ui95/pixel-glyphs",
    "@ui95/scroll-area"
  ],
  "files": [
    {
      "path": "components/retro/window.tsx",
      "content": "\"use client\"\n\nimport { cn } from \"cn\"\nimport * as React from \"react\"\n\nimport { Button } from \"@/components/ui/button\"\nimport { CloseGlyph, MaximizeGlyph, MinimizeGlyph } from \"@/components/ui/pixel-glyphs\"\nimport { ScrollArea } from \"@/components/ui/scroll-area\"\nimport { type Point, useWindowDrag } from \"@/hooks/use-window-drag\"\n\ntype WindowVariant = \"card\" | \"page\"\n\nconst VARIANT_SURFACES: Record<WindowVariant, { body: string; inactiveTitlebar: string }> = {\n  card: { body: \"bg-card text-card-foreground dark:bg-(--ink-1)\", inactiveTitlebar: \"bg-card text-card-foreground\" },\n  page: { body: \"bg-background text-foreground\", inactiveTitlebar: \"bg-background text-foreground\" },\n}\n\ntype WindowProps = Omit<React.ComponentProps<\"section\">, \"title\"> & {\n  title: string\n  variant?: WindowVariant\n  titleIcon?: React.ReactNode\n  defaultPosition?: Point\n  isActive?: boolean\n  statusBar?: React.ReactNode\n  onMinimize?: () => void\n  onMaximize?: () => void\n  onClose?: () => void\n  onActivate?: () => void\n}\n\nconst ORIGIN: Point = { x: 0, y: 0 }\n\nfunction stopPointerPropagation(event: React.PointerEvent) {\n  event.stopPropagation()\n}\n\nfunction TitleBarButton({\n  label,\n  onClick,\n  children,\n}: {\n  label: string\n  onClick?: () => void\n  children: React.ReactNode\n}) {\n  return (\n    <Button\n      variant=\"ghost\"\n      size=\"icon-xs\"\n      aria-label={label}\n      className=\"text-current\"\n      disabled={onClick === undefined}\n      onPointerDown={stopPointerPropagation}\n      onClick={onClick}\n    >\n      {children}\n    </Button>\n  )\n}\n\nfunction Window({\n  title,\n  variant = \"card\",\n  titleIcon,\n  defaultPosition,\n  isActive = true,\n  statusBar,\n  onMinimize,\n  onMaximize,\n  onClose,\n  onActivate,\n  className,\n  style,\n  children,\n\n  ...props\n}: WindowProps) {\n  const isDraggable = defaultPosition !== undefined\n  const { position, isDragging, startDrag } = useWindowDrag(defaultPosition ?? ORIGIN)\n  const hasFullControls = onMinimize !== undefined || onMaximize !== undefined\n\n  return (\n    <section\n      aria-label={title}\n      data-slot=\"window\"\n      data-variant={variant}\n      data-active={isActive}\n      data-dragging={isDragging}\n      className={cn(\n        \"flex flex-col pixel-border border-border dither-shadow bevel-frame\",\n        VARIANT_SURFACES[variant].body,\n        isDraggable ? \"absolute\" : \"relative\",\n        className\n      )}\n      style={isDraggable ? { left: position.x, top: position.y, ...style } : style}\n      onPointerDownCapture={onActivate}\n      {...props}\n    >\n      <header\n        data-slot=\"window-titlebar\"\n        onPointerDown={isDraggable ? startDrag : undefined}\n        className={cn(\n          \"flex h-8 shrink-0 items-center gap-3 border-b-(length:--pixel) border-border pr-1 pl-3 font-pixel text-[16px] leading-none uppercase select-none\",\n          isActive\n            ? \"bg-titlebar text-titlebar-foreground\"\n            : VARIANT_SURFACES[variant].inactiveTitlebar,\n          isDraggable && \"cursor-grab touch-none\",\n          isDragging && \"cursor-grabbing\",\n        )}\n      >\n        {titleIcon}\n        <h2 className=\"truncate\">{title}</h2>\n        <span aria-hidden=\"true\" className=\"h-4 min-w-6 flex-1 stripes\" />\n        {hasFullControls && (\n          <TitleBarButton label={`Minimize ${title}`} onClick={onMinimize}>\n            <MinimizeGlyph />\n          </TitleBarButton>\n        )}\n        {hasFullControls && (\n          <TitleBarButton label={`Maximize ${title}`} onClick={onMaximize}>\n            <MaximizeGlyph />\n          </TitleBarButton>\n        )}\n        {(hasFullControls || onClose) && (\n          <TitleBarButton label={`Close ${title}`} onClick={onClose}>\n            <CloseGlyph />\n          </TitleBarButton>\n        )}\n      </header>\n      <ScrollArea\n        data-slot=\"window-body\"\n        className=\"min-h-0 flex-1 [&>[data-slot=scroll-area-viewport]>div]:relative [&>[data-slot=scroll-area-viewport]>div]:min-h-full\"\n      >\n        {children}\n      </ScrollArea>\n      {statusBar && (\n        <footer\n          data-slot=\"window-statusbar\"\n          className=\"flex h-7 shrink-0 items-center gap-2 border-t-(length:--pixel) border-border px-2 text-lg leading-none\"\n        >\n          {statusBar}\n        </footer>\n      )}\n    </section>\n  )\n}\n\nexport { Window, type WindowProps, type WindowVariant }\n",
      "type": "registry:component",
      "target": "@components/retro/window.tsx"
    },
    {
      "path": "hooks/use-window-drag.ts",
      "content": "\"use client\"\n\nimport {\n  type PointerEvent as ReactPointerEvent,\n  useCallback,\n  useEffect,\n  useRef,\n  useState,\n} from \"react\"\n\nexport type Point = {\n  x: number\n  y: number\n}\n\nexport type WindowDrag = {\n  position: Point\n  isDragging: boolean\n  startDrag: (event: ReactPointerEvent<HTMLElement>) => void\n}\n\nconst DEFAULT_PIXEL = 2\n\nfunction readPixel(): number {\n  const raw = getComputedStyle(document.documentElement).getPropertyValue(\"--pixel\")\n  const pixel = Number.parseFloat(raw)\n  return Number.isFinite(pixel) && pixel > 0 ? pixel : DEFAULT_PIXEL\n}\n\nfunction snapToPixel(value: number, pixel: number): number {\n  return Math.round(value / pixel) * pixel\n}\n\nexport function useWindowDrag(initialPosition: Point): WindowDrag {\n  const [position, setPosition] = useState(initialPosition)\n  const [isDragging, setIsDragging] = useState(false)\n  const positionRef = useRef(initialPosition)\n  const stopRef = useRef<(() => void) | null>(null)\n\n  useEffect(() => () => stopRef.current?.(), [])\n\n  const startDrag = useCallback((event: ReactPointerEvent<HTMLElement>) => {\n    if (event.button !== 0) {\n      return\n    }\n    event.preventDefault()\n    stopRef.current?.()\n    const pixel = readPixel()\n    const windowElement = event.currentTarget.closest<HTMLElement>(\"[data-slot='window']\") ?? event.currentTarget\n    const scale = windowElement.offsetWidth > 0 ? windowElement.getBoundingClientRect().width / windowElement.offsetWidth : 1\n    const origin = { x: event.clientX, y: event.clientY, position: positionRef.current }\n\n    function trackPointer(moveEvent: PointerEvent) {\n      const next = {\n        x: snapToPixel(origin.position.x + (moveEvent.clientX - origin.x) / scale, pixel),\n        y: snapToPixel(origin.position.y + (moveEvent.clientY - origin.y) / scale, pixel),\n      }\n      positionRef.current = next\n      setPosition(next)\n    }\n\n    function stop() {\n      window.removeEventListener(\"pointermove\", trackPointer)\n      window.removeEventListener(\"pointerup\", drop)\n      window.removeEventListener(\"pointercancel\", drop)\n      stopRef.current = null\n    }\n\n    function drop() {\n      stop()\n      setIsDragging(false)\n    }\n\n    window.addEventListener(\"pointermove\", trackPointer)\n    window.addEventListener(\"pointerup\", drop)\n    window.addEventListener(\"pointercancel\", drop)\n    stopRef.current = stop\n    setIsDragging(true)\n  }, [])\n\n  return { position, isDragging, startDrag }\n}\n",
      "type": "registry:hook"
    }
  ],
  "meta": {
    "sounds": {
      "drag.pickup": {
        "note": "Small tactile click when the drag starts",
        "layers": [
          {
            "wave": "noise",
            "frequency": 0,
            "attack": 0.0066,
            "decay": 0.004,
            "gain": 0.4,
            "filter": {
              "type": "bandpass",
              "frequency": 4000,
              "q": 2
            }
          },
          {
            "wave": "square",
            "frequency": 254,
            "attack": 0.0004,
            "decay": 0.003,
            "gain": 0
          }
        ]
      },
      "drag.drop": {
        "note": "Lower tactile click when it lands",
        "layers": [
          {
            "wave": "noise",
            "frequency": 0,
            "attack": 0.0045,
            "decay": 0.0064,
            "gain": 0.5,
            "filter": {
              "type": "bandpass",
              "frequency": 234,
              "q": 0.524
            }
          },
          {
            "wave": "square",
            "frequency": 396,
            "attack": 0.0005,
            "decay": 0.00494,
            "gain": 0,
            "delay": 0.15
          }
        ]
      },
      "drag.friction": {
        "note": "Filtered noise that opens up with drag speed, updated at a stepped rate",
        "loop": {
          "source": "noise",
          "frequency": 90,
          "filter": "highpass",
          "filterLow": 55.8,
          "filterHigh": 974,
          "q": 0.421,
          "levelMin": 0,
          "levelMax": 0.11,
          "fullSpeed": 3560,
          "gate": 15,
          "fps": 10,
          "smoothing": 46.3,
          "stopRamp": 15
        }
      }
    }
  },
  "categories": [
    "ui"
  ],
  "type": "registry:block"
}
