Files
archived-tauri-docs/docs/api/js/modules/window.md
2022-03-29 07:36:52 +02:00

5.4 KiB

@tauri-apps/api / window

Module: window

Provides APIs to create windows, communicate with other windows and manipulate the current window.

This package is also accessible with window.__TAURI__.window when tauri.conf.json > build > withGlobalTauri is set to true.

The APIs must be allowlisted on tauri.conf.json:

{
  "tauri": {
    "allowlist": {
      "window": {
        "all": true, // enable all window APIs
        "create": true, // enable window creation
        "center": true,
        "requestUserAttention": true,
        "setResizable": true,
        "setTitle": true,
        "maximize": true,
        "unmaximize": true,
        "minimize": true,
        "unminimize": true,
        "show": true,
        "hide": true,
        "close": true,
        "setDecorations": true,
        "setAlwaysOnTop": true,
        "setSize": true,
        "setMinSize": true,
        "setMaxSize": true,
        "setPosition": true,
        "setFullscreen": true,
        "setFocus": true,
        "setIcon": true,
        "setSkipTaskbar": true,
        "startDragging": true,
        "print": true
      }
    }
  }
}

It is recommended to allowlist only the APIs you use for optimal bundle size and security.

Window events

Events can be listened using appWindow.listen:

import { appWindow } from '@tauri-apps/api/window'
appWindow.listen('tauri://move', ({ event, payload }) => {
  const { x, y } = payload // payload here is a `PhysicalPosition`
})

Window-specific events emitted by the backend:

'tauri://resize'

Emitted when the size of the window has changed. EventPayload:

type ResizePayload = PhysicalSize

'tauri://move'

Emitted when the position of the window has changed. EventPayload:

type MovePayload = PhysicalPosition

'tauri://close-requested'

Emitted when the user requests the window to be closed. If a listener is registered for this event, Tauri won't close the window so you must call appWindow.close() manually.

'tauri://focus'

Emitted when the window gains focus.

'tauri://blur'

Emitted when the window loses focus.

'tauri://scale-change'

Emitted when the window's scale factor has changed. The following user actions can cause DPI changes:

  • Changing the display's resolution.
  • Changing the display's scale factor (e.g. in Control Panel on Windows).
  • Moving the window to a display with a different scale factor. Event payload:
interface ScaleFactorChanged {
  scaleFactor: number
  size: PhysicalSize
}

'tauri://menu'

Emitted when a menu item is clicked. EventPayload:

type MenuClicked = string

Enumerations

Classes

Interfaces

Variables

appWindow

appWindow: WebviewWindow

The WebviewWindow for the current window.

Defined in

window.ts:1171

Functions

availableMonitors

availableMonitors(): Promise<Monitor[]>

Returns the list of all the monitors available on the system.

Returns

Promise<Monitor[]>

Defined in

window.ts:1287


currentMonitor

currentMonitor(): Promise<Monitor | null>

Returns the monitor on which the window currently resides. Returns null if current monitor can't be detected.

Returns

Promise<Monitor | null>

Defined in

window.ts:1254


getAll

getAll(): WebviewWindow[]

Gets an instance of WebviewWindow for all available webview windows.

Returns

WebviewWindow[]

The list of WebviewWindow.

Defined in

window.ts:230


getCurrent

getCurrent(): WebviewWindow

Get an instance of WebviewWindow for the current webview window.

Returns

WebviewWindow

The current WebviewWindow.

Defined in

window.ts:218


primaryMonitor

primaryMonitor(): Promise<Monitor | null>

Returns the primary monitor of the system. Returns null if it can't identify any monitor as a primary one.

Returns

Promise<Monitor | null>

Defined in

window.ts:1272