跳到内容
Tauri

webviewWindow

重新导出 Color

重新导出 DragDropEvent

创建新的 Webview 或获取现有 Webview 的句柄。

Webview 通过一个 _标签_(一个唯一的标识符,可用于后续引用)来标识。它只能包含字母数字字符 a-zA-Z 以及以下特殊字符 -, /, :_

import { Window } from "@tauri-apps/api/window"
import { Webview } from "@tauri-apps/api/webview"
const appWindow = new Window('uniqueLabel');
appWindow.once('tauri://created', async function () {
// `new Webview` Should be called after the window is successfully created,
// or webview may not be attached to the window since window is not created yet.
// loading embedded asset:
const webview = new Webview(appWindow, 'theUniqueLabel', {
url: 'path/to/page.html',
// create a webview with specific logical position and size
x: 0,
y: 0,
width: 800,
height: 600,
});
// alternatively, load a remote URL:
const webview = new Webview(appWindow, 'theUniqueLabel', {
url: 'https://github.com/tauri-apps/tauri',
// create a webview with specific logical position and size
x: 0,
y: 0,
width: 800,
height: 600,
});
webview.once('tauri://created', function () {
// webview successfully created
});
webview.once('tauri://error', function (e) {
// an error happened creating the webview
});
// emit an event to the backend
await webview.emit("some-event", "data");
// listen to an event from the backend
const unlisten = await webview.listen("event-name", e => { });
unlisten();
});

2.0.0

new WebviewWindow(label, options): WebviewWindow

创建新的 Window 来托管 Webview

参数类型描述
标签字符串唯一的 webview 标签。必须是字母数字字符:a-zA-Z-/:_
选项Omit<WebviewOptions, "x" | "y" | "width" | "height"> & WindowOptions-

WebviewWindow

用于与窗口和 webview 通信的 WebviewWindow 实例。

import { WebviewWindow } from '@tauri-apps/api/webviewWindow'
const webview = new WebviewWindow('my-label', {
url: 'https://github.com/tauri-apps/tauri'
});
webview.once('tauri://created', function () {
// webview successfully created
});
webview.once('tauri://error', function (e) {
// an error happened creating the webview
});

Window.constructor

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webviewWindow.ts#L75

属性类型描述继承自 (Inherited from)定义于
label字符串webview 标签。它是 webview 的唯一标识符,可用于后续引用。Window.label: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webviewWindow.ts#L51
listenersRecord<string, EventCallback<any>[]>本地事件监听器。Window.listeners: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webviewWindow.ts#L54
window窗口托管此 webview 的窗口。Webview.window: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L156

center(): Promise<void>

将窗口居中。

Promise<void>

表示操作成功或失败的 Promise。

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

Window.center

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L835

clearAllBrowsingData(): Promise<void>

清除此 webview 的所有浏览数据。

Promise<void>

表示操作成功或失败的 Promise。

import { getCurrentWebview } from '@tauri-apps/api/webview';
await getCurrentWebview().clearAllBrowsingData();

Webview.clearAllBrowsingData

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L588

clearEffects(): Promise<void>

清除所有应用的效果(如果可能)。

Promise<void>

Window.clearEffects

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1223

close(): Promise<void>

关闭 webview。

Promise<void>

表示操作成功或失败的 Promise。

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

Window.close

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L435

destroy(): Promise<void>

销毁窗口。行为类似于 Window.close,但强制关闭窗口而不是发出 closeRequested 事件。

Promise<void>

表示操作成功或失败的 Promise。

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

Window.destroy

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1160

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

向所有 目标 发出事件。

类型参数
T
参数类型描述
event字符串事件名称。必须只包含字母数字字符、-/:_
载荷 (payload)?T事件载荷。

Promise<void>

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

Window.emit

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L324

emitTo<T>(
target,
event,
payload?): Promise<void>

向所有与给定目标匹配的 目标 发出事件。

