diff --git a/CHANGELOG.md b/CHANGELOG.md index 312dc8f..cf75adf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ ### Changed +- Subtract the time needed for calculations from the popup delay - Arguments for VIM motions (e.g. f, t, F, T) do not trigger the popup anymore ## 0.2 - 2021-02-17 diff --git a/src/main/kotlin/eu/theblob42/idea/whichkey/WhichKeyShortcutKeyAction.kt b/src/main/kotlin/eu/theblob42/idea/whichkey/WhichKeyShortcutKeyAction.kt index a7604c9..d60e34c 100644 --- a/src/main/kotlin/eu/theblob42/idea/whichkey/WhichKeyShortcutKeyAction.kt +++ b/src/main/kotlin/eu/theblob42/idea/whichkey/WhichKeyShortcutKeyAction.kt @@ -24,12 +24,14 @@ class WhichKeyShortcutKeyAction: AnAction(), DumbAware { if (editor != null) { val inputEvent = actionEvent.inputEvent if (inputEvent is KeyEvent) { + val startTime = System.currentTimeMillis() // save start time for the popup delay + val mappingState = CommandState.getInstance(editor).mappingState val typedKeySequence = mappingState.keys + listOf(KeyStroke.getKeyStroke(inputEvent.keyCode, inputEvent.modifiers)) val nestedMappings = MappingConfig.getNestedMappings(mappingState.mappingMode, typedKeySequence) val window = WindowManager.getInstance().getFrame(editor.project) - PopupConfig.showPopup(window!!, typedKeySequence, nestedMappings) + PopupConfig.showPopup(window!!, typedKeySequence, nestedMappings, startTime) } } diff --git a/src/main/kotlin/eu/theblob42/idea/whichkey/WhichKeyTypeActionHandler.kt b/src/main/kotlin/eu/theblob42/idea/whichkey/WhichKeyTypeActionHandler.kt index 66c7cf4..6277232 100644 --- a/src/main/kotlin/eu/theblob42/idea/whichkey/WhichKeyTypeActionHandler.kt +++ b/src/main/kotlin/eu/theblob42/idea/whichkey/WhichKeyTypeActionHandler.kt @@ -32,11 +32,13 @@ class WhichKeyTypeActionHandler(private val vimTypedActionHandler: VimTypedActio if (commandState.commandBuilder.expectedArgumentType != Argument.Type.DIGRAPH) { val ideFrame = WindowManager.getInstance().getFrame(editor.project) if (ideFrame != null) { + val startTime = System.currentTimeMillis() // save start time for the popup delay + val mappingState = commandState.mappingState val typedKeySequence = mappingState.keys + listOf(KeyStroke.getKeyStroke(charTyped)) val nestedMappings = MappingConfig.getNestedMappings(mappingState.mappingMode, typedKeySequence) - PopupConfig.showPopup(ideFrame, typedKeySequence, nestedMappings) + PopupConfig.showPopup(ideFrame, typedKeySequence, nestedMappings, startTime) } } diff --git a/src/main/kotlin/eu/theblob42/idea/whichkey/config/PopupConfig.kt b/src/main/kotlin/eu/theblob42/idea/whichkey/config/PopupConfig.kt index eb742cf..5e9c634 100644 --- a/src/main/kotlin/eu/theblob42/idea/whichkey/config/PopupConfig.kt +++ b/src/main/kotlin/eu/theblob42/idea/whichkey/config/PopupConfig.kt @@ -15,6 +15,8 @@ import kotlin.math.ceil object PopupConfig { + private const val DEFAULT_POPUP_DELAY = 200 + private var currentBalloon: Balloon? = null private var displayBalloonJob: Job? = null @@ -42,8 +44,9 @@ object PopupConfig { * @param ideFrame The [JFrame] to attach the popup to * @param typedKeys The already typed key stroke sequence * @param nestedMappings A [List] of nested mappings to display + * @param startTime Timestamp to consider for the calculation of the popup delay */ - fun showPopup(ideFrame: JFrame, typedKeys: List, nestedMappings: List>) { + fun showPopup(ideFrame: JFrame, typedKeys: List, nestedMappings: List>, startTime: Long) { if (nestedMappings.isEmpty()) { return } @@ -117,10 +120,16 @@ object PopupConfig { .setFadeoutTime(fadeoutTime) .createBalloon() - // wait for a few ms before showing the Balloon to prevent - // flickering on fast consecutive key presses + /* + * wait for a few ms before showing the Balloon to prevent flickering on fast consecutive key presses + * subtract the already passed time (for calculations etc.) to make the delay as consistent as possible + */ + val delay = (DEFAULT_POPUP_DELAY - (System.currentTimeMillis() - startTime)).let { + if (it < 0) 0 else it + } + displayBalloonJob = GlobalScope.launch { - delay(200) + delay(delay) newWhichKeyBalloon.show(target, Balloon.Position.above) currentBalloon = newWhichKeyBalloon }