diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatNativeWindowsLibrary.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatNativeWindowsLibrary.java new file mode 100644 index 00000000..9df5ae23 --- /dev/null +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatNativeWindowsLibrary.java @@ -0,0 +1,106 @@ +/* + * Copyright 2022 FormDev Software GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.formdev.flatlaf.ui; + +import java.awt.Window; + +/** + * Native methods for Windows. + *
+ * Note: This is private API. Do not use! + * + * @author Karl Tauber + * @since 3.1 + */ +public class FlatNativeWindowsLibrary +{ + private static long osBuildNumber = Long.MIN_VALUE; + + public static boolean isLoaded() { + return FlatNativeLibrary.isLoaded(); + } + + /** + * Gets the Windows operating system build number. + *
+ * Invokes Win32 API method {@code GetVersionEx()} and returns {@code OSVERSIONINFO.dwBuildNumber}. + * See https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getversionexa + */ + public static long getOSBuildNumber() { + if( osBuildNumber == Long.MIN_VALUE ) + osBuildNumber = getOSBuildNumberImpl(); + return osBuildNumber; + } + + /** + * Invokes Win32 API method {@code GetVersionEx()} and returns {@code OSVERSIONINFO.dwBuildNumber}. + * See https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getversionexa + */ + private native static long getOSBuildNumberImpl(); + + /** + * Gets the Windows window handle (HWND) for the given Swing window. + *
+ * Note that the underlying Windows window must be already created, + * otherwise this method returns zero. Use following to ensure this: + *
{@code
+ * if( !window.isDisplayable() )
+ * window.addNotify();
+ * }
+ * or invoke this method after packing the window. E.g.
+ * {@code
+ * window.pack();
+ * long hwnd = getHWND( window );
+ * }
+ */
+ public native static long getHWND( Window window );
+
+ /**
+ * DWM_WINDOW_CORNER_PREFERENCE
+ * see https://learn.microsoft.com/en-us/windows/win32/api/dwmapi/ne-dwmapi-dwm_window_corner_preference
+ */
+ public static final int
+ DWMWCP_DEFAULT = 0,
+ DWMWCP_DONOTROUND = 1,
+ DWMWCP_ROUND = 2,
+ DWMWCP_ROUNDSMALL = 3;
+
+ /**
+ * Sets the rounded corner preference for the window.
+ * Allowed values are {@link #DWMWCP_DEFAULT}, {@link #DWMWCP_DONOTROUND},
+ * {@link #DWMWCP_ROUND} and {@link #DWMWCP_ROUNDSMALL}.
+ * + * Invokes Win32 API method {@code DwmSetWindowAttribute(DWMWA_WINDOW_CORNER_PREFERENCE)}. + * See https://learn.microsoft.com/en-us/windows/win32/api/dwmapi/nf-dwmapi-dwmsetwindowattribute + *
+ * Supported since Windows 11 Build 22000. + */ + public native static boolean setWindowCornerPreference( long hwnd, int cornerPreference ); + + /** + * Sets the color of the window border. + * The red/green/blue values must be in range {@code 0 - 255}. + * If red is {@code -1}, then the system default border color is used (useful to reset the border color). + * If red is {@code -2}, then no border is painted. + *
+ * Invokes Win32 API method {@code DwmSetWindowAttribute(DWMWA_BORDER_COLOR)}. + * See https://learn.microsoft.com/en-us/windows/win32/api/dwmapi/nf-dwmapi-dwmsetwindowattribute + *
+ * Supported since Windows 11 Build 22000.
+ */
+ public native static boolean setWindowBorderColor( long hwnd, int red, int green, int blue );
+}
diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatPopupFactory.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatPopupFactory.java
index c58b72eb..1ea34982 100644
--- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatPopupFactory.java
+++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatPopupFactory.java
@@ -52,6 +52,8 @@ import javax.swing.SwingUtilities;
import javax.swing.ToolTipManager;
import javax.swing.UIManager;
import javax.swing.border.Border;
+import javax.swing.border.EmptyBorder;
+import javax.swing.border.LineBorder;
import com.formdev.flatlaf.FlatClientProperties;
import com.formdev.flatlaf.util.SystemInfo;
import com.formdev.flatlaf.util.UIScale;
@@ -88,6 +90,14 @@ public class FlatPopupFactory
if( SystemInfo.isMacOS || SystemInfo.isLinux )
return new NonFlashingPopup( getPopupForScreenOfOwner( owner, contents, x, y, true ), contents );
+ // Windows 11 with FlatLaf native library can use rounded corners and shows drop shadow for heavy weight popups
+ if( SystemInfo.isWindows_11_orLater && FlatNativeWindowsLibrary.isLoaded() ) {
+ NonFlashingPopup popup = new NonFlashingPopup( getPopupForScreenOfOwner( owner, contents, x, y, true ), contents );
+ if( popup.popupWindow != null )
+ setupWindows11Border( popup.popupWindow, contents );
+ return popup;
+ }
+
// create drop shadow popup
return new DropShadowPopup( getPopupForScreenOfOwner( owner, contents, x, y, forceHeavyWeight ), owner, contents );
}
@@ -300,6 +310,43 @@ public class FlatPopupFactory
((JComponent)owner).getToolTipLocation( me ) != null;
}
+ private static void setupWindows11Border( Window popupWindow, Component contents ) {
+ // make sure that the Windows 11 window is created
+ if( !popupWindow.isDisplayable() )
+ popupWindow.addNotify();
+
+ // get window handle
+ long hwnd = FlatNativeWindowsLibrary.getHWND( popupWindow );
+
+ // set corner preference
+ FlatNativeWindowsLibrary.setWindowCornerPreference( hwnd, FlatNativeWindowsLibrary.DWMWCP_ROUNDSMALL );
+
+ // set border color
+ int red = -1; // use system default color
+ int green = 0;
+ int blue = 0;
+ if( contents instanceof JComponent ) {
+ Border border = ((JComponent)contents).getBorder();
+ border = FlatUIUtils.unwrapNonUIResourceBorder( border );
+
+ // get color from border of contents (e.g. JPopupMenu or JToolTip)
+ Color borderColor = null;
+ if( border instanceof FlatLineBorder )
+ borderColor = ((FlatLineBorder)border).getLineColor();
+ else if( border instanceof LineBorder )
+ borderColor = ((LineBorder)border).getLineColor();
+ else if( border instanceof EmptyBorder )
+ red = -2; // do not paint border
+
+ if( borderColor != null ) {
+ red = borderColor.getRed();
+ green = borderColor.getGreen();
+ blue = borderColor.getBlue();
+ }
+ }
+ FlatNativeWindowsLibrary.setWindowBorderColor( hwnd, red, green, blue );
+ }
+
//---- class NonFlashingPopup ---------------------------------------------
private class NonFlashingPopup
diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java
index 2378eefa..c43df699 100644
--- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java
+++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java
@@ -200,6 +200,10 @@ public class FlatUIUtils
return (border instanceof UIResource) ? new NonUIResourceBorder( border ) : border;
}
+ static Border unwrapNonUIResourceBorder( Border border ) {
+ return (border instanceof NonUIResourceBorder) ? ((NonUIResourceBorder)border).delegate : border;
+ }
+
public static int minimumWidth( JComponent c, int minimumWidth ) {
return FlatClientProperties.clientPropertyInt( c, FlatClientProperties.MINIMUM_WIDTH, minimumWidth );
}
diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/util/SystemInfo.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/SystemInfo.java
index 8678afcd..6be6e98f 100644
--- a/flatlaf-core/src/main/java/com/formdev/flatlaf/util/SystemInfo.java
+++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/SystemInfo.java
@@ -18,6 +18,7 @@ package com.formdev.flatlaf.util;
import java.util.Locale;
import java.util.StringTokenizer;
+import com.formdev.flatlaf.ui.FlatNativeWindowsLibrary;
/**
* Provides information about the current system.
@@ -34,9 +35,7 @@ public class SystemInfo
// OS versions
public static final long osVersion;
public static final boolean isWindows_10_orLater;
- /** Note: This requires Java 8u321, 11.0.14, 17.0.2 or 18 (or later).
- * (see https://bugs.openjdk.java.net/browse/JDK-8274840)
- * @since 2 */ public static final boolean isWindows_11_orLater;
+ /** @since 2 */ public static final boolean isWindows_11_orLater;
public static final boolean isMacOS_10_11_ElCapitan_orLater;
public static final boolean isMacOS_10_14_Mojave_orLater;
public static final boolean isMacOS_10_15_Catalina_orLater;
@@ -80,8 +79,6 @@ public class SystemInfo
// OS versions
osVersion = scanVersion( System.getProperty( "os.version" ) );
isWindows_10_orLater = (isWindows && osVersion >= toVersion( 10, 0, 0, 0 ));
- isWindows_11_orLater = (isWindows_10_orLater && osName.length() > "windows ".length() &&
- scanVersion( osName.substring( "windows ".length() ) ) >= toVersion( 11, 0, 0, 0 ));
isMacOS_10_11_ElCapitan_orLater = (isMacOS && osVersion >= toVersion( 10, 11, 0, 0 ));
isMacOS_10_14_Mojave_orLater = (isMacOS && osVersion >= toVersion( 10, 14, 0, 0 ));
isMacOS_10_15_Catalina_orLater = (isMacOS && osVersion >= toVersion( 10, 15, 0, 0 ));
@@ -119,6 +116,24 @@ public class SystemInfo
isMacFullWindowContentSupported =
javaVersion >= toVersion( 11, 0, 8, 0 ) ||
(javaVersion >= toVersion( 1, 8, 0, 292 ) && !isJava_9_orLater);
+
+
+ // Note: Keep following at the end of this block because (optional) loading
+ // of native library uses fields of this class. E.g. isX86_64
+
+ // Windows 11 detection is implemented in Java 8u321, 11.0.14, 17.0.2 and 18 (or later).
+ // (see https://bugs.openjdk.java.net/browse/JDK-8274840)
+ // For older Java versions, use native library to get OS build number.
+ boolean isWin_11_orLater = false;
+ try {
+ isWin_11_orLater = (isWindows_10_orLater &&
+ (scanVersion( StringUtils.removeLeading( osName, "windows " ) ) >= toVersion( 11, 0, 0, 0 )) ||
+ (FlatNativeWindowsLibrary.isLoaded() && FlatNativeWindowsLibrary.getOSBuildNumber() >= 22000));
+ } catch( Throwable ex ) {
+ // catch to avoid that application can not start if native library is not up-to-date
+ LoggingFacade.INSTANCE.logSevere( null, ex );
+ }
+ isWindows_11_orLater = isWin_11_orLater;
}
public static long scanVersion( String version ) {
diff --git a/flatlaf-natives/flatlaf-natives-windows/build.gradle.kts b/flatlaf-natives/flatlaf-natives-windows/build.gradle.kts
index 99acaf2a..69490664 100644
--- a/flatlaf-natives/flatlaf-natives-windows/build.gradle.kts
+++ b/flatlaf-natives/flatlaf-natives-windows/build.gradle.kts
@@ -22,6 +22,7 @@ plugins {
flatlafJniHeaders {
headers = listOf(
+ "com_formdev_flatlaf_ui_FlatNativeWindowsLibrary.h",
"com_formdev_flatlaf_ui_FlatWindowsNativeWindowBorder.h",
"com_formdev_flatlaf_ui_FlatWindowsNativeWindowBorder_WndProc.h"
)
@@ -74,8 +75,8 @@ tasks {
linkerArgs.addAll( toolChain.map {
when( it ) {
- is Gcc, is Clang -> listOf( "-l${jawt}", "-lUser32", "-lGdi32", "-lshell32", "-lAdvAPI32", "-lKernel32" )
- is VisualCpp -> listOf( "${jawt}.lib", "User32.lib", "Gdi32.lib", "shell32.lib", "AdvAPI32.lib", "Kernel32.lib", "/NODEFAULTLIB" )
+ is Gcc, is Clang -> listOf( "-l${jawt}", "-lUser32", "-lGdi32", "-lshell32", "-lAdvAPI32", "-lKernel32", "-lDwmapi" )
+ is VisualCpp -> listOf( "${jawt}.lib", "User32.lib", "Gdi32.lib", "shell32.lib", "AdvAPI32.lib", "Kernel32.lib", "Dwmapi.lib", "/NODEFAULTLIB" )
else -> emptyList()
}
} )
diff --git a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/FlatWndProc.cpp b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/FlatWndProc.cpp
index 77b8006d..45d6b850 100644
--- a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/FlatWndProc.cpp
+++ b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/FlatWndProc.cpp
@@ -29,6 +29,8 @@
* @author Karl Tauber
*/
+HWND getWindowHandle( JNIEnv* env, jobject window );
+
//---- JNI methods ------------------------------------------------------------
extern "C"
@@ -540,7 +542,7 @@ void FlatWndProc::setMenuItemState( HMENU systemMenu, int item, bool enabled ) {
::SetMenuItemInfo( systemMenu, item, FALSE, &mii );
}
-HWND FlatWndProc::getWindowHandle( JNIEnv* env, jobject window ) {
+HWND getWindowHandle( JNIEnv* env, jobject window ) {
JAWT awt;
awt.version = JAWT_VERSION_1_4;
if( !JAWT_GetAWT( env, &awt ) )
diff --git a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/FlatWndProc.h b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/FlatWndProc.h
index 768b7779..96e255d1 100644
--- a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/FlatWndProc.h
+++ b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/FlatWndProc.h
@@ -67,6 +67,4 @@ private:
void sendMessageToClientArea( HWND hwnd, int uMsg, LPARAM lParam );
void openSystemMenu( HWND hwnd, int x, int y );
void setMenuItemState( HMENU systemMenu, int item, bool enabled );
-
- static HWND getWindowHandle( JNIEnv* env, jobject window );
};
diff --git a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/WinWrapper.cpp b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/WinWrapper.cpp
new file mode 100644
index 00000000..37fc9e84
--- /dev/null
+++ b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/WinWrapper.cpp
@@ -0,0 +1,102 @@
+/*
+ * Copyright 2022 FormDev Software GmbH
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// avoid inlining of printf()
+#define _NO_CRT_STDIO_INLINE
+
+#include