Skip to content

Instantly share code, notes, and snippets.

@krzyzanowskim
Created March 21, 2022 13:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krzyzanowskim/fcfcc6d4a2ca87c253307d63ef9bc61c to your computer and use it in GitHub Desktop.
Save krzyzanowskim/fcfcc6d4a2ca87c253307d63ef9bc61c to your computer and use it in GitHub Desktop.
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