KCEFHelper.kt 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package eu.mjdev.desktop.helpers
  2. import androidx.compose.runtime.MutableState
  3. import androidx.compose.runtime.mutableStateOf
  4. import dev.datlag.kcef.KCEF
  5. import kotlinx.coroutines.CoroutineScope
  6. import kotlinx.coroutines.Dispatchers
  7. import kotlinx.coroutines.launch
  8. import java.io.File
  9. import kotlin.math.max
  10. class KCEFHelper(
  11. private val scope: CoroutineScope?,
  12. val initialized: MutableState<Boolean> = mutableStateOf(false),
  13. val restartRequired: MutableState<Boolean> = mutableStateOf(false),
  14. val downloading: MutableState<Int> = mutableStateOf(0)
  15. ) {
  16. fun init() {
  17. if (!initialized.value && !restartRequired.value) {
  18. scope?.launch(Dispatchers.IO) {
  19. KCEF.init(builder = {
  20. installDir(File("kcef-bundle"))
  21. progress {
  22. onDownloading {
  23. downloading.value = max(it, 0F).toInt()
  24. }
  25. onInitialized {
  26. initialized.value = true
  27. }
  28. }
  29. settings {
  30. cachePath = File("cache").absolutePath
  31. }
  32. }, onError = { err ->
  33. err?.printStackTrace()
  34. }, onRestartRequired = {
  35. restartRequired.value = true
  36. })
  37. }
  38. }
  39. }
  40. fun dispose() {
  41. KCEF.disposeBlocking()
  42. }
  43. }