类型参数
T
参数类型描述
目标string | EventTarget目标窗口/WebView/WebviewWindow 的标签,或原始 EventTarget 对象。
event字符串事件名称。必须只包含字母数字字符、-/:_
载荷 (payload)?T事件载荷。

Promise<void>

import { getCurrentWebview } from '@tauri-apps/api/webview';
await getCurrentWebview().emitTo('main', 'webview-loaded', { loggedIn: true, token: 'authToken' });

Window.emitTo

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L352

hide(): Promise<void>

隐藏 webview。

Promise<void>

表示操作成功或失败的 Promise。

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

Window.hide

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L522

innerPosition(): Promise<PhysicalPosition>

窗口客户区左上角相对于桌面左上角的位置。

Promise<PhysicalPosition>

窗口的内部位置。

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

Window.innerPosition

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L537

innerSize(): Promise<PhysicalSize>

窗口客户区的物理大小。客户区是窗口的内容,不包括标题栏和边框。

Promise<PhysicalSize>

窗口的内部大小。

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

Window.innerSize

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L570

isAlwaysOnTop(): Promise<boolean>

窗口是否配置为始终置于其他窗口之上。

Promise<boolean>

窗口是否可见。

import { getCurrentWindow } from '@tauri-apps/api/window';
const alwaysOnTop = await getCurrentWindow().isAlwaysOnTop();

Window.isAlwaysOnTop

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L817

isClosable(): Promise<boolean>

获取窗口的原生关闭按钮状态。

平台特定

  • iOS / Android: 不支持。

Promise<boolean>

窗口的原生关闭按钮是否启用。

import { getCurrentWindow } from '@tauri-apps/api/window';
const closable = await getCurrentWindow().isClosable();

Window.isClosable

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L750

isDecorated(): Promise<boolean>

获取窗口当前的装饰状态。

Promise<boolean>

窗口是否已装饰。

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

Window.isDecorated

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L671

isEnabled(): Promise<boolean>

窗口是否启用或禁用。

Promise<boolean>

表示操作成功或失败的 Promise。

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

2.0.0

Window.isEnabled

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L927

isFocused(): Promise<boolean>

获取窗口当前的焦点状态。

Promise<boolean>

窗口是否聚焦。

import { getCurrentWindow } from '@tauri-apps/api/window';
const focused = await getCurrentWindow().isFocused();

Window.isFocused

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L655

isFullscreen(): Promise<boolean>

获取窗口当前的满屏状态。

Promise<boolean>

窗口是否处于全屏模式。

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

Window.isFullscreen

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L609

isMaximizable(): Promise<boolean>

获取窗口原生最大化按钮的状态。

平台特定

  • Linux / iOS / Android: 不支持。

Promise<boolean>

窗口的原生最大化按钮是否启用。

import { getCurrentWindow } from '@tauri-apps/api/window';
const maximizable = await getCurrentWindow().isMaximizable();

Window.isMaximizable

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L708

isMaximized(): Promise<boolean>

获取窗口当前的最大化状态。

Promise<boolean>

窗口是否最大化。

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

Window.isMaximized

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L639

isMinimizable(): Promise<boolean>

获取窗口的原生最小化按钮状态。

平台特定

  • Linux / iOS / Android: 不支持。

Promise<boolean>

窗口的原生最小化按钮是否启用。

import { getCurrentWindow } from '@tauri-apps/api/window';
const minimizable = await getCurrentWindow().isMinimizable();

Window.isMinimizable

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L729

isMinimized(): Promise<boolean>

获取窗口当前的最小化状态。

Promise<boolean>

import { getCurrentWindow } from '@tauri-apps/api/window';
const minimized = await getCurrentWindow().isMinimized();

Window.isMinimized

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L623

isResizable(): Promise<boolean>

获取窗口当前的可调整大小状态。

Promise<boolean>

窗口是否可调整大小。

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

Window.isResizable

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L687

isVisible(): Promise<boolean>

获取窗口当前的可见状态。

Promise<boolean>

窗口是否可见。

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

Window.isVisible

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L766

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

