-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathPopover.swift
89 lines (81 loc) · 2.36 KB
/
Popover.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
//
// Popover.swift
// Popovers
//
// Created by A. Zheng (github.com/aheze) on 12/23/21.
// Copyright © 2022 A. Zheng. All rights reserved.
//
#if os(iOS)
import Combine
import SwiftUI
/**
A view that is placed over other views.
*/
public struct Popover: Identifiable {
/**
Stores information about the popover.
This includes the attributes, frame, and acts like a view model. If using SwiftUI, access it using `PopoverReader`.
*/
public var context: Context
/// The view that the popover presents.
public var view: AnyView
/// A view that goes behind the popover.
public var background: AnyView
/**
Convenience accessor for the popover's ID.
*/
public var id: UUID {
get {
context.id
} set {
context.id = newValue
}
}
/// Convenience accessor for the popover's attributes.
public var attributes: Attributes {
get {
context.attributes
} set {
context.attributes = newValue
}
}
/**
A popover.
- parameter attributes: Customize the popover.
- parameter view: The view to present.
*/
public init<Content: View>(
attributes: Attributes = .init(),
@ViewBuilder view: @escaping () -> Content
) {
let context = Context()
context.attributes = attributes
self.context = context
self.view = AnyView(view().environmentObject(context))
background = AnyView(Color.clear)
}
/**
A popover with a background.
- parameter attributes: Customize the popover.
- parameter view: The view to present.
- parameter background: The view to present in the background.
*/
public init<MainContent: View, BackgroundContent: View>(
attributes: Attributes = .init(),
@ViewBuilder view: @escaping () -> MainContent,
@ViewBuilder background: @escaping () -> BackgroundContent
) {
let context = Context()
context.attributes = attributes
self.context = context
self.view = AnyView(view().environmentObject(self.context))
self.background = AnyView(background().environmentObject(self.context))
}
}
extension Popover: Equatable {
/// Conform to equatable.
public static func == (lhs: Popover, rhs: Popover) -> Bool {
return lhs.id == rhs.id
}
}
#endif