Skip to content

Commit

Permalink
ui: readd window opacity setting
Browse files Browse the repository at this point in the history
The undecorated frame requirement appears to not be real, at least on Windows.

AWT applies translucency via setting WS_EX_LAYERED to make the window layered and then changes the opacity of the layered window with SetLayeredWindowAttributes.

This appears to work regardless of the frame state, even with custom chrome off.
  • Loading branch information
Adam- committed Jan 27, 2024
1 parent 4ca62a9 commit 2ef47d7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,22 @@ default boolean enableCustomChrome()
return OSType.getOSType() == OSType.Windows;
}

@Range(
min = 10,
max = 100
)
@ConfigItem(
keyName = "uiWindowOpacity",
name = "Window opacity",
description = "Set the windows opacity.",
position = 16,
section = windowSettings
)
default int windowOpacity()
{
return 100;
}

@ConfigItem(
keyName = "gameAlwaysOnTop",
name = "Always on top",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,11 @@ private void updateFrameConfig(boolean updateResizable)
return;
}

if (frame.getGraphicsConfiguration().getDevice().getFullScreenWindow() == null)
{
frame.setOpacity(((float) config.windowOpacity()) / 100.0f);
}

if (config.usernameInTitle() && (client instanceof Client))
{
final Player player = ((Client) client).getLocalPlayer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.formdev.flatlaf.ui.FlatNativeWindowsLibrary;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.IllegalComponentStateException;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
Expand All @@ -52,6 +53,7 @@ public enum Mode
private boolean rightSideSuction;
private boolean boundsOpSet;
private boolean scaleMinSize = false;
private boolean overrideUndecorated;

@Override
@SuppressWarnings("deprecation")
Expand Down Expand Up @@ -259,4 +261,30 @@ void updateContainsInScreen()
FlatNativeWindowsLibrary.setContainInScreen(this, containedInScreen == Mode.ALWAYS);
}
}

@Override
public void setOpacity(float opacity)
{
// JDK-6993784 requires the frame to be undecorated to apply opacity, but in practice it works on Windows regardless.
// Temporarily pretend to be an undecorated frame to satisfy Frame.setOpacity().
overrideUndecorated = true;
try
{
super.setOpacity(opacity);
}
catch (IllegalComponentStateException | UnsupportedOperationException | IllegalArgumentException ex)
{
log.warn("unable to set opacity {}", opacity, ex);
}
finally
{
overrideUndecorated = false;
}
}

@Override
public boolean isUndecorated()
{
return overrideUndecorated || super.isUndecorated();
}
}

0 comments on commit 2ef47d7

Please sign in to comment.