Progress Bars

Overview

A progress bar enables a window to provide progress information to the user without the need of switching to the window itself.

On Windows, you can use a taskbar button to display a progress bar.

Windows Progress Bar

On macOS, the progress bar will be displayed as a part of the dock icon.

macOS Progress Bar

On Linux, the Unity graphical interface also has a similar feature that allows you to specify the progress bar in the launcher.

Linux Progress Bar

NOTE: on Windows, each window can have its own progress bar, whereas on macOS and Linux (Unity) there can be only one progress bar for the application.


All three cases are covered by the same API - the setProgressBar() method available on an instance of BrowserWindow. To indicate your progress, call this method with a number between 0 and 1. For example, if you have a long-running task that is currently at 63% towards completion, you would call it as setProgressBar(0.63).

Setting the parameter to negative values (e.g. -1) will remove the progress bar. Setting it to a value greater than 1 will indicate an indeterminate progress bar in Windows or clamp to 100% in other operating systems. An indeterminate progress bar remains active but does not show an actual percentage, and is used for situations when you do not know how long an operation will take to complete.

See the API documentation for more options and modes.

Example

In this example, we add a progress bar to the main window that increments over time using Node.js timers.

  1. // fiddle='docs/fiddles/features/progress-bar'
  2. const { app, BrowserWindow } = require('electron')
  3. let progressInterval
  4. const createWindow = () => {
  5. const win = new BrowserWindow({
  6. width: 800,
  7. height: 600
  8. })
  9. win.loadFile('index.html')
  10. const INCREMENT = 0.03
  11. const INTERVAL_DELAY = 100 // ms
  12. let c = 0
  13. progressInterval = setInterval(() => {
  14. // update progress bar to next value
  15. // values between 0 and 1 will show progress, >1 will show indeterminate or stick at 100%
  16. win.setProgressBar(c)
  17. // increment or reset progress bar
  18. if (c < 2) c += INCREMENT
  19. else c = 0
  20. }, INTERVAL_DELAY)
  21. }
  22. app.whenReady().then(createWindow)
  23. // before the app is terminated, clear both timers
  24. app.on('before-quit', () => {
  25. clearInterval(progressInterval)
  26. })
  27. app.on('window-all-closed', () => {
  28. if (process.platform !== 'darwin') {
  29. app.quit()
  30. }
  31. })
  32. app.on('activate', () => {
  33. if (BrowserWindow.getAllWindows().length === 0) {
  34. createWindow()
  35. }
  36. })

After launching the Electron application, the dock (macOS) or taskbar (Windows, Unity) should show a progress bar that starts at zero and progresses through 100% to completion. It should then show indeterminate (Windows) or pin to 100% (other operating systems) briefly and then loop.

macOS dock progress bar

For macOS, the progress bar will also be indicated for your application when using Mission Control:

Mission Control Progress Bar