forked from aheze/Popovers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extensions.swift
352 lines (314 loc) · 11.7 KB
/
Extensions.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
//
// Extensions.swift
// Popovers
//
// Created by A. Zheng (github.com/aheze) on 2/4/22.
// Copyright © 2022 A. Zheng. All rights reserved.
//
#if os(iOS)
import SwiftUI
// MARK: - Shadows
public extension View {
/// A convenient way to apply a shadow.
func popoverShadow(shadow: Templates.Shadow = .system) -> some View {
self.shadow(
color: shadow.color,
radius: shadow.radius,
x: shadow.x,
y: shadow.y
)
}
/// A convenient way to apply a nullable shadow.
@ViewBuilder func popoverShadowIfNeeded(shadow: Templates.Shadow?) -> some View {
if let shadow = shadow {
popoverShadow(shadow: shadow)
}
}
}
// MARK: - Arrow Positioning
public extension Popover.Attributes.Position {
/// Determine which side an arrow is best placed.
func getArrowPosition() -> Templates.ArrowSide {
/// This only applied when the position is `.absolute`.
if case let .absolute(originAnchor, popoverAnchor) = self {
/// X = popover
switch originAnchor {
case .topLeft:
// X ------------
// | source frame
// |
switch popoverAnchor {
case .topRight:
return .right(.mostCounterClockwise)
case .right:
return .right(.centered)
case .bottomLeft:
return .bottom(.mostClockwise)
case .bottom:
return .bottom(.centered)
default:
break
}
case .top:
// -------X-------
// | source frame |
// | |
switch popoverAnchor {
case .bottomRight:
return .bottom(.mostCounterClockwise)
case .bottom:
return .bottom(.centered)
case .bottomLeft:
return .bottom(.mostClockwise)
default:
break
}
case .topRight:
// ------------- X
// source frame |
// |
switch popoverAnchor {
case .bottomRight:
return .bottom(.mostCounterClockwise)
case .bottom:
return .bottom(.centered)
case .left:
return .left(.centered)
case .topLeft:
return .left(.mostClockwise)
default:
break
}
case .right:
// ------------- |
// source frame X
// ______________|
switch popoverAnchor {
case .bottomLeft:
return .left(.mostCounterClockwise)
case .left:
return .left(.centered)
case .topLeft:
return .left(.mostClockwise)
default:
break
}
case .bottomRight:
// |
// source frame |
// ______________ X
switch popoverAnchor {
case .bottomLeft:
return .left(.mostCounterClockwise)
case .left:
return .left(.centered)
case .top:
return .top(.centered)
case .topRight:
return .top(.mostClockwise)
default:
break
}
case .bottom:
// | |
// | source frame |
// |_______X________|
switch popoverAnchor {
case .topRight:
return .top(.mostCounterClockwise)
case .top:
return .top(.centered)
case .topLeft:
return .top(.mostClockwise)
default:
break
}
case .bottomLeft:
// |
// | source frame
// X ______________
switch popoverAnchor {
case .topLeft:
return .top(.mostCounterClockwise)
case .top:
return .top(.centered)
case .right:
return .right(.centered)
case .bottomRight:
return .top(.mostClockwise)
default:
break
}
case .left:
// |--------------
// X source frame
// |______________
switch popoverAnchor {
case .topRight:
return .right(.mostCounterClockwise)
case .right:
return .right(.centered)
case .bottomRight:
return .right(.mostClockwise)
default:
break
}
case .center:
break
}
}
/// No preferred arrow. Just go with a top-centered one.
return .top(.centered)
}
}
// MARK: - Utilities
/// Convert degrees to radians and back. From https://stackoverflow.com/a/29179878
public extension BinaryInteger {
var degreesToRadians: CGFloat { CGFloat(self) * .pi / 180 }
}
public extension FloatingPoint {
var degreesToRadians: Self { self * .pi / 180 }
var radiansToDegrees: Self { self * 180 / .pi }
}
/// Get an array of views from `@ViewBuilder`. From https://github.com/GeorgeElsham/ViewExtractor
/// Used for the Menu template.
/// Extract SwiftUI views from ViewBuilder content.
public struct ViewExtractor {
/// Represents a `View`. Can be used to get `AnyView` from `Any`.
public struct GenericView {
let body: Any
/// Get `AnyView` from a generic view.
var anyView: AnyView? {
AnyView(_fromValue: body)
}
}
/// If the content is a `ForEach`, this gives the range. If it fails, it returns `nil`.
public var forEachRange: Range<Int>? {
struct FakeCollection {
let indices: Range<Int>
}
// Reflect `ForEach` to get the `data`.
guard let forEach = forEach else { return nil }
let mirror = Mirror(reflecting: forEach)
guard let data = mirror.descendant("data") else { return nil }
// Bind the collection to `FakeCollection`, to get the `indices`.
return withUnsafeBytes(of: data) { ptr -> Range<Int>? in
let binded = ptr.bindMemory(to: FakeCollection.self)
return binded.first?.indices
}
}
private let forEach: DynamicViewContentProvider?
public init<Content: View & DynamicViewContentProvider>(content: ForEachContent<Content>) {
forEach = content()
}
/// Get the view at this exact index, ignoring types of views
/// checks. For example, `EmptyView` won't be ignored.
///
/// - Parameter index: Index within `ForEach` to get.
/// - Returns: View at this index, or `nil` if none.
public func uncheckedView(at index: Int) -> AnyView? {
forEach?.extractContent(at: index)
}
/// Gets views from a `TupleView`.
/// - Parameter content: Content to extract the views from.
/// - Returns: Extracted views.
public static func getViews<Views>(@ViewBuilder from content: TupleContent<Views>) -> [AnyView] {
content().views
}
/// Get views from a normal view closure.
/// - Parameter content: Content to extract the views from.
/// - Returns: Extracted views.
public static func getViews<Content: View>(@ViewBuilder from content: NormalContent<Content>) -> [AnyView] {
ViewExtractor.views(from: content())
}
/// Gets views from `Any`. Also splits up `DynamicViewContent` into separate views.
/// - Parameter view: View of `Any` type.
/// - Returns: Views contained by this `view`.
public static func views(from view: Any) -> [AnyView] {
checkingViewContent(view) {
// Just a normal view. Convert it from type `Any` to `AnyView`.
withUnsafeBytes(of: view) { ptr -> [AnyView] in
// Cast from type `Any` to `GenericView`,
// which mimics the structure of a `View`.
let binded = ptr.bindMemory(to: GenericView.self)
// Get `AnyView` from the 'fake' view body.
return binded.first?.anyView.map { [$0] } ?? []
}
}
}
/// Return the view content. This removes views like `EmptyView`,
/// and gets content from within `ForEach`.
///
/// - Parameters:
/// - view: View to test.
/// - actual: If this is a normal view, this content is used.
/// - Returns: Array of content views.
fileprivate static func checkingViewContent(_ view: Any, actual: () -> [AnyView]) -> [AnyView] {
// Check this is not an empty view with no content.
if view is EmptyView {
return []
}
// Check this is not a `nil` view. Can occur due to conditionals.
if case Optional<Any>.none = view {
return []
}
// If this view is a `ForEach`, extract all contained views.
if let forEach = view as? DynamicViewContentProvider {
return forEach.extractContent()
}
// Actual view.
return actual()
}
}
// MARK: - Content types
public typealias TupleContent<Views> = () -> TupleView<Views>
public typealias NormalContent<Content: View> = () -> Content
public typealias ForEachContent<Content: View & DynamicViewContentProvider> = () -> Content
// MARK: - TupleView views
public extension TupleView {
/// Convert tuple of views to array of `AnyView`s.
var views: [AnyView] {
let children = Mirror(reflecting: value).children
return children.flatMap { ViewExtractor.views(from: $0.value) }
}
}
// MARK: - Dynamic view content
public protocol DynamicViewContentProvider {
func extractContent() -> [AnyView]
func extractContent(at index: Int) -> AnyView?
}
extension ForEach: DynamicViewContentProvider where Content: View {
public func extractContent() -> [AnyView] {
// Dynamically mirrors the current instance.
let mirror = Mirror(reflecting: self)
// Retrieving hidden properties containing the data and content.
if let data = mirror.descendant("data") as? Data,
let content = mirror.descendant("content") as? (Data.Element) -> Content
{
return data.flatMap { element -> [AnyView] in
// Create content given the data for this `ForEach` element.
let newContent = content(element)
// Gets content for element.
return ViewExtractor.checkingViewContent(newContent) {
[AnyView(newContent)]
}
}
} else {
// Return no content if failure.
return []
}
}
public func extractContent(at index: Int) -> AnyView? {
// Dynamically mirrors the current instance.
let mirror = Mirror(reflecting: self)
// Check view is valid.
guard let data = mirror.descendant("data") as? Data,
0 ..< data.count ~= index,
let content = mirror.descendant("content") as? (Data.Element) -> Content
else { return nil }
// Return view for specific index.
let dataIndex = data.index(data.startIndex, offsetBy: index)
return AnyView(content(data[dataIndex]))
}
}
#endif