Skip to content
Closed
Changes from 4 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
d2df187
fix(api): resolve TypeScript errors in webviewWindow.ts
KushalMeghani1644 Sep 2, 2025
f254da8
Merge branch 'dev' into webviewWindowFIX
KushalMeghani1644 Sep 2, 2025
e06aafb
Added back the documentation.
KushalMeghani1644 Sep 2, 2025
9a7b6ba
Merge branch 'webviewWindowFIX' of github.com:KushalMeghani1644/tauri…
KushalMeghani1644 Sep 2, 2025
2639f11
Removed to strengthen type-safety
KushalMeghani1644 Sep 3, 2025
012bec8
Merge branch 'dev' into webviewWindowFIX
KushalMeghani1644 Sep 8, 2025
f7253e0
Merge branch 'dev' into webviewWindowFIX
KushalMeghani1644 Sep 9, 2025
61044d8
Merge branch 'dev' into webviewWindowFIX
KushalMeghani1644 Sep 12, 2025
11dec7e
Merge branch 'dev' into webviewWindowFIX
KushalMeghani1644 Sep 20, 2025
34f594f
fix(api): address webviewWindow.ts review comments
KushalMeghani1644 Sep 20, 2025
2a8e5cc
Merge branch 'webviewWindowFIX' of github.com:KushalMeghani1644/tauri…
KushalMeghani1644 Sep 20, 2025
c46bedc
Merge branch 'dev' into webviewWindowFIX
KushalMeghani1644 Sep 23, 2025
f7d4387
refactor: remove InternalWebviewOptions interface in webviewWindow.ts
KushalMeghani1644 Sep 24, 2025
984da5d
Merge branch 'webviewWindowFIX' of github.com:KushalMeghani1644/tauri…
KushalMeghani1644 Sep 24, 2025
8c5b7a5
Merge branch 'dev' into webviewWindowFIX
KushalMeghani1644 Sep 25, 2025
677a1d0
Update packages/api/src/webviewWindow.ts
KushalMeghani1644 Sep 25, 2025
370ccd3
Update webviewWindow.ts, added comments.
KushalMeghani1644 Sep 25, 2025
53e2462
Merge branch 'dev' into webviewWindowFIX
KushalMeghani1644 Sep 26, 2025
01ccff9
Merge branch 'dev' into webviewWindowFIX
KushalMeghani1644 Oct 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 56 additions & 43 deletions packages/api/src/webviewWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,21 @@ import type { EventName, EventCallback, UnlistenFn } from './event'
import { invoke } from './core'
import type { Color, DragDropEvent } from './webview'

// Internal-only type to avoid @ts-expect-error everywhere
interface InternalWebviewOptions
extends Omit<WebviewOptions, 'x' | 'y' | 'width' | 'height'> {
skip?: boolean
parent?: Window | WebviewWindow | string
}

/**
* Get an instance of `Webview` for the current webview window.
*
* @since 2.0.0
*/
function getCurrentWebviewWindow(): WebviewWindow {
const webview = getCurrentWebview()
// @ts-expect-error `skip` is not defined in the public API but it is handled by the constructor
return new WebviewWindow(webview.label, { skip: true })
return new WebviewWindow(webview.label, { skip: true } as any)
}

/**
Expand All @@ -36,9 +42,8 @@ async function getAllWebviewWindows(): Promise<WebviewWindow[]> {
windows.map(
(w) =>
new WebviewWindow(w, {
// @ts-expect-error `skip` is not defined in the public API but it is handled by the constructor
skip: true
})
} as any)
)
)
}
Expand All @@ -52,7 +57,6 @@ class WebviewWindow {
/** Local event listeners. */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
listeners: Record<string, Array<EventCallback<any>>>

/**
* Creates a new {@link Window} hosting a {@link Webview}.
* @example
Expand All @@ -75,21 +79,21 @@ class WebviewWindow {
constructor(
label: WebviewLabel,
options: Omit<WebviewOptions, 'x' | 'y' | 'width' | 'height'>
& WindowOptions = {}
& WindowOptions & { skip?: boolean } = {}
) {
this.label = label
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
this.listeners = Object.create(null)

// @ts-expect-error `skip` is not a public API so it is not defined in WebviewOptions
if (!options?.skip) {
const internalOptions = options as InternalWebviewOptions

if (!internalOptions?.skip) {
invoke('plugin:webview|create_webview_window', {
options: {
...options,
...internalOptions,
parent:
typeof options.parent === 'string'
? options.parent
: options.parent?.label,
typeof internalOptions.parent === 'string'
? internalOptions.parent
: internalOptions.parent?.label,
label
}
})
Expand All @@ -109,30 +113,27 @@ class WebviewWindow {
* @param label The webview label.
* @returns The Webview instance to communicate with the webview or null if the webview doesn't exist.
*/

