8267307: Introduce new client property for XAWT: xawt.mwm_decor_title

Reviewed-by: azvegint, serb
This commit is contained in:
Maxim Kartashev 2021-07-01 09:53:28 +00:00 committed by Alexey Ushakov
parent 54a9c3e39f
commit 18f356a38e
3 changed files with 264 additions and 6 deletions
src/java.desktop/unix/classes/sun/awt/X11
test/jdk/java/awt/Window/WindowTitleVisibleTest

@ -32,6 +32,7 @@ import java.awt.event.WindowEvent;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import sun.awt.IconInfo;
import sun.util.logging.PlatformLogger;
@ -340,7 +341,19 @@ abstract class XDecoratedPeer extends XWindowPeer {
|| ev.get_atom() == XWM.XA_NET_FRAME_EXTENTS.getAtom())
{
if (XWM.getWMID() != XWM.UNITY_COMPIZ_WM) {
getWMSetInsets(XAtom.get(ev.get_atom()));
if (getMWMDecorTitleProperty().isPresent()) {
// Insets might have changed "in-flight" if that property
// is present, so we need to get the actual values of
// insets from the WM and propagate them through all the
// proper channels.
wm_set_insets = null;
Insets in = getWMSetInsets(XAtom.get(ev.get_atom()));
if (in != null && !in.equals(dimensions.getInsets())) {
handleCorrectInsets(in);
}
} else {
getWMSetInsets(XAtom.get(ev.get_atom()));
}
} else {
if (!isReparented()) {
return;
@ -1305,4 +1318,24 @@ abstract class XDecoratedPeer extends XWindowPeer {
}
super.handleWindowFocusOut(oppositeWindow, serial);
}
public static final String MWM_DECOR_TITLE_PROPERTY_NAME = "xawt.mwm_decor_title";
public final Optional<Boolean> getMWMDecorTitleProperty() {
Optional<Boolean> res = Optional.empty();
if (SunToolkit.isInstanceOf(target, "javax.swing.RootPaneContainer")) {
javax.swing.JRootPane rootpane = ((javax.swing.RootPaneContainer) target).getRootPane();
Object prop = rootpane.getClientProperty(MWM_DECOR_TITLE_PROPERTY_NAME);
if (prop != null) {
res = Optional.of(Boolean.parseBoolean(prop.toString()));
}
}
return res;
}
public final boolean getWindowTitleVisible() {
return getMWMDecorTitleProperty().orElse(true);
}
}

@ -34,6 +34,7 @@ import java.awt.Insets;
import java.awt.MenuBar;
import java.awt.Rectangle;
import java.awt.peer.FramePeer;
import sun.awt.SunToolkit;
import sun.util.logging.PlatformLogger;
import sun.awt.AWTAccessor;
@ -66,11 +67,7 @@ class XFramePeer extends XDecoratedPeer implements FramePeer {
state = 0;
undecorated = Boolean.valueOf(target.isUndecorated());
winAttr.nativeDecor = !target.isUndecorated();
if (winAttr.nativeDecor) {
winAttr.decorations = XWindowAttributesData.AWT_DECOR_ALL;
} else {
winAttr.decorations = XWindowAttributesData.AWT_DECOR_NONE;
}
winAttr.decorations = getWindowDecorationBits();
winAttr.functions = MWMConstants.MWM_FUNC_ALL;
winAttr.isResizable = true; // target.isResizable();
winAttr.title = target.getTitle();
@ -80,6 +77,38 @@ class XFramePeer extends XDecoratedPeer implements FramePeer {
Integer.valueOf(winAttr.decorations), Boolean.valueOf(winAttr.initialResizability),
Boolean.valueOf(!winAttr.nativeDecor), Integer.valueOf(winAttr.initialState));
}
registerWindowDecorationChangeListener();
}
private void registerWindowDecorationChangeListener() {
if (SunToolkit.isInstanceOf(target, "javax.swing.RootPaneContainer")) { // avoid unnecessary class loading
javax.swing.JRootPane rootpane = ((javax.swing.RootPaneContainer) target).getRootPane();
rootpane.addPropertyChangeListener(MWM_DECOR_TITLE_PROPERTY_NAME, e -> winAttr.decorations = getWindowDecorationBits() );
}
}
private int getWindowDecorationBits() {
int decorations = XWindowAttributesData.AWT_DECOR_NONE;
final Frame target = (Frame)(this.target);
final boolean useNativeDecor = !target.isUndecorated();
if (useNativeDecor) {
decorations = XWindowAttributesData.AWT_DECOR_ALL;
if (!getWindowTitleVisible()) {
// NB: the window must be [re-]mapped to make this change effective. Also, window insets will probably
// change and that'll be caught by one of the subsequent property change events in XDecoratedPeer
// (not necessarily the very next event, though).
decorations = XWindowAttributesData.AWT_DECOR_BORDER;
}
if (log.isLoggable(PlatformLogger.Level.FINE)) {
log.fine("Frame''s initial decorations affected by the client property {0}={1}",
MWM_DECOR_TITLE_PROPERTY_NAME, getMWMDecorTitleProperty());
}
}
return decorations;
}
void postInit(XCreateWindowParams params) {

@ -0,0 +1,196 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, JetBrains s.r.o.. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @key headful
* @summary Verifies the client property xawt.mwm_decor_title for Linux.
* Note: the test requires GNOME Shell window manager and will automatically
* pass with any other WM.
* @requires (os.family == "linux")
* @run main WindowTitleVisibleTestLinuxGnome
*/
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import javax.swing.*;
import java.nio.charset.StandardCharsets;
import java.io.File;
import javax.imageio.*;
public class WindowTitleVisibleTestLinuxGnome
{
private static WindowTitleVisibleTestLinuxGnome theTest;
private Robot robot;
private JFrame frame;
private JRootPane rootPane;
private Rectangle titleBarBounds;
private BufferedImage titleBarImageVisible;
private BufferedImage titleBarImageNotVisible;
private int DELAY = 1000;
public WindowTitleVisibleTestLinuxGnome() {
try {
robot = new Robot();
} catch (AWTException ex) {
throw new RuntimeException(ex);
}
}
public void performTest() {
constructAndShowFrame();
robot.delay(DELAY);
Insets insets = frame.getInsets();
Rectangle bounds = frame.getBounds();
titleBarBounds = new Rectangle(bounds.x, bounds.y, bounds.width, insets.top);
captureTitleBarVisible();
robot.delay(DELAY);
hideTitleBar();
robot.delay(DELAY);
captureTitleBarNotVisible();
if (imagesEqual(titleBarImageVisible, titleBarImageNotVisible)) {
throw new RuntimeException("Test failed: title bars shown and hidden are the same.");
}
runSwing(() -> frame.dispose());
frame = null;
rootPane = null;
}
private static boolean imagesEqual(BufferedImage img1, BufferedImage img2) {
for (int px = 0; px < img1.getWidth(); px++) {
for (int py = 0; py < img1.getHeight(); py++) {
int rgb1 = img1.getRGB(px, py);
int rgb2 = img2.getRGB(px, py);
if (rgb1 != rgb2) {
return false;
}
}
}
return true;
}
private void captureTitleBarNotVisible() {
titleBarImageNotVisible = robot.createScreenCapture(titleBarBounds);
}
private void hideTitleBar() {
runSwing( () -> {
rootPane.putClientProperty("xawt.mwm_decor_title", false);
frame.setVisible(false);
frame.setVisible(true);
});
}
private void captureTitleBarVisible() {
titleBarImageVisible = robot.createScreenCapture(titleBarBounds);
}
private void constructAndShowFrame() {
runSwing(() -> {
frame = new JFrame("IIIIIIIIIIIIIIII");
frame.setBounds(100, 100, 300, 150);
rootPane = frame.getRootPane();
rootPane.putClientProperty("xawt.mwm_decor_title", true);
JComponent contentPane = (JComponent) frame.getContentPane();
JPanel comp = new JPanel();
contentPane.add(comp);
comp.setBackground(Color.RED);
frame.setVisible(true);
});
}
public void dispose() {
if (frame != null) {
frame.dispose();
frame = null;
}
}
private static void runSwing(Runnable r) {
try {
SwingUtilities.invokeAndWait(r);
} catch (InterruptedException e) {
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
}
private static String getWindowManagerID() {
String WMID = null;
try {
Process p = new ProcessBuilder("xprop", "-root", "_NET_SUPPORTING_WM_CHECK").start();
System.out.println( new String(p.getErrorStream().readAllBytes(), StandardCharsets.UTF_8) );
String stdout = new String(p.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
final String windowID = stdout.substring(stdout.lastIndexOf(" ")).strip();
p = new ProcessBuilder("xprop", "-id", windowID, "_NET_WM_NAME").start();
System.out.println( new String(p.getErrorStream().readAllBytes(), StandardCharsets.UTF_8) );
stdout = new String(p.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
WMID = stdout.substring(stdout.lastIndexOf("=")).strip();
System.out.println("WM name: " + WMID);
}
catch (Exception e) {
e.printStackTrace();
}
return WMID;
}
public static void main(String[] args) {
final String WMID = getWindowManagerID();
if (WMID == null) {
System.out.println("Failed to determine Window Manager. The test will not run and is considered passed.");
return;
} else if (!WMID.toLowerCase().contains("gnome")) {
System.out.println("Window Manager " + WMID + " is not supported, only GNOME is. The test will not run and is considered passed.");
return;
}
try {
runSwing(() -> theTest = new WindowTitleVisibleTestLinuxGnome());
theTest.performTest();
} finally {
if (theTest != null) {
runSwing(() -> theTest.dispose());
}
}
}
}