Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
44 changes: 19 additions & 25 deletions packages/api/src/webviewWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,17 @@ import type { Color, DragDropEvent } from './webview'
*/
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 })
}

/**
* Gets a list of instances of `Webview` for all available webview windows.
*
* @since 2.0.0
* Gets a list of instances of `Webview` for all available webviews.
Comment on lines -30 to +29
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

*/
async function getAllWebviewWindows(): Promise<WebviewWindow[]> {
return invoke<string[]>('plugin:window|get_all_windows').then((windows) =>
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
})
)
Expand All @@ -52,7 +48,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,14 +70,12 @@ 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) {
if (!(options && 'skip' in options && options.skip)) {
invoke('plugin:webview|create_webview_window', {
options: {
...options,
Expand All @@ -109,11 +102,11 @@ 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 null
Expand All @@ -126,13 +119,9 @@ class 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 +141,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,22 +177,22 @@ 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 }
})
}

/**
* Set the window and webview background color.
*
Expand All @@ -220,12 +210,16 @@ class WebviewWindow {
* @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', { cause: 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])

Expand All @@ -243,12 +237,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
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading