Compare commits

..

14 Commits

Author SHA1 Message Date
semantic-release-bot
59ce9e450c chore(release): 4.8.0 [CI SKIP] 2024-10-05 03:52:37 +00:00
AAGaming
980df687e7 fix(components/Router): dont break in desktop ui 2024-10-04 23:52:04 -04:00
AAGaming
bea9ac7774 fix(components/Modal): dont break in desktop ui 2024-10-04 23:51:49 -04:00
AAGaming
063dedbbc1 feat(utils/react): add some window-related utils 2024-10-04 23:51:36 -04:00
semantic-release-bot
723e2ff76e chore(release): 4.7.4 [CI SKIP] 2024-10-04 17:16:01 +00:00
AAGaming
241b22cad7 fix(DialogCheckbox): don't access getters to prevent their side effects from breaking the component 2024-10-04 13:15:29 -04:00
semantic-release-bot
93c59d8026 chore(release): 4.7.3 [CI SKIP] 2024-10-03 20:50:33 +00:00
AAGaming
0f9fb5a3b8 fix(components): fix missing components on oct 2 2024 beta 2024-10-03 16:46:50 -04:00
semantic-release-bot
8c6043b5a7 chore(release): 4.7.2 [CI SKIP] 2024-09-16 23:30:08 +00:00
AAGaming
3aa07dc9ce fix(utils): fix potential race condition in findSP 2024-09-16 19:29:38 -04:00
semantic-release-bot
3f335b3583 chore(release): 4.7.1 [CI SKIP] 2024-08-08 18:18:52 +00:00
AAGaming
4c97097757 fix(utils/react): fix potential race condition in injectFCTrampoline 2024-08-08 14:18:19 -04:00
AAGaming
1ee43e86d6 chore(utils/react): get rid of ts-expect-error 2024-08-03 13:50:33 -04:00
AAGaming
db6ab9c448 chore(docs): add note to CloseSideMenus 2024-07-28 18:51:08 -04:00
11 changed files with 131 additions and 48 deletions

View File

@@ -4,7 +4,7 @@ on:
pull_request:
push:
branches:
- v4-dev
- main
jobs:
release:

View File

