SPB Git

Repository Public MIT

doc-glass-gallery.md 3.7 KB 79 lines markdown

1# GlassGallery23| | |4|---|---|5| Identifier | `glass-gallery` |6| Module | `SPGlassMedia` |7| Kind | `content-component` — images on an opaque surface; glass only for the viewer's floating buttons |8| Platforms | iOS 26, iPadOS 26, macOS 26 |9| Example | `Examples/Gallery/Media/GlassGalleryExample.swift` |1011## Minimal usage1213```swift14GlassGallery(items: [15    GlassGalleryItem(id: "cover", description: "Album cover", image: Image("cover")),16    GlassGalleryItem(id: "photo-1", description: "Harbour at dusk") { try await loadPhoto(1) },17])18```1920## Variants2122| Item initialiser | Use |23|---|---|24| `init(id:description:image:)` | A local `Image` (asset catalogue, already decoded). Not cached. |25| `init(id:description:load:)` | `@Sendable () async throws -> Image`, run when the cell appears. |26| `init(id:description:loadCGImage:)` | Same with a `CGImage`, for decoders that work off the main actor. |2728## Parameters2930| Parameter | Type | Default | Meaning |31|---|---|---|---|32| `items` | `[GlassGalleryItem]` | — | Images in display order; identifiers must be unique. |33| `cache` | `GlassGalleryCache?` | `nil` | A cache shared with other views. By default the gallery owns one. |34| `cacheCapacity` | `Int` | `64` | Capacity of the owned cache. |35| `minimumCellWidth` | `CGFloat` | `104` | Smallest cell width; the adaptive grid fits as many columns as possible. |3637`GlassBoundedCache<Key, Value>` is a public `actor` with a capacity and least-recently-used eviction (`value(for:)`, `insert(_:for:)`, `removeValue(for:)`, `removeAll()`, `count`, `keysByRecency`). `GlassGalleryCache` is `GlassBoundedCache<String, Image>`.3839## States4041- **Loading**: a progress indicator on the theme's surface colour.42- **Loaded**: the image, cropped to a square in the grid and fitted in the viewer.43- **Failed**: a warning symbol with "Media unavailable". Cancelled loads are not failures: the cell loads again when it reappears.44- **Empty** (`items` is empty): a `ContentUnavailableView`.45- **Viewer**: full-screen cover on iOS and iPadOS, sheet on macOS; previous and next buttons (disabled at the ends), close button, caption, pinch and double-tap zoom from 1× to 4×, pan while zoomed.4647## Advanced example4849```swift50let sharedCache = GlassGalleryCache(capacity: 200)5152GlassGallery(53    items: listing.photos.map { photo in54        GlassGalleryItem(id: photo.id, description: photo.caption, loadCGImage: {55            let data = try await store.data(for: photo)        // cancellable I/O56            try Task.checkCancellation()57            return try decoder.thumbnail(from: data, maxPixelSize: 600)58        })59    },60    cache: sharedCache,61    minimumCellWidth: 14062)63```6465## Accessibility6667- Each cell is a button labelled "Image n of m, description" with the image trait.68- In the viewer the image carries the same label and supports the VoiceOver zoom action; the buttons are labelled "Previous", "Next" and "Close".69- Keyboard: left and right arrows navigate, Escape closes.70- Failure is a symbol plus text. The double-tap zoom animation becomes a short fade with Reduce Motion.7172## Limits7374- Loading starts in the cell's `.task` and is cancelled when the cell leaves the screen; the loader must itself honour cancellation for the work to actually stop.75- The grid and the viewer use the same image: there is no separate thumbnail and full-size source. Provide an image large enough for the viewer, or downscale in the loader and accept a softer zoom.76- The cache bounds the number of images, not their memory size; choose the capacity for the image size you load.77- The viewer navigates with buttons and keys, not with a horizontal swipe.78- Zoom gestures were compiled but not exercised in a running application during verification; the clamping logic is unit-tested.79
Files main
Menu

Theme