Skip to content

Commit 86b631c

Browse files
mkartashevYaaZ
authored andcommitted
JBR-8618 Wayland: GTK LaF does not change appearance when system theme changes
1 parent f05a6b2 commit 86b631c

File tree

4 files changed

+60
-24
lines changed

4 files changed

+60
-24
lines changed

src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/GTKLookAndFeel.java

+14-7
Original file line numberDiff line numberDiff line change
@@ -289,12 +289,19 @@ public UIDefaults getDefaults() {
289289
private void installPropertyChangeListeners() {
290290
if(!pclInstalled) {
291291
Toolkit kit = Toolkit.getDefaultToolkit();
292-
WeakPCL pcl = new WeakPCL(this, kit, "gnome.Net/ThemeName");
293-
kit.addPropertyChangeListener(pcl.getKey(), pcl);
294-
pcl = new WeakPCL(this, kit, "gnome.Gtk/FontName");
295-
kit.addPropertyChangeListener(pcl.getKey(), pcl);
296-
pcl = new WeakPCL(this, kit, "gnome.Xft/DPI");
297-
kit.addPropertyChangeListener(pcl.getKey(), pcl);
292+
WeakPCL pcl;
293+
if (isWayland) {
294+
pcl = new WeakPCL(this, kit, "awt.os.theme.isDark");
295+
kit.addPropertyChangeListener(pcl.getKey(), pcl);
296+
} else {
297+
// These properties are only available from the X server
298+
pcl = new WeakPCL(this, kit, "gnome.Net/ThemeName");
299+
kit.addPropertyChangeListener(pcl.getKey(), pcl);
300+
pcl = new WeakPCL(this, kit, "gnome.Gtk/FontName");
301+
kit.addPropertyChangeListener(pcl.getKey(), pcl);
302+
pcl = new WeakPCL(this, kit, "gnome.Xft/DPI");
303+
kit.addPropertyChangeListener(pcl.getKey(), pcl);
304+
}
298305

299306
flushUnreferenced();
300307
pclInstalled = true;
@@ -1532,7 +1539,7 @@ public void run() {
15321539
* the UIDefaults reads them and this event causes
15331540
* those to be reinitialised.
15341541
*/
1535-
if ("gnome.Net/ThemeName".equals(name)) {
1542+
if ("gnome.Net/ThemeName".equals(name) || "awt.os.theme.isDark".equals(name)) {
15361543
GTKEngine.INSTANCE.themeChanged();
15371544
GTKIconFactory.resetIcons();
15381545
}

src/java.desktop/unix/classes/sun/awt/wl/WLDecoratedPeer.java

+6
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,10 @@ Cursor cursorAt(int x, int y) {
154154
return super.cursorAt(x, y);
155155
}
156156
}
157+
158+
@Override
159+
public void dispose() {
160+
decoration.dispose();
161+
super.dispose();
162+
}
157163
}

src/java.desktop/unix/classes/sun/awt/wl/WLFrameDecoration.java

+40-10
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,28 @@
2626

2727
import sun.swing.SwingUtilities2;
2828

29-
import java.awt.*;
30-
import java.awt.event.*;
29+
import java.awt.AlphaComposite;
30+
import java.awt.Color;
31+
import java.awt.Composite;
32+
import java.awt.Cursor;
33+
import java.awt.Dimension;
34+
import java.awt.Font;
35+
import java.awt.FontMetrics;
36+
import java.awt.Frame;
37+
import java.awt.Graphics;
38+
import java.awt.Graphics2D;
39+
import java.awt.Insets;
40+
import java.awt.Point;
41+
import java.awt.Rectangle;
42+
import java.awt.RenderingHints;
43+
import java.awt.Toolkit;
44+
import java.awt.event.MouseEvent;
3145
import java.awt.geom.Ellipse2D;
46+
import java.beans.PropertyChangeEvent;
47+
import java.beans.PropertyChangeListener;
3248
import java.util.function.Supplier;
3349

34-
public class WLFrameDecoration {
50+
public class WLFrameDecoration implements PropertyChangeListener {
3551
private static final int HEIGHT = 30;
3652
private static final int BUTTON_ICON_SIZE = 4;
3753
private static final int BUTTON_CIRCLE_SIZE = 10;
@@ -53,7 +69,11 @@ public class WLFrameDecoration {
5369
private static final int SIGNIFICANT_DRAG_DISTANCE = 4;
5470
private static final int RESIZE_EDGE_THICKNESS = 5;
5571

56-
private static volatile boolean isDarkTheme = false;
72+
private static volatile boolean isDarkTheme;
73+
static {
74+
Object isDarkThemeProp = Toolkit.getDefaultToolkit().getDesktopProperty("awt.os.theme.isDark");
75+
isDarkTheme = isDarkThemeProp instanceof Boolean ? (Boolean) isDarkThemeProp : false;
76+
}
5777

5878
private static final int XDG_TOPLEVEL_RESIZE_EDGE_TOP = 1;
5979
private static final int XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM = 2;
@@ -99,6 +119,7 @@ public WLFrameDecoration(WLDecoratedPeer peer, boolean isUndecorated, boolean sh
99119
closeButton = new ButtonState(this::getCloseButtonCenter, peer::postWindowClosing);
100120
maximizeButton = showMaximize ? new ButtonState(this::getMaximizeButtonCenter, this::toggleMaximizedState) : null;
101121
minimizeButton = showMinimize ? new ButtonState(this::getMinimizeButtonCenter, this::minimizeWindow) : null;
122+
WLToolkit.getDefaultToolkit().addPropertyChangeListener("awt.os.theme.isDark", this);
102123
}
103124
}
104125

@@ -157,11 +178,6 @@ private static boolean isDarkTheme() {
157178
return isDarkTheme;
158179
}
159180

160-
private static void updateTheme() {
161-
Boolean isDark = (Boolean) Toolkit.getDefaultToolkit().getDesktopProperty("awt.os.theme.isDark");
162-
isDarkTheme = isDark != null && isDark;
163-
}
164-
165181
private static Color getBackgroundColor(boolean isActive) {
166182
if (isActive) {
167183
return isDarkTheme() ? ACTIVE_BACKGROUND_DARK : ACTIVE_BACKGROUND;
@@ -198,7 +214,6 @@ public void paint(final Graphics g) {
198214
if (width <= 0 || height <= 0) return;
199215
Graphics2D g2d = (Graphics2D) g.create(0, 0, width, HEIGHT);
200216
try {
201-
updateTheme();
202217
doPaint(g2d);
203218
} finally {
204219
g2d.dispose();
@@ -448,6 +463,21 @@ Cursor getCursor(int x, int y) {
448463
return null;
449464
}
450465

466+
@Override
467+
public void propertyChange(PropertyChangeEvent evt) {
468+
if ("awt.os.theme.isDark".equals(evt.getPropertyName())) {
469+
Object newValue = evt.getNewValue();
470+
if (newValue != null) {
471+
isDarkTheme = (Boolean) newValue;
472+
peer.notifyClientDecorationsChanged();
473+
}
474+
}
475+
}
476+
477+
public void dispose() {
478+
WLToolkit.getDefaultToolkit().removePropertyChangeListener("awt.os.theme.isDark", this);
479+
}
480+
451481
private static class ButtonState {
452482
private final Supplier<Point> location;
453483
private final Runnable action;

src/java.desktop/unix/classes/sun/awt/wl/WLToolkit.java

-7
Original file line numberDiff line numberDiff line change
@@ -911,13 +911,6 @@ protected Object lazilyLoadDesktopProperty(String name) {
911911
return null;
912912
}
913913

914-
@Override
915-
public synchronized void addPropertyChangeListener(String name, PropertyChangeListener pcl) {
916-
if (log.isLoggable(PlatformLogger.Level.FINE)) {
917-
log.fine("Not implemented: WLToolkit.addPropertyChangeListener()");
918-
}
919-
}
920-
921914
/**
922915
* @see SunToolkit#needsXEmbedImpl
923916
*/

0 commit comments

Comments
 (0)