|
7 | 7 | // PopoverExploration.swift
|
8 | 8 | //
|
9 | 9 |
|
10 |
| -import SwiftUI |
11 |
| - |
12 |
| -struct PopView: View { |
13 |
| - @Binding var isPresented: Bool |
14 |
| - var body: some View { |
15 |
| - PBCard( |
16 |
| - border: false, |
17 |
| - padding: Spacing.small, |
18 |
| - shadow: .deeper, |
19 |
| - width: nil) { |
20 |
| - Text("I'm a popover. I can show content of any size.") |
21 |
| - .pbFont(.body, color: .text(.default)) |
22 |
| - } |
23 |
| - .onTapGesture { |
24 |
| - isPresented = false |
25 |
| - } |
26 |
| - } |
27 |
| -} |
28 |
| - |
29 |
| -extension View { |
30 |
| - func handlerPopoverController<Content: View>(isPresented: Binding<Bool>, position: CGPoint, @ViewBuilder content: @escaping (() -> Content)) -> some View { |
31 |
| -// #if os(macOS) |
32 |
| -// self.background(PopHostingView(isPresented: isPresented, content: content)) |
33 |
| -// #elseif os(iOS) |
34 |
| - self.background(PopHostingView(isPresented: isPresented, position: position, content: content)) |
35 |
| -// #endif |
36 |
| - } |
37 |
| -} |
38 |
| - |
39 |
| -#if os(macOS) |
40 |
| -import AppKit |
41 |
| - |
42 |
| -struct PopHostingView<Content: View>: NSViewRepresentable { |
43 |
| - @Binding var isPresented: Bool |
44 |
| - let position: CGPoint |
45 |
| - let content: () -> Content |
46 |
| - |
47 |
| - func makeNSView(context: Context) -> NSView { |
48 |
| - let nsView = NSView(frame: .zero) |
49 |
| - return nsView |
50 |
| - } |
51 |
| - |
52 |
| - func updateNSView(_ nsView: NSView, context: Context) { |
53 |
| - if isPresented { |
54 |
| - context.coordinator.showPopover(from: nsView) |
55 |
| - } else { |
56 |
| - context.coordinator.dismissPopover() |
57 |
| - } |
58 |
| - } |
59 |
| - |
60 |
| - func makeCoordinator() -> Coordinator { |
61 |
| - Coordinator(isPresented: $isPresented, content: content()) |
62 |
| - } |
63 |
| - |
64 |
| - class Coordinator: NSObject { |
65 |
| - let popover: NSPopover |
66 |
| - let contentController: NSHostingController<Content> |
67 |
| - @Binding var isPresented: Bool |
68 |
| - |
69 |
| - init(isPresented: Binding<Bool>, content: Content) { |
70 |
| - self.popover = NSPopover() |
71 |
| - self.contentController = NSHostingController(rootView: content) |
72 |
| - self._isPresented = isPresented |
73 |
| - |
74 |
| - super.init() |
75 |
| - self.popover.contentViewController = contentController |
76 |
| - self.popover.behavior = .transient |
77 |
| - } |
78 |
| - |
79 |
| - func showPopover(from sender: NSView) { |
80 |
| - var positioningView: NSView? |
81 |
| - positioningView = NSView() |
82 |
| - positioningView?.frame = sender.frame |
83 |
| - if let view = positioningView { |
84 |
| - sender.superview?.addSubview(view, positioned: .below, relativeTo: sender) |
85 |
| - } |
86 |
| - |
87 |
| - popover.contentViewController = PopViewController() |
88 |
| - popover.behavior = .transient |
89 |
| - popover.show(relativeTo: .zero, of: positioningView!, preferredEdge: .minY) |
90 |
| - |
91 |
| - positioningView?.frame = NSMakeRect(0, 200, 0, 0) |
92 |
| - } |
93 |
| - |
94 |
| - func dismissPopover() { |
95 |
| - popover.performClose(nil) |
96 |
| - } |
97 |
| - } |
98 |
| - |
99 |
| - class PopViewController: NSViewController { |
100 |
| - override func loadView() { |
101 |
| - let hostingView = NSHostingView(rootView: PopView(isPresented: .constant(false))) |
102 |
| - hostingView.layoutSubtreeIfNeeded() |
103 |
| - let contentSize = hostingView.fittingSize |
104 |
| - let popoverView = NSView(frame: CGRect(origin: .zero, size: contentSize)) |
105 |
| - hostingView.frame = popoverView.bounds |
106 |
| - popoverView.addSubview(hostingView) |
107 |
| - self.view = popoverView |
108 |
| - } |
109 |
| - } |
110 |
| -} |
111 |
| -#endif |
112 |
| - |
113 |
| -#if os(iOS) |
114 |
| -import UIKit |
115 |
| - |
116 |
| -struct PopHostingView<Content: View>: UIViewRepresentable { |
117 |
| - @Binding var isPresented: Bool |
118 |
| - let position: CGPoint |
119 |
| - let content: () -> Content |
120 |
| - |
121 |
| - func makeUIView(context: Context) -> UIView { |
122 |
| - let uiView = UIView(frame: .zero) |
123 |
| - return uiView |
124 |
| - } |
125 |
| - |
126 |
| - func updateUIView(_ uiView: UIView, context: Context) { |
127 |
| - if isPresented { |
128 |
| - context.coordinator.showPopover(from: uiView) |
129 |
| - } else { |
130 |
| - context.coordinator.dismissPopover() |
131 |
| - } |
132 |
| - } |
133 |
| - |
134 |
| - func makeCoordinator() -> Coordinator { |
135 |
| - Coordinator(isPresented: $isPresented, position: position, content: content()) |
136 |
| - } |
137 |
| - |
138 |
| - class Coordinator: NSObject, UIPopoverPresentationControllerDelegate { |
139 |
| - private let contentViewController: UIHostingController<Content> |
140 |
| - private weak var anchorView: UIView? |
141 |
| - @Binding var isPresented: Bool |
142 |
| - private let position: CGPoint |
143 |
| - |
144 |
| - var popoverView: UIView? |
145 |
| - |
146 |
| - init(isPresented: Binding<Bool>, position: CGPoint, content: Content) { |
147 |
| - self.contentViewController = UIHostingController(rootView: content) |
148 |
| - self._isPresented = isPresented |
149 |
| - self.position = position |
150 |
| - super.init() |
151 |
| - |
152 |
| - let hostingView = UIHostingController(rootView: PopView(isPresented: $isPresented)) |
153 |
| - let popoverView = hostingView.view |
154 |
| - popoverView?.frame = CGRect(x: position.x, y: position.y, width: 400, height: 400) |
155 |
| - popoverView?.layer.cornerRadius = 10 |
156 |
| - popoverView?.backgroundColor = .clear |
157 |
| - self.popoverView = popoverView |
158 |
| - } |
159 |
| - |
160 |
| - func showPopover(from anchorView: UIView) { |
161 |
| - guard let rootVC = anchorView.window?.rootViewController else { return } |
162 |
| - |
163 |
| - rootVC.modalPresentationStyle = .popover |
164 |
| - if let popoverView = popoverView { |
165 |
| - rootVC.view.addSubview(popoverView) |
166 |
| - } |
167 |
| - } |
168 |
| - |
169 |
| - func dismissPopover() { |
170 |
| - if let popoverView = popoverView { |
171 |
| - popoverView.removeFromSuperview() |
172 |
| - } |
173 |
| - } |
174 |
| - } |
175 |
| -} |
176 |
| -#endif |
| 10 | +//import SwiftUI |
| 11 | +// |
| 12 | +//public extension View { |
| 13 | +// public func pbPopoverAppKit<Content: View>(isPresented: Binding<Bool>, position: CGPoint = .init(x: 0, y: 0), @ViewBuilder content: @escaping (() -> Content)) -> some View { |
| 14 | +// self.background(PopHostingView(isPresented: isPresented, position: position, content: content)) |
| 15 | +// } |
| 16 | +//} |
| 17 | +// |
| 18 | +// |
| 19 | +// |
| 20 | +//#if os(macOS) |
| 21 | +//import AppKit |
| 22 | +// |
| 23 | +//struct PopHostingView<Content: View>: NSViewRepresentable { |
| 24 | +// @Binding var isPresented: Bool |
| 25 | +// let position: CGPoint |
| 26 | +// let content: () -> Content |
| 27 | +// |
| 28 | +// func makeNSView(context: Context) -> NSView { |
| 29 | +// let nsView = NSView(frame: .zero) |
| 30 | +// return nsView |
| 31 | +// } |
| 32 | +// |
| 33 | +// func updateNSView(_ nsView: NSView, context: Context) { |
| 34 | +// if isPresented { |
| 35 | +// context.coordinator.showPopover(from: nsView) |
| 36 | +// } else { |
| 37 | +// context.coordinator.dismissPopover() |
| 38 | +// } |
| 39 | +// } |
| 40 | +// |
| 41 | +// func makeCoordinator() -> Coordinator { |
| 42 | +// Coordinator(isPresented: $isPresented, content: content()) |
| 43 | +// } |
| 44 | +// |
| 45 | +// class Coordinator: NSObject { |
| 46 | +// let popover: NSPopover |
| 47 | +// @Binding var isPresented: Bool |
| 48 | +// let content: Content |
| 49 | +// |
| 50 | +// init(isPresented: Binding<Bool>, content: Content) { |
| 51 | +// self.popover = NSPopover() |
| 52 | +// self.content = content |
| 53 | +// self._isPresented = isPresented |
| 54 | +// super.init() |
| 55 | +// self.popover.contentViewController = PopViewController(contentView: content) |
| 56 | +// self.popover.behavior = .transient |
| 57 | +// } |
| 58 | +// |
| 59 | +// func showPopover(from sender: NSView) { |
| 60 | +// var positioningView: NSView? |
| 61 | +// positioningView = NSView() |
| 62 | +// positioningView?.frame = sender.frame |
| 63 | +// if let view = positioningView { |
| 64 | +// sender.superview?.addSubview(view, positioned: .below, relativeTo: sender) |
| 65 | +// } |
| 66 | +// |
| 67 | +// popover.contentViewController = PopViewController(contentView: content) |
| 68 | +// popover.behavior = .transient |
| 69 | +// popover.show(relativeTo: .zero, of: positioningView!, preferredEdge: .minY) |
| 70 | +// |
| 71 | +// positioningView?.frame = NSMakeRect(0, 200, 0, 0) |
| 72 | +// } |
| 73 | +// |
| 74 | +// func dismissPopover() { |
| 75 | +// popover.performClose(nil) |
| 76 | +// } |
| 77 | +// } |
| 78 | +// |
| 79 | +// class PopViewController: NSViewController { |
| 80 | +// private let contentView: Content |
| 81 | +// |
| 82 | +// init(contentView: Content) { |
| 83 | +// self.contentView = contentView |
| 84 | +// super.init(nibName: nil, bundle: nil) |
| 85 | +// } |
| 86 | +// |
| 87 | +// required init?(coder: NSCoder) { |
| 88 | +// fatalError("init(coder:) has not been implemented") |
| 89 | +// } |
| 90 | +// |
| 91 | +// override func loadView() { |
| 92 | +// let hostingView = NSHostingView(rootView: contentView) |
| 93 | +// hostingView.layoutSubtreeIfNeeded() |
| 94 | +// let contentSize = hostingView.fittingSize |
| 95 | +// let popoverView = NSView(frame: CGRect(origin: .zero, size: contentSize)) |
| 96 | +// hostingView.frame = popoverView.bounds |
| 97 | +// popoverView.addSubview(hostingView) |
| 98 | +// self.view = popoverView |
| 99 | +// } |
| 100 | +// } |
| 101 | +//} |
| 102 | +//#endif |
| 103 | +// |
| 104 | +//#if os(iOS) |
| 105 | +//import UIKit |
| 106 | +// |
| 107 | +//struct PopHostingView<Content: View>: UIViewRepresentable { |
| 108 | +// @Binding var isPresented: Bool |
| 109 | +// let position: CGPoint |
| 110 | +// let content: () -> Content |
| 111 | +// |
| 112 | +// func makeUIView(context: Context) -> UIView { |
| 113 | +// let uiView = UIView(frame: .zero) |
| 114 | +// return uiView |
| 115 | +// } |
| 116 | +// |
| 117 | +// func updateUIView(_ uiView: UIView, context: Context) { |
| 118 | +// if isPresented { |
| 119 | +// context.coordinator.showPopover(from: uiView) |
| 120 | +// } else { |
| 121 | +// context.coordinator.dismissPopover() |
| 122 | +// } |
| 123 | +// } |
| 124 | +// |
| 125 | +// func makeCoordinator() -> Coordinator { |
| 126 | +// Coordinator(isPresented: $isPresented, position: position, content: content()) |
| 127 | +// } |
| 128 | +// |
| 129 | +// class Coordinator: NSObject, UIPopoverPresentationControllerDelegate { |
| 130 | +// private let contentViewController: UIHostingController<Content> |
| 131 | +// private weak var anchorView: UIView? |
| 132 | +// @Binding var isPresented: Bool |
| 133 | +// private let position: CGPoint |
| 134 | +// |
| 135 | +// var popoverView: UIView? |
| 136 | +// |
| 137 | +// init(isPresented: Binding<Bool>, position: CGPoint, content: Content) { |
| 138 | +// self.contentViewController = UIHostingController(rootView: content) |
| 139 | +// self._isPresented = isPresented |
| 140 | +// self.position = position |
| 141 | +// super.init() |
| 142 | +// |
| 143 | +// let hostingView = UIHostingController(rootView: PopView(isPresented: $isPresented)) |
| 144 | +// let popoverView = hostingView.view |
| 145 | +// popoverView?.frame = CGRect(x: position.x, y: position.y, width: 400, height: 400) |
| 146 | +// popoverView?.layer.cornerRadius = 10 |
| 147 | +// popoverView?.backgroundColor = .clear |
| 148 | +// self.popoverView = popoverView |
| 149 | +// } |
| 150 | +// |
| 151 | +// func showPopover(from anchorView: UIView) { |
| 152 | +// guard let rootVC = anchorView.window?.rootViewController else { return } |
| 153 | +// |
| 154 | +// rootVC.modalPresentationStyle = .popover |
| 155 | +// if let popoverView = popoverView { |
| 156 | +// rootVC.view.addSubview(popoverView) |
| 157 | +// } |
| 158 | +// } |
| 159 | +// |
| 160 | +// func dismissPopover() { |
| 161 | +// if let popoverView = popoverView { |
| 162 | +// popoverView.removeFromSuperview() |
| 163 | +// } |
| 164 | +// } |
| 165 | +// } |
| 166 | +//} |
| 167 | +//#endif |
0 commit comments