Files
archived-tauri-docs/docs/api/js/classes/window.WindowManager.md
2022-07-15 12:00:05 +02:00

29 KiB

@tauri-apps/api / window / WindowManager

Class: WindowManager

window.WindowManager

Manage the current window object.

Hierarchy

Constructors

constructor

new WindowManager(label)

Parameters

Name Type
label string

Inherited from

WebviewWindowHandle.constructor

Properties

label

label: string

The window label. It is a unique identifier for the window, can be used to reference it later.

Inherited from

WebviewWindowHandle.label

Defined in

window.ts:280


listeners

listeners: Object

Local event listeners.

Index signature

▪ [key: string]: EventCallback<any>[]

Inherited from

WebviewWindowHandle.listeners

Defined in

window.ts:282

Methods

_handleTauriEvent

_handleTauriEvent<T>(event, handler): boolean

Type parameters

Name
T

Parameters

Name Type
event string
handler EventCallback<T>

Returns

boolean

Inherited from

WebviewWindowHandle._handleTauriEvent


center

center(): Promise<void>

Centers the window.

Example

import { appWindow } from '@tauri-apps/api/window';
await appWindow.center();

Returns

Promise<void>

A promise indicating the success or failure of the operation.


close

close(): Promise<void>

Closes the window.

Example

import { appWindow } from '@tauri-apps/api/window';
await appWindow.close();

Returns

Promise<void>

A promise indicating the success or failure of the operation.


emit

emit(event, payload?): Promise<void>

Emits an event to the backend, tied to the webview window.

Example

import { appWindow } from '@tauri-apps/api/window';
await appWindow.emit('window-loaded', { loggedIn: true, token: 'authToken' });

Parameters

Name Type Description
event string Event name. Must include only alphanumeric characters, -, /, : and _.
payload? unknown Event payload.

Returns

Promise<void>

Inherited from

WebviewWindowHandle.emit


hide

hide(): Promise<void>

Sets the window visibility to false.

Example

import { appWindow } from '@tauri-apps/api/window';
await appWindow.hide();

Returns

Promise<void>

A promise indicating the success or failure of the operation.


innerPosition

innerPosition(): Promise<PhysicalPosition>

The position of the top-left hand corner of the window's client area relative to the top-left hand corner of the desktop.

Example

import { appWindow } from '@tauri-apps/api/window';
const position = await appWindow.innerPosition();

Returns

Promise<PhysicalPosition>

The window's inner position.


innerSize

innerSize(): Promise<PhysicalSize>

The physical size of the window's client area. The client area is the content of the window, excluding the title bar and borders.

Example

import { appWindow } from '@tauri-apps/api/window';
const size = await appWindow.innerSize();

Returns

Promise<PhysicalSize>

The window's inner size.


isDecorated

isDecorated(): Promise<boolean>

Gets the window's current decorated state.

Example

import { appWindow } from '@tauri-apps/api/window';
const decorated = await appWindow.isDecorated();

Returns

Promise<boolean>

Whether the window is decorated or not.


isFullscreen

isFullscreen(): Promise<boolean>

Gets the window's current fullscreen state.

Example

import { appWindow } from '@tauri-apps/api/window';
const fullscreen = await appWindow.isFullscreen();

Returns

Promise<boolean>

Whether the window is in fullscreen mode or not.


isMaximized

isMaximized(): Promise<boolean>

Gets the window's current maximized state.

Example

import { appWindow } from '@tauri-apps/api/window';
const maximized = await appWindow.isMaximized();

Returns

Promise<boolean>

Whether the window is maximized or not.


isResizable

isResizable(): Promise<boolean>

Gets the window's current resizable state.

Example

import { appWindow } from '@tauri-apps/api/window';
const resizable = await appWindow.isResizable();

Returns

Promise<boolean>

Whether the window is resizable or not.


isVisible

isVisible(): Promise<boolean>

Gets the window's current visible state.

Example

import { appWindow } from '@tauri-apps/api/window';
const visible = await appWindow.isVisible();

