Pin-by-name support
This commit is contained in:
parent
fc40a1b522
commit
05703f4209
3 changed files with 57 additions and 38 deletions
|
|
@ -8,41 +8,36 @@
|
|||
</head>
|
||||
|
||||
<body>
|
||||
<sdpi-item label="Server Index">
|
||||
<sdpi-textfield
|
||||
setting="serverIndex"
|
||||
type="number"
|
||||
value="0"
|
||||
placeholder="0"
|
||||
pattern="\d*"
|
||||
required
|
||||
showlabels
|
||||
></sdpi-textfield>
|
||||
<sdpi-item label="Pinned Server">
|
||||
<sdpi-select id="pin-server-select" setting="pinnedServerName"
|
||||
placeholder="Please choose a server to pin"></sdpi-select>
|
||||
</sdpi-item>
|
||||
|
||||
<sdpi-item label="Refresh Frequency (Seconds)">
|
||||
<sdpi-textfield
|
||||
setting="refreshFrequency"
|
||||
type="number"
|
||||
value="5"
|
||||
placeholder="5"
|
||||
pattern="\d*"
|
||||
required
|
||||
showlabels
|
||||
></sdpi-textfield>
|
||||
<sdpi-textfield setting="refreshFrequency" type="number" value="5" placeholder="5" pattern="\d*" required
|
||||
showlabels></sdpi-textfield>
|
||||
</sdpi-item>
|
||||
|
||||
<sdpi-item label="Reset Timeout (Seconds)">
|
||||
<sdpi-textfield
|
||||
setting="resetTimeout"
|
||||
type="number"
|
||||
value="10"
|
||||
placeholder="10"
|
||||
pattern="\d*"
|
||||
required
|
||||
showlabels
|
||||
></sdpi-textfield>
|
||||
<sdpi-textfield setting="resetTimeout" type="number" value="10" placeholder="10" pattern="\d*" required
|
||||
showlabels></sdpi-textfield>
|
||||
</sdpi-item>
|
||||
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', async () => {
|
||||
const resp = await fetch('https://api.forgeserv.net');
|
||||
if (!resp.ok) return;
|
||||
|
||||
const data = await resp.json();
|
||||
const rt = document.getElementById('pin-server-select');
|
||||
data.map(x => x.name).sort().forEach(serverName => {
|
||||
const el = document.createElement('option');
|
||||
el.setAttribute('value', serverName);
|
||||
el.innerText = serverName;
|
||||
rt.appendChild(el);
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
|
@ -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<SelectServerSettingsRaw>,
|
||||
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<SelectServerSettingsRaw>
|
|||
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<SelectServerSettingsRaw>
|
|||
|
||||
this.processors[key].onRotate(evt, this.servers);
|
||||
}
|
||||
|
||||
override async onDidReceiveSettings(
|
||||
evt: DidReceiveSettingsEvent<SelectServerSettingsRaw>
|
||||
) {
|
||||
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? */;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
Loading…
Reference in a new issue