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 += '
Name | +Apply on | +Action | +|
---|---|---|---|
+ ${presetName} + ${valueSummary} + | ++ + | ++ + | ++ + + | +
Disable auto apply | ++ + | ++ + | ++ |