@decky/ui 4.3.1 → 4.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,8 @@
1
1
  export * from './patcher';
2
- export * from './react';
3
- export * from './react-patching';
4
2
  export * from './static-classes';
3
+ export * from './react/react';
4
+ export * from './react/fc';
5
+ export * from './react/treepatcher';
5
6
  declare global {
6
7
  var FocusNavController: any;
7
8
  var GamepadNavTree: any;
@@ -1,7 +1,8 @@
1
1
  export * from './patcher';
2
- export * from './react';
3
- export * from './react-patching';
4
2
  export * from './static-classes';
3
+ export * from './react/react';
4
+ export * from './react/fc';
5
+ export * from './react/treepatcher';
5
6
  export function joinClassNames(...classes) {
6
7
  return classes.join(' ');
7
8
  }
@@ -0,0 +1,6 @@
1
+ import { type FC } from 'react';
2
+ export interface FCTrampoline {
3
+ component: FC;
4
+ }
5
+ export declare function setFCTrampolineLoggingEnabled(value?: boolean): void;
6
+ export declare function injectFCTrampoline(component: FC, customHooks?: any): FCTrampoline;
@@ -0,0 +1,74 @@
1
+ import { createElement } from 'react';
2
+ import { applyHookStubs, removeHookStubs } from './react';
3
+ import Logger from '../../logger';
4
+ let loggingEnabled = false;
5
+ export function setFCTrampolineLoggingEnabled(value = true) { loggingEnabled = value; }
6
+ ;
7
+ let logger = new Logger('FCTrampoline');
8
+ export function injectFCTrampoline(component, customHooks) {
9
+ const newComponent = function (...args) {
10
+ loggingEnabled && logger.debug("new component rendering with props", args);
11
+ return component.apply(this, args);
12
+ };
13
+ const userComponent = { component: newComponent };
14
+ component.prototype.render = function (...args) {
15
+ loggingEnabled && logger.debug("rendering trampoline", args, this);
16
+ return createElement(userComponent.component, this.props, this.props.children);
17
+ };
18
+ component.prototype.isReactComponent = true;
19
+ let stubsApplied = false;
20
+ let oldCreateElement = window.SP_REACT.createElement;
21
+ const applyStubsIfNeeded = () => {
22
+ if (!stubsApplied) {
23
+ loggingEnabled && logger.debug("applied stubs");
24
+ stubsApplied = true;
25
+ applyHookStubs(customHooks);
26
+ window.SP_REACT.createElement = () => {
27
+ loggingEnabled && logger.debug("createElement hook called");
28
+ return Object.create(component.prototype);
29
+ };
30
+ }
31
+ };
32
+ const removeStubsIfNeeded = () => {
33
+ if (stubsApplied) {
34
+ loggingEnabled && logger.debug("removed stubs");
35
+ stubsApplied = false;
36
+ removeHookStubs();
37
+ window.SP_REACT.createElement = oldCreateElement;
38
+ }
39
+ };
40
+ Object.defineProperty(component, "contextType", {
41
+ configurable: true,
42
+ get: function () {
43
+ loggingEnabled && logger.debug("get contexttype", this, stubsApplied);
44
+ applyStubsIfNeeded();
45
+ return this._contextType;
46
+ },
47
+ set: function (value) {
48
+ this._contextType = value;
49
+ }
50
+ });
51
+ Object.defineProperty(component, "getDerivedStateFromProps", {
52
+ configurable: true,
53
+ get: function () {
54
+ loggingEnabled && logger.debug("get getDerivedStateFromProps", this, stubsApplied);
55
+ removeStubsIfNeeded();
56
+ return this._getDerivedStateFromProps;
57
+ },
58
+ set: function (value) {
59
+ this._getDerivedStateFromProps = value;
60
+ }
61
+ });
62
+ Object.defineProperty(component.prototype, "updater", {
63
+ configurable: true,
64
+ get: function () {
65
+ return this._updater;
66
+ },
67
+ set: function (value) {
68
+ loggingEnabled && logger.debug("set updater", this, value, stubsApplied);
69
+ removeStubsIfNeeded();
70
+ return this._updater = value;
71
+ }
72
+ });
73
+ return userComponent;
74
+ }
@@ -5,6 +5,8 @@ declare global {
5
5
  }
6
6
  }
7
7
  export declare function createPropListRegex(propList: string[], fromStart?: boolean): RegExp;
8
+ export declare function applyHookStubs(customHooks?: any): any;
9
+ export declare function removeHookStubs(): void;
8
10
  export declare function fakeRenderComponent(fun: Function, customHooks?: any): any;
9
11
  export declare function wrapReactType(node: any, prop?: any): any;
10
12
  export declare function wrapReactClass(node: any, prop?: any): any;
@@ -12,10 +12,11 @@ export function createPropListRegex(propList, fromStart = true) {
12
12
  });