侦听此 webivew 窗口上发出的事件。

类型参数
T
参数类型描述
event事件名称事件名称。必须只包含字母数字字符、-/:_
处理程序EventCallback<T>事件处理程序。

Promise<UnlistenFn>

一个解析为取消监听事件函数的 Promise。请注意,如果您的监听器超出范围(例如组件已卸载),则必须删除该监听器。

import { WebviewWindow } from '@tauri-apps/api/webviewWindow';
const unlisten = await WebviewWindow.getCurrent().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();

Window.listen

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webviewWindow.ts#L155

maximize(): Promise<void>

最大化窗口。

Promise<void>

表示操作成功或失败的 Promise。

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

Window.maximize

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1030

minimize(): Promise<void>

最小化窗口。

Promise<void>

表示操作成功或失败的 Promise。

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

Window.minimize

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1078

onCloseRequested(handler): Promise<UnlistenFn>

侦听窗口关闭请求。当用户请求关闭窗口时发出。

参数类型
处理程序(event) => void | Promise<void>

Promise<UnlistenFn>

一个解析为取消监听事件函数的 Promise。请注意,如果您的监听器超出范围(例如组件已卸载),则必须删除该监听器。

import { getCurrentWindow } from "@tauri-apps/api/window";
import { confirm } from '@tauri-apps/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();

Window.onCloseRequested

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1885

onDragDropEvent(handler): Promise<UnlistenFn>

监听文件拖放事件。当用户将选定的文件悬停在 webview 上、拖放文件或取消操作时,会触发监听器。

参数类型
处理程序EventCallback<DragDropEvent>

Promise<UnlistenFn>

一个解析为取消监听事件函数的 Promise。请注意,如果您的监听器超出范围(例如组件已卸载),则必须删除该监听器。

import { getCurrentWebview } from "@tauri-apps/api/webview";
const unlisten = await getCurrentWebview().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();

当调试器面板打开时,由于已知限制,此事件的拖放位置可能不准确。要检索正确的拖放位置,请分离调试器。

Window.onDragDropEvent

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L640

onFocusChanged(handler): Promise<UnlistenFn>

监听窗口焦点变化。

参数类型
处理程序EventCallback<boolean>

Promise<UnlistenFn>

一个解析为取消监听事件函数的 Promise。请注意,如果您的监听器超出范围(例如组件已卸载),则必须删除该监听器。

import { getCurrentWindow } from "@tauri-apps/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();

Window.onFocusChanged

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2001

onMoved(handler): Promise<UnlistenFn>

侦听窗口移动。

参数类型
处理程序EventCallback<PhysicalPosition>

Promise<UnlistenFn>

一个解析为取消监听事件函数的 Promise。请注意,如果您的监听器超出范围(例如组件已卸载),则必须删除该监听器。

import { getCurrentWindow } from "@tauri-apps/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();

Window.onMoved

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1856

onResized(handler): Promise<UnlistenFn>

侦听窗口大小调整。

参数类型
处理程序EventCallback<PhysicalSize>

Promise<UnlistenFn>

一个解析为取消监听事件函数的 Promise。请注意,如果您的监听器超出范围(例如组件已卸载),则必须删除该监听器。

import { getCurrentWindow } from "@tauri-apps/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();

Window.onResized

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1832

onScaleChanged(handler): Promise<UnlistenFn>

侦听窗口缩放更改。当窗口的缩放因子发生变化时发出。以下用户操作可能导致 DPI 更改:

  • 更改显示器的分辨率。
  • 更改显示器的缩放因子(例如在 Windows 的控制面板中)。
  • 将窗口移动到具有不同缩放因子的显示器。
参数类型
处理程序EventCallback<ScaleFactorChanged>

Promise<UnlistenFn>

一个解析为取消监听事件函数的 Promise。请注意,如果您的监听器超出范围(例如组件已卸载),则必须删除该监听器。

import { getCurrentWindow } from "@tauri-apps/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();

Window.onScaleChanged

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2041

