From 4498e8a73e6f6ba19521db9ee5a110f05d7b04af Mon Sep 17 00:00:00 2001 From: Mastermindzh Date: Thu, 27 Apr 2023 11:35:00 +0200 Subject: [PATCH] feat: you can now set updateFrequency in the settings window --- src/constants/settings.js | 23 ++++---- src/pages/settings/preload.js | 90 +++++++++++++++++--------------- src/pages/settings/settings.html | 18 ++++++- src/preload.js | 29 +++++++--- src/scripts/settings.js | 25 ++++----- 5 files changed, 110 insertions(+), 75 deletions(-) diff --git a/src/constants/settings.js b/src/constants/settings.js index 85cdb77..eca4bdb 100644 --- a/src/constants/settings.js +++ b/src/constants/settings.js @@ -9,34 +9,35 @@ * windowBounds: { width: 800, height: 600 }, */ const settings = { - notifications: "notifications", - api: "api", - menuBar: "menuBar", - playBackControl: "playBackControl", - skipArtists: "skipArtists", - skippedArtists: "skippedArtists", adBlock: "adBlock", - disableBackgroundThrottle: "disableBackgroundThrottle", + api: "api", apiSettings: { root: "apiSettings", port: "apiSettings.port", }, - singleInstance: "singleInstance", + disableBackgroundThrottle: "disableBackgroundThrottle", disableHardwareMediaKeys: "disableHardwareMediaKeys", + enableCustomHotkeys: "enableCustomHotkeys", + enableDiscord: "enableDiscord", flags: { disableHardwareMediaKeys: "flags.disableHardwareMediaKeys", gpuRasterization: "flags.gpuRasterization", }, + menuBar: "menuBar", + minimizeOnClose: "minimizeOnClose", mpris: "mpris", - enableCustomHotkeys: "enableCustomHotkeys", + notifications: "notifications", + playBackControl: "playBackControl", + singleInstance: "singleInstance", + skipArtists: "skipArtists", + skippedArtists: "skippedArtists", trayIcon: "trayIcon", - enableDiscord: "enableDiscord", + updateFrequency: "updateFrequency", windowBounds: { root: "windowBounds", width: "windowBounds.width", height: "windowBounds.height", }, - minimizeOnClose: "minimizeOnClose", }; module.exports = settings; diff --git a/src/pages/settings/preload.js b/src/pages/settings/preload.js index 905128d..d986337 100644 --- a/src/pages/settings/preload.js +++ b/src/pages/settings/preload.js @@ -1,20 +1,21 @@ -let trayIcon, - minimizeOnClose, - mpris, +let adBlock, + api, + disableBackgroundThrottle, + disableHardwareMediaKeys, enableCustomHotkeys, enableDiscord, - skipArtists, + gpuRasterization, + menuBar, + minimizeOnClose, + mpris, notifications, playBackControl, - api, port, - menuBar, - skippedArtists, - adBlock, - disableBackgroundThrottle, singleInstance, - disableHardwareMediaKeys, - gpuRasterization; + skipArtists, + skippedArtists, + trayIcon, + updateFrequency; const { store, settings } = require("./../../scripts/settings"); const { ipcRenderer } = require("electron"); @@ -25,23 +26,24 @@ const { app } = remote; * Sync the UI forms with the current settings */ function refreshSettings() { - notifications.checked = store.get(settings.notifications); - playBackControl.checked = store.get(settings.playBackControl); + adBlock.checked = store.get(settings.adBlock); api.checked = store.get(settings.api); - port.value = store.get(settings.apiSettings.port); - menuBar.checked = store.get(settings.menuBar); - trayIcon.checked = store.get(settings.trayIcon); - mpris.checked = store.get(settings.mpris); + disableBackgroundThrottle.checked = store.get("disableBackgroundThrottle"); + disableHardwareMediaKeys.checked = store.get(settings.flags.disableHardwareMediaKeys); enableCustomHotkeys.checked = store.get(settings.enableCustomHotkeys); enableDiscord.checked = store.get(settings.enableDiscord); + gpuRasterization.checked = store.get(settings.flags.gpuRasterization); + menuBar.checked = store.get(settings.menuBar); minimizeOnClose.checked = store.get(settings.minimizeOnClose); + mpris.checked = store.get(settings.mpris); + notifications.checked = store.get(settings.notifications); + playBackControl.checked = store.get(settings.playBackControl); + port.value = store.get(settings.apiSettings.port); + singleInstance.checked = store.get(settings.singleInstance); skipArtists.checked = store.get(settings.skipArtists); skippedArtists.value = store.get(settings.skippedArtists).join("\n"); - adBlock.checked = store.get(settings.adBlock); - singleInstance.checked = store.get(settings.singleInstance); - disableHardwareMediaKeys.checked = store.get(settings.flags.disableHardwareMediaKeys); - gpuRasterization.checked = store.get(settings.flags.gpuRasterization); - disableBackgroundThrottle.checked = store.get("disableBackgroundThrottle"); + trayIcon.checked = store.get(settings.trayIcon); + updateFrequency.value = store.get(settings.updateFrequency); } /** @@ -109,41 +111,43 @@ window.addEventListener("DOMContentLoaded", () => { document.getElementById(tab).click(); }); - notifications = get("notifications"); - playBackControl = get("playBackControl"); + adBlock = get("adBlock"); api = get("apiCheckbox"); - port = get("port"); - menuBar = get("menuBar"); - trayIcon = get("trayIcon"); - minimizeOnClose = get("minimizeOnClose"); - mpris = get("mprisCheckbox"); + disableBackgroundThrottle = get("disableBackgroundThrottle"); + disableHardwareMediaKeys = get("disableHardwareMediaKeys"); enableCustomHotkeys = get("enableCustomHotkeys"); enableDiscord = get("enableDiscord"); + gpuRasterization = get("gpuRasterization"); + menuBar = get("menuBar"); + minimizeOnClose = get("minimizeOnClose"); + mpris = get("mprisCheckbox"); + notifications = get("notifications"); + playBackControl = get("playBackControl"); + port = get("port"); + trayIcon = get("trayIcon"); skipArtists = get("skipArtists"); skippedArtists = get("skippedArtists"); - adBlock = get("adBlock"); - disableBackgroundThrottle = get("disableBackgroundThrottle"); singleInstance = get("singleInstance"); - disableHardwareMediaKeys = get("disableHardwareMediaKeys"); - gpuRasterization = get("gpuRasterization"); + updateFrequency = get("updateFrequency"); refreshSettings(); - addInputListener(notifications, settings.notifications); - addInputListener(playBackControl, settings.playBackControl); + addInputListener(adBlock, settings.adBlock); addInputListener(api, settings.api); - addInputListener(port, settings.apiSettings.port); - addInputListener(menuBar, settings.menuBar); - addInputListener(trayIcon, settings.trayIcon); - addInputListener(mpris, settings.mpris); + addInputListener(disableBackgroundThrottle, settings.disableBackgroundThrottle); + addInputListener(disableHardwareMediaKeys, settings.flags.disableHardwareMediaKeys); addInputListener(enableCustomHotkeys, settings.enableCustomHotkeys); addInputListener(enableDiscord, settings.enableDiscord); + addInputListener(gpuRasterization, settings.flags.gpuRasterization); + addInputListener(menuBar, settings.menuBar); addInputListener(minimizeOnClose, settings.minimizeOnClose); + addInputListener(mpris, settings.mpris); + addInputListener(notifications, settings.notifications); + addInputListener(playBackControl, settings.playBackControl); + addInputListener(port, settings.apiSettings.port); addInputListener(skipArtists, settings.skipArtists); addTextAreaListener(skippedArtists, settings.skippedArtists); - addInputListener(adBlock, settings.adBlock); - addInputListener(disableBackgroundThrottle, settings.disableBackgroundThrottle); addInputListener(singleInstance, settings.singleInstance); - addInputListener(disableHardwareMediaKeys, settings.flags.disableHardwareMediaKeys); - addInputListener(gpuRasterization, settings.flags.gpuRasterization); + addInputListener(trayIcon, settings.trayIcon); + addInputListener(updateFrequency, settings.updateFrequency); }); diff --git a/src/pages/settings/settings.html b/src/pages/settings/settings.html index 01a3142..d55fb87 100644 --- a/src/pages/settings/settings.html +++ b/src/pages/settings/settings.html @@ -160,7 +160,7 @@
- +
@@ -212,6 +212,22 @@
+
+

Settings

+
+
+

Update frequency

+

+ The amount of time, in milliseconds, that tidal-hifi will refresh its playback info by scraping the + website. + The default of 500 seems to work in more cases but if you are fine with a bit more resource usage you + can decrease it as well. +

+ +
+
+
+

Flags

diff --git a/src/preload.js b/src/preload.js index 24d8845..90f9015 100644 --- a/src/preload.js +++ b/src/preload.js @@ -7,7 +7,7 @@ const { downloadFile } = require("./scripts/download"); const statuses = require("./constants/statuses"); const hotkeys = require("./scripts/hotkeys"); const globalEvents = require("./constants/globalEvents"); -const { skipArtists } = require("./constants/settings"); +const { skipArtists, updateFrequency } = require("./constants/settings"); const notificationPath = `${app.getPath("userData")}/notification.jpg`; const appName = "Tidal Hifi"; let currentSong = ""; @@ -68,14 +68,13 @@ const elements = { /** * returns an array of all artists in the current song * @returns {Array} artists - */ + */ getArtistsArray: function () { const footer = this.get("footer"); if (footer) { const artists = footer.querySelectorAll(this.artists); - if (artists) - return Array.from(artists).map((artist) => artist.textContent); + if (artists) return Array.from(artists).map((artist) => artist.textContent); } return []; }, @@ -84,10 +83,9 @@ const elements = { * unify the artists array into a string separated by commas * @param {Array} artistsArray * @returns {String} artists - */ + */ getArtistsString: function (artistsArray) { - if (artistsArray.length > 0) - return artistsArray.join(", "); + if (artistsArray.length > 0) return artistsArray.join(", "); return "unknown artist(s)"; }, @@ -145,6 +143,21 @@ const elements = { }, }; +/** + * Get the update frequency from the store + * make sure it returns a number, if not use the default + */ +function getUpdateFrequency() { + const storeValue = store.get(updateFrequency); + const defaultValue = 500; + + if (!isNaN(storeValue)) { + return storeValue; + } else { + return defaultValue; + } +} + /** * Play or pause the current song */ @@ -416,7 +429,7 @@ setInterval(function () { } } } -}, 100); +}, getUpdateFrequency()); if (process.platform === "linux" && store.get(settings.mpris)) { try { diff --git a/src/scripts/settings.js b/src/scripts/settings.js index bd30cd0..c010914 100644 --- a/src/scripts/settings.js +++ b/src/scripts/settings.js @@ -7,29 +7,30 @@ let settingsWindow; const store = new Store({ defaults: { - notifications: true, - api: true, - playBackControl: true, - skipArtists: false, - skippedArtists: [""], adBlock: false, - disableBackgroundThrottle: true, - menuBar: true, + api: true, apiSettings: { port: 47836, }, - singleInstance: true, + disableBackgroundThrottle: true, disableHardwareMediaKeys: false, - trayIcon: true, - minimizeOnClose: false, - mpris: false, enableCustomHotkeys: false, enableDiscord: false, - windowBounds: { width: 800, height: 600 }, flags: { gpuRasterization: true, disableHardwareMediaKeys: false, }, + menuBar: true, + minimizeOnClose: false, + mpris: false, + notifications: true, + playBackControl: true, + singleInstance: true, + skipArtists: false, + skippedArtists: [""], + trayIcon: true, + updateFrequency: 500, + windowBounds: { width: 800, height: 600 }, }, migrations: { "3.1.0": (migrationStore) => {