How to Configure BrowserWindow Options for Always-On-Top Windows in Electron

Set the alwaysOnTop: true flag in the BrowserWindow constructor options to create a window that stays above other applications, or use win.setAlwaysOnTop() to toggle the state at runtime.

Electron's BrowserWindow API provides native controls for window layering, allowing developers to configure always-on-top behavior during initialization or dynamically during the application lifecycle. According to the electron/electron source code, this functionality is implemented through a native BaseWindow object that maps JavaScript options to platform-specific Z-order levels. Understanding how to properly configure BrowserWindow options for always-on-top windows ensures your application windows maintain the correct visibility hierarchy across operating systems.

Setting the Always-On-Top Flag at Window Creation

To create a window that remains above other applications from the moment it appears, pass alwaysOnTop: true in the constructor options object. This flag is defined in the BrowserWindowConstructorOptions interface and stored internally on the native BaseWindow object.

const { BrowserWindow } = require('electron');

const win = new BrowserWindow({
  width: 800,
  height: 600,
  alwaysOnTop: true,          // ← enables always-on-top at creation
  title: 'Important'
});

win.loadURL('https://example.com');

In shell/browser/api/electron_api_base_window.cc, the constructor processes these options and invokes BaseWindow::SetAlwaysOnTop to apply the initial state, converting the boolean flag into ui::ZOrderLevel::kFloatingWindow for true values or ui::ZOrderLevel::kNormal for false.

Changing Always-On-Top State at Runtime

For dynamic control, the setAlwaysOnTop method allows you to toggle the window's Z-order after creation. This method accepts an optional second parameter specifying the Z-order level, which determines the window's priority relative to system elements like screensavers.

// Turn off always-on-top
win.setAlwaysOnTop(false);

// Turn it back on with a higher Z-order level (e.g., screen-saver)
win.setAlwaysOnTop(true, 'screen-saver');

The C++ implementation in shell/browser/api/electron_api_base_window.cc (lines 72-81) handles the conversion of the boolean flag and level string into Chromium's ui::ZOrderLevel enum values. Valid level strings include 'normal', 'floating', 'torn-off-menu', 'modal-panel', 'main-menu', 'status', 'pop-up-menu', 'screen-saver', and 'dock'.

Querying the Current Always-On-Top State

To verify whether a window is currently configured to stay on top, call the isAlwaysOnTop method. This queries the underlying native state set during initialization or previous setAlwaysOnTop calls.

if (win.isAlwaysOnTop()) {
  console.log('Window stays on top of other apps');
}

This method is tested in spec/api-browser-window-spec.ts (around line 2661), which validates the JavaScript API surface and level handling across platforms.

Understanding the Native Implementation

When you configure BrowserWindow options for always-on-top windows, the architecture bridges JavaScript and native platform APIs through three distinct layers:

  1. JavaScript layer – Options pass through lib/browser/api/browser-window.ts, which bridges to the native electron_browser_window binding.
  2. Native binding – The options are stored on the underlying BaseWindow instance maintained by the Electron runtime.
  3. Platform implementation – BaseWindow::SetAlwaysOnTop converts the boolean flag into ui::ZOrderLevel values and invokes platform-specific window manager APIs to adjust the Z-order.

This design ensures that the always-on-top state survives window focus changes and can be queried reliably via win.isAlwaysOnTop() regardless of platform-specific window manager differences.

Summary

  • Set alwaysOnTop: true in BrowserWindow constructor options to enable the behavior at creation time
  • Use win.setAlwaysOnTop(boolean, level?) to dynamically change the state, with optional Z-order levels like 'screen-saver' or 'floating'
  • Query the current state anytime using win.isAlwaysOnTop()
  • The implementation spans lib/browser/api/browser-window.ts and shell/browser/api/electron_api_base_window.cc, converting JavaScript options to native ui::ZOrderLevel values

Frequently Asked Questions

Can I change the always-on-top level after creating a BrowserWindow?

Yes. Call win.setAlwaysOnTop(true, level) where level is a string like 'floating' or 'screen-saver'. According to the electron/electron source code in shell/browser/api/electron_api_base_window.cc, this updates the native BaseWindow Z-order immediately without requiring window recreation.

What are the available Z-order levels for always-on-top windows?

Electron supports Chromium-defined levels including 'normal', 'floating', 'torn-off-menu', 'modal-panel', and 'screen-saver'. These map to ui::ZOrderLevel enum values in the C++ layer, with 'screen-saver' representing the highest priority that can appear above system screensavers.

Does the always-on-top state persist when the window is recreated?

No. The alwaysOnTop property is tied to the specific BaseWindow instance. If you close and recreate the BrowserWindow, you must explicitly set alwaysOnTop: true again in the new constructor options or call setAlwaysOnTop(true) after creation, as demonstrated in spec/chromium-spec.ts.

How do I check if a BrowserWindow is currently set to always-on-top?

Use the synchronous method win.isAlwaysOnTop(), which returns a boolean indicating the current state. This queries the native window manager through the BaseWindow implementation, returning the cached state from the last SetAlwaysOnTop call.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →