Skip to content

Commit e0ff7fe

Browse files
authored
Merge pull request swiftlang#12302 from moiseev/appkit-swift4
[overlay] Build AppKit overlay with Swift version 4
2 parents 316accc + b49adc2 commit e0ff7fe

File tree

9 files changed

+277
-38
lines changed

9 files changed

+277
-38
lines changed

stdlib/public/SDK/AppKit/AppKit.swift

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public func NSApplicationMain(
7373
extension NSApplication {
7474
@available(swift 4)
7575
public static func loadApplication() {
76-
NSApplicationLoad()
76+
__NSApplicationLoad()
7777
}
7878
}
7979

@@ -90,7 +90,7 @@ public typealias _ColorLiteralType = NSColor
9090

9191
extension NSImage : _ExpressibleByImageLiteral {
9292
private convenience init!(failableImageLiteral name: String) {
93-
self.init(named: name)
93+
self.init(named: .init(name))
9494
}
9595

9696
@nonobjc
@@ -100,3 +100,61 @@ extension NSImage : _ExpressibleByImageLiteral {
100100
}
101101

102102
public typealias _ImageLiteralType = NSImage
103+
104+
// Numeric backed types
105+
106+
@available(swift 4)
107+
public protocol _AppKitKitNumericRawRepresentable : RawRepresentable, Comparable
108+
where RawValue: Comparable & Numeric { }
109+
110+
extension _AppKitKitNumericRawRepresentable {
111+
public static func <(lhs: Self, rhs: Self) -> Bool {
112+
return lhs.rawValue < rhs.rawValue
113+
}
114+
115+
public static func +(lhs: Self, rhs: RawValue) -> Self {
116+
return Self(rawValue: lhs.rawValue + rhs)!
117+
}
118+
119+
public static func +(lhs: RawValue, rhs: Self) -> Self {
120+
return Self(rawValue: lhs + rhs.rawValue)!
121+
}
122+
123+
public static func -(lhs: Self, rhs: RawValue) -> Self {
124+
return Self(rawValue: lhs.rawValue - rhs)!
125+
}
126+
127+
public static func -(lhs: Self, rhs: Self) -> RawValue {
128+
return lhs.rawValue - rhs.rawValue
129+
}
130+
131+
public static func +=(lhs: inout Self, rhs: RawValue) {
132+
lhs = Self(rawValue: lhs.rawValue + rhs)!
133+
}
134+
135+
public static func -=(lhs: inout Self, rhs: RawValue) {
136+
lhs = Self(rawValue: lhs.rawValue - rhs)!
137+
}
138+
}
139+
140+
@available(swift 4)
141+
extension NSAppKitVersion : _AppKitKitNumericRawRepresentable { }
142+
143+
@available(swift 4)
144+
extension NSLayoutConstraint.Priority : _AppKitKitNumericRawRepresentable { }
145+
146+
@available(swift 4)
147+
extension NSStackView.VisibilityPriority : _AppKitKitNumericRawRepresentable { }
148+
149+
@available(swift 4)
150+
extension NSToolbarItem.VisibilityPriority : _AppKitKitNumericRawRepresentable { }
151+
152+
@available(macOS 10.12.2, *)
153+
@available(swift 4)
154+
extension NSTouchBarItem.Priority : _AppKitKitNumericRawRepresentable { }
155+
156+
@available(swift 4)
157+
extension NSWindow.Level : _AppKitKitNumericRawRepresentable { }
158+
159+
@available(swift 4)
160+
extension NSFont.Weight : _AppKitKitNumericRawRepresentable { }

stdlib/public/SDK/AppKit/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ add_swift_library(swiftAppKit ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES} IS_SDK_OV
88
NSGraphics.swift
99
NSOpenGL.swift
1010

11-
SWIFT_COMPILE_FLAGS "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}" "-swift-version" "3"
11+
SWIFT_COMPILE_FLAGS ${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS} -swift-version 4
1212
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
1313
TARGET_SDKS OSX
1414
SWIFT_MODULE_DEPENDS_OSX Darwin CoreData CoreGraphics CoreImage Dispatch Foundation IOKit ObjectiveC QuartzCore XPC # auto-updated

stdlib/public/SDK/AppKit/NSGraphics.swift

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ extension NSRect {
2121
/// - precondition: There must be a set current NSGraphicsContext.
2222
@available(swift 4)
2323
public func fill(using operation: NSCompositingOperation =
24-
NSGraphicsContext.current()?.compositingOperation ?? .sourceOver) {
25-
precondition(NSGraphicsContext.current() != nil,
24+
NSGraphicsContext.current?.compositingOperation ?? .sourceOver) {
25+
precondition(NSGraphicsContext.current != nil,
2626
"There must be a set current NSGraphicsContext")
27-
NSRectFillUsingOperation(self, operation)
27+
__NSRectFillUsingOperation(self, operation)
2828
}
2929

3030
/// Draws a frame around the inside of this rect in the current
@@ -35,10 +35,10 @@ extension NSRect {
3535
@available(swift 4)
3636
public func frame(withWidth width: CGFloat = 1.0,
3737
using operation: NSCompositingOperation =
38-
NSGraphicsContext.current()?.compositingOperation ?? .sourceOver) {
39-
precondition(NSGraphicsContext.current() != nil,
38+
NSGraphicsContext.current?.compositingOperation ?? .sourceOver) {
39+
precondition(NSGraphicsContext.current != nil,
4040
"There must be a set current NSGraphicsContext")
41-
NSFrameRectWithWidthUsingOperation(self, width, operation)
41+
__NSFrameRectWithWidthUsingOperation(self, width, operation)
4242
}
4343

4444
/// Modifies the current graphics context clipping path by intersecting it
@@ -48,9 +48,9 @@ extension NSRect {
4848
/// - precondition: There must be a set current NSGraphicsContext.
4949
@available(swift 4)
5050
public func clip() {
51-
precondition(NSGraphicsContext.current() != nil,
51+
precondition(NSGraphicsContext.current != nil,
5252
"There must be a set current NSGraphicsContext")
53-
NSRectClip(self)
53+
__NSRectClip(self)
5454
}
5555
}
5656

@@ -62,15 +62,15 @@ extension Sequence where Iterator.Element == NSRect {
6262
/// - precondition: There must be a set current NSGraphicsContext.
6363
@available(swift 4)
6464
public func fill(using operation: NSCompositingOperation =
65-
NSGraphicsContext.current()?.compositingOperation ?? .sourceOver) {
66-
precondition(NSGraphicsContext.current() != nil,
65+
NSGraphicsContext.current?.compositingOperation ?? .sourceOver) {
66+
precondition(NSGraphicsContext.current != nil,
6767
"There must be a set current NSGraphicsContext")
6868
let rects = Array(self)
6969
let count = rects.count
7070
guard count > 0 else { return }
7171
rects.withUnsafeBufferPointer { rectBufferPointer in
7272
guard let rectArray = rectBufferPointer.baseAddress else { return }
73-
NSRectFillListUsingOperation(rectArray, count, operation)
73+
__NSRectFillListUsingOperation(rectArray, count, operation)
7474
}
7575
}
7676

@@ -81,14 +81,14 @@ extension Sequence where Iterator.Element == NSRect {
8181
/// - precondition: There must be a set current NSGraphicsContext.
8282
@available(swift 4)
8383
public func clip() {
84-
precondition(NSGraphicsContext.current() != nil,
84+
precondition(NSGraphicsContext.current != nil,
8585
"There must be a set current NSGraphicsContext")
8686
let rects = Array(self)
8787
let count = rects.count
8888
guard count > 0 else { return }
8989
rects.withUnsafeBufferPointer { rectBufferPointer in
9090
guard let rectArray = rectBufferPointer.baseAddress else { return }
91-
NSRectClipList(rectArray, count)
91+
__NSRectClipList(rectArray, count)
9292
}
9393
}
9494
}
@@ -101,8 +101,8 @@ extension Sequence where Iterator.Element == (CGRect, NSColor) {
101101
/// - precondition: There must be a set current NSGraphicsContext.
102102
@available(swift 4)
103103
public func fill(using operation: NSCompositingOperation =
104-
NSGraphicsContext.current()?.compositingOperation ?? .sourceOver) {
105-
precondition(NSGraphicsContext.current() != nil,
104+
NSGraphicsContext.current?.compositingOperation ?? .sourceOver) {
105+
precondition(NSGraphicsContext.current != nil,
106106
"There must be a set current NSGraphicsContext")
107107
let rects = map { $0.0 }
108108
let colors = map { $0.1 }
@@ -112,7 +112,7 @@ extension Sequence where Iterator.Element == (CGRect, NSColor) {
112112
colors.withUnsafeBufferPointer { colorBufferPointer in
113113
guard let rectArray = rectBufferPointer.baseAddress else { return }
114114
guard let colorArray = colorBufferPointer.baseAddress else { return }
115-
NSRectFillListWithColorsUsingOperation(
115+
__NSRectFillListWithColorsUsingOperation(
116116
rectArray, colorArray, count, operation)
117117
}
118118
}
@@ -128,10 +128,10 @@ extension Sequence where Iterator.Element == (CGRect, gray: CGFloat) {
128128
/// - precondition: There must be a set current NSGraphicsContext.
129129
@available(swift 4)
130130
public func fill(using operation: NSCompositingOperation =
131-
NSGraphicsContext.current()?.compositingOperation ?? .sourceOver) {
131+
NSGraphicsContext.current?.compositingOperation ?? .sourceOver) {
132132
// NSRectFillListWithGrays does not have a variant taking an operation, but
133133
// is added here for consistency with the other drawing operations.
134-
guard let graphicsContext = NSGraphicsContext.current() else {
134+
guard let graphicsContext = NSGraphicsContext.current else {
135135
fatalError("There must be a set current NSGraphicsContext")
136136
}
137137
let cgContext: CGContext
@@ -144,31 +144,31 @@ extension Sequence where Iterator.Element == (CGRect, gray: CGFloat) {
144144
cgContext.saveGState()
145145
forEach {
146146
cgContext.setFillColor(gray: $0.gray, alpha: 1.0)
147-
NSRectFillUsingOperation($0.0, operation)
147+
__NSRectFillUsingOperation($0.0, operation)
148148
}
149149
cgContext.restoreGState()
150150
}
151151
}
152152

153-
extension NSWindowDepth {
153+
extension NSWindow.Depth {
154154
@available(swift 4)
155155
public static func bestDepth(
156156
colorSpaceName: NSColorSpaceName,
157157
bitsPerSample: Int,
158158
bitsPerPixel: Int,
159159
isPlanar: Bool
160-
) -> (NSWindowDepth, isExactMatch: Bool) {
160+
) -> (NSWindow.Depth, isExactMatch: Bool) {
161161
var isExactMatch: ObjCBool = false
162-
let depth = NSBestDepth(
163-
colorSpaceName as String,
162+
let depth = __NSBestDepth(
163+
colorSpaceName,
164164
bitsPerSample, bitsPerPixel, isPlanar, &isExactMatch)
165165
return (depth, isExactMatch: isExactMatch.boolValue)
166166
}
167167
@available(swift 4)
168-
public static var availableDepths: [NSWindowDepth] {
168+
public static var availableDepths: [NSWindow.Depth] {
169169
// __NSAvailableWindowDepths is NULL terminated, the length is not known up front
170-
let depthsCArray = NSAvailableWindowDepths()
171-
var depths: [NSWindowDepth] = []
170+
let depthsCArray = __NSAvailableWindowDepths()
171+
var depths: [NSWindow.Depth] = []
172172
var length = 0
173173
var depth = depthsCArray[length]
174174
while depth.rawValue != 0 {
@@ -194,7 +194,7 @@ extension NSAnimationEffect {
194194
delegate.completionHandler = completionHandler
195195
// Note that the delegate of `__NSShowAnimationEffect` is retained for the
196196
// duration of the animation.
197-
NSShowAnimationEffect(
197+
__NSShowAnimationEffect(
198198
self,
199199
centerLocation,
200200
size,
@@ -207,6 +207,6 @@ extension NSAnimationEffect {
207207
extension NSSound {
208208
@available(swift 4)
209209
public static func beep() {
210-
NSBeep()
210+
__NSBeep()
211211
}
212212
}

stdlib/public/SDK/AppKit/NSOpenGL.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ extension NSOpenGLGlobalOption {
1818
public var globalValue: GLint {
1919
get {
2020
var value: GLint = 0
21-
NSOpenGLGetOption(self, &value)
21+
__NSOpenGLGetOption(self, &value)
2222
return value
2323
}
2424
set {
25-
NSOpenGLSetOption(self, newValue)
25+
__NSOpenGLSetOption(self, newValue)
2626
}
2727
}
2828
}
@@ -32,7 +32,7 @@ extension NSOpenGLContext {
3232
public static var openGLVersion: (major: GLint, minor: GLint) {
3333
var major: GLint = 0
3434
var minor: GLint = 0
35-
NSOpenGLGetVersion(&major, &minor)
35+
__NSOpenGLGetVersion(&major, &minor)
3636
return (major: major, minor: minor)
3737
}
3838
}

stdlib/public/SDK/UIKit/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ add_swift_library(swiftUIKit ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES} IS_SDK_OVE
66
UIKit.swift
77
UIKit_FoundationExtensions.swift.gyb
88

9-
SWIFT_COMPILE_FLAGS "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}"
9+
SWIFT_COMPILE_FLAGS ${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS} -swift-version 4
1010
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
1111
TARGET_SDKS IOS IOS_SIMULATOR TVOS TVOS_SIMULATOR WATCHOS WATCHOS_SIMULATOR
1212
SWIFT_MODULE_DEPENDS_IOS Darwin CoreFoundation CoreGraphics CoreImage Dispatch Foundation ObjectiveC QuartzCore os # auto-updated

stdlib/public/SDK/UIKit/UIKit.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,52 @@ public func == (lhs: UIOffset, rhs: UIOffset) -> Bool {
5757

5858
extension UIOffset : Equatable {}
5959

60+
//===----------------------------------------------------------------------===//
61+
// Numeric backed types
62+
//===----------------------------------------------------------------------===//
63+
64+
@available(swift 4)
65+
public protocol _UIKitNumericRawRepresentable : RawRepresentable, Comparable where RawValue: Comparable & Numeric {}
66+
67+
extension _UIKitNumericRawRepresentable {
68+
69+
public static func <(lhs: Self, rhs: Self) -> Bool {
70+
return lhs.rawValue < rhs.rawValue
71+
}
72+
73+
public static func +(lhs: Self, rhs: RawValue) -> Self {
74+
return Self(rawValue: lhs.rawValue + rhs)!
75+
}
76+
77+
public static func +(lhs: RawValue, rhs: Self) -> Self {
78+
return Self(rawValue: lhs + rhs.rawValue)!
79+
}
80+
81+
public static func -(lhs: Self, rhs: RawValue) -> Self {
82+
return Self(rawValue: lhs.rawValue - rhs)!
83+
}
84+
85+
public static func -(lhs: Self, rhs: Self) -> RawValue {
86+
return lhs.rawValue - rhs.rawValue
87+
}
88+
89+
public static func +=(lhs: inout Self, rhs: RawValue) {
90+
lhs = Self(rawValue: lhs.rawValue + rhs)!
91+
}
92+
93+
public static func -=(lhs: inout Self, rhs: RawValue) {
94+
lhs = Self(rawValue: lhs.rawValue - rhs)!
95+
}
96+
}
97+
98+
@available(swift 4)
99+
extension UIFont.Weight : _UIKitNumericRawRepresentable {}
100+
101+
#if !os(watchOS)
102+
@available(swift 4)
103+
extension UILayoutPriority : _UIKitNumericRawRepresentable {}
104+
#endif
105+
60106
// These are un-imported macros in UIKit.
61107

62108
//===----------------------------------------------------------------------===//
@@ -247,6 +293,7 @@ extension UIFontTextStyle {
247293

248294
#if !os(watchOS) // UIContentSizeCategory not available on watchOS
249295
extension UIContentSizeCategory {
296+
250297
@available(iOS 11.0, tvOS 11.0, *)
251298
public var isAccessibilityCategory: Bool {
252299
return __UIContentSizeCategoryIsAccessibilityCategory(self)

test/stdlib/AppKit_Swift3.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,36 @@ AppKitTests.test("NSEventMaskFromType") {
1717
expectEqual(eventMask, .keyDown)
1818
}
1919

20+
AppKitTests.test("NSLayoutPriority") {
21+
let highPriority: NSLayoutPriority = NSLayoutPriorityDefaultHigh
22+
23+
let adjustedPriority1 = highPriority + 1
24+
let adjustedPriority1RawValue: Float = NSLayoutPriorityDefaultHigh + 1
25+
expectEqual(adjustedPriority1, adjustedPriority1RawValue)
26+
27+
let adjustedPriority2 = highPriority - 5.0
28+
let adjustedPriority2RawValue: Float = NSLayoutPriorityDefaultHigh - 5.0
29+
expectEqual(adjustedPriority2, adjustedPriority2RawValue)
30+
31+
let adjustedPriority3 = 5.0 + highPriority
32+
let adjustedPriority3RawValue: Float = 5.0 + NSLayoutPriorityDefaultHigh
33+
expectEqual(adjustedPriority3, adjustedPriority3RawValue)
34+
35+
// Inferred typing from result type
36+
let adjustedPriority4: NSLayoutPriority = NSLayoutPriorityDefaultHigh + 2.0
37+
let adjustedPriority4RawValue: Float = NSLayoutPriorityDefaultHigh + 2.0
38+
expectEqual(adjustedPriority4, adjustedPriority4RawValue)
39+
40+
// Comparable
41+
expectTrue(adjustedPriority1 > adjustedPriority2)
42+
expectTrue(adjustedPriority2 < adjustedPriority1)
43+
44+
// Compound assignment
45+
var variablePriority: NSLayoutPriority = NSLayoutPriorityDefaultHigh
46+
variablePriority += 1
47+
variablePriority -= 5.0
48+
let variablePriorityRawValue: Float = NSLayoutPriorityDefaultHigh + 1 - 5.0
49+
expectEqual(variablePriority, variablePriorityRawValue)
50+
}
51+
2052
runAllTests()

0 commit comments

Comments
 (0)