8181910: [macos] Support dark title bars on macOS
authorsveerabhadra
Mon, 16 Apr 2018 10:35:22 +0530
changeset 49989 ecc7b10a1cca
parent 49988 4ed714d1f90b
child 49990 6cd19862c742
8181910: [macos] Support dark title bars on macOS Reviewed-by: serb, denis
src/java.desktop/macosx/classes/sun/lwawt/macosx/CPlatformWindow.java
src/java.desktop/macosx/native/libawt_lwawt/awt/AWTWindow.m
test/jdk/javax/swing/JFrame/DarkTitleBar/DarkTitleBar.java
--- a/src/java.desktop/macosx/classes/sun/lwawt/macosx/CPlatformWindow.java	Sat Apr 14 00:25:12 2018 +0100
+++ b/src/java.desktop/macosx/classes/sun/lwawt/macosx/CPlatformWindow.java	Mon Apr 16 10:35:22 2018 +0530
@@ -101,6 +101,7 @@
     // for client properties
     public static final String WINDOW_BRUSH_METAL_LOOK = "apple.awt.brushMetalLook";
     public static final String WINDOW_DRAGGABLE_BACKGROUND = "apple.awt.draggableWindowBackground";
+    public static final String WINDOW_DARK_APPEARANCE = "apple.awt.windowDarkAppearance";
 
     public static final String WINDOW_ALPHA = "Window.alpha";
     public static final String WINDOW_SHADOW = "Window.shadow";
@@ -148,6 +149,7 @@
     static final int IS_DIALOG = 1 << 25;
     static final int IS_MODAL = 1 << 26;
     static final int IS_POPUP = 1 << 27;
+    static final int DARK_TITLE_BAR = 1 << 28;
 
     static final int _STYLE_PROP_BITMASK = DECORATED | TEXTURED | UNIFIED | UTILITY | HUD | SHEET | CLOSEABLE | MINIMIZABLE | RESIZABLE;
 
@@ -230,7 +232,10 @@
 
             final String filename = ((java.io.File)value).getAbsolutePath();
             c.execute(ptr->nativeSetNSWindowRepresentedFilename(ptr, filename));
-        }}
+        }},
+        new Property<CPlatformWindow>(WINDOW_DARK_APPEARANCE) { public void applyProperty(final CPlatformWindow c, final Object value) {
+            c.setStyleBits(DARK_TITLE_BAR, value == null ? true : Boolean.parseBoolean(value.toString()));
+        }},
     }) {
         @SuppressWarnings("deprecation")
         public CPlatformWindow convertJComponentToTarget(final JRootPane p) {
@@ -468,6 +473,11 @@
             if (prop != null) {
                 styleBits = SET(styleBits, DRAGGABLE_BACKGROUND, Boolean.parseBoolean(prop.toString()));
             }
+
+            prop = rootpane.getClientProperty(WINDOW_DARK_APPEARANCE);
+            if (prop != null) {
+               styleBits = SET(styleBits, DARK_TITLE_BAR, Boolean.parseBoolean(prop.toString()));
+            }
         }
 
         if (isDialog) {
--- a/src/java.desktop/macosx/native/libawt_lwawt/awt/AWTWindow.m	Sat Apr 14 00:25:12 2018 +0100
+++ b/src/java.desktop/macosx/native/libawt_lwawt/awt/AWTWindow.m	Mon Apr 16 10:35:22 2018 +0530
@@ -264,6 +264,11 @@
         }
     }
 
+    if (IS(self.styleBits, DARK_TITLE_BAR)) {
+        [self.nsWindow setAppearance:[NSAppearance appearanceNamed:NSAppearanceNameVibrantDark]];
+    } else {
+        [self.nsWindow setAppearance:[NSAppearance appearanceNamed:NSAppearanceNameVibrantLight]];
+    }    
 }
 
 - (id) initWithPlatformWindow:(JNFWeakJObjectWrapper *)platformWindow
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/javax/swing/JFrame/DarkTitleBar/DarkTitleBar.java	Mon Apr 16 10:35:22 2018 +0530
@@ -0,0 +1,85 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. 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.
+ */
+
+import java.awt.Rectangle;
+import java.awt.image.BufferedImage;
+import java.awt.Robot;
+import java.awt.Color;
+import javax.swing.JFrame;
+import javax.swing.SwingUtilities;
+
+/**
+ * @test
+ * @key headful
+ * @bug 8181910
+ * @requires (os.family == "mac")
+ * @summary [macos] Support dark title bars on macOS
+ * @run main DarkTitleBar
+ */
+
+public final class DarkTitleBar {
+    private static BufferedImage image = null;
+    private static Rectangle bounds;
+    private static JFrame frame;
+    private static Robot robot;
+
+    public static void main(final String[] args) throws Exception {
+        robot = new Robot();
+        robot.setAutoDelay(50);
+
+        // Capture and compare pixel color
+        testFrame();
+    }
+
+    private static void testFrame() throws Exception {
+        createUI();
+        image = robot.createScreenCapture(bounds);
+        SwingUtilities.invokeAndWait(frame::dispose);
+        robot.waitForIdle();
+
+        Color titleColor = new Color(image.getRGB(100, 5), true);
+        if(titleColor.getRed() > 50 || titleColor.getGreen() > 50 || titleColor.getBlue() > 50) {
+            throw new RuntimeException("Test failed: Title bar is not of dark colored - " + titleColor);
+        }
+    }
+
+    private static void createUI() throws Exception {
+        SwingUtilities.invokeAndWait(() -> {
+            frame = new JFrame();
+            frame.setUndecorated(false);
+            frame.setSize(400, 400);
+            frame.setLocationRelativeTo(null);
+            frame.getRootPane().putClientProperty("apple.awt.windowDarkAppearance", true);
+            frame.setVisible(true);
+        });
+
+        robot.waitForIdle();
+        SwingUtilities.invokeAndWait(() -> {
+            bounds = frame.getBounds();
+        });
+        robot.waitForIdle();
+
+        robot.mouseMove(frame.getX() + 100, frame.getY() + 5); // pixel at which color will be captured
+        robot.waitForIdle();
+    }
+}