콘텐츠로 이동

Window

이 콘텐츠는 아직 번역되지 않았습니다.

Module @netwiv/api/window · Kind Class

import { Window } from '@netwiv/api/window';
new Window(label: string, options?: WindowOptions | undefined)
getAll(): Promise<Window[]>
getByLabel(label: string): Promise<Window | null>
getCurrent(): Window
getFocusedWindow(): Promise<Window | null>
center(): Promise<void>

Centers the window.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().center();
clearEffects(): Promise<void>

Clear any applied effects if possible.

close(): Promise<void>

Closes the window.

Note this emits a closeRequested event so you can intercept it. To force window close, use {@link Window.destroy}.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().close();
destroy(): Promise<void>

Destroys the window. Behaves like {@link Window.close} but forces the window close instead of emitting a closeRequested event.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().destroy();
emit<T>(event: string, payload?: T | undefined): Promise<void>

Emits an event to all {@link EventTargettargets}.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().emit('window-loaded', { loggedIn: true, token: 'authToken' });
emitTo<T>(target: string | EventTarget, event: string, payload?: T | undefined): Promise<void>

Emits an event to all {@link EventTargettargets} matching the given target.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().emit('main', 'window-loaded', { loggedIn: true, token: 'authToken' });
hide(): Promise<void>

Sets the window visibility to false.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().hide();
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 { getCurrentWindow } from '@netwiv/api/window';
const position = await getCurrentWindow().innerPosition();
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 { getCurrentWindow } from '@netwiv/api/window';
const size = await getCurrentWindow().innerSize();
isAlwaysOnTop(): Promise<boolean>

Whether the window is configured to be always on top of other windows or not.

Example

import { getCurrentWindow } from '@netwiv/api/window';
const alwaysOnTop = await getCurrentWindow().isAlwaysOnTop();
isClosable(): Promise<boolean>

Gets the window’s native close button state.

  • iOS / Android: Unsupported.

Example

import { getCurrentWindow } from '@netwiv/api/window';
const closable = await getCurrentWindow().isClosable();
isDecorated(): Promise<boolean>

Gets the window’s current decorated state.

Example

import { getCurrentWindow } from '@netwiv/api/window';
const decorated = await getCurrentWindow().isDecorated();
isEnabled(): Promise<boolean>

Whether the window is enabled or disabled.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().setEnabled(false);
isFocused(): Promise<boolean>

Gets the window’s current focus state.

Example

import { getCurrentWindow } from '@netwiv/api/window';
const focused = await getCurrentWindow().isFocused();
isFullscreen(): Promise<boolean>

Gets the window’s current fullscreen state.

Example

import { getCurrentWindow } from '@netwiv/api/window';
const fullscreen = await getCurrentWindow().isFullscreen();
isMaximizable(): Promise<boolean>

Gets the window’s native maximize button state.

  • Linux / iOS / Android: Unsupported.

Example

import { getCurrentWindow } from '@netwiv/api/window';
const maximizable = await getCurrentWindow().isMaximizable();
isMaximized(): Promise<boolean>

Gets the window’s current maximized state.

Example

import { getCurrentWindow } from '@netwiv/api/window';
const maximized = await getCurrentWindow().isMaximized();
isMinimizable(): Promise<boolean>

Gets the window’s native minimize button state.

  • Linux / iOS / Android: Unsupported.

Example

import { getCurrentWindow } from '@netwiv/api/window';
const minimizable = await getCurrentWindow().isMinimizable();
isMinimized(): Promise<boolean>

Gets the window’s current minimized state.

Example

import { getCurrentWindow } from '@netwiv/api/window';
const minimized = await getCurrentWindow().isMinimized();
isResizable(): Promise<boolean>

Gets the window’s current resizable state.

Example

import { getCurrentWindow } from '@netwiv/api/window';
const resizable = await getCurrentWindow().isResizable();
isVisible(): Promise<boolean>

Gets the window’s current visible state.

Example

import { getCurrentWindow } from '@netwiv/api/window';
const visible = await getCurrentWindow().isVisible();
label: string

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

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

Listen to an emitted event on this window.

Example

import { getCurrentWindow } from '@netwiv/api/window';
const unlisten = await getCurrentWindow().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();
listeners: Record<string, EventCallback<any>[]>

Local event listeners.

maximize(): Promise<void>

Maximizes the window.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().maximize();
minimize(): Promise<void>

Minimizes the window.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().minimize();
once<T>(event: EventName, handler: EventCallback<T>): Promise<UnlistenFn>

Listen to an emitted event on this window only once.

Example

import { getCurrentWindow } from '@netwiv/api/window';
const unlisten = await getCurrentWindow().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();
onCloseRequested(handler: (event: CloseRequestedEvent): void | Promise<void>) => Promise<UnlistenFn>

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

Example

import { getCurrentWindow } from "@netwiv/api/window";
import { confirm } from '@netwiv/api/dialog';
const unlisten = await getCurrentWindow().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();
onDragDropEvent(handler: EventCallback<DragDropEvent>): Promise<UnlistenFn>

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

Example

