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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/**
 * WindowFrame.tsx — A single draggable / resizable OS window.
 *
 * Reads geometry from osWindowStore and renders app content passed as
 * children. Dragging uses pointer events on the header; resizing works from
 * every edge and corner (8 directions), with a visible grip at the
 * bottom-right. Maximise fills the desktop minus the taskbar.
 * On small viewports windows are forced full-screen and drag is disabled.
 *
 * Gesture geometry is transient: pointermove writes straight to the DOM
 * (coalesced per animation frame) and the final rect is committed to the
 * store once on pointerup — store subscribers and persistence stay off the
 * pointermove hot path.
 */
import {
  useCallback,
  useEffect,
  useLayoutEffect,
  useRef,
  useState,
} from "react";
import { theme as antdTheme } from "antd";
import { Minus, X, Maximize2, type LucideIcon } from "lucide-react";
import { useTranslation } from "react-i18next";
import { useShallow } from "zustand/react/shallow";
import { useTheme } from "../contexts/ThemeContext";
import { useOsWindows, type OsWindow, type OsRect } from "./osWindowStore";
import { computeSnapRect, type SnapZone } from "./snap";
import { clampWindowY } from "./windowGeometry";
import OsAppHost from "./OsAppHost";
import { useOsStyles, MENUBAR_H } from "./useOsStyles";

interface WindowFrameProps {
  win: OsWindow;
  title: string;
  Icon: LucideIcon;
  accent: string;
  isMobile: boolean;
  /**
   * Route-backed app windows set this so the content area becomes a themed
   * surface (antd token background + text colour). Pages assume the layout
   * beneath them supplies colorBgLayout — the classic MainLayout does, so the
   * OS window must too, or light-theme pages render dark text on the dark
   * glass. OS-native apps (App Store, Settings) keep the dark glass styling.
   */
  themedSurface?: boolean;
  /** Per-app minimum size; falls back to the global window minimums. */
  minW?: number;
  minH?: number;
  children: React.ReactNode;
}

const MIN_W = 360;
const MIN_H = 260;

type ResizeDir = "n" | "s" | "e" | "w" | "ne" | "nw" | "se" | "sw";

/** Invisible hit-area thickness for edge/corner resize zones. */
const RESIZE_EDGE = 6;

/** Edge + corner resize zones (the SE corner keeps the visible grip). */
const RESIZE_HANDLES: { dir: ResizeDir; style: React.CSSProperties }[] = [
  {
    dir: "n",
    style: {
      top: -RESIZE_EDGE / 2,
      left: RESIZE_EDGE,
      right: RESIZE_EDGE,
      height: RESIZE_EDGE,
      cursor: "ns-resize",
    },
  },
  {
    dir: "s",
    style: {
      bottom: -RESIZE_EDGE / 2,
      left: RESIZE_EDGE,
      right: RESIZE_EDGE,
      height: RESIZE_EDGE,
      cursor: "ns-resize",
    },
  },
  {
    dir: "e",
    style: {
      right: -RESIZE_EDGE / 2,
      top: RESIZE_EDGE,
      bottom: RESIZE_EDGE,
      width: RESIZE_EDGE,
      cursor: "ew-resize",
    },
  },
  {
    dir: "w",
    style: {
      left: -RESIZE_EDGE / 2,
      top: RESIZE_EDGE,
      bottom: RESIZE_EDGE,
      width: RESIZE_EDGE,
      cursor: "ew-resize",
    },
  },
  {
    dir: "nw",
    style: {
      top: -RESIZE_EDGE / 2,
      left: -RESIZE_EDGE / 2,
      width: RESIZE_EDGE * 2,
      height: RESIZE_EDGE * 2,
      cursor: "nwse-resize",
    },
  },
  {
    dir: "ne",
    style: {
      top: -RESIZE_EDGE / 2,
      right: -RESIZE_EDGE / 2,
      width: RESIZE_EDGE * 2,
      height: RESIZE_EDGE * 2,
      cursor: "nesw-resize",
    },
  },
  {
    dir: "sw",
    style: {
      bottom: -RESIZE_EDGE / 2,
      left: -RESIZE_EDGE / 2,
      width: RESIZE_EDGE * 2,
      height: RESIZE_EDGE * 2,
      cursor: "nesw-resize",
    },
  },
];

