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>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<sdpi-item label="Server Index">
|
<sdpi-item label="Pinned Server">
|
||||||
<sdpi-textfield
|
<sdpi-select id="pin-server-select" setting="pinnedServerName"
|
||||||
setting="serverIndex"
|
placeholder="Please choose a server to pin"></sdpi-select>
|
||||||
type="number"
|
|
||||||
value="0"
|
|
||||||
placeholder="0"
|
|
||||||
pattern="\d*"
|
|
||||||
required
|
|
||||||
showlabels
|
|
||||||
></sdpi-textfield>
|
|
||||||
</sdpi-item>
|
</sdpi-item>
|
||||||
|
|
||||||
<sdpi-item label="Refresh Frequency (Seconds)">
|
<sdpi-item label="Refresh Frequency (Seconds)">
|
||||||
<sdpi-textfield
|
<sdpi-textfield setting="refreshFrequency" type="number" value="5" placeholder="5" pattern="\d*" required
|
||||||
setting="refreshFrequency"
|
showlabels></sdpi-textfield>
|
||||||
type="number"
|
|
||||||
value="5"
|
|
||||||
placeholder="5"
|
|
||||||
pattern="\d*"
|
|
||||||
required
|
|
||||||
showlabels
|
|
||||||
></sdpi-textfield>
|
|
||||||
</sdpi-item>
|
</sdpi-item>
|
||||||
|
|
||||||
<sdpi-item label="Reset Timeout (Seconds)">
|
<sdpi-item label="Reset Timeout (Seconds)">
|
||||||
<sdpi-textfield
|
<sdpi-textfield setting="resetTimeout" type="number" value="10" placeholder="10" pattern="\d*" required
|
||||||
setting="resetTimeout"
|
showlabels></sdpi-textfield>
|
||||||
type="number"
|
|
||||||
value="10"
|
|
||||||
placeholder="10"
|
|
||||||
pattern="\d*"
|
|
||||||
required
|
|
||||||
showlabels
|
|
||||||
></sdpi-textfield>
|
|
||||||
</sdpi-item>
|
</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>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
|
|
@ -2,6 +2,7 @@ import streamDeck, {
|
||||||
action,
|
action,
|
||||||
DialAction,
|
DialAction,
|
||||||
DialRotateEvent,
|
DialRotateEvent,
|
||||||
|
DidReceiveSettingsEvent,
|
||||||
SingletonAction,
|
SingletonAction,
|
||||||
WillAppearEvent,
|
WillAppearEvent,
|
||||||
WillDisappearEvent,
|
WillDisappearEvent,
|
||||||
|
|
@ -28,14 +29,20 @@ class SingleDialProcessor {
|
||||||
this.settings = settings;
|
this.settings = settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
getEffectiveIndex(): number {
|
getEffectiveIndex(servers: Server[]): number {
|
||||||
return this.currentIndexOverride >= 0
|
streamDeck.logger.error(
|
||||||
? this.currentIndexOverride
|
`[getEffectiveIndex]: pinnedServerName == ${this.settings.pinnedServerName}`
|
||||||
: this.settings!.serverIndex;
|
);
|
||||||
|
|
||||||
|
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[]) {
|
public async render(servers: Server[]) {
|
||||||
const index = this.getEffectiveIndex();
|
const index = this.getEffectiveIndex(servers);
|
||||||
const server = servers[index];
|
const server = servers[index];
|
||||||
if (!server) return;
|
if (!server) return;
|
||||||
|
|
||||||
|
|
@ -49,6 +56,10 @@ class SingleDialProcessor {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public updateSettings(settings: SelectServerSettings) {
|
||||||
|
this.settings = settings;
|
||||||
|
}
|
||||||
|
|
||||||
public onRotate(
|
public onRotate(
|
||||||
evt: DialRotateEvent<SelectServerSettingsRaw>,
|
evt: DialRotateEvent<SelectServerSettingsRaw>,
|
||||||
servers: Server[]
|
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.reset(); // Reset the existing timeout or else we'll be stuck in a hell loop
|
||||||
this.currentIndexOverride = wrap(
|
this.currentIndexOverride = wrap(
|
||||||
this.getEffectiveIndex() + evt.payload.ticks
|
this.getEffectiveIndex(servers) + evt.payload.ticks
|
||||||
);
|
);
|
||||||
|
|
||||||
this.render(servers);
|
this.render(servers);
|
||||||
|
|
@ -127,7 +138,7 @@ export class SelectServerAction extends SingletonAction<SelectServerSettingsRaw>
|
||||||
await this.getServers();
|
await this.getServers();
|
||||||
|
|
||||||
const settings = {
|
const settings = {
|
||||||
serverIndex: safeCastToInt(evt.payload.settings.serverIndex),
|
pinnedServerName: evt.payload.settings.pinnedServerName,
|
||||||
refreshFrequency: safeCastToInt(evt.payload.settings.refreshFrequency),
|
refreshFrequency: safeCastToInt(evt.payload.settings.refreshFrequency),
|
||||||
resetTimeout: safeCastToInt(evt.payload.settings.resetTimeout),
|
resetTimeout: safeCastToInt(evt.payload.settings.resetTimeout),
|
||||||
};
|
};
|
||||||
|
|
@ -161,6 +172,19 @@ export class SelectServerAction extends SingletonAction<SelectServerSettingsRaw>
|
||||||
|
|
||||||
this.processors[key].onRotate(evt, this.servers);
|
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 = {
|
type Coordinates = {
|
||||||
|
|
@ -185,13 +209,13 @@ type Server = {
|
||||||
};
|
};
|
||||||
|
|
||||||
type SelectServerSettingsRaw = {
|
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? */;
|
refreshFrequency: string /* How often should we poll for updates? */;
|
||||||
resetTimeout: string /* After how many seconds should we reset? */;
|
resetTimeout: string /* After how many seconds should we reset? */;
|
||||||
};
|
};
|
||||||
|
|
||||||
type SelectServerSettings = {
|
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? */;
|
refreshFrequency: number /* How often should we poll for updates? */;
|
||||||
resetTimeout: number /* After how many seconds should we reset? */;
|
resetTimeout: number /* After how many seconds should we reset? */;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ import streamDeck, { LogLevel } from "@elgato/streamdeck";
|
||||||
import { SelectServerAction } from "./actions/action";
|
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
|
// 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());
|
streamDeck.actions.registerAction(new SelectServerAction());
|
||||||
|
|
||||||
// Finally, connect to the Stream Deck.
|
// Finally, connect to the Stream Deck.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue