Objects created with `new BaseWindow` emit the following events:
**Note:** Some events are only available on specific operating systems and are
labeled as such.
#### Event: 'close'
Returns:
*`event` Event
Emitted when the window is going to be closed. It's emitted before the
`beforeunload` and `unload` event of the DOM. Calling `event.preventDefault()`
will cancel the close.
Usually you would want to use the `beforeunload` handler to decide whether the
window should be closed, which will also be called when the window is
reloaded. In Electron, returning any value other than `undefined` would cancel the
close. For example:
```js
window.onbeforeunload = (e) => {
console.log('I do not want to be closed')
// Unlike usual browsers that a message box will be prompted to users, returning
// a non-void value will silently cancel the close.
// It is recommended to use the dialog API to let the user confirm closing the
// application.
e.returnValue = false
}
```
_**Note**: There is a subtle difference between the behaviors of `window.onbeforeunload = handler` and `window.addEventListener('beforeunload', handler)`. It is recommended to always set the `event.returnValue` explicitly, instead of only returning a value, as the former works more consistently within Electron._
#### Event: 'closed'
Emitted when the window is closed. After you have received this event you should
remove the reference to the window and avoid using it any more.
#### Event: 'session-end' _Windows_
Emitted when window session is going to end due to force shutdown or machine restart
or session log off.
#### Event: 'blur'
Emitted when the window loses focus.
#### Event: 'focus'
Emitted when the window gains focus.
#### Event: 'show'
Emitted when the window is shown.
#### Event: 'hide'
Emitted when the window is hidden.
#### Event: 'maximize'
Emitted when window is maximized.
#### Event: 'unmaximize'
Emitted when the window exits from a maximized state.
#### Event: 'minimize'
Emitted when the window is minimized.
#### Event: 'restore'
Emitted when the window is restored from a minimized state.
#### Event: 'will-resize' _macOS_ _Windows_
Returns:
*`event` Event
*`newBounds` [Rectangle](structures/rectangle.md) - Size the window is being resized to.
*`details` Object
*`edge` (string) - The edge of the window being dragged for resizing. Can be `bottom`, `left`, `right`, `top-left`, `top-right`, `bottom-left` or `bottom-right`.
Emitted before the window is resized. Calling `event.preventDefault()` will prevent the window from being resized.
Note that this is only emitted when the window is being resized manually. Resizing the window with `setBounds`/`setSize` will not emit this event.
The possible values and behaviors of the `edge` option are platform dependent. Possible values are:
* On Windows, possible values are `bottom`, `top`, `left`, `right`, `top-left`, `top-right`, `bottom-left`, `bottom-right`.
* On macOS, possible values are `bottom` and `right`.
* The value `bottom` is used to denote vertical resizing.
* The value `right` is used to denote horizontal resizing.
#### Event: 'resize'
Emitted after the window has been resized.
#### Event: 'resized' _macOS_ _Windows_
Emitted once when the window has finished being resized.
This is usually emitted when the window has been resized manually. On macOS, resizing the window with `setBounds`/`setSize` and setting the `animate` parameter to `true` will also emit this event once resizing has finished.
#### Event: 'will-move' _macOS_ _Windows_
Returns:
*`event` Event
*`newBounds` [Rectangle](structures/rectangle.md) - Location the window is being moved to.
Emitted before the window is moved. On Windows, calling `event.preventDefault()` will prevent the window from being moved.
Note that this is only emitted when the window is being moved manually. Moving the window with `setPosition`/`setBounds`/`center` will not emit this event.
#### Event: 'move'
Emitted when the window is being moved to a new position.
#### Event: 'moved' _macOS_ _Windows_
Emitted once when the window is moved to a new position.
**Note**: On macOS this event is an alias of `move`.
#### Event: 'enter-full-screen'
Emitted when the window enters a full-screen state.
#### Event: 'leave-full-screen'
Emitted when the window leaves a full-screen state.
#### Event: 'always-on-top-changed'
Returns:
*`event` Event
*`isAlwaysOnTop` boolean
Emitted when the window is set or unset to show always on top of other windows.
#### Event: 'app-command' _Windows_ _Linux_
Returns:
*`event` Event
*`command` string
Emitted when an [App Command](https://learn.microsoft.com/en-us/windows/win32/inputdev/wm-appcommand)
is invoked. These are typically related to keyboard media keys or browser
commands, as well as the "Back" button built into some mice on Windows.
Commands are lowercased, underscores are replaced with hyphens, and the
`APPCOMMAND_` prefix is stripped off.
e.g. `APPCOMMAND_BROWSER_BACKWARD` is emitted as `browser-backward`.
```js
const { BaseWindow } = require('electron')
const win = new BaseWindow()
win.on('app-command', (e, cmd) => {
// Navigate the window back when the user hits their mouse back button
if (cmd === 'browser-backward') {
// Find the appropriate WebContents to navigate.
}
})
```
The following app commands are explicitly supported on Linux:
*`browser-backward`
*`browser-forward`
#### Event: 'swipe' _macOS_
Returns:
*`event` Event
*`direction` string
Emitted on 3-finger swipe. Possible directions are `up`, `right`, `down`, `left`.
The method underlying this event is built to handle older macOS-style trackpad swiping,
where the content on the screen doesn't move with the swipe. Most macOS trackpads are not
configured to allow this kind of swiping anymore, so in order for it to emit properly the
'Swipe between pages' preference in `System Preferences > Trackpad > More Gestures` must be
set to 'Swipe with two or three fingers'.
#### Event: 'rotate-gesture' _macOS_
Returns:
*`event` Event
*`rotation` Float
Emitted on trackpad rotation gesture. Continually emitted until rotation gesture is
ended. The `rotation` value on each emission is the angle in degrees rotated since
the last emission. The last emitted event upon a rotation gesture will always be of
value `0`. Counter-clockwise rotation values are positive, while clockwise ones are
negative.
#### Event: 'sheet-begin' _macOS_
Emitted when the window opens a sheet.
#### Event: 'sheet-end' _macOS_
Emitted when the window has closed a sheet.
#### Event: 'new-window-for-tab' _macOS_
Emitted when the native new tab button is clicked.
#### Event: 'system-context-menu' _Windows_
Returns:
*`event` Event
*`point` [Point](structures/point.md) - The screen coordinates the context menu was triggered at
Emitted when the system context menu is triggered on the window, this is
normally only triggered when the user right clicks on the non-client area
of your window. This is the window titlebar or any area you have declared
as `-webkit-app-region: drag` in a frameless window.
Calling `event.preventDefault()` will prevent the menu from being displayed.
### Static Methods
The `BaseWindow` class has the following static methods:
#### `BaseWindow.getAllWindows()`
Returns `BaseWindow[]` - An array of all opened browser windows.
#### `BaseWindow.getFocusedWindow()`
Returns `BaseWindow | null` - The window that is focused in this application, otherwise returns `null`.
#### `BaseWindow.fromId(id)`
*`id` Integer
Returns `BaseWindow | null` - The window with the given `id`.
### Instance Properties
Objects created with `new BaseWindow` have the following properties:
```js
const { BaseWindow } = require('electron')
// In this example `win` is our instance
const win = new BaseWindow({ width: 800, height: 600 })
```
#### `win.id` _Readonly_
A `Integer` property representing the unique ID of the window. Each ID is unique among all `BaseWindow` instances of the entire Electron application.
#### `win.contentView`
A `View` property for the content view of the window.
A `boolean` property that determines whether the window menu bar should hide itself automatically. Once set, the menu bar will only show when users press the single `Alt` key.
If the menu bar is already visible, setting this property to `true` won't
hide it immediately.
#### `win.simpleFullScreen`
A `boolean` property that determines whether the window is in simple (pre-Lion) fullscreen mode.
#### `win.fullScreen`
A `boolean` property that determines whether the window is in fullscreen mode.
#### `win.focusable` _Windows_ _macOS_
A `boolean` property that determines whether the window is focusable.
#### `win.visibleOnAllWorkspaces` _macOS_ _Linux_
A `boolean` property that determines whether the window is visible on all workspaces.
**Note:** Always returns false on Windows.
#### `win.shadow`
A `boolean` property that determines whether the window has a shadow.
#### `win.menuBarVisible` _Windows_ _Linux_
A `boolean` property that determines whether the menu bar should be visible.
**Note:** If the menu bar is auto-hide, users can still bring up the menu bar by pressing the single `Alt` key.
#### `win.kiosk`
A `boolean` property that determines whether the window is in kiosk mode.
#### `win.documentEdited` _macOS_
A `boolean` property that specifies whether the window’s document has been edited.
The icon in title bar will become gray when set to `true`.
#### `win.representedFilename` _macOS_
A `string` property that determines the pathname of the file the window represents,
and the icon of the file will show in window's title bar.
#### `win.title`
A `string` property that determines the title of the native window.
**Note:** The title of the web page can be different from the title of the native window.
#### `win.minimizable` _macOS_ _Windows_
A `boolean` property that determines whether the window can be manually minimized by user.
On Linux the setter is a no-op, although the getter returns `true`.
#### `win.maximizable` _macOS_ _Windows_
A `boolean` property that determines whether the window can be manually maximized by user.
On Linux the setter is a no-op, although the getter returns `true`.
#### `win.fullScreenable`
A `boolean` property that determines whether the maximize/zoom window button toggles fullscreen mode or
maximizes the window.
#### `win.resizable`
A `boolean` property that determines whether the window can be manually resized by user.
#### `win.closable` _macOS_ _Windows_
A `boolean` property that determines whether the window can be manually closed by user.
On Linux the setter is a no-op, although the getter returns `true`.
#### `win.movable` _macOS_ _Windows_
A `boolean` property that determines Whether the window can be moved by user.
On Linux the setter is a no-op, although the getter returns `true`.
#### `win.excludedFromShownWindowsMenu` _macOS_
A `boolean` property that determines whether the window is excluded from the application’s Windows menu. `false` by default.
```js @ts-expect-error=[12]
const { Menu, BaseWindow } = require('electron')
const win = new BaseWindow({ height: 600, width: 600 })
const template = [
{
role: 'windowmenu'
}
]
win.excludedFromShownWindowsMenu = true
const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
```
#### `win.accessibleTitle`
A `string` property that defines an alternative title provided only to
accessibility tools such as screen readers. This string is not directly
visible to users.
### Instance Methods
Objects created with `new BaseWindow` have the following instance methods:
**Note:** Some methods are only available on specific operating systems and are
labeled as such.
#### `win.setContentView(view)`
*`view` [View](view.md)
Sets the content view of the window.
#### `win.getContentView()`
Returns [View](view.md) - The content view of the window.
#### `win.destroy()`
Force closing the window, the `unload` and `beforeunload` event won't be emitted
for the web page, and `close` event will also not be emitted
for this window, but it guarantees the `closed` event will be emitted.
#### `win.close()`
Try to close the window. This has the same effect as a user manually clicking
the close button of the window. The web page may cancel the close though. See
the [close event](#event-close).
#### `win.focus()`
Focuses on the window.
#### `win.blur()`
Removes focus from the window.
#### `win.isFocused()`
Returns `boolean` - Whether the window is focused.
#### `win.isDestroyed()`
Returns `boolean` - Whether the window is destroyed.
**Note:** On macOS, fullscreen transitions take place asynchronously. If further actions depend on the fullscreen state, use the ['enter-full-screen'](base-window.md#event-enter-full-screen) or ['leave-full-screen'](base-window.md#event-leave-full-screen) events.
* Options are listed in [SkParseColor.cpp](https://source.chromium.org/chromium/chromium/src/+/main:third_party/skia/src/utils/SkParseColor.cpp;l=11-152;drc=eea4bf52cb0d55e2a39c828b017c80a5ee054148)
* Similar to CSS Color Module Level 3 keywords, but case-sensitive.
* e.g. `blueviolet` or `red`
Sets the background color of the window. See [Setting `backgroundColor`](browser-window.md#setting-the-backgroundcolor-property).
**Note:** On macOS, the y-coordinate value cannot be smaller than the [Tray](tray.md) height. The tray height has changed over time and depends on the operating system, but is between 20-40px. Passing a value lower than the tray height will result in a window that is flush to the tray.
**Note:** On macOS, the y-coordinate value returned will be at minimum the [Tray](tray.md) height. For example, calling `win.setBounds({ x: 25, y: 20, width: 800, height: 600 })` with a tray height of 38 means that `win.getBounds()` will return `{ x: 25, y: 38, width: 800, height: 600 }`.
Returns `string` - Gets the background color of the window in Hex (`#RRGGBB`) format.
See [Setting `backgroundColor`](browser-window.md#setting-the-backgroundcolor-property).
**Note:** The alpha value is _not_ returned alongside the red, green, and blue values.
#### `win.setContentBounds(bounds[, animate])`
*`bounds` [Rectangle](structures/rectangle.md)
*`animate` boolean (optional) _macOS_
Resizes and moves the window's client area (e.g. the web page) to
the supplied bounds.
#### `win.getContentBounds()`
Returns [`Rectangle`](structures/rectangle.md) - The `bounds` of the window's client area as `Object`.
#### `win.getNormalBounds()`
Returns [`Rectangle`](structures/rectangle.md) - Contains the window bounds of the normal state
**Note:** whatever the current state of the window : maximized, minimized or in fullscreen, this function always returns the position and size of the window in normal state. In normal state, getBounds and getNormalBounds returns the same [`Rectangle`](structures/rectangle.md).
#### `win.setEnabled(enable)`
*`enable` boolean
Disable or enable the window.
#### `win.isEnabled()`
Returns `boolean` - whether the window is enabled.
#### `win.setSize(width, height[, animate])`
*`width` Integer
*`height` Integer
*`animate` boolean (optional) _macOS_
Resizes the window to `width` and `height`. If `width` or `height` are below any set minimum size constraints the window will snap to its minimum size.
#### `win.getSize()`
Returns `Integer[]` - Contains the window's width and height.