export default function WindowFrame({
  win,
  title,
  Icon,
  accent,
  isMobile,
  themedSurface = false,
  minW = MIN_W,
  minH = MIN_H,
  children,
}: WindowFrameProps) {
  const { styles, cx } = useOsStyles();
  const { t } = useTranslation();
  const { isDark } = useTheme();
  const { token } = antdTheme.useToken();
  // Actions only (referentially stable) + a boolean activity flag — this
  // frame re-renders when ITS activation flips, not on every store change.
  const { focus, close, minimize, toggleMaximize, move, resize, snap } =
    useOsWindows(
      useShallow((s) => ({
        focus: s.focus,
        close: s.close,
        minimize: s.minimize,
        toggleMaximize: s.toggleMaximize,
        move: s.move,
        resize: s.resize,
        snap: s.snap,
      })),
    );
  const isActive = useOsWindows((s) => s.activeId === win.id);
  const dragRef = useRef<{ dx: number; dy: number } | null>(null);
  const resizeRef = useRef<
    ({ dir: ResizeDir; sx: number; sy: number } & OsRect) | null
  >(null);
  // Transient gesture geometry: applied to the DOM each animation frame and
  // committed to the store once when the gesture ends.
  const frameRef = useRef<HTMLDivElement | null>(null);
  const pendingRef = useRef<Partial<OsRect> | null>(null);
  const rafRef = useRef<number | null>(null);
  // Live edge-snap zone while dragging the header; drives the preview overlay.
  const [snapZone, setSnapZone] = useState<SnapZone | null>(null);
  // Minimize animation: keep the frame mounted briefly to play the transition.
  const [minimizing, setMinimizing] = useState(false);

  const isFull = win.maximized || isMobile;
  const closeLabel = t("common.close", "Close");
  const minimizeLabel = t("os.minimize", "Minimize");
  const zoomLabel = isFull
    ? t("common.restore", "Restore")
    : t("os.zoom", "Zoom");

  const onWindowKeyDown = useCallback(
    (event: React.KeyboardEvent<HTMLDivElement>) => {
      if (event.target !== event.currentTarget || !event.altKey || isFull) {
        return;
      }
      const step = 20;
      const direction = {
        ArrowLeft: [-step, 0],
        ArrowRight: [step, 0],
        ArrowUp: [0, -step],
        ArrowDown: [0, step],
      }[event.key];
      if (!direction) return;

      event.preventDefault();
      const [dx, dy] = direction;
      if (event.shiftKey) {
        resize(win.id, {
          w: Math.max(minW, win.w + dx),
          h: Math.max(minH, win.h + dy),
        });
      } else {
        move(
          win.id,
          Math.max(0, win.x + dx),
          clampWindowY(win.y + dy, win.h, window.innerHeight),
        );
      }
    },
    [isFull, minH, minW, move, resize, win],
  );

  const writeRect = useCallback((rect: Partial<OsRect>) => {
    const el = frameRef.current;
    if (!el) return;
    if (rect.x !== undefined) el.style.left = `${rect.x}px`;
    if (rect.y !== undefined) el.style.top = `${rect.y}px`;
    if (rect.w !== undefined) el.style.width = `${rect.w}px`;
    if (rect.h !== undefined) el.style.height = `${rect.h}px`;
  }, []);

  // Write the pending gesture rect to the DOM (idempotent, cheap no-op
  // when no gesture is in flight).
  const applyRect = useCallback(() => {
    const rect = pendingRef.current;
    if (rect) writeRect(rect);
  }, [writeRect]);

  // Undo transient DOM writes before React reconciles the store commit.
  // Otherwise unchanged VDOM style fields can retain stale inline values.
  const restoreCommittedRect = useCallback(() => {
    writeRect({ x: win.x, y: win.y, w: win.w, h: win.h });
  }, [win.h, win.w, win.x, win.y, writeRect]);

  const onFrame = useCallback(() => {
    rafRef.current = null;
    applyRect();
  }, [applyRect]);

  const queueApply = useCallback(
    (rect: Partial<OsRect>) => {
      pendingRef.current = { ...pendingRef.current, ...rect };
      if (rafRef.current === null) {
        rafRef.current = requestAnimationFrame(onFrame);
      }
    },
    [onFrame],
  );

  // Mid-gesture re-renders (snap preview state) reset the inline styles
  // from props — re-apply the transient rect before paint.
  useLayoutEffect(() => {
    applyRect();
  });

  /** Stop frame scheduling and return the gesture's final rect (if any). */
  const takeGestureRect = useCallback((): Partial<OsRect> | null => {
    if (rafRef.current !== null) {
      cancelAnimationFrame(rafRef.current);
      rafRef.current = null;
    }
    const rect = pendingRef.current;
    pendingRef.current = null;
    return rect;
  }, []);

  useEffect(
    () => () => {
      if (rafRef.current !== null) cancelAnimationFrame(rafRef.current);
    },
    [],
  );

  const onHeaderPointerDown = useCallback(
    (e: React.PointerEvent) => {
      if ((e.target as HTMLElement).closest("button")) return;
      focus(win.id);
      if (isFull) return;
      dragRef.current = { dx: e.clientX - win.x, dy: e.clientY - win.y };
      (e.target as HTMLElement).setPointerCapture(e.pointerId);
    },
    [focus, isFull, win.id, win.x, win.y],
  );

  const onHeaderPointerMove = useCallback(
    (e: React.PointerEvent) => {
      if (!dragRef.current) return;
      const maxX = window.innerWidth - 80;
      const nx = Math.min(Math.max(0, e.clientX - dragRef.current.dx), maxX);
      const ny = clampWindowY(
        e.clientY - dragRef.current.dy,
        win.h,
        window.innerHeight,
      );
      // Transient: DOM only — the store is untouched until pointerup.
      queueApply({ x: nx, y: ny });
      const EDGE = 12;
      if (e.clientY <= MENUBAR_H + EDGE) setSnapZone("maximize");
      else if (e.clientX <= EDGE) setSnapZone("left");
      else if (e.clientX >= window.innerWidth - EDGE) setSnapZone("right");
      else setSnapZone(null);
    },
    [queueApply, win.h],
  );

  const endDrag = useCallback(
    (e: React.PointerEvent) => {
      // Finalizes the gesture. Also bound to pointercancel and
      // lostpointercapture so system-cancelled gestures (touch, app
      // switch, OS gestures) still commit the last on-screen position;
      // idempotent, so the capture-release cascade is harmless.
      const wasDragging = dragRef.current !== null;
      dragRef.current = null;
      const rect = takeGestureRect();
      if (wasDragging) restoreCommittedRect();
      if (wasDragging && snapZone) {
        snap(win.id, snapZone);
        setSnapZone(null);
      } else if (wasDragging && rect?.x !== undefined && rect.y !== undefined) {
        // Single commit: one store update (and one persisted write) per drag.
        move(win.id, rect.x, rect.y);
      }
      try {
        (e.target as HTMLElement).releasePointerCapture(e.pointerId);
      } catch {
        /* pointer may already be released */
      }
    },
    [snapZone, snap, move, restoreCommittedRect, takeGestureRect, win.id],
  );

  const onResizePointerDown = useCallback(
    (e: React.PointerEvent, dir: ResizeDir) => {
      e.stopPropagation();
      focus(win.id);
      resizeRef.current = {
        dir,
        sx: e.clientX,
        sy: e.clientY,
        x: win.x,
        y: win.y,
        w: win.w,
        h: win.h,
      };
      (e.target as HTMLElement).setPointerCapture(e.pointerId);
    },
    [focus, win.id, win.x, win.y, win.w, win.h],
  );

  const onResizePointerMove = useCallback(
    (e: React.PointerEvent) => {
      const r = resizeRef.current;
      if (!r) return;
      const dx = e.clientX - r.sx;
      const dy = e.clientY - r.sy;
      const rect: Partial<OsRect> = {};
      if (r.dir.includes("e")) rect.w = Math.max(minW, r.w + dx);
      if (r.dir.includes("s")) rect.h = Math.max(minH, r.h + dy);
      if (r.dir.includes("w")) {
        // Left edge moves: keep the right edge anchored.
        const nw = Math.max(minW, r.w - dx);
        rect.w = nw;
        rect.x = r.x + (r.w - nw);
      }
      if (r.dir.includes("n")) {
        // Top edge moves: keep the bottom edge anchored, never cross the menu bar.
        let nh = Math.max(minH, r.h - dy);
        let ny = r.y + (r.h - nh);
        if (ny < MENUBAR_H) {
          ny = MENUBAR_H;
          nh = r.y + r.h - MENUBAR_H;
        }
        rect.h = nh;
        rect.y = ny;
      }
      // Transient: DOM only — the store is untouched until pointerup.
      queueApply(rect);
    },
    [queueApply, minW, minH],
  );

  const endResize = useCallback(
    (e: React.PointerEvent) => {
      // Same finalize-on-cancel semantics as endDrag; idempotent.
      const wasResizing = resizeRef.current !== null;
      resizeRef.current = null;
      const rect = takeGestureRect();
      if (wasResizing) restoreCommittedRect();
      if (wasResizing && rect) {
        // Single commit: one store update (and one persisted write) per
        // resize gesture.
        resize(win.id, rect);
      }
      try {
        (e.target as HTMLElement).releasePointerCapture(e.pointerId);
      } catch {
        /* noop */
      }
    },
    [resize, restoreCommittedRect, takeGestureRect, win.id],
  );

  const handleMinimize = useCallback(() => {
    setMinimizing(true);
    window.setTimeout(() => {
      setMinimizing(false);
      minimize(win.id);
    }, 200);
  }, [minimize, win.id]);

  const geometry: React.CSSProperties = isFull
    ? {
        left: 0,
        top: MENUBAR_H,
        width: "100%",
        height: `calc(100% - ${MENUBAR_H}px)`,
        borderRadius: 0,
        zIndex: win.z,
      }
    : {
        left: win.x,
        top: win.y,
        width: win.w,
        height: win.h,
        zIndex: win.z,
      };

  // Themed surface for route-backed pages: in dark mode the existing glass
  // already matches the dark tokens; in light mode swap in the theme
  // background so light-theme pages stay readable.
  const contentStyle: React.CSSProperties | undefined = themedSurface
    ? {
        background: isDark ? undefined : token.colorBgLayout,
        color: token.colorText,
      }
    : undefined;

  if (win.minimized) return null;

  return (
    <div
      ref={frameRef}
      className={cx(
        styles.window,
        isActive && styles.windowActive,
        minimizing && styles.windowMinimizing,
      )}
      style={geometry}
      onPointerDown={() => focus(win.id)}
      onFocus={() => focus(win.id)}
      onKeyDown={onWindowKeyDown}
      role="group"
      tabIndex={0}
      aria-label={title}
      aria-keyshortcuts="Alt+ArrowLeft Alt+ArrowRight Alt+ArrowUp Alt+ArrowDown Alt+Shift+ArrowLeft Alt+Shift+ArrowRight Alt+Shift+ArrowUp Alt+Shift+ArrowDown"
    >
      {snapZone && <SnapPreview zone={snapZone} />}
      <div
        className={styles.headerMac}
        onPointerDown={onHeaderPointerDown}
        onPointerMove={onHeaderPointerMove}
        onPointerUp={endDrag}
        onPointerCancel={endDrag}
        onLostPointerCapture={endDrag}
        onDoubleClick={() => !isMobile && toggleMaximize(win.id)}
        data-active={isActive ? "true" : "false"}
      >
        <div className={styles.lights}>
          <button
            className={cx(styles.light, styles.lightClose)}
            title={closeLabel}
            aria-label={closeLabel}
            onClick={() => close(win.id)}
          >
            <X size={8} strokeWidth={3} />
          </button>
          <button
            className={cx(styles.light, styles.lightMin)}
            title={minimizeLabel}
            aria-label={minimizeLabel}
            onClick={handleMinimize}
          >
            <Minus size={8} strokeWidth={3} />
          </button>
          <button
            className={cx(styles.light, styles.lightMax)}
            title={zoomLabel}
            aria-label={zoomLabel}
            disabled={isMobile}
            onClick={() => !isMobile && toggleMaximize(win.id)}
          >
            <Maximize2 size={7} strokeWidth={3} />
          </button>
        </div>
        <div className={styles.macTitle} title={title}>
          <Icon size={14} color={accent} />
          {title}
        </div>
        {/* Right spacer keeps the title visually centred. */}
        <div style={{ width: 70 }} />
      </div>

      <OsAppHost
        contentClassName={cx(styles.content, "os-window-body")}
        contentStyle={contentStyle}
      >
        {children}
      </OsAppHost>

      {!isFull && (
        <>
          {RESIZE_HANDLES.map(({ dir, style }) => (
            <div
              key={dir}
              className={styles.resizeArea}
              style={style}
              onPointerDown={(e) => onResizePointerDown(e, dir)}
              onPointerMove={onResizePointerMove}
              onPointerUp={endResize}
              onPointerCancel={endResize}
              onLostPointerCapture={endResize}
            />
          ))}
          <div
            className={styles.resizeHandle}
            onPointerDown={(e) => onResizePointerDown(e, "se")}
            onPointerMove={onResizePointerMove}
            onPointerUp={endResize}
            onPointerCancel={endResize}
            onLostPointerCapture={endResize}
          />
        </>
      )}
    </div>
  );
}

function SnapPreview({ zone }: { zone: SnapZone }) {
  const { styles } = useOsStyles();
  const r = computeSnapRect(zone, window.innerWidth, window.innerHeight);
  return (
    <div
      className={styles.snapPreview}
      style={{
        position: "fixed",
        left: r.x,
        top: r.y,
        width: r.w,
        height: r.h,
      }}
    />
  );
}