Skip to content

Commit

Permalink
containable frame: remove setMaximizedBounds workaround
Browse files Browse the repository at this point in the history
Flatlaf uses the native ShowWindow method to maximize the screen, making the setMaximizedBounds code unused on Windows.
  • Loading branch information
Adam- committed Nov 13, 2023
1 parent feea784 commit 24617c0
Showing 1 changed file with 0 additions and 193 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,19 @@
*/
package net.runelite.client.ui;

import com.google.common.annotations.VisibleForTesting;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Arrays;
import java.util.Comparator;
import javax.swing.JFrame;
import lombok.extern.slf4j.Slf4j;
import net.runelite.client.util.OSType;

@Slf4j
public class ContainableFrame extends JFrame
Expand All @@ -52,115 +49,6 @@ public enum Mode
}

private static final int SCREEN_EDGE_CLOSE_DISTANCE = 40;
/**
* JDK-8231564 changes Frame#setMaximizedBounds() to apply ui scaling
*/
private static boolean jdk8231564;
/**
* JDK-8243925 changes Toolkit#getScreenInsets() to apply ui scaling
*/
private static boolean jdk8243925;

static
{
try
{
String javaVersion = System.getProperty("java.version");
jdk8231564 = jdk8231564(javaVersion);
jdk8243925 = jdk8243925(javaVersion);
}
catch (Exception ex)
{
log.error("error checking java version", ex);
}
}

@VisibleForTesting
static boolean jdk8231564(String javaVersion)
{
if (isVersionOrGreater(javaVersion, 15, -1, -1))
{
return true; // JDK-8231564
}
if (isVersionOrGreater(javaVersion, 14, -1, -1))
{
return false; // unpatched
}
if (isVersionOrGreater(javaVersion, 13, 0, 4))
{
return true; // JDK-8247209
}
if (isVersionOrGreater(javaVersion, 12, -1, -1))
{
return false; // unpatched
}
return isVersionOrGreater(javaVersion, 11, 0, 8); // JDK-8243374
}

@VisibleForTesting
static boolean jdk8243925(String javaVersion)
{
if (isVersionOrGreater(javaVersion, 15, -1, -1))
{
return true; // JDK-8243925
}
if (isVersionOrGreater(javaVersion, 14, -1, -1))
{
return false; // unpatched
}
if (isVersionOrGreater(javaVersion, 13, 0, 7))
{
return true; // JDK-8261342
}
if (isVersionOrGreater(javaVersion, 12, -1, -1))
{
return false; // unpatched
}
return isVersionOrGreater(javaVersion, 11, 0, 9); // JDK-8246659
}

private static boolean isVersionOrGreater(String javaVersion, int versionMajor, int versionMinor, int versionPatch)
{
int idx = javaVersion.indexOf('_');
if (idx != -1)
{
javaVersion = javaVersion.substring(0, idx);
}
String[] s = javaVersion.split("\\.");
int major, minor, patch;
if (s.length >= 3)
{
major = Integer.parseInt(s[0]);
minor = Integer.parseInt(s[1]);
patch = Integer.parseInt(s[2]);
}
else
{
major = Integer.parseInt(s[0]);
minor = -1;
patch = -1;
}

int i = Integer.compare(major, versionMajor);
if (i != 0)
{
return i > 0;
}

i = Integer.compare(minor, versionMinor);
if (i != 0)
{
return i > 0;
}

i = Integer.compare(patch, versionPatch);
if (i != 0)
{
return i > 0;
}

return true;
}

private Mode containedInScreen;
private boolean rightSideSuction;
Expand Down Expand Up @@ -321,26 +209,6 @@ public void containedSetSize(Dimension size)
applyChange(getX(), getY(), size.width, size.height, getX(), getY(), this.containedInScreen != Mode.NEVER);
}

/**
* Due to Java bug JDK-4737788, maximizing an undecorated frame causes it to cover the taskbar.
* As a workaround, Substance calls this method when the window is maximized to manually set the
* bounds, but its calculation ignores high-DPI scaling. We're overriding it to correctly calculate
* the maximized bounds.
*/
@Override
public void setMaximizedBounds(Rectangle bounds)
{
if (OSType.getOSType() == OSType.MacOS)
{
// OSX seems to correctly handle DPI scaling already
super.setMaximizedBounds(bounds);
}
else
{
super.setMaximizedBounds(getWindowAreaBounds());
}
}

/**
* Finds the {@link GraphicsConfiguration} of the display the window is currently on. If it's on more than
* one screen, returns the one it's most on (largest area of intersection)
Expand All @@ -357,67 +225,6 @@ private GraphicsConfiguration getCurrentDisplayConfiguration()
.orElseGet(this::getGraphicsConfiguration);
}

/**
* Calculates the bounds of the window area of the screen.
* <p>
* The bounds returned by {@link GraphicsEnvironment#getMaximumWindowBounds} are incorrectly calculated on
* high-DPI screens.
*/
private Rectangle getWindowAreaBounds()
{
Toolkit toolkit = Toolkit.getDefaultToolkit();

log.trace("Current bounds: {}", getBounds());
for (GraphicsDevice device : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices())
{
GraphicsConfiguration graphicsConfiguration = device.getDefaultConfiguration();
log.trace("Device: {} bounds {} insets {}", device, graphicsConfiguration.getBounds(),
toolkit.getScreenInsets(graphicsConfiguration));
}

GraphicsConfiguration config = getCurrentDisplayConfiguration();
// get screen bounds
Rectangle bounds = config.getBounds();
log.trace("Chosen device: {} bounds {}", config, bounds);

// transform bounds to dpi-independent coordinates
if (!jdk8231564)
{
// JDK-8231564 fixed setMaximizedBounds to scale the bounds, so this must only be done on <11.0.8
bounds = config.getDefaultTransform().createTransformedShape(bounds).getBounds();
log.trace("Transformed bounds {}", bounds);
}

// subtract insets (taskbar, etc.)
Insets insets = toolkit.getScreenInsets(config);
if (!jdk8231564)
{
// Prior to JDK-8231564, WFramePeer expects the bounds to be relative to the current monitor instead of the
// primary display.
bounds.x = bounds.y = 0;

assert !jdk8243925 : "scaled insets without scaled bounds";
}
else if (!jdk8243925)
{
// The insets from getScreenInsets are not scaled prior to JDK-8243925, we must convert them to DPI scaled
// pixels on 11.0.8 due to JDK-8231564 which expects the bounds to be in DPI-aware pixels.
double scaleX = config.getDefaultTransform().getScaleX();
double scaleY = config.getDefaultTransform().getScaleY();
insets.top /= scaleY;
insets.bottom /= scaleY;
insets.left /= scaleX;
insets.right /= scaleX;
}
bounds.x += insets.left;
bounds.y += insets.top;
bounds.height -= (insets.bottom + insets.top);
bounds.width -= (insets.right + insets.left);

log.trace("Final bounds: {}", bounds);
return bounds;
}

/**
* Force minimum size of frame to be it's layout manager's minimum size
*/
Expand Down

0 comments on commit 24617c0

Please sign in to comment.