From 05703f420975707a3217c61daf2641eae71622bf Mon Sep 17 00:00:00 2001 From: oitsjustjose Date: Wed, 2 Jul 2025 19:33:06 -0500 Subject: [PATCH] Pin-by-name support --- .../ui/select-server.html | 51 +++++++++---------- src/actions/action.ts | 42 +++++++++++---- src/plugin.ts | 2 +- 3 files changed, 57 insertions(+), 38 deletions(-) diff --git a/net.forgeserv.api.sdPlugin/ui/select-server.html b/net.forgeserv.api.sdPlugin/ui/select-server.html index 43a6ff9..f8000c1 100644 --- a/net.forgeserv.api.sdPlugin/ui/select-server.html +++ b/net.forgeserv.api.sdPlugin/ui/select-server.html @@ -8,41 +8,36 @@ - - + + - + - + + + \ No newline at end of file diff --git a/src/actions/action.ts b/src/actions/action.ts index fa9f4ea..83ebd06 100644 --- a/src/actions/action.ts +++ b/src/actions/action.ts @@ -2,6 +2,7 @@ import streamDeck, { action, DialAction, DialRotateEvent, + DidReceiveSettingsEvent, SingletonAction, WillAppearEvent, WillDisappearEvent, @@ -28,14 +29,20 @@ class SingleDialProcessor { this.settings = settings; } - getEffectiveIndex(): number { - return this.currentIndexOverride >= 0 - ? this.currentIndexOverride - : this.settings!.serverIndex; + getEffectiveIndex(servers: Server[]): number { + streamDeck.logger.error( + `[getEffectiveIndex]: pinnedServerName == ${this.settings.pinnedServerName}` + ); + + if (this.currentIndexOverride >= 0) return this.currentIndexOverride; + return Math.max( + 0, + servers.map((x) => x.name).indexOf(`${this.settings!.pinnedServerName}`) + ); } public async render(servers: Server[]) { - const index = this.getEffectiveIndex(); + const index = this.getEffectiveIndex(servers); const server = servers[index]; if (!server) return; @@ -49,6 +56,10 @@ class SingleDialProcessor { }); } + public updateSettings(settings: SelectServerSettings) { + this.settings = settings; + } + public onRotate( evt: DialRotateEvent, servers: Server[] @@ -60,7 +71,7 @@ class SingleDialProcessor { this.reset(); // Reset the existing timeout or else we'll be stuck in a hell loop this.currentIndexOverride = wrap( - this.getEffectiveIndex() + evt.payload.ticks + this.getEffectiveIndex(servers) + evt.payload.ticks ); this.render(servers); @@ -127,7 +138,7 @@ export class SelectServerAction extends SingletonAction await this.getServers(); const settings = { - serverIndex: safeCastToInt(evt.payload.settings.serverIndex), + pinnedServerName: evt.payload.settings.pinnedServerName, refreshFrequency: safeCastToInt(evt.payload.settings.refreshFrequency), resetTimeout: safeCastToInt(evt.payload.settings.resetTimeout), }; @@ -161,6 +172,19 @@ export class SelectServerAction extends SingletonAction this.processors[key].onRotate(evt, this.servers); } + + override async onDidReceiveSettings( + evt: DidReceiveSettingsEvent + ) { + const key = this.makeKey(evt.action.coordinates as Coordinates); + const proc = this.processors[key]; + proc.updateSettings({ + pinnedServerName: evt.payload.settings.pinnedServerName, + refreshFrequency: safeCastToInt(evt.payload.settings.refreshFrequency), + resetTimeout: safeCastToInt(evt.payload.settings.resetTimeout), + }); + await proc.render(this.servers); + } } type Coordinates = { @@ -185,13 +209,13 @@ type Server = { }; type SelectServerSettingsRaw = { - serverIndex: string /* What ID to reset to */; + pinnedServerName: string /* What server name to pin back to */; refreshFrequency: string /* How often should we poll for updates? */; resetTimeout: string /* After how many seconds should we reset? */; }; type SelectServerSettings = { - serverIndex: number /* What ID to reset to */; + pinnedServerName: string /* What server name to pin back to */; refreshFrequency: number /* How often should we poll for updates? */; resetTimeout: number /* After how many seconds should we reset? */; }; diff --git a/src/plugin.ts b/src/plugin.ts index 8ffcfb3..bbf8fd1 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -3,7 +3,7 @@ import streamDeck, { LogLevel } from "@elgato/streamdeck"; import { SelectServerAction } from "./actions/action"; // We can enable "trace" logging so that all messages between the Stream Deck, and the plugin are recorded. When storing sensitive information -streamDeck.logger.setLevel(LogLevel.WARN); +streamDeck.logger.setLevel(LogLevel.TRACE); streamDeck.actions.registerAction(new SelectServerAction()); // Finally, connect to the Stream Deck.