Skip to content

Commit

Permalink
Window decorations: fixed wrong window placement when moving window t…
Browse files Browse the repository at this point in the history
…o another screen with different scaling factor (issue #166)
  • Loading branch information
DevCharly committed Sep 3, 2020
1 parent c6beb9d commit 0247308
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ FlatLaf Change Log
- Demo: Improved "SplitPane & Tabs" and "Data Components" tabs.
- 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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -754,8 +754,7 @@ public void windowStateChanged( WindowEvent e ) {

//---- interface MouseListener ----

private int lastXOnScreen;
private int lastYOnScreen;
private Point dragOffset;

@Override
public void mouseClicked( MouseEvent e ) {
Expand All @@ -779,8 +778,10 @@ public void mouseClicked( MouseEvent e ) {

@Override
public void mousePressed( MouseEvent e ) {
lastXOnScreen = e.getXOnScreen();
lastYOnScreen = e.getYOnScreen();
if( window == null )
return; // should newer occur

dragOffset = SwingUtilities.convertPoint( FlatTitlePane.this, e.getPoint(), window );
}

@Override public void mouseReleased( MouseEvent e ) {}
Expand All @@ -791,46 +792,45 @@ public void mousePressed( MouseEvent e ) {

@Override
public void mouseDragged( MouseEvent e ) {
if( window == null )
return; // should newer occur

if( hasJBRCustomDecoration() )
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
if( window instanceof Frame ) {
Frame frame = (Frame) window;
int state = frame.getExtendedState();
if( (state & Frame.MAXIMIZED_BOTH) != 0 ) {
int maximizedX = window.getX();
int maximizedY = window.getY();
int maximizedWidth = window.getWidth();

// restore window size, which also moves window to pre-maximized location
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 newX = maximizedX;
JComponent rightComp = getComponentOrientation().isLeftToRight() ? buttonPanel : leftPanel;
if( xOnScreen >= maximizedX + restoredWidth - rightComp.getWidth() - 10 )
newX = xOnScreen + rightComp.getWidth() + 10 - restoredWidth;

// move window near mouse
window.setLocation( newX, maximizedY );
return;
int center = restoredWidth / 2;
if( dragOffset.x > center ) {
// this is same/similar to what Windows 10 does
if( dragOffset.x > maximizedWidth - center )
dragOffset.x = restoredWidth - (maximizedWidth - dragOffset.x);
else
dragOffset.x = center;
}
}
}

// compute new window location
int newX = window.getX() + (xOnScreen - lastXOnScreen);
int newY = window.getY() + (yOnScreen - lastYOnScreen);
int newX = e.getXOnScreen() - dragOffset.x;
int newY = e.getYOnScreen() - dragOffset.y;

if( newX == window.getX() && newY == window.getY() )
return;

// move window
window.setLocation( newX, newY );

lastXOnScreen = xOnScreen;
lastYOnScreen = yOnScreen;
}

@Override public void mouseMoved( MouseEvent e ) {}
Expand Down

0 comments on commit 0247308

Please sign in to comment.