webviewWindow
参考
颜色
重新导出 Color
DragDropEvent
重新导出 DragDropEvent
类
WebviewWindow
创建新的 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');
// loading embedded asset:const webview = new Webview(appWindow, 'theUniqueLabel', { url: 'path/to/page.html'});// alternatively, load a remote URL:const webview = new Webview(appWindow, 'theUniqueLabel', { 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});
// emit an event to the backendawait webview.emit("some-event", "data");// listen to an event from the backendconst unlisten = await webview.listen("event-name", e => {});unlisten();
始于
2.0.0
继承自
构造函数
new WebviewWindow()
new WebviewWindow(label, options): WebviewWindow
参数
参数 | 类型 | 描述 |
---|---|---|
label | string | 唯一的 webview 标签。必须是字母数字:a-zA-Z-/:_ 。 |
options | Omit <WebviewOptions , "width" | "height" | "x" | "y" > & WindowOptions | - |
返回值
用于与窗口和 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});
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webviewWindow.ts#L75
属性
属性 | 类型 | 描述 | 继承自 | 定义在 |
---|---|---|---|---|
label | string | webview 标签。它是 webview 的唯一标识符,可用于稍后引用它。 | Window .label | Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webviewWindow.ts#L51 |
listeners | Record <string , EventCallback <any >[]> | 本地事件监听器。 | Window .listeners | Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webviewWindow.ts#L54 |
window | 窗口 | 托管此 webview 的窗口。 | Webview .window | Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L138 |
方法
center()
center(): Promise<void>
将窗口居中。
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().center();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L814
clearAllBrowsingData()
clearAllBrowsingData(): Promise<void>
清除此 webview 的所有浏览数据。
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWebview } from '@tauri-apps/api/webview';await getCurrentWebview().clearAllBrowsingData();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L541
clearEffects()
clearEffects(): Promise<void>
清除任何已应用的效果(如果可能)。
返回值
Promise
<void
>
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1202
close()
close(): Promise<void>
关闭 webview。
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWebview } from '@tauri-apps/api/webview';await getCurrentWebview().close();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L405
destroy()
destroy(): Promise<void>
销毁窗口。行为类似于 Window.close,但强制关闭窗口,而不是发出 closeRequested 事件。
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().destroy();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1139
emit()
emit(event, payload?): Promise<void>
向所有 目标 发送事件。
参数
参数 | 类型 | 描述 |
---|---|---|
event | string | 事件名称。必须仅包含字母数字字符、- 、/ 、: 和 _ 。 |
payload ? | unknown | 事件负载。 |
返回值
Promise
<void
>
示例
import { getCurrentWebview } from '@tauri-apps/api/webview';await getCurrentWebview().emit('webview-loaded', { loggedIn: true, token: 'authToken' });
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L294
emitTo()
emitTo( target, event,payload?): Promise<void>
向与给定目标匹配的所有 目标 发送事件。
参数
参数 | 类型 | 描述 |
---|---|---|
target | string | EventTarget | 目标 Window/Webview/WebviewWindow 的标签或原始 EventTarget 对象。 |
event | string | 事件名称。必须仅包含字母数字字符、- 、/ 、: 和 _ 。 |
payload ? | unknown | 事件负载。 |
返回值
Promise
<void
>
示例
import { getCurrentWebview } from '@tauri-apps/api/webview';await getCurrentWebview().emitTo('main', 'webview-loaded', { loggedIn: true, token: 'authToken' });
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L322
hide()
hide(): Promise<void>
隐藏 webview。
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWebview } from '@tauri-apps/api/webview';await getCurrentWebview().hide();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L475
innerPosition()
innerPosition(): Promise<PhysicalPosition>
窗口客户端区域的左上角相对于桌面左上角的位置。
返回值
窗口的内部位置。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';const position = await getCurrentWindow().innerPosition();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L532
innerSize()
innerSize(): Promise<PhysicalSize>
窗口客户端区域的物理大小。客户端区域是窗口的内容,不包括标题栏和边框。
返回值
窗口的内部大小。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';const size = await getCurrentWindow().innerSize();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L565
isClosable()
isClosable(): Promise<boolean>
获取窗口的本机关闭按钮状态。
平台特定
- iOS / Android: 不支持。
返回值
Promise
<boolean
>
窗口的本机关闭按钮是否启用。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';const closable = await getCurrentWindow().isClosable();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L745
isDecorated()
isDecorated(): Promise<boolean>
获取窗口当前的装饰状态。
返回值
Promise
<boolean
>
窗口是否被装饰。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';const decorated = await getCurrentWindow().isDecorated();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L666
isEnabled()
isEnabled(): Promise<boolean>
窗口是否启用或禁用。
返回值
Promise
<boolean
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().setEnabled(false);
始于
2.0.0
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L906
isFocused()
isFocused(): Promise<boolean>
获取窗口当前焦点状态。
返回值
Promise
<boolean
>
窗口是否获得焦点。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';const focused = await getCurrentWindow().isFocused();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L650
isFullscreen()
isFullscreen(): Promise<boolean>
获取窗口当前全屏状态。
返回值
Promise
<boolean
>
窗口是否处于全屏模式。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';const fullscreen = await getCurrentWindow().isFullscreen();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L604
isMaximizable()
isMaximizable(): Promise<boolean>
获取窗口的本机最大化按钮状态。
平台特定
- Linux / iOS / Android: 不支持。
返回值
Promise
<boolean
>
窗口的本机最大化按钮是否启用。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';const maximizable = await getCurrentWindow().isMaximizable();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L703
isMaximized()
isMaximized(): Promise<boolean>
获取窗口当前最大化状态。
返回值
Promise
<boolean
>
窗口是否最大化。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';const maximized = await getCurrentWindow().isMaximized();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L634
isMinimizable()
isMinimizable(): Promise<boolean>
获取窗口的本机最小化按钮状态。
平台特定
- Linux / iOS / Android: 不支持。
返回值
Promise
<boolean
>
窗口的本机最小化按钮是否启用。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';const minimizable = await getCurrentWindow().isMinimizable();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L724
isMinimized()
isMinimized(): Promise<boolean>
获取窗口当前最小化状态。
返回值
Promise
<boolean
>
示例
import { getCurrentWindow } from '@tauri-apps/api/window';const minimized = await getCurrentWindow().isMinimized();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L618
isResizable()
isResizable(): Promise<boolean>
获取窗口当前可调整大小的状态。
返回值
Promise
<boolean
>
窗口是否可调整大小。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';const resizable = await getCurrentWindow().isResizable();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L682
isVisible()
isVisible(): Promise<boolean>
获取窗口当前可见状态。
返回值
Promise
<boolean
>
窗口是否可见。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';const visible = await getCurrentWindow().isVisible();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L761
listen()
listen<T>(event, handler): Promise<UnlistenFn>
监听此 webview 窗口上发出的事件。
类型参数
类型参数 |
---|
T |
参数
参数 | 类型 | 描述 |
---|---|---|
event | EventName | 事件名称。必须仅包含字母数字字符、- 、/ 、: 和 _ 。 |
handler | EventCallback <T > | 事件处理程序。 |
返回值
一个 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 unmountedunlisten();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webviewWindow.ts#L155
maximize()
maximize(): Promise<void>
最大化窗口。
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().maximize();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1009
minimize()
minimize(): Promise<void>
最小化窗口。
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().minimize();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1057
once()
once<T>(event, handler): Promise<UnlistenFn>
仅监听此 webview 窗口上发出的一次事件。
类型参数
类型参数 |
---|
T |
参数
参数 | 类型 | 描述 |
---|---|---|
event | EventName | 事件名称。必须仅包含字母数字字符、- 、/ 、: 和 _ 。 |
handler | EventCallback <T > | 事件处理程序。 |
返回值
一个 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 unmountedunlisten();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webviewWindow.ts#L190
onCloseRequested()
onCloseRequested(handler): Promise<UnlistenFn>
监听窗口关闭请求。当用户请求关闭窗口时触发。
参数
参数 | 类型 |
---|---|
handler | (event ) => void | Promise <void > |
返回值
一个 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 unmountedunlisten();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1824
onDragDropEvent()
onDragDropEvent(handler): Promise<UnlistenFn>
监听文件拖放事件。当用户在 webview 上悬停选定的文件、拖放文件或取消操作时,将触发侦听器。
参数
参数 | 类型 |
---|---|
handler | EventCallback <DragDropEvent > |
返回值
一个 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 unmountedunlisten();
当调试器面板打开时,由于已知限制,此事件的拖放位置可能不准确。要获取正确的拖放位置,请分离调试器。
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L593
onFocusChanged()
onFocusChanged(handler): Promise<UnlistenFn>
监听窗口焦点变化。
参数
参数 | 类型 |
---|---|
handler | EventCallback <boolean > |
返回值
一个 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 unmountedunlisten();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1940
onMoved()
onMoved(handler): Promise<UnlistenFn>
监听窗口移动。
参数
参数 | 类型 |
---|---|
handler | EventCallback <PhysicalPosition > |
返回值
一个 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 unmountedunlisten();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1795
onResized()
onResized(handler): Promise<UnlistenFn>
监听窗口大小调整。
参数
参数 | 类型 |
---|---|
handler | EventCallback <PhysicalSize > |
返回值
一个 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 unmountedunlisten();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1771
onScaleChanged()
onScaleChanged(handler): Promise<UnlistenFn>
监听窗口缩放变化。当窗口的缩放因子发生变化时触发。以下用户操作可能导致 DPI 变化
- 更改显示器的分辨率。
- 更改显示器的缩放因子(例如,在 Windows 上的控制面板中)。
- 将窗口移动到具有不同缩放因子的显示器。
参数
参数 | 类型 |
---|---|
handler | EventCallback <ScaleFactorChanged > |
返回值
一个 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 unmountedunlisten();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1980
onThemeChanged()
onThemeChanged(handler): Promise<UnlistenFn>
监听系统主题变化。
参数
参数 | 类型 |
---|---|
handler | EventCallback <Theme > |
返回值
一个 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 unmountedunlisten();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2006
outerPosition()
outerPosition(): Promise<PhysicalPosition>
窗口左上角相对于桌面左上角的位置。
返回值
窗口的外部位置。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';const position = await getCurrentWindow().outerPosition();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L548
outerSize()
outerSize(): Promise<PhysicalSize>
整个窗口的物理尺寸。这些尺寸包括标题栏和边框。如果您不需要这些(通常情况下不需要),请使用 inner_size。
返回值
窗口的外部尺寸。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';const size = await getCurrentWindow().outerSize();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L585
position()
position(): Promise<PhysicalPosition>
webview 客户端区域的左上角相对于桌面左上角的位置。
返回值
webview 的位置。
示例
import { getCurrentWebview } from '@tauri-apps/api/webview';const position = await getCurrentWebview().position();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L367
reparent()
reparent(window): Promise<void>
将此 webview 移动到给定的标签。
参数
参数 | 类型 |
---|---|
window | string | Window | WebviewWindow |
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWebview } from '@tauri-apps/api/webview';await getCurrentWebview().reparent('other-window');
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L524
requestUserAttention()
requestUserAttention(requestType): Promise<void>
请求用户注意窗口,如果应用程序已获得焦点,则此操作无效。请求用户注意的表现形式取决于平台,有关详细信息,请参阅 UserAttentionType。
提供 null
将取消用户注意请求。当窗口接收到输入时,WM 可能不会自动取消用户注意请求。
平台特定
- macOS:
null
无效。 - Linux: 紧急级别具有相同的效果。
参数
参数 | 类型 |
---|---|
requestType | null | UserAttentionType |
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().requestUserAttention();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L840
scaleFactor()
scaleFactor(): Promise<number>
可用于将物理像素映射到逻辑像素的缩放因子。
返回值
Promise
<number
>
窗口的显示器缩放因子。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';const factor = await getCurrentWindow().scaleFactor();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L516
setAlwaysOnBottom()
setAlwaysOnBottom(alwaysOnBottom): Promise<void>
窗口是否应始终位于其他窗口下方。
参数
参数 | 类型 | 描述 |
---|---|---|
alwaysOnBottom | boolean | 窗口是否应始终位于其他窗口下方。 |
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().setAlwaysOnBottom(true);
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1238
setAlwaysOnTop()
setAlwaysOnTop(alwaysOnTop): Promise<void>
窗口是否应始终位于其他窗口之上。
参数
参数 | 类型 | 描述 |
---|---|---|
alwaysOnTop | boolean | 窗口是否应始终位于其他窗口之上。 |
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().setAlwaysOnTop(true);
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1220
setBackgroundColor()
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
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webviewWindow.ts#L222
setBadgeCount()
setBadgeCount(count?): Promise<void>
设置徽章计数。它是应用程序范围的,而不是特定于此窗口的。
平台特定
- Windows: 不支持。请改用 @{linkcode Window.setOverlayIcon}。
参数
参数 | 类型 | 描述 |
---|---|---|
count ? | number | 徽章计数。使用 undefined 删除徽章。 |
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().setBadgeCount(5);
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1624
setBadgeLabel()
setBadgeLabel(label?): Promise<void>
设置徽章内容,仅限 macOS。
参数
参数 | 类型 | 描述 |
---|---|---|
label ? | string | 徽章标签。使用 undefined 删除徽章。 |
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().setBadgeLabel("Hello");
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1643
setClosable()
setClosable(closable): Promise<void>
设置是否启用窗口的原生关闭按钮。
平台特定
- Linux: GTK+ 将尽力说服窗口管理器不显示关闭按钮。根据系统不同,在已可见的窗口上调用此函数可能没有任何效果
- iOS / Android: 不支持。
参数
参数 | 类型 |
---|---|
closable | boolean |
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().setClosable(false);
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L974
setContentProtected()
setContentProtected(protected_): Promise<void>
阻止窗口内容被其他应用程序捕获。
参数
参数 | 类型 |
---|---|
protected_ | boolean |
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().setContentProtected(true);
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1255
setCursorGrab()
setCursorGrab(grab): Promise<void>
抓取光标,防止其离开窗口。
不保证光标会被隐藏。如果需要,您应该自行隐藏它。
平台特定
- Linux: 不支持。
- macOS: 这会将光标锁定在固定位置,这在视觉上看起来很别扭。
参数
参数 | 类型 | 描述 |
---|---|---|
grab | boolean | true 抓取光标图标, false 释放光标图标。 |
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().setCursorGrab(true);
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1471
setCursorIcon()
setCursorIcon(icon): Promise<void>
修改窗口的光标图标。
参数
参数 | 类型 | 描述 |
---|---|---|
icon | CursorIcon | 新的光标图标。 |
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().setCursorIcon('help');
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1513
setCursorPosition()
setCursorPosition(position): Promise<void>
更改窗口坐标中光标的位置。
参数
参数 | 类型 | 描述 |
---|---|---|
position | LogicalPosition | PhysicalPosition | Position | 新的光标位置。 |
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow, LogicalPosition } from '@tauri-apps/api/window';await getCurrentWindow().setCursorPosition(new LogicalPosition(600, 300));
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1547
setCursorVisible()
setCursorVisible(visible): Promise<void>
修改光标的可见性。
平台特定
- Windows: 光标仅在窗口范围内隐藏。
- macOS: 只要窗口具有输入焦点,即使光标在窗口外部,光标也会被隐藏。
参数
参数 | 类型 | 描述 |
---|---|---|
visible | boolean | 如果 false ,这将隐藏光标。如果 true ,这将显示光标。 |
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().setCursorVisible(false);
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1495
setDecorations()
setDecorations(decorations): Promise<void>
窗口是否应具有边框和标题栏。
参数
参数 | 类型 | 描述 |
---|---|---|
decorations | boolean | 窗口是否应具有边框和标题栏。 |
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().setDecorations(false);
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1156
setEffects()
setEffects(effects): Promise<void>
设置窗口效果。
参数
参数 | 类型 |
---|---|
effects | Effects |
返回值
Promise
<void
>
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1192
setEnabled()
setEnabled(enabled): Promise<void>
启用或禁用窗口。
参数
参数 | 类型 |
---|---|
enabled | boolean |
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().setEnabled(false);
始于
2.0.0
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L887
setFocus()
setFocus(): Promise<void>
将 webview 带到前台并聚焦。
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWebview } from '@tauri-apps/api/webview';await getCurrentWebview().setFocus();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L459
setFullscreen()
setFullscreen(fullscreen): Promise<void>
设置窗口全屏状态。
参数
参数 | 类型 | 描述 |
---|---|---|
fullscreen | boolean | 窗口是否应进入全屏模式。 |
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().setFullscreen(true);
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1380
setIcon()
setIcon(icon): Promise<void>
设置窗口图标。
参数
参数 | 类型 | 描述 |
---|---|---|
icon | | string | number [] | ArrayBuffer | Uint8Array <ArrayBufferLike > | Image | 图标字节或图标文件路径。 |
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().setIcon('/tauri/awesome.png');
请注意,您可能需要 image-ico
或 image-png
Cargo 功能才能使用此 API。要启用它,请更改您的 Cargo.toml 文件
[dependencies]tauri = { version = "...", features = ["...", "image-png"] }
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1421
setIgnoreCursorEvents()
setIgnoreCursorEvents(ignore): Promise<void>
更改光标事件行为。
参数
参数 | 类型 | 描述 |
---|---|---|
ignore | boolean | true 忽略光标事件; false 像往常一样处理它们。 |
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().setIgnoreCursorEvents(true);
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1568
setMaximizable()
setMaximizable(maximizable): Promise<void>
设置是否启用窗口的原生最大化按钮。如果 resizable 设置为 false,则此设置将被忽略。
平台特定
- macOS: 禁用窗口标题栏中的“缩放”按钮,该按钮也用于进入全屏模式。
- Linux / iOS / Android: 不支持。
参数
参数 | 类型 |
---|---|
maximizable | boolean |
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().setMaximizable(false);
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L929
setMaxSize()
setMaxSize(size): Promise<void>
设置窗口最大内部尺寸。如果未提供 size
参数,则取消约束。
参数
参数 | 类型 | 描述 |
---|---|---|
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));
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1311
setMinimizable()
setMinimizable(minimizable): Promise<void>
设置是否启用窗口的原生最小化按钮。
平台特定
- Linux / iOS / Android: 不支持。
参数
参数 | 类型 |
---|---|
minimizable | boolean |
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().setMinimizable(false);
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L951
setMinSize()
setMinSize(size): Promise<void>
设置窗口最小内部尺寸。如果未提供 size
参数,则取消约束。
参数
参数 | 类型 | 描述 |
---|---|---|
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));
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1291
setOverlayIcon()
setOverlayIcon(icon?): Promise<void>
设置覆盖图标。仅限 Windows 覆盖图标可以为每个窗口设置。
请注意,您可能需要 image-ico
或 image-png
Cargo 功能才能使用此 API。要启用它,请更改您的 Cargo.toml 文件
[dependencies]tauri = { version = "...", features = ["...", "image-png"] }
参数
参数 | 类型 | 描述 |
---|---|---|
icon ? | | string | number [] | ArrayBuffer | Uint8Array <ArrayBufferLike > | Image | 图标字节或图标文件路径。使用 undefined 删除覆盖图标。 |
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().setOverlayIcon("/tauri/awesome.png");
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1672
setPosition()
setPosition(position): Promise<void>
设置 webview 位置。
参数
参数 | 类型 | 描述 |
---|---|---|
position | LogicalPosition | PhysicalPosition | Position | 新位置,以逻辑或物理像素为单位。 |
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrent, LogicalPosition } from '@tauri-apps/api/webview';await getCurrentWebview().setPosition(new LogicalPosition(600, 500));
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L440
setProgressBar()
setProgressBar(state): Promise<void>
设置任务栏进度状态。
平台特定
- Linux / macOS: 进度条是应用程序范围的,而不是特定于此窗口的。
- Linux: 仅支持具有
libunity
的桌面环境(例如 GNOME)。
参数
参数 | 类型 |
---|---|
state | ProgressBarState |
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow, ProgressBarStatus } from '@tauri-apps/api/window';await getCurrentWindow().setProgressBar({ status: ProgressBarStatus.Normal, progress: 50,});
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1700
setResizable()
setResizable(resizable): Promise<void>
更新窗口可调整大小标志。
参数
参数 | 类型 |
---|---|
resizable | boolean |
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().setResizable(false);
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L868
setShadow()
setShadow(enable): Promise<void>
窗口是否应具有阴影。
平台特定
- Windows
false
对带边框的窗口无效,阴影始终为开启状态。true
将使无边框窗口具有 1px 白色边框,并且在 Windows 11 上,它将具有圆角。
- Linux: 不支持。
参数
参数 | 类型 |
---|---|
enable | boolean |
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().setShadow(false);
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1182
setSize()
setSize(size): Promise<void>
调整 webview 大小。
参数
参数 | 类型 | 描述 |
---|---|---|
size | LogicalSize | PhysicalSize | Size | 逻辑或物理尺寸。 |
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrent, LogicalSize } from '@tauri-apps/api/webview';await getCurrentWebview().setSize(new LogicalSize(600, 500));
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L422
setSizeConstraints()
setSizeConstraints(constraints): Promise<void>
设置窗口内部尺寸约束。
参数
参数 | 类型 | 描述 |
---|---|---|
constraints | undefined | null | WindowSizeConstraints | 逻辑或物理内部尺寸,或 null 取消约束。 |
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().setSizeConstraints({ minWidth: 300 });
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1331
setSkipTaskbar()
setSkipTaskbar(skip): Promise<void>
窗口图标是否应从任务栏中隐藏。
平台特定
- macOS: 不支持。
参数
参数 | 类型 | 描述 |
---|---|---|
skip | boolean | true 隐藏窗口图标, false 显示窗口图标。 |
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().setSkipTaskbar(true);
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1445
setTheme()
setTheme(theme?): Promise<void>
设置窗口主题,传入 null
或 undefined
以跟随系统主题
平台特定
- Linux / macOS: 主题是应用程序范围的,而不是特定于此窗口的。
- iOS / Android: 不支持。
参数
参数 | 类型 |
---|---|
theme ? | null | Theme |
返回值
Promise
<void
>
始于
2.0.0
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1745
setTitle()
setTitle(title): Promise<void>
设置窗口标题。
参数
参数 | 类型 | 描述 |
---|---|---|
title | string | 新标题 |
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().setTitle('Tauri');
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L992
setTitleBarStyle()
setTitleBarStyle(style): Promise<void>
设置标题栏样式。仅限 macOS。
参数
参数 | 类型 |
---|---|
style | TitleBarStyle |
返回值
Promise
<void
>
始于
2.0.0
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1728
setVisibleOnAllWorkspaces()
setVisibleOnAllWorkspaces(visible): Promise<void>
设置窗口是否应在所有工作区或虚拟桌面上可见。
平台特定
- Windows / iOS / Android: 不支持。
参数
参数 | 类型 |
---|---|
visible | boolean |
返回值
Promise
<void
>
始于
2.0.0
继承自
Window
.setVisibleOnAllWorkspaces
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1716
setZoom()
setZoom(scaleFactor): Promise<void>
设置 webview 缩放级别。
参数
参数 | 类型 |
---|---|
scaleFactor | number |
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWebview } from '@tauri-apps/api/webview';await getCurrentWebview().setZoom(1.5);
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L507
show()
show(): Promise<void>
显示 webview。
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWebview } from '@tauri-apps/api/webview';await getCurrentWebview().show();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L491
size()
size(): Promise<PhysicalSize>
webview 客户端区域的物理尺寸。客户端区域是 webview 的内容,不包括标题栏和边框。
返回值
webview 的尺寸。
示例
import { getCurrentWebview } from '@tauri-apps/api/webview';const size = await getCurrentWebview().size();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L384
startDragging()
startDragging(): Promise<void>
开始拖动窗口。
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().startDragging();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1585
startResizeDragging()
startResizeDragging(direction): Promise<void>
开始调整窗口大小拖动。
参数
参数 | 类型 |
---|---|
direction | ResizeDirection |
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().startResizeDragging();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1601
theme()
theme(): Promise<null | Theme>
获取窗口的当前主题。
平台特定
- macOS: 主题在 macOS 10.14 中引入。在 macOS 10.13 及更低版本上返回
light
。
返回值
窗口主题。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';const theme = await getCurrentWindow().theme();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L796
title()
title(): Promise<string>
获取窗口的当前标题。
返回值
Promise
<string
>
示例
import { getCurrentWindow } from '@tauri-apps/api/window';const title = await getCurrentWindow().title();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L775
toggleMaximize()
toggleMaximize(): Promise<void>
切换窗口最大化状态。
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().toggleMaximize();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1041
unmaximize()
unmaximize(): Promise<void>
取消窗口最大化。
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().unmaximize();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1025
unminimize()
unminimize(): Promise<void>
取消窗口最小化。
返回值
Promise
<void
>
表示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().unminimize();
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1073
getAll()
static getAll(): Promise<WebviewWindow[]>
获取所有可用 webview 的 Webview
实例列表。
返回值
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webviewWindow.ts#L132
getByLabel()
static getByLabel(label): Promise<null | WebviewWindow>
获取与给定标签关联的 webview 的 Webview
。
参数
参数 | 类型 | 描述 |
---|---|---|
label | string | webview 标签。 |
返回值
Promise
<null
| WebviewWindow
>
用于与 webview 通信的 Webview
实例,如果 webview 不存在,则为 null。
示例
import { Webview } from '@tauri-apps/api/webviewWindow';const mainWebview = Webview.getByLabel('main');
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webviewWindow.ts#L112
getCurrent()
static getCurrent(): WebviewWindow
获取当前 webview 的 Webview
实例。
返回值
继承自
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webviewWindow.ts#L125
函数
getAllWebviewWindows()
function getAllWebviewWindows(): Promise<WebviewWindow[]>
获取所有可用 webview 窗口的 Webview
实例列表。
返回值
始于
2.0.0
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webviewWindow.ts#L34
getCurrentWebviewWindow()
function getCurrentWebviewWindow(): WebviewWindow
获取当前 webview 窗口的 Webview
实例。
返回值
始于
2.0.0
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webviewWindow.ts#L23
© 2025 Tauri 贡献者。 CC-BY / MIT