SPB Git

Repository Public MIT

GlassGalleryExample.swift 4.7 KB 108 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Examples/Gallery/Media/GlassGalleryExample.swift8// Module           : Examples9// Repository       : https://git.spboucher.ai/sp-glasskit10// Project Website  : https://glass.spboucher.ai11// Created          : 2026-12-0212// Last Modified    : 2026-12-0213//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : MIT33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import SwiftUI42import CoreGraphics43import SPGlassKit4445public struct GlassGalleryExample: GlassExample {46    public static let componentID = "glass-gallery"4748    public init() {}4950    public var body: some View {51        VStack(alignment: .leading, spacing: 8) {52            Text("Sample data · gradients generated in code, one slow image and one failing image.")53                .font(.caption)54                .foregroundStyle(.secondary)55                .padding(.horizontal)56            GlassGallery(items: Self.items, cacheCapacity: 12)57        }58    }5960    private struct SampleLoadingError: Error {}6162    static let items: [GlassGalleryItem] = {63        let names = ["Dawn", "Lagoon", "Orchard", "Ember", "Glacier", "Dusk", "Moss", "Coral", "Slate", "Saffron", "Iris", "Tundra"]64        var items = names.enumerated().map { index, name in65            GlassGalleryItem(id: "gradient-\(index)", description: "\(name) gradient", loadCGImage: {66                // Stands in for decoding a file; the sleep makes cancellation observable while scrolling.67                try await Task.sleep(for: .milliseconds(index == 3 ? 1_500 : 120))68                return try gradient(hue: Double(index) / Double(names.count))69            })70        }71        items.insert(GlassGalleryItem(id: "local-symbol", description: "Local symbol image", image: Image(systemName: "mountain.2.fill")), at: 1)72        items.insert(GlassGalleryItem(id: "broken", description: "An image that fails to load", load: { throw SampleLoadingError() }), at: 5)73        return items74    }()7576    /// Draws a diagonal two-colour gradient into a bitmap.77    nonisolated static func gradient(hue: Double, side: Int = 256) throws -> CGImage {78        let space = CGColorSpaceCreateDeviceRGB()79        guard let context = CGContext(data: nil, width: side, height: side, bitsPerComponent: 8, bytesPerRow: 0, space: space, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else {80            throw SampleLoadingError()81        }82        let colors = [rgb(hue: hue), rgb(hue: (hue + 0.18).truncatingRemainder(dividingBy: 1))] as CFArray83        guard let gradient = CGGradient(colorsSpace: space, colors: colors, locations: [0, 1]) else {84            throw SampleLoadingError()85        }86        context.drawLinearGradient(gradient, start: .zero, end: CGPoint(x: side, y: side), options: [])87        guard let image = context.makeImage() else { throw SampleLoadingError() }88        return image89    }9091    /// A saturated colour for a hue in 0…1, without any platform colour type.92    nonisolated private static func rgb(hue: Double) -> CGColor {93        let sector = hue * 694        let x = 1 - abs(sector.truncatingRemainder(dividingBy: 2) - 1)95        let (r, g, b): (Double, Double, Double) = switch Int(sector) % 6 {96        case 0: (1, x, 0)97        case 1: (x, 1, 0)98        case 2: (0, 1, x)99        case 3: (0, x, 1)100        case 4: (x, 0, 1)101        default: (1, 0, x)102        }103        return CGColor(red: 0.25 + 0.6 * r, green: 0.25 + 0.6 * g, blue: 0.25 + 0.6 * b, alpha: 1)104    }105}106107#Preview("GlassGallery") { GlassGalleryExample().glassTheme(.crystal) }108
Files main
Menu

Theme