import { memo, useCallback, useMemo, useRef, useState, type FC } from "react"; import rndDefaults from "@/components/Window/RndWindow/rndDefaults"; import StyledTitlebar from "@/components/Window/Titlebar/StyledTitlebar"; import { CloseIcon, MaximizeIcon, MaximizedIcon, MinimizeIcon, } from "@/components/Window/Titlebar/WindowActionIcons"; import useTitlebarContextMenu from "@/components/Window/Titlebar/useTitlebarContextMenu"; import useWindowActions from "@/components/Window/Titlebar/useWindowActions"; import { useMenu } from "@/contexts/menu"; import { type MenuState } from "@/contexts/menu/useMenuContextState"; import { useWindows } from "@/contexts/windows"; import { useSession } from "@/contexts/session"; import useDoubleClick from "@/hooks/useDoubleClick"; import Button from "@/styles/common/Button"; import Icon from "@/styles/common/Icon"; import { FOCUSABLE_ELEMENT, LONG_PRESS_DELAY_MS, PREVENT_SCROLL, } from "@/utils/constants"; import { haltEvent, label } from "@/utils/functions"; import React from "react"; import StyledMoveButton from "./StyledMoveButton"; import { Popover } from "react-tiny-popover"; import StyledSnapOptions from "./StyledSnapOptions"; import StyledSnapLayout from "./StyledSnapLayout"; import StyledSnapper from "./StyledSnapper"; import SnapOptions from "./SnapOptions"; type TitlebarProps = { id: string; }; const Titlebar: FC = ({ id }) => { const { windows: { [id]: currentWindow }, } = useWindows(); const { allowResizing = true, closing, componentWindow, hideMaximizeButton, hideMinimizeButton, hideTitlebarIcon, icon, titlebarIcon, title, renderTitle, maximized, } = currentWindow || {}; const { foregroundId } = useSession(); const isForeground = id === foregroundId; const { onClose, onMaximize, onMinimize } = useWindowActions(id); const onClickClose = useDoubleClick(onClose); const onClickMaximize = useDoubleClick(onMaximize); const { menu, setMenu } = useMenu(); const titlebarContextMenu = useTitlebarContextMenu(id); const touchStartTimeRef = useRef(0); const touchStartPositionRef = useRef(); const touchesRef = useRef(); const [showSnapOptions, setShowSnapOptions] = useState(0); const onTouchEnd = useCallback>( (event) => { const { x, y } = componentWindow?.getBoundingClientRect() || {}; if ( Date.now() - touchStartTimeRef.current >= LONG_PRESS_DELAY_MS && touchStartPositionRef.current && touchStartPositionRef.current.x === x && touchStartPositionRef.current.y === y ) { titlebarContextMenu.onContextMenuCapture( Object.assign(event, { touches: touchesRef.current, }) ); } }, [componentWindow, titlebarContextMenu] ); const onTouchStart = useCallback>( ({ touches }) => { if (componentWindow) { componentWindow.blur(); componentWindow.focus(PREVENT_SCROLL); touchStartTimeRef.current = Date.now(); touchStartPositionRef.current = componentWindow.getBoundingClientRect(); touchesRef.current = touches as unknown as TouchList; } }, [componentWindow] ); const renderIcon = useMemo(() => { if (!icon && !titlebarIcon) return false; const actualIcon = titlebarIcon || icon; if (typeof actualIcon === "string") { return ( ); } else if (React.isValidElement(actualIcon)) { return React.cloneElement(actualIcon, { ...onClickClose }); } else if (typeof actualIcon === "function") { // eslint-disable-next-line @typescript-eslint/no-explicit-any const IconComponent = actualIcon as React.ComponentType; return ; } }, [icon, titlebarIcon, onClickClose, title]); const lessShowOpt = useCallback(() => { setShowSnapOptions((prevOpt) => Math.max(prevOpt - 1, 0)); }, [setShowSnapOptions]); const moreShowOpt = useCallback(() => { setShowSnapOptions((prevOpt) => { const newOpt = prevOpt + 1; if (newOpt > 3) { return 1; } return newOpt; }); }, [setShowSnapOptions]); // console.log({showSnapOptions}) // for debugging return ( { if (button === 0 && Object.keys(menu).length > 0) { setMenu(Object.create(null) as MenuState); } }} onMouseUpCapture={() => { if (componentWindow && componentWindow !== document.activeElement) { componentWindow.focus(PREVENT_SCROLL); } }} onTouchEndCapture={onTouchEnd} onTouchStartCapture={onTouchStart} >
{!hideTitlebarIcon && renderIcon !== false && ( {renderIcon} )}
{renderTitle ? renderTitle : title}
); }; export default memo(Titlebar);