onThemeChanged(handler): Promise<UnlistenFn>

侦听系统主题更改。

参数类型
处理程序EventCallback<Theme>

Promise<UnlistenFn>

一个解析为取消监听事件函数的 Promise。请注意,如果您的监听器超出范围(例如组件已卸载),则必须删除该监听器。

import { getCurrentWindow } from "@tauri-apps/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();

Window.onThemeChanged

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2067

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

仅在此 webview 窗口上侦听一次发出的事件。

类型参数
T
参数类型描述
event事件名称事件名称。必须只包含字母数字字符、-/:_
处理程序EventCallback<T>事件处理程序。

Promise<UnlistenFn>

一个解析为取消监听事件函数的 Promise。请注意,如果您的监听器超出范围(例如组件已卸载),则必须删除该监听器。

import { WebviewWindow } from '@tauri-apps/api/webviewWindow';
const unlisten = await WebviewWindow.getCurrent().once<null>('initialized', (event) => {
console.log(`Webview initialized!`);
});
// you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
unlisten();

Window.once

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webviewWindow.ts#L190

outerPosition(): Promise<PhysicalPosition>

窗口左上角相对于桌面左上角的位置。

Promise<PhysicalPosition>

窗口的外部位置。

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

Window.outerPosition

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L553

outerSize(): Promise<PhysicalSize>

整个窗口的物理大小。这些尺寸包括标题栏和边框。如果你不想要(通常也不需要),请改用 inner_size。

Promise<PhysicalSize>

窗口的外部大小。

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

Window.outerSize

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L590

position(): Promise<PhysicalPosition>

webview 客户区左上角相对于桌面左上角的位置。

Promise<PhysicalPosition>

webview 的位置。

import { getCurrentWebview } from '@tauri-apps/api/webview';
const position = await getCurrentWebview().position();

Webview.position

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L397

reparent(window): Promise<void>

将此 webview 移动到给定的标签。

参数类型
windowstring | Window | WebviewWindow

Promise<void>

表示操作成功或失败的 Promise。

import { getCurrentWebview } from '@tauri-apps/api/webview';
await getCurrentWebview().reparent('other-window');

Webview.reparent

: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L571

requestUserAttention(requestType): Promise<void>

请求用户关注窗口,如果应用程序已经获得焦点,则此操作无效。请求用户关注的方式因平台而异,详情请参见 UserAttentionType

提供 null 将取消对用户关注的请求。当窗口接收到输入时,WM 可能不会自动取消用户关注的请求。

平台特定

  • macOS: null 无效。
  • Linux: 紧急程度具有相同的效果。
参数类型
requestTypenull | UserAttentionType

Promise<void>

表示操作成功或失败的 Promise。

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

Window.requestUserAttention

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L861

scaleFactor(): Promise<number>

可用于将物理像素映射到逻辑像素的缩放因子。

Promise<number>

窗口的监视器缩放因子。

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

Window.scaleFactor

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L521

setAlwaysOnBottom(alwaysOnBottom): Promise<void>

窗口是否应始终位于其他窗口下方。

参数类型描述
alwaysOnBottom布尔值 (boolean)窗口是否应始终位于其他窗口下方。

Promise<void>

表示操作成功或失败的 Promise。

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

Window.setAlwaysOnBottom

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1259

setAlwaysOnTop(alwaysOnTop): Promise<void>

窗口是否应始终位于其他窗口之上。

参数类型描述
alwaysOnTop布尔值 (boolean)窗口是否应始终位于其他窗口之上。

Promise<void>

表示操作成功或失败的 Promise。

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

Window.setAlwaysOnTop

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1241

setAutoResize(autoResize): Promise<void>

设置当父窗口调整大小时,webview 是否应自动增长和收缩其大小和位置。

参数类型
autoResize布尔值 (boolean)

Promise<void>

表示操作成功或失败的 Promise。

import { getCurrentWebview } from '@tauri-apps/api/webview';
await getCurrentWebview().setAutoResize(true);

