2022-10-24 20:33:40 -04:00
|
|
|
import { useEffect, useState } from 'react';
|
2024-05-12 15:48:13 -04:00
|
|
|
|
2023-03-19 22:22:39 +02:00
|
|
|
import { getGamepadNavigationTrees } from '../utils';
|
2022-10-02 04:38:46 +03:00
|
|
|
|
2022-11-02 18:56:11 +02:00
|
|
|
function getQuickAccessWindow(): Window | null {
|
2023-03-19 22:22:39 +02:00
|
|
|
const navTrees = getGamepadNavigationTrees();
|
2024-05-12 15:48:13 -04:00
|
|
|
return (
|
|
|
|
|
navTrees.find((tree: any) => tree?.id === 'QuickAccess-NA')?.m_Root?.m_element?.ownerDocument.defaultView ?? null
|
|
|
|
|
);
|
2022-11-02 18:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-02 04:38:46 +03:00
|
|
|
/**
|
|
|
|
|
* Returns state indicating the visibility of quick access menu.
|
|
|
|
|
*
|
2023-10-09 21:38:34 +03:00
|
|
|
* @returns `true` if quick access menu is visible and `false` otherwise.
|
2022-10-24 20:33:40 -04:00
|
|
|
*
|
2022-10-02 04:38:46 +03:00
|
|
|
* @example
|
2024-05-27 13:19:11 -04:00
|
|
|
* import { FC, useEffect } from "react";
|
2022-10-02 04:38:46 +03:00
|
|
|
* import { useQuickAccessVisible } from "decky-frontend-lib";
|
|
|
|
|
*
|
2024-05-27 13:19:11 -04:00
|
|
|
* export const PluginPanelView: FC<{}> = ({ }) => {
|
2022-10-02 04:38:46 +03:00
|
|
|
* const isVisible = useQuickAccessVisible();
|
|
|
|
|
*
|
|
|
|
|
* useEffect(() => {
|
|
|
|
|
* if (!isVisible) {
|
|
|
|
|
* return;
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* const interval = setInterval(() => console.log("Hello world!"), 1000);
|
|
|
|
|
* return () => {
|
|
|
|
|
* clearInterval(interval);
|
|
|
|
|
* }
|
|
|
|
|
* }, [isVisible])
|
|
|
|
|
*
|
|
|
|
|
* return (
|
|
|
|
|
* <div>
|
|
|
|
|
* {isVisible ? "VISIBLE" : "INVISIBLE"}
|
|
|
|
|
* </div>
|
|
|
|
|
* );
|
|
|
|
|
* };
|
|
|
|
|
*/
|
|
|
|
|
export function useQuickAccessVisible(): boolean {
|
2023-10-09 21:38:34 +03:00
|
|
|
// By default we say that document is not hidden, unless we know otherwise.
|
|
|
|
|
// This would cover the cases when Valve breaks something and the quick access window
|
|
|
|
|
// cannot be accessed anymore - the plugins that use this would continue working somewhat.
|
|
|
|
|
const [isHidden, setIsHidden] = useState(getQuickAccessWindow()?.document.hidden ?? false);
|
2022-10-02 04:38:46 +03:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-11-02 18:56:11 +02:00
|
|
|
const quickAccessWindow = getQuickAccessWindow();
|
2022-10-02 04:38:46 +03:00
|
|
|
if (quickAccessWindow === null) {
|
2022-10-24 20:33:40 -04:00
|
|
|
console.error('Could not get window of QuickAccess menu!');
|
2022-10-02 04:38:46 +03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-09 21:38:34 +03:00
|
|
|
const onVisibilityChange = () => setIsHidden(quickAccessWindow.document.hidden);
|
|
|
|
|
quickAccessWindow.addEventListener('visibilitychange', onVisibilityChange);
|
2022-10-02 04:38:46 +03:00
|
|
|
return () => {
|
2023-10-09 21:38:34 +03:00
|
|
|
quickAccessWindow.removeEventListener('visibilitychange', onVisibilityChange);
|
2022-10-02 04:38:46 +03:00
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
2023-10-09 21:38:34 +03:00
|
|
|
return !isHidden;
|
2022-10-02 04:38:46 +03:00
|
|
|
}
|