Open In App

Progress Bars in ElectronJS | Set – 2

This article is a continuation of the article: Progress Bars in ElectronJS | Set – 1. In the previous article, we discussed how to create progress bars in the taskbar/dock icon of the Electron application using the Instance method of the BrowserWindow object. In this article, we will implement a Custom Progress Bar within the Electron Application using the electron-progressbar npm package that we had installed earlier during the application setup. We will continue using the same code base from where we left off in the previous article. 

The project structure and the package.json file also remain the same. As discussed in the previous article, the electron-progressbar package provides an easy and highly customizable API to display and control progress bars within the Electron application. This package can only be imported and used directly in the Main Process. To import and use this package in the Renderer Process, we need to perform an additional change to the main.js file. We cannot use the Electron remote module in this case since it is an external package. Also, this package can only be used after the ready event of the app Module is emitted in the Main Process of the application.






const { app, BrowserWindow } = require('electron')
global.ProgressBar = require('electron-progressbar');




<h3>Progress Bar in Electron</h3>
<button id="progress">
    Click to Launch BrowserWindow Progress Bar
</button>
<br><br>
<button id="custom">
    Click to Launch Electron ProgressBar from Package
</button>




const { getGlobal, app, BrowserWindow } = electron.remote
// Importing ProgressBar in the Renderer Process from global
// Actual Import in main.js file
const ProgressBar = getGlobal('ProgressBar');
 
// Cannot Import Directly in the Renderer Process
// const ProgressBar = require('electron-progressbar')
 
let custom = document.getElementById('custom');
custom.addEventListener('click', () => {
    let progressBar = new ProgressBar({
        abortOnError: false,
        // Determinate Progress Bar
        indeterminate: false,
        initialValue: 0,
        maxValue: 100,
        // On Completion, Progress Bar window will not close.
        closeOnComplete: false,
        title: 'Title of the Progress Bar',
        text: 'Text of the Progress Bar',
        detail: 'Detail to Show actual Progress, ' +
            'Can be set as Dynamic to show actual Status',
        style: {
            text: {},
            detail: {},
            bar: { 'width': '100%', 'background-color': '#BBE0F1' },
            value: {}
        },
        browserWindow: {
            parent: null,
            modal: true,
            resizable: false,
            closable: false,
            minimizable: false,
            maximizable: false,
            width: 500,
            height: 170,
            // Important - If not passed, Progress Bar will not be displayed.
            webPreferences: {
                nodeIntegration: true
            }
        }
    });
 
    progressBar
        .on('completed', (value) => {
            console.log(progressBar.isCompleted());
            console.log('Progress Bar Completed');
            progressBar.detail = 'Task completed. Exiting...';
        })
        .on('ready', () => {
            console.log(progressBar.getOptions())
        })
        .on('aborted', (value) => {
            console.log(`aborted... ${value}`);
        })
        .on('progress', (value) => {
            progressBar.detail =
                `Value ${value} out of ${progressBar.getOptions().maxValue}...`;
        });
 
    setInterval(function () {
        if (!progressBar.isCompleted()) {
            progressBar.value += 1;
        }
    }, 100);
});

The new ProgressBar(options) Constructor is used to create and initialize a new progress bar within the application. We need to pass in options to object to this constructor for initializing the progress bar with custom configurations. It takes in the following parameters. This Constructor returns an Instance of the ProgressBar

The Instance of the ProgressBar returned from the Constructor supports the following Instance Events. The on(eventName, listener) Instance method adds the listener function for the eventName event. No checks are made to see if the listener function has already been added. Multiple calls passing the same combination of eventName event and listener function will result in the listener being added and called multiple times. This Instance method returns a reference to this so that multiple EventListener calls can be chained together to the same ProgressBar Instance. Refer to the code for a better understanding. 



The Instance of the ProgressBar returned from the Constructor supports the following Instance Properties. These Instance Properties can be used to dynamically change the status of the progress bar. 

Additionally, The Instance of the ProgressBar returned from the Constructor supports the following Instance Methods. These Instance methods are useful for controlling the behavior of the progress bar and checking the current status of the progress bar. 

At this point, we have successfully implemented a determinate progress bar which is visible in the application window with the help of the electron-progressbar npm package. For this tutorial, we have made the progress bar dynamic by simulating an actual task using the setInterval() method. Upon launching the Electron application, we should see the following 

Output:


Article Tags :