13
13
  return new RegExp(regexString);
14
14
  }
15
- export function fakeRenderComponent(fun, customHooks = {}) {
15
+ let oldHooks = {};
16
+ export function applyHookStubs(customHooks = {}) {
16
17
  const hooks = window.SP_REACT.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentDispatcher
17
18
  .current;
18
- let oldHooks = {
19
+ oldHooks = {
19
20
  useContext: hooks.useContext,
20
21
  useCallback: hooks.useCallback,
21
22
  useLayoutEffect: hooks.useLayoutEffect,
@@ -35,8 +36,18 @@ export function fakeRenderComponent(fun, customHooks = {}) {
35
36
  return [val, (n) => (val = n)];
36
37
  };
37
38
  Object.assign(hooks, customHooks);
38
- const res = fun(hooks);
39
+ return hooks;
40
+ }
41
+ export function removeHookStubs() {
42
+ const hooks = window.SP_REACT.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentDispatcher
43
+ .current;
39
44
  Object.assign(hooks, oldHooks);
45
+ oldHooks = {};
46
+ }
47
+ export function fakeRenderComponent(fun, customHooks) {
48
+ const hooks = applyHookStubs(customHooks);
49
+ const res = fun(hooks);
50
+ removeHookStubs();
40
51
  return res;
41
52
  }
42
53
  export function wrapReactType(node, prop = 'type') {
@@ -1,4 +1,4 @@
1
- import { GenericPatchHandler } from './patcher';
1
+ import { GenericPatchHandler } from '../patcher';
2
2
  export type GenericNodeStep = (node: any) => any;
3
3
  export type NodeStep = GenericNodeStep;
4
4
  export type ReactPatchHandler = GenericPatchHandler;
@@ -1,5 +1,5 @@
1
- import Logger from '../logger';
2
- import { afterPatch } from './patcher';
1
+ import Logger from '../../logger';
2
+ import { afterPatch } from '../patcher';
3
3
  import { wrapReactClass, wrapReactType } from './react';
4
4
  let loggingEnabled = false;
5
5
  let perfLoggingEnabled = false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decky/ui",
3
- "version": "4.3.1",
3
+ "version": "4.4.0",
4
4
  "description": "A library for interacting with the Steam frontend in Decky plugins and elsewhere.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,7 +1,8 @@
1
1
  export * from './patcher';
2
- export * from './react';
3
- export * from './react-patching';
4
2
  export * from './static-classes';
3
+ export * from './react/react';
4
+ export * from './react/fc';
5
+ export * from './react/treepatcher';
5
6
 
6
7
  declare global {
7
8
  var FocusNavController: any;
@@ -0,0 +1,109 @@
1
+ // Utilities for patching function components
2
+ import { createElement, type FC } from 'react';
3
+ import { applyHookStubs, removeHookStubs } from './react';
4
+ import Logger from '../../logger';
5
+
6
+ export interface FCTrampoline {
7
+ component: FC
8
+ }
9
+
10
+ let loggingEnabled = false;
11
+
12
+ export function setFCTrampolineLoggingEnabled(value: boolean = true) { loggingEnabled = value };
13
+
14
+ let logger = new Logger('FCTrampoline');
15
+
16
+ /**
17
+ * Directly hooks a function component from its reference, redirecting it to a user-patchable wrapper in its returned object.
18
+ * This only works if the original component when called directly returns either nothing, null, or another child element.
19
+ *
20
+ * This works by tricking react into thinking it's a class component by cleverly working around its class component checks,
21
+ * keeping the unmodified function component intact as a mostly working constructor (as it is impossible to direcly modify a function),
22
+ * stubbing out hooks to prevent errors by detecting setter/getter triggers that occur direcly before/after the class component is instantiated by react,
23
+ * and creating a fake class component render method to trampoline out into your own handler.
24
+ *
25
+ * Due to the nature of this method of hooking a component, please only use this where it is *absolutely necessary.*
26
+ * Incorrect hook stubs can cause major instability, be careful when writing them. Refer to fakeRenderComponent for the hook stub implementation.
27
+ * Make sure your hook stubs can handle all the cases they could possibly need to within the component you are injecting into.
28
+ * You do not need to worry about its children, as they are never called due to the first instance never actually rendering.
29
+ */
30
+ export function injectFCTrampoline(component: FC, customHooks?: any): FCTrampoline {
31
+ // It needs to be wrapped so React doesn't infinitely call the fake class render method.
32
+ const newComponent = function (this: any, ...args: any) {
33
+ loggingEnabled && logger.debug("new component rendering with props", args);
34
+ return component.apply(this, args);
35
+ }
36
+ const userComponent = { component: newComponent };
37
+ // Create a fake class component render method
38
+ component.prototype.render = function (...args: any[]) {
39
+ loggingEnabled && logger.debug("rendering trampoline", args, this);
40
+ // Pass through rendering via creating the component as a child so React can use function component logic instead of class component logic (setting up the hooks)
41
+ return createElement(userComponent.component, this.props, this.props.children);
42
+ };
43
+ component.prototype.isReactComponent = true;
44
+ let stubsApplied = false;
45
+ let oldCreateElement = window.SP_REACT.createElement;
46
+
47
+ const applyStubsIfNeeded = () => {
48
+ if (!stubsApplied) {
49
+ loggingEnabled && logger.debug("applied stubs");
50
+ stubsApplied = true;
51
+ applyHookStubs(customHooks)
52
+ // we have to redirect this to return an object with component's prototype as a constructor returning a value overrides its prototype
53
+ window.SP_REACT.createElement = () => {
54
+ loggingEnabled && logger.debug("createElement hook called");
55
+ return Object.create(component.prototype);
56
+ };
57
+ }
58
+ }
59
+
60
+ const removeStubsIfNeeded = () => {
61
+ if (stubsApplied) {
62
+ loggingEnabled && logger.debug("removed stubs");
63
+ stubsApplied = false;
64
+ removeHookStubs();
65
+ window.SP_REACT.createElement = oldCreateElement;
66
+ }
67
+ }
68
+
69
+ // 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.
70
+ Object.defineProperty(component, "contextType", {
71
+ configurable: true,
72
+ get: function () {
73
+ loggingEnabled && logger.debug("get contexttype", this, stubsApplied);
74
+ applyStubsIfNeeded();
75
+ return this._contextType;
76
+ },
77
+ set: function (value) {
78
+ this._contextType = value;
79
+ }
80
+ });
81
+
82
+ // Undoes the second contextType access we can't detect shortly before render before it's able to cause any damage
83
+ Object.defineProperty(component, "getDerivedStateFromProps", {
84
+ configurable: true,
85
+ get: function () {
86
+ loggingEnabled && logger.debug("get getDerivedStateFromProps", this, stubsApplied);
87
+ removeStubsIfNeeded();
88
+ return this._getDerivedStateFromProps;
89
+ },
90
+ set: function (value) {
91
+ this._getDerivedStateFromProps = value;
92
+ }
93
+ });
94
+
95
+ // Set directly after class is instantiated
96
+ Object.defineProperty(component.prototype, "updater", {
97
+ configurable: true,
98
+ get: function () {
99
+ return this._updater;
100
+ },
101
+ set: function (value) {
102
+ loggingEnabled && logger.debug("set updater", this, value, stubsApplied);
103
+ removeStubsIfNeeded();
104
+ return this._updater = value;
105
+ }
106
+ });
107
+
108
+ return userComponent;
109
+ }
@@ -30,13 +30,15 @@ export function createPropListRegex(propList: string[], fromStart: boolean = tru
30
30
  return new RegExp(regexString);
31
31
  }
32
32
 
33
- export function fakeRenderComponent(fun: Function, customHooks: any = {}): any {
33
+ let oldHooks = {};
34
+
35
+ export function applyHookStubs(customHooks: any = {}): any {
34
36
  const hooks = (window.SP_REACT as any).__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentDispatcher
35
37
  .current;
36
38
 
37
39
  // TODO: add more hooks
38
40
 
39
- let oldHooks = {
41
+ oldHooks = {
40
42
  useContext: hooks.useContext,
41
43
  useCallback: hooks.useCallback,
42
44
  useLayoutEffect: hooks.useLayoutEffect,
@@ -60,9 +62,22 @@ export function fakeRenderComponent(fun: Function, customHooks: any = {}): any {
60
62
 
61
63
  Object.assign(hooks, customHooks);
62
64
 
63
- const res = fun(hooks);
65
+ return hooks;
66
+ }
64
67
 
68
+ export function removeHookStubs() {
69
+ const hooks = (window.SP_REACT as any).__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentDispatcher
70
+ .current;
65
71
  Object.assign(hooks, oldHooks);
72
+ oldHooks = {};
73
+ }
74
+
75
+ export function fakeRenderComponent(fun: Function, customHooks?: any): any {
76
+ const hooks = applyHookStubs(customHooks);
77
+
78
+ const res = fun(hooks); // TODO why'd we do this?
79
+
80
+ removeHookStubs();
66
81
 
67
82
  return res;
68
83
  }
@@ -1,5 +1,5 @@
1
- import Logger from '../logger';
2
- import { GenericPatchHandler, afterPatch } from './patcher';
1
+ import Logger from '../../logger';
2
+ import { GenericPatchHandler, afterPatch } from '../patcher';
3
3
  import { wrapReactClass, wrapReactType } from './react';
4
4
 
5
5
  // TODO max size limit? could implement as a class extending map perhaps