Skip to content

Commit 49b1dee

Browse files
committed
Refine WebView destruction and creation logic for non-macOS platforms
1 parent 561880f commit 49b1dee

1 file changed

Lines changed: 42 additions & 1 deletion

File tree

wrywebview/src/main/kotlin/io/github/kdroidfilter/webview/wry/WryWebViewPanel.kt

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ class WryWebViewPanel(
5656
override fun removeNotify() {
5757
log("removeNotify")
5858
stopCreateTimer()
59-
scheduleDestroyIfNeeded()
59+
if (IS_MAC) {
60+
scheduleDestroyIfNeeded()
61+
} else {
62+
destroyIfNeeded()
63+
}
6064
super.removeNotify()
6165
}
6266

@@ -316,6 +320,43 @@ class WryWebViewPanel(
316320
val userAgent = customUserAgent
317321
val initialUrl = pendingUrl
318322
val handleSnapshot = parentHandle
323+
if (!IS_MAC) {
324+
return try {
325+
webviewId =
326+
if (userAgent == null) {
327+
NativeBindings.createWebview(handleSnapshot, width, height, initialUrl)
328+
} else {
329+
NativeBindings.createWebviewWithUserAgent(handleSnapshot, width, height, initialUrl, userAgent)
330+
}
331+
updateBounds()
332+
startGtkPumpIfNeeded()
333+
startWindowsPumpIfNeeded()
334+
// Apply any pending content that requires an explicit call after creation.
335+
val id = webviewId
336+
val html = pendingHtml
337+
val urlWithHeaders = pendingUrlWithHeaders
338+
val headers = pendingHeaders
339+
if (id != null) {
340+
when {
341+
html != null -> {
342+
pendingHtml = null
343+
NativeBindings.loadHtml(id, html)
344+
}
345+
urlWithHeaders != null && headers.isNotEmpty() -> {
346+
pendingUrlWithHeaders = null
347+
pendingHeaders = emptyMap()
348+
NativeBindings.loadUrlWithHeaders(id, urlWithHeaders, headers)
349+
}
350+
}
351+
}
352+
log("createIfNeeded success id=$webviewId")
353+
true
354+
} catch (e: RuntimeException) {
355+
System.err.println("Failed to create Wry webview: ${e.message}")
356+
e.printStackTrace()
357+
true
358+
}
359+
}
319360
createInFlight = true
320361
stopCreateTimer()
321362
thread(name = "wry-webview-create", isDaemon = true) {

0 commit comments

Comments
 (0)