Skip to content

Commit

Permalink
Add alphanumeric mnemonics in popup
Browse files Browse the repository at this point in the history
While investigating #482, I found that I could add more mnemonics, which is kinda cool. So I did.
  • Loading branch information
FWDekker committed Oct 22, 2023
1 parent 941c214 commit 6ac1221
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 29 deletions.
58 changes: 37 additions & 21 deletions src/main/kotlin/com/fwdekker/randomness/PopupAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}


Expand Down Expand Up @@ -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
)
)
)
}
)
}
}
)
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
}
}

0 comments on commit 6ac1221

Please sign in to comment.