From 55d13e2c5a65abc99d1be5130d7df87368f7923c Mon Sep 17 00:00:00 2001 From: Quentin Decaunes Date: Sat, 17 Aug 2019 17:50:02 +0200 Subject: [PATCH] feat: #39 You can now define preset to be applied on AC status change. --- js/app.js | 1 + js/methods.js | 43 ++++++++++++---- js/preset.js | 132 ++++++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 152 insertions(+), 24 deletions(-) diff --git a/js/app.js b/js/app.js index 5b02c64..6f05bd7 100644 --- a/js/app.js +++ b/js/app.js @@ -15,6 +15,7 @@ ready(function(){ checkForAdminRights(); displayVersion(); reApplyPeriodically(require('electron-settings').get('settings.reapply_periodically')); + handleAcStatusChanges(); if (isWindows() && isDevMode()) { recreateShortcut(); } diff --git a/js/methods.js b/js/methods.js index 75c6255..0920065 100644 --- a/js/methods.js +++ b/js/methods.js @@ -202,16 +202,16 @@ function checkForAdminRights() { ); } } else { - const child = require('child_process').execFile; - child('NET SESSION', function(err,so,se) { - if (se.length !== 0) { - notification('warning', - `Warning: you should launch this app as administrator, ` - + `ryzenadj.exe doesn't seems to work correctly without administrator rights.` - ); - } - }); -} + const child = require('child_process').execFile; + child('NET SESSION', function(err,so,se) { + if (se.length !== 0) { + notification('warning', + `Warning: you should launch this app as administrator, ` + + `ryzenadj.exe doesn't seems to work correctly without administrator rights.` + ); + } + }); + } } /** @@ -670,3 +670,26 @@ function updateScheduledStartOnBoot(toBeEnabled) { function isDevMode() { return !require('electron').remote.app.isPackaged; } + +/** + * Will listen for system events and handle status for it. + */ +function handleAcStatusChanges() { + const powerMonitor = require('electron').remote.powerMonitor; + const settings = require('electron-settings'); + + let applyPresetOnAcStatusChange = function(presetName) { + appendLog(`applyPresetOnAcStatusChange(${presetName})`); + if (!presetName) { + return; + } + preset_apply(presetName); + }; + + powerMonitor.on('on-ac', () => { + applyPresetOnAcStatusChange(settings.get(`auto-apply.update-ac-plugged-in`)); + }); + powerMonitor.on('on-battery', () => { + applyPresetOnAcStatusChange(settings.get(`auto-apply.update-ac-plugged-out`)); + }); +} diff --git a/js/preset.js b/js/preset.js index 1b62f21..cd26b61 100644 --- a/js/preset.js +++ b/js/preset.js @@ -85,6 +85,33 @@ function preset_findUnusedPresetName(newPresetName, suffix = 1) { return `${newPresetName}${suffix}`; } +/** + * Will save the preset to be enabled on AC plugged out. + */ +function preset_enableAutoApplyOnAcStatusChange(statusName, presetName) { + const settings = require('electron-settings'); + const status = { + "update-ac-plugged-in": `will be applied on AC plugged in.`, + "update-ac-plugged-out": `will be applied on AC plugged out.`, + }; + + if (typeof status[statusName] === "undefined") { + let message = `Error while updating auto apply on AC status change.`; + notification('danger', message); + console.log(`preset_enableAutoApplyOnAcStatusChange(statusName:"${statusName}", presetName:"${presetName}")`); + Sentry.captureException(new Error(message)); + return; + } + + settings.set(`auto-apply.${statusName}`, presetName); + + if (presetName) { + notification('primary', `Preset "${presetName}" ${status[statusName]}`); + } else { + notification('primary', `No preset ${status[statusName]}`); + } +} + /** * This will update the preset tab based on saved presets. */ @@ -93,10 +120,20 @@ function preset_updateList() { const currentPresets = require('electron-settings').get('presets') || {}; var content = ''; - content += '