Browse Source

Avoid installing listeners on popup menus that don't need them.

pull/187/head
weisj 4 years ago
parent
commit
3b16fc65a7
  1. 14
      core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/PopupMenuContainer.java

14
core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/PopupMenuContainer.java

@ -109,13 +109,16 @@ public class PopupMenuContainer extends JPanel {
return menuListener; return menuListener;
} }
protected void uninstallListeners() {
if (this.popupMenu != null) {
this.popupMenu.removeMenuKeyListener(menuKeyListener);
this.popupMenu.removePopupMenuListener(menuListener);
}
}
public void setPopupMenu(final JPopupMenu popupMenu) { public void setPopupMenu(final JPopupMenu popupMenu) {
MenuKeyListener keyListener = getMenuKeyListener(); MenuKeyListener keyListener = getMenuKeyListener();
PopupMenuListener popupMenuListener = getMenuListener(); PopupMenuListener popupMenuListener = getMenuListener();
if (this.popupMenu != null) {
this.popupMenu.removeMenuKeyListener(keyListener);
this.popupMenu.removePopupMenuListener(popupMenuListener);
}
this.popupMenu = popupMenu; this.popupMenu = popupMenu;
if (popupMenu != null) { if (popupMenu != null) {
popupMenu.removeMenuKeyListener(keyListener); popupMenu.removeMenuKeyListener(keyListener);
@ -126,14 +129,15 @@ public class PopupMenuContainer extends JPanel {
} }
public Popup createPopup(final JPopupMenu popupMenu, final int posX, final int posY, final int maxHeight) { public Popup createPopup(final JPopupMenu popupMenu, final int posX, final int posY, final int maxHeight) {
setPopupMenu(popupMenu);
final Dimension prefSize = popupMenu.getPreferredSize(); final Dimension prefSize = popupMenu.getPreferredSize();
uninstallListeners();
if (maxHeight <= 0 || prefSize.height <= maxHeight) { if (maxHeight <= 0 || prefSize.height <= maxHeight) {
setBounds(0, 0, prefSize.width, prefSize.height); setBounds(0, 0, prefSize.width, prefSize.height);
popupMenu.setBorderPainted(true); popupMenu.setBorderPainted(true);
return PopupFactory.getSharedInstance().getPopup(popupMenu.getInvoker(), popupMenu, posX, posY); return PopupFactory.getSharedInstance().getPopup(popupMenu.getInvoker(), popupMenu, posX, posY);
} else { } else {
initComponents(); initComponents();
setPopupMenu(popupMenu);
int increment = 1; int increment = 1;
if (popupMenu.getComponentCount() > 0) { if (popupMenu.getComponentCount() > 0) {
increment = Math.max(1, popupMenu.getComponent(0).getPreferredSize().height / 2); increment = Math.max(1, popupMenu.getComponent(0).getPreferredSize().height / 2);

Loading…
Cancel
Save