Skip to content

[overlay] Build CoreGraphics overlay in Swift 4 mode #19598

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 2 commits into from
Sep 28, 2018
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
2 changes: 1 addition & 1 deletion stdlib/public/SDK/CoreGraphics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ add_swift_library(swiftCoreGraphics ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES} IS_
CGFloat.swift.gyb
Private.swift

SWIFT_COMPILE_FLAGS "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}" "-swift-version" "3"
SWIFT_COMPILE_FLAGS "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}"
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
SWIFT_MODULE_DEPENDS_OSX Darwin CoreFoundation Dispatch IOKit ObjectiveC # auto-updated
SWIFT_MODULE_DEPENDS_IOS Darwin CoreFoundation Dispatch ObjectiveC # auto-updated
Expand Down
34 changes: 32 additions & 2 deletions stdlib/public/SDK/CoreGraphics/CoreGraphics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,44 @@ extension CGColor {
}

public protocol _CGColorInitTrampoline {
#if os(macOS)
init(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)
#else
init?(colorSpace space: CGColorSpace, components: UnsafePointer<CGFloat>)
#endif
}

extension _CGColorInitTrampoline {
public init(_colorLiteralRed red: Float, green: Float, blue: Float,
alpha: Float) {
self.init(red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue),
alpha: CGFloat(alpha))
let red = CGFloat(red)
let green = CGFloat(green)
let blue = CGFloat(blue)
let alpha = CGFloat(alpha)
// This initializer used to call the CGColorCreateGenericRGB, which is
// known to Swift as CGColor(red:green:blue:alpha:). Unfortunately this API
// is not available on platforms other than macOS. It would be possible to
// replicate the exact functionality of that API using
// kGColorSpaceGenericRGB, but it is marked as unavailable for Swift. The
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should change the behavior on macOS, though; that should continue using the generic init(red:green:blue:alpha:).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(unless the CoreGraphics folks want it to be consistent across platforms)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, knowing nothing about color spaces etc., I asked them for a review. Calling the old one for macOS is a good idea, and I can even do that with if #available, thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, #available won't do that, since it treats platforms you don't mention as "true". You'll still want #if.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😞

// next best option is to use an sRGB color space, which is available but
// was introduced a little later than the (now legacy) generic RGB color
// space.
// Should be OK, since this code only affects the playgrounds, where
// you can't really pick the OS version other than "what's currently
// shipping".
#if os(macOS)
self.init(red: red, green: green, blue: blue, alpha: alpha)
#else
if #available(iOS 9.0, tvOS 9.0, watchOS 2.0, *) {
guard let space = CGColorSpace(name: CGColorSpace.sRGB) else {
fatalError("Unable to create an sRGB color space")
}
self.init(colorSpace: space, components: [red, green, blue, alpha])!
}
else {
fatalError("Cannot create a CGColor on this version of OS")
}
#endif
}
}

Expand Down
18 changes: 18 additions & 0 deletions test/stdlib/CoreGraphics.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// RUN: %target-run-simple-swift
// REQUIRES: executable_test
// REQUIRES: objc_interop


import CoreGraphics
import StdlibUnittest

let CoreGraphicsTests = TestSuite("CoreGraphics")

CoreGraphicsTests.test("CGColor/_ExpressibleByColorLiteral") {
let color: CGColor = #colorLiteral(
red: 0.5, green: 0.5, blue: 0.5, alpha: 1.0)
// we primarilly care that the code above does not crash
expectEqual([0.5, 0.5, 0.5, 1.0], color.components ?? [])
}

runAllTests()