File size: 7,212 Bytes
8af739b
 
5ab4f0a
8af739b
 
 
 
 
 
 
 
 
3c1a712
8af739b
 
 
 
 
3c1a712
8af739b
 
 
75d362b
8af739b
 
 
 
 
 
 
 
 
 
3c1a712
8af739b
 
 
 
 
3c1a712
8af739b
 
 
75d362b
8af739b
 
 
3c1a712
5ab4f0a
bc139ec
5ab4f0a
992b46f
 
 
 
b15ad2e
5ab4f0a
 
b15ad2e
5ab4f0a
 
 
 
 
8af739b
 
 
2b334f6
 
 
 
b15ad2e
 
 
 
 
 
 
 
5ab4f0a
 
 
b15ad2e
 
 
 
 
 
 
 
 
 
 
5ab4f0a
 
b15ad2e
5ab4f0a
b15ad2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ab4f0a
b15ad2e
5ab4f0a
 
 
 
 
 
8af739b
 
bc139ec
992b46f
 
 
 
bc139ec
8af739b
 
 
 
ad31128
8af739b
 
bc139ec
 
75d362b
 
 
062c414
75d362b
bc139ec
 
 
992b46f
 
 
 
 
 
 
bc139ec
7767652
bc139ec
8af739b
 
 
bc139ec
8af739b
 
 
bc139ec
 
7767652
 
 
 
 
3c1a712
992b46f
75d362b
992b46f
75d362b
3c1a712
 
992b46f
 
3c1a712
8af739b
bc139ec
3c1a712
992b46f
75d362b
992b46f
3c1a712
8af739b
7767652
 
 
 
8af739b
bc139ec
 
992b46f
 
bc139ec
 
992b46f
 
bc139ec
8af739b
7767652
3c1a712
8af739b
 
bc139ec
3c1a712
8af739b
 
734142c
bc139ec
8af739b
734142c
8af739b
 
 
 
734142c
8af739b
75d362b
8af739b
 
 
 
 
 
 
 
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
'use client';

import React, { ReactNode, useState, useEffect } from 'react';
import { Rnd } from 'react-rnd';

interface WindowProps {
  id: string;
  title: string;
  isOpen: boolean;
  onClose: () => void;
  onMinimize?: () => void;
  onMaximize?: () => void;
  onFocus?: () => void;
  children: ReactNode;
  width?: number | string;
  height?: number | string;
  x?: number;
  y?: number;
  zIndex?: number;
  resizable?: boolean;
  className?: string;
  headerClassName?: string;
  contentClassName?: string;
  darkMode?: boolean;
}

