fix(gtk): move some network-dependent computation to background thread

This commit is contained in:
Johannes Frohnmeyer 2024-06-22 20:59:33 +02:00
parent 56c25880e8
commit b6ef0da344
Signed by: Johannes
GPG Key ID: E76429612C2929F4
5 changed files with 28 additions and 15 deletions

View File

@ -9,6 +9,10 @@ fun schedule(task: Runnable) {
SCHEDULED.add(task)
}
fun backgroundTask(task: Runnable) {
Thread.ofVirtual().start(task)
}
fun runScheduledTasks() {
var r: Runnable?
while (SCHEDULED.poll().also { r = it } != null) {

View File

@ -2,6 +2,7 @@ package io.gitlab.jfronny.inceptum.gtk.window.create
import io.gitlab.jfronny.commons.StringFormatter
import io.gitlab.jfronny.inceptum.common.InceptumConfig
import io.gitlab.jfronny.inceptum.gtk.backgroundTask
import io.gitlab.jfronny.inceptum.gtk.control.KDropDown
import io.gitlab.jfronny.inceptum.gtk.control.KEntry
import io.gitlab.jfronny.inceptum.gtk.control.assistant.KAssistant
@ -137,12 +138,12 @@ class NewInstanceWindow(app: Application) : KAssistant(app) {
onClose { pState.cancel() }
onCancel { pState.cancel() }
pState.updateStep("Starting install process")
Thread {
backgroundTask {
try {
for (step in Steps.STEPS) {
if (state.isCancelled) {
state.tryRemoveInstance()
return@Thread
return@backgroundTask
}
pState.incrementStep(step.name)
step.execute(state)
@ -159,7 +160,7 @@ class NewInstanceWindow(app: Application) : KAssistant(app) {
schedule { setComplete(true) }
schedule { nextPage() }
}
}.start()
}
}
}
page("Done", AssistantPageType.SUMMARY) {

View File

@ -3,6 +3,7 @@ package io.gitlab.jfronny.inceptum.gtk.window.dialog
import io.gitlab.jfronny.commons.StringFormatter
import io.gitlab.jfronny.commons.throwable.ThrowingRunnable
import io.gitlab.jfronny.inceptum.gtk.GtkEnvBackend
import io.gitlab.jfronny.inceptum.gtk.backgroundTask
import io.gitlab.jfronny.inceptum.gtk.schedule
import io.gitlab.jfronny.inceptum.gtk.util.I18n
import io.gitlab.jfronny.inceptum.gtk.util.Log
@ -61,7 +62,7 @@ class ProcessStateWatcherDialog(
}
GLib.SOURCE_CONTINUE
}
Thread {
backgroundTask {
try {
executor.run()
} catch (e: Throwable) {
@ -78,7 +79,7 @@ class ProcessStateWatcherDialog(
finished = true
schedule { close() }
}
}.start()
}
}
data class State(val msg: String, val progress: Float) {

View File

@ -6,8 +6,11 @@ import io.gitlab.jfronny.commons.io.JFiles
import io.gitlab.jfronny.inceptum.common.InceptumConfig
import io.gitlab.jfronny.inceptum.common.MetaHolder
import io.gitlab.jfronny.inceptum.common.Utils
import io.gitlab.jfronny.inceptum.gtk.backgroundTask
import io.gitlab.jfronny.inceptum.gtk.control.ILabel
import io.gitlab.jfronny.inceptum.gtk.control.KDropDown
import io.gitlab.jfronny.inceptum.gtk.control.settings.SectionedSettingsTab
import io.gitlab.jfronny.inceptum.gtk.schedule
import io.gitlab.jfronny.inceptum.gtk.util.*
import io.gitlab.jfronny.inceptum.launcher.api.FabricMetaApi
import io.gitlab.jfronny.inceptum.launcher.api.McApi
@ -53,7 +56,7 @@ class GeneralTab(window: InstanceSettingsWindow) : SectionedSettingsTab<Instance
run {
var fabricEnabled: Switch? = null
var versionChanged: (() -> Unit)? = null
var fabricVersion: DropDown? = null
var fabricVersion: KDropDown<String>? = null
var defaultFabric: String? = null
var fabricVersions: Array<String>? = null
@ -61,7 +64,7 @@ class GeneralTab(window: InstanceSettingsWindow) : SectionedSettingsTab<Instance
.filter { InceptumConfig.snapshots || it.type == "release" }
.map { it.id }
.toTypedArray()
val def = versions.withIndex().firstOrNull { it.value == instance.gameVersion }?.index ?: 0
val def = instance.gameVersion.let { gameVersion -> versions.withIndex().firstOrNull { it.value == gameVersion }?.index ?: 0 }
row("instance.settings.general.game.version", "instance.settings.general.game.version.subtitle") {
setDropdown(versions, def) { i ->
@ -93,7 +96,7 @@ class GeneralTab(window: InstanceSettingsWindow) : SectionedSettingsTab<Instance
}
fabricEnabled.bindProperty("active", loaderRow, "visible", BindingFlags.DEFAULT)
versionChanged = {
versionChanged = { backgroundTask {
val ver = VERSIONS.versions.stream()
.filter { it.id == instance.gameVersion }
.findFirst()
@ -108,18 +111,21 @@ class GeneralTab(window: InstanceSettingsWindow) : SectionedSettingsTab<Instance
fabricVersions = ver.map { Arrays.stream(it) }
.map { it.map { l -> l.loader.version }.toTypedArray() }
.orElse(null)
if (fabricVersions == null || fabricVersions!!.isEmpty()) {
fabricEnabled.active = false
} else if (fabricVersion != null) fabricVersion!!.model = StringList(fabricVersions)
}
versionChanged()
schedule {
if (fabricVersions == null || fabricVersions!!.isEmpty()) {
fabricEnabled.active = false
fabricVersion!!.updateOptions(arrayOf(), Gtk.INVALID_LIST_POSITION)
} else fabricVersion!!.updateOptions(fabricVersions!!, fabricVersions!!.indexOf(defaultFabric))
}
} }
fabricVersion =
loaderRow.setDropdown(fabricVersions!!, fabricVersions!!.indexOf(defaultFabric)) { i: Int ->
loaderRow.setDropdown(arrayOf(), Gtk.INVALID_LIST_POSITION) { i: Int ->
instance.meta.gameVersion =
if (i == -1) instance.gameVersion
else GameVersionParser.createVersionWithFabric(instance.gameVersion, fabricVersions!![i])
instance.writeMeta()
}
versionChanged()
fabricVersion.enableSearch = true
}
row("instance.settings.general.game.java", "instance.settings.general.game.java.subtitle") {

View File

@ -2,6 +2,7 @@ package io.gitlab.jfronny.inceptum.gtk.window.settings.instance
import io.gitlab.jfronny.commons.concurrent.AsyncRequest
import io.gitlab.jfronny.commons.concurrent.VoidFuture
import io.gitlab.jfronny.inceptum.gtk.backgroundTask
import io.gitlab.jfronny.inceptum.gtk.control.ILabel
import io.gitlab.jfronny.inceptum.gtk.control.KDropDown
import io.gitlab.jfronny.inceptum.gtk.control.KSignalListItemFactory
@ -154,7 +155,7 @@ class ModsTab(window: InstanceSettingsWindow) : SettingsTab<Leaflet, InstanceSet
fun updateSearch(search: String): Unit {
descriptionLabel.text = "Searching is currently unsupported"
currentSearchString = search
this.search.request()
backgroundTask { this.search.request() }
}
fun selectMod(mod: ModState): Unit {
//TODO detailed menu for version selection, ...