Returns

Promise<boolean>

Whether the window is visible or not.


listen

listen<T>(event, handler): Promise<UnlistenFn>

Listen to an event emitted by the backend that is tied to the webview window.

Example

import { appWindow } from '@tauri-apps/api/window';
const unlisten = await appWindow.listen<string>('state-changed', (event) => {
  console.log(`Got error: ${payload}`);
});

// you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
unlisten();

Type parameters

Name
T

Parameters

Name Type Description
event string Event name. Must include only alphanumeric characters, -, /, : and _.
handler EventCallback<T> Event handler.

Returns

Promise<UnlistenFn>

A promise resolving to a function to unlisten to the event. Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.

Inherited from

WebviewWindowHandle.listen


maximize

maximize(): Promise<void>

Maximizes the window.

Example

import { appWindow } from '@tauri-apps/api/window';
await appWindow.maximize();

Returns

Promise<void>

A promise indicating the success or failure of the operation.


minimize

minimize(): Promise<void>

Minimizes the window.

Example

import { appWindow } from '@tauri-apps/api/window';
await appWindow.minimize();

Returns

Promise<void>

A promise indicating the success or failure of the operation.


onCloseRequested

onCloseRequested(handler): Promise<UnlistenFn>

Listen to window close requested. Emitted when the user requests to closes the window.

Example

import { appWindow } from "@tauri-apps/api/window";
import { confirm } from '@tauri-apps/api/dialog';
const unlisten = await appWindow.onCloseRequested(async (event) => {
  const confirmed = await confirm('Are you sure?');
  if (!confirmed) {
    // user did not confirm closing the window; let's prevent it
    event.preventDefault();
  }
});

// you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
unlisten();

Parameters

Name Type
handler (event: CloseRequestedEvent) => void

Returns

Promise<UnlistenFn>

A promise resolving to a function to unlisten to the event. Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.


onFileDropEvent

onFileDropEvent(handler): Promise<UnlistenFn>

Listen to a file drop event. The listener is triggered when the user hovers the selected files on the window, drops the files or cancels the operation.

Example

import { appWindow } from "@tauri-apps/api/window";
const unlisten = await appWindow.onFileDropEvent((event) => {
 if (event.payload.type === 'hover') {
   console.log('User hovering', event.payload.paths);
 } else if (event.payload.type === 'drop') {
   console.log('User dropped', event.payload.paths);
 } else {
   console.log('File drop cancelled');
 }
});

// you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
unlisten();

Parameters

Name Type
handler EventCallback<FileDropEvent>

Returns

Promise<UnlistenFn>

A promise resolving to a function to unlisten to the event. Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.


onFocusChanged

onFocusChanged(handler): Promise<UnlistenFn>

Listen to window focus change.

Example

import { appWindow } from "@tauri-apps/api/window";
const unlisten = await appWindow.onFocusChanged(({ payload: focused }) => {
 console.log('Focus changed, window is focused? ' + focused);
});

// you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
unlisten();

Parameters

Name Type
handler EventCallback<boolean>

Returns

Promise<UnlistenFn>

A promise resolving to a function to unlisten to the event. Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.


onMenuClicked

onMenuClicked(handler): Promise<UnlistenFn>

Listen to the window menu item click. The payload is the item id.

Example

import { appWindow } from "@tauri-apps/api/window";
const unlisten = await appWindow.onMenuClicked(({ payload: menuId }) => {
 console.log('Menu clicked: ' + menuId);
});

// you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
unlisten();

Parameters

Name Type
handler EventCallback<string>

Returns

Promise<UnlistenFn>

A promise resolving to a function to unlisten to the event. Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.


onMoved

onMoved(handler): Promise<UnlistenFn>

Listen to window move.

Example

import { appWindow } from "@tauri-apps/api/window";
const unlisten = await appWindow.onMoved(({ payload: position }) => {
 console.log('Window moved', position);
});

// you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
unlisten();

Parameters

Name Type
handler EventCallback<PhysicalPosition>

