feat: #39 You can now define preset to be applied on AC status change.

This commit is contained in:
Quentin Decaunes 2019-08-17 17:50:02 +02:00
parent 2152f32b8f
commit 55d13e2c5a
3 changed files with 152 additions and 24 deletions

View File

@ -15,6 +15,7 @@ ready(function(){
checkForAdminRights(); checkForAdminRights();
displayVersion(); displayVersion();
reApplyPeriodically(require('electron-settings').get('settings.reapply_periodically')); reApplyPeriodically(require('electron-settings').get('settings.reapply_periodically'));
handleAcStatusChanges();
if (isWindows() && isDevMode()) { if (isWindows() && isDevMode()) {
recreateShortcut(); recreateShortcut();
} }

View File

@ -202,16 +202,16 @@ function checkForAdminRights() {
); );
} }
} else { } else {
const child = require('child_process').execFile; const child = require('child_process').execFile;
child('NET SESSION', function(err,so,se) { child('NET SESSION', function(err,so,se) {
if (se.length !== 0) { if (se.length !== 0) {
notification('warning', notification('warning',
`Warning: you should launch this app as administrator, ` `Warning: you should launch this app as administrator, `
+ `ryzenadj.exe doesn't seems to work correctly without administrator rights.` + `ryzenadj.exe doesn't seems to work correctly without administrator rights.`
); );
} }
}); });
} }
} }
/** /**
@ -670,3 +670,26 @@ function updateScheduledStartOnBoot(toBeEnabled) {
function isDevMode() { function isDevMode() {
return !require('electron').remote.app.isPackaged; 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`));
});
}

View File

@ -85,6 +85,33 @@ function preset_findUnusedPresetName(newPresetName, suffix = 1) {
return `${newPresetName}${suffix}`; 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. * This will update the preset tab based on saved presets.
*/ */
@ -93,10 +120,20 @@ function preset_updateList() {
const currentPresets = require('electron-settings').get('presets') || {}; const currentPresets = require('electron-settings').get('presets') || {};
var content = ''; var content = '';
content += '<ul class="uk-list">'; content += /*html*/`
<table class="uk-table uk-table-striped uk-table-hover uk-table-middle">
<thead>
<tr>
<th>Name</th>
<th colspan="2">Apply on</th>
<th>Action</th>
</tr>
</thead>
<tbody>
`;
if (Object.keys(currentPresets).length === 0) { if (Object.keys(currentPresets).length === 0) {
content += `<li>No preset has been created yet, import them or use the "Save to preset" button on Controller tab to create one.</li>`; content += /*html*/`<li>No preset has been created yet, import them or use the "Save to preset" button on Controller tab to create one.</li>`;
} }
for (const presetName in currentPresets) { for (const presetName in currentPresets) {
@ -112,22 +149,89 @@ function preset_updateList() {
} }
valueSummary.join(', '); valueSummary.join(', ');
content += ` content += /*html*/`
<li class="uk-margin"> <tr class="uk-margin">
<span class="uk-text-lead">${presetName}</span> <td>
<button class="uk-button uk-button-danger uk-align-right" type="button" onClick="preset_deletion('${presetName}')"> <span class="uk-text-lead">${presetName}</span>
Delete <i class="uk-text-small">${valueSummary}</i>
</button> </td>
<button class="uk-button uk-button-primary uk-align-right" type="button" onClick="preset_apply('${presetName}')"> <td class="uk-table-expand">
Apply <label style="cursor: pointer;">
</button> <input
<i class="uk-text-small">${valueSummary}</i> value="${presetName}"
</li> class="uk-radio onAcStatusChange"
type="radio"
name="update-ac-plugged-in"
${presetName === require('electron-settings').get('auto-apply.update-ac-plugged-in') ? 'checked' : ''}
/>
AC&nbsp;plugged&nbsp;in
</label>
</td>
<td class="uk-table-expand">
<label style="cursor: pointer;">
<input
value="${presetName}"
class="uk-radio onAcStatusChange"
type="radio"
name="update-ac-plugged-out"
${presetName === require('electron-settings').get('auto-apply.update-ac-plugged-out') ? 'checked' : ''}
/>
AC&nbsp;plugged&nbsp;out
</label>
</td>
<td>
<button class="uk-button uk-margin uk-button-danger" type="button" onClick="preset_deletion('${presetName}')">
Delete
</button>
<button class="uk-button uk-button-primary" type="button" onClick="preset_apply('${presetName}')">
Apply
</button>
</td>
</tr>
`; `;
} }
} }
content += '</ul>'; content += /*html*/`
<tr>
<td><span class="uk-align-right">Disable auto apply</span></td>
<td>
<label style="cursor: pointer;">
<input
value=""
class="uk-radio onAcStatusChange"
type="radio"
name="update-ac-plugged-in"
${!require('electron-settings').get('auto-apply.update-ac-plugged-in') ? 'checked' : ''}
/>
On&nbsp;charging
</label>
</td>
<td>
<label style="cursor: pointer;">
<input
value=""
class="uk-radio onAcStatusChange"
type="radio"
name="update-ac-plugged-out"
${!require('electron-settings').get('auto-apply.update-ac-plugged-out') ? 'checked' : ''}
/>
On&nbsp;discharging
</label>
</td>
<td></td>
</tr>
<tbody>
</table>
`;
presetTab.innerHTML = content; presetTab.innerHTML = content;
var acStatusChangeRadios = document.querySelectorAll('.onAcStatusChange');
Array.from(acStatusChangeRadios).forEach(radio => {
radio.addEventListener('click', function(event) {
preset_enableAutoApplyOnAcStatusChange(this.name, this.value);
});
});
} }
/** /**