Webview.setAutoResize

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L505

setBackgroundColor(color): Promise<void>

设置窗口和 webview 背景颜色。

平台特定

  • Android / iOS: 窗口层不支持。
  • macOS / iOS: webview 层未实现。
  • Windows:
    • 窗口层的 alpha 通道被忽略。
    • 在 Windows 7 上,webview 层的 alpha 通道被忽略。
    • 在 Windows 8 及更高版本上,如果 alpha 通道不为 0,则会被忽略。
参数类型
颜色Color

Promise<void>

表示操作成功或失败的 Promise。

2.1.0

Window.setBackgroundColor

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webviewWindow.ts#L222

setBadgeCount(count?): Promise<void>

设置徽章计数。它是应用程序范围的,不特定于此窗口。

平台特定

  • Windows: 不支持。请改用 @{linkcode Window.setOverlayIcon}。
参数类型描述
count?数字徽章计数。使用 undefined 删除徽章。

Promise<void>

表示操作成功或失败的 Promise。

import { getCurrentWindow } from '@tauri-apps/api/window';
await getCurrentWindow().setBadgeCount(5);

Window.setBadgeCount

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1685

setBadgeLabel(label?): Promise<void>

设置徽章内容,仅支持 **macOS**。

参数类型描述
标签?字符串徽章标签。使用 undefined 删除徽章。

Promise<void>

表示操作成功或失败的 Promise。

import { getCurrentWindow } from '@tauri-apps/api/window';
await getCurrentWindow().setBadgeLabel("Hello");

Window.setBadgeLabel

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1704

setClosable(closable): Promise<void>

设置窗口的原生关闭按钮是否启用。

平台特定

  • Linux: GTK+ 将尽力说服窗口管理器不显示关闭按钮。根据系统不同,此功能在已可见的窗口上调用时可能无效。
  • iOS / Android: 不支持。
参数类型
可关闭布尔值 (boolean)

Promise<void>

表示操作成功或失败的 Promise。

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

Window.setClosable

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L995

setContentProtected(protected_): Promise<void>

防止窗口内容被其他应用程序捕获。

参数类型
protected_布尔值 (boolean)

Promise<void>

表示操作成功或失败的 Promise。

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

Window.setContentProtected

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1276

setCursorGrab(grab): Promise<void>

抓取光标,防止光标离开窗口。

无法保证光标会隐藏。如果您希望隐藏,应自行隐藏。

平台特定

  • Linux: 不支持。
  • macOS: 这会将光标锁定在固定位置,视觉上会显得很笨拙。
参数类型描述
grab布尔值 (boolean)true 抓取光标图标,false 释放光标图标。

Promise<void>

表示操作成功或失败的 Promise。

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

Window.setCursorGrab

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1532

setCursorIcon(icon): Promise<void>

修改窗口的光标图标。

参数类型描述
icon光标图标新的光标图标。

Promise<void>

表示操作成功或失败的 Promise。

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

Window.setCursorIcon

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1574

setCursorPosition(position): Promise<void>

更改窗口坐标中光标的位置。

参数类型描述
位置LogicalPosition | PhysicalPosition | Position新的光标位置。

Promise<void>

表示操作成功或失败的 Promise。

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

Window.setCursorPosition

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1608

setCursorVisible(visible): Promise<void>

修改光标的可见性。

平台特定

  • Windows: 光标仅在窗口范围内隐藏。
  • macOS: 只要窗口有输入焦点,即使光标在窗口外,也会隐藏光标。
参数类型描述
可见布尔值 (boolean)如果为 false,这将隐藏光标。如果为 true,这将显示光标。

Promise<void>

表示操作成功或失败的 Promise。

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

Window.setCursorVisible

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1556

setDecorations(decorations): Promise<void>

窗口是否应该有边框和标题栏。

参数类型描述
装饰布尔值 (boolean)窗口是否应该有边框和标题栏。

Promise<void>

表示操作成功或失败的 Promise。

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

