SPB Git

Repository Public MIT

example-glass-gallery.txt 3.3 KB 71 lines text

1import SwiftUI2import CoreGraphics3import SPGlassKit45public struct GlassGalleryExample: GlassExample {6    public static let componentID = "glass-gallery"78    public init() {}910    public var body: some View {11        VStack(alignment: .leading, spacing: 8) {12            Text("Sample data · drawings and gradients generated in code, one slow image and one failing image.")13                .font(.caption)14                .foregroundStyle(.secondary)15                .padding(.horizontal)16            GlassGallery(items: Self.items, cacheCapacity: 12, layout: .featured, selection: "drawing-1")17        }18    }1920    private struct SampleLoadingError: Error {}2122    static let items: [GlassGalleryItem] = {23        let names = ["Dawn", "Lagoon", "Orchard", "Ember", "Glacier", "Dusk", "Moss", "Coral", "Slate", "Saffron", "Iris", "Tundra"]24        var items = names.enumerated().map { index, name in25            GlassGalleryItem(id: "gradient-\(index)", description: "\(name) gradient", loadCGImage: {26                // Stands in for decoding a file; the sleep makes cancellation observable while scrolling.27                try await Task.sleep(for: .milliseconds(index == 3 ? 1_500 : 120))28                return try gradient(hue: Double(index) / Double(names.count))29            })30        }31        // Local images, already in memory: the drawn compositions of the examples.32        for index in (0..<6).reversed() {33            items.insert(GlassGalleryItem(id: "drawing-\(index)", description: SampleArtwork.title(index), image: SampleArtwork.image(index)), at: 0)34        }35        items.insert(GlassGalleryItem(id: "broken", description: "An image that fails to load", load: { throw SampleLoadingError() }), at: 8)36        return items37    }()3839    /// Draws a diagonal two-colour gradient into a bitmap.40    nonisolated static func gradient(hue: Double, side: Int = 256) throws -> CGImage {41        let space = CGColorSpaceCreateDeviceRGB()42        guard let context = CGContext(data: nil, width: side, height: side, bitsPerComponent: 8, bytesPerRow: 0, space: space, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else {43            throw SampleLoadingError()44        }45        let colors = [rgb(hue: hue), rgb(hue: (hue + 0.18).truncatingRemainder(dividingBy: 1))] as CFArray46        guard let gradient = CGGradient(colorsSpace: space, colors: colors, locations: [0, 1]) else {47            throw SampleLoadingError()48        }49        context.drawLinearGradient(gradient, start: .zero, end: CGPoint(x: side, y: side), options: [])50        guard let image = context.makeImage() else { throw SampleLoadingError() }51        return image52    }5354    /// A saturated colour for a hue in 0…1, without any platform colour type.55    nonisolated private static func rgb(hue: Double) -> CGColor {56        let sector = hue * 657        let x = 1 - abs(sector.truncatingRemainder(dividingBy: 2) - 1)58        let (r, g, b): (Double, Double, Double) = switch Int(sector) % 6 {59        case 0: (1, x, 0)60        case 1: (x, 1, 0)61        case 2: (0, 1, x)62        case 3: (0, x, 1)63        case 4: (x, 0, 1)64        default: (1, 0, x)65        }66        return CGColor(red: 0.25 + 0.6 * r, green: 0.25 + 0.6 * g, blue: 0.25 + 0.6 * b, alpha: 1)67    }68}6970#Preview("GlassGallery") { GlassGalleryExample().glassTheme(.crystal) }71
Files main
Menu

Theme