diff --git a/core/src/main/java/com/github/weisj/darklaf/components/ScrollPopupMenu.java b/core/src/main/java/com/github/weisj/darklaf/components/ScrollPopupMenu.java index 79cabf2a..ceab8233 100644 --- a/core/src/main/java/com/github/weisj/darklaf/components/ScrollPopupMenu.java +++ b/core/src/main/java/com/github/weisj/darklaf/components/ScrollPopupMenu.java @@ -119,7 +119,7 @@ public class ScrollPopupMenu extends JPopupMenu { } private Popup createPopup() { - return popupMenuContainer.createPopup(this, posX, posY, maxHeight); + return popupMenuContainer.createPopup(this, posX, posY, -1, maxHeight); } @Override diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/DarkPopupMenuUI.java b/core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/DarkPopupMenuUI.java index 723cb73f..4e280874 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/DarkPopupMenuUI.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/DarkPopupMenuUI.java @@ -33,6 +33,7 @@ import javax.swing.plaf.basic.BasicPopupMenuUI; import com.github.weisj.darklaf.components.ScrollPopupMenu; import com.github.weisj.darklaf.ui.DarkPopupFactory; import com.github.weisj.darklaf.util.DarkUIUtil; +import com.github.weisj.darklaf.util.PropertyUtil; /** * This implementation for PopupMenuUI is almost identical to the one of BasicPopupMenuUI. The key @@ -48,6 +49,7 @@ public class DarkPopupMenuUI extends BasicPopupMenuUI { public static final String KEY_DO_NOT_CANCEL_ON_SCROLL = "doNotCancelOnScroll"; public static final String HIDE_POPUP_VALUE = "doNotCancelPopup"; public static final String KEY_DEFAULT_LIGHTWEIGHT_POPUPS = "PopupMenu.defaultLightWeightPopups"; + public static final String KEY_MAX_POPUP_SIZE = "maxPopupSize"; private PopupMenuContainer popupMenuContainer; public static ComponentUI createUI(final JComponent x) { @@ -103,7 +105,18 @@ public class DarkPopupMenuUI extends BasicPopupMenuUI { public Popup getPopup(final JPopupMenu popup, final int x, final int y) { PopupMenuContainer container = getPopupMenuContainer(); if (container == null) return super.getPopup(popup, x, y); - int maxHeight = DarkUIUtil.getScreenBounds(popup, x, y, false).height; - return container.createPopup(popup, x, y, maxHeight); + Dimension maxSize = PropertyUtil.getObject(popup, KEY_MAX_POPUP_SIZE, Dimension.class); + int maxHeight = -1; + int maxWidth = -1; + if (maxSize != null) { + maxHeight = maxSize.height; + maxWidth = maxSize.width; + } + if (maxHeight <= 0 || maxWidth <= 0) { + Rectangle screenSize = DarkUIUtil.getScreenBounds(popup, x, y, false); + if (maxHeight <= 0) maxHeight = screenSize.height; + if (maxWidth <= 0) maxWidth = screenSize.width; + } + return container.createPopup(popup, x, y, maxWidth, maxHeight); } } diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/PopupMenuContainer.java b/core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/PopupMenuContainer.java index f8aa3d62..9dbb3d7c 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/PopupMenuContainer.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/PopupMenuContainer.java @@ -125,10 +125,13 @@ 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 maxWidth, final int maxHeight) { final Dimension prefSize = popupMenu.getPreferredSize(); uninstallListeners(); - if (maxHeight <= 0 || prefSize.height <= maxHeight) { + boolean exceedsHorizontalSize = (maxHeight > 0 && prefSize.height > maxHeight); + boolean exceedsVerticalSize = (maxWidth > 0 && prefSize.width > maxWidth); + if (!exceedsHorizontalSize && !exceedsVerticalSize) { setBounds(0, 0, prefSize.width, prefSize.height); popupMenu.setBorderPainted(true); return PopupFactory.getSharedInstance().getPopup(popupMenu.getInvoker(), popupMenu, posX, posY); @@ -139,14 +142,24 @@ public class PopupMenuContainer extends JPanel { if (popupMenu.getComponentCount() > 0) { increment = Math.max(1, popupMenu.getComponent(0).getPreferredSize().height / 2); } - JScrollBar bar = scrollPane.getVerticalScrollBar(); - bar.setValue(bar.getMinimum()); - bar.setUnitIncrement(increment); + JScrollBar verticalBar = scrollPane.getVerticalScrollBar(); + verticalBar.setValue(verticalBar.getMinimum()); + verticalBar.setUnitIncrement(increment); + + JScrollBar horizontalBar = scrollPane.getHorizontalScrollBar(); + horizontalBar.setValue(horizontalBar.getMinimum()); + horizontalBar.setUnitIncrement(increment); + view.removeAll(); view.add(popupMenu, BorderLayout.CENTER); setBorder(popupMenu.getBorder()); popupMenu.setBorderPainted(false); - setPreferredSize(new Dimension(prefSize.width + bar.getPreferredSize().width, maxHeight)); + Dimension constraintSize = new Dimension(prefSize); + if (exceedsHorizontalSize) constraintSize.width += verticalBar.getPreferredSize().width; + if (exceedsVerticalSize) constraintSize.height += horizontalBar.getPreferredSize().height; + if (maxWidth > 0) constraintSize.width = Math.min(constraintSize.width, maxWidth); + if (maxHeight > 0) constraintSize.height = Math.min(constraintSize.height, maxHeight); + setPreferredSize(constraintSize); return PopupFactory.getSharedInstance().getPopup(popupMenu.getInvoker(), this, posX, posY); } }