diff --git a/kcef/src/main/kotlin/dev/datlag/kcef/KCEF.kt b/kcef/src/main/kotlin/dev/datlag/kcef/KCEF.kt index 8cb040f4..f463a905 100644 --- a/kcef/src/main/kotlin/dev/datlag/kcef/KCEF.kt +++ b/kcef/src/main/kotlin/dev/datlag/kcef/KCEF.kt @@ -95,15 +95,8 @@ data object KCEF { CefApp.addAppHandler(currentBuilder.appHandler ?: AppHandler()) - if (initFromRuntime(currentBuilder.args)) { - val result = suspendCatching { - CefApp.getInstanceIfAny() ?: suspendCatching { - CefApp.getInstance(currentBuilder.settings.toJcefSettings()) - }.getOrNull() ?: CefApp.getInstance() - } - setInitResult(result) - result.exceptionOrNull()?.let(onError::invoke) - + currentBuilder.initFromRuntime()?.let { + setInitResult(Result.success(it)) return } @@ -303,22 +296,6 @@ data object KCEF { dispose() } - private fun initFromRuntime(cefArgs: Collection): Boolean { - systemLoadLibrary("jawt") || return false - - if (cefArgs.none { it.trim().equals("--disable-gpu", true) }) { - systemLoadLibrary("EGL") - systemLoadLibrary("GLESv2") - systemLoadLibrary("vk_swiftshader") - } - - systemLoadLibrary("libcef") || systemLoadLibrary("cef") || systemLoadLibrary("jcef") || return false - - return scopeCatching { - CefApp.startup(cefArgs.toTypedArray()) - }.getOrNull() ?: false - } - private fun setInitResult(result: Result): Boolean { val nextState = if (result.isSuccess) { cefApp = result.getOrThrow() diff --git a/kcef/src/main/kotlin/dev/datlag/kcef/KCEFBuilder.kt b/kcef/src/main/kotlin/dev/datlag/kcef/KCEFBuilder.kt index 57afe678..2c8ef89f 100644 --- a/kcef/src/main/kotlin/dev/datlag/kcef/KCEFBuilder.kt +++ b/kcef/src/main/kotlin/dev/datlag/kcef/KCEFBuilder.kt @@ -290,6 +290,46 @@ class KCEFBuilder { return this.instance!! } + internal fun initFromRuntime(): CefApp? { + this.instance?.let { return it } + if (!systemLoadLibrary("jawt")) { + return null + } + + if (args.none { it.trim().equals("--disable-gpu", true) }) { + systemLoadLibrary("EGL") + systemLoadLibrary("GLESv2") + systemLoadLibrary("vk_swiftshader") + } + + val cefLoaded = systemLoadLibrary("libcef") || systemLoadLibrary("cef") || systemLoadLibrary("jcef") + if (!cefLoaded) { + return null + } + + val started = scopeCatching { + CefApp.startup(args.toTypedArray()) + }.getOrNull() ?: false + + if (!started) { + return null + } + + this.instance = CefApp.getInstanceIfAny() ?: scopeCatching { + CefApp.getInstance(settings.toJcefSettings()) + }.getOrNull() ?: scopeCatching { + CefApp.getInstance() + }.getOrNull() + + this.instance?.onInitialization { state -> + if (state == CefApp.CefAppState.INITIALIZED) { + this.progress.initialized() + } + } + + return this.instance + } + @JvmDefaultWithCompatibility interface InitProgress {