diff --git a/src/main/kotlin/com/fwdekker/randomness/PopupAction.kt b/src/main/kotlin/com/fwdekker/randomness/PopupAction.kt index a253b04a7..2938349f8 100644 --- a/src/main/kotlin/com/fwdekker/randomness/PopupAction.kt +++ b/src/main/kotlin/com/fwdekker/randomness/PopupAction.kt @@ -54,7 +54,7 @@ class PopupAction : AnAction(Icons.RANDOMNESS) { val popup = JBPopupFactory.getInstance() .createActionGroupPopup( Bundle("popup.title"), popupGroup, event.dataContext, - JBPopupFactory.ActionSelectionAid.NUMBERING, + JBPopupFactory.ActionSelectionAid.ALPHA_NUMBERING, true ) as ListPopupImpl @@ -125,6 +125,20 @@ class PopupAction : AnAction(Icons.RANDOMNESS) { Separator() + TemplateSettingsAction() } + + + /** + * Holds constants. + */ + companion object { + /** + * Returns the mnemonic for the entry at the [index]th row. + * + * @see JBPopupFactory.ActionSelectionAid.ALPHA_NUMBERING + */ + fun indexToMnemonic(index: Int) = + (('1'..'9') + '0' + ('A'..'Z')).getOrNull(index) + } } @@ -201,26 +215,28 @@ fun ListPopupImpl.registerModifierActions(captionModifier: (ActionEvent?) -> Str } ) - @Suppress("detekt:MagicNumber") // Not worth a constant - for (key in 0..9) { - registerAction( - "${a}${b}${c}invokeAction$key", - KeyStroke.getKeyStroke("$a $b $c pressed $key"), - SimpleAbstractAction { event -> - event ?: return@SimpleAbstractAction - - val targetRow = if (key == 0) 9 else key - 1 - list.addSelectionInterval(targetRow, targetRow) - handleSelect( - true, - KeyEvent( - component, - event.id, event.getWhen(), event.modifiers, - KeyEvent.VK_ENTER, KeyEvent.CHAR_UNDEFINED, KeyEvent.KEY_LOCATION_UNKNOWN + @Suppress("detekt:ForEachOnRange") // Not relevant for such small numbers + (0 until list.model.size) + .associateWith { PopupAction.indexToMnemonic(it) } + .filterValues { it != null } + .forEach { (index, mnemonic) -> + registerAction( + "${a}${b}${c}invokeAction$mnemonic", + KeyStroke.getKeyStroke("$a $b $c pressed $mnemonic"), + SimpleAbstractAction { event -> + event ?: return@SimpleAbstractAction + + list.addSelectionInterval(index, index) + handleSelect( + true, + KeyEvent( + component, + event.id, event.getWhen(), event.modifiers, + KeyEvent.VK_ENTER, KeyEvent.CHAR_UNDEFINED, KeyEvent.KEY_LOCATION_UNKNOWN + ) ) - ) - } - ) - } + } + ) + } } } diff --git a/src/main/kotlin/com/fwdekker/randomness/template/TemplateJTree.kt b/src/main/kotlin/com/fwdekker/randomness/template/TemplateJTree.kt index 59b322bf6..5cb02fd0e 100644 --- a/src/main/kotlin/com/fwdekker/randomness/template/TemplateJTree.kt +++ b/src/main/kotlin/com/fwdekker/randomness/template/TemplateJTree.kt @@ -1,6 +1,7 @@ package com.fwdekker.randomness.template import com.fwdekker.randomness.Bundle +import com.fwdekker.randomness.PopupAction import com.fwdekker.randomness.Scheme import com.fwdekker.randomness.datetime.DateTimeScheme import com.fwdekker.randomness.decimal.DecimalScheme @@ -386,9 +387,8 @@ class TemplateJTree( icon = scheme.icon if (scheme is Template) { - val index = currentTemplateList.templates.indexOf(scheme) + 1 - if (index <= INDEXED_TEMPLATE_COUNT) - append("${index % INDEXED_TEMPLATE_COUNT} ", SimpleTextAttributes.GRAYED_SMALL_ATTRIBUTES, false) + PopupAction.indexToMnemonic(currentTemplateList.templates.indexOf(scheme)) + ?.run { append("$this ", SimpleTextAttributes.GRAYED_SMALL_ATTRIBUTES, false) } } append( @@ -691,10 +691,5 @@ class TemplateJTree( DateTimeScheme(), TemplateReference(), ) - - /** - * Number of [Template]s that should be rendered with the index in front. - */ - const val INDEXED_TEMPLATE_COUNT = 10 } }