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
ProgressBarStatus
枚舉成員
錯誤
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
UserAttentionType
在視窗上請求注意的類型。
自
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 | 目標視窗/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();
來源: 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();
來源: 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();
來源: 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();
來源: 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();
來源: 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();
來源: 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();
來源: 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();
來源: 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();
來源: 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();
來源: 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);
來源: 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);
來源: 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
來源: 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);
來源: 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");
來源: 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);
來源: 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);
來源: 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);
來源: 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');
來源: 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));
來源: 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);
來源: 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);
來源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1156
setEffects()
setEffects(effects): Promise<void>
設定視窗效果。
參數
參數 | 型別 |
---|---|
effects | 效果 |
回傳
Promise
<void
>
來源: 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
來源: 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();
來源: 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);
來源: 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"] }
來源: 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);
來源: 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);
來源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L929
setMaxSize()
setMaxSize(size): Promise<void>
設定視窗最大內部尺寸。如果 size
參數為 undefined,則取消設定約束。
參數
參數 | 型別 | 描述 |
---|---|---|
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));
來源: 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);
來源: 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));
來源: 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");
來源: 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));
來源: 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,});
來源: 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);
來源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L868
setShadow()
setShadow(enable): Promise<void>
視窗是否應具有陰影。
平台特定
- Windows
false
對於有裝飾的視窗沒有任何效果,陰影始終為開啟狀態。true
將使未裝飾的視窗具有 1 像素的白色邊框,並且在 Windows 11 上,它將具有圓角。
- Linux: 不支援。
參數
參數 | 型別 |
---|---|
enable | boolean |
回傳
Promise
<void
>
指示操作成功或失敗的 Promise。
範例
import { getCurrentWindow } from '@tauri-apps/api/window';await getCurrentWindow().setShadow(false);
來源: 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));
來源: 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 });
來源: 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);
來源: 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
來源: 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');
來源: 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
來源: 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
來源: 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();
來源: 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();
來源: 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();
來源: 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();
來源: 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();
來源: 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();
來源: 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();
來源: 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();
來源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L1073
getAll()
static getAll(): Promise<Window[]>
取得所有可用視窗的 Window
實例列表。
回傳
來源: 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');
來源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L326
getCurrent()
static getCurrent(): Window
取得目前視窗的 Window
實例。
回傳
來源: 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();
來源: 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 上沒有任何效果。 | 來源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2205 |
effects | 效果 [] | 要應用於視窗的視窗效果列表。衝突的效果將應用第一個效果並忽略其餘效果。 | 來源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2192 |
radius? | number | 視窗效果圓角半徑,僅限 macOS | 來源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2200 |
state? | 效果狀態 | 視窗效果狀態,僅限 macOS | 來源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L2196 |
螢幕
允許您檢索有關給定螢幕的資訊。
自
1.0.0
屬性
屬性 | 型別 | 描述 | 定義於 |
---|---|---|---|
name | null | string | 螢幕的人類可讀名稱 | 來源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L49 |
position | PhysicalPosition | 螢幕左上角相對於較大全螢幕區域的位置。 | 來源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L53 |
scaleFactor | number | 可用於將物理像素映射到邏輯像素的縮放比例因子。 | 來源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L55 |
size | PhysicalSize | 螢幕的解析度。 | 來源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L51 |
ProgressBarState
屬性
屬性 | 型別 | 描述 | 定義於 |
---|---|---|---|
progress? | number | 進度列進度。這可以是從 0 到 100 的值 | 來源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L203 |
status? | ProgressBarStatus | 進度列狀態。 | 來源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L199 |
ScaleFactorChanged
用於 scaleChange
事件的 payload。
自
1.0.2
屬性
屬性 | 型別 | 描述 | 定義於 |
---|---|---|---|
scaleFactor | number | 新的視窗縮放比例因子。 | 來源: https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/window.ts#L78 |
size | PhysicalSize | 新的視窗尺寸 | 來源: 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
主題
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