Skip to content

Add CorePlatformImage support #150

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 3 commits into from
Nov 3, 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
4 changes: 2 additions & 2 deletions Sources/OpenSwiftUICore/Graphic/Color/ColorResolved.swift
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@
var green: CGFloat = 0
var blue: CGFloat = 0
var alpha: CGFloat = 0
let result = OpenSwiftUICoreColorPlatformColorGetComponents(isAppKitBased(), platformColor, &red, &green, &blue, &alpha)
let result = CoreColorPlatformColorGetComponents(system: isAppKitBased() ? .appKit : .uiKit, color: platformColor, red: &red, green: &green, blue: &blue, alpha: &alpha)

Check warning on line 325 in Sources/OpenSwiftUICore/Graphic/Color/ColorResolved.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Graphic/Color/ColorResolved.swift#L325

Added line #L325 was not covered by tests
if result {
self.init(red: Float(red), green: Float(green), blue: Float(blue), opacity: Float(alpha))
} else {
Expand All @@ -332,7 +332,7 @@

// ID: 4330A474F53D66045762501ED6F8A749
private static let cache: ObjectCache<Color.Resolved, NSObject> = ObjectCache { resolved in
OpenSwiftUICoreColor.platformColor(resolvedColor: resolved)!
CoreColor.platformColor(resolvedColor: resolved)!
}

package var kitColor: NSObject {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
import Foundation
internal import OpenSwiftUI_SPI

extension OpenSwiftUICoreColor {
extension CoreColor {
package static func platformColor(resolvedColor: Color.Resolved) -> NSObject? {
platformColor(red: CGFloat(resolvedColor.red), green: CGFloat(resolvedColor.green), blue: CGFloat(resolvedColor.blue), alpha: CGFloat(resolvedColor.opacity))
}

package static func platformColor(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) -> NSObject? {
OpenSwiftUICorePlatformColorForRGBA(isAppKitBased(), red, green, blue, alpha)
CorePlatformColorForRGBA(system: isAppKitBased() ? .appKit : .uiKit, red: red, green: green, blue: blue, alpha: alpha)
}
}

Expand Down
57 changes: 57 additions & 0 deletions Sources/OpenSwiftUICore/Graphic/Image/CorePlatformImage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// CorePlatformImage.swift
// OpenSwiftUICore
//
// Audited for RELEASE_2024
// Status: WIP

#if canImport(Darwin)
import Foundation
package import OpenSwiftUI_SPI

@objc(OpenSwiftUICorePlatformImage)
package final class CorePlatformImage: NSObject {
package var system: CoreSystem
package var kitImage: NSObject
package var isTemplate: Bool

package init(system: CoreSystem, kitImage: NSObject) {
self.system = system
self.kitImage = kitImage
self.isTemplate = _CorePlatformImageIsTemplate(system: system, kitImage: kitImage)
super.init()

Check warning on line 22 in Sources/OpenSwiftUICore/Graphic/Image/CorePlatformImage.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Graphic/Image/CorePlatformImage.swift#L18-L22

Added lines #L18 - L22 were not covered by tests
}

convenience package init(system: CoreSystem, cgImage: CGImage, scale: CGFloat, orientation: UInt8){
self.init(
system: system,
kitImage: _CorePlatformImageMakeKitImage(system: system, cgImage: cgImage, scale: scale, orientation: orientation)
)

Check warning on line 29 in Sources/OpenSwiftUICore/Graphic/Image/CorePlatformImage.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Graphic/Image/CorePlatformImage.swift#L25-L29

Added lines #L25 - L29 were not covered by tests
}

package var cgImage: CGImage? {
_CorePlatformImageGetCGImage(system: system, kitImage: kitImage)

Check warning on line 33 in Sources/OpenSwiftUICore/Graphic/Image/CorePlatformImage.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Graphic/Image/CorePlatformImage.swift#L32-L33

Added lines #L32 - L33 were not covered by tests
}

package var size: CGSize {
_CorePlatformImageGetSize(system: system, kitImage: kitImage)

Check warning on line 37 in Sources/OpenSwiftUICore/Graphic/Image/CorePlatformImage.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Graphic/Image/CorePlatformImage.swift#L36-L37

Added lines #L36 - L37 were not covered by tests
}

package var scale: CGFloat {
_CorePlatformImageGetScale(system: system, kitImage: kitImage)

Check warning on line 41 in Sources/OpenSwiftUICore/Graphic/Image/CorePlatformImage.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Graphic/Image/CorePlatformImage.swift#L40-L41

Added lines #L40 - L41 were not covered by tests
}

package var imageOrientation: UInt8 {
_CorePlatformImageGetImageOrientation(system: system, kitImage: kitImage)

Check warning on line 45 in Sources/OpenSwiftUICore/Graphic/Image/CorePlatformImage.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Graphic/Image/CorePlatformImage.swift#L44-L45

Added lines #L44 - L45 were not covered by tests
}

package var baselineOffsetFromBottom: CGFloat {
_CorePlatformImageGetBaselineOffsetFromBottom(system: system, kitImage: kitImage)

Check warning on line 49 in Sources/OpenSwiftUICore/Graphic/Image/CorePlatformImage.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Graphic/Image/CorePlatformImage.swift#L48-L49

Added lines #L48 - L49 were not covered by tests
}

package var alignmentRect: CGRect {
get { _CorePlatformImageGetAlignmentRect(system: system, kitImage: kitImage) }
set { _CorePlatformImageSetAlignmentRect(system: system, kitImage: kitImage, newValue) }

Check warning on line 54 in Sources/OpenSwiftUICore/Graphic/Image/CorePlatformImage.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Graphic/Image/CorePlatformImage.swift#L53-L54

Added lines #L53 - L54 were not covered by tests
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
}
}

open class var system: PlatformViewDefinition.System { .init(base: ._2) }
open class var system: PlatformViewDefinition.System { .init(base: .swiftUIView) }

Check warning on line 50 in Sources/OpenSwiftUICore/Render/PlatformViewDefinition.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Render/PlatformViewDefinition.swift#L50

Added line #L50 was not covered by tests
open class func makeView(kind: PlatformViewDefinition.ViewKind) -> AnyObject { fatalError() }
#if canImport(Darwin)
open class func makeLayerView(type: CALayer.Type, kind: PlatformViewDefinition.ViewKind) -> AnyObject { fatalError() }
Expand Down
16 changes: 16 additions & 0 deletions Sources/OpenSwiftUI_SPI/OpenSwiftUIBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,20 @@

#define OPENSWIFTUI_ENUM CF_ENUM

#ifndef OPENSWIFTUI_IMPLICIT_BRIDGING_ENABLED
#if __has_feature(arc_cf_code_audited)
#define OPENSWIFTUI_IMPLICIT_BRIDGING_ENABLED _Pragma("clang arc_cf_code_audited begin")
#else
#define OPENSWIFTUI_IMPLICIT_BRIDGING_ENABLED
#endif
#endif

#ifndef OPENSWIFTUI_IMPLICIT_BRIDGING_DISABLED
#if __has_feature(arc_cf_code_audited)
#define OPENSWIFTUI_IMPLICIT_BRIDGING_DISABLED _Pragma("clang arc_cf_code_audited end")
#else
#define OPENSWIFTUI_IMPLICIT_BRIDGING_DISABLED
#endif
#endif

#endif /* OpenSwiftUIBase_h */
38 changes: 20 additions & 18 deletions Sources/OpenSwiftUI_SPI/Overlay/CoreGraphics/OpenSwiftUICoreColor.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,37 @@
#else
#include <AppKit/AppKit.h>
#endif
#include "OpenSwiftUICoreSystem.h"

OPENSWIFTUI_ASSUME_NONNULL_BEGIN

OPENSWIFTUI_EXPORT
BOOL OpenSwiftUICoreColorPlatformColorGetComponents(BOOL system, id color, CGFloat *red, CGFloat *green, CGFloat *blue, CGFloat *alpha);
BOOL OpenSwiftUICoreColorPlatformColorGetComponents(OpenSwiftUICoreSystem system, id color, CGFloat *red, CGFloat *green, CGFloat *blue, CGFloat *alpha) OPENSWIFTUI_SWIFT_NAME(CoreColorPlatformColorGetComponents(system:color:red:green:blue:alpha:));

OPENSWIFTUI_EXPORT
NSObject * _Nullable OpenSwiftUICorePlatformColorForRGBA(BOOL system, CGFloat red, CGFloat green, CGFloat blue, CGFloat alpha);
NSObject * _Nullable OpenSwiftUICorePlatformColorForRGBA(OpenSwiftUICoreSystem system, CGFloat red, CGFloat green, CGFloat blue, CGFloat alpha) OPENSWIFTUI_SWIFT_NAME(CorePlatformColorForRGBA(system:red:green:blue:alpha:));

OPENSWIFTUI_EXPORT
Class _Nullable OpenSwiftUICoreColorGetKitColorClass(BOOL system);
Class _Nullable OpenSwiftUICoreColorGetKitColorClass(OpenSwiftUICoreSystem system) OPENSWIFTUI_SWIFT_NAME(CoreColorGetKitColorClass(system:));

OPENSWIFTUI_SWIFT_NAME(CoreColor)
@interface OpenSwiftUICoreColor : NSObject

+ (nullable NSObject *)colorWithSystem:(BOOL)system cgColor: (CGColorRef)cgColor;
+ (nullable NSObject *)blackColorWithSystem:(BOOL)system;
+ (nullable NSObject *)systemRedColorWithSystem:(BOOL)system;
+ (nullable NSObject *)systemOrangeColorWithSystem:(BOOL)system;
+ (nullable NSObject *)systemYellowColorWithSystem:(BOOL)system;
+ (nullable NSObject *)systemGreenColorWithSystem:(BOOL)system;
+ (nullable NSObject *)systemTealColorWithSystem:(BOOL)system;
+ (nullable NSObject *)systemMintColorWithSystem:(BOOL)system;
+ (nullable NSObject *)systemCyanColorWithSystem:(BOOL)system;
+ (nullable NSObject *)systemBlueColorWithSystem:(BOOL)system;
+ (nullable NSObject *)systemIndigoColorWithSystem:(BOOL)system;
+ (nullable NSObject *)systemPurpleColorWithSystem:(BOOL)system;
+ (nullable NSObject *)systemPinkColorWithSystem:(BOOL)system;
+ (nullable NSObject *)systemBrownColorWithSystem:(BOOL)system;
+ (nullable NSObject *)systemGrayColorWithSystem:(BOOL)system;
+ (nullable NSObject *)colorWithSystem:(OpenSwiftUICoreSystem)system cgColor: (CGColorRef)cgColor;
+ (nullable NSObject *)blackColorWithSystem:(OpenSwiftUICoreSystem)system;
+ (nullable NSObject *)systemRedColorWithSystem:(OpenSwiftUICoreSystem)system;
+ (nullable NSObject *)systemOrangeColorWithSystem:(OpenSwiftUICoreSystem)system;
+ (nullable NSObject *)systemYellowColorWithSystem:(OpenSwiftUICoreSystem)system;
+ (nullable NSObject *)systemGreenColorWithSystem:(OpenSwiftUICoreSystem)system;
+ (nullable NSObject *)systemTealColorWithSystem:(OpenSwiftUICoreSystem)system;
+ (nullable NSObject *)systemMintColorWithSystem:(OpenSwiftUICoreSystem)system;
+ (nullable NSObject *)systemCyanColorWithSystem:(OpenSwiftUICoreSystem)system;
+ (nullable NSObject *)systemBlueColorWithSystem:(OpenSwiftUICoreSystem)system;
+ (nullable NSObject *)systemIndigoColorWithSystem:(OpenSwiftUICoreSystem)system;
+ (nullable NSObject *)systemPurpleColorWithSystem:(OpenSwiftUICoreSystem)system;
+ (nullable NSObject *)systemPinkColorWithSystem:(OpenSwiftUICoreSystem)system;
+ (nullable NSObject *)systemBrownColorWithSystem:(OpenSwiftUICoreSystem)system;
+ (nullable NSObject *)systemGrayColorWithSystem:(OpenSwiftUICoreSystem)system;

- (instancetype)initWithCGColor:(CGColorRef)cgColor;
- (void)set;
Expand Down
46 changes: 23 additions & 23 deletions Sources/OpenSwiftUI_SPI/Overlay/CoreGraphics/OpenSwiftUICoreColor.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
#if OPENSWIFTUI_TARGET_OS_DARWIN
#include "OpenSwiftUICoreGraphicsContext.h"

Class OpenSwiftUICoreColorClass(BOOL system);
Class OpenSwiftUICoreColorClass(OpenSwiftUICoreSystem system);

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

BOOL OpenSwiftUICoreColorPlatformColorGetComponents(BOOL system, id color, CGFloat *red, CGFloat *green, CGFloat *blue, CGFloat *alpha) {
BOOL OpenSwiftUICoreColorPlatformColorGetComponents(OpenSwiftUICoreSystem system, id color, CGFloat *red, CGFloat *green, CGFloat *blue, CGFloat *alpha) {
Class colorClass = OpenSwiftUICoreColorClass(system);
if (!colorClass) {
return NO;
Expand All @@ -44,13 +44,13 @@ BOOL OpenSwiftUICoreColorPlatformColorGetComponents(BOOL system, id color, CGFlo
#endif
}

NSObject *OpenSwiftUICorePlatformColorForRGBA(BOOL system, CGFloat red, CGFloat green, CGFloat blue, CGFloat alpha) {
NSObject *OpenSwiftUICorePlatformColorForRGBA(OpenSwiftUICoreSystem system, CGFloat red, CGFloat green, CGFloat blue, CGFloat alpha) {
Class colorClass = OpenSwiftUICoreColorClass(system);
if (!colorClass) {
return nil;
}
#if OPENSWIFTUI_TARGET_OS_OSX
if (system) {
if (system == OpenSwiftUICoreSystemAppKit) {
id colorSpace = NSColorSpaceForCGColorSpace(CGColorSpaceCreateWithName(kCGColorSpaceExtendedSRGB));
return [colorClass colorWithColorSpace:colorSpace components:(CGFloat[]){red, green, blue, alpha} count:4];
} else {
Expand All @@ -61,22 +61,22 @@ BOOL OpenSwiftUICoreColorPlatformColorGetComponents(BOOL system, id color, CGFlo
#endif
}

Class _Nullable OpenSwiftUICoreColorGetKitColorClass(BOOL system) {
Class _Nullable OpenSwiftUICoreColorGetKitColorClass(OpenSwiftUICoreSystem system) {
return OpenSwiftUICoreColorClass(system);
}

Class _Nullable OpenSwiftUICoreColorClass(BOOL system) {
Class _Nullable OpenSwiftUICoreColorClass(OpenSwiftUICoreSystem system) {
static BOOL isValid = true;
static Class colorClass;
static dispatch_once_t once;
dispatch_once(&once, ^{
#if OPENSWIFTUI_TARGET_OS_OSX
if (!system) {
if (system == OpenSwiftUICoreSystemUIKit) {
Class class = NSClassFromString(@"UIColor");
colorClass = class;
isValid = class != nil;
}
if (system) {
if (system == OpenSwiftUICoreSystemAppKit) {
Class class = NSClassFromString(@"NSColor");
colorClass = class;
isValid = class != nil;
Expand Down Expand Up @@ -122,7 +122,7 @@ @interface OpenSwiftUICoreColor () {

@implementation OpenSwiftUICoreColor

+ (NSObject *)colorWithSystem:(BOOL)system cgColor: (CGColorRef)cgColor {
+ (NSObject *)colorWithSystem:(OpenSwiftUICoreSystem)system cgColor: (CGColorRef)cgColor {
Class colorClass = OpenSwiftUICoreColorClass(system);
if (colorClass) {
return [colorClass colorWithCGColor: cgColor];
Expand All @@ -131,72 +131,72 @@ + (NSObject *)colorWithSystem:(BOOL)system cgColor: (CGColorRef)cgColor {
}
}

+ (NSObject *)blackColorWithSystem:(BOOL)system {
+ (NSObject *)blackColorWithSystem:(OpenSwiftUICoreSystem)system {
Class colorClass = OpenSwiftUICoreColorClass(system);
return [colorClass blackColor];
}

+ (NSObject *)systemRedColorWithSystem:(BOOL)system {
+ (NSObject *)systemRedColorWithSystem:(OpenSwiftUICoreSystem)system {
Class colorClass = OpenSwiftUICoreColorClass(system);
return [colorClass systemRedColor];
}

+ (NSObject *)systemOrangeColorWithSystem:(BOOL)system {
+ (NSObject *)systemOrangeColorWithSystem:(OpenSwiftUICoreSystem)system {
Class colorClass = OpenSwiftUICoreColorClass(system);
return [colorClass systemOrangeColor];
}

+ (NSObject *)systemYellowColorWithSystem:(BOOL)system {
+ (NSObject *)systemYellowColorWithSystem:(OpenSwiftUICoreSystem)system {
Class colorClass = OpenSwiftUICoreColorClass(system);
return [colorClass systemYellowColor];
}

+ (NSObject *)systemGreenColorWithSystem:(BOOL)system {
+ (NSObject *)systemGreenColorWithSystem:(OpenSwiftUICoreSystem)system {
Class colorClass = OpenSwiftUICoreColorClass(system);
return [colorClass systemGreenColor];
}

+ (NSObject *)systemTealColorWithSystem:(BOOL)system {
+ (NSObject *)systemTealColorWithSystem:(OpenSwiftUICoreSystem)system {
Class colorClass = OpenSwiftUICoreColorClass(system);
return [colorClass systemTealColor];
}

+ (NSObject *)systemMintColorWithSystem:(BOOL)system {
+ (NSObject *)systemMintColorWithSystem:(OpenSwiftUICoreSystem)system {
Class colorClass = OpenSwiftUICoreColorClass(system);
return [colorClass systemMintColor];
}

+ (NSObject *)systemCyanColorWithSystem:(BOOL)system {
+ (NSObject *)systemCyanColorWithSystem:(OpenSwiftUICoreSystem)system {
Class colorClass = OpenSwiftUICoreColorClass(system);
return [colorClass systemCyanColor];
}

+ (NSObject *)systemBlueColorWithSystem:(BOOL)system {
+ (NSObject *)systemBlueColorWithSystem:(OpenSwiftUICoreSystem)system {
Class colorClass = OpenSwiftUICoreColorClass(system);
return [colorClass systemBlueColor];
}

+ (NSObject *)systemIndigoColorWithSystem:(BOOL)system {
+ (NSObject *)systemIndigoColorWithSystem:(OpenSwiftUICoreSystem)system {
Class colorClass = OpenSwiftUICoreColorClass(system);
return [colorClass systemIndigoColor];
}

+ (NSObject *)systemPurpleColorWithSystem:(BOOL)system {
+ (NSObject *)systemPurpleColorWithSystem:(OpenSwiftUICoreSystem)system {
Class colorClass = OpenSwiftUICoreColorClass(system);
return [colorClass systemPurpleColor];
}

+ (NSObject *)systemPinkColorWithSystem:(BOOL)system {
+ (NSObject *)systemPinkColorWithSystem:(OpenSwiftUICoreSystem)system {
Class colorClass = OpenSwiftUICoreColorClass(system);
return [colorClass systemPinkColor];
}

+ (NSObject *)systemBrownColorWithSystem:(BOOL)system {
+ (NSObject *)systemBrownColorWithSystem:(OpenSwiftUICoreSystem)system {
Class colorClass = OpenSwiftUICoreColorClass(system);
return [colorClass systemBrownColor];
}

+ (NSObject *)systemGrayColorWithSystem:(BOOL)system {
+ (NSObject *)systemGrayColorWithSystem:(OpenSwiftUICoreSystem)system {
Class colorClass = OpenSwiftUICoreColorClass(system);
return [colorClass systemGrayColor];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

OPENSWIFTUI_ASSUME_NONNULL_BEGIN

OPENSWIFTUI_SWIFT_NAME(CoreGraphicsContext)
@interface OpenSwiftUICoreGraphicsContext : NSObject

@property (class, nonatomic, readonly) OpenSwiftUICoreGraphicsContext *current;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// OpenSwiftUI_SPI
//
// Audited for RELEASE_2024
// Status: WIP
// Status: Complete

#include "OpenSwiftUICoreGraphicsContext.h"

Expand Down
Loading
Loading