Returns

Promise<UnlistenFn>

A promise resolving to a function to unlisten to the event. Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.


onResized

onResized(handler): Promise<UnlistenFn>

Listen to window resize.

Example

import { appWindow } from "@tauri-apps/api/window";
const unlisten = await appWindow.onResized(({ payload: size }) => {
 console.log('Window resized', size);
});

// you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
unlisten();

Parameters

Name Type
handler EventCallback<PhysicalSize>

Returns

Promise<UnlistenFn>

A promise resolving to a function to unlisten to the event. Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.


onScaleChanged

onScaleChanged(handler): Promise<UnlistenFn>

Listen to window 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.

Example

import { appWindow } from "@tauri-apps/api/window";
const unlisten = await appWindow.onScaleChanged(({ payload }) => {
 console.log('Scale changed', payload.scaleFactor, payload.size);
});

// you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
unlisten();

Parameters

Name Type
handler EventCallback<ScaleFactorChanged>

Returns

Promise<UnlistenFn>

A promise resolving to a function to unlisten to the event. Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.


onThemeChanged

onThemeChanged(handler): Promise<UnlistenFn>

Listen to the system theme change.

Example

import { appWindow } from "@tauri-apps/api/window";
const unlisten = await appWindow.onThemeChanged(({ payload: theme }) => {
 console.log('New theme: ' + theme);
});

// you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
unlisten();

Parameters

Name Type
handler EventCallback<Theme>

Returns

Promise<UnlistenFn>

A promise resolving to a function to unlisten to the event. Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.


once

once<T>(event, handler): Promise<UnlistenFn>

Listen to an one-off event emitted by the backend that is tied to the webview window.

Example

import { appWindow } from '@tauri-apps/api/window';
const unlisten = await appWindow.once<null>('initialized', (event) => {
  console.log(`Window initialized!`);
});

// you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
unlisten();

Type parameters

Name
T

Parameters

Name Type Description
event string Event name. Must include only alphanumeric characters, -, /, : and _.
handler EventCallback<T> Event handler.

Returns

Promise<UnlistenFn>

A promise resolving to a function to unlisten to the event. Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.

Inherited from

WebviewWindowHandle.once


outerPosition

outerPosition(): Promise<PhysicalPosition>

The position of the top-left hand corner of the window relative to the top-left hand corner of the desktop.

Example

import { appWindow } from '@tauri-apps/api/window';
const position = await appWindow.outerPosition();

Returns

Promise<PhysicalPosition>

The window's outer position.


outerSize

outerSize(): Promise<PhysicalSize>

The physical size of the entire window. These dimensions include the title bar and borders. If you don't want that (and you usually don't), use inner_size instead.

Example

import { appWindow } from '@tauri-apps/api/window';
const size = await appWindow.outerSize();

Returns

Promise<PhysicalSize>

The window's outer size.


requestUserAttention

requestUserAttention(requestType): Promise<void>

Requests user attention to the window, this has no effect if the application is already focused. How requesting for user attention manifests is platform dependent, see UserAttentionType for details.

Providing null will unset the request for user attention. Unsetting the request for user attention might not be done automatically by the WM when the window receives input.

Platform-specific

  • macOS: null has no effect.
  • Linux: Urgency levels have the same effect.

Example

import { appWindow } from '@tauri-apps/api/window';
await appWindow.requestUserAttention();

Parameters

Name Type
requestType null | UserAttentionType

Returns

Promise<void>

A promise indicating the success or failure of the operation.


scaleFactor

scaleFactor(): Promise<number>

The scale factor that can be used to map physical pixels to logical pixels.

Example

import { appWindow } from '@tauri-apps/api/window';
const factor = await appWindow.scaleFactor();

Returns

Promise<number>

The window's monitor scale factor.


setAlwaysOnTop

setAlwaysOnTop(alwaysOnTop): Promise<void>

Whether the window should always be on top of other windows.

Example

import { appWindow } from '@tauri-apps/api/window';
await appWindow.setAlwaysOnTop(true);

