@@ -72,15 +72,18 @@ import kotlinx.coroutines.flow.MutableStateFlow
7272import kotlinx.coroutines.flow.debounce
7373import kotlinx.coroutines.isActive
7474import kotlinx.coroutines.launch
75+ import kotlinx.coroutines.withContext
7576import org.jetbrains.compose.resources.painterResource
7677import org.koin.compose.KoinApplication
7778import org.koin.compose.viewmodel.koinViewModel
7879import org.openani.mediamp.PlaybackState
7980import org.openani.mediamp.compose.rememberMediampPlayer
81+ import java.awt.EventQueue
8082import java.awt.Dimension
8183import java.io.File
8284import java.io.RandomAccessFile
8385import java.util.concurrent.atomic.AtomicBoolean
86+ import kotlin.coroutines.CoroutineContext
8487
8588private object WindowsDisplaySleepBlocker {
8689 private const val ES_SYSTEM_REQUIRED = 0x00000001
@@ -113,12 +116,6 @@ fun main() {
113116 Logger .setLogWriters(ConsoleLogWriter (), FileLogWriter (logDir))
114117 Logger .withTag(" main" ).i { " Application started. Logs directory: ${logDir.absolutePath} " }
115118
116- WebViewBootstrap .start(
117- installDir = kcefInstallDir(),
118- cacheDir = kcefCacheDir(),
119- logDir = logDir
120- )
121-
122119 application {
123120 DisposableEffect (Unit ) {
124121 ProxyManager .start()
@@ -127,6 +124,14 @@ fun main() {
127124 }
128125 }
129126
127+ LaunchedEffect (Unit ) {
128+ WebViewBootstrap .start(
129+ installDir = kcefInstallDir(),
130+ cacheDir = kcefCacheDir(),
131+ logDir = logDir
132+ )
133+ }
134+
130135 val webViewInitialized by WebViewBootstrap .initialized.collectAsState()
131136 val webViewRestartRequired by WebViewBootstrap .restartRequired.collectAsState()
132137 val webViewInitError by WebViewBootstrap .initError.collectAsState()
@@ -467,7 +472,16 @@ fun main() {
467472
468473private object WebViewBootstrap {
469474 private val started = AtomicBoolean (false )
470- private val scope = CoroutineScope (SupervisorJob () + Dispatchers .IO )
475+ private val scope = CoroutineScope (SupervisorJob () + Dispatchers .Default )
476+ private val awtDispatcher = object : kotlinx.coroutines.CoroutineDispatcher () {
477+ override fun dispatch (context : CoroutineContext , block : Runnable ) {
478+ if (EventQueue .isDispatchThread()) {
479+ block.run ()
480+ } else {
481+ EventQueue .invokeLater(block)
482+ }
483+ }
484+ }
471485
472486 val initialized = MutableStateFlow (false )
473487 val restartRequired = MutableStateFlow (false )
@@ -488,27 +502,35 @@ private object WebViewBootstrap {
488502 if (! installDir.exists()) installDir.mkdirs()
489503 if (! cacheDir.exists()) cacheDir.mkdirs()
490504
491- KCEF .init (
492- builder = {
493- installDir(installDir)
494- settings {
495- cachePath = cacheDir.absolutePath
496- logFile = kcefLog.absolutePath
497- // logSeverity = KCEFBuilder.Settings.LogSeverity.INFO
498- }
499- progress {
500- onInitialized {
501- initialized.value = true
505+ val initFailure = runCatching {
506+ withContext(awtDispatcher) {
507+ KCEF .init (
508+ builder = {
509+ installDir(installDir)
510+ settings {
511+ cachePath = cacheDir.absolutePath
512+ logFile = kcefLog.absolutePath
513+ // logSeverity = KCEFBuilder.Settings.LogSeverity.INFO
502514 }
515+ progress {
516+ onInitialized {
517+ initialized.value = true
518+ }
519+ }
520+ },
521+ onError = { throwable ->
522+ initError.value = throwable
523+ },
524+ onRestartRequired = {
525+ restartRequired.value = true
503526 }
504- },
505- onError = { throwable ->
506- initError.value = throwable
507- },
508- onRestartRequired = {
509- restartRequired.value = true
527+ )
510528 }
511- )
529+ }.exceptionOrNull()
530+
531+ if (initFailure != null ) {
532+ initError.value = initFailure
533+ }
512534 } catch (t: Throwable ) {
513535 initError.value = t
514536 }
@@ -567,27 +589,29 @@ private object WebViewBootstrap {
567589private fun kcefInstallDir (): File {
568590 val platform = currentPlatformDesktop()
569591 val baseDir = when (platform) {
570- is Platform .Linux -> File (System .getProperty(" user.home" ), " .local/share/fn-media " )
571- is Platform .MacOS -> File (System .getProperty(" user.home" ), " Library/Application Support/fn-media " )
592+ is Platform .Linux -> File (System .getProperty(" user.home" ), " .local/share/fly-narwhal " )
593+ is Platform .MacOS -> File (System .getProperty(" user.home" ), " Library/Application Support/fly-narwhal " )
572594 is Platform .Windows -> {
573595 val localAppData = System .getenv(" LOCALAPPDATA" )?.takeIf { it.isNotBlank() }
574596 File (localAppData ? : System .getProperty(" user.home" ), " FlyNarwhal" )
575597 }
576598 }
577- return File (baseDir, " kcef-bundle" )
599+ val version = BuildConfig .VERSION_NAME .replace(Regex (" [^A-Za-z0-9._-]" ), " _" )
600+ return File (baseDir, " kcef-bundle-$version " )
578601}
579602
580603private fun kcefCacheDir (): File {
581604 val platform = currentPlatformDesktop()
582605 val baseDir = when (platform) {
583- is Platform .Linux -> File (System .getProperty(" user.home" ), " .local/share/fn-media " )
584- is Platform .MacOS -> File (System .getProperty(" user.home" ), " Library/Application Support/fn-media " )
606+ is Platform .Linux -> File (System .getProperty(" user.home" ), " .local/share/fly-narwhal " )
607+ is Platform .MacOS -> File (System .getProperty(" user.home" ), " Library/Application Support/fly-narwhal " )
585608 is Platform .Windows -> {
586609 val localAppData = System .getenv(" LOCALAPPDATA" )?.takeIf { it.isNotBlank() }
587610 File (localAppData ? : System .getProperty(" user.home" ), " FlyNarwhal" )
588611 }
589612 }
590- return File (baseDir, " kcef-cache" )
613+ val version = BuildConfig .VERSION_NAME .replace(Regex (" [^A-Za-z0-9._-]" ), " _" )
614+ return File (baseDir, " kcef-cache-$version " )
591615}
592616
593617/* *
0 commit comments