From 9f53b7e1ee94a2436f0cc3555c3a524d4cee317e Mon Sep 17 00:00:00 2001 From: SomeTroglodyte <63000004+SomeTroglodyte@users.noreply.github.com> Date: Tue, 13 Feb 2024 22:27:00 +0100 Subject: [PATCH] Key binding categories properly sorted in options popup (#11116) Delay some of the KeyBindingsTab work until it's viewed --- .../unciv/ui/popups/options/KeyBindingsTab.kt | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/core/src/com/unciv/ui/popups/options/KeyBindingsTab.kt b/core/src/com/unciv/ui/popups/options/KeyBindingsTab.kt index 28a5f5e407..4c10180932 100644 --- a/core/src/com/unciv/ui/popups/options/KeyBindingsTab.kt +++ b/core/src/com/unciv/ui/popups/options/KeyBindingsTab.kt @@ -22,7 +22,10 @@ class KeyBindingsTab( private val labelWidth: Float ) : Table(BaseScreen.skin), TabbedPager.IPageExtensions { private val keyBindings = optionsPopup.settings.keyBindings - private val groupedWidgets: LinkedHashMap> + + // lazy triggers on activate(), not on init: init runs when Options is opened, even if we never look at this tab. + private val groupedWidgets by lazy { createGroupedWidgets() } + private val disclaimer = MarkupRenderer.render(listOf( FormattedLine("This is a work in progress.", color = "#b22222", centered = true), // FIREBRICK FormattedLine(), @@ -39,20 +42,26 @@ class KeyBindingsTab( top() pad(10f) defaults().pad(5f) + } + private fun createGroupedWidgets(): LinkedHashMap> { + // We want: For each category, sorted by their translated label, + // a sorted (by translated label) collection of all visible bindings in that category, + // associated with the actual UI widget (a KeyCapturingButton), + // and we want to easily index that by binding, so it should be a order-preserving map. val collator = UncivGame.Current.settings.getCollatorFromLocale() - groupedWidgets = KeyboardBinding.values().asSequence() - .filterNot { it.hidden } - .groupBy { it.category } // Materializes a Map> - .asSequence() - .map { (category, bindings) -> - category to bindings.asSequence() - .sortedWith(compareBy(collator) { it.label.tr() }) - .map { it to KeyCapturingButton(it.defaultKey) { onKeyHit() } } // associate would materialize a map - .toMap(LinkedHashMap()) - } - .sortedBy { it.first.name.tr() } - .toMap(LinkedHashMap()) + return KeyboardBinding.values().asSequence() + .filterNot { it.hidden } + .groupBy { it.category } // Materializes a Map> + .asSequence() + .map { (category, bindings) -> + category to bindings.asSequence() + .sortedWith(compareBy(collator) { it.label.tr() }) // sort bindings within each category + .map { it to KeyCapturingButton(it.defaultKey) { onKeyHit() } } // associate would materialize a map + .toMap(LinkedHashMap()) // Stuff into a map that preserves our sorted order + } + .sortedWith(compareBy(collator) { it.first.label.tr() }) // sort the categories + .toMap(LinkedHashMap()) } private fun update() {