@@ -1,3 +1,44 @@
# [4.8.0](https://github.com/SteamDeckHomebrew/decky-frontend-lib/compare/v4.7.4...v4.8.0) (2024-10-05)
### Bug Fixes
* **components/Modal:** dont break in desktop ui ([bea9ac7](https://github.com/SteamDeckHomebrew/decky-frontend-lib/commit/bea9ac777452b883c2f3f3ebf8510c22c4577cd0))
* **components/Router:** dont break in desktop ui ([980df68](https://github.com/SteamDeckHomebrew/decky-frontend-lib/commit/980df687e7b7bf389f478e831f992fb9475eeb15))
### Features
* **utils/react:** add some window-related utils ([063dedb](https://github.com/SteamDeckHomebrew/decky-frontend-lib/commit/063dedbbc1573a825571c675bf6bfa8a1bc1c6cd))
## [4.7.4](https://github.com/SteamDeckHomebrew/decky-frontend-lib/compare/v4.7.3...v4.7.4) (2024-10-04)
### Bug Fixes
* **DialogCheckbox:** don't access getters to prevent their side effects from breaking the component ([241b22c](https://github.com/SteamDeckHomebrew/decky-frontend-lib/commit/241b22cad711621a1695dfd11da857f13c3fffdf))
## [4.7.3](https://github.com/SteamDeckHomebrew/decky-frontend-lib/compare/v4.7.2...v4.7.3) (2024-10-03)
### Bug Fixes
* **components:** fix missing components on oct 2 2024 beta ([0f9fb5a](https://github.com/SteamDeckHomebrew/decky-frontend-lib/commit/0f9fb5a3b8ef4f9978025a28323ab080fb0e7a4c))
## [4.7.2](https://github.com/SteamDeckHomebrew/decky-frontend-lib/compare/v4.7.1...v4.7.2) (2024-09-16)
### Bug Fixes
* **utils:** fix potential race condition in findSP ([3aa07dc](https://github.com/SteamDeckHomebrew/decky-frontend-lib/commit/3aa07dc9ce798ff8d1148424ee9e8a8bf2ba78c6))
## [4.7.1](https://github.com/SteamDeckHomebrew/decky-frontend-lib/compare/v4.7.0...v4.7.1) (2024-08-08)
### Bug Fixes
* **utils/react:** fix potential race condition in injectFCTrampoline ([4c97097](https://github.com/SteamDeckHomebrew/decky-frontend-lib/commit/4c97097757919580a380b70785e6c161de6b03cc))
# [4.7.0](https://github.com/SteamDeckHomebrew/decky-frontend-lib/compare/v4.6.0...v4.7.0) (2024-07-28)

View File

@@ -1,6 +1,6 @@
{
"name": "@decky/ui",
"version": "4.7.0",
"version": "4.8.0",
"description": "A library for interacting with the Steam frontend in Decky plugins and elsewhere.",
"main": "dist/index.js",
"types": "dist/index.d.ts",

View File

@@ -1,6 +1,6 @@
import { FC, ReactNode } from 'react';
import { findModule } from '../webpack';
import { findModuleExport } from '../webpack';
import { DialogCommonProps } from './Dialog';
import { FooterLegendProps } from './FooterLegend';
@@ -18,19 +18,15 @@ export interface DialogCheckboxProps extends DialogCommonProps, FooterLegendProp
onClick?(evt: Event): void;
}
export const DialogCheckbox = Object.values(
findModule((m: any) => {
if (typeof m !== 'object') return false;
for (const prop in m) {
if (m[prop]?.prototype?.GetPanelElementProps) return true;
}
return false;
}),
).find(
(m: any) =>
m.contextType &&
m.prototype?.render.toString().includes('fallback:') &&
m?.prototype?.SetChecked &&
m?.prototype?.Toggle &&
m?.prototype?.GetPanelElementProps,
// Do not access KeyDown, SetChecked, Toggle here as they are getters and accessing them outside of a render breaks them globally
export const DialogCheckbox = findModuleExport(e =>
e.prototype &&
"GetPanelElementProps" in e?.prototype &&
"SetChecked" in e?.prototype &&
"Toggle" in e?.prototype &&
// beta || stable as of oct 2 2024
(e?.prototype?.render?.toString().includes('="DialogCheckbox"') || (
e.contextType &&
e.prototype?.render?.toString().includes('fallback:')
))
) as FC<DialogCheckboxProps>;

View File

@@ -1,7 +1,7 @@
import { FC, ReactNode } from 'react';
import { findSP } from '../utils';
import { Export, findModule, findModuleByExport, findModuleExport } from '../webpack';
import { Export, findModule, findModuleDetailsByExport, findModuleExport } from '../webpack';
// All of the popout options + strTitle are related. Proper usage is not yet known...
export interface ShowModalProps {
@@ -50,7 +50,7 @@ export const showModal = (
bHideMainWindowForPopouts: false,
},
): ShowModalResult => {
return showModalRaw(modal, parent || findSP(), props.strTitle, props, undefined, {
return showModalRaw(modal, parent || findSP() || window, props.strTitle, props, undefined, {
bHideActions: props.bHideActionIcons,
});
};
@@ -105,7 +105,7 @@ interface SimpleModalProps {
children: ReactNode;
}
const ModalModule = findModuleByExport((e: Export) => e?.toString().includes('.ModalPosition,fallback:'), 5);
const [ModalModule, _ModalPosition] = findModuleDetailsByExport((e: Export) => e?.toString().includes('.ModalPosition'), 5)
const ModalModuleProps = ModalModule ? Object.values(ModalModule) : [];
@@ -114,6 +114,4 @@ export const SimpleModal = ModalModuleProps.find((prop) => {
return string?.includes('.ShowPortalModal()') && string?.includes('.OnElementReadyCallbacks.Register(');
}) as FC<SimpleModalProps>;
export const ModalPosition = ModalModuleProps.find((prop) =>
prop?.toString().includes('.ModalPosition,fallback:'),
) as FC<SimpleModalProps>;
export const ModalPosition = _ModalPosition as FC<SimpleModalProps>;

View File

@@ -29,5 +29,6 @@ export interface SliderFieldProps extends ItemProps {
}
export const SliderField = Object.values(CommonUIModule).find((mod: any) =>
mod?.toString()?.includes('SliderField,fallback'),
// stable || beta as of oct 2 2024
mod?.toString()?.includes('SliderField,fallback') || mod?.toString()?.includes("SliderField\",")
) as FC<SliderFieldProps>;

View File

@@ -11,5 +11,6 @@ export interface ToggleFieldProps extends ItemProps {
}
export const ToggleField = Object.values(CommonUIModule).find((mod: any) =>
mod?.render?.toString()?.includes('ToggleField,fallback'),
// stable || beta as of oct 2 2024
mod?.render?.toString()?.includes('ToggleField,fallback') || mod?.render?.toString()?.includes("ToggleField\",")
) as FC<ToggleFieldProps>;

View File

@@ -126,6 +126,7 @@ export interface Navigation {
OpenQuickAccessMenu(quickAccessTab?: QuickAccessTab): void;
OpenMainMenu(): void;
OpenPowerMenu(unknown?: any): void;
/** if calling this to perform navigation, call it after Navigate to prevent a race condition in desktop Big Picture mode that hides the overlay unintentionally */
CloseSideMenus(): void;
}
@@ -143,8 +144,8 @@ try {
logger.warn("Navigation interface failed to call GetFocusedWindowInstance", e);
}
if (!win) {
logger.warn("Navigation interface could not find any focused window. Falling back to GamepadUIMainWindowInstance");
win = Router.WindowStore?.GamepadUIMainWindowInstance;
logger.warn("Navigation interface could not find any focused window. Falling back to Main Window Instance");
win = Router.WindowStore?.GamepadUIMainWindowInstance || Router?.WindowStore?.SteamUIWindows?.[0];
}
if (win) {

View File

@@ -25,7 +25,7 @@ export function findSP(): Window {
if (document.title == 'SP') return window;
// new (SP as popup)
const navTrees = getGamepadNavigationTrees();
return navTrees?.find((x: any) => x.m_ID == 'root_1_').Root.Element.ownerDocument.defaultView;
return navTrees?.find((x: any) => x.m_ID == 'root_1_')?.Root?.Element?.ownerDocument?.defaultView;
}
/**
@@ -40,6 +40,6 @@ export function getFocusNavController(): any {
*/
export function getGamepadNavigationTrees(): any {
const focusNav = getFocusNavController();
const context = focusNav.m_ActiveContext || focusNav.m_LastActiveContext;
const context = focusNav?.m_ActiveContext || focusNav?.m_LastActiveContext;
return context?.m_rgGamepadNavigationTrees;
}

View File

@@ -52,6 +52,7 @@ export function injectFCTrampoline(component: FC, customHooks?: any): FCTrampoli
// we have to redirect this to return an object with component's prototype as a constructor returning a value overrides its prototype
window.SP_REACT.createElement = () => {
loggingEnabled && logger.debug("createElement hook called");
loggingEnabled && console.trace("createElement trace");
return Object.create(component.prototype);
};
}
@@ -66,12 +67,16 @@ export function injectFCTrampoline(component: FC, customHooks?: any): FCTrampoli
}
}
let renderHookStep = 0;
// Accessed two times, once directly before class instantiation, and again in some extra logic we don't need to worry about that we hanlde below just in case.
Object.defineProperty(component, "contextType", {
configurable: true,
get: function () {
loggingEnabled && logger.debug("get contexttype", this, stubsApplied);
applyStubsIfNeeded();
loggingEnabled && logger.debug("get contexttype", this, stubsApplied, renderHookStep);
loggingEnabled && console.trace("contextType trace");
if (renderHookStep == 0) renderHookStep = 1;
else if (renderHookStep == 3) renderHookStep = 4;
return this._contextType;
},
set: function (value) {
@@ -79,16 +84,20 @@ export function injectFCTrampoline(component: FC, customHooks?: any): FCTrampoli
}
});
// Undoes the second contextType access we can't detect shortly before render before it's able to cause any damage
Object.defineProperty(component, "getDerivedStateFromProps", {
// Always accessed directly after contextType for the path we want to catch.
Object.defineProperty(component, "contextTypes", {
configurable: true,
get: function () {
loggingEnabled && logger.debug("get getDerivedStateFromProps", this, stubsApplied);
removeStubsIfNeeded();
return this._getDerivedStateFromProps;
loggingEnabled && logger.debug("get contexttypes", this, stubsApplied, renderHookStep);
loggingEnabled && console.trace("contextTypes trace");
if (renderHookStep == 1) {
renderHookStep = 2;
applyStubsIfNeeded();
};
return this._contextTypes;
},
set: function (value) {
this._getDerivedStateFromProps = value;
this._contextTypes = value;
}
});
@@ -99,11 +108,32 @@ export function injectFCTrampoline(component: FC, customHooks?: any): FCTrampoli
return this._updater;
},
set: function (value) {
loggingEnabled && logger.debug("set updater", this, value, stubsApplied);
removeStubsIfNeeded();
loggingEnabled && logger.debug("set updater", this, value, stubsApplied, renderHookStep);
loggingEnabled && console.trace("updater trace");
if (renderHookStep == 2) {
renderHookStep = 0;
removeStubsIfNeeded();
}
return this._updater = value;
}
});
// Prevents the second contextType+contextTypes access from leaving its hooks around
Object.defineProperty(component, "getDerivedStateFromProps", {
configurable: true,
get: function () {
loggingEnabled && logger.debug("get getDerivedStateFromProps", this, stubsApplied, renderHookStep);
loggingEnabled && console.trace("getDerivedStateFromProps trace");
if (renderHookStep == 2) {
renderHookStep = 0;
removeStubsIfNeeded();
}
return this._getDerivedStateFromProps;
},
set: function (value) {
this._getDerivedStateFromProps = value;
}
});
return userComponent;
}

View File

@@ -1,4 +1,5 @@
import * as React from 'react';
import { Ref, useState } from 'react';
// this shouldn't need to be redeclared but it does for some reason
@@ -104,19 +105,15 @@ export function wrapReactClass(node: any, prop: any = 'type') {
export function getReactRoot(o: HTMLElement | Element | Node) {
return (
// @ts-expect-error 7053
o[Object.keys(o).find((k) => k.startsWith('__reactContainer$')) as string] ||
// @ts-expect-error 7053
o['_reactRootContainer']?._internalRoot?.current
(o as any)[Object.keys(o).find((k) => k.startsWith('__reactContainer$')) as string] ||
(o as any)['_reactRootContainer']?._internalRoot?.current
);
}
export function getReactInstance(o: HTMLElement | Element | Node) {
return (
// @ts-expect-error 7053
o[Object.keys(o).find((k) => k.startsWith('__reactFiber')) as string] ||
// @ts-expect-error 7053
o[Object.keys(o).find((k) => k.startsWith('__reactInternalInstance')) as string]
(o as any)[Object.keys(o).find((k) => k.startsWith('__reactFiber')) as string] ||
(o as any)[Object.keys(o).find((k) => k.startsWith('__reactInternalInstance')) as string]
);
}
@@ -154,3 +151,21 @@ export const findInReactTree = (node: any, filter: findInTreeFilter) =>
// Specialised findInTree for React nodes
walkable: ['props', 'children', 'child', 'sibling'],
});
/**
* Finds the parent window of a DOM element
*/
export function getParentWindow<WindowType = Window>(elem: HTMLElement | null): WindowType | null | undefined {
return elem?.ownerDocument?.defaultView as any;
}
/**
* React hook to find the host window of a component
* Pass the returned ref into a React element and window will be its host window.
* @returns [ref, window]
*/
export function useWindowRef<RefElementType extends HTMLElement, WindowType = Window>(): [Ref<RefElementType>, WindowType | null | undefined] {
const [win, setWin] = useState<WindowType | null | undefined>(null);
return [(elem) => setWin(getParentWindow<WindowType>(elem)), win];
}