6
6
// Copyright © 2020 Ben Leggiero. All rights reserved.
7
7
//
8
8
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)
10
16
import WatchKit
11
17
12
18
public typealias NativeEdgeInsets = UIEdgeInsets
28
34
29
35
@available ( watchOS 2 . 1 , * )
30
36
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
+
31
87
@available ( watchOS 2 . 1 , * )
32
88
public init ( top: CGFloat , right: CGFloat , bottom: CGFloat , left: CGFloat ) {
33
89
self . init ( top: top, left: left, bottom: bottom, right: right)
@@ -47,10 +103,13 @@ extension NativeEdgeInsets: FourSidedAbsolute {
47
103
self . init ( top: top, right: trailing, bottom: bottom, left: leading)
48
104
}
49
105
}
106
+
107
+ #endif
50
108
}
51
109
52
110
53
111
112
+ #if !canImport(SwiftUI)
54
113
@available ( watchOS 2 . 1 , * )
55
114
public extension NativeEdgeInsets {
56
115
/// The value of whichever edge inset is leading in the current app's UI direction
@@ -76,11 +135,19 @@ public extension NativeEdgeInsets {
76
135
}
77
136
}
78
137
}
138
+ #endif
79
139
80
140
81
141
82
142
#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
+ }
84
151
#endif
85
152
86
153
@@ -90,12 +157,25 @@ public extension UserInterfaceLayoutDirection {
90
157
// TODO: Move this to some other package
91
158
@inline ( __always)
92
159
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
95
175
#elseif canImport(UIKit)
96
- return UIApplication . shared. userInterfaceLayoutDirection
176
+ return UIApplication . shared. userInterfaceLayoutDirection
97
177
#elseif canImport(AppKit)
98
- return NSApp ? . userInterfaceLayoutDirection ?? . leftToRight
178
+ return NSApp ? . userInterfaceLayoutDirection ?? . leftToRight
99
179
#endif
100
180
}
101
181
}
0 commit comments