跳到內容
Tauri

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 backend
await webview.emit("some-event", "data");
// listen to an event from the backend
const unlisten = await webview.listen("event-name", e => {});
unlisten();

始於

2.0.0

繼承自

建構函式

new WebviewWindow()
new WebviewWindow(label, options): WebviewWindow

建立新的 Window 以託管 Webview

參數
參數類型描述
label字串唯一的 webview 標籤。必須是字母數字:a-zA-Z-/:_
optionsOmit<WebviewOptions, "width" | "height" | "x" | "y"> & WindowOptions-
回傳

WebviewWindow

用於與視窗和 webview 通訊的 WebviewWindow 實例。

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

Window.constructor

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

屬性

屬性類型描述繼承自定義於
label字串webview 標籤。它是 webview 的唯一識別符,可用於稍後參考它。Window.label來源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webviewWindow.ts#L51
listenersRecord<string, EventCallback<any>[]>本機事件監聽器。Window.listeners來源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webviewWindow.ts#L54
window視窗託管此 webview 的視窗。Webview.window來源https://github.com/tauri-apps/tauri/blob/dev/packages/api/src/webview.ts#L138

方法

center()
center(): Promise<void>

將視窗置中。

回傳

Promise<void>

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

範例
import { getCurrentWindow } from '@tauri-apps/api/window';
await getCurrentWindow().center();
繼承自

Window.center

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

clearAllBrowsingData()
clearAllBrowsingData(): Promise<void>

清除此 webview 的所有瀏覽資料。

回傳

Promise<void>

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

範例
import { getCurrentWebview } from '@tauri-apps/api/webview';
await getCurrentWebview().clearAllBrowsingData();
繼承自

Webview.clearAllBrowsingData

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

clearEffects()
clearEffects(): Promise<void>

盡可能清除任何已套用的效果。

回傳

Promise<void>

繼承自

Window.clearEffects

來源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();
繼承自

Window.close

來源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();
繼承自

Window.destroy

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

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

向所有 目標發出事件。

參數
參數類型描述
event字串事件名稱。必須僅包含字母數字字元、-/:_
payload?unknown事件酬載。
回傳

Promise<void>

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

Window.emit

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

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

向所有符合指定目標的 目標發出事件。

參數
參數類型描述
targetstring | EventTarget目標視窗/Webview/WebviewWindow 的標籤或原始 EventTarget 物件。
event字串事件名稱。必須僅包含字母數字字元、-/:_
payload?unknown事件酬載。
回傳

Promise<void>

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

Window.emitTo

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

hide()
hide(): Promise<void>

隱藏 webview。

回傳

Promise<void>

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

範例
import { getCurrentWebview } from '@tauri-apps/api/webview';
await getCurrentWebview().hide();
繼承自

Window.hide

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

innerPosition()
innerPosition(): Promise<PhysicalPosition>

視窗用戶端區域左上角相對於桌面左上角的位置。

回傳

Promise<PhysicalPosition>

視窗的內部位置。

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

Window.innerPosition

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

innerSize()
innerSize(): Promise<PhysicalSize>

視窗用戶端區域的實際大小。用戶端區域是視窗的內容,不包括標題列和邊框。

回傳

Promise<PhysicalSize>

視窗的內部大小。

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

Window.innerSize

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

isClosable()
isClosable(): Promise<boolean>

取得視窗的原生關閉按鈕狀態。

平台特定

  • iOS / Android: 不支援。
回傳

Promise<boolean>

視窗的原生關閉按鈕是否啟用。

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

Window.isClosable

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

isDecorated()
isDecorated(): Promise<boolean>

取得視窗目前的裝飾狀態。

回傳

Promise<boolean>

視窗是否已裝飾。

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

Window.isDecorated

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

isEnabled()
isEnabled(): Promise<boolean>

視窗是否已啟用或停用。

回傳

Promise<boolean>

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

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

2.0.0

繼承自

Window.isEnabled

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

isFocused()
isFocused(): Promise<boolean>

