Window decorations: fixed wrong window placement when moving window to another screen with different scaling factor (issue #166)

This commit is contained in:
Karl Tauber
2020-09-03 19:26:52 +02:00
parent c6beb9dc0a
commit 02473080a5
2 changed files with 29 additions and 24 deletions

View File

@@ -8,6 +8,11 @@ FlatLaf Change Log
- Demo: Improved "SplitPane & Tabs" and "Data Components" tabs. - Demo: Improved "SplitPane & Tabs" and "Data Components" tabs.
- Menu items "File > Open" and "File > Save As" now show file choosers. - Menu items "File > Open" and "File > Save As" now show file choosers.
#### Fixed bugs
- Custom window decorations: Fixed wrong window placement when moving window to
another screen with different scaling factor. (issue #166)
## 0.41 ## 0.41

View File

@@ -754,8 +754,7 @@ debug*/
//---- interface MouseListener ---- //---- interface MouseListener ----
private int lastXOnScreen; private Point dragOffset;
private int lastYOnScreen;
@Override @Override
public void mouseClicked( MouseEvent e ) { public void mouseClicked( MouseEvent e ) {
@@ -779,8 +778,10 @@ debug*/
@Override @Override
public void mousePressed( MouseEvent e ) { public void mousePressed( MouseEvent e ) {
lastXOnScreen = e.getXOnScreen(); if( window == null )
lastYOnScreen = e.getYOnScreen(); return; // should newer occur
dragOffset = SwingUtilities.convertPoint( FlatTitlePane.this, e.getPoint(), window );
} }
@Override public void mouseReleased( MouseEvent e ) {} @Override public void mouseReleased( MouseEvent e ) {}
@@ -791,46 +792,45 @@ debug*/
@Override @Override
public void mouseDragged( MouseEvent e ) { public void mouseDragged( MouseEvent e ) {
if( window == null )
return; // should newer occur
if( hasJBRCustomDecoration() ) if( hasJBRCustomDecoration() )
return; // do nothing if running in JBR return; // do nothing if running in JBR
int xOnScreen = e.getXOnScreen();
int yOnScreen = e.getYOnScreen();
if( lastXOnScreen == xOnScreen && lastYOnScreen == yOnScreen )
return;
// restore window if it is maximized // restore window if it is maximized
if( window instanceof Frame ) { if( window instanceof Frame ) {
Frame frame = (Frame) window; Frame frame = (Frame) window;
int state = frame.getExtendedState(); int state = frame.getExtendedState();
if( (state & Frame.MAXIMIZED_BOTH) != 0 ) { if( (state & Frame.MAXIMIZED_BOTH) != 0 ) {
int maximizedX = window.getX(); int maximizedWidth = window.getWidth();
int maximizedY = window.getY();
// restore window size, which also moves window to pre-maximized location // restore window size, which also moves window to pre-maximized location
frame.setExtendedState( state & ~Frame.MAXIMIZED_BOTH ); frame.setExtendedState( state & ~Frame.MAXIMIZED_BOTH );
// fix drag offset to ensure that window remains under mouse position
// for the case that dragging starts in the right area of the maximized window
int restoredWidth = window.getWidth(); int restoredWidth = window.getWidth();
int newX = maximizedX; int center = restoredWidth / 2;
JComponent rightComp = getComponentOrientation().isLeftToRight() ? buttonPanel : leftPanel; if( dragOffset.x > center ) {
if( xOnScreen >= maximizedX + restoredWidth - rightComp.getWidth() - 10 ) // this is same/similar to what Windows 10 does
newX = xOnScreen + rightComp.getWidth() + 10 - restoredWidth; if( dragOffset.x > maximizedWidth - center )
dragOffset.x = restoredWidth - (maximizedWidth - dragOffset.x);
// move window near mouse else
window.setLocation( newX, maximizedY ); dragOffset.x = center;
return; }
} }
} }
// compute new window location // compute new window location
int newX = window.getX() + (xOnScreen - lastXOnScreen); int newX = e.getXOnScreen() - dragOffset.x;
int newY = window.getY() + (yOnScreen - lastYOnScreen); int newY = e.getYOnScreen() - dragOffset.y;
if( newX == window.getX() && newY == window.getY() )
return;
// move window // move window
window.setLocation( newX, newY ); window.setLocation( newX, newY );
lastXOnScreen = xOnScreen;
lastYOnScreen = yOnScreen;
} }
@Override public void mouseMoved( MouseEvent e ) {} @Override public void mouseMoved( MouseEvent e ) {}