|
| 1 | +// |
| 2 | +// InstantPopoverModifier.swift |
| 3 | +// CodeEdit |
| 4 | +// |
| 5 | +// Created by Kihron on 10/26/24. |
| 6 | +// |
| 7 | + |
| 8 | +import SwiftUI |
| 9 | + |
| 10 | +struct InstantPopoverModifier<PopoverContent: View>: ViewModifier { |
| 11 | + @Binding var isPresented: Bool |
| 12 | + let arrowEdge: Edge |
| 13 | + let popoverContent: PopoverContent |
| 14 | + |
| 15 | + func body(content: Content) -> some View { |
| 16 | + content |
| 17 | + .background( |
| 18 | + PopoverPresenter( |
| 19 | + isPresented: $isPresented, |
| 20 | + arrowEdge: arrowEdge, |
| 21 | + contentView: popoverContent |
| 22 | + ) |
| 23 | + ) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +struct PopoverPresenter<ContentView: View>: NSViewRepresentable { |
| 28 | + @Binding var isPresented: Bool |
| 29 | + let arrowEdge: Edge |
| 30 | + let contentView: ContentView |
| 31 | + |
| 32 | + func makeNSView(context: Context) -> NSView { NSView() } |
| 33 | + |
| 34 | + func updateNSView(_ nsView: NSView, context: Context) { |
| 35 | + if isPresented, context.coordinator.popover == nil { |
| 36 | + let popover = NSPopover() |
| 37 | + popover.animates = false |
| 38 | + let hostingController = NSHostingController(rootView: contentView) |
| 39 | + |
| 40 | + hostingController.view.layoutSubtreeIfNeeded() |
| 41 | + let contentSize = hostingController.view.fittingSize |
| 42 | + popover.contentSize = contentSize |
| 43 | + |
| 44 | + popover.contentViewController = hostingController |
| 45 | + popover.delegate = context.coordinator |
| 46 | + popover.behavior = .semitransient |
| 47 | + |
| 48 | + let nsRectEdge = edgeToNSRectEdge(arrowEdge) |
| 49 | + popover.show(relativeTo: nsView.bounds, of: nsView, preferredEdge: nsRectEdge) |
| 50 | + context.coordinator.popover = popover |
| 51 | + |
| 52 | + if let parentWindow = nsView.window { |
| 53 | + context.coordinator.startObservingWindow(parentWindow) |
| 54 | + } |
| 55 | + } else if !isPresented, let popover = context.coordinator.popover { |
| 56 | + popover.close() |
| 57 | + context.coordinator.popover = nil |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + func makeCoordinator() -> Coordinator { |
| 62 | + Coordinator(isPresented: $isPresented) |
| 63 | + } |
| 64 | + |
| 65 | + class Coordinator: NSObject, NSPopoverDelegate { |
| 66 | + @Binding var isPresented: Bool |
| 67 | + var popover: NSPopover? |
| 68 | + |
| 69 | + init(isPresented: Binding<Bool>) { |
| 70 | + _isPresented = isPresented |
| 71 | + super.init() |
| 72 | + } |
| 73 | + |
| 74 | + func startObservingWindow(_ window: NSWindow) { |
| 75 | + /// Observe when the window loses focus |
| 76 | + NotificationCenter.default.addObserver( |
| 77 | + forName: NSWindow.didResignKeyNotification, |
| 78 | + object: window, |
| 79 | + queue: .main |
| 80 | + ) { [weak self] _ in |
| 81 | + guard let self = self else { return } |
| 82 | + /// The parent window is no longer focused, close the popover |
| 83 | + DispatchQueue.main.async { |
| 84 | + self.isPresented = false |
| 85 | + self.popover?.close() |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + func popoverWillClose(_ notification: Notification) { |
| 91 | + DispatchQueue.main.async { |
| 92 | + self.isPresented = false |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + func popoverDidClose(_ notification: Notification) { |
| 97 | + popover = nil |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private func edgeToNSRectEdge(_ edge: Edge) -> NSRectEdge { |
| 102 | + switch edge { |
| 103 | + case .top: return .minY |
| 104 | + case .leading: return .minX |
| 105 | + case .bottom: return .maxY |
| 106 | + case .trailing: return .maxX |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +extension View { |
| 112 | + |
| 113 | + /// A custom view modifier that presents a popover attached to the view with no animation. |
| 114 | + /// - Parameters: |
| 115 | + /// - isPresented: A binding to whether the popover is presented. |
| 116 | + /// - arrowEdge: The edge of the view that the popover points to. Defaults to `.bottom`. |
| 117 | + /// - content: A closure returning the content of the popover. |
| 118 | + /// - Returns: A view that presents a popover when `isPresented` is `true`. |
| 119 | + func instantPopover<Content: View>( |
| 120 | + isPresented: Binding<Bool>, |
| 121 | + arrowEdge: Edge = .bottom, |
| 122 | + @ViewBuilder content: () -> Content |
| 123 | + ) -> some View { |
| 124 | + self.modifier( |
| 125 | + InstantPopoverModifier( |
| 126 | + isPresented: isPresented, |
| 127 | + arrowEdge: arrowEdge, |
| 128 | + popoverContent: content() |
| 129 | + ) |
| 130 | + ) |
| 131 | + } |
| 132 | +} |
0 commit comments