取得視窗目前的焦點狀態。

回傳

Promise<boolean>

視窗是否已取得焦點。

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

Window.isFocused

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

isFullscreen()
isFullscreen(): Promise<boolean>

取得視窗目前的全螢幕狀態。

回傳

Promise<boolean>

視窗是否處於全螢幕模式。

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

Window.isFullscreen

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

isMaximizable()
isMaximizable(): Promise<boolean>

取得視窗的原生最大化按鈕狀態。

平台特定

  • Linux / iOS / Android: 不支援。
回傳

Promise<boolean>

視窗的原生最大化按鈕是否啟用。

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

Window.isMaximizable

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

isMaximized()
isMaximized(): Promise<boolean>

取得視窗目前的最大化狀態。

回傳

Promise<boolean>

視窗是否已最大化。

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

Window.isMaximized

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

isMinimizable()
isMinimizable(): Promise<boolean>

取得視窗的原生最小化按鈕狀態。

平台特定

  • Linux / iOS / Android: 不支援。
回傳

Promise<boolean>

視窗的原生最小化按鈕是否啟用。

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

Window.isMinimizable

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

isMinimized()
isMinimized(): Promise<boolean>

取得視窗目前的最小化狀態。

回傳

Promise<boolean>

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

Window.isMinimized

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

isResizable()
isResizable(): Promise<boolean>

取得視窗目前的調整大小狀態。

回傳

Promise<boolean>

視窗是否可調整大小。

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

Window.isResizable

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

isVisible()
isVisible(): Promise<boolean>

取得視窗目前的顯示狀態。

回傳

Promise<boolean>

視窗是否可見。

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

Window.isVisible

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

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

監聽此 webview 視窗上發出的事件。

類型參數
類型參數
T
參數
參數類型描述
eventEventName事件名稱。必須僅包含字母數字字元、-/:_
handlerEventCallback<T>事件處理常式。
回傳

Promise<UnlistenFn>

解析為取消監聽事件函式的 Promise。請注意,如果您的監聽器超出範圍(例如,元件已卸載),則需要移除監聽器。

範例
import { WebviewWindow } from '@tauri-apps/api/webviewWindow';
const unlisten = await WebviewWindow.getCurrent().listen<string>('state-changed', (event) => {
console.log(`Got error: ${payload}`);
});
// you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
unlisten();
繼承自

Window.listen

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

maximize()
maximize(): Promise<void>

最大化視窗。

回傳

Promise<void>

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

範例
import { getCurrentWindow } from '@tauri-apps/api/window';
await getCurrentWindow().maximize();
繼承自

Window.maximize

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

minimize()
minimize(): Promise<void>

最小化視窗。

回傳

Promise<void>

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

範例
import { getCurrentWindow } from '@tauri-apps/api/window';
await getCurrentWindow().minimize();
繼承自

Window.minimize

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

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

僅監聽此 webview 視窗上發出的一次事件。

類型參數
類型參數
T
參數
參數類型描述
eventEventName事件名稱。必須僅包含字母數字字元、-/:_
handlerEventCallback<T>事件處理常式。
回傳

Promise<UnlistenFn>

解析為取消監聽事件函式的 Promise。請注意,如果您的監聽器超出範圍(例如,元件已卸載),則需要移除監聽器。

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

Window.once

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

onCloseRequested()
onCloseRequested(handler): Promise<UnlistenFn>

監聽視窗關閉請求。當使用者請求關閉視窗時發出。

參數
參數類型
handler(event) => void | Promise<void>
回傳

Promise<UnlistenFn>

解析為取消監聽事件函式的 Promise。請注意,如果您的監聽器超出範圍(例如,元件已卸載),則需要移除監聽器。

範例
import { getCurrentWindow } from "@tauri-apps/api/window";
import { confirm } from '@tauri-apps/api/dialog';
const unlisten = await getCurrentWindow().onCloseRequested(async (event) => {
const confirmed = await confirm('Are you sure?');
if (!confirmed) {
// user did not confirm closing the window; let's prevent it
event.preventDefault();
}
});
// you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
unlisten();
繼承自