const Window: React.FC<WindowProps> = ({
  id,
  title,
  isOpen,
  onClose,
  onMinimize,
  onMaximize,
  onFocus,
  children,
  width = 800,
  height = 600,
  x = 100,
  y = 100,
  zIndex = 1000,
  resizable = true,
  className = '',
  headerClassName = '',
  contentClassName = '',
  darkMode = false,
}) => {
  const [isMaximized, setIsMaximized] = React.useState(false);
  const [previousSize, setPreviousSize] = React.useState({ width, height, x, y: Math.max(y, 32) });
  const [isMobile, setIsMobile] = React.useState(false);
  const [isDraggingOrResizing, setIsDraggingOrResizing] = React.useState(false);

  // Use refs to track current position/size without causing re-renders
  const currentPositionRef = React.useRef({ x, y: Math.max(y, 32) });
  const currentSizeRef = React.useRef({ width, height });

  // Detect mobile device - update threshold to better match small screens
  useEffect(() => {
    const checkMobile = () => {
      setIsMobile(window.innerWidth <= 768);
    };
    checkMobile();
    window.addEventListener('resize', checkMobile);
    return () => window.removeEventListener('resize', checkMobile);
  }, []);

  if (!isOpen) return null;

  // Define window classes
  const windowClass = darkMode ? 'bg-gray-900 border-gray-700' : 'bg-[#f5f5f5] border-gray-300/50';
  const headerClass = darkMode ? 'bg-gray-800 border-gray-700' : 'macos-window-header';

  // Bring window to front function - define it early
  const bringToFront = () => {
    if (onFocus) {
      onFocus();
    }
  };

  // On mobile, render with reduced size instead of full screen
  if (isMobile) {
    return (
      <div
        className={`fixed flex flex-col overflow-hidden ${windowClass} ${className} rounded-lg shadow-2xl`}
        style={{
          top: '60px',
          left: '10px',
          right: '10px',
          bottom: '80px',
          zIndex: zIndex || 1000,
          maxHeight: 'calc(100vh - 140px)'
        }}
        onMouseDown={bringToFront}
        onTouchStart={bringToFront}
      >
        <div
          className={`h-12 flex items-center px-3 space-x-2 border-b ${headerClass} ${headerClassName}`}
        >
          <div className="flex space-x-2 group relative z-10">
            <button
              className="traffic-light traffic-close w-6 h-6 sm:w-4 sm:h-4"
              onClick={onClose}
              aria-label="Close"
            />
            <button
              className="traffic-light traffic-min w-6 h-6 sm:w-4 sm:h-4"
              onClick={onMinimize}
              aria-label="Minimize"
            />
            <button
              className="traffic-light traffic-max w-6 h-6 sm:w-4 sm:h-4"
              onClick={onMaximize}
              aria-label="Maximize"
            />
          </div>
          <span className="font-semibold text-gray-700 flex-1 text-center text-sm truncate px-2">{title}</span>
        </div>
        <div className="flex-1 overflow-auto">{children}</div>
      </div>
    );
  }

  const handleMaximize = () => {
    if (!isMaximized) {
      setPreviousSize({
        width: currentSizeRef.current.width,
        height: currentSizeRef.current.height,
        x: currentPositionRef.current.x,
        y: currentPositionRef.current.y
      });
      setIsMaximized(true);
    } else {
      setIsMaximized(false);
    }
    if (onMaximize) onMaximize();
  };

  // Calculate Rnd props based on state
  const rndProps = isMaximized ? {
    position: { x: 0, y: 4 }, // 32px for TopBar offset (h-8 = 32px)
    size: {
      width: typeof window !== 'undefined' ? window.innerWidth : '100vw',
      height: typeof window !== 'undefined' ? window.innerHeight - 120 : 'calc(100vh - 120px)'
    },
    disableDragging: true,
    enableResizing: false
  } : {
    // Use default for uncontrolled mode - only sets initial position
    default: {
      x: currentPositionRef.current.x,
      y: currentPositionRef.current.y,
      width: currentSizeRef.current.width,
      height: currentSizeRef.current.height
    },
    disableDragging: false,
    enableResizing: resizable
  };

  return (
    <Rnd
      {...rndProps}
      minWidth={400}
      minHeight={300}
      dragHandleClassName="window-drag-handle"
      enableResizing={!isMaximized && resizable}
      disableDragging={isMaximized}
      onMouseDown={bringToFront}
      onDragStart={() => {
        setIsDraggingOrResizing(true);
        bringToFront();
      }}
      onDrag={(e, d) => {
        // Constrain dragging to not go above TopBar
        if (d.y < 4) {
          // Update position ref to boundary
          currentPositionRef.current = { x: d.x, y: 4 };
          return false;
        }
        // Update position ref during drag
        currentPositionRef.current = { x: d.x, y: d.y };
      }}
      onDragStop={(e, d) => {
        setIsDraggingOrResizing(false);
        if (!isMaximized) {
          // Ensure window doesn't go above TopBar and update position ref
          const constrainedY = Math.max(d.y, 4);
          currentPositionRef.current = { x: d.x, y: constrainedY };
        }
      }}
      onResizeStart={() => {
        setIsDraggingOrResizing(true);
        bringToFront();
      }}
      onResizeStop={(e, direction, ref, delta, position) => {
        setIsDraggingOrResizing(false);
        if (!isMaximized) {
          // Update size and position refs
          currentSizeRef.current = {
            width: ref.offsetWidth,
            height: ref.offsetHeight,
          };
          currentPositionRef.current = position;
        }
      }}
      className="absolute"
      style={{ zIndex: zIndex || 1000 }}
    >
      <div
        className={`h-full macos-window flex flex-col ${className} ${windowClass}`}
        onMouseDown={bringToFront}
      >
        <div
          className={`window-drag-handle h-10 flex items-center px-4 space-x-4 border-b cursor-move ${headerClass} ${headerClassName}`}
          onDoubleClick={handleMaximize}
        >
          <div className="flex space-x-2 group">
            <div className="traffic-light traffic-close" onClick={onClose} />
            <div className="traffic-light traffic-min" onClick={onMinimize} />
            <div className="traffic-light traffic-max" onClick={handleMaximize} />
          </div>
          <span className="font-semibold text-gray-700 flex-1 text-center pr-16 text-sm select-none">{title}</span>
        </div>
        <div className={`flex-1 overflow-auto ${darkMode ? 'bg-gray-900/95 text-white' : 'bg-white/90'} backdrop-blur-sm ${contentClassName || ''}`} style={{ height: 'calc(100% - 2.5rem)' }}>
          {children}
        </div>
      </div>
    </Rnd>
  );
};

export default Window;