forked from LoopKit/LoopKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPopoverLink.swift
More file actions
75 lines (65 loc) · 2.37 KB
/
PopoverLink.swift
File metadata and controls
75 lines (65 loc) · 2.37 KB
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
//
// PopoverLink.swift
//
// Created by Manuel Weiel on 09.07.20.
//
// From https://manuel.weiel.eu/bringing-the-simplicity-of-navigationlink-to-popovers/
import SwiftUI
public struct PopoverLink<Label, Destination> : View where Label : View, Destination : View {
@Environment(\.horizontalSizeClass) var horizontalSizeClass
private let destination: Destination
private let label: Label
private var isActive: Binding<Bool>?
private var isFullScreen: Bool = false
@State private var internalIsActive = false
public init(_ localizedText: Text, destination: Destination) where Label == Text {
self.init(destination: destination, label: { localizedText })
}
/// Creates an instance that presents `destination`.
public init(destination: Destination, @ViewBuilder label: () -> Label) {
self.destination = destination
self.label = label()
}
/// Creates an instance that presents `destination` when active.
public init(destination: Destination, isActive: Binding<Bool>, @ViewBuilder label: () -> Label) {
self.destination = destination
self.label = label()
self.isActive = isActive
}
private init(_ other: PopoverLink, isFullScreen: Bool) {
self.destination = other.destination
self.isActive = other.isActive
self.label = other.label
self.isFullScreen = isFullScreen
}
private func popoverButton() -> some View {
Button {
(isActive ?? _internalIsActive.projectedValue).wrappedValue = true
} label: {
label
}
}
/// The content and behavior of the view.
public var body: some View {
if isFullScreen {
popoverButton().fullScreenCover(isPresented: (isActive ?? _internalIsActive.projectedValue)) {
destination
}
} else {
if horizontalSizeClass == .compact {
popoverButton().sheet(isPresented: (isActive ?? _internalIsActive.projectedValue)) {
destination
}
} else {
popoverButton().popover(isPresented: (isActive ?? _internalIsActive.projectedValue)) {
destination
}
}
}
}
}
extension PopoverLink {
public func fullScreen() -> PopoverLink {
PopoverLink(self, isFullScreen: true)
}
}