window
提供 API 来创建窗口、与其他窗口通信和操作当前窗口。
窗口事件
可以使用 Window.listen 监听事件
import { getCurrentWindow } from "@tauri-apps/api/window";getCurrentWindow().listen("my-window-event", ({ event, payload }) => { });
参考
颜色
重新导出 Color
DragDropEvent
重新导出 DragDropEvent
LogicalPosition
重新导出 LogicalPosition
LogicalSize
重新导出 LogicalSize
PhysicalPosition
重新导出 PhysicalPosition
PhysicalSize
重新导出 PhysicalSize
枚举
BackgroundThrottlingPolicy
后台节流策略
自从
2.0.0
枚举成员
禁用
Disabled: "disabled";
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2030
暂停
Suspend: "suspend";
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2032
节流
Throttle: "throttle";
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2031
效果
平台特定的窗口效果
自从
2.0.0
枚举成员
亚克力
Acrylic: "acrylic";
Windows 10/11
注意
在 Windows 10 v1903+ 和 Windows 11 build 22000 上调整大小/拖动窗口时,此效果性能不佳。
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2146
基于外观
AppearanceBased: "appearanceBased";
适用于视图有效外观的默认材质。macOS 10.14-
已弃用
自 macOS 10.14 起。您应该改为选择合适的语义材质。
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2046
模糊
Blur: "blur";
Windows 7/10/11(22H1) 仅限
注意
在 Windows 11 build 22621 上调整大小/拖动窗口时,此效果性能不佳。
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2138
内容背景
ContentBackground: "contentBackground";
macOS 10.14+
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2118
深色
Dark: "dark";
macOS 10.14-
已弃用
自 macOS 10.14 起。请改用语义材质。
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2058
全屏 UI
FullScreenUI: "fullScreenUI";
macOS 10.14+
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2110
标题栏视图
HeaderView: "headerView";
macOS 10.14+
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2094
Hud 窗口
HudWindow: "hudWindow";
macOS 10.14+
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2106
浅色
Light: "light";
macOS 10.14-
已弃用
自 macOS 10.14 起。请改用语义材质。
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2052
中等浅色
MediumLight: "mediumLight";
macOS 10.14-
已弃用
自 macOS 10.14 起。请改用语义材质。
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2064
菜单
Menu: "menu";
macOS 10.11+
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2082
云母
Mica: "mica";
仅限 Windows 11
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2130
弹出窗口
Popover: "popover";
macOS 10.11+
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2086
选择
Selection: "selection";
macOS 10.10+
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2078
工作表
Sheet: "sheet";
macOS 10.14+
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2098
侧边栏
Sidebar: "sidebar";
macOS 10.11+
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2090
选项卡式
Tabbed: "tabbed";
与系统深色偏好匹配的选项卡式效果 仅限 Windows 11
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2150
选项卡式深色
TabbedDark: "tabbedDark";
带有深色模式的选项卡式效果,但仅当系统上启用深色模式时 仅限 Windows 11
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2154
选项卡式浅色
TabbedLight: "tabbedLight";
带有浅色模式的选项卡式效果 仅限 Windows 11
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2158
标题栏
Titlebar: "titlebar";
macOS 10.10+
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2074
工具提示
Tooltip: "tooltip";
macOS 10.14+
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2114
超深色
UltraDark: "ultraDark";
macOS 10.14-
已弃用
自 macOS 10.14 起。请改用语义材质。
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2070
页面背景下
UnderPageBackground: "underPageBackground";
macOS 10.14+
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2126
窗口背景下
UnderWindowBackground: "underWindowBackground";
macOS 10.14+
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2122
窗口背景
WindowBackground: "windowBackground";
macOS 10.14+
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2102
效果状态
窗口效果状态 仅限 macOS
参见
https://developer.apple.com/documentation/appkit/nsvisualeffectview/state
自从
2.0.0
枚举成员
活动
Active: "active";
使窗口效果状态始终处于活动状态 仅限 macOS
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2176
跟随窗口活动状态
FollowsWindowActiveState: "followsWindowActiveState";
使窗口效果状态跟随窗口的活动状态 仅限 macOS
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2172
非活动
Inactive: "inactive";
使窗口效果状态始终处于非活动状态 仅限 macOS
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2180
进度条状态
枚举成员
错误
Error: "error";
错误状态。在 Linux 上视为正常
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L185
不确定
Indeterminate: "indeterminate";
不确定状态。在 Linux 和 macOS 上视为正常
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L177
无
None: "none";
隐藏进度条。
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L169
正常
Normal: "normal";
正常状态。
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L173
已暂停
Paused: "paused";
暂停状态。在 Linux 上视为正常
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L181
用户注意类型
要在窗口上请求的注意类型。
自从
1.0.0
枚举成员
紧急
Critical: 1;
平台特定
- macOS: 弹跳 Dock 图标,直到应用程序处于焦点状态。
- Windows: 闪烁窗口和任务栏按钮,直到应用程序处于焦点状态。
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L94
信息性
Informational: 2;
平台特定
- macOS: 弹跳 Dock 图标一次。
- Windows: 闪烁任务栏按钮,直到应用程序处于焦点状态。
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L100
类
CloseRequestedEvent
构造函数
new CloseRequestedEvent()
new CloseRequestedEvent(event): CloseRequestedEvent
参数
参数 | 类型 |
---|---|
event | Event <unknown > |
返回
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L110
属性
属性 | 类型 | 描述 | 定义在 |
---|---|---|---|
event | 事件名称 | 事件名称 | 来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L105 |
id | number | 用于取消监听的事件标识符 | 来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L107 |
方法
isPreventDefault()
isPreventDefault(): boolean
返回
boolean
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L119
preventDefault()
preventDefault(): void
返回
void
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L115
Window
创建新窗口或获取现有窗口的句柄。
窗口通过标签标识,这是一个唯一的标识符,可用于稍后引用它。它只能包含字母数字字符 a-zA-Z
以及以下特殊字符 -
、 /
、 :
和 _
。
示例
import { Window } from "@tauri-apps/api/window"
const appWindow = new Window('theUniqueLabel');
appWindow.once('tauri://created', function () { // window successfully created});appWindow.once('tauri://error', function (e) { // an error happened creating the window});
// emit an event to the backendawait appWindow.emit("some-event", "data");// listen to an event from the backendconst unlisten = await appWindow.listen("event-name", e => {});unlisten();
自从
2.0.0
扩展自
构造函数
new Window()
new Window(label, options): Window
创建一个新窗口。
参数
参数 | 类型 | 描述 |
---|---|---|
label | string | 唯一的窗口标签。必须是字母数字:a-zA-Z-/:_ 。 |
options | WindowOptions | - |
返回
要与之通信的 Window 实例。
示例
import { Window } from '@tauri-apps/api/window';const appWindow = new Window('my-label');appWindow.once('tauri://created', function () { // window successfully created});appWindow.once('tauri://error', function (e) { // an error happened creating the window});
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L293
属性
属性 | 类型 | 描述 | 定义在 |
---|---|---|---|
label | string | 窗口标签。它是窗口的唯一标识符,可用于稍后引用它。 | 来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L271 |
listeners | Record <string , EventCallback <any >[]> | 本地事件监听器。 | 来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L274 |
方法
center()
center(): Promise<void>
将窗口居中。
返回
Promise
<void
>
指示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().center();
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L814
clearEffects()
clearEffects(): Promise<void>
如果可能,清除任何应用的效果。
返回
Promise
<void
>
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1202
close()
close(): Promise<void>
关闭窗口。
请注意,这将发出 closeRequested 事件,以便您可以拦截它。要强制关闭窗口,请使用 Window.destroy。
返回
Promise
<void
>
指示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().close();
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1123
destroy()
destroy(): Promise<void>
销毁窗口。行为类似于 Window.close,但强制关闭窗口而不是发出 closeRequested 事件。
返回
Promise
<void
>
指示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().destroy();
来源: 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 { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().emit('window-loaded', { loggedIn: true, token: 'authToken' });
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L444
emitTo()
emitTo( target, event,payload?): Promise<void>
向与给定目标匹配的所有 目标 发送事件。
参数
参数 | 类型 | 描述 |
---|---|---|
target | string | EventTarget | 目标 Window/Webview/WebviewWindow 的标签或原始 EventTarget 对象。 |
event | string | 事件名称。必须仅包含字母数字字符、- 、/ 、: 和 _ 。 |
payload ? | unknown | 事件负载。 |
返回
Promise
<void
>
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().emit('main', 'window-loaded', { loggedIn: true, token: 'authToken' });
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L471
hide()
hide(): Promise<void>
将窗口可见性设置为 false。
返回
Promise
<void
>
指示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().hide();
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1105
innerPosition()
innerPosition(): Promise<PhysicalPosition>
窗口客户端区域的左上角相对于桌面左上角的位置。
返回
窗口的内部位置。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';const position = await getCurrentWindow().innerPosition();
来源: 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();
来源: 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();
来源: 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();
来源: 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
来源: 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();
来源: 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();
来源: 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();
来源: 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();
来源: 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();
来源: 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();
来源: 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();
来源: 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();
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L761
listen()
listen<T>(event, handler): Promise<UnlistenFn>
监听在此窗口上发出的事件。
类型参数
类型参数 |
---|
T |
参数
参数 | 类型 | 描述 |
---|---|---|
event | 事件名称 | 事件名称。必须仅包含字母数字字符、- 、/ 、: 和 _ 。 |
handler | EventCallback <T > | 事件处理程序。 |
返回
一个 Promise,解析为一个取消监听事件的函数。请注意,如果您的监听器超出作用域(例如,组件卸载),则需要删除监听器。
示例
import { getCurrentWindow } from '@tauri-apps/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 unmountedunlisten();
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L382
maximize()
maximize(): Promise<void>
最大化窗口。
返回
Promise
<void
>
指示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().maximize();
来源: 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();
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1057
once()
once<T>(event, handler): Promise<UnlistenFn>
仅监听在此窗口上发出的一次事件。
类型参数
类型参数 |
---|
T |
参数
参数 | 类型 | 描述 |
---|---|---|
event | 事件名称 | 事件名称。必须仅包含字母数字字符、- 、/ 、: 和 _ 。 |
handler | EventCallback <T > | 事件处理程序。 |
返回
一个 Promise,解析为一个取消监听事件的函数。请注意,如果您的监听器超出作用域(例如,组件卸载),则需要删除监听器。
示例
import { getCurrentWindow } from '@tauri-apps/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 unmountedunlisten();
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L417
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();
来源: 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 { getCurrentWindow } from "@tauri-apps/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 unmountedunlisten();
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1862
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
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>
设置窗口背景颜色。
平台特定
- Windows: alpha 通道被忽略。
- iOS / Android: 不支持。
参数
参数 | 类型 |
---|---|
color | 颜色 |
返回
Promise
<void
>
指示操作成功或失败的 Promise。
自从
2.1.0
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1532
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 | 效果 |
返回
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>
将窗口置于前台并聚焦。
返回
Promise
<void
>
指示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().setFocus();
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1397
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>
设置窗口外部位置。
参数
参数 | 类型 | 描述 |
---|---|---|
position | LogicalPosition | PhysicalPosition | Position | 新的位置,以逻辑像素或物理像素为单位。 |
返回
Promise
<void
>
指示操作成功或失败的 Promise。
示例
import { getCurrentWindow, LogicalPosition } from '@tauri-apps/api/window';await getCurrentWindow().setPosition(new LogicalPosition(600, 500));
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1360
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>
使用新的内部尺寸调整窗口大小。
参数
参数 | 类型 | 描述 |
---|---|---|
size | LogicalSize | PhysicalSize | Size | 逻辑或物理内部尺寸。 |
返回
Promise
<void
>
指示操作成功或失败的 Promise。
示例
import { getCurrentWindow, LogicalSize } from '@tauri-apps/api/window';await getCurrentWindow().setSize(new LogicalSize(600, 500));
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1273
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
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1716
show()
show(): Promise<void>
将窗口可见性设置为 true。
返回
Promise
<void
>
指示操作成功或失败的 Promise。
示例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().show();
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1089
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<Window[]>
获取所有可用窗口的 Window
实例列表。
返回
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L340
getByLabel()
static getByLabel(label): Promise<null | Window>
获取与给定标签关联的 Window。
参数
参数 | 类型 | 描述 |
---|---|---|
label | string | 窗口标签。 |
返回
与窗口通信的 Window 实例,如果窗口不存在,则为 null。
示例
import { Window } from '@tauri-apps/api/window';const mainWindow = Window.getByLabel('main');
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L326
getCurrent()
static getCurrent(): Window
获取当前窗口的 Window
实例。
返回
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L333
getFocusedWindow()
static getFocusedWindow(): Promise<null | Window>
获取焦点窗口。
返回
Window 实例,如果没有焦点窗口,则为 undefined
。
示例
import { Window } from '@tauri-apps/api/window';const focusedWindow = Window.getFocusedWindow();
Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L354
接口
效果
窗口效果配置对象
自从
2.0.0
属性
属性 | 类型 | 描述 | 定义在 |
---|---|---|---|
color? | 颜色 | 窗口效果颜色。仅在 Windows 10 v1903+ 上影响 Effect.Blur 和 Effect.Acrylic。在 Windows 7 或 Windows 11 上没有任何效果。 | Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2205 |
effects | 效果 [] | 要应用于窗口的窗口效果列表。冲突的效果将应用第一个,并忽略其余效果。 | Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2192 |
radius? | number | 窗口效果圆角半径 仅限 macOS | Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2200 |
state? | 效果状态 | 窗口效果状态 仅限 macOS | Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2196 |
监视器
允许您检索有关给定显示器的信息。
自从
1.0.0
属性
属性 | 类型 | 描述 | 定义在 |
---|---|---|---|
name | null | string | 显示器的可读名称 | Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L49 |
position | PhysicalPosition | 显示器左上角相对于较大全屏区域的位置。 | Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L53 |
scaleFactor | number | 可用于将物理像素映射到逻辑像素的缩放因子。 | Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L55 |
size | PhysicalSize | 显示器的分辨率。 | Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L51 |
ProgressBarState
属性
属性 | 类型 | 描述 | 定义在 |
---|---|---|---|
progress? | number | 进度条进度。这可以是 0 到 100 范围内的值 | Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L203 |
status? | 进度条状态 | 进度条状态。 | Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L199 |
ScaleFactorChanged
用于 scaleChange
事件的有效负载。
自从
1.0.2
属性
属性 | 类型 | 描述 | 定义在 |
---|---|---|---|
scaleFactor | number | 新的窗口缩放因子。 | Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L78 |
size | PhysicalSize | 新的窗口大小 | Source: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L80 |
WindowOptions
要创建的窗口的配置。
自从
1.0.0
属性
WindowSizeConstraints
属性
属性 | 类型 | 定义在 |
---|---|---|
maxHeight? | number | 来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L192 |
maxWidth? | number | 来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L191 |
minHeight? | number | 来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L190 |
minWidth? | number | 来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L189 |
类型别名
CursorIcon
type CursorIcon: | "default" | "crosshair" | "hand" | "arrow" | "move" | "text" | "wait" | "help" | "progress" | "notAllowed" | "contextMenu" | "cell" | "verticalText" | "alias" | "copy" | "noDrop" | "grab" | "grabbing" | "allScroll" | "zoomIn" | "zoomOut" | "eResize" | "nResize" | "neResize" | "nwResize" | "sResize" | "seResize" | "swResize" | "wResize" | "ewResize" | "nsResize" | "neswResize" | "nwseResize" | "colResize" | "rowResize";
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L124
Theme
type Theme: "light" | "dark";
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L58
TitleBarStyle
type TitleBarStyle: "visible" | "transparent" | "overlay";
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L59
函数
availableMonitors()
function availableMonitors(): Promise<Monitor[]>
返回系统上所有可用显示器的列表。
返回
示例
import { availableMonitors } from '@tauri-apps/api/window';const monitors = availableMonitors();
自从
1.0.0
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2440
currentMonitor()
function currentMonitor(): Promise<Monitor | null>
返回窗口当前所在的显示器。如果无法检测到当前显示器,则返回 null
。
返回
示例
import { currentMonitor } from '@tauri-apps/api/window';const monitor = currentMonitor();
自从
1.0.0
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2390
cursorPosition()
function cursorPosition(): Promise<PhysicalPosition>
获取光标相对于桌面左上角的位置。
请注意,桌面的左上角不一定与屏幕的左上角相同。如果用户使用多显示器桌面,则桌面的左上角在 Windows 和 macOS 上是主显示器的左上角,或者在 X11 上是最左侧显示器的左上角。
如果窗口的左上角在可见屏幕区域之外,则坐标可以是负数。
返回
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2456
getAllWindows()
function getAllWindows(): Promise<Window[]>
获取所有可用窗口的 Window
实例列表。
返回
自从
1.0.0
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L223
getCurrentWindow()
function getCurrentWindow(): Window
获取当前窗口的 Window
实例。
返回
自从
1.0.0
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L211
monitorFromPoint()
function monitorFromPoint(x, y): Promise<Monitor | null>
返回包含给定点的显示器。如果找不到任何显示器,则返回 null
。
参数
参数 | 类型 |
---|---|
x | number |
y | number |
返回
示例
import { monitorFromPoint } from '@tauri-apps/api/window';const monitor = monitorFromPoint();
自从
1.0.0
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2423
primaryMonitor()
function primaryMonitor(): Promise<Monitor | null>
返回系统的主显示器。如果无法识别任何显示器作为主显示器,则返回 null
。
返回
示例
import { primaryMonitor } from '@tauri-apps/api/window';const monitor = primaryMonitor();
自从
1.0.0
来源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2407
© 2025 Tauri 贡献者。CC-BY / MIT