Skip to content

Commit

Permalink
Consider the time needed for calculations in the popup delay
Browse files Browse the repository at this point in the history
Subtract the time needed for calculations of the nested mappings and
available width etc. from the popup delay. We do this to provide a
consistent delay time independent from the time needed for everything
else.

Especially since the first calculations tend to need more time than
the following ones this makes the overall experience more consistent.
  • Loading branch information
TheBlob42 committed Feb 27, 2021
1 parent 55d9862 commit 5ced4b7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

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

Expand Down
17 changes: 13 additions & 4 deletions src/main/kotlin/eu/theblob42/idea/whichkey/config/PopupConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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<KeyStroke>, nestedMappings: List<Pair<String, Mapping>>) {
fun showPopup(ideFrame: JFrame, typedKeys: List<KeyStroke>, nestedMappings: List<Pair<String, Mapping>>, startTime: Long) {
if (nestedMappings.isEmpty()) {
return
}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 5ced4b7

Please sign in to comment.