Merge e37b2f99cc856f819ed87f5621f15b3a00e2b6d1 into 887a3d8a45d7f4241a3e0ca64efbb7200ca6f836

This commit is contained in:
TheRockYT 2024-02-10 03:47:50 -07:00 committed by GitHub
commit 2d383ae828
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 119 additions and 68 deletions

View File

@ -24,6 +24,9 @@ export const settings = {
detailsPrefix: "discord.detailsPrefix", detailsPrefix: "discord.detailsPrefix",
buttonText: "discord.buttonText", buttonText: "discord.buttonText",
includeTimestamps: "discord.includeTimestamps", includeTimestamps: "discord.includeTimestamps",
showSong: "discord.showSong",
idleText: "discord.idleText",
listeningText: "discord.listeningText",
}, },
ListenBrainz: { ListenBrainz: {
root: "ListenBrainz", root: "ListenBrainz",

View File

@ -20,6 +20,11 @@ const switchesWithSettings = {
classToHide: "discord_options", classToHide: "discord_options",
settingsKey: settings.enableDiscord, settingsKey: settings.enableDiscord,
}, },
discord_show_song: {
switch: "discord_show_song",
classToHide: "discord_show_song_options",
settingsKey: settings.discord.showSong,
}
}; };
let adBlock: HTMLInputElement, let adBlock: HTMLInputElement,
@ -49,7 +54,10 @@ let adBlock: HTMLInputElement,
enableWaylandSupport: HTMLInputElement, enableWaylandSupport: HTMLInputElement,
discord_details_prefix: HTMLInputElement, discord_details_prefix: HTMLInputElement,
discord_include_timestamps: HTMLInputElement, discord_include_timestamps: HTMLInputElement,
discord_button_text: HTMLInputElement; discord_button_text: HTMLInputElement,
discord_show_song: HTMLInputElement,
discord_idle_text: HTMLInputElement,
discord_listening_text: HTMLInputElement;
addCustomCss(app); addCustomCss(app);
@ -138,6 +146,9 @@ function refreshSettings() {
discord_details_prefix.value = settingsStore.get(settings.discord.detailsPrefix); discord_details_prefix.value = settingsStore.get(settings.discord.detailsPrefix);
discord_include_timestamps.checked = settingsStore.get(settings.discord.includeTimestamps); discord_include_timestamps.checked = settingsStore.get(settings.discord.includeTimestamps);
discord_button_text.value = settingsStore.get(settings.discord.buttonText); discord_button_text.value = settingsStore.get(settings.discord.buttonText);
discord_show_song.checked = settingsStore.get(settings.discord.showSong);
discord_idle_text.value = settingsStore.get(settings.discord.idleText);
discord_listening_text.value = settingsStore.get(settings.discord.listeningText);
// set state of all switches with additional settings // set state of all switches with additional settings
Object.values(switchesWithSettings).forEach((settingSwitch) => { Object.values(switchesWithSettings).forEach((settingSwitch) => {
@ -251,6 +262,9 @@ window.addEventListener("DOMContentLoaded", () => {
discord_include_timestamps = get("discord_include_timestamps"); discord_include_timestamps = get("discord_include_timestamps");
listenbrainz_delay = get("listenbrainz_delay"); listenbrainz_delay = get("listenbrainz_delay");
discord_button_text = get("discord_button_text"); discord_button_text = get("discord_button_text");
discord_show_song = get("discord_show_song");
discord_listening_text = get("discord_listening_text");
discord_idle_text = get("discord_idle_text")
refreshSettings(); refreshSettings();
addInputListener(adBlock, settings.adBlock); addInputListener(adBlock, settings.adBlock);
@ -285,4 +299,7 @@ window.addEventListener("DOMContentLoaded", () => {
addInputListener(discord_details_prefix, settings.discord.detailsPrefix); addInputListener(discord_details_prefix, settings.discord.detailsPrefix);
addInputListener(discord_include_timestamps, settings.discord.includeTimestamps); addInputListener(discord_include_timestamps, settings.discord.includeTimestamps);
addInputListener(discord_button_text, settings.discord.buttonText); addInputListener(discord_button_text, settings.discord.buttonText);
addInputListener(discord_show_song, settings.discord.showSong, switchesWithSettings.discord_show_song);
addInputListener(discord_idle_text, settings.discord.idleText);
addInputListener(discord_listening_text, settings.discord.listeningText);
}); });

View File

@ -216,6 +216,36 @@
</label> </label>
</div> </div>
<div id="discord_options"> <div id="discord_options">
<div class="group__option" class="hidden">
<div class="group__description">
<h4>Idle Text</h4>
<p>The text displayed on Discord's rich presence while idling in the app.</p>
<input id="discord_idle_text" type="text" class="text-input" name="discord_idle_text" />
</div>
</div>
<div class="group__option" class="hidden">
<div class="group__description">
<h4>Listening Text</h4>
<p>The text displayed on Discord's rich presence while listening to a song.</p>
<input id="discord_listening_text" type="text" class="text-input" name="discord_listening_text" />
</div>
</div>
<div class="group__option" class="hidden">
<div class="group__description">
<h4>Show song</h4>
<p>Show the current song in the Discord client</p>
</div>
<label class="switch">
<input id="discord_show_song" type="checkbox" />
<span class="switch__slider"></span>
</label>
</div>
<div id="discord_show_song_options" class="hidden">
<div class="group__option" class="hidden"> <div class="group__option" class="hidden">
<div class="group__description"> <div class="group__description">
<h4>Include timestamps</h4> <h4>Include timestamps</h4>
@ -243,6 +273,8 @@
</div> </div>
</div> </div>
</div> </div>
</div>
</div> </div>
<div class="group"> <div class="group">
<p class="group__title">ListenBrainz</p> <p class="group__title">ListenBrainz</p>

View File

@ -1,4 +1,4 @@
import { Client } from "discord-rpc"; import { Client, Presence } from "discord-rpc";
import { app, ipcMain } from "electron"; import { app, ipcMain } from "electron";
import { globalEvents } from "../constants/globalEvents"; import { globalEvents } from "../constants/globalEvents";
import { settings } from "../constants/settings"; import { settings } from "../constants/settings";
@ -18,58 +18,54 @@ function timeToSeconds(timeArray: string[]) {
export let rpc: Client; export let rpc: Client;
const observer = () => { const observer = () => {
if (mediaInfo.status === MediaStatus.paused && rpc) { if (rpc) {
rpc.setActivity(idleStatus); rpc.setActivity(getActivity());
} else if (rpc) { }
};
const getActivity = (): Presence | undefined => {
const presence: Presence | undefined = {
largeImageKey: "tidal-hifi-icon",
largeImageText: `TIDAL Hi-Fi ${app.getVersion()}`,
instance: false,
};
if (mediaInfo.status === MediaStatus.paused) {
presence.details = settingsStore.get<string, string>(settings.discord.idleText) ?? "Browsing Tidal";
} else {
const showSong = settingsStore.get<string, boolean>(settings.discord.showSong) ?? false;
if (showSong) {
const includeTimestamps =
settingsStore.get<string, boolean>(settings.discord.includeTimestamps) ?? true;
if (includeTimestamps) {
const currentSeconds = timeToSeconds(mediaInfo.current.split(":")); const currentSeconds = timeToSeconds(mediaInfo.current.split(":"));
const durationSeconds = timeToSeconds(mediaInfo.duration.split(":")); const durationSeconds = timeToSeconds(mediaInfo.duration.split(":"));
const date = new Date(); const date = new Date();
const now = (date.getTime() / 1000) | 0; const now = (date.getTime() / 1000) | 0;
const remaining = date.setSeconds(date.getSeconds() + (durationSeconds - currentSeconds)); const remaining = date.setSeconds(date.getSeconds() + (durationSeconds - currentSeconds));
const detailsPrefix = presence.startTimestamp = now;
settingsStore.get<string, string>(settings.discord.detailsPrefix) ?? "Listening to "; presence.endTimestamp = remaining;
const buttonText = }
settingsStore.get<string, string>(settings.discord.buttonText) ?? "Play on TIDAL";
const includeTimestamps = const detailsPrefix = settingsStore.get<string, string>(settings.discord.detailsPrefix) ?? "Listening to ";
settingsStore.get<string, boolean>(settings.discord.includeTimestamps) ?? true; const buttonText = settingsStore.get<string, string>(settings.discord.buttonText) ?? "Play on TIDAL";
let activity = {
...idleStatus,
...{
startTimestamp: includeTimestamps ? now : undefined,
endTimestamp: includeTimestamps ? remaining : undefined,
},
};
if (mediaInfo.url) { if (mediaInfo.url) {
activity = { presence.details = `${detailsPrefix}${mediaInfo.title}`;
...activity, presence.state = mediaInfo.artists ? mediaInfo.artists : "unknown artist(s)";
...{ presence.largeImageKey = mediaInfo.image;
details: `${detailsPrefix}${mediaInfo.title}`, if (mediaInfo.album) {
state: mediaInfo.artists ? mediaInfo.artists : "unknown artist(s)", presence.largeImageText = mediaInfo.album;
largeImageKey: mediaInfo.image, }
largeImageText: mediaInfo.album ? mediaInfo.album : `${idleStatus.largeImageText}`, presence.buttons = [{ label: buttonText, url: mediaInfo.url }];
buttons: [{ label: buttonText, url: mediaInfo.url }],
},
};
} else { } else {
activity = { presence.details = `Watching ${mediaInfo.title}`;
...activity, presence.state = mediaInfo.artists;
...{
details: `Watching ${mediaInfo.title}`,
state: mediaInfo.artists,
},
};
} }
} else {
rpc.setActivity(activity); presence.details = settingsStore.get<string, string>(settings.discord.listeningText) ?? "Listening Tidal";
} }
}; }
return presence;
const idleStatus = {
details: `Browsing Tidal`,
largeImageKey: "tidal-hifi-icon",
largeImageText: `TIDAL Hi-Fi ${app.getVersion()}`,
instance: false,
}; };
/** /**
@ -80,7 +76,7 @@ export const initRPC = () => {
rpc.login({ clientId }).then( rpc.login({ clientId }).then(
() => { () => {
rpc.on("ready", () => { rpc.on("ready", () => {
rpc.setActivity(idleStatus); rpc.setActivity(getActivity());
}); });
ipcMain.on(globalEvents.updateInfo, observer); ipcMain.on(globalEvents.updateInfo, observer);
}, },

View File

@ -19,6 +19,9 @@ export const settingsStore = new Store({
enableCustomHotkeys: false, enableCustomHotkeys: false,
enableDiscord: false, enableDiscord: false,
discord: { discord: {
showSong: false,
idleText: "Browsing Tidal",
listeningText: "Listening Tidal",
detailsPrefix: "Listening to ", detailsPrefix: "Listening to ",
buttonText: "Play on Tidal", buttonText: "Play on Tidal",
includeTimestamps: true, includeTimestamps: true,