-
-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Windows 11: use rounded popups with system border and system drop shadow
- Loading branch information
Showing
9 changed files
with
338 additions
and
10 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatNativeWindowsLibrary.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
flatlaf-natives/flatlaf-natives-windows/src/main/cpp/WinWrapper.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.