Skip to content

Add Color.Resolved init?(platformColor:) #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ if warningsAsErrorsCondition {
let openSwiftUICoreTarget = Target.target(
name: "OpenSwiftUICore",
dependencies: [
"COpenSwiftUI",
.product(name: "OpenGraphShims", package: "OpenGraph"),
],
swiftSettings: sharedSwiftSettings
Expand All @@ -105,14 +106,6 @@ let openSwiftUIExtensionTarget = Target.target(
],
swiftSettings: sharedSwiftSettings
)
let openSwiftUICoreTestTarget = Target.testTarget(
name: "OpenSwiftUICoreTests",
dependencies: [
"OpenSwiftUICore",
],
exclude: ["README.md"],
swiftSettings: sharedSwiftSettings
)
let openSwiftUITestTarget = Target.testTarget(
name: "OpenSwiftUITests",
dependencies: [
Expand Down Expand Up @@ -145,6 +138,8 @@ let package = Package(
platforms: platforms,
products: [
.library(name: "OpenSwiftUI", targets: ["OpenSwiftUI", "OpenSwiftUIExtension"]),
// FIXME: This will block xcodebuild build(iOS CI) somehow
// .library(name: "COpenSwiftUI", targets: ["COpenSwiftUI"]),
],
targets: [
// TODO: Add SwiftGTK as an backend alternative for UIKit/AppKit on Linux and macOS
Expand Down Expand Up @@ -211,7 +206,6 @@ extension Target {
if attributeGraphCondition {
openSwiftUICoreTarget.addAGSettings()
openSwiftUITarget.addAGSettings()
openSwiftUICoreTestTarget.addAGSettings()
openSwiftUITestTarget.addAGSettings()
openSwiftUITempTestTarget.addAGSettings()
openSwiftUICompatibilityTestTarget.addAGSettings()
Expand Down Expand Up @@ -250,12 +244,10 @@ if swiftTestingCondition {
// Fix it to be 0.3.0 before we bump to Swift 5.10
.package(url: "https://github.com/apple/swift-testing", exact: "0.6.0")
)
openSwiftUICoreTestTarget.addSwiftTestingSettings()
openSwiftUITestTarget.addSwiftTestingSettings()
openSwiftUITempTestTarget.addSwiftTestingSettings()
openSwiftUICompatibilityTestTarget.addSwiftTestingSettings()

package.targets.append(openSwiftUICoreTestTarget)
package.targets.append(openSwiftUITestTarget)
package.targets.append(openSwiftUITempTestTarget)
package.targets.append(openSwiftUICompatibilityTestTarget)
Expand Down
90 changes: 90 additions & 0 deletions Sources/COpenSwiftUI/Overlay/CoreColor/CoreColor.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//
// CoreColor.h
// COpenSwiftUI
// Audited for RELEASE_2024
// Status: Complete

#import "CoreColor.h"

#if OPENSWIFTUI_TARGET_OS_DARWIN

Class CoreColorClass(BOOL isAppKitBased);

#if OPENSWIFTUI_TARGET_OS_OSX
id NSColorSpaceForCGColorSpace(CGColorSpaceRef cgColorSpace);
Class NSColorSpaceClass(void);
#endif

BOOL CoreColorPlatformColorGetComponents(BOOL isAppKitBased, id color, CGFloat *red, CGFloat *green, CGFloat *blue, CGFloat *alpha) {
if (!color) {
return NO;
}
Class colorClass = CoreColorClass(isAppKitBased);
if (colorClass) {
#if OPENSWIFTUI_TARGET_OS_OSX
if (isAppKitBased) {
id colorSpace =
NSColorSpaceForCGColorSpace(CGColorSpaceCreateWithName(kCGColorSpaceExtendedSRGB));
NSColor *nameSpaceColor = [color colorUsingColorSpace:colorSpace];
if (nameSpaceColor) {
[nameSpaceColor getRed:red green:green blue: blue alpha: alpha];
return YES;
} else {
return NO;
}
}
#endif
return ((BOOL (*)(id, SEL))[color methodForSelector:@selector(getRed:green:blue:alpha:)])(color, @selector(getRed:green:blue:alpha:));
} else {
return NO;
}
}

Class CoreColorGetKitColorClass(BOOL isAppKitBased) {
CoreColorClass(isAppKitBased);
}

Class CoreColorClass(BOOL isAppKitBased) {
static BOOL isValid = false;
static Class colorClass;
static dispatch_once_t once;
dispatch_once(&once, ^{
if (isAppKitBased) {
Class class = NSClassFromString(@"NSColor");
colorClass = class;
isValid = class != nil;
} else {
Class class = NSClassFromString(@"UIColor");
colorClass = class;
isValid = class != nil;
}
});
if (isValid) {
return colorClass;
} else {
[NSException raise:@"Invalid core color" format:@""];
}
}

#if OPENSWIFTUI_TARGET_OS_OSX
id NSColorSpaceForCGColorSpace(CGColorSpaceRef cgColorSpace) {
Class colorSpaceClass = NSColorSpaceClass();
if (colorSpaceClass) {
return [[colorSpaceClass alloc] initWithCGColorSpace:cgColorSpace];
} else {
return nil;
}
}

OPENSWIFTUI_INLINE
Class NSColorSpaceClass(void) {
static Class colorSpaceClass;
static dispatch_once_t once;
dispatch_once(&once, ^{
colorSpaceClass = NSClassFromString(@"NSColorSpace");
});
return colorSpaceClass;
}
#endif

#endif
16 changes: 16 additions & 0 deletions Sources/COpenSwiftUI/Shims/CoreFoundation_Private.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// CoreFoundation_Private.c
// COpenSwiftUI

#include "CoreFoundation_Private.h"
#include "dyld_Private.h"

#if !OPENSWIFTUI_TARGET_OS_DARWIN

#define PLATFORM_IOS 2
#define PLATFORM_MACCATALYST 6

bool _CFMZEnabled(void) {
return dyld_get_active_platform() == PLATFORM_MACCATALYST || dyld_get_active_platform() == PLATFORM_IOS;
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@
bool dyld_program_sdk_at_least(dyld_build_version_t version) {
return true;
}

dyld_platform_t dyld_get_active_platform(void) {
return 0;
}
#endif
32 changes: 32 additions & 0 deletions Sources/COpenSwiftUI/include/CoreColor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// CoreColor.h
// COpenSwiftUI
// Audited for RELEASE_2024
// Status: Complete

#ifndef CoreColor_h
#define CoreColor_h

#include "OpenSwiftUIBase.h"

#if OPENSWIFTUI_TARGET_OS_DARWIN

#if OPENSWIFTUI_TARGET_OS_IOS
#include <UIKit/UIKit.h>
#else
#include <AppKit/AppKit.h>
#endif

OPENSWIFTUI_ASSUME_NONNULL_BEGIN

OPENSWIFTUI_EXPORT
BOOL CoreColorPlatformColorGetComponents(BOOL isAppKitBased, id color, CGFloat *red, CGFloat *green, CGFloat *blue, CGFloat *alpha);

OPENSWIFTUI_EXPORT
Class CoreColorGetKitColorClass(BOOL isAppKitBased);

OPENSWIFTUI_ASSUME_NONNULL_END

#endif

#endif /* CoreColor_h */
13 changes: 13 additions & 0 deletions Sources/COpenSwiftUI/include/CoreFoundation_Private.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// CoreFoundation_Private.h
// COpenSwiftUI

#ifndef CoreFoundation_Private_h
#define CoreFoundation_Private_h

#include "OpenSwiftUIBase.h"

OPENSWIFTUI_EXPORT
bool _CFMZEnabled(void);

#endif /* CoreFoundation_Private_h */
13 changes: 12 additions & 1 deletion Sources/COpenSwiftUI/include/OpenSwiftUITargetConditionals.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,24 @@

#define OPENSWIFTUI_TARGET_OS_WIN32 OPENSWIFTUI_TARGET_OS_WINDOWS
#define OPENSWIFTUI_TARGET_OS_MAC OPENSWIFTUI_TARGET_OS_DARWIN
#define OPENSWIFTUI_TARGET_OS_OSX OPENSWIFTUI_TARGET_OS_DARWIN

/* OpenSwiftUI Addition Begin */
#if OPENSWIFTUI_TARGET_OS_DARWIN
#include <TargetConditionals.h>
#define OPENSWIFTUI_TARGET_OS_OSX TARGET_OS_OSX
#define OPENSWIFTUI_TARGET_OS_IPHONE TARGET_OS_IPHONE
#define OPENSWIFTUI_TARGET_OS_IOS TARGET_OS_IOS
#define OPENSWIFTUI_TARGET_OS_WATCH TARGET_OS_WATCH
#define OPENSWIFTUI_TARGET_OS_TV TARGET_OS_TV
#else
// iOS, watchOS, and tvOS are not supported
#define OPENSWIFTUI_TARGET_OS_IPHONE 0
#define OPENSWIFTUI_TARGET_OS_IOS 0
#define OPENSWIFTUI_TARGET_OS_WATCH 0
#define OPENSWIFTUI_TARGET_OS_TV 0
#endif

/* OpenSwiftUI Addition End */

#if __x86_64__
#define OPENSWIFTUI_TARGET_CPU_PPC 0
Expand Down
2 changes: 1 addition & 1 deletion Sources/COpenSwiftUI/include/__CGPathParseString.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// __CGPathParseString.m
// __CGPathParseString.h
// COpenSwiftUI

#ifndef __CGPathParseString_h
Expand Down
6 changes: 5 additions & 1 deletion Sources/COpenSwiftUI/include/dyld_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ typedef struct {
} dyld_build_version_t;

// Always return true on non-Darwin platform
extern bool dyld_program_sdk_at_least(dyld_build_version_t version);
OPENSWIFTUI_EXPORT
bool dyld_program_sdk_at_least(dyld_build_version_t version);

OPENSWIFTUI_EXPORT
dyld_platform_t dyld_get_active_platform(void);

#endif /* dyld_Private_h */
7 changes: 0 additions & 7 deletions Sources/OpenSwiftUI/Core/Util/Utils.swift

This file was deleted.

22 changes: 22 additions & 0 deletions Sources/OpenSwiftUI/View/Graphic/Color/ColorResolved.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// Status: WIP

import Foundation
import OpenSwiftUICore
internal import COpenSwiftUI

// MARK: - Color.Resolved

Expand Down Expand Up @@ -289,3 +291,23 @@
}
return sRGB > 0 ? result : -result
}

#if canImport(Darwin)

// MARK: - Color.Resolved + platformColor

extension Color.Resolved {
package init?(platformColor: AnyObject) {
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
var alpha: CGFloat = 0
let result = CoreColorPlatformColorGetComponents(isAppKitBased(), platformColor, &red, &green, &blue, &alpha)
if result {
self.init(red: Float(red), green: Float(green), blue: Float(blue), opacity: Float(alpha))
} else {
return nil

Check warning on line 309 in Sources/OpenSwiftUI/View/Graphic/Color/ColorResolved.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUI/View/Graphic/Color/ColorResolved.swift#L300-L309

Added lines #L300 - L309 were not covered by tests
}
}
}
#endif
50 changes: 50 additions & 0 deletions Sources/OpenSwiftUICore/Util/Utils.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// Utils.swift
// OpenSwiftUICore
//
// Audited for RELEASE_2024
// Status: WIP

internal import COpenSwiftUI

@inlinable
@inline(__always)
func asOptional<Value>(_ value: Value) -> Value? {
func unwrap<T>() -> T { value as! T }
let optionalValue: Value? = unwrap()
return optionalValue

Check warning on line 15 in Sources/OpenSwiftUICore/Util/Utils.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Util/Utils.swift#L12-L15

Added lines #L12 - L15 were not covered by tests
}

@inline(__always)
package func isCatalyst() -> Bool {
#if os(iOS) || os(tvOS) || os(watchOS)
false
#elseif os(macOS)
// NOTE: use runtime check instead of #if targetEnvironment(macCatalyst) here
_CFMZEnabled()
#else
false
#endif

Check warning on line 27 in Sources/OpenSwiftUICore/Util/Utils.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Util/Utils.swift#L19-L27

Added lines #L19 - L27 were not covered by tests
}

@inline(__always)
package func isUIKitBased() -> Bool {
#if os(iOS) || os(tvOS) || os(watchOS)
true
#elseif os(macOS)
_CFMZEnabled()
#else
false
#endif

Check warning on line 38 in Sources/OpenSwiftUICore/Util/Utils.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Util/Utils.swift#L31-L38

Added lines #L31 - L38 were not covered by tests
}

@inline(__always)
package func isAppKitBased() -> Bool {
#if os(iOS) || os(tvOS) || os(watchOS)
false
#elseif os(macOS)
!_CFMZEnabled()
#else
false
#endif

Check warning on line 49 in Sources/OpenSwiftUICore/Util/Utils.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Util/Utils.swift#L42-L49

Added lines #L42 - L49 were not covered by tests
}
8 changes: 0 additions & 8 deletions Tests/OpenSwiftUICoreTests/README.md

This file was deleted.

15 changes: 0 additions & 15 deletions Tests/OpenSwiftUICoreTests/Scaffolding.swift

This file was deleted.

Loading