Skip to content

Commit

Permalink
support progress when loading from runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed Oct 24, 2024
1 parent ad9c1dd commit ba06fdf
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
27 changes: 2 additions & 25 deletions kcef/src/main/kotlin/dev/datlag/kcef/KCEF.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -303,22 +296,6 @@ data object KCEF {
dispose()
}

private fun initFromRuntime(cefArgs: Collection<String>): 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<CefApp>): Boolean {
val nextState = if (result.isSuccess) {
cefApp = result.getOrThrow()
Expand Down
40 changes: 40 additions & 0 deletions kcef/src/main/kotlin/dev/datlag/kcef/KCEFBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down

0 comments on commit ba06fdf

Please sign in to comment.