From b939274d157257d0a57b339193d961b39ded6207 Mon Sep 17 00:00:00 2001
From: AAGaming <aagaming@riseup.net>
Date: Thu, 13 Jun 2024 18:11:44 -0400
Subject: support new APIs fully

---
 src/index.tsx | 110 ++++++++++++++++++++++++++++------------------------------
 1 file changed, 54 insertions(+), 56 deletions(-)

(limited to 'src/index.tsx')

diff --git a/src/index.tsx b/src/index.tsx
index b43e86a..9bd8951 100755
--- a/src/index.tsx
+++ b/src/index.tsx
@@ -1,67 +1,63 @@
 import {
   ButtonItem,
-  definePlugin,
-  DialogButton,
-  Menu,
-  MenuItem,
-  Navigation,
   PanelSection,
   PanelSectionRow,
-  ServerAPI,
-  showContextMenu,
+  Router,
+  // ServerAPI,
   staticClasses,
-} from "decky-frontend-lib";
-import { VFC } from "react";
+} from "@decky/ui";
+import {
+  addEventListener,
+  removeEventListener,
+  callable,
+  definePlugin,
+  toaster,
+  // routerHook
+} from "@decky/api"
+// import { call, callable } from "@decky/backend";
+import { useState } from "react";
 import { FaShip } from "react-icons/fa";
 
-import logo from "../assets/logo.png";
+// import logo from "../assets/logo.png";
 
 // interface AddMethodArgs {
 //   left: number;
 //   right: number;
 // }
+const add = callable<[first: number, second: number], number>("add");
+const startTimer = callable<[], void>("start_timer");
+function Content() {
+  const [result, setResult] = useState<number | undefined>();
 
-const Content: VFC<{ serverAPI: ServerAPI }> = ({serverAPI}) => {
-  // const [result, setResult] = useState<number | undefined>();
-
-  // const onClick = async () => {
-  //   const result = await serverAPI.callPluginMethod<AddMethodArgs, number>(
-  //     "add",
-  //     {
-  //       left: 2,
-  //       right: 2,
-  //     }
-  //   );
-  //   if (result.success) {
-  //     setResult(result.result);
-  //   }
-  // };
+  const onClick = async () => {
+    const result = await add(Math.random(), Math.random());
+    setResult(result);
+  };
 
   return (
     <PanelSection title="Panel Section">
       <PanelSectionRow>
         <ButtonItem
           layout="below"
-          onClick={(e) =>
-            showContextMenu(
-              <Menu label="Menu" cancelText="CAAAANCEL" onCancel={() => {}}>
-                <MenuItem onSelected={() => {}}>Item #1</MenuItem>
-                <MenuItem onSelected={() => {}}>Item #2</MenuItem>
-                <MenuItem onSelected={() => {}}>Item #3</MenuItem>
-              </Menu>,
-              e.currentTarget ?? window
-            )
-          }
+          onClick={onClick}
         >
-          Server says yolo
+          {result || "Add two numbers via Python"}
         </ButtonItem>
       </PanelSectionRow>
-
       <PanelSectionRow>
+        <ButtonItem
+          layout="below"
+          onClick={() => startTimer()}
+        >
+          {"Start Python timer"}
+        </ButtonItem>
+      </PanelSectionRow>
+
+      {/* <PanelSectionRow>
         <div style={{ display: "flex", justifyContent: "center" }}>
           <img src={logo} />
         </div>
-      </PanelSectionRow>
+      </PanelSectionRow> */}
 
       <PanelSectionRow>
         <ButtonItem
@@ -78,28 +74,30 @@ const Content: VFC<{ serverAPI: ServerAPI }> = ({serverAPI}) => {
   );
 };
 
-const DeckyPluginRouterTest: VFC = () => {
-  return (
-    <div style={{ marginTop: "50px", color: "white" }}>
-      Hello World!
-      <DialogButton onClick={() => Navigation.NavigateToLibraryTab()}>
-        Go to Library
-      </DialogButton>
-    </div>
-  );
-};
-
-export default definePlugin((serverApi: ServerAPI) => {
-  serverApi.routerHook.addRoute("/decky-plugin-test", DeckyPluginRouterTest, {
-    exact: true,
+export default definePlugin(() => {
+  // serverApi.routerHook.addRoute("/decky-plugin-test", DeckyPluginRouterTest, {
+  //   exact: true,
+  // });
+  // console.log("init plugin", call, callable)
+  const listener = addEventListener<[
+    test1: string,
+    test2: boolean,
+    test3: number
+  ]>("test_event", (test1, test2, test3) => {
+    console.log("Template got event", test1, test2, test3)
+    toaster.toast({
+      title: "template got event",
+      body: `${test1}, ${test2}, ${test3}`
+    });
   });
-
   return {
-    title: <div className={staticClasses.Title}>Example Plugin</div>,
-    content: <Content serverAPI={serverApi} />,
+    title: <div className={staticClasses.Title}>API v2 Example Plugin</div>,
+    content: <Content />,
     icon: <FaShip />,
     onDismount() {
-      serverApi.routerHook.removeRoute("/decky-plugin-test");
+      console.log("Unloading")
+      removeEventListener("test_event", listener);
+      // serverApi.routerHook.removeRoute("/decky-plugin-test");
     },
   };
 });
-- 
cgit v1.2.3

