import { useEffect, useLayoutEffect, useState, useRef, useCallback, } from "react"; import { type MotionProps, type Variant } from "framer-motion"; import { useWindows } from "@/contexts/windows"; import { useWindow } from "@/contexts/windows/window"; import { TRANSITIONS_IN_MILLISECONDS, TRANSITIONS_IN_SECONDS, } from "@/utils/constants"; import { getElementSize } from "@/utils/functions"; import { useMainElements } from "@/contexts/elements"; import { useSignal } from "@/contexts/signal"; import { useConfig } from "@/contexts/config"; import { Size } from "./RndWindow/useResizable"; import { type Position } from "react-rnd"; import { parseSnap } from "./functions"; import { useSession } from "@/reactdesk"; import useFocusable from "./useFocusable"; const active = { height: "inherit", opacity: 1, scale: 1, width: "inherit", }; const preview = { opacity: 0.15, scale: 0.98, }; const preview_hover: Variant = { opacity: 1, scale: 1, boxShadow: "rgb(255, 255, 255) 0px 0px 20px inset", }; const exit = { opacity: 0, scale: 0.95, }; const initial = { ...exit, height: "inherit", width: "inherit", }; const fullScaleInitial = { ...initial, scale: 1, }; const baseMaximize = { opacity: 1, scale: 1, }; const baseMinimize = { opacity: 0, scale: 0.3, }; const useWindowTransitions = ( id: string, noInitialScaling = false ): MotionProps => { const { disableAnimations } = useConfig(); const { windows: { [id]: curWindow } = {} } = useWindows(); const { closing, componentWindow, maximized, minimized, taskbarEntry, snap } = curWindow || {}; const { WindowsViewArea, WindowsViewAreaSize } = useMainElements(); const { Events: RD_WindowEvents } = useWindow(); const { previewID } = useSession(); const [maximize, setMaximize] = useState( Object.create(null) as Variant ); const [minimize, setMinimize] = useState( Object.create(null) as Variant ); const [snapVar, setSnapVar] = useState( Object.create(active) as Variant ); const [snapBasePos, setSnapBasePos] = useState({ x: 0, y: 0 }); const [isVisible, setIsVisible] = useState(true); const { events: RD_Events } = useSignal(); const visibilityTimeoutRef = useRef(null); const isSnap = snap && typeof snap === "object"; const getBasePos = useCallback((): Position => { if (!componentWindow || closing) return { x: 0, y: 0 }; const { x: windowX = 0, y: windowY = 0 } = componentWindow.getBoundingClientRect(); const { x: viewX = 0, y: viewY = 0 } = WindowsViewArea ? WindowsViewArea.getBoundingClientRect() : { x: 0, y: 0 }; return { x: -windowX + viewX, y: -windowY + viewY, }; }, [WindowsViewArea, closing, componentWindow]); /* useLayoutEffect(() => { if (!componentWindow || closing) return; const parentSize = WindowsViewArea ? getElementSize(WindowsViewArea) : WindowsViewAreaSize; // Reminder: snapBasePos, are the zero values to work everything out with dimensions. // so if were to position on the right side of screen would be: // x = snapBasePos.x + parentSize.width / 2; for example... if (isSnap) { setTimeout(() => { const cleanData: Variant = parseSnap(snap, parentSize); const basePos = getBasePos(); setSnapVar({ ...active, ...cleanData, x: basePos.x + (cleanData?.x || 0), y: basePos.y + (cleanData?.y || 0), }); }, TRANSITIONS_IN_MILLISECONDS.WINDOW + 50); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [closing, getBasePos, componentWindow, snap, WindowsViewArea]); */ useLayoutEffect(() => { if (!componentWindow || closing) return; const basePos = getBasePos(); setMaximize({ ...baseMaximize, ...(WindowsViewArea ? getElementSize(WindowsViewArea) : WindowsViewAreaSize), x: basePos.x, y: basePos.y, }); // eslint-disable-next-line react-hooks/exhaustive-deps }, [closing, getBasePos, componentWindow, maximized, WindowsViewArea]); useLayoutEffect(() => { if (!taskbarEntry || !componentWindow || closing) return; const { height: taskbarHeight = 0, width: taskbarWidth = 0, x: taskbarX = 0, y: taskbarY = 0, } = taskbarEntry.getBoundingClientRect(); const { height: windowHeight = 0, width: windowWidth = 0, x: windowX = 0, y: windowY = 0, } = componentWindow.getBoundingClientRect(); const x = Math.round( taskbarX - windowX - windowWidth / 2 + taskbarWidth / 2 ); const y = Math.round( taskbarY - windowY - windowHeight / 2 + taskbarHeight / 2 ); if (!(x === 0 && y === 0)) { setMinimize({ ...baseMinimize, x, y }); } }, [closing, componentWindow, minimized, taskbarEntry]); useEffect(() => { const monitorViewportResize = (newWindowsViewSize: Size): void => { if (maximized) { setMaximize((currentMaximize: Variant) => { return { ...currentMaximize, ...newWindowsViewSize, }; }); } }; const registeredEvents = []; if (maximized) { registeredEvents.push( RD_Events.on("event:elements/resize/windowsview", monitorViewportResize) ); } return () => { // Cleanup registeredEvents.forEach(RD_Events.removeListenerByID); }; }, [id, maximized, WindowsViewArea, RD_Events, RD_WindowEvents]); useEffect(() => { if (visibilityTimeoutRef.current) { clearTimeout(visibilityTimeoutRef.current); } if (minimized) { // Hide window after minimize animation visibilityTimeoutRef.current = setTimeout(() => { setIsVisible(false); }, TRANSITIONS_IN_MILLISECONDS.WINDOW + 150); } else { // Show window when not minimized setIsVisible(true); } return () => { if (visibilityTimeoutRef.current) { clearTimeout(visibilityTimeoutRef.current); } }; }, [minimized]); const isPreview = previewID !== false; return { animate: (() => { if (!disableAnimations) { if (!closing && maximized && isPreview && previewID === id) return "phover_maximize"; if (isPreview && previewID === id) return "preview_hover"; if (minimized) return "minimize"; if (!closing && maximized && isPreview && previewID !== id) return "preview_maximize"; if (!closing && maximized) return "maximize"; } if (isPreview && previewID !== id && !minimized) return "preview"; // if (!closing && isSnap) return "snap"; return "active"; })(), exit: "exit", initial: "initial", transition: { duration: isPreview ? TRANSITIONS_IN_SECONDS.WINDOW / 2 : TRANSITIONS_IN_SECONDS.WINDOW, ease: [0.05, 0.1, 0.7, 0.3], }, variants: { active, exit, initial: noInitialScaling ? fullScaleInitial : initial, preview, preview_hover, maximize, preview_maximize: { ...maximize, ...preview, }, phover_maximize: { ...preview_hover, ...maximize, }, minimize, // snap: snapVar, }, // style: { display: isVisible ? "block" : "none" }, // Conditionally render based on visibility }; }; export default useWindowTransitions;