From d99f332523c1e70df8ed1f0d024cc6dd77607433 Mon Sep 17 00:00:00 2001 From: AAGaming Date: Fri, 12 May 2023 02:02:04 +0000 Subject: Initial implementation of global DFL instance (#451) --- frontend/src/developer.tsx | 46 +--------------------- frontend/src/index.ts | 6 +++ frontend/src/index.tsx | 95 ---------------------------------------------- frontend/src/start.tsx | 77 +++++++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 140 deletions(-) create mode 100644 frontend/src/index.ts delete mode 100644 frontend/src/index.tsx create mode 100644 frontend/src/start.tsx (limited to 'frontend/src') diff --git a/frontend/src/developer.tsx b/frontend/src/developer.tsx index 56d28fbf..30b8cf91 100644 --- a/frontend/src/developer.tsx +++ b/frontend/src/developer.tsx @@ -1,23 +1,4 @@ -import { - Navigation, - ReactRouter, - Router, - fakeRenderComponent, - findInReactTree, - findInTree, - findModule, - findModuleChild, - gamepadDialogClasses, - gamepadSliderClasses, - playSectionClasses, - quickAccessControlsClasses, - quickAccessMenuClasses, - scrollClasses, - scrollPanelClasses, - sleep, - staticClasses, - updaterFieldClasses, -} from 'decky-frontend-lib'; +import { findModuleChild, sleep } from 'decky-frontend-lib'; import { useTranslation } from 'react-i18next'; import { FaReact } from 'react-icons/fa'; @@ -80,29 +61,4 @@ export async function startup() { if ((isRDTEnabled && !window.deckyHasConnectedRDT) || (!isRDTEnabled && window.deckyHasConnectedRDT)) setShouldConnectToReactDevTools(isRDTEnabled); - - logger.log('Exposing decky-frontend-lib APIs as DFL'); - window.DFL = { - findModuleChild, - findModule, - Navigation, - Router, - ReactRouter, - ReactUtils: { - fakeRenderComponent, - findInReactTree, - findInTree, - }, - classes: { - scrollClasses, - staticClasses, - playSectionClasses, - scrollPanelClasses, - updaterFieldClasses, - gamepadDialogClasses, - gamepadSliderClasses, - quickAccessMenuClasses, - quickAccessControlsClasses, - }, - }; } diff --git a/frontend/src/index.ts b/frontend/src/index.ts new file mode 100644 index 00000000..6588cb5c --- /dev/null +++ b/frontend/src/index.ts @@ -0,0 +1,6 @@ +// Sets up DFL, then loads start.ts which starts up the loader +(async () => { + console.debug('Setting up decky-frontend-lib...'); + window.DFL = await import('decky-frontend-lib'); + await import('./start'); +})(); diff --git a/frontend/src/index.tsx b/frontend/src/index.tsx deleted file mode 100644 index b9edc0b1..00000000 --- a/frontend/src/index.tsx +++ /dev/null @@ -1,95 +0,0 @@ -import { Navigation, Router, sleep } from 'decky-frontend-lib'; -import i18n from 'i18next'; -import Backend from 'i18next-http-backend'; -import { initReactI18next } from 'react-i18next'; - -import PluginLoader from './plugin-loader'; -import { DeckyUpdater } from './updater'; - -declare global { - interface Window { - DeckyPluginLoader: PluginLoader; - DeckyUpdater?: DeckyUpdater; - importDeckyPlugin: Function; - syncDeckyPlugins: Function; - deckyHasLoaded: boolean; - deckyHasConnectedRDT?: boolean; - deckyAuthToken: string; - DFL?: any; - } -} - -(async () => { - try { - if (!Router.NavigateToAppProperties || !Router.NavigateToLibraryTab || !Router.NavigateToInvites) { - while (!Navigation.NavigateToAppProperties) await sleep(100); - const shims = { - NavigateToAppProperties: Navigation.NavigateToAppProperties, - NavigateToInvites: Navigation.NavigateToInvites, - NavigateToLibraryTab: Navigation.NavigateToLibraryTab, - }; - (Router as unknown as any).deckyShim = true; - Object.assign(Router, shims); - } - } catch (e) { - console.error('[DECKY]: Error initializing Navigation interface shims', e); - } -})(); - -(async () => { - window.deckyAuthToken = await fetch('http://127.0.0.1:1337/auth/token').then((r) => r.text()); - - i18n - .use(Backend) - .use(initReactI18next) - .init({ - load: 'currentOnly', - detection: { - order: ['querystring', 'navigator'], - lookupQuerystring: 'lng', - }, - //debug: true, - lng: navigator.language, - fallbackLng: 'en-US', - interpolation: { - escapeValue: true, - }, - returnEmptyString: false, - backend: { - loadPath: 'http://127.0.0.1:1337/locales/{{lng}}.json', - customHeaders: { - Authentication: window.deckyAuthToken, - }, - requestOptions: { - credentials: 'include', - }, - }, - }); - - window.DeckyPluginLoader?.dismountAll(); - window.DeckyPluginLoader?.deinit(); - window.DeckyPluginLoader = new PluginLoader(); - window.DeckyPluginLoader.init(); - window.importDeckyPlugin = function (name: string, version: string) { - window.DeckyPluginLoader?.importPlugin(name, version); - }; - - window.syncDeckyPlugins = async function () { - const plugins = await ( - await fetch('http://127.0.0.1:1337/plugins', { - credentials: 'include', - headers: { Authentication: window.deckyAuthToken }, - }) - ).json(); - for (const plugin of plugins) { - if (!window.DeckyPluginLoader.hasPlugin(plugin.name)) - window.DeckyPluginLoader?.importPlugin(plugin.name, plugin.version); - } - - window.DeckyPluginLoader.checkPluginUpdates(); - }; - - setTimeout(() => window.syncDeckyPlugins(), 5000); -})(); - -export default i18n; diff --git a/frontend/src/start.tsx b/frontend/src/start.tsx new file mode 100644 index 00000000..94b22ffe --- /dev/null +++ b/frontend/src/start.tsx @@ -0,0 +1,77 @@ +import i18n from 'i18next'; +import Backend from 'i18next-http-backend'; +import { initReactI18next } from 'react-i18next'; + +import PluginLoader from './plugin-loader'; +import { DeckyUpdater } from './updater'; + +declare global { + interface Window { + DeckyPluginLoader: PluginLoader; + DeckyUpdater?: DeckyUpdater; + importDeckyPlugin: Function; + syncDeckyPlugins: Function; + deckyHasLoaded: boolean; + deckyHasConnectedRDT?: boolean; + deckyAuthToken: string; + DFL?: any; + } +} + +(async () => { + window.deckyAuthToken = await fetch('http://127.0.0.1:1337/auth/token').then((r) => r.text()); + + i18n + .use(Backend) + .use(initReactI18next) + .init({ + load: 'currentOnly', + detection: { + order: ['querystring', 'navigator'], + lookupQuerystring: 'lng', + }, + //debug: true, + lng: navigator.language, + fallbackLng: 'en-US', + interpolation: { + escapeValue: true, + }, + returnEmptyString: false, + backend: { + loadPath: 'http://127.0.0.1:1337/locales/{{lng}}.json', + customHeaders: { + Authentication: window.deckyAuthToken, + }, + requestOptions: { + credentials: 'include', + }, + }, + }); + + window.DeckyPluginLoader?.dismountAll(); + window.DeckyPluginLoader?.deinit(); + window.DeckyPluginLoader = new PluginLoader(); + window.DeckyPluginLoader.init(); + window.importDeckyPlugin = function (name: string, version: string) { + window.DeckyPluginLoader?.importPlugin(name, version); + }; + + window.syncDeckyPlugins = async function () { + const plugins = await ( + await fetch('http://127.0.0.1:1337/plugins', { + credentials: 'include', + headers: { Authentication: window.deckyAuthToken }, + }) + ).json(); + for (const plugin of plugins) { + if (!window.DeckyPluginLoader.hasPlugin(plugin.name)) + window.DeckyPluginLoader?.importPlugin(plugin.name, plugin.version); + } + + window.DeckyPluginLoader.checkPluginUpdates(); + }; + + setTimeout(() => window.syncDeckyPlugins(), 5000); +})(); + +export default i18n; -- cgit v1.2.3