manager: make inset synchronize

This commit is contained in:
KOWX712
2025-11-21 11:36:04 +08:00
committed by ShirkNeko
parent a585989a03
commit 4f9b745cd0

View File

@@ -22,7 +22,9 @@ import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.webkit.WebViewAssetLoader import androidx.webkit.WebViewAssetLoader
import com.topjohnwu.superuser.Shell import com.topjohnwu.superuser.Shell
import kotlinx.coroutines.CancellableContinuation
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine
import com.sukisu.ultra.ui.util.createRootShell import com.sukisu.ultra.ui.util.createRootShell
import com.sukisu.ultra.ui.viewmodel.SuperUserViewModel import com.sukisu.ultra.ui.viewmodel.SuperUserViewModel
import top.yukonga.miuix.kmp.basic.InfiniteProgressIndicator import top.yukonga.miuix.kmp.basic.InfiniteProgressIndicator
@@ -34,6 +36,7 @@ class WebUIActivity : ComponentActivity() {
private var rootShell: Shell? = null private var rootShell: Shell? = null
private lateinit var insets: Insets private lateinit var insets: Insets
private var insetsContinuation: CancellableContinuation<Unit>? = null
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
@@ -62,7 +65,7 @@ class WebUIActivity : ComponentActivity() {
} }
} }
private fun setupWebView() { private suspend fun setupWebView() {
val moduleId = intent.getStringExtra("id")!! val moduleId = intent.getStringExtra("id")!!
val name = intent.getStringExtra("name")!! val name = intent.getStringExtra("name")!!
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
@@ -81,6 +84,35 @@ class WebUIActivity : ComponentActivity() {
val rootShell = createRootShell(true).also { this.rootShell = it } val rootShell = createRootShell(true).also { this.rootShell = it }
insets = Insets(0, 0, 0, 0) insets = Insets(0, 0, 0, 0)
val webView = WebView(this).apply {
setBackgroundColor(Color.TRANSPARENT)
val density = resources.displayMetrics.density
ViewCompat.setOnApplyWindowInsetsListener(this) { _, windowInsets ->
val inset = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout())
insets = Insets(
top = (inset.top / density).toInt(),
bottom = (inset.bottom / density).toInt(),
left = (inset.left / density).toInt(),
right = (inset.right / density).toInt()
)
insetsContinuation?.resumeWith(Result.success(Unit))
insetsContinuation = null
WindowInsetsCompat.CONSUMED
}
}
setContentView(webView)
if (insets == Insets(0, 0, 0, 0)) {
suspendCancellableCoroutine<Unit> { cont ->
insetsContinuation = cont
cont.invokeOnCancellation {
insetsContinuation = null
}
}
}
val webViewAssetLoader = WebViewAssetLoader.Builder() val webViewAssetLoader = WebViewAssetLoader.Builder()
.setDomain("mui.kernelsu.org") .setDomain("mui.kernelsu.org")
.addPathHandler( .addPathHandler(
@@ -114,30 +146,15 @@ class WebUIActivity : ComponentActivity() {
} }
} }
val webView = WebView(this).apply { webView.apply {
setBackgroundColor(Color.TRANSPARENT)
val density = resources.displayMetrics.density
ViewCompat.setOnApplyWindowInsetsListener(this) { _, windowInsets ->
val inset = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout())
insets = Insets(
top = (inset.top / density).toInt(),
bottom = (inset.bottom / density).toInt(),
left = (inset.left / density).toInt(),
right = (inset.right / density).toInt()
)
WindowInsetsCompat.CONSUMED
}
settings.javaScriptEnabled = true settings.javaScriptEnabled = true
settings.domStorageEnabled = true settings.domStorageEnabled = true
settings.allowFileAccess = false settings.allowFileAccess = false
webviewInterface = WebViewInterface(this@WebUIActivity, this, moduleDir) webviewInterface = WebViewInterface(this@WebUIActivity, webView, moduleDir)
addJavascriptInterface(webviewInterface, "ksu") addJavascriptInterface(webviewInterface, "ksu")
setWebViewClient(webViewClient) setWebViewClient(webViewClient)
loadUrl("https://mui.kernelsu.org/index.html") loadUrl("https://mui.kernelsu.org/index.html")
} }
setContentView(webView)
} }
override fun onDestroy() { override fun onDestroy() {