Webview
Module @netwiv/api/webview · Kind Class
import { Webview } from '@netwiv/api/webview';Constructor
Section titled “Constructor”new Webview(window: Window, label: string, options: WebviewOptions)Static members
Section titled “Static members”getAll
Section titled “getAll”getAll(): Promise<Webview[]>getByLabel
Section titled “getByLabel”getByLabel(label: string): Promise<Webview | null>getCurrent
Section titled “getCurrent”getCurrent(): WebviewInstance members
Section titled “Instance members”clearAllBrowsingData
Section titled “clearAllBrowsingData”clearAllBrowsingData(): Promise<void>Clears all browsing data for this webview.
Example
import { getCurrentWebview } from '@netwiv/api/webview';await getCurrentWebview().clearAllBrowsingData();close(): Promise<void>Closes the webview.
Example
import { getCurrentWebview } from '@netwiv/api/webview';await getCurrentWebview().close();emit<T>(event: string, payload?: T | undefined): Promise<void>Emits an event to all {@link EventTargettargets}.
Example
import { getCurrentWebview } from '@netwiv/api/webview';await getCurrentWebview().emit('webview-loaded', { loggedIn: true, token: 'authToken' });emitTo
Section titled “emitTo”emitTo<T>(target: string | EventTarget, event: string, payload?: T | undefined): Promise<void>Emits an event to all {@link EventTargettargets} matching the given target.
Example
import { getCurrentWebview } from '@netwiv/api/webview';await getCurrentWebview().emitTo('main', 'webview-loaded', { loggedIn: true, token: 'authToken' });hide(): Promise<void>Hide the webview.
Example
import { getCurrentWebview } from '@netwiv/api/webview';await getCurrentWebview().hide();label: stringThe webview label. It is a unique identifier for the webview, can be used to reference it later.
listen
Section titled “listen”listen<T>(event: EventName, handler: EventCallback<T>): Promise<UnlistenFn>Listen to an emitted event on this webview.
Example
import { getCurrentWebview } from '@netwiv/api/webview';const unlisten = await getCurrentWebview().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();listeners
Section titled “listeners”listeners: Record<string, EventCallback<any>[]>Local event listeners.
once<T>(event: EventName, handler: EventCallback<T>): Promise<UnlistenFn>Listen to an emitted event on this webview only once.
Example
import { getCurrentWebview } from '@netwiv/api/webview';const unlisten = await getCurrent().once<null>('initialized', (event) => { console.log(`Webview initialized!`);});
// you need to call unlisten if your handler goes out of scope e.g. the component is unmountedunlisten();onDragDropEvent
Section titled “onDragDropEvent”onDragDropEvent(handler: EventCallback<DragDropEvent>): Promise<UnlistenFn>Listen to a file drop event. The listener is triggered when the user hovers the selected files on the webview, drops the files or cancels the operation.
Example
import { getCurrentWebview } from "@netwiv/api/webview";const unlisten = await getCurrentWebview().onDragDropEvent((event) => { if (event.payload.type === 'over') { console.log('User hovering', event.payload.position); } else if (event.payload.type === 'drop') { console.log('User dropped', event.payload.paths); } else { console.log('File drop cancelled'); }});
// you need to call unlisten if your handler goes out of scope e.g. the component is unmountedunlisten();When the debugger panel is open, the drop position of this event may be inaccurate due to a known limitation. To retrieve the correct drop position, please detach the debugger.
position
Section titled “position”position(): Promise<PhysicalPosition>The position of the top-left hand corner of the webview’s client area relative to the top-left hand corner of the desktop.
Example
import { getCurrentWebview } from '@netwiv/api/webview';const position = await getCurrentWebview().position();reparent
Section titled “reparent”reparent(window: string | Window | WebviewWindow): Promise<void>setAutoResize
Section titled “setAutoResize”setAutoResize(autoResize: boolean): Promise<void>Sets whether the webview should automatically grow and shrink its size and position when the parent window resizes.
Example
import { getCurrentWebview } from '@netwiv/api/webview';await getCurrentWebview().setAutoResize(true);setBackgroundColor
Section titled “setBackgroundColor”setBackgroundColor(color: Color | null): Promise<void>Specify the webview background color.
Platfrom-specific:
Section titled “Platfrom-specific:”- macOS / iOS: Not implemented.
- Windows:
- On Windows 7, transparency is not supported and the alpha value will be ignored.
- On Windows higher than 7: translucent colors are not supported so any alpha value other than
0will be replaced by255
setFocus
Section titled “setFocus”setFocus(): Promise<void>Bring the webview to front and focus.
Example
import { getCurrentWebview } from '@netwiv/api/webview';await getCurrentWebview().setFocus();setPosition
Section titled “setPosition”setPosition(position: LogicalPosition | PhysicalPosition | Position): Promise<void>Sets the webview position.
Example
import { getCurrent, LogicalPosition } from '@netwiv/api/webview';await getCurrentWebview().setPosition(new LogicalPosition(600, 500));setSize
Section titled “setSize”setSize(size: LogicalSize | PhysicalSize | Size): Promise<void>Resizes the webview.
Example
import { getCurrent, LogicalSize } from '@netwiv/api/webview';await getCurrentWebview().setSize(new LogicalSize(600, 500));setZoom
Section titled “setZoom”setZoom(scaleFactor: number): Promise<void>Set webview zoom level.
Example
import { getCurrentWebview } from '@netwiv/api/webview';await getCurrentWebview().setZoom(1.5);show(): Promise<void>Show the webview.
Example
import { getCurrentWebview } from '@netwiv/api/webview';await getCurrentWebview().show();size(): Promise<PhysicalSize>The physical size of the webview’s client area. The client area is the content of the webview, excluding the title bar and borders.
Example
import { getCurrentWebview } from '@netwiv/api/webview';const size = await getCurrentWebview().size();window
Section titled “window”window: WindowDescriptions come from the pinned @tauri-apps/api@2.11.1 declarations (MIT OR Apache-2.0); imports are shown with @netwiv/api paths.