Parameters

Name Type Description
alwaysOnTop boolean Whether the window should always be on top of other windows or not.

Returns

Promise<void>

A promise indicating the success or failure of the operation.


setCursorGrab

setCursorGrab(grab): Promise<void>

Grabs the cursor, preventing it from leaving the window.

There's no guarantee that the cursor will be hidden. You should hide it by yourself if you want so.

Platform-specific

  • Linux: Unsupported.
  • macOS: This locks the cursor in a fixed location, which looks visually awkward.

Example

import { appWindow } from '@tauri-apps/api/window';
await appWindow.setCursorGrab(true);

Parameters

Name Type Description
grab boolean true to grab the cursor icon, false to release it.

Returns

Promise<void>

A promise indicating the success or failure of the operation.


setCursorIcon

setCursorIcon(icon): Promise<void>

Modifies the cursor icon of the window.

Example

import { appWindow } from '@tauri-apps/api/window';
await appWindow.setCursorIcon('help');

Parameters

Name Type Description
icon CursorIcon The new cursor icon.

Returns

Promise<void>

A promise indicating the success or failure of the operation.


setCursorPosition

setCursorPosition(position): Promise<void>

Changes the position of the cursor in window coordinates.

Example

import { appWindow, LogicalPosition } from '@tauri-apps/api/window';
await appWindow.setCursorPosition(new LogicalPosition(600, 300));

Parameters

Name Type Description
position PhysicalPosition | LogicalPosition The new cursor position.

Returns

Promise<void>

A promise indicating the success or failure of the operation.


setCursorVisible

setCursorVisible(visible): Promise<void>

Modifies the cursor's visibility.

Platform-specific

  • Windows: The cursor is only hidden within the confines of the window.
  • macOS: The cursor is hidden as long as the window has input focus, even if the cursor is outside of the window.

Example

import { appWindow } from '@tauri-apps/api/window';
await appWindow.setCursorVisible(false);

Parameters

Name Type Description
visible boolean If false, this will hide the cursor. If true, this will show the cursor.

Returns

Promise<void>

A promise indicating the success or failure of the operation.


setDecorations

setDecorations(decorations): Promise<void>

Whether the window should have borders and bars.

Example

import { appWindow } from '@tauri-apps/api/window';
await appWindow.setDecorations(false);

Parameters

Name Type Description
decorations boolean Whether the window should have borders and bars.

Returns

Promise<void>

A promise indicating the success or failure of the operation.


setFocus

setFocus(): Promise<void>

Bring the window to front and focus.

Example

import { appWindow } from '@tauri-apps/api/window';
await appWindow.setFocus();

Returns

Promise<void>

A promise indicating the success or failure of the operation.


setFullscreen

setFullscreen(fullscreen): Promise<void>

Sets the window fullscreen state.

Example

import { appWindow } from '@tauri-apps/api/window';
await appWindow.setFullscreen(true);

Parameters

Name Type Description
fullscreen boolean Whether the window should go to fullscreen or not.

Returns

Promise<void>

A promise indicating the success or failure of the operation.


setIcon

setIcon(icon): Promise<void>

Sets the window icon.

Example

import { appWindow } from '@tauri-apps/api/window';
await appWindow.setIcon('/tauri/awesome.png');

Note that you need the icon-ico or icon-png Cargo features to use this API. To enable it, change your Cargo.toml file:

[dependencies]
tauri = { version = "...", features = ["...", "icon-png"] }

Parameters

Name Type Description
icon string | Uint8Array Icon bytes or path to the icon file.

Returns

Promise<void>

A promise indicating the success or failure of the operation.


setMaxSize

setMaxSize(size): Promise<void>

Sets the window maximum inner size. If the size argument is undefined, the constraint is unset.

Example

import { appWindow, LogicalSize } from '@tauri-apps/api/window';
await appWindow.setMaxSize(new LogicalSize(600, 500));

Parameters