Window.setDecorations

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1177

setEffects(effects): Promise<void>

设置窗口效果。

参数类型
效果效果

Promise<void>

Window.setEffects

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1213

setEnabled(enabled): Promise<void>

启用或禁用窗口。

参数类型
已启用布尔值 (boolean)

Promise<void>

表示操作成功或失败的 Promise。

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

2.0.0

Window.setEnabled

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L908

setFocus(): Promise<void>

将 webview 置于最前端并聚焦。

Promise<void>

表示操作成功或失败的 Promise。

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

Window.setFocus

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L489

setFocusable(focusable): Promise<void>

设置窗口是否可聚焦。

平台特定

  • macOS:如果窗口已聚焦,则在调用 set_focusable(false) 后无法取消聚焦。在这种情况下,您可以考虑调用 Window.setFocus,但这会将窗口移动到背面,即在 z 轴顺序上置于底部。
参数类型描述
可聚焦布尔值 (boolean)窗口是否可聚焦。

Promise<void>

表示操作成功或失败的 Promise。

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

Window.setFocusable

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1457

setFullscreen(fullscreen): Promise<void>

设置窗口全屏状态。

参数类型描述
全屏布尔值 (boolean)窗口是否全屏。

Promise<void>

表示操作成功或失败的 Promise。

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

Window.setFullscreen

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1401

setIcon(icon): Promise<void>

设置窗口图标。

参数类型描述
icon| string | Uint8Array | number[] | ArrayBuffer | Image图标字节或图标文件路径。

Promise<void>

表示操作成功或失败的 Promise。

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

请注意,您可能需要 image-icoimage-png Cargo 功能才能使用此 API。要启用它,请更改您的 Cargo.toml 文件

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

Window.setIcon

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1482

setIgnoreCursorEvents(ignore): Promise<void>

更改光标事件行为。

参数类型描述
忽略布尔值 (boolean)true 忽略光标事件;false 正常处理。

Promise<void>

表示操作成功或失败的 Promise。

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

Window.setIgnoreCursorEvents

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1629

setMaxSize(size): Promise<void>

设置窗口的最大内部尺寸。如果 size 参数未定义,则取消限制。

参数类型描述
尺寸| undefined | null | LogicalSize | PhysicalSize | Size逻辑或物理内部尺寸,或 null 取消约束。

Promise<void>

表示操作成功或失败的 Promise。

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

Window.setMaxSize

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1332

setMaximizable(maximizable): Promise<void>

设置窗口的原生最大化按钮是否启用。如果 resizable 设置为 false,则此设置将被忽略。

平台特定

  • macOS: 禁用窗口标题栏中的“缩放”按钮,该按钮也用于进入全屏模式。
  • Linux / iOS / Android: 不支持。
参数类型
可最大化布尔值 (boolean)

Promise<void>

表示操作成功或失败的 Promise。

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

Window.setMaximizable

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L950

setMinSize(size): Promise<void>

设置窗口的最小内部尺寸。如果未提供 size 参数,则取消约束。

参数类型描述
尺寸| undefined | null | LogicalSize | PhysicalSize | Size逻辑或物理内部尺寸,或 null 取消约束。

Promise<void>

表示操作成功或失败的 Promise。

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

Window.setMinSize

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1312

setMinimizable(minimizable): Promise<void>

设置窗口的原生最小化按钮是否启用。

平台特定

  • Linux / iOS / Android: 不支持。
参数类型
可最小化布尔值 (boolean)

Promise<void>

表示操作成功或失败的 Promise。

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

Window.setMinimizable

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L972

setOverlayIcon(icon?): Promise<void>

设置覆盖图标。**仅限 Windows**。每个窗口都可以设置覆盖图标。

请注意,您可能需要 image-icoimage-png Cargo 功能才能使用此 API。要启用它,请更改您的 Cargo.toml 文件

[dependencies]
tauri = { version = "...", features = ["...", "image-png"] }
参数类型描述
icon?| string | Uint8Array | number[] | ArrayBuffer | Image图标字节或图标文件路径。使用 undefined 删除覆盖图标。

