From 2f4666a71ecd3c921b669b5f2bd3f595dd5ef047 Mon Sep 17 00:00:00 2001 From: Kevin Rushforth Date: Wed, 15 Jul 2020 13:30:45 +0000 Subject: [PATCH] 8248490: [macOS] Undecorated stage does not minimize Reviewed-by: arapte, aghaisas --- .../src/main/native-glass/mac/GlassWindow.m | 18 ++- .../test/robot/javafx/stage/IconifyTest.java | 118 ++++++++++++++++++ 2 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 tests/system/src/test/java/test/robot/javafx/stage/IconifyTest.java diff --git a/modules/javafx.graphics/src/main/native-glass/mac/GlassWindow.m b/modules/javafx.graphics/src/main/native-glass/mac/GlassWindow.m index 52702f0b8ed..59b8c9522ed 100644 --- a/modules/javafx.graphics/src/main/native-glass/mac/GlassWindow.m +++ b/modules/javafx.graphics/src/main/native-glass/mac/GlassWindow.m @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2011, 2020, 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 @@ -1405,6 +1405,16 @@ static jlong _createWindowCommonDo(JNIEnv *env, jobject jWindow, jlong jOwnerPtr { GlassWindow *window = getGlassWindow(env, jPtr); + NSUInteger styleMask = [window->nsWindow styleMask]; + BOOL isMiniaturizable = (styleMask & NSMiniaturizableWindowMask) != 0; + + // if the window does not have NSMiniaturizableWindowMask set + // we need to temporarily set it to allow the window to + // be programmatically minimized or restored. + if (!isMiniaturizable) { + [window->nsWindow setStyleMask: styleMask | NSMiniaturizableWindowMask]; + } + if (jMiniaturize == JNI_TRUE) { [window->nsWindow miniaturize:nil]; @@ -1413,6 +1423,12 @@ static jlong _createWindowCommonDo(JNIEnv *env, jobject jWindow, jlong jOwnerPtr { [window->nsWindow deminiaturize:nil]; } + + // Restore the state of NSMiniaturizableWindowMask + if (!isMiniaturizable) { + [window->nsWindow setStyleMask: styleMask]; + } + } GLASS_POOL_EXIT; GLASS_CHECK_EXCEPTION(env); diff --git a/tests/system/src/test/java/test/robot/javafx/stage/IconifyTest.java b/tests/system/src/test/java/test/robot/javafx/stage/IconifyTest.java new file mode 100644 index 00000000000..5965d554e19 --- /dev/null +++ b/tests/system/src/test/java/test/robot/javafx/stage/IconifyTest.java @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2020, 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. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * 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. + */ + +package test.robot.javafx.stage; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import javafx.application.Platform; +import javafx.scene.Scene; +import javafx.scene.layout.Pane; +import javafx.scene.paint.Color; +import javafx.stage.Stage; +import javafx.stage.StageStyle; +import org.junit.Test; +import test.robot.testharness.VisualTestBase; + +import static org.junit.Assert.assertTrue; +import static test.util.Util.TIMEOUT; + +/** + * Test ability to programmatically iconify UNDECORATED and TRANSPARENT stages + */ +public class IconifyTest extends VisualTestBase { + + private static final int WIDTH = 300; + private static final int HEIGHT = 300; + + private static final Color BOTTOM_COLOR = Color.LIME; + private static final Color TOP_COLOR = Color.RED; + + private static final double TOLERANCE = 0.07; + + private Stage bottomStage; + private Stage topStage; + + public void canIconifyStage(StageStyle stageStyle) throws Exception { + final CountDownLatch shownLatch = new CountDownLatch(2); + + runAndWait(() -> { + // Bottom stage, should be visible after top stage is iconified + bottomStage = getStage(true); + Scene bottomScene = new Scene(new Pane(), WIDTH, HEIGHT); + bottomScene.setFill(BOTTOM_COLOR); + bottomStage.setScene(bottomScene); + bottomStage.setX(0); + bottomStage.setY(0); + bottomStage.setOnShown(e -> Platform.runLater(shownLatch::countDown)); + bottomStage.show(); + + // Top stage, will be inconified + topStage = getStage(true); + topStage.initStyle(stageStyle); + Scene topScene = new Scene(new Pane(), WIDTH, HEIGHT); + topScene.setFill(TOP_COLOR); + topStage.setScene(topScene); + topStage.setX(0); + topStage.setY(0); + topStage.setOnShown(e -> Platform.runLater(shownLatch::countDown)); + topStage.show(); + }); + + assertTrue("Timeout waiting for stages to be shown", + shownLatch.await(TIMEOUT, TimeUnit.MILLISECONDS)); + + runAndWait(() -> { + topStage.toFront(); + }); + + sleep(500); + runAndWait(() -> { + Color color = getColor(100, 100); + assertColorEquals(TOP_COLOR, color, TOLERANCE); + }); + + runAndWait(() -> { + topStage.setIconified(true); + }); + + sleep(500); + runAndWait(() -> { + Color color = getColor(100, 100); + assertColorEquals(BOTTOM_COLOR, color, TOLERANCE); + }); + } + + @Test(timeout = 15000) + public void canIconifyUndecoratedStage() throws Exception { + canIconifyStage(StageStyle.UNDECORATED); + } + + @Test(timeout = 15000) + public void canIconifyTransparentStage() throws Exception { + canIconifyStage(StageStyle.TRANSPARENT); + } + +}