Skip to content

Commit 2247eff

Browse files
authored
Merge pull request #62 from RougeWare/feature/56-Compiler-error-SwiftUI-previews_v2.10.0
Treating SwiftUI as a native framework
2 parents 094f0bb + 213c845 commit 2247eff

File tree

5 files changed

+100
-80
lines changed

5 files changed

+100
-80
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
# Swift Rectangle Tools #
22

33
A set of Swift utilities for dealing with rectangles, including a way to generically build your own!
4+
5+
6+
7+
## SwiftUI ##
8+
9+
If you're using this with SwiftUI, you can also `import RectangleTools_SwiftUI` to gain SwiftUI-specific rectangle tools!

Sources/RectangleTools/Default Conformances/EdgeInsets + FourSided + SwiftUI.swift

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

Sources/RectangleTools/Default Conformances/EdgeInsets + FourSided.swift

Lines changed: 86 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
// Copyright © 2020 Ben Leggiero. All rights reserved.
77
//
88

9-
#if canImport(WatchKit)
9+
#if canImport(SwiftUI)
10+
import SwiftUI
11+
12+
public typealias NativeEdgeInsets = SwiftUI.EdgeInsets
13+
@available(watchOS 2.1, *)
14+
public typealias UserInterfaceLayoutDirection = SwiftUI.LayoutDirection
15+
#elseif canImport(WatchKit)
1016
import WatchKit
1117

1218
public typealias NativeEdgeInsets = UIEdgeInsets
@@ -28,6 +34,56 @@
2834

2935
@available(watchOS 2.1, *)
3036
extension NativeEdgeInsets: FourSidedAbsolute {
37+
38+
#if canImport(SwiftUI)
39+
40+
public init(top: CGFloat, right: CGFloat, bottom: CGFloat, left: CGFloat) {
41+
switch UserInterfaceLayoutDirection.current {
42+
case .leftToRight:
43+
self.init(top: top, leading: left, bottom: bottom, trailing: right)
44+
45+
case .rightToLeft:
46+
self.init(top: top, leading: right, bottom: bottom, trailing: left)
47+
48+
@unknown default:
49+
assertionFailure("Unknown user interface layout direction (will assume LTR): \(UserInterfaceLayoutDirection.current)")
50+
self.init(top: top, leading: left, bottom: bottom, trailing: right)
51+
}
52+
}
53+
54+
55+
public init(top: CGFloat, trailing: CGFloat, bottom: CGFloat, leading: CGFloat) {
56+
self.init(top: top, leading: leading, bottom: bottom, trailing: trailing)
57+
}
58+
59+
60+
public var left: CGFloat {
61+
switch UserInterfaceLayoutDirection.current {
62+
case .leftToRight: return leading
63+
case .rightToLeft: return trailing
64+
65+
@unknown default:
66+
assertionFailure("Unknown user interface layout direction (will assume LTR): \(UserInterfaceLayoutDirection.current)")
67+
return leading
68+
}
69+
}
70+
71+
72+
public var right: CGFloat {
73+
switch UserInterfaceLayoutDirection.current {
74+
case .leftToRight: return trailing
75+
case .rightToLeft: return leading
76+
77+
@unknown default:
78+
assertionFailure("Unknown user interface layout direction (will assume LTR): \(UserInterfaceLayoutDirection.current)")
79+
return trailing
80+
}
81+
}
82+
83+
84+
#else
85+
86+
3187
@available(watchOS 2.1, *)
3288
public init(top: CGFloat, right: CGFloat, bottom: CGFloat, left: CGFloat) {
3389
self.init(top: top, left: left, bottom: bottom, right: right)
@@ -47,10 +103,13 @@ extension NativeEdgeInsets: FourSidedAbsolute {
47103
self.init(top: top, right: trailing, bottom: bottom, left: leading)
48104
}
49105
}
106+
107+
#endif
50108
}
51109

52110

53111

112+
#if !canImport(SwiftUI)
54113
@available(watchOS 2.1, *)
55114
public extension NativeEdgeInsets {
56115
/// The value of whichever edge inset is leading in the current app's UI direction
@@ -76,11 +135,19 @@ public extension NativeEdgeInsets {
76135
}
77136
}
78137
}
138+
#endif
79139

80140

81141

82142
#if !canImport(UIKit) && canImport(AppKit)
83-
extension NSEdgeInsets: Equatable {}
143+
extension NSEdgeInsets: Equatable {
144+
public static func == (lhs: Self, rhs: Self) -> Bool {
145+
return lhs.top == rhs.top
146+
&& lhs.right == rhs.right
147+
&& lhs.bottom == rhs.bottom
148+
&& lhs.left == rhs.left
149+
}
150+
}
84151
#endif
85152

86153

