Skip to content

Commit 0c506cf

Browse files
committed
Backport d7245f70e7bac1236bbcdcd9b25346ca22ab8bb2
1 parent 682c939 commit 0c506cf

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

src/java.desktop/unix/classes/sun/awt/X11/XToolkit.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2002, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -116,6 +116,7 @@
116116
import java.util.ArrayList;
117117
import java.util.Collection;
118118
import java.util.HashMap;
119+
import java.util.Hashtable;
119120
import java.util.Iterator;
120121
import java.util.LinkedList;
121122
import java.util.Map;
@@ -358,6 +359,11 @@ public void dispatchEvent(XEvent ev) {
358359
} finally {
359360
awtLock();
360361
}
362+
} else {
363+
final XAtom XA_NET_WORKAREA = XAtom.get("_NET_WORKAREA");
364+
final boolean rootWindowWorkareaResized = (ev.get_type() == XConstants.PropertyNotify
365+
&& ev.get_xproperty().get_atom() == XA_NET_WORKAREA.getAtom());
366+
if (rootWindowWorkareaResized) resetScreenInsetsCache();
361367
}
362368
}
363369
});
@@ -848,8 +854,7 @@ private static Rectangle getWorkArea(long root, int scale)
848854
* When two screens overlap and the first contains a dock(*****), then
849855
* _NET_WORKAREA may start at point x1,y1 and end at point x2,y2.
850856
*/
851-
@Override
852-
public Insets getScreenInsets(final GraphicsConfiguration gc) {
857+
private Insets getScreenInsetsImpl(final GraphicsConfiguration gc) {
853858
GraphicsDevice gd = gc.getDevice();
854859
XNETProtocol np = XWM.getWM().getNETProtocol();
855860
if (np == null || !(gd instanceof X11GraphicsDevice) || !np.active()) {
@@ -877,6 +882,30 @@ public Insets getScreenInsets(final GraphicsConfiguration gc) {
877882
}
878883
}
879884

885+
private void resetScreenInsetsCache() {
886+
final GraphicsDevice[] devices = ((X11GraphicsEnvironment)GraphicsEnvironment.
887+
getLocalGraphicsEnvironment()).getScreenDevices();
888+
for (var gd : devices) {
889+
((X11GraphicsDevice)gd).resetInsets();
890+
}
891+
}
892+
893+
@Override
894+
public Insets getScreenInsets(final GraphicsConfiguration gc) {
895+
final X11GraphicsDevice device = (X11GraphicsDevice) gc.getDevice();
896+
Insets insets = device.getInsets();
897+
if (insets == null) {
898+
synchronized (device) {
899+
insets = device.getInsets();
900+
if (insets == null) {
901+
insets = getScreenInsetsImpl(gc);
902+
device.setInsets(insets);
903+
}
904+
}
905+
}
906+
return (Insets) insets.clone();
907+
}
908+
880909
/*
881910
* The current implementation of disabling background erasing for
882911
* canvases is that we don't set any native background color

src/java.desktop/unix/classes/sun/awt/X11GraphicsDevice.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -30,13 +30,15 @@
3030
import java.awt.GraphicsConfiguration;
3131
import java.awt.GraphicsDevice;
3232
import java.awt.GraphicsEnvironment;
33+
import java.awt.Insets;
3334
import java.awt.Rectangle;
3435
import java.awt.Window;
3536
import java.security.AccessController;
3637
import java.security.PrivilegedAction;
3738
import java.util.ArrayList;
3839
import java.util.HashMap;
3940
import java.util.HashSet;
41+
import java.util.Objects;
4042

4143
import sun.awt.util.ThreadGroupUtils;
4244
import sun.java2d.SunGraphicsEnvironment;
@@ -66,12 +68,15 @@ public final class X11GraphicsDevice extends GraphicsDevice
6668
private final Object configLock = new Object();
6769
private SunDisplayChanger topLevels = new SunDisplayChanger();
6870
private DisplayMode origDisplayMode;
71+
private volatile Rectangle bounds;
72+
private volatile Insets insets;
6973
private boolean shutdownHookRegistered;
7074
private int scale;
7175

7276
public X11GraphicsDevice(int screennum) {
7377
this.screen = screennum;
7478
this.scale = initScaleFactor();
79+
this.bounds = getBoundsImpl();
7580
}
7681

7782
/**
@@ -118,7 +123,7 @@ public int scaleDown(int x) {
118123
return Region.clipRound(x / (double)getScaleFactor());
119124
}
120125

121-
public Rectangle getBounds() {
126+
private Rectangle getBoundsImpl() {
122127
Rectangle rect = pGetBounds(getScreen());
123128
if (getScaleFactor() != 1) {
124129
rect.x = scaleDown(rect.x);
@@ -129,6 +134,23 @@ public Rectangle getBounds() {
129134
return rect;
130135
}
131136

137+
public Rectangle getBounds() {
138+
return bounds.getBounds();
139+
}
140+
141+
public Insets getInsets() {
142+
return insets;
143+
}
144+
145+
public void setInsets(Insets newInsets) {
146+
Objects.requireNonNull(newInsets);
147+
insets = newInsets;
148+
}
149+
150+
public void resetInsets() {
151+
insets = null;
152+
}
153+
132154
/**
133155
* Returns the identification string associated with this graphics
134156
* device.
@@ -510,6 +532,8 @@ private synchronized DisplayMode getMatchingDisplayMode(DisplayMode dm) {
510532
@Override
511533
public synchronized void displayChanged() {
512534
scale = initScaleFactor();
535+
bounds = getBoundsImpl();
536+
insets = null;
513537
// On X11 the visuals do not change, and therefore we don't need
514538
// to reset the defaultConfig, config, doubleBufferVisuals,
515539
// neither do we need to reset the native data.

0 commit comments

Comments
 (0)