Improve MouseWheel support in play choice popup

This commit is contained in:
tool4EvEr
2022-11-12 17:18:04 +01:00
parent feec07bc35
commit 5dc7a22228
2 changed files with 31 additions and 16 deletions

View File

@@ -12,6 +12,7 @@ import java.awt.Point;
import java.awt.Toolkit; import java.awt.Toolkit;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener; import java.awt.event.MouseWheelListener;
@@ -334,7 +335,7 @@ public class MenuScroller {
* *
* @return the number of items to display at a time * @return the number of items to display at a time
*/ */
public int getscrollCount() { public int getScrollCount() {
return scrollCount; return scrollCount;
} }
@@ -603,10 +604,20 @@ public class MenuScroller {
} }
private class MouseScrollListener implements MouseWheelListener { private class MouseScrollListener implements MouseWheelListener {
public void mouseWheelMoved(MouseWheelEvent mwe){ public void mouseWheelMoved(MouseWheelEvent mwe) {
firstIndex += mwe.getWheelRotation(); int rot = mwe.getWheelRotation();
refreshMenu(); 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(); mwe.consume();
refreshMenu();
} }
} }
@@ -631,4 +642,14 @@ public class MenuScroller {
return scrollCount; 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));
}
}
});
}
} }

View File

@@ -35,7 +35,6 @@ import javax.swing.JOptionPane;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JPopupMenu; import javax.swing.JPopupMenu;
import javax.swing.KeyStroke; import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.event.PopupMenuEvent; import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener; import javax.swing.event.PopupMenuListener;
@@ -918,9 +917,6 @@ public final class CMatchUI
//show menu if mouse was trigger for ability //show menu if mouse was trigger for ability
final JPopupMenu menu = new JPopupMenu(Localizer.getInstance().getMessage("lblAbilities")); 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; boolean enabled;
int firstEnabled = -1; 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 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 CardPanel panel = findCardPanel(hostCard);
final Component menuParent; final Component menuParent;
final int x, y; final int x, y;
@@ -983,14 +983,8 @@ public final class CMatchUI
menu.show(menuParent, x, y); menu.show(menuParent, x, y);
openAbilityMenu = menu; openAbilityMenu = menu;
final int _firstEnabled = firstEnabled; // TODO seems 1 would now always lead to the first enabled one?
SwingUtilities.invokeLater(new Runnable() { //use invoke later to ensure first enabled ability selected by default MenuScroller.setMenuSelectedIndex(menu, firstEnabled, false);
@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));
}
}
});
} }
return null; //delay ability until choice made return null; //delay ability until choice made