Skip to content

Commit f7eb6d5

Browse files
authored
Add Color.Resolved init?(platformColor:) (#113)
* Fix OPENSWIFTUI_TARGET_OS_IOS issue * Add _CFMZEnabled API * Add Color.Resolved init with platformColor * Fix iOS CI issue * Fix iOS link issue on OpenSwiftUICoreTests
1 parent 33c7419 commit f7eb6d5

File tree

16 files changed

+248
-44
lines changed

16 files changed

+248
-44
lines changed

Package.swift

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ if warningsAsErrorsCondition {
8484
let openSwiftUICoreTarget = Target.target(
8585
name: "OpenSwiftUICore",
8686
dependencies: [
87+
"COpenSwiftUI",
8788
.product(name: "OpenGraphShims", package: "OpenGraph"),
8889
],
8990
swiftSettings: sharedSwiftSettings
@@ -105,14 +106,6 @@ let openSwiftUIExtensionTarget = Target.target(
105106
],
106107
swiftSettings: sharedSwiftSettings
107108
)
108-
let openSwiftUICoreTestTarget = Target.testTarget(
109-
name: "OpenSwiftUICoreTests",
110-
dependencies: [
111-
"OpenSwiftUICore",
112-
],
113-
exclude: ["README.md"],
114-
swiftSettings: sharedSwiftSettings
115-
)
116109
let openSwiftUITestTarget = Target.testTarget(
117110
name: "OpenSwiftUITests",
118111
dependencies: [
@@ -145,6 +138,8 @@ let package = Package(
145138
platforms: platforms,
146139
products: [
147140
.library(name: "OpenSwiftUI", targets: ["OpenSwiftUI", "OpenSwiftUIExtension"]),
141+
// FIXME: This will block xcodebuild build(iOS CI) somehow
142+
// .library(name: "COpenSwiftUI", targets: ["COpenSwiftUI"]),
148143
],
149144
targets: [
150145
// TODO: Add SwiftGTK as an backend alternative for UIKit/AppKit on Linux and macOS
@@ -211,7 +206,6 @@ extension Target {
211206
if attributeGraphCondition {
212207
openSwiftUICoreTarget.addAGSettings()
213208
openSwiftUITarget.addAGSettings()
214-
openSwiftUICoreTestTarget.addAGSettings()
215209
openSwiftUITestTarget.addAGSettings()
216210
openSwiftUITempTestTarget.addAGSettings()
217211
openSwiftUICompatibilityTestTarget.addAGSettings()
@@ -250,12 +244,10 @@ if swiftTestingCondition {
250244
// Fix it to be 0.3.0 before we bump to Swift 5.10
251245
.package(url: "https://github.com/apple/swift-testing", exact: "0.6.0")
252246
)
253-
openSwiftUICoreTestTarget.addSwiftTestingSettings()
254247
openSwiftUITestTarget.addSwiftTestingSettings()
255248
openSwiftUITempTestTarget.addSwiftTestingSettings()
256249
openSwiftUICompatibilityTestTarget.addSwiftTestingSettings()
257250

258-
package.targets.append(openSwiftUICoreTestTarget)
259251
package.targets.append(openSwiftUITestTarget)
260252
package.targets.append(openSwiftUITempTestTarget)
261253
package.targets.append(openSwiftUICompatibilityTestTarget)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//
2+
// CoreColor.h
3+
// COpenSwiftUI
4+
// Audited for RELEASE_2024
5+
// Status: Complete
6+
7+
#import "CoreColor.h"
8+
9+
#if OPENSWIFTUI_TARGET_OS_DARWIN
10+
11+
Class CoreColorClass(BOOL isAppKitBased);
12+
13+
#if OPENSWIFTUI_TARGET_OS_OSX
14+
id NSColorSpaceForCGColorSpace(CGColorSpaceRef cgColorSpace);
15+
Class NSColorSpaceClass(void);
16+
#endif
17+
18+
BOOL CoreColorPlatformColorGetComponents(BOOL isAppKitBased, id color, CGFloat *red, CGFloat *green, CGFloat *blue, CGFloat *alpha) {
19+
if (!color) {
20+
return NO;
21+
}
22+
Class colorClass = CoreColorClass(isAppKitBased);
23+
if (colorClass) {
24+
#if OPENSWIFTUI_TARGET_OS_OSX
25+
if (isAppKitBased) {
26+
id colorSpace =
27+
NSColorSpaceForCGColorSpace(CGColorSpaceCreateWithName(kCGColorSpaceExtendedSRGB));
28+
NSColor *nameSpaceColor = [color colorUsingColorSpace:colorSpace];
29+
if (nameSpaceColor) {
30+
[nameSpaceColor getRed:red green:green blue: blue alpha: alpha];
31+
return YES;
32+
} else {
33+
return NO;
34+
}
35+
}
36+
#endif
37+
return ((BOOL (*)(id, SEL))[color methodForSelector:@selector(getRed:green:blue:alpha:)])(color, @selector(getRed:green:blue:alpha:));
38+
} else {
39+
return NO;
40+
}
41+
}
42+
43+
Class CoreColorGetKitColorClass(BOOL isAppKitBased) {
44+
CoreColorClass(isAppKitBased);
45+
}
46+
47+
Class CoreColorClass(BOOL isAppKitBased) {
48+
static BOOL isValid = false;
49+
static Class colorClass;
50+
static dispatch_once_t once;
51+
dispatch_once(&once, ^{
52+
if (isAppKitBased) {
53+
Class class = NSClassFromString(@"NSColor");
54+
colorClass = class;
55+
isValid = class != nil;
56+
} else {
57+
Class class = NSClassFromString(@"UIColor");
58+
colorClass = class;
59+
isValid = class != nil;
60+
}
61+
});
62+
if (isValid) {
63+
return colorClass;
64+
} else {
65+
[NSException raise:@"Invalid core color" format:@""];
66+
}
67+
}
68+
69+
#if OPENSWIFTUI_TARGET_OS_OSX
70+
id NSColorSpaceForCGColorSpace(CGColorSpaceRef cgColorSpace) {
71+
Class colorSpaceClass = NSColorSpaceClass();
72+
if (colorSpaceClass) {
73+
return [[colorSpaceClass alloc] initWithCGColorSpace:cgColorSpace];
74+
} else {
75+
return nil;
76+
}
77+
}
78+
79+
OPENSWIFTUI_INLINE
80+
Class NSColorSpaceClass(void) {
81+
static Class colorSpaceClass;
82+
static dispatch_once_t once;
83+
dispatch_once(&once, ^{
84+
colorSpaceClass = NSClassFromString(@"NSColorSpace");
85+
});
86+
return colorSpaceClass;
87+
}
88+
#endif
89+
90+
#endif
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// CoreFoundation_Private.c
3+
// COpenSwiftUI
4+
5+
#include "CoreFoundation_Private.h"
6+
#include "dyld_Private.h"
7+
8+
#if !OPENSWIFTUI_TARGET_OS_DARWIN
9+
10+
#define PLATFORM_IOS 2
11+
#define PLATFORM_MACCATALYST 6
12+
13+
bool _CFMZEnabled(void) {
14+
return dyld_get_active_platform() == PLATFORM_MACCATALYST || dyld_get_active_platform() == PLATFORM_IOS;
15+
}
16+
#endif

Sources/COpenSwiftUI/dyld_Private.c renamed to Sources/COpenSwiftUI/Shims/dyld_Private.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@
88
bool dyld_program_sdk_at_least(dyld_build_version_t version) {
99
return true;
1010
}
11+
12+
dyld_platform_t dyld_get_active_platform(void) {
13+
return 0;
14+
}
1115
#endif
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// CoreColor.h
3+
// COpenSwiftUI
4+
// Audited for RELEASE_2024
5+
// Status: Complete
6+
7+
#ifndef CoreColor_h
8+
#define CoreColor_h
9+
10+
#include "OpenSwiftUIBase.h"
11+
12+
#if OPENSWIFTUI_TARGET_OS_DARWIN
13+
14+
#if OPENSWIFTUI_TARGET_OS_IOS
15+
#include <UIKit/UIKit.h>
16+
#else
17+
#include <AppKit/AppKit.h>
18+
#endif
19+
20+
OPENSWIFTUI_ASSUME_NONNULL_BEGIN
21+
22+
OPENSWIFTUI_EXPORT
23+
BOOL CoreColorPlatformColorGetComponents(BOOL isAppKitBased, id color, CGFloat *red, CGFloat *green, CGFloat *blue, CGFloat *alpha);
24+
25+
OPENSWIFTUI_EXPORT
26+
Class CoreColorGetKitColorClass(BOOL isAppKitBased);
27+
28+
OPENSWIFTUI_ASSUME_NONNULL_END
29+
30+
#endif
31+
32+
#endif /* CoreColor_h */
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// CoreFoundation_Private.h
3+
// COpenSwiftUI
4+
5+
#ifndef CoreFoundation_Private_h
6+
#define CoreFoundation_Private_h
7+
8+
#include "OpenSwiftUIBase.h"
9+
10+
OPENSWIFTUI_EXPORT
11+
bool _CFMZEnabled(void);
12+
13+
#endif /* CoreFoundation_Private_h */

Sources/COpenSwiftUI/include/OpenSwiftUITargetConditionals.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,24 @@
136136

137137
#define OPENSWIFTUI_TARGET_OS_WIN32 OPENSWIFTUI_TARGET_OS_WINDOWS
138138
#define OPENSWIFTUI_TARGET_OS_MAC OPENSWIFTUI_TARGET_OS_DARWIN
139-
#define OPENSWIFTUI_TARGET_OS_OSX OPENSWIFTUI_TARGET_OS_DARWIN
140139

140+
/* OpenSwiftUI Addition Begin */
141+
#if OPENSWIFTUI_TARGET_OS_DARWIN
142+
#include <TargetConditionals.h>
143+
#define OPENSWIFTUI_TARGET_OS_OSX TARGET_OS_OSX
144+
#define OPENSWIFTUI_TARGET_OS_IPHONE TARGET_OS_IPHONE
145+
#define OPENSWIFTUI_TARGET_OS_IOS TARGET_OS_IOS
146+
#define OPENSWIFTUI_TARGET_OS_WATCH TARGET_OS_WATCH
147+
#define OPENSWIFTUI_TARGET_OS_TV TARGET_OS_TV
148+
#else
141149
// iOS, watchOS, and tvOS are not supported
142150
#define OPENSWIFTUI_TARGET_OS_IPHONE 0
143151
#define OPENSWIFTUI_TARGET_OS_IOS 0
144152
#define OPENSWIFTUI_TARGET_OS_WATCH 0
145153
#define OPENSWIFTUI_TARGET_OS_TV 0
154+
#endif
155+
156+
/* OpenSwiftUI Addition End */
146157

147158
#if __x86_64__
148159
#define OPENSWIFTUI_TARGET_CPU_PPC 0

Sources/COpenSwiftUI/include/__CGPathParseString.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// __CGPathParseString.m
2+
// __CGPathParseString.h
33
// COpenSwiftUI
44

55
#ifndef __CGPathParseString_h

Sources/COpenSwiftUI/include/dyld_Private.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ typedef struct {
1818
} dyld_build_version_t;
1919

2020
// Always return true on non-Darwin platform
21-
extern bool dyld_program_sdk_at_least(dyld_build_version_t version);
21+
OPENSWIFTUI_EXPORT
22+
bool dyld_program_sdk_at_least(dyld_build_version_t version);
23+
24+
OPENSWIFTUI_EXPORT
25+
dyld_platform_t dyld_get_active_platform(void);
2226

2327
#endif /* dyld_Private_h */

Sources/OpenSwiftUI/Core/Util/Utils.swift

Lines changed: 0 additions & 7 deletions
This file was deleted.

Sources/OpenSwiftUI/View/Graphic/Color/ColorResolved.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
// Status: WIP
77

88
import Foundation
9+
import OpenSwiftUICore
10+
internal import COpenSwiftUI
911

1012
// MARK: - Color.Resolved
1113

@@ -289,3 +291,23 @@ func sRGBToLinear(_ sRGB: Float) -> Float {
289291
}
290292
return sRGB > 0 ? result : -result
291293
}
294+
295+
#if canImport(Darwin)
296+
297+
// MARK: - Color.Resolved + platformColor
298+
299+
extension Color.Resolved {
300+
package init?(platformColor: AnyObject) {
301+
var red: CGFloat = 0
302+
var green: CGFloat = 0
303+
var blue: CGFloat = 0
304+
var alpha: CGFloat = 0
305+
let result = CoreColorPlatformColorGetComponents(isAppKitBased(), platformColor, &red, &green, &blue, &alpha)
306+
if result {
307+
self.init(red: Float(red), green: Float(green), blue: Float(blue), opacity: Float(alpha))
308+
} else {
309+
return nil
310+
}
311+
}
312+
}
313+
#endif
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//
2+
// Utils.swift
3+
// OpenSwiftUICore
4+
//
5+
// Audited for RELEASE_2024
6+
// Status: WIP
7+
8+
internal import COpenSwiftUI
9+
10+
@inlinable
11+
@inline(__always)
12+
func asOptional<Value>(_ value: Value) -> Value? {
13+
func unwrap<T>() -> T { value as! T }
14+
let optionalValue: Value? = unwrap()
15+
return optionalValue
16+
}
17+
18+
@inline(__always)
19+
package func isCatalyst() -> Bool {
20+
#if os(iOS) || os(tvOS) || os(watchOS)
21+
false
22+
#elseif os(macOS)
23+
// NOTE: use runtime check instead of #if targetEnvironment(macCatalyst) here
24+
_CFMZEnabled()
25+
#else
26+
false
27+
#endif
28+
}
29+
30+
@inline(__always)
31+
package func isUIKitBased() -> Bool {
32+
#if os(iOS) || os(tvOS) || os(watchOS)
33+
true
34+
#elseif os(macOS)
35+
_CFMZEnabled()
36+
#else
37+
false
38+
#endif
39+
}
40+
41+
@inline(__always)
42+
package func isAppKitBased() -> Bool {
43+
#if os(iOS) || os(tvOS) || os(watchOS)
44+
false
45+
#elseif os(macOS)
46+
!_CFMZEnabled()
47+
#else
48+
false
49+
#endif
50+
}

Tests/OpenSwiftUICoreTests/README.md

Lines changed: 0 additions & 8 deletions
This file was deleted.

Tests/OpenSwiftUICoreTests/Scaffolding.swift

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)