8025130: [macosx] Frame setLocationByPlatform has no effect under Mac OS X
authordmarkov
Thu, 09 Jun 2016 14:21:42 +0300
changeset 39511 a7ad23d3d161
parent 39510 b61e0e7ee904
child 39512 9c164c3f6025
8025130: [macosx] Frame setLocationByPlatform has no effect under Mac OS X Reviewed-by: ssadetsky, alexp
jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CPlatformWindow.java
jdk/src/java.desktop/macosx/native/libawt_lwawt/awt/AWTWindow.m
jdk/test/java/awt/Window/SetWindowLocationByPlatformTest/SetWindowLocationByPlatformTest.java
--- a/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CPlatformWindow.java	Thu Jun 09 13:54:48 2016 +0300
+++ b/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CPlatformWindow.java	Thu Jun 09 14:21:42 2016 +0300
@@ -51,6 +51,7 @@
     private static native void nativeSetNSWindowMenuBar(long nsWindowPtr, long menuBarPtr);
     private static native Insets nativeGetNSWindowInsets(long nsWindowPtr);
     private static native void nativeSetNSWindowBounds(long nsWindowPtr, double x, double y, double w, double h);
+    private static native void nativeSetNSWindowLocationByPlatform(long nsWindowPtr);
     private static native void nativeSetNSWindowStandardFrame(long nsWindowPtr,
             double x, double y, double w, double h);
     private static native void nativeSetNSWindowMinMax(long nsWindowPtr, double minW, double minH, double maxW, double maxH);
@@ -530,6 +531,10 @@
 
         boolean wasMaximized = isMaximized();
 
+        if (visible && target.isLocationByPlatform()) {
+            nativeSetNSWindowLocationByPlatform(getNSWindowPtr());
+        }
+
         // Actually show or hide the window
         LWWindowPeer blocker = (peer == null)? null : peer.getBlocker();
         if (blocker == null || !visible) {
--- a/jdk/src/java.desktop/macosx/native/libawt_lwawt/awt/AWTWindow.m	Thu Jun 09 13:54:48 2016 +0300
+++ b/jdk/src/java.desktop/macosx/native/libawt_lwawt/awt/AWTWindow.m	Thu Jun 09 14:21:42 2016 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2016, 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
@@ -55,6 +55,11 @@
 // window or the app currently has no key window.
 static AWTWindow* lastKeyWindow = nil;
 
+// This variable contains coordinates of a window's top left
+// which was positioned via java.awt.Window.setLocationByPlatform.
+// It would be NSZeroPoint if 'Location by Platform' is not used.
+static NSPoint lastTopLeftPoint;
+
 // --------------------------------------------------------------
 // NSWindow/NSPanel descendants implementation
 #define AWT_NS_WINDOW_IMPLEMENTATION                            \
@@ -1084,6 +1089,31 @@
 
 /*
  * Class:     sun_lwawt_macosx_CPlatformWindow
+ * Method:    nativeSetNSWindowLocationByPlatform
+ * Signature: (J)V
+ */
+JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowLocationByPlatform
+(JNIEnv *env, jclass clazz, jlong windowPtr)
+{
+    JNF_COCOA_ENTER(env);
+
+    NSWindow *nsWindow = OBJC(windowPtr);
+    [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
+
+        if (NSEqualPoints(lastTopLeftPoint, NSZeroPoint)) {
+            // This is the first usage of lastTopLeftPoint. So invoke cascadeTopLeftFromPoint
+            // twice to avoid positioning the window's top left to zero-point, since it may
+            // cause negative user experience.
+            lastTopLeftPoint = [nsWindow cascadeTopLeftFromPoint:lastTopLeftPoint];
+        }
+        lastTopLeftPoint = [nsWindow cascadeTopLeftFromPoint:lastTopLeftPoint];
+    }];
+
+    JNF_COCOA_EXIT(env);
+}
+
+/*
+ * Class:     sun_lwawt_macosx_CPlatformWindow
  * Method:    nativeSetNSWindowMinMax
  * Signature: (JDDDD)V
  */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/awt/Window/SetWindowLocationByPlatformTest/SetWindowLocationByPlatformTest.java	Thu Jun 09 14:21:42 2016 +0300
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2016, 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.
+ */
+
+/* @test
+ * @bug 8025130
+ * @summary setLocationByPlatform has no effect
+ * @author Dmitry Markov
+ * @library ../../regtesthelpers
+ * @build Util
+ * @run main SetWindowLocationByPlatformTest
+ */
+import java.awt.*;
+
+import test.java.awt.regtesthelpers.Util;
+
+public class SetWindowLocationByPlatformTest {
+    public static void main(String[] args) {
+        Robot r = Util.createRobot();
+
+        Frame frame1 = new Frame ("First Frame");
+        frame1.setSize(500, 300);
+        frame1.setLocationByPlatform(true);
+        frame1.setVisible(true);
+        Util.waitForIdle(r);
+
+        Frame frame2 = new Frame ("Second Frame");
+        frame2.setSize(500, 300);
+        frame2.setLocationByPlatform(true);
+        frame2.setVisible(true);
+        Util.waitForIdle(r);
+
+        Point point1 = frame1.getLocationOnScreen();
+        Point point2 = frame2.getLocationOnScreen();
+
+        try {
+            if (point1.equals(point2)) {
+                throw new RuntimeException("Test FAILED: both frames have the same location " + point1);
+            }
+        } finally {
+            frame1.dispose();
+            frame2.dispose();
+        }
+    }
+}
+