Promise<void>

表示操作成功或失败的 Promise。

import { getCurrentWindow } from '@tauri-apps/api/window';
await getCurrentWindow().setOverlayIcon("/tauri/awesome.png");

Window.setOverlayIcon

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1733

setPosition(position): Promise<void>

设置 webview 位置。

参数类型描述
位置LogicalPosition | PhysicalPosition | Position新位置,以逻辑像素或物理像素表示。

Promise<void>

表示操作成功或失败的 Promise。

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

Window.setPosition

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L470

setProgressBar(state): Promise<void>

设置任务栏进度状态。

平台特定

  • Linux / macOS: 进度条是应用程序范围的,不特定于此窗口。
  • Linux: 仅支持带有 libunity 的桌面环境(例如 GNOME)。
参数类型
state进度条状态

Promise<void>

表示操作成功或失败的 Promise。

import { getCurrentWindow, ProgressBarStatus } from '@tauri-apps/api/window';
await getCurrentWindow().setProgressBar({
status: ProgressBarStatus.Normal,
progress: 50,
});

Window.setProgressBar

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1761

setResizable(resizable): Promise<void>

更新窗口可调整大小的标志。

参数类型
可调整大小布尔值 (boolean)

Promise<void>

表示操作成功或失败的 Promise。

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

Window.setResizable

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L889

setShadow(enable): Promise<void>

窗口是否应该有阴影。

平台特定

  • Windows
    • false 对有装饰的窗口无效,阴影始终开启。
    • true 将使无装饰窗口有 1 像素的白色边框,在 Windows 11 上,它将有圆角。
  • Linux: 不支持。
参数类型
启用布尔值 (boolean)

Promise<void>

表示操作成功或失败的 Promise。

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

Window.setShadow

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1203

setSimpleFullscreen(fullscreen): Promise<void>

在 macOS 上,切换到不需要新 macOS 空间的全屏模式。返回一个布尔值,表示转换是否成功(如果窗口已经处于原生全屏模式,则此操作无效)。这是 macOS Lion 以前版本中全屏的工作方式。它允许用户在不使用另一个空间或不控制整个显示器的情况下拥有全屏窗口。

在其他平台上,这与 Window.setFullscreen 相同。

参数类型描述
全屏布尔值 (boolean)窗口是否应进入简单全屏模式。

Promise<void>

表示操作成功或失败的 Promise。

Window.setSimpleFullscreen

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1417

setSize(size): Promise<void>

调整 webview 大小。

参数类型描述
尺寸LogicalSize | PhysicalSize | Size逻辑或物理大小。

Promise<void>

表示操作成功或失败的 Promise。

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

Window.setSize

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L452

setSizeConstraints(constraints): Promise<void>

设置窗口内部大小约束。

参数类型描述
约束undefined | null | WindowSizeConstraints逻辑或物理内部尺寸,或 null 取消约束。

Promise<void>

表示操作成功或失败的 Promise。

import { getCurrentWindow } from '@tauri-apps/api/window';
await getCurrentWindow().setSizeConstraints({ minWidth: 300 });

Window.setSizeConstraints

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1352

setSkipTaskbar(skip): Promise<void>

窗口图标是否应从任务栏中隐藏。

平台特定

  • macOS: 不支持。
参数类型描述
跳过布尔值 (boolean)true 隐藏窗口图标,false 显示窗口图标。

Promise<void>

表示操作成功或失败的 Promise。

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

Window.setSkipTaskbar

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1506

setTheme(theme?): Promise<void>

设置窗口主题,传入 nullundefined 以跟随系统主题

平台特定

  • Linux / macOS: 主题是应用程序范围的,不特定于此窗口。
  • iOS / Android: 不支持。
参数类型
主题?null | Theme

Promise<void>

2.0.0

Window.setTheme

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1806

setTitle(title): Promise<void>

设置窗口标题。

