diff --git a/forge-gui-desktop/src/main/java/forge/gui/MenuScroller.java b/forge-gui-desktop/src/main/java/forge/gui/MenuScroller.java index b20fb3261f3..17f7ab323d6 100644 --- a/forge-gui-desktop/src/main/java/forge/gui/MenuScroller.java +++ b/forge-gui-desktop/src/main/java/forge/gui/MenuScroller.java @@ -12,6 +12,7 @@ import java.awt.Point; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelListener; @@ -334,7 +335,7 @@ public class MenuScroller { * * @return the number of items to display at a time */ - public int getscrollCount() { + public int getScrollCount() { return scrollCount; } @@ -603,10 +604,20 @@ public class MenuScroller { } private class MouseScrollListener implements MouseWheelListener { - public void mouseWheelMoved(MouseWheelEvent mwe){ - firstIndex += mwe.getWheelRotation(); - refreshMenu(); + public void mouseWheelMoved(MouseWheelEvent mwe) { + int rot = mwe.getWheelRotation(); + if (rot == 0) { + return; + } + // anything to scroll? otherwise select items directly + if (menu.getComponentCount() <= scrollCount + topFixedCount + bottomFixedCount) { + setMenuSelectedIndex(menu, Math.abs(rot), rot < 0); + return; + } + + firstIndex += rot; mwe.consume(); + refreshMenu(); } } @@ -631,4 +642,14 @@ public class MenuScroller { return scrollCount; } + + public static void setMenuSelectedIndex(final JPopupMenu menu, final int index, boolean scrollUp) { + SwingUtilities.invokeLater(new Runnable() { //use invoke later to ensure first enabled item selected by default + public void run() { + for (int i = 0; i < index; i++) { + menu.dispatchEvent(new KeyEvent(menu, KeyEvent.KEY_PRESSED, 0, 0, scrollUp ? KeyEvent.VK_UP : KeyEvent.VK_DOWN, KeyEvent.CHAR_UNDEFINED)); + } + } + }); + } } diff --git a/forge-gui-desktop/src/main/java/forge/screens/match/CMatchUI.java b/forge-gui-desktop/src/main/java/forge/screens/match/CMatchUI.java index 8071e9b35da..01ebb5a7c96 100644 --- a/forge-gui-desktop/src/main/java/forge/screens/match/CMatchUI.java +++ b/forge-gui-desktop/src/main/java/forge/screens/match/CMatchUI.java @@ -35,7 +35,6 @@ import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.KeyStroke; -import javax.swing.SwingUtilities; import javax.swing.event.PopupMenuEvent; import javax.swing.event.PopupMenuListener; @@ -918,9 +917,6 @@ public final class CMatchUI //show menu if mouse was trigger for ability final JPopupMenu menu = new JPopupMenu(Localizer.getInstance().getMessage("lblAbilities")); - //add scroll area when too big - // TODO: do we need a user setting for the scrollCount? - MenuScroller.setScrollerFor(menu, 8, 125, 3, 1); boolean enabled; int firstEnabled = -1; @@ -949,6 +945,10 @@ public final class CMatchUI } if (firstEnabled >= 0) { //only show menu if at least one ability can be played + //add scroll area when too big + // TODO: do we need a user setting for the scrollCount? + MenuScroller.setScrollerFor(menu, 8, 125, 3, 1); + final CardPanel panel = findCardPanel(hostCard); final Component menuParent; final int x, y; @@ -983,14 +983,8 @@ public final class CMatchUI menu.show(menuParent, x, y); openAbilityMenu = menu; - final int _firstEnabled = firstEnabled; - SwingUtilities.invokeLater(new Runnable() { //use invoke later to ensure first enabled ability selected by default - @Override public final void run() { - for (int i = 0; i <= _firstEnabled; i++) { - menu.dispatchEvent(new KeyEvent(menu, KeyEvent.KEY_PRESSED, 0, 0, KeyEvent.VK_DOWN, KeyEvent.CHAR_UNDEFINED)); - } - } - }); + // TODO seems 1 would now always lead to the first enabled one? + MenuScroller.setMenuSelectedIndex(menu, firstEnabled, false); } return null; //delay ability until choice made