From 2e61b1caa083d94c994075a4c0e442b71f88930f Mon Sep 17 00:00:00 2001 From: Quentin Decaunes Date: Thu, 7 Mar 2019 20:00:12 +0100 Subject: [PATCH] Fix #2 Added presets capabilities. --- index.html | 31 ++++++++--- js/app.js | 9 +--- js/methods.js | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 166 insertions(+), 14 deletions(-) diff --git a/index.html b/index.html index 16d0049..7b55cd7 100644 --- a/index.html +++ b/index.html @@ -10,7 +10,7 @@

Ryzen Controller

- + - - + + + + + + diff --git a/js/app.js b/js/app.js index fc0f45e..079ae60 100644 --- a/js/app.js +++ b/js/app.js @@ -10,6 +10,7 @@ ready(function(){ reApplyPeriodically(require('electron-settings').get('settings.reapply_periodically')); displayOptionDescription(); recreateShortcut(); + updatePresetList(); document.isStarting = false; settings.set('settings', { ...settings.get('settings'), @@ -21,13 +22,7 @@ ready(function(){ * Will create and handle ryzenadj.exe execution. */ function applyRyzenSettings() { - const settings = { - "--stapm-limit=": document.getElementById('stapm_limit_w').value, - "--fast-limit=": document.getElementById('ppt_fast_limit_w').value, - "--slow-limit=": document.getElementById('ppt_slow_limit_w').value, - "--tctl-temp=": document.getElementById('temperature_limit_c').value, - "--vrmmax-current=": document.getElementById('vrm_current_m_a').value, - }; + const settings = getCurrentSettings("ryzenadjArgs"); const child = require('child_process').execFile; const executablePath = getRyzenAdjExecutablePath(); diff --git a/js/methods.js b/js/methods.js index 0767236..5dcec87 100644 --- a/js/methods.js +++ b/js/methods.js @@ -311,3 +311,143 @@ function recreateShortcut() { } } } + +/** + * Will return an object completed with the current settings from inputs. + * @param {string} keyType "inputId" or "ryzenadjArgs" + */ +function getCurrentSettings(keyType) { + if (keyType === "ryzenadjArgs") { + return { + "--stapm-limit=": document.getElementById('stapm_limit_w').value, + "--fast-limit=": document.getElementById('ppt_fast_limit_w').value, + "--slow-limit=": document.getElementById('ppt_slow_limit_w').value, + "--tctl-temp=": document.getElementById('temperature_limit_c').value, + "--vrmmax-current=": document.getElementById('vrm_current_m_a').value, + }; + } else { + var inputs = document.querySelectorAll('#controller-tab input'); + var currentSettings = {}; + inputs.forEach(element => { + let id = element.id; + let value = element.value; + currentSettings[id] = value; + }); + return currentSettings; + } +} + +/** + * Will save the current settings to a new preset. + */ +function saveToNewPreset() { + const settingsToBeSaved = getCurrentSettings("inputId"); + const currentPresets = require('electron-settings').get('presets') || {}; + var newPresetName = document.getElementById('new_preset_name').value; + + if (!newPresetName) { + notification('danger', 'You must provide a preset name.'); + return; + } + + if (typeof currentPresets[newPresetName] !== "undefined") { + newPresetName = findUnusedNewPresetName(newPresetName); + notification('warning', `This preset name already exist, your preset has been saved with the name "${newPresetName}".`); + } + + const newPresetList = { + ...currentPresets, + [newPresetName]: settingsToBeSaved, + }; + require('electron-settings').set('presets', newPresetList); + appendLog(`saveToNewPreset(): Saved preset ${newPresetName}, ${JSON.stringify(newPresetList)}`); + updatePresetList(); + if (newPresetName === document.getElementById('new_preset_name').value) { + notification('success', `The preset ${newPresetName} has been saved.`); + } +} + +/** + * This recursive function will return an available preset name to be used to save a preset. + * + * @param {string} newPresetName The preset name to be edited. + * @param {number} suffix The preset name suffix + */ +function findUnusedNewPresetName(newPresetName, suffix = 1) { + const currentPresets = require('electron-settings').get('presets') || {}; + if (typeof currentPresets[`${newPresetName}${suffix}`] !== "undefined") { + suffix++; + return findUnusedNewPresetName(newPresetName, suffix); + } + return `${newPresetName}${suffix}`; +} + +/** + * This will update the preset tab based on saved presets. + */ +function updatePresetList() { + var presetTab = document.getElementById('presetTab'); + const currentPresets = require('electron-settings').get('presets') || {}; + + var content = ''; + content += ''; + presetTab.innerHTML = content; +} + +/** + * This will apply the preset you asked for. + * @param {string} presetName The preset name to be applied. + */ +function applyPreset(presetName) { + const presets = require('electron-settings').get(`presets.${presetName}`); + appendLog(`applyPreset(): preset ${presetName}: ${JSON.stringify(presets)}`); + var ret = require('electron-settings').set("latest_controller_tabs_settings", presets); + appendLog(`applyPreset(): saved preset: ${JSON.stringify(ret)}`); + loadLatestUsedSettings(); + applyRyzenSettings(); +} + +/** + * This will delete the preset you asked for. + * @param {string} presetName The preset name to be deleted. + */ +function presetDeletion(presetName) { + var presets = require('electron-settings').get(`presets`); + delete presets[presetName]; + require('electron-settings').set(`presets`, presets); + notification('success', `The preset ${presetName} has been deleted.`); + updatePresetList(); +}