@@ -90,12 +157,25 @@ public extension UserInterfaceLayoutDirection {
90157
// TODO: Move this to some other package
91158
@inline(__always)
92159
static var current: UserInterfaceLayoutDirection {
93-
#if canImport(WatchKit)
94-
return WKInterfaceDevice.current().layoutDirection
160+
#if canImport(SwiftUI)
161+
#if canImport(UIKit)
162+
let legacyCurrent = UIApplication.shared.userInterfaceLayoutDirection
163+
#elseif canImport(AppKit)
164+
let legacyCurrent = NSApp?.userInterfaceLayoutDirection ?? .leftToRight
165+
#endif
166+
switch legacyCurrent {
167+
case .leftToRight: return .leftToRight
168+
case .rightToLeft: return .rightToLeft
169+
@unknown default:
170+
assertionFailure("Unknown user interface layout direction (will assume LTR): \(UserInterfaceLayoutDirection.current)")
171+
return .leftToRight
172+
}
173+
#elseif canImport(WatchKit)
174+
return WKInterfaceDevice.current().layoutDirection
95175
#elseif canImport(UIKit)
96-
return UIApplication.shared.userInterfaceLayoutDirection
176+
return UIApplication.shared.userInterfaceLayoutDirection
97177
#elseif canImport(AppKit)
98-
return NSApp?.userInterfaceLayoutDirection ?? .leftToRight
178+
return NSApp?.userInterfaceLayoutDirection ?? .leftToRight
99179
#endif
100180
}
101181
}

Sources/RectangleTools/Synthesized Conveniences/Size2DCollection.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,8 @@ extension Size2DCollectionIndex: Point2D {
100100

101101

102102

103-
public extension Size2DCollection
104-
where
105-
Length: BinaryInteger,
106-
Element == Index
107-
{
103+
public extension Size2DCollection where Element == Index {
104+
108105
var startIndex: Index { Index(x: 0, y: 0) }
109106
var endIndex: Index { Index(x: width, y: height) }
110107
var lastValidIndex: Index { Index(x: width - 1, y: height - 1) }

Tests/RectangleToolsTests/FourSided Tests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ final class FourSidedTests: XCTestCase {
2424

2525
extension FourSidedTests {
2626

27-
private static let insets_raw_1_2_3_4 = NativeEdgeInsets(top: 1, left: 2, bottom: 3, right: 4)
27+
private static let insets_raw_1_2_3_4 = NativeEdgeInsets(top: 1, right: 4, bottom: 3, left: 2)
2828
private static let insets_synth_5_6_7_8 = NativeEdgeInsets(top: 5, right: 6, bottom: 7, left: 8)
2929

3030
func testFourSidedAbsoluteFields_EdgeInsets() {
@@ -44,11 +44,11 @@ extension FourSidedTests {
4444

4545

4646
func testInitializers_EdgeInsets() {
47-
XCTAssertEqual(NativeEdgeInsets(top: 5, right: 6, bottom: 7, left: 8), NativeEdgeInsets(top: 5, left: 8, bottom: 7, right: 6))
48-
XCTAssertEqual(NativeEdgeInsets(top: 9, eachHorizontal: 10, bottom: 11), NativeEdgeInsets(top: 9, left: 10, bottom: 11, right: 10))
49-
XCTAssertEqual(NativeEdgeInsets(eachVertical: 12, eachHorizontal: 13), NativeEdgeInsets(top: 12, left: 13, bottom: 12, right: 13))
50-
XCTAssertEqual(NativeEdgeInsets(each: 14), NativeEdgeInsets(top: 14, left: 14, bottom: 14, right: 14))
51-
XCTAssertEqual(NativeEdgeInsets.zero, NativeEdgeInsets(top: 0, left: 0, bottom: 0, right: 0))
47+
XCTAssertEqual(NativeEdgeInsets(top: 5, right: 6, bottom: 7, left: 8), NativeEdgeInsets(top: 5, right: 6, bottom: 7, left: 8))
48+
XCTAssertEqual(NativeEdgeInsets(top: 9, eachHorizontal: 10, bottom: 11), NativeEdgeInsets(top: 9, right: 10, bottom: 11, left: 10))
49+
XCTAssertEqual(NativeEdgeInsets(eachVertical: 12, eachHorizontal: 13), NativeEdgeInsets(top: 12, right: 13, bottom: 12, left: 13))
50+
XCTAssertEqual(NativeEdgeInsets(each: 14), NativeEdgeInsets(top: 14, right: 14, bottom: 14, left: 14))
51+
XCTAssertEqual(NativeEdgeInsets.zero, NativeEdgeInsets(top: 0, right: 0, bottom: 0, left: 0))
5252
}
5353

5454

0 commit comments

Comments
 (0)