Skip to content

Commit

Permalink
Windows 11: use rounded popups with system border and system drop shadow
Browse files Browse the repository at this point in the history
  • Loading branch information
DevCharly committed Jan 26, 2023
1 parent 35e2357 commit 07ad467
Show file tree
Hide file tree
Showing 9 changed files with 338 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* <b>Note</b>: 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.
* <p>
* 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.
* <p>
* Note that the underlying Windows window must be already created,
* otherwise this method returns zero. Use following to ensure this:
* <pre>{@code
* if( !window.isDisplayable() )
* window.addNotify();
* }</pre>
* or invoke this method after packing the window. E.g.
* <pre>{@code
* window.pack();
* long hwnd = getHWND( window );
* }</pre>
*/
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}.
* <p>
* Invokes Win32 API method {@code DwmSetWindowAttribute(DWMWA_WINDOW_CORNER_PREFERENCE)}.
* See https://learn.microsoft.com/en-us/windows/win32/api/dwmapi/nf-dwmapi-dwmsetwindowattribute
* <p>
* 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.
* <p>
* Invokes Win32 API method {@code DwmSetWindowAttribute(DWMWA_BORDER_COLOR)}.
* See https://learn.microsoft.com/en-us/windows/win32/api/dwmapi/nf-dwmapi-dwmsetwindowattribute
* <p>
* Supported since Windows 11 Build 22000.
*/
public native static boolean setWindowBorderColor( long hwnd, int red, int green, int blue );
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
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;
Expand Down Expand Up @@ -88,6 +90,14 @@ public Popup getPopup( Component owner, Component contents, int x, int y )
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 );
}
Expand Down Expand Up @@ -300,6 +310,43 @@ private boolean hasTipLocation( Component owner ) {
((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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ public static Border nonUIResource( Border border ) {
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 );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.Locale;
import java.util.StringTokenizer;
import com.formdev.flatlaf.ui.FlatNativeWindowsLibrary;

/**
* Provides information about the current system.
Expand All @@ -34,9 +35,7 @@ public class SystemInfo
// OS versions
public static final long osVersion;
public static final boolean isWindows_10_orLater;
/** <strong>Note</strong>: 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;
Expand Down Expand Up @@ -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 ));
Expand Down Expand Up @@ -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 ) {
Expand Down
5 changes: 3 additions & 2 deletions flatlaf-natives/flatlaf-natives-windows/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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()
}
} )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
* @author Karl Tauber
*/

HWND getWindowHandle( JNIEnv* env, jobject window );

//---- JNI methods ------------------------------------------------------------

extern "C"
Expand Down Expand Up @@ -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 ) )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,4 @@ class FlatWndProc
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 );
};
102 changes: 102 additions & 0 deletions flatlaf-natives/flatlaf-natives-windows/src/main/cpp/WinWrapper.cpp
Original file line number Diff line number Diff line change
@@ -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 <windows.h>
#include <dwmapi.h>
#include "com_formdev_flatlaf_ui_FlatNativeWindowsLibrary.h"

/**
* @author Karl Tauber
*/

// see FlatWndProc.cpp
HWND getWindowHandle( JNIEnv* env, jobject window );

//---- Utility ----------------------------------------------------------------

extern "C"
JNIEXPORT jlong JNICALL Java_com_formdev_flatlaf_ui_FlatNativeWindowsLibrary_getOSBuildNumberImpl
( JNIEnv* env, jclass cls )
{
OSVERSIONINFO info;
info.dwOSVersionInfoSize = sizeof( info );
if( !::GetVersionEx( &info ) )
return 0;
return info.dwBuildNumber;
}

extern "C"
JNIEXPORT jlong JNICALL Java_com_formdev_flatlaf_ui_FlatNativeWindowsLibrary_getHWND
( JNIEnv* env, jclass cls, jobject window )
{
return reinterpret_cast<jlong>( getWindowHandle( env, window ) );
}

//---- Desktop Window Manager (DWM) -------------------------------------------

// define constants that may not available in older development environments

#ifndef DWMWA_COLOR_DEFAULT

#define DWMWA_WINDOW_CORNER_PREFERENCE 33
#define DWMWA_BORDER_COLOR 34

typedef enum {
DWMWCP_DEFAULT = 0,
DWMWCP_DONOTROUND = 1,
DWMWCP_ROUND = 2,
DWMWCP_ROUNDSMALL = 3
} DWM_WINDOW_CORNER_PREFERENCE;

// Use this constant to reset any window part colors to the system default behavior
#define DWMWA_COLOR_DEFAULT 0xFFFFFFFF

// Use this constant to specify that a window part should not be rendered
#define DWMWA_COLOR_NONE 0xFFFFFFFE

#endif


extern "C"
JNIEXPORT jboolean JNICALL Java_com_formdev_flatlaf_ui_FlatNativeWindowsLibrary_setWindowCornerPreference
( JNIEnv* env, jclass cls, jlong hwnd, jint cornerPreference )
{
if( hwnd == 0 )
return FALSE;

DWM_WINDOW_CORNER_PREFERENCE attr = (DWM_WINDOW_CORNER_PREFERENCE) cornerPreference;
return ::DwmSetWindowAttribute( reinterpret_cast<HWND>( hwnd ), DWMWA_WINDOW_CORNER_PREFERENCE, &attr, sizeof( attr ) ) == S_OK;
}

extern "C"
JNIEXPORT jboolean JNICALL Java_com_formdev_flatlaf_ui_FlatNativeWindowsLibrary_setWindowBorderColor
( JNIEnv* env, jclass cls, jlong hwnd, jint red, jint green, jint blue )
{
if( hwnd == 0 )
return FALSE;

COLORREF attr;
if( red == -1 )
attr = DWMWA_COLOR_DEFAULT;
else if( red == -2 )
attr = DWMWA_COLOR_NONE;
else
attr = RGB( red, green, blue );
return ::DwmSetWindowAttribute( reinterpret_cast<HWND>( hwnd ), DWMWA_BORDER_COLOR, &attr, sizeof( attr ) ) == S_OK;
}
Loading

0 comments on commit 07ad467

Please sign in to comment.