Android: run canvas WebView loads on main
This commit is contained in:
@@ -3,6 +3,7 @@ package com.steipete.clawdis.node.node
|
|||||||
import android.graphics.Bitmap
|
import android.graphics.Bitmap
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.graphics.Canvas
|
import android.graphics.Canvas
|
||||||
|
import android.os.Looper
|
||||||
import android.webkit.WebView
|
import android.webkit.WebView
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||||
@@ -33,16 +34,28 @@ class CanvasController {
|
|||||||
reload()
|
reload()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun reload() {
|
private inline fun withWebViewOnMain(crossinline block: (WebView) -> Unit) {
|
||||||
val wv = webView ?: return
|
val wv = webView ?: return
|
||||||
when (mode) {
|
if (Looper.myLooper() == Looper.getMainLooper()) {
|
||||||
Mode.WEB -> {
|
block(wv)
|
||||||
// Match iOS behavior: if URL is missing/invalid, keep the current page (canvas scaffold).
|
} else {
|
||||||
val trimmed = url.trim()
|
wv.post { block(wv) }
|
||||||
if (trimmed.isBlank()) return
|
}
|
||||||
wv.loadUrl(trimmed)
|
}
|
||||||
|
|
||||||
|
private fun reload() {
|
||||||
|
val currentMode = mode
|
||||||
|
val currentUrl = url
|
||||||
|
withWebViewOnMain { wv ->
|
||||||
|
when (currentMode) {
|
||||||
|
Mode.WEB -> {
|
||||||
|
// Match iOS behavior: if URL is missing/invalid, keep the current page (canvas scaffold).
|
||||||
|
val trimmed = currentUrl.trim()
|
||||||
|
if (trimmed.isBlank()) return@withWebViewOnMain
|
||||||
|
wv.loadUrl(trimmed)
|
||||||
|
}
|
||||||
|
Mode.CANVAS -> wv.loadDataWithBaseURL(null, canvasHtml, "text/html", "utf-8", null)
|
||||||
}
|
}
|
||||||
Mode.CANVAS -> wv.loadDataWithBaseURL(null, canvasHtml, "text/html", "utf-8", null)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user