Git Product home page Git Product logo

quicklookpreview's Introduction

QuickLook Preview for SwiftUI on Mac Catalyst

According to the documentation for QLPreviewController:

For Mac apps built with Mac Catalyst, presenting a QLPreviewController displays the preview in a QLPreviewPanel and dims the previously active window.

This package provides a way to present the preview that doesn't dim the window.

โš ๏ธ This package is designed for and only tested on Mac Catalyst apps.

screen-recording

Basic Usage

.quickLookPreview(_ items: Binding<[QuickLookPreviewItem]>, at index: Binding<Int> = Binding<Int>.constant(0))

Add the line above to a view. When $items is populated with QuickLookPreviewItems, the preview is presented.

Full Example

Note that the example uses files named 1.png, 2.png, and 3.png in the Xcode project.

import SwiftUI

struct ContentView: View {
    @State private var items: [QuickLookPreviewItem] = []
    @State private var index = 0
    
    var body: some View {
        ZStack {
            Button("Present QuickLook Preview") {
                // Workaround needed because there's no way to know when the preview window is dismissed.
                if self.items.isEmpty {
                    fillItems()
                } else {
                    Task {
                        self.items = []
                        do { try await Task.sleep(nanoseconds: 0) } catch { fillItems() }
                        fillItems()
                    }
                }
            }
        }
        .quickLookPreview(self.$items, at: self.$index)
    }
    
    func fillItems() {
        self.items = [
            QuickLookPreviewItem(url: Bundle.main.url(forResource: "1", withExtension: "png")!, title: "No. 1"),
            QuickLookPreviewItem(url: Bundle.main.url(forResource: "2", withExtension: "png")!, title: "No. 2"),
            QuickLookPreviewItem(url: Bundle.main.url(forResource: "3", withExtension: "png")!, title: "No. 3"),
        ]
        self.index = 2
    }
}

quicklookpreview's People

Contributors

tomdai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.