Window Customization

The BrowserWindow module is the foundation of your Electron application, and it exposes many APIs that can change the look and behavior of your browser windows. In this tutorial, we will be going over the various use-cases for window customization on macOS, Windows, and Linux.

Create frameless windows

A frameless window is a window that has no chrome. Not to be confused with the Google Chrome browser, window chrome refers to the parts of the window (e.g. toolbars, controls) that are not a part of the web page.

To create a frameless window, you need to set frame to false in the BrowserWindow constructor.

```javascript title=’main.js’ const { BrowserWindow } = require(‘electron’) const win = new BrowserWindow({ frame: false })

  1. ## Apply custom title bar styles _macOS_ _Windows_
  2. Title bar styles allow you to hide most of a BrowserWindow's chrome while keeping the
  3. system's native window controls intact and can be configured with the `titleBarStyle`
  4. option in the `BrowserWindow` constructor.
  5. Applying the `hidden` title bar style results in a hidden title bar and a full-size
  6. content window.
  7. ```javascript title='main.js'
  8. const { BrowserWindow } = require('electron')
  9. const win = new BrowserWindow({ titleBarStyle: 'hidden' })

Control the traffic lights macOS

On macOS, applying the hidden title bar style will still expose the standard window controls (“traffic lights”) in the top left.

Customize the look of your traffic lights macOS

The customButtonsOnHover title bar style will hide the traffic lights until you hover over them. This is useful if you want to create custom traffic lights in your HTML but still use the native UI to control the window.

  1. const { BrowserWindow } = require('electron')
  2. const win = new BrowserWindow({ titleBarStyle: 'customButtonsOnHover' })

Customize the traffic light position macOS

To modify the position of the traffic light window controls, there are two configuration options available.

Applying hiddenInset title bar style will shift the vertical inset of the traffic lights by a fixed amount.

```javascript title=’main.js’ const { BrowserWindow } = require(‘electron’) const win = new BrowserWindow({ titleBarStyle: ‘hiddenInset’ })

  1. If you need more granular control over the positioning of the traffic lights, you can pass
  2. a set of coordinates to the `trafficLightPosition` option in the `BrowserWindow`
  3. constructor.
  4. ```javascript title='main.js'
  5. const { BrowserWindow } = require('electron')
  6. const win = new BrowserWindow({
  7. titleBarStyle: 'hidden',
  8. trafficLightPosition: { x: 10, y: 10 }
  9. })

Show and hide the traffic lights programmatically macOS

You can also show and hide the traffic lights programmatically from the main process. The win.setWindowButtonVisibility forces traffic lights to be show or hidden depending on the value of its boolean parameter.

```javascript title=’main.js’ const { BrowserWindow } = require(‘electron’) const win = new BrowserWindow() // hides the traffic lights win.setWindowButtonVisibility(false)

  1. > Note: Given the number of APIs available, there are many ways of achieving this. For instance,
  2. > combining `frame: false` with `win.setWindowButtonVisibility(true)` will yield the same
  3. > layout outcome as setting `titleBarStyle: 'hidden'`.
  4. ## Window Controls Overlay _macOS_ _Windows_
  5. The [Window Controls Overlay API] is a web standard that gives web apps the ability to
  6. customize their title bar region when installed on desktop. Electron exposes this API
  7. through the `BrowserWindow` constructor option `titleBarOverlay`.
  8. This option only works whenever a custom `titlebarStyle` is applied on macOS or Windows.
  9. When `titleBarOverlay` is enabled, the window controls become exposed in their default
  10. position, and DOM elements cannot use the area underneath this region.
  11. The `titleBarOverlay` option accepts two different value formats.
  12. Specifying `true` on either platform will result in an overlay region with default
  13. system colors:
  14. ```javascript title='main.js'
  15. // on macOS or Windows
  16. const { BrowserWindow } = require('electron')
  17. const win = new BrowserWindow({
  18. titleBarStyle: 'hidden',
  19. titleBarOverlay: true
  20. })

On Windows, you can also specify the color of the overlay and its symbols by setting titleBarOverlay to an object with the color and symbolColor properties. If an option is not specified, the color will default to its system color for the window control buttons:

```javascript title=’main.js’ // on Windows const { BrowserWindow } = require(‘electron’) const win = new BrowserWindow({ titleBarStyle: ‘hidden’, titleBarOverlay: { color: ‘#2f3241’, symbolColor: ‘#74b1be’ } })

  1. > Note: Once your title bar overlay is enabled from the main process, you can access the overlay's
  2. > color and dimension values from a renderer using a set of readonly
  3. > [JavaScript APIs][overlay-javascript-apis] and [CSS Environment Variables][overlay-css-env-vars].
  4. ## Create transparent windows
  5. By setting the `transparent` option to `true`, you can make a fully transparent window.
  6. ```javascript title='main.js'
  7. const { BrowserWindow } = require('electron')
  8. const win = new BrowserWindow({ transparent: true })

Limitations

  • You cannot click through the transparent area. See #1335 for details.
  • Transparent windows are not resizable. Setting resizable to true may make a transparent window stop working on some platforms.
  • The CSS blur() filter only applies to the window’s web contents, so there is no way to apply blur effect to the content below the window (i.e. other applications open on the user’s system).
  • The window will not be transparent when DevTools is opened.
  • On Windows:
    • Transparent windows will not work when DWM is disabled.
    • Transparent windows can not be maximized using the Windows system menu or by double clicking the title bar. The reasoning behind this can be seen on PR #28207.
  • On macOS:
    • The native window shadow will not be shown on a transparent window.

Create click-through windows

To create a click-through window, i.e. making the window ignore all mouse events, you can call the win.setIgnoreMouseEvents(ignore) API:

```javascript title=’main.js’ const { BrowserWindow } = require(‘electron’) const win = new BrowserWindow() win.setIgnoreMouseEvents(true)

  1. ### Forward mouse events _macOS_ _Windows_
  2. Ignoring mouse messages makes the web contents oblivious to mouse movement,
  3. meaning that mouse movement events will not be emitted. On Windows and macOS, an
  4. optional parameter can be used to forward mouse move messages to the web page,
  5. allowing events such as `mouseleave` to be emitted:
  6. ```javascript title='main.js'
  7. const { BrowserWindow, ipcMain } = require('electron')
  8. const path = require('path')
  9. const win = new BrowserWindow({
  10. webPreferences: {
  11. preload: path.join(__dirname, 'preload.js')
  12. }
  13. })
  14. ipcMain.on('set-ignore-mouse-events', (event, ...args) => {
  15. const win = BrowserWindow.fromWebContents(event.sender)
  16. win.setIgnoreMouseEvents(...args)
  17. })

```javascript title=’preload.js’ window.addEventListener(‘DOMContentLoaded’, () => { const el = document.getElementById(‘clickThroughElement’) el.addEventListener(‘mouseenter’, () => { ipcRenderer.send(‘set-ignore-mouse-events’, true, { forward: true }) }) el.addEventListener(‘mouseleave’, () => { ipcRenderer.send(‘set-ignore-mouse-events’, false) }) })

  1. This makes the web page click-through when over the `#clickThroughElement` element,
  2. and returns to normal outside it.
  3. ## Set custom draggable region
  4. By default, the frameless window is non-draggable. Apps need to specify
  5. `-webkit-app-region: drag` in CSS to tell Electron which regions are draggable
  6. (like the OS's standard titlebar), and apps can also use
  7. `-webkit-app-region: no-drag` to exclude the non-draggable area from the
  8. draggable region. Note that only rectangular shapes are currently supported.
  9. To make the whole window draggable, you can add `-webkit-app-region: drag` as
  10. `body`'s style:
  11. ```css title='styles.css'
  12. body {
  13. -webkit-app-region: drag;
  14. }

And note that if you have made the whole window draggable, you must also mark buttons as non-draggable, otherwise it would be impossible for users to click on them:

```css title=’styles.css’ button { -webkit-app-region: no-drag; }

  1. If you're only setting a custom titlebar as draggable, you also need to make all
  2. buttons in titlebar non-draggable.
  3. ### Tip: disable text selection
  4. When creating a draggable region, the dragging behavior may conflict with text selection.
  5. For example, when you drag the titlebar, you may accidentally select its text contents.
  6. To prevent this, you need to disable text selection within a draggable area like this:
  7. ```css
  8. .titlebar {
  9. -webkit-user-select: none;
  10. -webkit-app-region: drag;
  11. }

Tip: disable context menus

On some platforms, the draggable area will be treated as a non-client frame, so when you right click on it, a system menu will pop up. To make the context menu behave correctly on all platforms, you should never use a custom context menu on draggable areas.