Key binding categories properly sorted in options popup (#11116)

Delay some of the KeyBindingsTab work until it's viewed
This commit is contained in:
SomeTroglodyte 2024-02-13 22:27:00 +01:00 committed by GitHub
parent 539db00cb6
commit 9f53b7e1ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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<KeyboardBinding.Category, LinkedHashMap<KeyboardBinding, KeyCapturingButton>>
// 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,19 +42,25 @@ class KeyBindingsTab(
top()
pad(10f)
defaults().pad(5f)
}
private fun createGroupedWidgets(): LinkedHashMap<KeyboardBinding.Category, LinkedHashMap<KeyboardBinding, KeyCapturingButton>> {
// 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()
return KeyboardBinding.values().asSequence()
.filterNot { it.hidden }
.groupBy { it.category } // Materializes a Map<Category,List<KeyboardBinding>>
.asSequence()
.map { (category, bindings) ->
category to bindings.asSequence()
.sortedWith(compareBy(collator) { it.label.tr() })
.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())
.toMap(LinkedHashMap()) // Stuff into a map that preserves our sorted order
}
.sortedBy { it.first.name.tr() }
.sortedWith(compareBy(collator) { it.first.label.tr() }) // sort the categories
.toMap(LinkedHashMap())
}