Window.onCloseRequested

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

onDragDropEvent()
onDragDropEvent(handler): Promise<UnlistenFn>

監聽檔案拖放事件。當使用者將選取的檔案懸停在 webview 上、拖放檔案或取消操作時,會觸發監聽器。

參數
參數類型
handlerEventCallback<DragDropEvent>
回傳

Promise<UnlistenFn>

解析為取消監聽事件函式的 Promise。請注意,如果您的監聽器超出範圍(例如,元件已卸載),則需要移除監聽器。

範例
import { getCurrentWebview } from "@tauri-apps/api/webview";
const unlisten = await getCurrentWebview().onDragDropEvent((event) => {
if (event.payload.type === 'over') {
console.log('User hovering', event.payload.position);
} else if (event.payload.type === 'drop') {
console.log('User dropped', event.payload.paths);
} else {
console.log('File drop cancelled');
}
});
// you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
unlisten();

當偵錯工具面板開啟時,由於已知的限制,此事件的拖放位置可能不準確。若要擷取正確的拖放位置,請分離偵錯工具。

繼承自

Window.onDragDropEvent

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

onFocusChanged()
onFocusChanged(handler): Promise<UnlistenFn>

監聽視窗焦點變更。

參數
參數類型
handlerEventCallback<boolean>
回傳

Promise<UnlistenFn>

解析為取消監聽事件函式的 Promise。請注意,如果您的監聽器超出範圍(例如,元件已卸載),則需要移除監聽器。

範例
import { getCurrentWindow } from "@tauri-apps/api/window";
const unlisten = await getCurrentWindow().onFocusChanged(({ payload: focused }) => {
console.log('Focus changed, window is focused? ' + focused);
});
// you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
unlisten();
繼承自

Window.onFocusChanged

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

onMoved()
onMoved(handler): Promise<UnlistenFn>

監聽視窗移動。

參數
參數類型
handlerEventCallback<PhysicalPosition>
回傳

Promise<UnlistenFn>

解析為取消監聽事件函式的 Promise。請注意,如果您的監聽器超出範圍(例如,元件已卸載),則需要移除監聽器。

範例
import { getCurrentWindow } from "@tauri-apps/api/window";
const unlisten = await getCurrentWindow().onMoved(({ payload: position }) => {
console.log('Window moved', position);
});
// you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
unlisten();
繼承自

Window.onMoved

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

onResized()
onResized(handler): Promise<UnlistenFn>

監聽視窗調整大小。

參數
參數類型
handlerEventCallback<PhysicalSize>
回傳

Promise<UnlistenFn>

解析為取消監聽事件函式的 Promise。請注意,如果您的監聽器超出範圍(例如,元件已卸載),則需要移除監聽器。

範例
import { getCurrentWindow } from "@tauri-apps/api/window";
const unlisten = await getCurrentWindow().onResized(({ payload: size }) => {
console.log('Window resized', size);
});
// you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
unlisten();
繼承自

Window.onResized

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

onScaleChanged()
onScaleChanged(handler): Promise<UnlistenFn>

監聽視窗縮放變更。當視窗的縮放比例變更時發出。下列使用者動作可能會導致 DPI 變更

  • 變更顯示器的解析度。
  • 變更顯示器的縮放比例(例如,在 Windows 上的控制台中)。
  • 將視窗移動到具有不同縮放比例的顯示器。
參數
參數類型
handlerEventCallback<ScaleFactorChanged>
回傳

Promise<UnlistenFn>

解析為取消監聽事件函式的 Promise。請注意,如果您的監聽器超出範圍(例如,元件已卸載),則需要移除監聽器。

範例
import { getCurrentWindow } from "@tauri-apps/api/window";
const unlisten = await getCurrentWindow().onScaleChanged(({ payload }) => {
console.log('Scale changed', payload.scaleFactor, payload.size);
});
// you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
unlisten();
繼承自

