SPB Git

Repository Public MIT

glass-compass.md 2.6 KB

# GlassCompass

Identifier glass-compass
Module SPGlassMaps
Kind custom-glass — a round interactive glassSurface
Platforms iOS 26, iPadOS 26, macOS 26
Example Examples/Gallery/Maps/GlassCompassExample.swift

# Minimal usage

swift
GlassCompass(heading: mapHeading) { mapHeading = 0 }

# Variants

Variant Use
heading: Double The needle points north for a map rotated by that heading.
heading: nil Heading unavailable: outlined, dimmed needle; the button is disabled.
size: .small, .regular, .large.

# Parameters

Parameter Type Default Meaning
heading Double? — Direction the top of the map points to, degrees clockwise from north. Any finite value; it is normalised to 0…359. NaN and infinities count as unavailable.
size GlassControlSize .regular Size of the control.
onResetNorth @MainActor () -> Void — Called on activation; the host rotates its map back to north.

# States

  • Heading known: filled north arrow in the theme's destructive colour (the conventional red needle), rotated by the opposite of the heading.
  • Unavailable: outlined arrow in the secondary text colour, not rotated, button disabled.
  • Pressed / focused: the interactive glass reacts to touch and pointer.

The needle rotates along the shortest arc: from 350° to 10° it turns 20°, not 340° (GlassHeading.needleRotation).

# Advanced example

swift
struct MapChrome: View {
    @Bindable var engine: TileEngine          // a GlassMapEngine

    var body: some View {
        GlassCompass(heading: engine.heading, size: .large) {
            withAnimation(.smooth) { engine.resetNorth() }
        }
        .opacity(engine.heading == 0 ? 0.6 : 1)   // de-emphasise when already north-up
    }
}

# Accessibility

  • Label "Reset to north"; value "Heading 270 degrees" or "Heading unavailable". The tooltip on macOS is the same text.
  • Availability is conveyed by the shape of the symbol (filled or outlined), the disabled state and the value — not by colour alone.
  • With Reduce Motion the needle is set to its new angle without a rotation animation.
  • The target is at least 44 points on iOS and iPadOS.

# Limits

  • The compass shows an injected heading: it does not read the device compass or any sensor.
  • The needle is an SF Symbol; there are no cardinal letters or degree ticks.
  • Every change of heading starts a short animation; an engine that reports the heading on every frame of a gesture should round it or throttle it.
Files main
Menu

Theme