Skip to content

Commit

Permalink
Window decorations: fixed window resizing on Linux (issue #482)
Browse files Browse the repository at this point in the history
  • Loading branch information
DevCharly committed Feb 26, 2022
1 parent 05d795b commit 418f55f
Showing 1 changed file with 23 additions and 6 deletions.
Expand Up @@ -23,6 +23,7 @@
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Toolkit;
Expand All @@ -43,6 +44,7 @@
import javax.swing.JLayeredPane;
import javax.swing.JRootPane;
import javax.swing.UIManager;
import com.formdev.flatlaf.util.SystemInfo;
import com.formdev.flatlaf.util.UIScale;

/**
Expand Down Expand Up @@ -231,8 +233,15 @@ public static class WindowResizer
{
protected Window window;

private final boolean limitResizeToScreenBounds;

public WindowResizer( JRootPane rootPane ) {
super( rootPane );

// On Linux, limit window resizing to screen bounds because otherwise
// there would be a strange effect when the mouse is moved over a sidebar
// while resizing and the opposite window side is also resized.
limitResizeToScreenBounds = SystemInfo.isLinux;
}

@Override
Expand Down Expand Up @@ -289,11 +298,19 @@ protected void setWindowBounds( Rectangle r ) {

@Override
protected boolean limitToParentBounds() {
return false;
return limitResizeToScreenBounds && window != null;
}

@Override
protected Rectangle getParentBounds() {
if( limitResizeToScreenBounds && window != null ) {
GraphicsConfiguration gc = window.getGraphicsConfiguration();
Rectangle bounds = gc.getBounds();
Insets insets = window.getToolkit().getScreenInsets( gc );
return new Rectangle( bounds.x + insets.left, bounds.y + insets.top,
bounds.width - insets.left - insets.right,
bounds.height - insets.top - insets.bottom );
}
return null;
}

Expand Down Expand Up @@ -385,7 +402,7 @@ protected boolean limitToParentBounds() {

@Override
protected Rectangle getParentBounds() {
return getFrame().getParent().getBounds();
return new Rectangle( getFrame().getParent().getSize() );
}

@Override
Expand Down Expand Up @@ -579,8 +596,8 @@ public void mouseDragged( MouseEvent e ) {
// top
if( resizeDir == N_RESIZE_CURSOR || resizeDir == NW_RESIZE_CURSOR || resizeDir == NE_RESIZE_CURSOR ) {
newBounds.y = yOnScreen - dragTopOffset;
if( limitToParentBounds() && newBounds.y < 0 )
newBounds.y = 0;
if( limitToParentBounds() )
newBounds.y = Math.max( newBounds.y, getParentBounds().y );
newBounds.height += (oldBounds.y - newBounds.y);
}

Expand All @@ -597,8 +614,8 @@ public void mouseDragged( MouseEvent e ) {
// left
if( resizeDir == W_RESIZE_CURSOR || resizeDir == NW_RESIZE_CURSOR || resizeDir == SW_RESIZE_CURSOR ) {
newBounds.x = xOnScreen - dragLeftOffset;
if( limitToParentBounds() && newBounds.x < 0 )
newBounds.x = 0;
if( limitToParentBounds() )
newBounds.x = Math.max( newBounds.x, getParentBounds().x );
newBounds.width += (oldBounds.x - newBounds.x);
}

Expand Down

0 comments on commit 418f55f

Please sign in to comment.