From a325f111c91ec64c0bb0319dedd18d36f4b720c9 Mon Sep 17 00:00:00 2001 From: Kevin Lamonte Date: Sat, 8 Jul 2017 21:20:09 -0400 Subject: [PATCH] TButton animate for keyboard --- src/jexer/TButton.java | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/src/jexer/TButton.java b/src/jexer/TButton.java index 1c995c4..fc93c78 100644 --- a/src/jexer/TButton.java +++ b/src/jexer/TButton.java @@ -73,13 +73,27 @@ public final class TButton extends TWidget { */ private TAction action; + /** + * The time at which dispatch() was called. + */ + private long dispatchTime; + + /** + * How long to animate dispatch of the event in millis. + */ + private static final long DISPATCH_TIME = 75; + /** * Act as though the button was pressed. This is useful for other UI * elements to get the same action as if the user clicked the button. */ public void dispatch() { if (action != null) { - action.DO(); + long now = System.currentTimeMillis(); + if (now - dispatchTime > DISPATCH_TIME) { + action.DO(); + dispatchTime = now; + } } } @@ -153,6 +167,12 @@ public final class TButton extends TWidget { shadowColor.setForeColor(Color.BLACK); shadowColor.setBold(false); + long now = System.currentTimeMillis(); + boolean inDispatch = false; + if (now - dispatchTime < DISPATCH_TIME) { + inDispatch = true; + } + if (!isEnabled()) { buttonColor = getTheme().getColor("tbutton.disabled"); menuMnemonicColor = getTheme().getColor("tbutton.disabled"); @@ -164,7 +184,7 @@ public final class TButton extends TWidget { menuMnemonicColor = getTheme().getColor("tbutton.mnemonic"); } - if (inButtonPress) { + if (inButtonPress || inDispatch) { getScreen().putCharXY(1, 0, ' ', buttonColor); getScreen().putStringXY(2, 0, mnemonic.getRawLabel(), buttonColor); getScreen().putCharXY(getWidth() - 1, 0, ' ', buttonColor); @@ -179,7 +199,7 @@ public final class TButton extends TWidget { GraphicsChars.CP437[0xDF], shadowColor); } if (mnemonic.getShortcutIdx() >= 0) { - if (inButtonPress) { + if (inButtonPress || inDispatch) { getScreen().putCharXY(2 + mnemonic.getShortcutIdx(), 0, mnemonic.getShortcut(), menuMnemonicColor); } else { @@ -217,9 +237,7 @@ public final class TButton extends TWidget { if (inButtonPress && mouse.isMouse1()) { inButtonPress = false; // Dispatch the event - if (action != null) { - action.DO(); - } + dispatch(); } } @@ -249,9 +267,7 @@ public final class TButton extends TWidget { || keypress.equals(kbSpace) ) { // Dispatch - if (action != null) { - action.DO(); - } + dispatch(); return; } -- 2.27.0