Window.onScaleChanged

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

onThemeChanged()
onThemeChanged(handler): Promise<UnlistenFn>

監聽系統主題變更。

參數
參數類型
handlerEventCallback<Theme>
回傳

Promise<UnlistenFn>

解析為取消監聽事件函式的 Promise。請注意,如果您的監聽器超出範圍(例如,元件已卸載),則需要移除監聽器。

範例
import { getCurrentWindow } from "@tauri-apps/api/window";
const unlisten = await getCurrentWindow().onThemeChanged(({ payload: theme }) => {
console.log('New theme: ' + theme);
});
// you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
unlisten();
繼承自

Window.onThemeChanged

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

outerPosition()
outerPosition(): Promise<PhysicalPosition>

視窗左上角相對於桌面左上角的位置。

回傳

Promise<PhysicalPosition>

視窗的外部位置。

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

Window.outerPosition

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

outerSize()
outerSize(): Promise<PhysicalSize>

整個視窗的實際大小。這些尺寸包括標題列和邊框。如果您不想要這些(通常您不會想要),請改用 inner_size。

回傳

Promise<PhysicalSize>

視窗的外部大小。

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

Window.outerSize

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

position()
position(): Promise<PhysicalPosition>

webview 用戶端區域左上角相對於桌面左上角的位置。

回傳

Promise<PhysicalPosition>

webview 的位置。

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

Webview.position

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

reparent()
reparent(window): Promise<void>

將此 webview 移動到指定的標籤。

參數
參數類型
windowstring | Window | WebviewWindow
回傳

Promise<void>

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

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

Webview.reparent

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: 緊急程度具有相同的效果。
參數
參數類型
requestTypenull | UserAttentionType
回傳

Promise<void>

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

範例
import { getCurrentWindow } from '@tauri-apps/api/window';
await getCurrentWindow().requestUserAttention();
繼承自

Window.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();
繼承自

Window.scaleFactor

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

setAlwaysOnBottom()
setAlwaysOnBottom(alwaysOnBottom): Promise<void>

視窗是否應始終位於其他視窗下方。

參數
參數類型描述
alwaysOnBottomboolean視窗是否應始終位於其他視窗下方。
回傳

Promise<void>

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

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

Window.setAlwaysOnBottom

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

setAlwaysOnTop()
setAlwaysOnTop(alwaysOnTop): Promise<void>

視窗是否應始終位於其他視窗之上。

參數
參數類型描述
alwaysOnTopboolean視窗是否應始終位於其他視窗之上。
回傳

Promise<void>

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

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

Window.setAlwaysOnTop

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

繼承自

Window.setBackgroundColor

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);
繼承自

Window.setBadgeCount

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

setBadgeLabel()
setBadgeLabel(label?): Promise<void>

設定徽章標籤,僅限 macOS

參數
參數類型描述
label?字串徽章標籤。使用 undefined 以移除徽章。
回傳

Promise<void>

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

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

Window.setBadgeLabel

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

setClosable()
setClosable(closable): Promise<void>

設定是否啟用視窗的原生關閉按鈕。

平台特定

  • Linux: GTK+ 將盡力說服視窗管理器不要顯示關閉按鈕。根據系統的不同,當在已可見的視窗上呼叫此函式時,可能沒有任何效果。
  • iOS / Android: 不支援。
參數
參數類型
closableboolean
回傳

Promise<void>

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

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

Window.setClosable

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);
繼承自

Window.setContentProtected

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

setCursorGrab()
setCursorGrab(grab): Promise<void>

抓取游標,防止其離開視窗。

不保證游標會被隱藏。如果需要,您應該自行隱藏。

平台特定

  • Linux: 不支援。
  • macOS: 這會將游標鎖定在固定位置,視覺上看起來很奇怪。
參數
參數類型描述
grabbooleantrue 以抓取游標圖示,false 以釋放游標圖示。
回傳

Promise<void>

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

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

Window.setCursorGrab

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

