mirror of
https://gitlab.com/ryzen-controller-team/ryzen-controller.git
synced 2024-12-22 18:13:28 +07:00
feat: #39 You can now define preset to be applied on AC status change.
This commit is contained in:
parent
2152f32b8f
commit
55d13e2c5a
@ -15,6 +15,7 @@ ready(function(){
|
||||
checkForAdminRights();
|
||||
displayVersion();
|
||||
reApplyPeriodically(require('electron-settings').get('settings.reapply_periodically'));
|
||||
handleAcStatusChanges();
|
||||
if (isWindows() && isDevMode()) {
|
||||
recreateShortcut();
|
||||
}
|
||||
|
@ -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`));
|
||||
});
|
||||
}
|
||||
|
122
js/preset.js
122
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 += '<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) {
|
||||
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) {
|
||||
@ -112,22 +149,89 @@ function preset_updateList() {
|
||||
}
|
||||
valueSummary.join(', ');
|
||||
|
||||
content += `
|
||||
<li class="uk-margin">
|
||||
content += /*html*/`
|
||||
<tr class="uk-margin">
|
||||
<td>
|
||||
<span class="uk-text-lead">${presetName}</span>
|
||||
<button class="uk-button uk-button-danger uk-align-right" type="button" onClick="preset_deletion('${presetName}')">
|
||||
<i class="uk-text-small">${valueSummary}</i>
|
||||
</td>
|
||||
<td class="uk-table-expand">
|
||||
<label style="cursor: pointer;">
|
||||
<input
|
||||
value="${presetName}"
|
||||
class="uk-radio onAcStatusChange"
|
||||
type="radio"
|
||||
name="update-ac-plugged-in"
|
||||
${presetName === require('electron-settings').get('auto-apply.update-ac-plugged-in') ? 'checked' : ''}
|
||||
/>
|
||||
AC plugged 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 plugged 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 uk-align-right" type="button" onClick="preset_apply('${presetName}')">
|
||||
<button class="uk-button uk-button-primary" type="button" onClick="preset_apply('${presetName}')">
|
||||
Apply
|
||||
</button>
|
||||
<i class="uk-text-small">${valueSummary}</i>
|
||||
</li>
|
||||
</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 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 discharging
|
||||
</label>
|
||||
</td>
|
||||
<td></td>
|
||||
</tr>
|
||||
<tbody>
|
||||
</table>
|
||||
`;
|
||||
presetTab.innerHTML = content;
|
||||
var acStatusChangeRadios = document.querySelectorAll('.onAcStatusChange');
|
||||
|
||||
Array.from(acStatusChangeRadios).forEach(radio => {
|
||||
radio.addEventListener('click', function(event) {
|
||||
preset_enableAutoApplyOnAcStatusChange(this.name, this.value);
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user