参数类型描述
标题字符串新标题

Promise<void>

表示操作成功或失败的 Promise。

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

Window.setTitle

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1013

setTitleBarStyle(style): Promise<void>

设置标题栏样式。**仅限 macOS**。

参数类型
样式标题栏样式

Promise<void>

2.0.0

Window.setTitleBarStyle

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1789

setVisibleOnAllWorkspaces(visible): Promise<void>

设置窗口是否应在所有工作区或虚拟桌面上可见。

平台特定

  • Windows / iOS / Android: 不支持。
参数类型
可见布尔值 (boolean)

Promise<void>

2.0.0

Window.setVisibleOnAllWorkspaces

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1777

setZoom(scaleFactor): Promise<void>

设置 webview 缩放级别。

参数类型
缩放因子数字

Promise<void>

表示操作成功或失败的 Promise。

import { getCurrentWebview } from '@tauri-apps/api/webview';
await getCurrentWebview().setZoom(1.5);

Webview.setZoom

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L554

show(): Promise<void>

显示 webview。

Promise<void>

表示操作成功或失败的 Promise。

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

Window.show

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L538

size(): Promise<PhysicalSize>

webview 客户端区域的物理尺寸。客户端区域是 webview 的内容,不包括标题栏和边框。

Promise<PhysicalSize>

webview 的大小。

import { getCurrentWebview } from '@tauri-apps/api/webview';
const size = await getCurrentWebview().size();

Webview.size

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L414

startDragging(): Promise<void>

开始拖动窗口。

Promise<void>

表示操作成功或失败的 Promise。

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

Window.startDragging

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1646

startResizeDragging(direction): Promise<void>

开始拖动窗口以调整大小。

参数类型
方向调整大小方向

Promise<void>

表示操作成功或失败的 Promise。

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

Window.startResizeDragging

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1662

theme(): Promise<null | Theme>

获取窗口的当前主题。

平台特定

  • macOS: 主题在 macOS 10.14 中引入。在 macOS 10.13 及更低版本上返回 light

Promise<null | Theme>

窗口主题。

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

Window.theme

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L801

title(): Promise<string>

获取窗口的当前标题。

Promise<string>

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

Window.title

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L780

toggleMaximize(): Promise<void>

切换窗口最大化状态。

Promise<void>

表示操作成功或失败的 Promise。

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

Window.toggleMaximize

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1062

unmaximize(): Promise<void>

取消窗口最大化。

Promise<void>

表示操作成功或失败的 Promise。

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

Window.unmaximize

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1046

unminimize(): Promise<void>

取消窗口最小化。

Promise<void>

表示操作成功或失败的 Promise。

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

Window.unminimize

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1094

static getAll(): Promise<WebviewWindow[]>

获取所有可用 webview 的 Webview 实例列表。

Promise<WebviewWindow[]>

Window.getAll

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webviewWindow.ts#L132

static getByLabel(label): Promise<null | WebviewWindow>

获取与给定标签关联的 webview 的 Webview 实例。

参数类型描述
标签字符串webview 标签。

Promise<null | WebviewWindow>

与 webview 通信的 Webview 实例,如果 webview 不存在则为 null。

import { Webview } from '@tauri-apps/api/webviewWindow';
const mainWebview = Webview.getByLabel('main');

Window.getByLabel

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webviewWindow.ts#L112

static getCurrent(): WebviewWindow

获取当前 webview 的 Webview 实例。

WebviewWindow

Window.getCurrent

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webviewWindow.ts#L125

function getAllWebviewWindows(): Promise<WebviewWindow[]>

获取所有可用 webview 窗口的 Webview 实例列表。

Promise<WebviewWindow[]>

2.0.0

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webviewWindow.ts#L34


function getCurrentWebviewWindow(): WebviewWindow

获取当前 webview 窗口的 Webview 实例。

WebviewWindow

2.0.0

来源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webviewWindow.ts#L23


© 2025 Tauri 贡献者。CC-BY / MIT