import React, { type FC, type ReactNode, useMemo, useEffect, useCallback, cloneElement, isValidElement, useRef, memo, } from "react"; import { type Size } from "../Window/RndWindow/useResizable"; import { type Position } from "react-rnd"; import EventSignals, { type EventsHandlerType } from "@/utils/events"; import { type RD_WindowID_T, type WindowIconType, } from "@/contexts/windows/types"; import { type RD_WindowEventName } from "../../contexts/windows/window/types"; import WindowContext from "./WindowContext"; // Import the context import { useWindows } from "@/contexts/windows"; import { useSignal } from "@/contexts/signal"; import { useWindow } from "@/contexts/windows/window"; export interface WindowContainerProps { onFocus?: () => void; onUnfocus?: () => void; onBlur?: () => void; // Resize callbacks: onResize?: (currentSize: Size) => void; onResizeStart?: (currentSize: Size) => void; onResizeStop?: (newSize: Size) => void; // General Position changes. onPositionChange?: (newPosition: Position) => void; // Dragging callbacks: onDrag?: (newPosition: Position) => void; onDragStart?: (currentPosition: Position) => void; onDragStop?: (newPosition: Position) => void; children?: React.ReactNode; } type WindowContextState = { currentWindowContextID: RD_WindowID_T; id: RD_WindowID_T; }; type WindowEventEmitter = EventsHandlerType; const WindowContainer: FC = ({ onFocus, onUnfocus: onUnfocusProp, onBlur: onBlurProp, onResize, onResizeStart, onResizeStop, onPositionChange, onDrag, onDragStart, onDragStop, children, }) => { const { events: RD_Events } = useSignal(); const { Events: RD_WindowEvents, id: currentWindowContextID } = useWindow(); const onUnfocus = useMemo( () => onUnfocusProp || onBlurProp, [onUnfocusProp, onBlurProp] ); // Callback for focus to avoid re-creation of functions const handleUnfocus = useCallback(() => { if (onUnfocus) { onUnfocus(); } }, [onUnfocus]); // Callback for focus to avoid re-creation of functions const handleFocus = useCallback(() => { if (onFocus) { onFocus(); } }, [onFocus]); // Callback for resize to avoid re-creation of functions const handleResize = useCallback( (newSize: Size) => { if (onResize) { onResize(newSize); } }, [onResize] ); // Callback for resize start to avoid re-creation of functions const handleResizeStart = useCallback( (currentSize: Size) => { if (onResizeStart) { onResizeStart(currentSize); } }, [onResizeStart] ); // Callback for resize stop to avoid re-creation of functions const handleResizeStop = useCallback( (newSize: Size) => { if (onResizeStop) { onResizeStop(newSize); } }, [onResizeStop] ); // Callback for position change to avoid re-creation of functions const handlePositionChange = useCallback( (newPosition: Position) => { if (onPositionChange) { onPositionChange(newPosition); } }, [onPositionChange] ); // Callback for drag to avoid re-creation of functions const handleDrag = useCallback( (newPosition: Position) => { if (onDrag) { onDrag(newPosition); } }, [onDrag] ); // Callback for drag start to avoid re-creation of functions const handleDragStart = useCallback( (currentPosition: Position) => { if (onDragStart) { onDragStart(currentPosition); } }, [onDragStart] ); // Callback for drag stop to avoid re-creation of functions const handleDragStop = useCallback( (newPosition: Position) => { if (onDragStop) { onDragStop(newPosition); } }, [onDragStop] ); useEffect(() => { //console.log(`event:windows/resize:${currentWindowContextID}`); //RD_WindowEvents.onEmit((eventname, args) => console.log({ eventname })); const registeredEvents = [ RD_WindowEvents.on( `event:window/resize`, ({ size }) => handleResize(size) ), RD_WindowEvents.on( `event:window/resize_start`, ({ size }) => handleResizeStart(size) ), RD_WindowEvents.on( `event:window/resize_stop`, ({ size }) => handleResizeStop(size) ), RD_WindowEvents.on( `event:window/drag`, ({ position }) => handleDrag(position) ), RD_WindowEvents.on( `event:window/drag_start`, ({ position }) => handleDragStart(position) ), RD_WindowEvents.on( `event:window/drag_stop`, ({ position }) => handleDragStop(position) ), RD_WindowEvents.on(`event:window/focus`, () => handleFocus() ), RD_WindowEvents.on(`event:window/unfocus`, () => handleUnfocus() ), ]; return () => { // Cleanup registeredEvents.forEach(RD_Events.removeListenerByID); }; }, [ currentWindowContextID, RD_Events, RD_WindowEvents, handleResize, handleResizeStop, handleDrag, handleDragStart, handleDragStop, handleResizeStart, handleFocus, handleUnfocus, ]); return children; }; export default memo(WindowContainer);