Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 36 ยท Windows, Menus and Tray

Electron Window Management: Multiple, Child and Modal Windows

Manage Electron windows: open several windows, parent and modal dialogs, frameless windows with custom title bars, window state, multi-monitor placement, and a registry pattern. With runnable models.

Real desktop apps rarely have a single window. An editor has one window per project, a settings window, a modal "About" box, a floating palette, perhaps a splash screen. This lesson covers how to create and track several windows, how parent and modal relationships work, how to build a frameless window with your own title bar, and how to place windows well on multiple monitors.

Opening more than one window

Each new BrowserWindow(...) creates an independent window with its own renderer process. The only trick is keeping track of them. A registry (a Map) keyed by a purpose or an id solves most problems:

JavaScript
const { BrowserWindow } = require('electron')
const path = require('node:path')

const windows = new Map()   // name -> BrowserWindow

function openWindow(name, options = {}, page = 'index.html') {
  const existing = windows.get(name)
  if (existing && !existing.isDestroyed()) {
    if (existing.isMinimized()) existing.restore()
    existing.focus()
    return existing                           // reuse instead of opening duplicates
  }

  const win = new BrowserWindow({
    width: 900, height: 650, show: false,
    webPreferences: { preload: path.join(__dirname, '../preload/preload.js') },
    ...options,
  })
  windows.set(name, win)
  win.once('ready-to-show', () => win.show())
  win.on('closed', () => windows.delete(name))
  win.loadFile(path.join(__dirname, '../renderer', page))
  return win
}

This "singleton per name" behavior is what keeps users from opening ten settings windows. For document windows you would use an id (a file path or a document id) as the key instead.

Here is the registry as a runnable model so you can test the lifecycle: reuse, closing and recreating.

Node.js Stand-in: a window registry

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Parent, child and modal windows

Passing parent makes a child window. A child:

  • Always stays above its parent.
  • Is closed when the parent closes.
  • Is minimized and moved with the parent on some platforms.

Adding modal: true makes it block interaction with the parent until it closes. A modal needs a parent.

JavaScript
function openAbout(parent) {
  const about = new BrowserWindow({
    parent,
    modal: true,
    width: 420, height: 320,
    resizable: false, minimizable: false, maximizable: false,
    show: false,
  })
  about.setMenu(null)                         // no menu bar on Windows and Linux
  about.once('ready-to-show', () => about.show())
  about.loadFile('about.html')
}

On macOS, modal windows are "sheets" attached to the parent's title bar, a platform feature you get automatically. Use modal windows sparingly: for destructive confirmations, short forms or "About", never as a place for long work. For simple questions, a native dialog is lighter.

Frameless windows and custom title bars

frame: false removes the native title bar and borders. This allows any look you want, but you take over responsibility for dragging, resizing and window buttons.

JavaScript
const win = new BrowserWindow({
  width: 1000, height: 700,
  frame: false,                // Windows and Linux: no frame at all
  // macOS alternative: keep the traffic lights, hide the bar
  // titleBarStyle: 'hidden',
  // titleBarOverlay: { color: '#0f172a', symbolColor: '#ffffff', height: 36 }, // Windows window controls
})

In your page, mark a region as draggable with CSS and exclude the buttons:

CSS
.titlebar { -webkit-app-region: drag; height: 36px; display: flex; align-items: center; }
.titlebar button { -webkit-app-region: no-drag; }

The buttons call the main process through IPC (win.minimize(), win.maximize() or win.unmaximize(), win.close()), because renderers cannot control windows directly. You can try the layout idea here. The buttons in this preview just change the text, as a real app would call IPC:

A custom title bar layout

Do not forget accessibility: keep real <button> elements with labels, keyboard focus and visible focus styles. Also keep resizing working (frameless windows are resizable by default on Windows if resizable is true; on Linux there can be quirks) and double-click on the drag area should maximize on most platforms, which you can implement in JavaScript.

Window state and displays

The screen module (usable after ready) describes monitors: screen.getPrimaryDisplay(), screen.getAllDisplays(), screen.getDisplayNearestPoint({x, y}). You use workArea (the area minus taskbars) to size and position windows.

JavaScript
const { screen } = require('electron')

const { x, y, width, height } = screen.getPrimaryDisplay().workArea
const win = new BrowserWindow({
  width: 400, height: 300,
  x: x + width - 400 - 16,      // bottom-right corner, with a margin
  y: y + height - 300 - 16,
})

You can listen for display-added, display-removed and display-metrics-changed to reposition windows when monitors change. The following stand-in computes a bottom-right "toast" window position on different screen layouts, including a taskbar that eats part of the screen:

Node.js Stand-in: position a window on a display

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Other useful window features

  • win.setProgressBar(0..1) shows progress on the taskbar or dock icon.
  • win.flashFrame(true) flashes the taskbar entry to get attention.
  • win.setSkipTaskbar(true) hides a window from the taskbar (tool windows).
  • win.setVisibleOnAllWorkspaces(true) for overlays.
  • win.setContentProtection(true) blocks screen capture on supported platforms.
  • win.setOpacity(0.9) and transparent: true windows (use sparingly; performance and platform limits apply).
  • BrowserWindow.getAllWindows() and BrowserWindow.fromId(id) for lookup.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why keep a registry of windows in the main process?

    A Map lets you focus an existing settings window, look windows up later and delete entries on close.
  2. 2. Which options make a window that blocks its parent?

    A modal window must have a parent; interaction with the parent is blocked until it closes.
  3. 3. How does a custom title bar make a region draggable?

    Mark the bar with -webkit-app-region: drag and exclude buttons with no-drag.
  4. 4. Why use display.workArea instead of display.bounds to place a window?

    workArea is the usable area, so windows do not end up hidden under the taskbar.

Summary

  • Track windows in a Map, reuse them when appropriate and delete entries on closed.
  • parent creates child windows; add modal: true to block the parent.
  • frame: false plus -webkit-app-region gives a custom title bar, with window actions going through IPC.
  • Use the screen module and workArea for multi-monitor placement.

Next: Application Menus, Accelerators and Shortcuts