From b6ef0da344948580c07502f0a91dcb440d55af2b Mon Sep 17 00:00:00 2001 From: JFronny Date: Sat, 22 Jun 2024 20:59:33 +0200 Subject: [PATCH] fix(gtk): move some network-dependent computation to background thread --- .../jfronny/inceptum/gtk/IdleTaskQueue.kt | 4 ++++ .../gtk/window/create/NewInstanceWindow.kt | 7 +++--- .../dialog/ProcessStateWatcherDialog.kt | 5 ++-- .../window/settings/instance/GeneralTab.kt | 24 ++++++++++++------- .../gtk/window/settings/instance/ModsTab.kt | 3 ++- 5 files changed, 28 insertions(+), 15 deletions(-) diff --git a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/IdleTaskQueue.kt b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/IdleTaskQueue.kt index 38af799..eaa0539 100644 --- a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/IdleTaskQueue.kt +++ b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/IdleTaskQueue.kt @@ -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) { diff --git a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/window/create/NewInstanceWindow.kt b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/window/create/NewInstanceWindow.kt index 35a1906..a4183cf 100644 --- a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/window/create/NewInstanceWindow.kt +++ b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/window/create/NewInstanceWindow.kt @@ -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) { diff --git a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/window/dialog/ProcessStateWatcherDialog.kt b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/window/dialog/ProcessStateWatcherDialog.kt index 90f3cb2..41b6f1a 100644 --- a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/window/dialog/ProcessStateWatcherDialog.kt +++ b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/window/dialog/ProcessStateWatcherDialog.kt @@ -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) { diff --git a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/window/settings/instance/GeneralTab.kt b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/window/settings/instance/GeneralTab.kt index dd34fc7..b0baee3 100644 --- a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/window/settings/instance/GeneralTab.kt +++ b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/window/settings/instance/GeneralTab.kt @@ -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 Unit)? = null - var fabricVersion: DropDown? = null + var fabricVersion: KDropDown? = null var defaultFabric: String? = null var fabricVersions: Array? = null @@ -61,7 +64,7 @@ class GeneralTab(window: InstanceSettingsWindow) : SectionedSettingsTab 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 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") { diff --git a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/window/settings/instance/ModsTab.kt b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/window/settings/instance/ModsTab.kt index 67c73a6..8a2ee4f 100644 --- a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/window/settings/instance/ModsTab.kt +++ b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/window/settings/instance/ModsTab.kt @@ -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