mirror of
https://github.com/SteamDeckHomebrew/decky-frontend-lib.git
synced 2026-05-24 03:48:48 +02:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cd61f57a6f | ||
|
|
8eb921e8b7 | ||
|
|
26017e7de4 | ||
|
|
71c7afa1a6 | ||
|
|
d6a08feca0 | ||
|
|
160fbb493f | ||
|
|
076d9eb5e8 |
33
CHANGELOG.md
33
CHANGELOG.md
@@ -1,3 +1,36 @@
|
|||||||
|
# [3.0.0](https://github.com/SteamDeckHomebrew/decky-frontend-lib/compare/v2.0.0...v3.0.0) (2022-09-09)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* **button:** add style prop ([d6a08fe](https://github.com/SteamDeckHomebrew/decky-frontend-lib/commit/d6a08feca0f7c42e88b4d227b2953a28ac6c424d))
|
||||||
|
* **textfield:** extend HTMLAttributes ([71c7afa](https://github.com/SteamDeckHomebrew/decky-frontend-lib/commit/71c7afa1a641b6651e6e73ff5575b665e5e3c48e))
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* **modal:** add more props, refactor ([26017e7](https://github.com/SteamDeckHomebrew/decky-frontend-lib/commit/26017e7de4600cc677a8a1e0881f2e58b3d5fe65))
|
||||||
|
* **serverAPI:** add FilePicker ([8eb921e](https://github.com/SteamDeckHomebrew/decky-frontend-lib/commit/8eb921e8b787a8e5045badff58cd9a1a54038692))
|
||||||
|
|
||||||
|
|
||||||
|
### BREAKING CHANGES
|
||||||
|
|
||||||
|
* **modal:** ModalRoot ->ConfirmModal
|
||||||
|
add the actual ModalRoot which does not contain buttons
|
||||||
|
|
||||||
|
# [2.0.0](https://github.com/SteamDeckHomebrew/decky-frontend-lib/compare/v1.8.3...v2.0.0) (2022-09-04)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* **patcher:** rewrite to support multiple patches ([076d9eb](https://github.com/SteamDeckHomebrew/decky-frontend-lib/commit/076d9eb5e8f22bfa49afc242608698da2ded50e4))
|
||||||
|
|
||||||
|
|
||||||
|
### BREAKING CHANGES
|
||||||
|
|
||||||
|
* **patcher:** All usage of *Patch functions must now store the result and call .unpatch()
|
||||||
|
unpatch() has been removed.
|
||||||
|
|
||||||
## [1.8.3](https://github.com/SteamDeckHomebrew/decky-frontend-lib/compare/v1.8.2...v1.8.3) (2022-09-03)
|
## [1.8.3](https://github.com/SteamDeckHomebrew/decky-frontend-lib/compare/v1.8.2...v1.8.3) (2022-09-03)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "decky-frontend-lib",
|
"name": "decky-frontend-lib",
|
||||||
"version": "1.8.3",
|
"version": "3.0.0",
|
||||||
"lockfileVersion": 2,
|
"lockfileVersion": 2,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "decky-frontend-lib",
|
"name": "decky-frontend-lib",
|
||||||
"version": "1.8.3",
|
"version": "3.0.0",
|
||||||
"license": "GPL-2.0-or-later",
|
"license": "GPL-2.0-or-later",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"minimist": "^1.2.6"
|
"minimist": "^1.2.6"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "decky-frontend-lib",
|
"name": "decky-frontend-lib",
|
||||||
"version": "1.8.3",
|
"version": "3.0.0",
|
||||||
"description": "A library for building decky plugins",
|
"description": "A library for building decky plugins",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import { FC } from 'react';
|
import { CSSProperties, FC } from 'react';
|
||||||
|
|
||||||
import { CommonUIModule } from '../webpack';
|
import { CommonUIModule } from '../webpack';
|
||||||
|
|
||||||
export interface ButtonProps {
|
export interface ButtonProps {
|
||||||
|
style: CSSProperties;
|
||||||
className?: string;
|
className?: string;
|
||||||
noFocusRing?: boolean;
|
noFocusRing?: boolean;
|
||||||
disabled?: boolean;
|
disabled?: boolean;
|
||||||
|
|||||||
@@ -13,11 +13,10 @@ export const showModal: (children: ReactNode, parent?: EventTarget) => void = fi
|
|||||||
});
|
});
|
||||||
|
|
||||||
export interface ModalRootProps {
|
export interface ModalRootProps {
|
||||||
onMiddleButton?(): void;
|
|
||||||
onCancel?(): void;
|
onCancel?(): void;
|
||||||
|
closeModal?(): void;
|
||||||
onOK?(): void;
|
onOK?(): void;
|
||||||
onEscKeypress?(): void;
|
onEscKeypress?(): void;
|
||||||
closeModal?(): void;
|
|
||||||
className?: string;
|
className?: string;
|
||||||
modalClassName?: string;
|
modalClassName?: string;
|
||||||
bAllowFullSize?: boolean;
|
bAllowFullSize?: boolean;
|
||||||
@@ -27,11 +26,24 @@ export interface ModalRootProps {
|
|||||||
bOKDisabled?: boolean;
|
bOKDisabled?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ModalRoot = findModuleChild((m) => {
|
export interface ConfirmModalProps extends ModalRootProps {
|
||||||
|
onMiddleButton?(): void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const ConfirmModal = findModuleChild((m) => {
|
||||||
if (typeof m !== 'object') return undefined;
|
if (typeof m !== 'object') return undefined;
|
||||||
for (let prop in m) {
|
for (let prop in m) {
|
||||||
if (!m[prop]?.prototype?.OK && m[prop]?.prototype?.Cancel && m[prop]?.prototype?.render) {
|
if (!m[prop]?.prototype?.OK && m[prop]?.prototype?.Cancel && m[prop]?.prototype?.render) {
|
||||||
return m[prop];
|
return m[prop];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}) as FC<ModalRootProps>;
|
}) as FC<ConfirmModalProps>;
|
||||||
|
|
||||||
|
export const ModalRoot = findModuleChild((m) => {
|
||||||
|
if (typeof m !== 'object') return undefined;
|
||||||
|
for (let prop in m) {
|
||||||
|
if (m[prop]?.prototype?.OK && m[prop]?.prototype?.Cancel && m[prop]?.prototype?.render) {
|
||||||
|
return m[prop];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}) as FC<ModalRootProps>;
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
import { ChangeEventHandler, ReactNode, VFC } from 'react';
|
import { ChangeEventHandler, HTMLAttributes, ReactNode, VFC } from 'react';
|
||||||
|
|
||||||
import { CommonUIModule, Module } from '../webpack';
|
import { CommonUIModule, Module } from '../webpack';
|
||||||
|
|
||||||
export interface TextFieldProps {
|
export interface TextFieldProps extends HTMLAttributes<HTMLInputElement> {
|
||||||
label?: ReactNode;
|
label?: ReactNode;
|
||||||
requiredLabel?: ReactNode;
|
requiredLabel?: ReactNode;
|
||||||
description?: ReactNode;
|
description?: ReactNode;
|
||||||
|
|||||||
@@ -45,9 +45,15 @@ export interface Toaster {
|
|||||||
toast(toast: ToastData): void;
|
toast(toast: ToastData): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface FilePickerRes {
|
||||||
|
path: string;
|
||||||
|
realpath: string;
|
||||||
|
}
|
||||||
|
|
||||||
export interface ServerAPI {
|
export interface ServerAPI {
|
||||||
routerHook: RouterHook;
|
routerHook: RouterHook;
|
||||||
toaster: Toaster;
|
toaster: Toaster;
|
||||||
|
openFilePicker(startPath: string, includeFiles?: boolean, regex?: RegExp): Promise<FilePickerRes>
|
||||||
callPluginMethod<TArgs = {}, TRes = {}>(methodName: string, args: TArgs): Promise<ServerResponse<TRes>>;
|
callPluginMethod<TArgs = {}, TRes = {}>(methodName: string, args: TArgs): Promise<ServerResponse<TRes>>;
|
||||||
callServerMethod<TArgs = {}, TRes = {}>(methodName: string, args: TArgs): Promise<ServerResponse<TRes>>;
|
callServerMethod<TArgs = {}, TRes = {}>(methodName: string, args: TArgs): Promise<ServerResponse<TRes>>;
|
||||||
fetchNoCors<TRes = {}>(url: RequestInfo, request?: RequestInit): Promise<ServerResponse<TRes>>;
|
fetchNoCors<TRes = {}>(url: RequestInfo, request?: RequestInit): Promise<ServerResponse<TRes>>;
|
||||||
|
|||||||
10
src/utils/index.ts
Normal file
10
src/utils/index.ts
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
export * from "./patcher";
|
||||||
|
export * from "./react";
|
||||||
|
|
||||||
|
export function joinClassNames(...classes: string[]): string {
|
||||||
|
return classes.join(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
export function sleep(ms: number) {
|
||||||
|
return new Promise(res => setTimeout(res, ms));
|
||||||
|
}
|
||||||
112
src/utils/patcher.ts
Normal file
112
src/utils/patcher.ts
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
// TODO: implement storing patches as an option so we can offer unpatchAll selectively
|
||||||
|
// Return this in a replacePatch to call the original method (can still modify args).
|
||||||
|
export let callOriginal = Symbol("DECKY_CALL_ORIGINAL");
|
||||||
|
|
||||||
|
export interface PatchOptions {
|
||||||
|
singleShot?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
type GenericPatchHandler = (args: any[], ret?: any) => any;
|
||||||
|
|
||||||
|
export interface Patch {
|
||||||
|
original: Function;
|
||||||
|
property: string;
|
||||||
|
object: any;
|
||||||
|
patchedFunction: any;
|
||||||
|
hasUnpatched: boolean;
|
||||||
|
handler: GenericPatchHandler;
|
||||||
|
|
||||||
|
unpatch: () => void
|
||||||
|
};
|
||||||
|
|
||||||
|
// let patches = new Set<Patch>();
|
||||||
|
|
||||||
|
export function beforePatch(object: any, property: string, handler: (args: any[]) => any, options: PatchOptions = {}): Patch {
|
||||||
|
const orig = object[property];
|
||||||
|
object[property] = function (...args: any[]) {
|
||||||
|
handler.call(this, args);
|
||||||
|
const ret = patch.original.call(this, ...args);
|
||||||
|
if (options.singleShot) {
|
||||||
|
patch.unpatch();
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
const patch = processPatch(object, property, handler, object[property], orig);
|
||||||
|
return patch;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function afterPatch(object: any, property: string, handler: (args: any[], ret: any) => any, options: PatchOptions = {}): Patch {
|
||||||
|
const orig = object[property];
|
||||||
|
object[property] = function (...args: any[]) {
|
||||||
|
let ret = patch.original.call(this, ...args);
|
||||||
|
ret = handler.call(this, args, ret);
|
||||||
|
if (options.singleShot) {
|
||||||
|
patch.unpatch();
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
const patch = processPatch(object, property, handler, object[property], orig);
|
||||||
|
return patch;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function replacePatch(object: any, property: string, handler: (args: any[]) => any, options: PatchOptions = {}): Patch {
|
||||||
|
const orig = object[property];
|
||||||
|
object[property] = function (...args: any[]) {
|
||||||
|
const ret = handler.call(this, args);
|
||||||
|
if (ret == callOriginal) return patch.original.call(this, ...args);
|
||||||
|
if (options.singleShot) {
|
||||||
|
patch.unpatch();
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
};
|
||||||
|
const patch = processPatch(object, property, handler, object[property], orig);
|
||||||
|
return patch;
|
||||||
|
}
|
||||||
|
|
||||||
|
function processPatch(object: any, property: any, handler: GenericPatchHandler, patchedFunction: any, original: any): Patch {
|
||||||
|
// Assign all props of original function to new one
|
||||||
|
Object.assign(object[property], original);
|
||||||
|
// Allow toString webpack filters to continue to work
|
||||||
|
object[property].toString = () => original.toString();
|
||||||
|
|
||||||
|
// HACK: for compatibility, remove when all plugins are using new patcher
|
||||||
|
Object.defineProperty(object[property], "__deckyOrig", {
|
||||||
|
get: () => patch.original,
|
||||||
|
set: (val: any) => patch.original = val
|
||||||
|
})
|
||||||
|
|
||||||
|
// Build a Patch object of this patch
|
||||||
|
const patch: Patch = {
|
||||||
|
object,
|
||||||
|
property,
|
||||||
|
handler,
|
||||||
|
patchedFunction,
|
||||||
|
original,
|
||||||
|
hasUnpatched: false,
|
||||||
|
unpatch: () => unpatch(patch)
|
||||||
|
};
|
||||||
|
|
||||||
|
object[property].__deckyPatch = patch;
|
||||||
|
|
||||||
|
return patch;
|
||||||
|
}
|
||||||
|
|
||||||
|
function unpatch(patch: Patch): void {
|
||||||
|
const { object, property, handler, patchedFunction, original } = patch;
|
||||||
|
if (patch.hasUnpatched) throw new Error("Function is already unpatched.")
|
||||||
|
let realProp = property;
|
||||||
|
let realObject = object;
|
||||||
|
console.debug("[Patcher] unpatching", {realObject, realProp, object, property, handler, patchedFunction, original, isEqual: realObject[realProp] === patchedFunction})
|
||||||
|
|
||||||
|
// If another patch has been applied to this function after this one, move down until we find the correct patch
|
||||||
|
while (realObject[realProp] && realObject[realProp] !== patchedFunction) {
|
||||||
|
realObject = realObject[realProp].__deckyPatch;
|
||||||
|
realProp = "original";
|
||||||
|
console.debug("[Patcher] moved to next", {realObject, realProp, object, property, handler, patchedFunction, original, isEqual: realObject[realProp] === patchedFunction})
|
||||||
|
}
|
||||||
|
|
||||||
|
realObject[realProp] = realObject[realProp].__deckyPatch.original
|
||||||
|
|
||||||
|
patch.hasUnpatched = true;
|
||||||
|
console.debug("[Patcher] unpatched", {realObject, realProp, object, property, handler, patchedFunction, original, isEqual: realObject[realProp] === patchedFunction})
|
||||||
|
}
|
||||||
@@ -42,62 +42,6 @@ export function fakeRenderComponent(fun: Function): any {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PatchOptions {
|
|
||||||
singleShot?: boolean
|
|
||||||
}
|
|
||||||
|
|
||||||
export function beforePatch(obj: any, name: string, fnc: (args: any[]) => any, options: PatchOptions = {}): void {
|
|
||||||
const orig = obj[name];
|
|
||||||
obj[name] = function (...args: any[]) {
|
|
||||||
fnc.call(this, args);
|
|
||||||
const ret = orig.call(this, ...args);
|
|
||||||
if (options.singleShot) {
|
|
||||||
unpatch(obj, name);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
Object.assign(obj[name], orig);
|
|
||||||
obj[name].toString = () => orig.toString();
|
|
||||||
obj[name].__deckyOrig = orig;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function afterPatch(obj: any, name: string, fnc: (args: any[], ret: any) => any, options: PatchOptions = {}): void {
|
|
||||||
const orig = obj[name];
|
|
||||||
obj[name] = function (...args: any[]) {
|
|
||||||
let ret = orig.call(this, ...args);
|
|
||||||
ret = fnc.call(this, args, ret);
|
|
||||||
if (options.singleShot) {
|
|
||||||
unpatch(obj, name);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
Object.assign(obj[name], orig);
|
|
||||||
obj[name].toString = () => orig.toString();
|
|
||||||
obj[name].__deckyOrig = orig;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function replacePatch(obj: any, name: string, fnc: (args: any[]) => any, options: PatchOptions = {}): void {
|
|
||||||
const orig = obj[name];
|
|
||||||
obj[name] = function (...args: any[]) {
|
|
||||||
let ret = fnc.call(this, args);
|
|
||||||
if (ret == 'CALL_ORIGINAL') ret = orig.call(this, ...args);
|
|
||||||
if (options.singleShot) {
|
|
||||||
unpatch(obj, name);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
};
|
|
||||||
Object.assign(obj[name], orig);
|
|
||||||
obj[name].toString = () => orig.toString();
|
|
||||||
obj[name].__deckyOrig = orig;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO allow one method to be patched and unpatched multiple times independently using IDs in a Map or something
|
|
||||||
export function unpatch(obj: any, name: any): void {
|
|
||||||
if (obj[name].__deckyOrig !== undefined) {
|
|
||||||
obj[name] = obj[name].__deckyOrig;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function wrapReactType(node: any, prop: any = 'type') {
|
export function wrapReactType(node: any, prop: any = 'type') {
|
||||||
return node[prop] = {...node[prop]};
|
return node[prop] = {...node[prop]};
|
||||||
}
|
}
|
||||||
@@ -112,14 +56,6 @@ export function getReactInstance(o: HTMLElement | Element | Node) {
|
|||||||
return o[Object.keys(o).find(k => k.startsWith('__reactInternalInstance')) as string]
|
return o[Object.keys(o).find(k => k.startsWith('__reactInternalInstance')) as string]
|
||||||
}
|
}
|
||||||
|
|
||||||
export function joinClassNames(...classes: string[]): string {
|
|
||||||
return classes.join(" ");
|
|
||||||
}
|
|
||||||
|
|
||||||
export function sleep(ms: number) {
|
|
||||||
return new Promise(res => setTimeout(res, ms));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Based on https://github.com/GooseMod/GooseMod/blob/9ef146515a9e59ed4e25665ed365fd72fc0dcf23/src/util/react.js#L20
|
// Based on https://github.com/GooseMod/GooseMod/blob/9ef146515a9e59ed4e25665ed365fd72fc0dcf23/src/util/react.js#L20
|
||||||
export interface findInTreeOpts {
|
export interface findInTreeOpts {
|
||||||
walkable?: string[],
|
walkable?: string[],
|
||||||
@@ -147,4 +83,4 @@ export const findInTree = (parent: any, filter: findInTreeFilter, opts: findInTr
|
|||||||
|
|
||||||
export const findInReactTree = (node: any, filter: findInTreeFilter) => findInTree(node, filter, { // Specialised findInTree for React nodes
|
export const findInReactTree = (node: any, filter: findInTreeFilter) => findInTree(node, filter, { // Specialised findInTree for React nodes
|
||||||
walkable: [ 'props', 'children', 'child', 'sibling' ]
|
walkable: [ 'props', 'children', 'child', 'sibling' ]
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user