Created
March 21, 2022 13:14
-
-
Save krzyzanowskim/fcfcc6d4a2ca87c253307d63ef9bc61c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct SharingServicePicker: NSViewRepresentable { | |
@Binding var isPresented: Bool | |
var items: [Any] = [] | |
func makeNSView(context: Context) -> NSView { | |
let view = NSView() | |
view.translatesAutoresizingMaskIntoConstraints = false | |
return view | |
} | |
func updateNSView(_ nsView: NSView, context: Context) { | |
if isPresented { | |
context.coordinator.owner = self | |
let picker = NSSharingServicePicker(items: items) | |
picker.delegate = context.coordinator | |
DispatchQueue.main.async { | |
picker.show(relativeTo: .zero, of: nsView, preferredEdge: .minY) | |
} | |
} | |
} | |
func makeCoordinator() -> Coordinator { | |
Coordinator(owner: self) | |
} | |
class Coordinator: NSObject, NSSharingServicePickerDelegate { | |
var owner: SharingServicePicker | |
init(owner: SharingServicePicker) { | |
self.owner = owner | |
} | |
func sharingServicePicker(_ sharingServicePicker: NSSharingServicePicker, didChoose service: NSSharingService?) { | |
sharingServicePicker.delegate = nil | |
self.owner.isPresented = false | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment