macOS: native rounded borders for popups (issue #715)

This commit is contained in:
Karl Tauber
2023-12-09 16:12:35 +01:00
parent c25d857e78
commit 6f32236fb7
23 changed files with 639 additions and 31 deletions

View File

@@ -0,0 +1,129 @@
/*
* Copyright 2023 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.
*/
import java.io.FileOutputStream
val minOsARM64 = "11.0"
val minOsX86_64 = "10.14"
plugins {
`cpp-library`
`flatlaf-cpp-library`
`flatlaf-jni-headers`
}
flatlafJniHeaders {
headers = listOf( "com_formdev_flatlaf_ui_FlatNativeMacLibrary.h" )
}
library {
targetMachines.set( listOf(
machines.macOS.architecture( "arm64" ),
machines.macOS.x86_64
) )
}
var javaHome = System.getProperty( "java.home" )
if( javaHome.endsWith( "jre" ) )
javaHome += "/.."
tasks {
register( "build-natives" ) {
group = "build"
description = "Builds natives"
if( org.gradle.internal.os.OperatingSystem.current().isMacOsX() )
dependsOn( "linkReleaseArm64", "linkReleaseX86-64" )
}
withType<CppCompile>().configureEach {
onlyIf { name.contains( "Release" ) }
// generate and copy needed JNI headers
dependsOn( "jni-headers" )
includes.from(
"${javaHome}/include",
"${javaHome}/include/darwin"
)
// compile Objective-C++ sources
source.from( files( "src/main/objcpp" )
.asFileTree.matching { include( "**/*.mm" ) } )
val isARM64 = name.contains( "Arm64" )
val minOs = if( isARM64 ) minOsARM64 else minOsX86_64
compilerArgs.addAll( toolChain.map {
when( it ) {
is Gcc, is Clang -> listOf( "-x", "objective-c++", "-mmacosx-version-min=$minOs" )
else -> emptyList()
}
} )
}
withType<LinkSharedLibrary>().configureEach {
onlyIf { name.contains( "Release" ) }
val nativesDir = project( ":flatlaf-core" ).projectDir.resolve( "src/main/resources/com/formdev/flatlaf/natives" )
val isARM64 = name.contains( "Arm64" )
val minOs = if( isARM64 ) minOsARM64 else minOsX86_64
val libraryName = if( isARM64 ) "flatlaf-macos-arm64.dylib" else "flatlaf-macos-x86_64.dylib"
linkerArgs.addAll( toolChain.map {
when( it ) {
is Gcc, is Clang -> listOf( "-lobjc", "-framework", "Cocoa", "-mmacosx-version-min=$minOs" )
else -> emptyList()
}
} )
doLast {
// copy shared library to flatlaf-core resources
copy {
from( linkedFile )
into( nativesDir )
rename( "flatlaf-natives-macos.dylib", libraryName )
}
///*dump
val dylib = linkedFile.asFile.get()
val dylibDir = dylib.parent
exec { commandLine( "size", dylib ) }
exec { commandLine( "size", "-m", dylib ) }
exec {
commandLine( "objdump",
// commands
"--archive-headers",
"--section-headers",
"--private-headers",
"--reloc",
"--dynamic-reloc",
"--raw-clang-ast",
"--syms",
"--unwind-info",
// options
"--bind",
// "--private-header",
// files
dylib )
standardOutput = FileOutputStream( "$dylibDir/objdump.txt" )
}
exec { commandLine( "objdump", "--disassemble-all", dylib ); standardOutput = FileOutputStream( "$dylibDir/disassemble.txt" ) }
exec { commandLine( "objdump", "--full-contents", dylib ); standardOutput = FileOutputStream( "$dylibDir/full-contents.txt" ) }
//dump*/
}
}
}

View File

@@ -0,0 +1,45 @@
// from https://github.com/apple/openjdk/blob/xcodejdk14-release/apple/JavaNativeFoundation/JavaNativeFoundation/JNFRunLoop.h
/*
* Copyright (c) 2009-2020 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* --
*
* Used to perform selectors and blocks in the Java runloop mode.
*/
#import <Foundation/Foundation.h>
@interface FlatJNFRunLoop : NSObject { }
+ (NSString *)javaRunLoopMode;
+ (void)performOnMainThread:(SEL)aSelector on:(id)target withObject:(id)arg waitUntilDone:(BOOL)wait;
+ (void)performOnMainThreadWaiting:(BOOL)waitUntilDone withBlock:(void (^)(void))block;
@end

View File

@@ -0,0 +1,41 @@
/*
* Copyright 2023 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.
*/
#import <Foundation/NSException.h>
#import <Foundation/NSString.h>
#import <jni.h>
/**
* @author Karl Tauber
*/
// from JNFJNI.h
#ifndef jlong_to_ptr
#define jlong_to_ptr(a) ((void *)(uintptr_t)(a))
#endif
#define JNI_COCOA_ENTER() \
@autoreleasepool { \
@try {
#define JNI_COCOA_EXIT() \
} @catch( NSException *ex ) { \
NSLog( @"Exception: %@\nReason: %@\nUser Info: %@\nStack: %@", \
[ex name], [ex reason], [ex userInfo], [ex callStackSymbols] ); \
} \
}

View File

@@ -0,0 +1,29 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_formdev_flatlaf_ui_FlatNativeMacLibrary */
#ifndef _Included_com_formdev_flatlaf_ui_FlatNativeMacLibrary
#define _Included_com_formdev_flatlaf_ui_FlatNativeMacLibrary
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_formdev_flatlaf_ui_FlatNativeMacLibrary
* Method: getWindowPtr
* Signature: (Ljava/awt/Window;)J
*/
JNIEXPORT jlong JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_getWindowPtr
(JNIEnv *, jclass, jobject);
/*
* Class: com_formdev_flatlaf_ui_FlatNativeMacLibrary
* Method: setWindowRoundedBorder
* Signature: (JFFI)V
*/
JNIEXPORT void JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_setWindowRoundedBorder
(JNIEnv *, jclass, jlong, jfloat, jfloat, jint);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,76 @@
// from https://github.com/apple/openjdk/blob/xcodejdk14-release/apple/JavaNativeFoundation/JavaNativeFoundation/JNFRunLoop.m
/*
* Copyright (c) 2009-2020 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#import "JNFRunLoop.h"
#import <Cocoa/Cocoa.h>
NSString *JNFRunLoopDidStartNotification = @"JNFRunLoopDidStartNotification";
static NSString *AWTRunLoopMode = @"AWTRunLoopMode";
static NSArray *sPerformModes = nil;
@implementation FlatJNFRunLoop
+ (void)initialize {
if (sPerformModes) return;
sPerformModes = [[NSArray alloc] initWithObjects:NSDefaultRunLoopMode, NSModalPanelRunLoopMode, NSEventTrackingRunLoopMode, AWTRunLoopMode, nil];
}
+ (NSString *)javaRunLoopMode {
return AWTRunLoopMode;
}
+ (void)performOnMainThread:(SEL)aSelector on:(id)target withObject:(id)arg waitUntilDone:(BOOL)waitUntilDone {
[target performSelectorOnMainThread:aSelector withObject:arg waitUntilDone:waitUntilDone modes:sPerformModes];
}
+ (void)_performDirectBlock:(void (^)(void))block {
block();
}
+ (void)_performCopiedBlock:(void (^)(void))newBlock {
newBlock();
Block_release(newBlock);
}
+ (void)performOnMainThreadWaiting:(BOOL)waitUntilDone withBlock:(void (^)(void))block {
if (waitUntilDone) {
[self performOnMainThread:@selector(_performDirectBlock:) on:self withObject:block waitUntilDone:YES];
} else {
void (^newBlock)(void) = Block_copy(block);
[self performOnMainThread:@selector(_performCopiedBlock:) on:self withObject:newBlock waitUntilDone:NO];
}
}
@end

View File

@@ -0,0 +1,90 @@
/*
* Copyright 2023 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.
*/
#import <Cocoa/Cocoa.h>
#import <jni.h>
#import "JNIUtils.h"
#import "JNFRunLoop.h"
#import "com_formdev_flatlaf_ui_FlatNativeMacLibrary.h"
/**
* @author Karl Tauber
*/
extern "C"
JNIEXPORT jlong JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_getWindowPtr
( JNIEnv* env, jclass cls, jobject window )
{
if( window == NULL )
return NULL;
JNI_COCOA_ENTER()
// get field java.awt.Component.peer
jfieldID peerID = env->GetFieldID( env->GetObjectClass( window ), "peer", "Ljava/awt/peer/ComponentPeer;" );
jobject peer = (peerID != NULL) ? env->GetObjectField( window, peerID ) : NULL;
if( peer == NULL )
return NULL;
// get field sun.lwawt.LWWindowPeer.platformWindow
jfieldID platformWindowID = env->GetFieldID( env->GetObjectClass( peer ), "platformWindow", "Lsun/lwawt/PlatformWindow;" );
jobject platformWindow = (platformWindowID != NULL) ? env->GetObjectField( peer, platformWindowID ) : NULL;
if( platformWindow == NULL )
return NULL;
// get field sun.lwawt.macosx.CFRetainedResource.ptr
jfieldID ptrID = env->GetFieldID( env->GetObjectClass( platformWindow ), "ptr", "J" );
return (ptrID != NULL) ? env->GetLongField( platformWindow, ptrID ) : NULL;
JNI_COCOA_EXIT()
}
extern "C"
JNIEXPORT void JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_setWindowRoundedBorder
( JNIEnv* env, jclass cls, jlong windowPtr, jfloat radius, jfloat borderWidth, jint borderColor )
{
if( windowPtr == 0 )
return;
JNI_COCOA_ENTER()
[FlatJNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
NSWindow* window = (NSWindow *) jlong_to_ptr( windowPtr );
window.hasShadow = YES;
window.contentView.wantsLayer = YES;
window.contentView.layer.cornerRadius = radius;
window.contentView.layer.masksToBounds = YES;
window.contentView.layer.borderWidth = borderWidth;
if( borderWidth > 0 ) {
CGFloat red = ((borderColor >> 16) & 0xff) / 255.;
CGFloat green = ((borderColor >> 8) & 0xff) / 255.;
CGFloat blue = (borderColor & 0xff) / 255.;
CGFloat alpha = ((borderColor >> 24) & 0xff) / 255.;
window.contentView.layer.borderColor = [[NSColor colorWithDeviceRed:red green:green blue:blue alpha:alpha] CGColor];
}
window.backgroundColor = NSColor.clearColor;
window.opaque = NO;
[window.contentView.layer removeAllAnimations];
[window invalidateShadow];
}];
JNI_COCOA_EXIT()
}