mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2025-12-06 22:10:54 +03:00
PopupMenu: make sure that popup menu does not overlap any operating system task bar (issue #701)
This commit is contained in:
@@ -38,6 +38,8 @@ FlatLaf Change Log
|
|||||||
- FormattedTextField: On Linux, fixed `IllegalArgumentException: Invalid
|
- FormattedTextField: On Linux, fixed `IllegalArgumentException: Invalid
|
||||||
location` if `JFormattedTextField.setDocument()` is invoked in a focus gained
|
location` if `JFormattedTextField.setDocument()` is invoked in a focus gained
|
||||||
listener on that formatted text field. (issue #698)
|
listener on that formatted text field. (issue #698)
|
||||||
|
- PopupMenu: Make sure that popup menu does not overlap any operating system
|
||||||
|
task bar. (issue #701)
|
||||||
|
|
||||||
#### Incompatibilities
|
#### Incompatibilities
|
||||||
|
|
||||||
|
|||||||
@@ -193,27 +193,38 @@ public class FlatPopupMenuUI
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Popup getPopup( JPopupMenu popup, int x, int y ) {
|
public Popup getPopup( JPopupMenu popup, int x, int y ) {
|
||||||
|
Dimension popupSize = popup.getPreferredSize();
|
||||||
|
Rectangle screenBounds = getScreenBoundsAt( x, y );
|
||||||
|
|
||||||
|
// make sure that popup does not overlap any task/side bar
|
||||||
|
if( x + popupSize.width > screenBounds.x + screenBounds.width )
|
||||||
|
x = screenBounds.x + screenBounds.width - popupSize.width;
|
||||||
|
if( y + popupSize.height > screenBounds.y + screenBounds.height )
|
||||||
|
y = screenBounds.y + screenBounds.height - popupSize.height;
|
||||||
|
if( x < screenBounds.x )
|
||||||
|
x = screenBounds.x;
|
||||||
|
if( y < screenBounds.y )
|
||||||
|
y = screenBounds.y;
|
||||||
|
|
||||||
// do not add scroller to combobox popups or to popups that already have a scroll pane
|
// do not add scroller to combobox popups or to popups that already have a scroll pane
|
||||||
if( popup instanceof BasicComboPopup ||
|
if( popup instanceof BasicComboPopup ||
|
||||||
(popup.getComponentCount() > 0 && popup.getComponent( 0 ) instanceof JScrollPane) )
|
(popup.getComponentCount() > 0 && popup.getComponent( 0 ) instanceof JScrollPane) )
|
||||||
return super.getPopup( popup, x, y );
|
return super.getPopup( popup, x, y );
|
||||||
|
|
||||||
// do not add scroller if popup fits into screen
|
// do not add scroller if popup fits into screen
|
||||||
Dimension prefSize = popup.getPreferredSize();
|
if( popupSize.height <= screenBounds.height )
|
||||||
int screenHeight = getScreenHeightAt( x, y );
|
|
||||||
if( prefSize.height <= screenHeight )
|
|
||||||
return super.getPopup( popup, x, y );
|
return super.getPopup( popup, x, y );
|
||||||
|
|
||||||
// create scroller
|
// create scroller
|
||||||
FlatPopupScroller scroller = new FlatPopupScroller( popup );
|
FlatPopupScroller scroller = new FlatPopupScroller( popup );
|
||||||
scroller.setPreferredSize( new Dimension( prefSize.width, screenHeight ) );
|
scroller.setPreferredSize( new Dimension( popupSize.width, screenBounds.height ) );
|
||||||
|
|
||||||
// create popup
|
// create popup
|
||||||
PopupFactory popupFactory = PopupFactory.getSharedInstance();
|
PopupFactory popupFactory = PopupFactory.getSharedInstance();
|
||||||
return popupFactory.getPopup( popup.getInvoker(), scroller, x, y );
|
return popupFactory.getPopup( popup.getInvoker(), scroller, x, y );
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getScreenHeightAt( int x, int y ) {
|
private Rectangle getScreenBoundsAt( int x, int y ) {
|
||||||
// find GraphicsConfiguration at popup location (similar to JPopupMenu.getCurrentGraphicsConfiguration())
|
// find GraphicsConfiguration at popup location (similar to JPopupMenu.getCurrentGraphicsConfiguration())
|
||||||
GraphicsConfiguration gc = null;
|
GraphicsConfiguration gc = null;
|
||||||
for( GraphicsDevice device : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices() ) {
|
for( GraphicsDevice device : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices() ) {
|
||||||
@@ -234,7 +245,7 @@ public class FlatPopupMenuUI
|
|||||||
Toolkit toolkit = Toolkit.getDefaultToolkit();
|
Toolkit toolkit = Toolkit.getDefaultToolkit();
|
||||||
Rectangle screenBounds = (gc != null) ? gc.getBounds() : new Rectangle( toolkit.getScreenSize() );
|
Rectangle screenBounds = (gc != null) ? gc.getBounds() : new Rectangle( toolkit.getScreenSize() );
|
||||||
Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets( gc );
|
Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets( gc );
|
||||||
return screenBounds.height - screenInsets.top - screenInsets.bottom;
|
return FlatUIUtils.subtractInsets( screenBounds, screenInsets );
|
||||||
}
|
}
|
||||||
|
|
||||||
//---- class FlatPopupMenuLayout ------------------------------------------
|
//---- class FlatPopupMenuLayout ------------------------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user