setCursorIcon()
setCursorIcon(icon): Promise<void>

修改視窗的游標圖示。

參數
參數類型描述
iconCursorIcon新的游標圖示。
回傳

Promise<void>

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

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

Window.setCursorIcon

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

setCursorPosition()
setCursorPosition(position): Promise<void>

變更游標在視窗座標中的位置。

參數
參數類型描述
positionLogicalPosition | PhysicalPosition | Position新的游標位置。
回傳

Promise<void>

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

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

Window.setCursorPosition

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

setCursorVisible()
setCursorVisible(visible): Promise<void>

修改游標的能見度。

平台特定

  • Windows: 游標僅在視窗範圍內隱藏。
  • macOS: 只要視窗具有輸入焦點,游標就會隱藏,即使游標在視窗外部也是如此。
參數
參數類型描述
visibleboolean如果為 false,這將隱藏游標。如果為 true,這將顯示游標。
回傳

Promise<void>

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

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

Window.setCursorVisible

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

setDecorations()
setDecorations(decorations): Promise<void>

視窗是否應具有邊框和標題列。

參數
參數類型描述
decorationsboolean視窗是否應具有邊框和標題列。
回傳

Promise<void>

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

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

Window.setDecorations

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

setEffects()
setEffects(effects): Promise<void>

設定視窗效果。

參數
參數類型
effectsEffects
回傳

Promise<void>

繼承自

Window.setEffects

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

setEnabled()
setEnabled(enabled): Promise<void>

啟用或停用視窗。

參數
參數類型
enabledboolean
回傳

Promise<void>

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

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

2.0.0

繼承自

Window.setEnabled

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();
繼承自

Window.setFocus

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

setFullscreen()
setFullscreen(fullscreen): Promise<void>

設定視窗全螢幕狀態。

參數
參數類型描述
fullscreenboolean視窗是否應進入全螢幕模式。
回傳

Promise<void>

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

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

Window.setFullscreen

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-icoimage-png Cargo 功能才能使用此 API。若要啟用它,請變更您的 Cargo.toml 檔案

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

Window.setIcon

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

setIgnoreCursorEvents()
setIgnoreCursorEvents(ignore): Promise<void>

變更游標事件行為。

參數
參數類型描述
ignorebooleantrue 以忽略游標事件;false 以照常處理。
回傳

Promise<void>

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

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

Window.setIgnoreCursorEvents

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: 不支援。
參數
參數類型
maximizableboolean
回傳

Promise<void>

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

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

Window.setMaximizable

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));
繼承自

Window.setMaxSize

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

setMinimizable()
setMinimizable(minimizable): Promise<void>

設定是否啟用視窗的原生最小化按鈕。

平台特定

  • Linux / iOS / Android: 不支援。
參數
參數類型
minimizableboolean
回傳

Promise<void>

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

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

Window.setMinimizable

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));
繼承自

Window.setMinSize

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

setOverlayIcon()
setOverlayIcon(icon?): Promise<void>

設定覆蓋圖示。僅限 Windows 每個視窗都可以設定覆蓋圖示。

請注意,您可能需要 image-icoimage-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");
繼承自

Window.setOverlayIcon

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

setPosition()
setPosition(position): Promise<void>

設定 webview 位置。

參數
參數類型描述
positionLogicalPosition | PhysicalPosition | Position新的位置,以邏輯或物理像素為單位。
回傳

Promise<void>

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

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

Window.setPosition

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)。
參數
參數類型
stateProgressBarState
回傳

Promise<void>

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

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

Window.setProgressBar

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

setResizable()
setResizable(resizable): Promise<void>

更新視窗可調整大小的旗標。

參數
參數類型
resizableboolean
回傳

Promise<void>

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

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

Window.setResizable

Source: 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: 不支援。
參數
參數類型
enableboolean
回傳

Promise<void>

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

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

Window.setShadow

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

setSize()
setSize(size): Promise<void>

調整 webview 大小。

參數
參數類型描述
sizeLogicalSize | PhysicalSize | Size邏輯或物理尺寸。
回傳