static async getByLabel(label: string): Promise<WebviewWindow | null> {
const webview =
(await getAllWebviewWindows()).find((w) => w.label === label) ?? null
if (webview) {
// @ts-expect-error `skip` is not defined in the public API but it is handled by the constructor
return new WebviewWindow(webview.label, { skip: true })
return new WebviewWindow(webview.label, { skip: true } as any)
}
return null
}

/**
* Get an instance of `Webview` for the current webview.
*/

static getCurrent(): WebviewWindow {
return getCurrentWebviewWindow()
}

/**
* Gets a list of instances of `Webview` for all available webviews.
*/
Comment on lines -129 to -131
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert this

static async getAll(): Promise<WebviewWindow[]> {
return getAllWebviewWindows()
}

/**
* Listen to an emitted event on this webivew window.
*
Expand All @@ -152,15 +153,16 @@ class WebviewWindow {
* @returns A promise resolving to a function to unlisten to the event.
* Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
*/

async listen<T>(
event: EventName,
handler: EventCallback<T>
): Promise<UnlistenFn> {
if (this._handleTauriEvent(event, handler)) {
return () => {
// eslint-disable-next-line security/detect-object-injection
const listeners = this.listeners[event]
listeners.splice(listeners.indexOf(handler), 1)
const idx = listeners.indexOf(handler)
if (idx >= 0) listeners.splice(idx, 1)
}
}
return listen(event, handler, {
Expand All @@ -187,46 +189,57 @@ class WebviewWindow {
* @returns A promise resolving to a function to unlisten to the event.
* Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
*/

async once<T>(
event: EventName,
handler: EventCallback<T>
): Promise<UnlistenFn> {
if (this._handleTauriEvent(event, handler)) {
return () => {
// eslint-disable-next-line security/detect-object-injection
const listeners = this.listeners[event]
listeners.splice(listeners.indexOf(handler), 1)
const idx = listeners.indexOf(handler)
if (idx >= 0) listeners.splice(idx, 1)
}
}
return once(event, handler, {
target: { kind: 'WebviewWindow', label: this.label }
})
return once(
event,
handler,
/**
* Set the window and webview background color.
*
* #### Platform-specific:
*
* - **Android / iOS:** Unsupported for the window layer.
* - **macOS / iOS**: Not implemented for the webview layer.
* - **Windows**:
* - alpha channel is ignored for the window layer.
* - On Windows 7, alpha channel is ignored for the webview layer.
* - On Windows 8 and newer, if alpha channel is not `0`, it will be ignored.
*
* @returns A promise indicating the success or failure of the operation.
*
* @since 2.1.0
*/ {
target: { kind: 'WebviewWindow', label: this.label }
}
)
}

/**
* Set the window and webview background color.
*
* #### Platform-specific:
*
* - **Android / iOS:** Unsupported for the window layer.
* - **macOS / iOS**: Not implemented for the webview layer.
* - **Windows**:
* - alpha channel is ignored for the window layer.
* - On Windows 7, alpha channel is ignored for the webview layer.
* - On Windows 8 and newer, if alpha channel is not `0`, it will be ignored.
*
* @returns A promise indicating the success or failure of the operation.
*
Comment on lines 209 to 219
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert this

* @since 2.1.0
*/
async setBackgroundColor(color: Color): Promise<void> {
return invoke('plugin:window|set_background_color', { color }).then(() => {
return invoke('plugin:webview|set_webview_background_color', { color })
})
try {
await invoke('plugin:window|set_background_color', { color })
await invoke('plugin:webview|set_webview_background_color', { color })
} catch (err) {
throw new Error(`Failed to set background color: ${String(err)}`)
}
}
}

// Order matters, we use window APIs by default
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert this comment, it is vital

applyMixins(WebviewWindow, [Window, Webview])

/** Extends a base class by other specified classes, without overriding existing properties */
Expand All @@ -243,12 +256,12 @@ function applyMixins(
typeof baseClass.prototype === 'object'
&& baseClass.prototype
&& name in baseClass.prototype
)
) {
return
}
Object.defineProperty(
baseClass.prototype,
name,
// eslint-disable-next-line
Object.getOwnPropertyDescriptor(extendedClass.prototype, name)
?? Object.create(null)
)
Expand Down
Loading