diff --git a/src/components/client-windows.tsx b/src/components/client-windows.tsx index 610aa08..47c30de 100644 --- a/src/components/client-windows.tsx +++ b/src/components/client-windows.tsx @@ -45,6 +45,9 @@ export const FakeRelativeWindow = ({ content: "animate-[fade-in-half_1s]" }); + const offsetRef = useRef({ x: 0, y: 0 }); + const [isDragging, setDragging] = useState(false); + const popupRef = useRef(null); const pos = useRef({ dragging: false, @@ -72,10 +75,6 @@ export const FakeRelativeWindow = ({ } }, []); - useEffect(() => { - if (popupRef.current) popupRef.current.style.transform = `translate(${currentWindow?.offset.x || 0}px, ${currentWindow?.offset.y || 0}px)`; - }, [currentWindow?.offset]) - useEffect(() => { if (!withAnim) return; const node = popupRef.current; @@ -100,6 +99,18 @@ export const FakeRelativeWindow = ({ return () => observer.disconnect(); }, []); + useEffect(() => { + if (popupRef.current && currentWindow) { + const initPos = { + x: currentWindow.offset.x, + y: currentWindow.offset.y + } + offsetRef.current.x = initPos.x; + offsetRef.current.y = initPos.y; + popupRef.current.style.transform = `translate(${initPos.x}px, ${initPos.y}px)`; + } + }, [popupRef.current, currentWindow]); + const toggleMinimize = () => windowManager.toggleMinimize(windowName); const handleClose = () => windowManager.close(windowName); @@ -109,19 +120,26 @@ export const FakeRelativeWindow = ({ pos.current.dragging = true; pos.current.mouseX = e.clientX; pos.current.mouseY = e.clientY; - pos.current.x = currentWindow.offset.x; - pos.current.y = currentWindow.offset.y; + pos.current.x = offsetRef.current.x; + pos.current.y = offsetRef.current.y; document.body.style.userSelect = "none"; + setDragging(true); } window.addEventListener("mousemove", onMouseMove); window.addEventListener("mouseup", onMouseUp); }; const onMouseUp = () => { + windowManager.move(windowName, { + x: offsetRef.current.x, + y: offsetRef.current.y + }); + pos.current.dragging = false; document.body.style.userSelect = ""; window.removeEventListener("mousemove", onMouseMove); window.removeEventListener("mouseup", onMouseUp); + setDragging(false); }; const onMouseMove = (e: MouseEvent) => { @@ -129,12 +147,10 @@ export const FakeRelativeWindow = ({ if (pos.current.dragging && popupRef.current) { const dx = e.clientX - pos.current.mouseX; const dy = e.clientY - pos.current.mouseY; - const newX = pos.current.x + dx; - const newY = pos.current.y + dy; - windowManager.move(windowName, { - x: newX, - y: newY - }); + const x = pos.current.x + dx; + const y = pos.current.y + dy; + offsetRef.current = { x, y } + popupRef.current.style.transform = `translate(${x}px, ${y}px)`; } }; @@ -142,28 +158,60 @@ export const FakeRelativeWindow = ({ return (