Skip to content

Webview

Module @netwiv/api/webview · Kind Class

import { Webview } from '@netwiv/api/webview';
new Webview(window: Window, label: string, options: WebviewOptions)
getAll(): Promise<Webview[]>
getByLabel(label: string): Promise<Webview | null>
getCurrent(): Webview
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<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: string

The webview label. It is a unique identifier for the webview, can be used to reference it later.

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 unmounted
unlisten();
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 unmounted
unlisten();
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 unmounted
unlisten();

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(): 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(window: string | Window | WebviewWindow): Promise<void>
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(color: Color | null): Promise<void>

Specify the webview background color.

  • 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 0 will be replaced by 255
setFocus(): Promise<void>

Bring the webview to front and focus.

Example

import { getCurrentWebview } from '@netwiv/api/webview';
await getCurrentWebview().setFocus();
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(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(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: Window

Descriptions come from the pinned @tauri-apps/api@2.11.1 declarations (MIT OR Apache-2.0); imports are shown with @netwiv/api paths.