title: How do I show QLPreviewPanel with SwiftUI on macOS?
body: <p>Trying to figure out how to work with QuickLook in SwiftUI on both iOS and macOS. I suspect that in far future, there will be some unified SwiftUI QL API, but can’t see it in sight yet, so let’s work with what we have…</p>
  
  <p>How do I present and configure a QLPreviewPanel from my SwiftUI view? So far, I have this:</p>
  
  <pre><code>struct ItemView: View {
      let previewPanelThing = PreviewPanelThing()
      var body: some View {
          Button("OSX preview") {
              print("osx preview")
              if let previewPanel = QLPreviewPanel.shared() {
                  self.previewPanelThing.updateControllerForPanel(previewPanel)
                  previewPanel.makeKeyAndOrderFront(self.previewPanelThing)
              }                   
          }
      }
  }
  
  class PreviewPanelThing: QLPreviewPanelDataSource {
  
      func updateControllerForPanel(_ panel: QLPreviewPanel) {
          print("updating controller")
          panel.updateController()
      }
  
      func numberOfPreviewItems(in panel: QLPreviewPanel!) -&gt; Int {
          print("number of items")
          return 1
      }
  
      func previewPanel(_ panel: QLPreviewPanel!, previewItemAt index: Int) -&gt; QLPreviewItem! {
          print("requesting preview item")
          let fileURL: URL = Bundle.main.url(forResource: "Thinking-of-getting-a-cat", withExtension: "png")!
          return fileURL as QLPreviewItem
      }   
  }
  </code></pre>
  
  <p>This isn’t working. I suspect this is because the QLPreviewPanel documentation says: <code>The preview panel follows the responder chain and adapts to the first responder willing to control it.</code> My <code>previewPanelThing</code> instance isn’t in the UI and responder chain. I’m not sure how the responder chain works in SwiftUI and how to best go about it.</p>