From 9c5801d53d28489af5f48c470daf52e90715fe79 Mon Sep 17 00:00:00 2001 From: AAGaming Date: Fri, 4 Dec 2026 01:16:57 -0500 Subject: add tabs patching api --- frontend/src/components/DeckyQAMState.tsx | 124 ++++++++++++++++++++++++++++++ frontend/src/hooks/tabs.tsx | 92 +++++++++++++++++----- frontend/src/hooks/vr-dashboard-bar.tsx | 2 +- frontend/src/plugin-loader.tsx | 9 ++- 4 files changed, 202 insertions(+), 25 deletions(-) create mode 100644 frontend/src/components/DeckyQAMState.tsx (limited to 'frontend/src') diff --git a/frontend/src/components/DeckyQAMState.tsx b/frontend/src/components/DeckyQAMState.tsx new file mode 100644 index 00000000..48dd8971 --- /dev/null +++ b/frontend/src/components/DeckyQAMState.tsx @@ -0,0 +1,124 @@ +import { QuickAccessTab } from '@decky/ui'; +import { FC, ReactNode, createContext, useContext, useEffect, useState } from 'react'; +import Logger from '../logger'; + +export interface Tab { + id: QuickAccessTab | number; + title: ReactNode; + content: ReactNode; + icon: ReactNode; +} + +export interface TabData { + key: QuickAccessTab | number; + title: ReactNode; + tab: ReactNode; + panel: ReactNode; // the component + decky?: boolean; + initialVisibility?: boolean; +} + +export type TabPatch = (tab: TabData) => TabData; + +interface PublicDeckyQAMState { + tabs: Tab[]; + patches: Map>; +} + +export class DeckyQAMState extends Logger { + private _tabs: Tab[] = []; + private _patches = new Map>(); + + public eventBus = new EventTarget(); + + constructor() { + super('DeckyQAMState'); + } + + publicState(): PublicDeckyQAMState { + return { tabs: this._tabs, patches: this._patches }; + } + + add(tab: Tab) { + this.debug('Adding tab', tab.id, 'to render array'); + this._tabs.push(tab); + this.notifyUpdate(); + } + + removeById(id: QuickAccessTab | number) { + this.debug('Removing tab', id); + this._tabs = this._tabs.filter((tab) => tab.id !== id); + this.notifyUpdate(); + } + + addPatch(tab: QuickAccessTab | number, patch: TabPatch) { + let patchList = this._patches.get(tab); + if (!patchList) { + patchList = new Set(); + this._patches.set(tab, patchList); + } + patchList.add(patch); + this.notifyUpdate(); + return patch; + } + + removePatch(tab: QuickAccessTab | number, patch: TabPatch) { + const patchList = this._patches.get(tab); + patchList?.delete(patch); + if (patchList?.size == 0) { + this._patches.delete(tab); + } + this.notifyUpdate(); + } + + private notifyUpdate() { + this.eventBus.dispatchEvent(new Event('update')); + } +} + +interface DeckyQAMStateContext extends PublicDeckyQAMState { + add(tab: Tab): void; + removeById(id: QuickAccessTab | number): void; + addPatch(tab: QuickAccessTab | number, patch: TabPatch): TabPatch; + removePatch(tab: QuickAccessTab | number, patch: TabPatch): void; + visible: boolean; +} + +const DeckyQAMStateContext = createContext(null as any); + +export const useDeckyQAMState = () => useContext(DeckyQAMStateContext); + +interface Props { + deckyQAMState: DeckyQAMState; + visible: boolean; + children: ReactNode; +} + +export const DeckyQAMStateContextProvider: FC = ({ children, deckyQAMState, visible }) => { + const [publicDeckyQAMState, setPublicDeckyQAMState] = useState({ + ...deckyQAMState.publicState(), + }); + + useEffect(() => { + function onUpdate() { + setPublicDeckyQAMState({ ...deckyQAMState.publicState() }); + } + + deckyQAMState.eventBus.addEventListener('update', onUpdate); + + return () => deckyQAMState.eventBus.removeEventListener('update', onUpdate); + }, []); + + const add = deckyQAMState.add.bind(deckyQAMState); + const removeById = deckyQAMState.removeById.bind(deckyQAMState); + const addPatch = deckyQAMState.addPatch.bind(deckyQAMState); + const removePatch = deckyQAMState.removePatch.bind(deckyQAMState); + + return ( + + {children} + + ); +}; diff --git a/frontend/src/hooks/tabs.tsx b/frontend/src/hooks/tabs.tsx index 35f05472..802f2371 100644 --- a/frontend/src/hooks/tabs.tsx +++ b/frontend/src/hooks/tabs.tsx @@ -12,6 +12,8 @@ import { import { QuickAccessVisibleStateProvider } from '../components/QuickAccessVisibleState'; import Logger from '../logger'; +import { DeckyQAMState, DeckyQAMStateContextProvider, Tab, TabData, TabPatch, useDeckyQAMState } from '../components/DeckyQAMState'; +import { cloneElement, createElement, ReactNode } from 'react'; declare global { interface Window { @@ -19,18 +21,13 @@ declare global { } } -interface Tab { - id: QuickAccessTab | number; - title: any; - content: any; - icon: any; -} +const isPatched = Symbol('is patched'); class TabsHook extends Logger { - // private keys = 7; - tabs: Tab[] = []; + private qamState: DeckyQAMState = new DeckyQAMState(); private qamBrowserViewPatch?: Patch; private qamEmbeddedPatch?: Patch; + private toReplace = new Map(); constructor() { super('TabsHook'); @@ -49,12 +46,19 @@ class TabsHook extends Logger { e?.type?.toString?.()?.includes('QuickAccessMenuEmbedded'), ); + const DeckyQAMWrapper = this.render.bind(this); + const patchHandler = createReactTreePatcher( [(tree) => findInReactTree(tree, (node) => node?.props?.onFocusNavDeactivated)], (args, ret) => { - const tabs = findInReactTree(ret, (x) => x?.props?.tabs); - this.render(tabs.props.tabs, args[0].visible); - return ret; + if (ret._decky) { + return ret; + } + const returnVal = + {ret} + + (returnVal as any)._decky = true; + return returnVal; }, 'TabsHook', ); @@ -87,18 +91,61 @@ class TabsHook extends Logger { } add(tab: Tab) { - this.debug('Adding tab', tab.id, 'to render array'); - this.tabs.push(tab); + return this.qamState.add(tab); + } + + removeById(id: QuickAccessTab | number) { + return this.qamState.removeById(id); + } + + addPatch(tab: QuickAccessTab | number, patch: TabPatch) { + return this.qamState.addPatch(tab, patch); } - removeById(id: number) { - this.debug('Removing tab', id); - this.tabs = this.tabs.filter((tab) => tab.id !== id); + removePatch(tab: QuickAccessTab | number, patch: TabPatch) { + return this.qamState.removePatch(tab, patch); } - render(existingTabs: any[], visible: boolean) { + private render({children}: {children: any[]}) { + const { tabs, patches, visible } = useDeckyQAMState(); + const existingTabs = findInReactTree(children, (x) => x?.props?.tabs).props.tabs; let deckyTabAmount = existingTabs.reduce((prev: any, cur: any) => (cur.decky ? prev + 1 : prev), 0); - if (deckyTabAmount == this.tabs.length) { + for (const tabKey in existingTabs) { + let tab = existingTabs[tabKey]; + if (!tab) continue; + + const replaced = this.toReplace.get(tab.key); + if (replaced) { + existingTabs[tabKey].panel = replaced; + this.toReplace.delete(tab.key); + } + if (tab.key && patches.has(tab.key)) { + this.toReplace.set( + tab.key, + // @ts-ignore + existingTabs[tabKey].panel, + ); + patches.get(tab.key)?.forEach((patch) => { + const oType = existingTabs[tabKey].panel.type; + existingTabs[tabKey].panel = patch({ + ...existingTabs[tabKey].props, + panel: { + ...cloneElement(existingTabs[tabKey].panel), + type: existingTabs[tabKey].panel[isPatched] ? oType : (props) => createElement(oType, props), + }, + }).panel; + existingTabs[tabKey].panel[isPatched] = true; + }); + } + + if (patches.has(tab?.key)){ + const patchList = patches.get(tab?.key)!; + for (const patch of patchList) { + existingTabs[tabKey] = tab = patch(tab); + } + } + } + if (deckyTabAmount == tabs.length) { for (let tab of existingTabs) { if (tab?.decky) { if (tab?.qAMVisibilitySetter) { @@ -108,15 +155,17 @@ class TabsHook extends Logger { } } } - return; + return children; } - for (const { title, icon, content, id } of this.tabs) { - const tab: any = { + + for (const { title, icon, content, id } of tabs) { + const tab: TabData = { key: id, title, tab: icon, decky: true, initialVisibility: visible, + panel: null }; tab.panel = ( @@ -125,6 +174,7 @@ class TabsHook extends Logger { ); existingTabs.push(tab); } + return children; } } diff --git a/frontend/src/hooks/vr-dashboard-bar.tsx b/frontend/src/hooks/vr-dashboard-bar.tsx index 3fe1ad73..8ff551bd 100644 --- a/frontend/src/hooks/vr-dashboard-bar.tsx +++ b/frontend/src/hooks/vr-dashboard-bar.tsx @@ -84,7 +84,7 @@ class VRDashboardBarHook extends Logger { this.renderRightSideButtons(); } - renderRightSideButtons() { + private renderRightSideButtons() { const arr = []; for (const button of this.rightSideButtons) { arr.push( diff --git a/frontend/src/plugin-loader.tsx b/frontend/src/plugin-loader.tsx index b16e1bb1..1427cf10 100644 --- a/frontend/src/plugin-loader.tsx +++ b/frontend/src/plugin-loader.tsx @@ -755,7 +755,7 @@ class PluginLoader extends Logger { // Things will break *very* badly if plugin code touches this outside of @decky/api, so lets make that clear. window.__DECKY_SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED_deckyLoaderAPIInit = { connect: (version: number, pluginName: string) => { - if (version < 1 || version > 2) { + if (version < 1 || version > 3) { console.warn(`Plugin ${pluginName} requested unsupported api version ${version}.`); } @@ -804,6 +804,11 @@ class PluginLoader extends Logger { backendAPI.useQuickAccessVisible = useQuickAccessVisible; } + if (version >= 3) { + backendAPI._version = 3; + backendAPI.tabsHook = this.tabsHook; + } + // Note: version info not available at connection time this.debug(`Plugin ${pluginName} connected to loader API.`); return backendAPI; @@ -829,8 +834,6 @@ class PluginLoader extends Logger { } })(); } - } else { - this.warn(`event ${event} has no listeners`); } }; -- cgit v1.2.3