Compare commits

...

11 Commits

Author SHA1 Message Date
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
semantic-release-bot
4646f22b0c chore(release): 4.7.0 [CI SKIP] 2024-07-28 22:18:51 +00:00
AAGaming
7eb484d55c feat(router): support desktop bpm overlay 2024-07-28 18:17:52 -04:00
AAGaming
5164f980b3 chore(stores): add SteamUIStore, securitystore 2024-07-28 18:15:51 -04:00
AAGaming
0457feec95 chore(tabs): port back to normal find 2024-07-28 18:15:34 -04:00
semantic-release-bot
73b8d52c7f chore(release): 4.6.0 [CI SKIP] 2024-07-26 18:33:10 +00:00
AAGaming
2b8d2ae4db feat(classMapper): add findClassByName back 2024-07-26 14:32:34 -04:00
AAGaming
48de8928e4 chore(deprecation): deprecate useQuickAccessVisible as it has been moved to @decky/api 2024-07-26 14:07:58 -04:00
9 changed files with 139 additions and 128 deletions

View File

@@ -1,3 +1,24 @@
## [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)
### Features
* **router:** support desktop bpm overlay ([7eb484d](https://github.com/SteamDeckHomebrew/decky-frontend-lib/commit/7eb484d55c6be6e7844878eb47eda55591a6cf51))
# [4.6.0](https://github.com/SteamDeckHomebrew/decky-frontend-lib/compare/v4.5.0...v4.6.0) (2024-07-26)
### Features
* **classMapper:** add findClassByName back ([2b8d2ae](https://github.com/SteamDeckHomebrew/decky-frontend-lib/commit/2b8d2ae4dbd9a0c4a59a43be0101a0a8fe1c518f))
# [4.5.0](https://github.com/SteamDeckHomebrew/decky-frontend-lib/compare/v4.4.0...v4.5.0) (2024-07-24) # [4.5.0](https://github.com/SteamDeckHomebrew/decky-frontend-lib/compare/v4.4.0...v4.5.0) (2024-07-24)

View File

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

View File

@@ -17,15 +17,18 @@ export const classModuleMap: Map<ModuleID, ClassModule> = createModuleMapping((m
return false; return false;
}); });
export const classMap = classModuleMap.values(); export const classMap = [...classModuleMap.values()];
export function findClass(id: string, name: string): string | void { export function findClass(id: string, name: string): string | void {
return classModuleMap.get(id)?.[name]; return classModuleMap.get(id)?.[name];
} }
export function findClassByName(name: string): string | void {
return classMap.find((m) => m[name])?.[name];
}
export function findClassModule(filter: (module: any) => boolean): ClassModule | void { export function findClassModule(filter: (module: any) => boolean): ClassModule | void {
// TODO optimize return classMap.find((m) => filter(m));
return [...classModuleMap.values()].find((m) => filter(m));
} }
export function unminifyClass(minifiedClass: string): string | void { export function unminifyClass(minifiedClass: string): string | void {

View File

@@ -1,9 +1,6 @@
import { FC, ReactNode, createElement, useEffect, useState } from 'react'; import { FC, ReactNode } from 'react';
import { findModuleByExport } from '../webpack';
import { fakeRenderComponent, findInReactTree, sleep } from '../utils';
import { Export, findModuleByExport } from '../webpack';
import { FooterLegendProps } from './FooterLegend'; import { FooterLegendProps } from './FooterLegend';
import { SteamSpinner } from './SteamSpinner';
/** /**
* Individual tab objects for the Tabs component * Individual tab objects for the Tabs component
@@ -65,63 +62,9 @@ export interface TabsProps {
autoFocusContents?: boolean; autoFocusContents?: boolean;
} }
let tabsComponent: any; const tabsModule = findModuleByExport(e => e?.toString()?.includes(".TabRowTabs") && e?.toString()?.includes("activeTab:"));
const getTabs = async () => {
if (tabsComponent) return tabsComponent;
// @ts-ignore
while (!window?.DeckyPluginLoader?.routerHook?.routes) {
console.debug('[DFL:Tabs]: Waiting for Decky router...');
await sleep(500);
}
return (tabsComponent = fakeRenderComponent(
() => {
return findInReactTree(
findInReactTree(
// @ts-ignore
window.DeckyPluginLoader.routerHook.routes
.find((x: any) => x.props.path == '/library/app/:appid/achievements')
.props.children.type(),
(x) => x?.props?.scrollTabsTop,
).type({ appid: 1 }),
(x) => x?.props?.tabs,
).type;
},
{
useRef: () => ({ current: { reaction: { track: () => {} } } }),
useContext: () => ({ match: { params: { appid: 1 } } }),
useMemo: () => ({ data: {} }),
},
));
};
let oldTabs: any;
try {
const oldTabsModule = findModuleByExport((e: Export) => e.Unbleed);
if (oldTabsModule)
oldTabs = Object.values(oldTabsModule).find((x: any) => x?.type?.toString()?.includes('((function(){'));
} catch (e) {
console.error('Error finding oldTabs:', e);
}
/** /**
* Tabs component as used in the library and media tabs. See {@link TabsProps}. * Tabs component as used in the library and media tabs. See {@link TabsProps}.
* Unlike other components in `decky-frontend-lib`, this requires Decky Loader to be running.
*/ */
export const Tabs = (oldTabs || export const Tabs = tabsModule && Object.values(tabsModule).find((e: any) => e?.type?.toString()?.includes("((function()")) as FC<TabsProps>;
((props: TabsProps) => {
const found = tabsComponent;
const [tc, setTC] = useState<FC<TabsProps>>(found);
useEffect(() => {
if (found) return;
(async () => {
console.debug('[DFL:Tabs]: Finding component...');
const t = await getTabs();
console.debug('[DFL:Tabs]: Found!');
setTC(t);
})();
}, []);
console.log('tc', tc);
return tc ? createElement(tc, props) : <SteamSpinner />;
})) as FC<TabsProps>;

View File

@@ -11,12 +11,14 @@ function getQuickAccessWindow(): Window | null {
/** /**
* Returns state indicating the visibility of quick access menu. * Returns state indicating the visibility of quick access menu.
*
* @deprecated moved to @decky/api
* *
* @returns `true` if quick access menu is visible and `false` otherwise. * @returns `true` if quick access menu is visible and `false` otherwise.
* *
* @example * @example
* import { FC, useEffect } from "react"; * import { FC, useEffect } from "react";
* import { useQuickAccessVisible } from "decky-frontend-lib"; * import { useQuickAccessVisible } from "@decky/ui";
* *
* export const PluginPanelView: FC<{}> = ({ }) => { * export const PluginPanelView: FC<{}> = ({ }) => {
* const isVisible = useQuickAccessVisible(); * const isVisible = useQuickAccessVisible();

View File

@@ -1,3 +1,4 @@
import { WindowRouter } from '../modules/Router';
import { AppDetails, LogoPosition, SteamAppOverview } from './SteamClient'; import { AppDetails, LogoPosition, SteamAppOverview } from './SteamClient';
declare global { declare global {
interface Window { interface Window {
@@ -46,5 +47,11 @@ declare global {
GetCustomLogoPosition: (app: SteamAppOverview) => LogoPosition | null; GetCustomLogoPosition: (app: SteamAppOverview) => LogoPosition | null;
SaveCustomLogoPosition: (app: SteamAppOverview, logoPositions: LogoPosition) => any; SaveCustomLogoPosition: (app: SteamAppOverview, logoPositions: LogoPosition) => any;
}; };
SteamUIStore: {
GetFocusedWindowInstance: () => WindowRouter;
};
securitystore: {
IsLockScreenActive: () => boolean;
};
} }
} }

View File

@@ -1,4 +1,4 @@
import { sleep } from '../utils'; import Logger from '../logger';
import { Export, findModuleExport } from '../webpack'; import { Export, findModuleExport } from '../webpack';
export enum SideMenu { export enum SideMenu {
@@ -88,14 +88,23 @@ export interface WindowStore {
export interface Router { export interface Router {
WindowStore?: WindowStore; WindowStore?: WindowStore;
/** @deprecated use {@link Navigation} instead */
CloseSideMenus(): void; CloseSideMenus(): void;
/** @deprecated use {@link Navigation} instead */
Navigate(path: string): void; Navigate(path: string): void;
/** @deprecated use {@link Navigation} instead */
NavigateToAppProperties(): void; NavigateToAppProperties(): void;
/** @deprecated use {@link Navigation} instead */
NavigateToExternalWeb(url: string): void; NavigateToExternalWeb(url: string): void;
/** @deprecated use {@link Navigation} instead */
NavigateToInvites(): void; NavigateToInvites(): void;
/** @deprecated use {@link Navigation} instead */
NavigateToChat(): void; NavigateToChat(): void;
/** @deprecated use {@link Navigation} instead */
NavigateToLibraryTab(): void; NavigateToLibraryTab(): void;
/** @deprecated use {@link Navigation} instead */
NavigateToLayoutPreview(e: unknown): void; NavigateToLayoutPreview(e: unknown): void;
/** @deprecated use {@link Navigation} instead */
OpenPowerMenu(unknown?: any): void; OpenPowerMenu(unknown?: any): void;
get RunningApps(): AppOverview[]; get RunningApps(): AppOverview[];
get MainRunningApp(): AppOverview | undefined; get MainRunningApp(): AppOverview | undefined;
@@ -117,58 +126,58 @@ export interface Navigation {
OpenQuickAccessMenu(quickAccessTab?: QuickAccessTab): void; OpenQuickAccessMenu(quickAccessTab?: QuickAccessTab): void;
OpenMainMenu(): void; OpenMainMenu(): void;
OpenPowerMenu(unknown?: any): 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; CloseSideMenus(): void;
} }
export let Navigation = {} as Navigation; export let Navigation = {} as Navigation;
const logger = new Logger("Navigation");
try { try {
(async () => { function createNavigationFunction(fncName: string, handler?: (win: any) => any) {
let InternalNavigators: any = {}; return (...args: any) => {
if (!Router.NavigateToAppProperties || (Router as unknown as any).deckyShim) { let win: WindowRouter | undefined;
function initInternalNavigators() { try {
try { win = window.SteamUIStore.GetFocusedWindowInstance();
InternalNavigators = findModuleExport((e: Export) => e.GetNavigator && e.SetNavigator)?.GetNavigator(); } catch (e) {
} catch (e) { logger.warn("Navigation interface failed to call GetFocusedWindowInstance", e);
console.error('[DFL:Router]: Failed to init internal navigators, trying again');
}
} }
initInternalNavigators(); if (!win) {
while (!InternalNavigators?.AppProperties) { logger.warn("Navigation interface could not find any focused window. Falling back to GamepadUIMainWindowInstance");
console.log('[DFL:Router]: Trying to init internal navigators again'); win = Router.WindowStore?.GamepadUIMainWindowInstance;
await sleep(2000); }
initInternalNavigators();
if (win) {
try {
const thisObj = handler && handler(win);
(thisObj || win)[fncName](...args);
} catch (e) {
logger.error("Navigation handler failed", e);
}
} else {
logger.error("Navigation interface could not find a window to navigate");
} }
} }
const newNavigation = { }
Navigate: Router.Navigate?.bind(Router), const newNavigation = {
NavigateBack: Router.WindowStore?.GamepadUIMainWindowInstance?.NavigateBack?.bind( Navigate: createNavigationFunction("Navigate"),
Router.WindowStore.GamepadUIMainWindowInstance, NavigateBack: createNavigationFunction("NavigateBack"),
), NavigateToAppProperties: createNavigationFunction("AppProperties", win => win.Navigator),
NavigateToAppProperties: InternalNavigators?.AppProperties || Router.NavigateToAppProperties?.bind(Router), NavigateToExternalWeb: createNavigationFunction("ExternalWeb", win => win.Navigator),
NavigateToExternalWeb: InternalNavigators?.ExternalWeb || Router.NavigateToExternalWeb?.bind(Router), NavigateToInvites: createNavigationFunction("Invites", win => win.Navigator),
NavigateToInvites: InternalNavigators?.Invites || Router.NavigateToInvites?.bind(Router), NavigateToChat: createNavigationFunction("Chat", win => win.Navigator),
NavigateToChat: InternalNavigators?.Chat || Router.NavigateToChat?.bind(Router), NavigateToLibraryTab: createNavigationFunction("LibraryTab", win => win.Navigator),
NavigateToLibraryTab: InternalNavigators?.LibraryTab || Router.NavigateToLibraryTab?.bind(Router), NavigateToLayoutPreview: Router.NavigateToLayoutPreview?.bind(Router),
NavigateToLayoutPreview: Router.NavigateToLayoutPreview?.bind(Router), NavigateToSteamWeb: createNavigationFunction("NavigateToSteamWeb"),
NavigateToSteamWeb: Router.WindowStore?.GamepadUIMainWindowInstance?.NavigateToSteamWeb?.bind( OpenSideMenu: createNavigationFunction("OpenSideMenu", win => win.MenuStore),
Router.WindowStore.GamepadUIMainWindowInstance, OpenQuickAccessMenu: createNavigationFunction("OpenQuickAccessMenu", win => win.MenuStore),
), OpenMainMenu: createNavigationFunction("OpenMainMenu", win => win.MenuStore),
OpenSideMenu: Router.WindowStore?.GamepadUIMainWindowInstance?.MenuStore.OpenSideMenu?.bind( CloseSideMenus: createNavigationFunction("CloseSideMenus", win => win.MenuStore),
Router.WindowStore.GamepadUIMainWindowInstance.MenuStore, OpenPowerMenu: Router.OpenPowerMenu?.bind(Router),
), } as Navigation;
OpenQuickAccessMenu: Router.WindowStore?.GamepadUIMainWindowInstance?.MenuStore.OpenQuickAccessMenu?.bind(
Router.WindowStore.GamepadUIMainWindowInstance.MenuStore,
),
OpenMainMenu: Router.WindowStore?.GamepadUIMainWindowInstance?.MenuStore.OpenMainMenu?.bind(
Router.WindowStore.GamepadUIMainWindowInstance.MenuStore,
),
CloseSideMenus: Router.CloseSideMenus?.bind(Router),
OpenPowerMenu: Router.OpenPowerMenu?.bind(Router),
} as Navigation;
Object.assign(Navigation, newNavigation); Object.assign(Navigation, newNavigation);
})();
} catch (e) { } catch (e) {
console.error('[DFL:Router]: Error initializing Navigation interface', e); logger.error('Error initializing Navigation interface', e);
} }

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 // 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 = () => { window.SP_REACT.createElement = () => {
loggingEnabled && logger.debug("createElement hook called"); loggingEnabled && logger.debug("createElement hook called");
loggingEnabled && console.trace("createElement trace");
return Object.create(component.prototype); 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. // 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", { Object.defineProperty(component, "contextType", {
configurable: true, configurable: true,
get: function () { get: function () {
loggingEnabled && logger.debug("get contexttype", this, stubsApplied); loggingEnabled && logger.debug("get contexttype", this, stubsApplied, renderHookStep);
applyStubsIfNeeded(); loggingEnabled && console.trace("contextType trace");
if (renderHookStep == 0) renderHookStep = 1;
else if (renderHookStep == 3) renderHookStep = 4;
return this._contextType; return this._contextType;
}, },
set: function (value) { 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 // Always accessed directly after contextType for the path we want to catch.
Object.defineProperty(component, "getDerivedStateFromProps", { Object.defineProperty(component, "contextTypes", {
configurable: true, configurable: true,
get: function () { get: function () {
loggingEnabled && logger.debug("get getDerivedStateFromProps", this, stubsApplied); loggingEnabled && logger.debug("get contexttypes", this, stubsApplied, renderHookStep);
removeStubsIfNeeded(); loggingEnabled && console.trace("contextTypes trace");
return this._getDerivedStateFromProps; if (renderHookStep == 1) {
renderHookStep = 2;
applyStubsIfNeeded();
};
return this._contextTypes;
}, },
set: function (value) { set: function (value) {
this._getDerivedStateFromProps = value; this._contextTypes = value;
} }
}); });
@@ -99,11 +108,32 @@ export function injectFCTrampoline(component: FC, customHooks?: any): FCTrampoli
return this._updater; return this._updater;
}, },
set: function (value) { set: function (value) {
loggingEnabled && logger.debug("set updater", this, value, stubsApplied); loggingEnabled && logger.debug("set updater", this, value, stubsApplied, renderHookStep);
removeStubsIfNeeded(); loggingEnabled && console.trace("updater trace");
if (renderHookStep == 2) {
renderHookStep = 0;
removeStubsIfNeeded();
}
return this._updater = value; 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; return userComponent;
} }

View File

@@ -104,19 +104,15 @@ export function wrapReactClass(node: any, prop: any = 'type') {
export function getReactRoot(o: HTMLElement | Element | Node) { export function getReactRoot(o: HTMLElement | Element | Node) {
return ( return (
// @ts-expect-error 7053 (o as any)[Object.keys(o).find((k) => k.startsWith('__reactContainer$')) as string] ||
o[Object.keys(o).find((k) => k.startsWith('__reactContainer$')) as string] || (o as any)['_reactRootContainer']?._internalRoot?.current
// @ts-expect-error 7053
o['_reactRootContainer']?._internalRoot?.current
); );
} }
export function getReactInstance(o: HTMLElement | Element | Node) { export function getReactInstance(o: HTMLElement | Element | Node) {
return ( return (
// @ts-expect-error 7053 (o as any)[Object.keys(o).find((k) => k.startsWith('__reactFiber')) as string] ||
o[Object.keys(o).find((k) => k.startsWith('__reactFiber')) as string] || (o as any)[Object.keys(o).find((k) => k.startsWith('__reactInternalInstance')) as string]
// @ts-expect-error 7053
o[Object.keys(o).find((k) => k.startsWith('__reactInternalInstance')) as string]
); );
} }