Skip to content

Commit

Permalink
Added option to prevent system sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
Mattk70 committed Aug 5, 2024
1 parent fd8be29 commit 59d62e0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 13 deletions.
11 changes: 10 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,16 @@ <h5 class="offcanvas-title" id="offcanvasExampleLabel">Settings</h5>
<div class="pe-3 form-group rounded p-2 mb-2">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="audio-notification">
<label class="form-check-label" for="audio-notification"></label>Enable analysis completion notifications
<label class="form-check-label" for="audio-notification">Enable analysis completion notifications</label>
</div>
</div>
</div>
<div>
<a class="circle" tabindex="-1" data-bs-toggle="popover" data-bs-trigger="focus" data-bs-title="Prevent system sleep" data-bs-content="When enabled, prevent the system from suspending whilst Chirpity is in use.">?</a>
<div class="pe-3 form-group rounded p-2 mb-2">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="power-save-block">
<label class="form-check-label" for="power-save-block">Prevent system sleep</label>
</div>
</div>
</div>
Expand Down
3 changes: 2 additions & 1 deletion js/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export class State {
this.local = true,
this.incrementor = 1,
this.UUID = 0,
this.track = true
this.track = true,
this.powerSaveBlocker = false
}


Expand Down
22 changes: 14 additions & 8 deletions js/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,9 +506,9 @@ async function showOpenDialog(fileOrFolder) {
}
}

// function powerSave(on) {
// return window.electron.powerSaveBlocker(on);
// }
function powerSave(on) {
return window.electron.powerSaveBlocker(on);
}

const openFileInList = async (e) => {
if (!PREDICTING && e.target.tagName === 'A') {
Expand Down Expand Up @@ -1520,7 +1520,8 @@ const defaultConfig = {
limit: 500,
track: true,
debug: false,
VERSION: VERSION
VERSION: VERSION,
powerSaveBlocker: false
};
let appPath, tempPath, isMac;
window.onload = async () => {
Expand Down Expand Up @@ -1604,7 +1605,6 @@ window.onload = async () => {
document.getElementById('mid-color').value = config.customColormap.mid;
document.getElementById('quiet-color').value = config.customColormap.quiet;
document.getElementById('color-threshold-slider').value = config.customColormap.threshold;

// Audio preferences:
DOM.gain.value = config.audio.gain;
DOM.gainAdjustment.textContent = config.audio.gain + 'dB';
Expand All @@ -1619,6 +1619,10 @@ window.onload = async () => {
DOM.audioDownmix.checked = config.audio.downmix;
setNocmig(config.detect.nocmig);
modelSettingsDisplay();
// Block powersave?
document.getElementById('power-save-block').checked = config.powerSaveBlocker;
powerSave(config.powerSaveBlocker);

contextAwareIconDisplay();
DOM.debugMode.checked = config.debug;
showThreshold(config.detect.confidence);
Expand Down Expand Up @@ -2453,11 +2457,9 @@ function onChartData(args) {
const backendEL = document.getElementById(config.backend);
backendEL.checked = true;
if (config.backend === 'webgl' || config.backend === 'webgpu') {
//powerSave(true)
SNRSlider.disabled = true;
config.filters.SNR = 0;
} else {
// powerSave(false)
DOM.contextAware.disabled = false;
if (DOM.contextAware.checked) {
config.detect.contextAware = true;
Expand Down Expand Up @@ -3661,7 +3663,6 @@ function onChartData(args) {
nodeOnly.forEach(element => element.classList.add('d-none'));
}
}

}

const contextAwareIconDisplay = () => {
Expand Down Expand Up @@ -4420,6 +4421,11 @@ DOM.gain.addEventListener('input', () => {
config.audio.notification = element.checked;
break;
}
case 'power-save-block': {
config.powerSaveBlocker = element.checked;
powerSave(config.powerSaveBlocker);
break;
}
case 'species-week': {
config.useWeek = element.checked;

Expand Down
17 changes: 16 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { app, Menu, dialog, ipcMain, MessageChannelMain, BrowserWindow, globalShortcut } = require('electron');
const { app, Menu, dialog, ipcMain, MessageChannelMain, BrowserWindow, globalShortcut, powerSaveBlocker } = require('electron');
app.commandLine.appendSwitch('disable-renderer-backgrounding');
// WebGPU flags needed for Linux
app.commandLine.appendSwitch('enable-unsafe-webgpu');
Expand All @@ -17,6 +17,7 @@ let files = [];
let DEBUG = false;
let unsavedRecords = false;


//-------------------------------------------------------------------
// Logging

Expand Down Expand Up @@ -436,6 +437,8 @@ app.whenReady().then(async () => {
properties: ['openDirectory']
});
})



mainWindow.webContents.setWindowOpenHandler(({ url, frameName }) => {
require('electron').shell.openExternal(url);
Expand Down Expand Up @@ -526,3 +529,15 @@ ipcMain.handle('saveFile', (event, arg) => {
mainWindow.webContents.send('saveFile', { message: 'file saved!' });
});


let powerSaveID = powerSaveBlocker.start('prevent-app-suspension');
powerSaveBlocker.stop(powerSaveID);
ipcMain.handle('powerSaveControl', (e, on) => {
if (on){
powerSaveID = powerSaveBlocker.start('prevent-app-suspension')
//console.log(powerSaveBlocker.isStarted(powerSaveID), powerSaveID)
} else {
powerSaveBlocker.stop(powerSaveID)
//console.log(powerSaveBlocker.isStarted(powerSaveID), powerSaveID)
}
})
5 changes: 3 additions & 2 deletions preload.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {contextBridge, ipcRenderer} = require("electron");
const {contextBridge, ipcRenderer } = require("electron");
const fs = require('node:fs');
const colormap = require("colormap");
const p = require('node:path');
Expand Down Expand Up @@ -53,7 +53,8 @@ contextBridge.exposeInMainWorld('electron', {
getVersion: () => ipcRenderer.invoke('getVersion'),
getAudio: () => ipcRenderer.invoke('getAudio'),
isMac: () => ipcRenderer.invoke('isMac'),
exitApplication: () => ipcRenderer.invoke('exitApplication')
exitApplication: () => ipcRenderer.invoke('exitApplication'),
powerSaveBlocker: (onOff) => ipcRenderer.invoke('powerSaveControl', onOff)
});

contextBridge.exposeInMainWorld('module', {
Expand Down

0 comments on commit 59d62e0

Please sign in to comment.