import { getCurrentWindow } from "@tauri-apps/api/window"; import React, { createContext, useCallback, use, useEffect, useState, } from "react"; interface WindowContextType { decorated: boolean | null; maximized: boolean | null; toggleDecorations: () => Promise; refreshDecorated: () => Promise; minimize: () => void; close: () => void; toggleMaximize: () => Promise; toggleFullscreen: () => Promise; currentWindow: ReturnType; } const WindowContext = createContext(undefined); export const WindowProvider: React.FC<{ children: React.ReactNode }> = ({ children, }) => { const currentWindow = getCurrentWindow(); const [decorated, setDecorated] = useState(null); const [maximized, setMaximized] = useState(null); const close = useCallback(() => currentWindow.close(), [currentWindow]); const minimize = useCallback(() => currentWindow.minimize(), [currentWindow]); const toggleMaximize = useCallback(async () => { if (await currentWindow.isMaximized()) { await currentWindow.unmaximize(); setMaximized(false); } else { await currentWindow.maximize(); setMaximized(true); } }, [currentWindow]); const toggleFullscreen = useCallback(async () => { await currentWindow.setFullscreen(!(await currentWindow.isFullscreen())); }, [currentWindow]); const refreshDecorated = useCallback(async () => { const val = await currentWindow.isDecorated(); setDecorated(val); return val; }, [currentWindow]); const toggleDecorations = useCallback(async () => { const currentVal = await currentWindow.isDecorated(); await currentWindow.setDecorations(!currentVal); setDecorated(!currentVal); }, [currentWindow]); useEffect(() => { refreshDecorated(); currentWindow.setMinimizable?.(true); }, [currentWindow, refreshDecorated]); return ( {children} ); }; export const useWindow = () => { const context = use(WindowContext); if (context === undefined) { throw new Error("useWindow must be used within WindowProvider"); } return context; }; export const useWindowControls = () => { const { maximized, minimize, toggleMaximize, close, toggleFullscreen, currentWindow, } = useWindow(); return { maximized, minimize, toggleMaximize, close, toggleFullscreen, currentWindow, }; }; export const useWindowDecorations = () => { const { decorated, toggleDecorations, refreshDecorated } = useWindow(); return { decorated, toggleDecorations, refreshDecorated }; };