Promise<void>

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

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

Window.setSize

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

setSizeConstraints()
setSizeConstraints(constraints): Promise<void>

設定視窗內部尺寸約束。

參數
參數類型描述
constraintsundefined | null | WindowSizeConstraints邏輯或物理內部尺寸,或 null 以取消設定約束。
回傳

Promise<void>

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

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

Window.setSizeConstraints

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

setSkipTaskbar()
setSkipTaskbar(skip): Promise<void>

視窗圖示是否應從工作列中隱藏。

平台特定

  • macOS: 不支援。
參數
參數類型描述
skipbooleantrue 以隱藏視窗圖示,false 以顯示視窗圖示。
回傳

Promise<void>

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

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

Window.setSkipTaskbar

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

setTheme()
setTheme(theme?): Promise<void>

設定視窗主題,傳入 nullundefined 以跟隨系統主題

平台特定

  • Linux / macOS: 主題是應用程式範圍的,而非特定於此視窗。
  • iOS / Android: 不支援。
參數
參數類型
theme?null | Theme
回傳

Promise<void>

始於

2.0.0

繼承自

Window.setTheme

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

setTitle()
setTitle(title): Promise<void>

設定視窗標題。

參數
參數類型描述
title字串新的標題
回傳

Promise<void>

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

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

Window.setTitle

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

setTitleBarStyle()
setTitleBarStyle(style): Promise<void>

設定標題列樣式。僅限 macOS

參數
參數類型
styleTitleBarStyle
回傳

Promise<void>

始於

2.0.0

繼承自

Window.setTitleBarStyle

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

setVisibleOnAllWorkspaces()
setVisibleOnAllWorkspaces(visible): Promise<void>

設定視窗是否應在所有工作區或虛擬桌面上可見。

平台特定

  • Windows / iOS / Android: 不支援。
參數
參數類型
visibleboolean
回傳

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 縮放比例。

參數
參數類型
scaleFactornumber
回傳

Promise<void>

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

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

Webview.setZoom

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();
繼承自

Window.show

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

size()
size(): Promise<PhysicalSize>

webview 用戶端區域的物理尺寸。用戶端區域是 webview 的內容,不包括標題列和邊框。

回傳

Promise<PhysicalSize>

webview 的尺寸。

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

Webview.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();
繼承自

Window.startDragging

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

startResizeDragging()
startResizeDragging(direction): Promise<void>

開始調整視窗大小拖曳。

參數
參數類型
directionResizeDirection
回傳

Promise<void>

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

範例
import { getCurrentWindow } from '@tauri-apps/api/window';
await getCurrentWindow().startResizeDragging();
繼承自

Window.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
回傳

Promise<null | Theme>

視窗佈景主題。

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

Window.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();
繼承自

Window.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();
繼承自

Window.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();
繼承自

Window.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();
繼承自

Window.unminimize

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

getAll()
static getAll(): Promise<WebviewWindow[]>

取得所有可用 webview 的 Webview 實例列表。

回傳

Promise<WebviewWindow[]>

繼承自

Window.getAll

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字串webview 標籤。
回傳

Promise<null | WebviewWindow>

用於與 webview 通訊的 Webview 實例,如果 webview 不存在則為 null。

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

Window.getByLabel

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

getCurrent()
static getCurrent(): WebviewWindow

取得目前 webview 的 Webview 實例。

回傳

WebviewWindow

繼承自

Window.getCurrent

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

函式

getAllWebviewWindows()

function getAllWebviewWindows(): Promise<WebviewWindow[]>

取得所有可用 webview 視窗的 Webview 實例列表。

回傳

Promise<WebviewWindow[]>

始於

2.0.0

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


getCurrentWebviewWindow()

function getCurrentWebviewWindow(): WebviewWindow

取得目前 webview 視窗的 Webview 實例。

回傳

WebviewWindow

始於

2.0.0

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


© 2025 Tauri Contributors. CC-BY / MIT