Name Type Description
size undefined | null | PhysicalSize | LogicalSize The logical or physical inner size, or null to unset the constraint.

Returns

Promise<void>

A promise indicating the success or failure of the operation.


setMinSize

setMinSize(size): Promise<void>

Sets the window minimum inner size. If the size argument is not provided, the constraint is unset.

Example

import { appWindow, PhysicalSize } from '@tauri-apps/api/window';
await appWindow.setMinSize(new PhysicalSize(600, 500));

Parameters

Name Type Description
size undefined | null | PhysicalSize | LogicalSize The logical or physical inner size, or null to unset the constraint.

Returns

Promise<void>

A promise indicating the success or failure of the operation.


setPosition

setPosition(position): Promise<void>

Sets the window outer position.

Example

import { appWindow, LogicalPosition } from '@tauri-apps/api/window';
await appWindow.setPosition(new LogicalPosition(600, 500));

Parameters

Name Type Description
position PhysicalPosition | LogicalPosition The new position, in logical or physical pixels.

Returns

Promise<void>

A promise indicating the success or failure of the operation.


setResizable

setResizable(resizable): Promise<void>

Updates the window resizable flag.

Example

import { appWindow } from '@tauri-apps/api/window';
await appWindow.setResizable(false);

Parameters

Name Type
resizable boolean

Returns

Promise<void>

A promise indicating the success or failure of the operation.


setSize

setSize(size): Promise<void>

Resizes the window with a new inner size.

Example

import { appWindow, LogicalSize } from '@tauri-apps/api/window';
await appWindow.setSize(new LogicalSize(600, 500));

Parameters

Name Type Description
size PhysicalSize | LogicalSize The logical or physical inner size.

Returns

Promise<void>

A promise indicating the success or failure of the operation.


setSkipTaskbar

setSkipTaskbar(skip): Promise<void>

Whether to show the window icon in the task bar or not.

Example

import { appWindow } from '@tauri-apps/api/window';
await appWindow.setSkipTaskbar(true);

Parameters

Name Type Description
skip boolean true to hide window icon, false to show it.

Returns

Promise<void>

A promise indicating the success or failure of the operation.


setTitle

setTitle(title): Promise<void>

Sets the window title.

Example

import { appWindow } from '@tauri-apps/api/window';
await appWindow.setTitle('Tauri');

Parameters

Name Type Description
title string The new title

Returns

Promise<void>

A promise indicating the success or failure of the operation.


show

show(): Promise<void>

Sets the window visibility to true.

Example

import { appWindow } from '@tauri-apps/api/window';
await appWindow.show();

Returns

Promise<void>

A promise indicating the success or failure of the operation.


startDragging

startDragging(): Promise<void>

Starts dragging the window.

Example

import { appWindow } from '@tauri-apps/api/window';
await appWindow.startDragging();

Returns

Promise<void>

A promise indicating the success or failure of the operation.


theme

theme(): Promise<null | Theme>

Gets the window's current theme.

Platform-specific

  • Linux: Not implemented, always returns light.
  • macOS: Theme was introduced on macOS 10.14. Returns light on macOS 10.13 and below.

Example

import { appWindow } from '@tauri-apps/api/window';
const theme = await appWindow.theme();

Returns

Promise<null | Theme>

The window theme.


toggleMaximize

toggleMaximize(): Promise<void>

Toggles the window maximized state.

Example

import { appWindow } from '@tauri-apps/api/window';
await appWindow.toggleMaximize();

Returns

Promise<void>

A promise indicating the success or failure of the operation.


unmaximize

unmaximize(): Promise<void>

Unmaximizes the window.

Example

import { appWindow } from '@tauri-apps/api/window';
await appWindow.unmaximize();

Returns

Promise<void>

A promise indicating the success or failure of the operation.


unminimize

unminimize(): Promise<void>

Unminimizes the window.

Example

import { appWindow } from '@tauri-apps/api/window';
await appWindow.unminimize();

Returns

Promise<void>

A promise indicating the success or failure of the operation.