import { getCurrentWindow } from "@netwiv/api/webview";
const unlisten = await getCurrentWindow().onDragDropEvent((event) => {
if (event.payload.type === 'over') {
console.log('User hovering', event.payload.position);
} 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();
onFocusChanged(handler: EventCallback<boolean>): Promise<UnlistenFn>

Listen to window focus change.

Example

import { getCurrentWindow } from "@netwiv/api/window";
const unlisten = await getCurrentWindow().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();
onMoved(handler: EventCallback<PhysicalPosition>): Promise<UnlistenFn>

Listen to window move.

Example

import { getCurrentWindow } from "@netwiv/api/window";
const unlisten = await getCurrentWindow().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();
onResized(handler: EventCallback<PhysicalSize>): Promise<UnlistenFn>

Listen to window resize.

Example

import { getCurrentWindow } from "@netwiv/api/window";
const unlisten = await getCurrentWindow().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();
onScaleChanged(handler: EventCallback<ScaleFactorChanged>): 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 { getCurrentWindow } from "@netwiv/api/window";
const unlisten = await getCurrentWindow().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();
onThemeChanged(handler: EventCallback<Theme>): Promise<UnlistenFn>

Listen to the system theme change.

Example

import { getCurrentWindow } from "@netwiv/api/window";
const unlisten = await getCurrentWindow().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();
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 { getCurrentWindow } from '@netwiv/api/window';
const position = await getCurrentWindow().outerPosition();
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 { getCurrentWindow } from '@netwiv/api/window';
const size = await getCurrentWindow().outerSize();
requestUserAttention(requestType: UserAttentionType | null): 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.

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

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().requestUserAttention();
scaleFactor(): Promise<number>

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

Example

import { getCurrentWindow } from '@netwiv/api/window';
const factor = await getCurrentWindow().scaleFactor();
setAlwaysOnBottom(alwaysOnBottom: boolean): Promise<void>

Whether the window should always be below other windows.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().setAlwaysOnBottom(true);
setAlwaysOnTop(alwaysOnTop: boolean): Promise<void>

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

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().setAlwaysOnTop(true);
setBackgroundColor(color: Color): Promise<void>

Sets the window background color.

  • Windows: alpha channel is ignored.
  • iOS / Android: Unsupported.
setBadgeCount(count?: number | undefined): Promise<void>

Sets the badge count. It is app wide and not specific to this window.

  • Windows: Unsupported. Use

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().setBadgeCount(5);
setBadgeLabel(label?: string | undefined): Promise<void>

Sets the badge cont macOS only.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().setBadgeLabel("Hello");
setClosable(closable: boolean): Promise<void>

Sets whether the window’s native close button is enabled or not.

  • Linux: GTK+ will do its best to convince the window manager not to show a close button. Depending on the system, this function may not have any effect when called on a window that is already visible
  • iOS / Android: Unsupported.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().setClosable(false);
setContentProtected(protected_: boolean): Promise<void>

Prevents the window contents from being captured by other apps.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().setContentProtected(true);
setCursorGrab(grab: boolean): 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.

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

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().setCursorGrab(true);
setCursorIcon(icon: CursorIcon): Promise<void>

Modifies the cursor icon of the window.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().setCursorIcon('help');
setCursorPosition(position: LogicalPosition | PhysicalPosition | Position): Promise<void>

Changes the position of the cursor in window coordinates.

Example

import { getCurrentWindow, LogicalPosition } from '@netwiv/api/window';
await getCurrentWindow().setCursorPosition(new LogicalPosition(600, 300));
setCursorVisible(visible: boolean): Promise<void>

Modifies the cursor’s visibility.

  • 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 { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().setCursorVisible(false);
setDecorations(decorations: boolean): Promise<void>

Whether the window should have borders and bars.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().setDecorations(false);
setEffects(effects: Effects): Promise<void>

Set window effects.

setEnabled(enabled: boolean): Promise<void>

Enable or disable the window.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().setEnabled(false);
setFocus(): Promise<void>

Bring the window to front and focus.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().setFocus();
setFocusable(focusable: boolean): Promise<void>

Sets whether the window can be focused.

  • macOS: If the window is already focused, it is not possible to unfocus it after calling set_focusable(false). In this case, you might consider calling {@link Window.setFocus} but it will move the window to the back i.e. at the bottom in terms of z-order.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().setFocusable(true);
setFullscreen(fullscreen: boolean): Promise<void>

Sets the window fullscreen state.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().setFullscreen(true);
setIcon(icon: string | number[] | Image | ArrayBuffer | Uint8Array<ArrayBufferLike>): Promise<void>

Sets the window icon.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().setIcon('/tauri/awesome.png');

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

[dependencies]
tauri = { version = "...", features = ["...", "image-png"] }
setIgnoreCursorEvents(ignore: boolean): Promise<void>

Changes the cursor events behavior.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().setIgnoreCursorEvents(true);
setMaximizable(maximizable: boolean): Promise<void>

Sets whether the window’s native maximize button is enabled or not. If resizable is set to false, this setting is ignored.

  • macOS: Disables the “zoom” button in the window titlebar, which is also used to enter fullscreen mode.
  • Linux / iOS / Android: Unsupported.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().setMaximizable(false);
setMaxSize(size: LogicalSize | PhysicalSize | Size | null | undefined): Promise<void>

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

Example

import { getCurrentWindow, LogicalSize } from '@netwiv/api/window';
await getCurrentWindow().setMaxSize(new LogicalSize(600, 500));
setMinimizable(minimizable: boolean): Promise<void>

Sets whether the window’s native minimize button is enabled or not.

  • Linux / iOS / Android: Unsupported.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().setMinimizable(false);
setMinSize(size: LogicalSize | PhysicalSize | Size | null | undefined): Promise<void>

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

Example

import { getCurrentWindow, PhysicalSize } from '@netwiv/api/window';
await getCurrentWindow().setMinSize(new PhysicalSize(600, 500));
setOverlayIcon(icon?: string | number[] | Image | ArrayBuffer | Uint8Array<ArrayBufferLike> | undefined): Promise<void>

Sets the overlay icon. Windows only The overlay icon can be set for every window.

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

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

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().setOverlayIcon("/tauri/awesome.png");
setPosition(position: LogicalPosition | PhysicalPosition | Position): Promise<void>

Sets the window outer position.

Example

import { getCurrentWindow, LogicalPosition } from '@netwiv/api/window';
await getCurrentWindow().setPosition(new LogicalPosition(600, 500));
setProgressBar(state: ProgressBarState): Promise<void>

Sets the taskbar progress state.

  • Linux / macOS: Progress bar is app-wide and not specific to this window.
  • Linux: Only supported desktop environments with libunity (e.g. GNOME).

Example

import { getCurrentWindow, ProgressBarStatus } from '@netwiv/api/window';
await getCurrentWindow().setProgressBar({
status: ProgressBarStatus.Normal,
progress: 50,
});
setResizable(resizable: boolean): Promise<void>

Updates the window resizable flag.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().setResizable(false);
setShadow(enable: boolean): Promise<void>

Whether or not the window should have shadow.

  • Windows:
    • false has no effect on decorated window, shadows are always ON.
    • true will make undecorated window have a 1px white border, and on Windows 11, it will have a rounded corners.
  • Linux: Unsupported.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().setShadow(false);
setSimpleFullscreen(fullscreen: boolean): Promise<void>

On macOS, Toggles a fullscreen mode that doesn’t require a new macOS space. Returns a boolean indicating whether the transition was successful (this won’t work if the window was already in the native fullscreen). This is how fullscreen used to work on macOS in versions before Lion. And allows the user to have a fullscreen window without using another space or taking control over the entire monitor.

On other platforms, this is the same as {@link Window.setFullscreen}.

setSize(size: LogicalSize | PhysicalSize | Size): Promise<void>

Resizes the window with a new inner size.

Example

import { getCurrentWindow, LogicalSize } from '@netwiv/api/window';
await getCurrentWindow().setSize(new LogicalSize(600, 500));
setSizeConstraints(constraints: WindowSizeConstraints | null | undefined): Promise<void>

Sets the window inner size constraints.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().setSizeConstraints({ minWidth: 300 });
setSkipTaskbar(skip: boolean): Promise<void>

Whether the window icon should be hidden from the taskbar or not.

  • macOS: Unsupported.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().setSkipTaskbar(true);
setTheme(theme?: Theme | null | undefined): Promise<void>

Set window theme, pass in null or undefined to follow system theme

  • Linux / macOS: Theme is app-wide and not specific to this window.
  • iOS / Android: Unsupported.
setTitle(title: string): Promise<void>

Sets the window title.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().setTitle('Tauri');
setTitleBarStyle(style: TitleBarStyle): Promise<void>

Sets the title bar style. macOS only.

setVisibleOnAllWorkspaces(visible: boolean): Promise<void>

Sets whether the window should be visible on all workspaces or virtual desktops.

  • Windows / iOS / Android: Unsupported.
show(): Promise<void>

Sets the window visibility to true.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().show();
startDragging(): Promise<void>

Starts dragging the window.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().startDragging();
startResizeDragging(direction: ResizeDirection): Promise<void>

Starts resize-dragging the window.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().startResizeDragging();
theme(): Promise<Theme | null>

Gets the window’s current theme.

  • macOS: Theme was introduced on macOS 10.14. Returns light on macOS 10.13 and below.

Example

import { getCurrentWindow } from '@netwiv/api/window';
const theme = await getCurrentWindow().theme();
title(): Promise<string>

Gets the window’s current title.

Example

import { getCurrentWindow } from '@netwiv/api/window';
const title = await getCurrentWindow().title();
toggleMaximize(): Promise<void>

Toggles the window maximized state.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().toggleMaximize();
unmaximize(): Promise<void>

Unmaximizes the window.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().unmaximize();
unminimize(): Promise<void>

Unminimizes the window.

Example

import { getCurrentWindow } from '@netwiv/api/window';
await getCurrentWindow().unminimize();

Descriptions come from the pinned @tauri-apps/api@2.11.1 declarations (MIT OR Apache-2.0); imports are shown with @netwiv/api paths.