Monitoring power changes

To watch different power states in your machine, we use the powerMonitor module, which is available in Electron module. It provides events that you can use to watch various power states with your machine. Basically, this module provides four events--suspend, resume, on-ac, and on-battery. These events should be attached to the context only after the app emits the ready event. The following is the example to monitor the power state of your machine from the application:

const electron = require('electron');
const{app, BrowserWindow, dialog } = electron;

let appShell;
const appUrl = `file://${__dirname}/index.html`;

function createElectronShell() {
appShell = new BrowserWindow({ width: 800, height: 600 });
appShell.loadURL(appUrl);

electron.powerMonitor.on('suspend', () => {
dialog.showMessageBox({
type: 'warning',
buttons: ['Ok'],
title: 'Warning',
message: 'The system is going to sleep'
})
})

electron.powerMonitor.on('on-ac', () => {
dialog.showMessageBox({
type: 'info',
buttons: ['Ok'],
title: 'Warning',
message: 'System changes to ac-power'
});
});

electron.powerMonitor.on('on-battery', () => {
dialog.showMessageBox({
type: 'warning',
buttons: ['Ok'],
message: 'System changes to battery power'
});

});

}
app.on('ready', createElectronShell);

Like the preceding code, you can watch for a different power state of your application. You can watch suspend, on-ac, on-battery, and resume event using the powerMonitor module. You should not attach any event handler to the powerMonitor class until your application emits the ready event.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.51.246