Browse Source

Delay scroll popup menu container initialisation.

pull/187/head
weisj 4 years ago
parent
commit
92edb52b41
  1. 15
      core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/DarkPopupMenuUI.java
  2. 41
      core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/PopupMenuContainer.java

15
core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/DarkPopupMenuUI.java

@ -80,13 +80,17 @@ public class DarkPopupMenuUI extends BasicPopupMenuUI {
@Override
public void installDefaults() {
if (!(popupMenu instanceof ScrollPopupMenu)) {
popupMenuContainer = new PopupMenuContainer();
}
super.installDefaults();
popupMenu.putClientProperty(DarkPopupFactory.KEY_START_HIDDEN, true);
}
private PopupMenuContainer getPopupMenuContainer() {
if (popupMenuContainer == null && !(popupMenu instanceof ScrollPopupMenu)) {
popupMenuContainer = new PopupMenuContainer();
}
return popupMenuContainer;
}
@Override
protected void installListeners() {
super.installListeners();
@ -95,8 +99,9 @@ public class DarkPopupMenuUI extends BasicPopupMenuUI {
@Override
public Popup getPopup(final JPopupMenu popup, final int x, final int y) {
if (popupMenuContainer == null) return super.getPopup(popup, x, y);
PopupMenuContainer container = getPopupMenuContainer();
if (container == null) return super.getPopup(popup, x, y);
int maxHeight = DarkUIUtil.getScreenBounds(popup, x, y, false).height;
return popupMenuContainer.createPopup(popup, x, y, maxHeight);
return container.createPopup(popup, x, y, maxHeight);
}
}

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

@ -39,18 +39,28 @@ import com.github.weisj.darklaf.util.DarkUIUtil;
public class PopupMenuContainer extends JPanel {
private final JScrollPane scrollPane;
private final MenuKeyListener menuKeyListener;
private final PopupMenuListener menuListener;
private final JPanel view;
private JScrollPane scrollPane;
private JPanel view;
private MenuKeyListener menuKeyListener;
private PopupMenuListener menuListener;
private JPopupMenu popupMenu;
public PopupMenuContainer() {
super(new BorderLayout());
}
private void initComponents() {
if (view == null) {
view = new JPanel(new BorderLayout());
OverlayScrollPane overlayScrollPane = createScrollPane(view);
scrollPane = overlayScrollPane.getScrollPane();
add(overlayScrollPane, BorderLayout.CENTER);
}
}
protected MenuKeyListener getMenuKeyListener() {
if (menuKeyListener == null) {
menuKeyListener = new MenuKeyListener() {
@Override
public void menuKeyTyped(final MenuKeyEvent e) {}
@ -72,6 +82,12 @@ public class PopupMenuContainer extends JPanel {
@Override
public void menuKeyReleased(final MenuKeyEvent e) {}
};
}
return menuKeyListener;
}
protected PopupMenuListener getMenuListener() {
if (menuListener == null) {
menuListener = new PopupMenuAdapter() {
@Override
public void popupMenuWillBecomeInvisible(final PopupMenuEvent e) {
@ -90,18 +106,22 @@ public class PopupMenuContainer extends JPanel {
}
};
}
return menuListener;
}
public void setPopupMenu(final JPopupMenu popupMenu) {
MenuKeyListener keyListener = getMenuKeyListener();
PopupMenuListener popupMenuListener = getMenuListener();
if (this.popupMenu != null) {
this.popupMenu.removeMenuKeyListener(menuKeyListener);
this.popupMenu.removePopupMenuListener(menuListener);
this.popupMenu.removeMenuKeyListener(keyListener);
this.popupMenu.removePopupMenuListener(popupMenuListener);
}
this.popupMenu = popupMenu;
if (popupMenu != null) {
popupMenu.removeMenuKeyListener(menuKeyListener);
popupMenu.removePopupMenuListener(menuListener);
popupMenu.addMenuKeyListener(menuKeyListener);
popupMenu.addPopupMenuListener(menuListener);
popupMenu.removeMenuKeyListener(keyListener);
popupMenu.removePopupMenuListener(popupMenuListener);
popupMenu.addMenuKeyListener(keyListener);
popupMenu.addPopupMenuListener(popupMenuListener);
}
}
@ -113,6 +133,7 @@ public class PopupMenuContainer extends JPanel {
popupMenu.setBorderPainted(true);
return PopupFactory.getSharedInstance().getPopup(popupMenu.getInvoker(), popupMenu, posX, posY);
} else {
initComponents();
int increment = 1;
if (popupMenu.getComponentCount() > 0) {
increment = Math.max(1, popupMenu.getComponent(0).getPreferredSize().height / 2);

Loading…
Cancel
Save