import { type Position } from "react-rnd"; import { MIN_WINDOW_HEIGHT, MIN_WINDOW_WIDTH } from "./RndWindow/rndDefaults"; import { type Size } from "./RndWindow/useResizable"; import { type Windows, type SnapType, SnapInfo, WindowData, } from "@/contexts/windows/types"; import { WindowStates, type WindowState } from "@/contexts/session/types"; import { WIN_TASKBAR_HEIGHT } from "@/utils/constants"; import { pxToNum, viewHeight, viewWidth } from "@/utils/functions"; import { type Variant } from "framer-motion"; export const cascadePosition = ( id: string, windows: Windows, stackOrder: string[] = [], offset = 0, view?: Size ) => { const parentPositionWindow = stackOrder.find((stackPid) => stackPid !== id) || ""; const { componentWindow } = windows?.[parentPositionWindow] || {}; const { x = 0, y = 0, width = 0, height = 0, } = componentWindow?.getBoundingClientRect() || {}; // Default view dimensions if not provided const viewportWidth: number = view?.width ? pxToNum(view.width) : viewWidth(); const viewportHeight: number = view?.height ? pxToNum(view.height) : viewHeight(); const isOffscreen = x + offset + width > viewportWidth || y + offset + height > viewportHeight; return !isOffscreen && (x || y) ? { x: x + offset, y: y + offset, } : undefined; }; export const centerPosition = ({ height, width }: Size, viewport?: Size) => { const vh = viewport ? pxToNum(viewport.height) : viewHeight() - WIN_TASKBAR_HEIGHT; const vw = viewport ? pxToNum(viewport.width) : viewWidth(); return { x: Math.floor(vw / 2 - pxToNum(width) / 2), y: Math.floor(vh / 2 - pxToNum(height) / 2), }; }; export const WINDOW_OFFSCREEN_BUFFER_PX = { BOTTOM: 15, LEFT: 150, RIGHT: 50, TOP: 15, }; export const isWindowOutsideBounds = ( windowState: WindowState, bounds: Position, checkOffscreen = false ): boolean => { const { position, size } = windowState || {}; const { x = 0, y = 0 } = position || {}; const { height = 0, width = 0 } = size || {}; if (checkOffscreen) { return ( x + WINDOW_OFFSCREEN_BUFFER_PX.RIGHT > bounds.x || x + pxToNum(width) - WINDOW_OFFSCREEN_BUFFER_PX.LEFT < 0 || y + WINDOW_OFFSCREEN_BUFFER_PX.BOTTOM > bounds.y || y + WINDOW_OFFSCREEN_BUFFER_PX.TOP < 0 ); } return ( x < 0 || y < 0 || x + pxToNum(width) > bounds.x || y + pxToNum(height) > bounds.y ); }; export const minMaxSize = (size: Size, lockAspectRatio: boolean): Size => { const desiredHeight = Number(size.height); const desiredWidth = Number(size.width); const [vh, vw] = [viewHeight(), viewWidth()]; const vhWithoutTaskbar = vh - WIN_TASKBAR_HEIGHT; const height = Math.max( MIN_WINDOW_HEIGHT, Math.min(desiredHeight, vhWithoutTaskbar) ); const width = Math.max(MIN_WINDOW_WIDTH, Math.min(desiredWidth, vw)); if (!lockAspectRatio) return { height, width }; const isDesiredHeight = desiredHeight === height; const isDesiredWidth = desiredWidth === width; if (!isDesiredHeight && !isDesiredWidth) { if (desiredHeight > desiredWidth) { return { height, width: Math.round(width / (vhWithoutTaskbar / height)), }; } return { height: Math.round(height / (vw / width)), width, }; } if (!isDesiredHeight) { return { height, width: Math.round(width / (desiredHeight / height)), }; } if (!isDesiredWidth) { return { height: Math.round(height / (desiredWidth / width)), width, }; } return { height, width }; }; type ParsedSnapInfo = { width?: number; height?: number; x?: number; y?: number; }; // eslint-disable-next-line @typescript-eslint/no-explicit-any export const parseSnap = ( snapInfo: SnapInfo, parentSize: Size, // eslint-disable-next-line @typescript-eslint/no-explicit-any relatedWindows?: any, windowStates?: WindowStates ): ParsedSnapInfo => { const { type, index, widthPercent = 100, heightPercent = 100 } = snapInfo; const parentWidth = pxToNum(parentSize.width); const parentHeight = pxToNum(parentSize.height); let x = 0, y = 0, width = (widthPercent / 100) * parentWidth, height = (heightPercent / 100) * parentHeight; switch (type) { case "twoColumns": x = index === 0 ? 0 : parentWidth - width; break; case "threeColumns": // relatedWindows if (relatedWindows) { relatedWindows.forEach( ( [someWindowID, someWindowInfo]: [ id: string, windowInfo: WindowData ], thatWindowIndex ) => { if (!someWindowInfo.snap || typeof someWindowInfo.snap !== "object") return; const thisWindowWidth = (someWindowInfo.snap.widthPercent / 100) * parentWidth; if (index > thatWindowIndex) { x = x + thisWindowWidth; } } ); } break; case "fourGrid": x = (index % 2) * (parentWidth / 2); y = Math.floor(index / 2) * (parentHeight / 2); break; case "threeLeftOneRight": x = index === 3 ? parentWidth / 2 : (parentWidth / 3) * index; y = index === 3 ? 0 : (parentHeight / 3) * index; break; case "threeRightOneLeft": x = index === 0 ? 0 : (parentWidth / 3) * (index - 1) + parentWidth / 2; y = index === 0 ? 0 : (parentHeight / 3) * (index - 1); break; case "fiveLeftGridRight": x = index === 0 ? 0 : parentWidth / 2 + ((index - 1) % 2) * (parentWidth / 4); y = index === 0 ? 0 : Math.floor((index - 1) / 2) * (parentHeight / 2); break; case "fiveRightGridLeft": x = index === 4 ? parentWidth / 2 : (index % 2) * (parentWidth / 4); y = index === 4 ? 0 : Math.floor(index / 2) * (parentHeight / 2); break; } // Allow modifications of widthPercent / heightPercent to take effect. (width = (widthPercent / 100) * parentWidth), (height = (heightPercent / 100) * parentHeight); return { width, height, x, y, }; };