Glimmer / app · glimmertoday.ai / native / Glimmer / Design.swift
| 1 | import SwiftUI |
| 2 | #if os(macOS) |
| 3 | import AppKit |
| 4 | #else |
| 5 | import UIKit |
| 6 | #endif |
| 7 | |
| 8 | enum ReadingTheme: String, CaseIterable, Identifiable { |
| 9 | case paper = "Paper", light = "Light", glimmer = "Glimmer" |
| 10 | var id: Self { self } |
| 11 | static let preference = "glimmer.readingTheme" |
| 12 | var scheme: ColorScheme { self == .glimmer ? .dark : .light } |
| 13 | var canvas: Color { |
| 14 | switch self { |
| 15 | case .paper: Color(hex: "F5F2EC") |
| 16 | case .light: Color(hex: "FAFBFC") |
| 17 | case .glimmer: Color(hex: "181918") |
| 18 | } |
| 19 | } |
| 20 | var surface: Color { |
| 21 | switch self { |
| 22 | case .paper: Color(hex: "FFFDF9") |
| 23 | case .light: .white |
| 24 | case .glimmer: Color(hex: "232523") |
| 25 | } |
| 26 | } |
| 27 | var selectionInk: Color { |
| 28 | switch self { |
| 29 | case .paper: Color(hex: "5A4734") |
| 30 | case .light: Color(hex: "465667") |
| 31 | case .glimmer: Color(hex: "E1C6A3") |
| 32 | } |
| 33 | } |
| 34 | var selectionFill: Color { selectionInk.opacity(self == .glimmer ? 0.22 : 0.13) } |
| 35 | var selectionOnInk: Color { self == .glimmer ? Color(hex: "242019") : .white } |
| 36 | var inset: Color { |
| 37 | switch self { |
| 38 | case .paper: Color(hex: "ECE5D9") |
| 39 | case .light: Color(hex: "EDF0F3") |
| 40 | case .glimmer: Color(hex: "30332F") |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | extension EnvironmentValues { |
| 46 | @Entry var readingTheme: ReadingTheme = .paper |
| 47 | } |
| 48 | |
| 49 | struct ReadingAppearance: ViewModifier { |
| 50 | @AppStorage(ReadingTheme.preference) private var selected: ReadingTheme = .paper |
| 51 | func body(content: Content) -> some View { |
| 52 | let theme: ReadingTheme = { |
| 53 | #if DEBUG |
| 54 | if ReviewLaunch.colorScheme == .dark { return .glimmer } |
| 55 | if ReviewLaunch.colorScheme == .light { return .light } |
| 56 | #endif |
| 57 | return selected |
| 58 | }() |
| 59 | return content.buttonStyle(ReadingButtonStyle()).environment(\.readingTheme, theme).preferredColorScheme(theme.scheme) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | enum GlimmerColor { |
| 64 | /// The main action colour: warm ink in light mode, warm paper in dark mode. |
| 65 | static let ink = Color(light: "2b2a28", dark: "ebe7e0") |
| 66 | /// The label colour that sits on `ink`. |
| 67 | static let onInk = Color(light: "fbfaf7", dark: "1c1c1b") |
| 68 | static let accent = Color(light: "4a6f94", dark: "9dbbdc") |
| 69 | static let ember = Color(light: "b15b3e", dark: "e79c80") |
| 70 | static let sage = Color(light: "4b7a59", dark: "94c4a2") |
| 71 | static let honey = Color(light: "8f6a1c", dark: "e2ba5e") |
| 72 | static let dusk = Color(light: "665a92", dark: "b6a9dd") |
| 73 | |
| 74 | // Earlier names, kept so existing call sites read naturally. |
| 75 | static let blue = accent |
| 76 | static let softBlue = Color(light: "5d8fbf", dark: "8fb6df") |
| 77 | static let warm = ember |
| 78 | static let gold = honey |
| 79 | } |
| 80 | |
| 81 | extension Color { |
| 82 | init(hex: String) { |
| 83 | let rgb = Color.rgb(hex) |
| 84 | self.init(red: rgb.r, green: rgb.g, blue: rgb.b) |
| 85 | } |
| 86 | |
| 87 | init(light: String, dark: String) { |
| 88 | let lightRGB = Color.rgb(light), darkRGB = Color.rgb(dark) |
| 89 | #if os(macOS) |
| 90 | self.init(nsColor: NSColor(name: nil) { appearance in |
| 91 | let rgb = appearance.bestMatch(from: [.darkAqua, .aqua]) == .darkAqua ? darkRGB : lightRGB |
| 92 | return NSColor(srgbRed: rgb.r, green: rgb.g, blue: rgb.b, alpha: 1) |
| 93 | }) |
| 94 | #else |
| 95 | self.init(uiColor: UIColor { traits in |
| 96 | let rgb = traits.userInterfaceStyle == .dark ? darkRGB : lightRGB |
| 97 | return UIColor(red: rgb.r, green: rgb.g, blue: rgb.b, alpha: 1) |
| 98 | }) |
| 99 | #endif |
| 100 | } |
| 101 | |
| 102 | private static func rgb(_ hex: String) -> (r: Double, g: Double, b: Double) { |
| 103 | let value = UInt32(hex.trimmingCharacters(in: CharacterSet(charactersIn: "#")), radix: 16) ?? 0 |
| 104 | return (Double((value >> 16) & 255) / 255, Double((value >> 8) & 255) / 255, Double(value & 255) / 255) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | enum GlimmerFont { |
| 109 | /// Interface headings use SF, like the rest of the system. |
| 110 | static func display(_ style: Font.TextStyle, weight: Font.Weight = .semibold) -> Font { |
| 111 | .system(style, design: .default, weight: weight == .bold ? .semibold : weight) |
| 112 | } |
| 113 | |
| 114 | /// New York is kept for the journal's own voice: lesson text and the line of the day. |
| 115 | static func reading(_ style: Font.TextStyle, weight: Font.Weight = .regular) -> Font { |
| 116 | .system(style, design: .serif, weight: weight) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // MARK: - Page and surfaces |
| 121 | |
| 122 | struct Page<Content: View>: View { |
| 123 | let title: String |
| 124 | var kicker: String? = nil |
| 125 | var subtitle: String? = nil |
| 126 | var width: CGFloat = 760 |
| 127 | @ViewBuilder let content: Content |
| 128 | |
| 129 | var body: some View { |
| 130 | ScrollView { |
| 131 | VStack(alignment: .leading, spacing: 16) { |
| 132 | VStack(alignment: .leading, spacing: 4) { |
| 133 | if let kicker { |
| 134 | Text(kicker).font(.subheadline.weight(.medium)).foregroundStyle(.secondary) |
| 135 | } |
| 136 | Text(title).font(Page.titleFont) |
| 137 | .fixedSize(horizontal: false, vertical: true) |
| 138 | .accessibilityAddTraits(.isHeader) |
| 139 | if let subtitle { |
| 140 | Text(subtitle).font(.body).foregroundStyle(.secondary) |
| 141 | .fixedSize(horizontal: false, vertical: true) |
| 142 | } |
| 143 | } |
| 144 | .padding(.bottom, 6) |
| 145 | content |
| 146 | } |
| 147 | .frame(maxWidth: width, alignment: .leading) |
| 148 | .padding(.horizontal, 20).padding(.top, 16).padding(.bottom, 32) |
| 149 | .frame(maxWidth: .infinity) |
| 150 | } |
| 151 | .scrollDismissesKeyboard(.interactively) |
| 152 | .background { PageBackdrop() } |
| 153 | .tint(GlimmerColor.accent) |
| 154 | } |
| 155 | |
| 156 | private static var titleFont: Font { |
| 157 | #if os(macOS) |
| 158 | .system(.title, weight: .semibold) |
| 159 | #else |
| 160 | .system(.largeTitle, weight: .bold) |
| 161 | #endif |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /// The flat, opaque backdrop behind every page. |
| 166 | struct PageBackdrop: View { |
| 167 | @Environment(\.readingTheme) private var theme |
| 168 | var body: some View { theme.canvas.ignoresSafeArea() } |
| 169 | } |
| 170 | |
| 171 | struct Eyebrow: View { |
| 172 | let title: String |
| 173 | var color: Color = GlimmerColor.accent |
| 174 | init(_ title: String, color: Color = GlimmerColor.accent) { |
| 175 | self.title = title |
| 176 | self.color = color |
| 177 | } |
| 178 | |
| 179 | var body: some View { |
| 180 | // A quiet section heading. The colour argument is kept for call sites but headings stay neutral. |
| 181 | Text(title).font(.subheadline.weight(.semibold)).foregroundStyle(.secondary) |
| 182 | .accessibilityAddTraits(.isHeader) |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /// An opaque reading surface. Panels never use glass, so controls placed on them stay legible. |
| 187 | struct Panel<Content: View>: View { |
| 188 | @Environment(\.readingTheme) private var theme |
| 189 | var title: String? = nil |
| 190 | var accent: Color = GlimmerColor.accent |
| 191 | var padding: CGFloat = 16 |
| 192 | /// A plain panel has no surface. Use it for groups of glass rows, which sit directly on the page. |
| 193 | var plain = false |
| 194 | @ViewBuilder let content: Content |
| 195 | @Environment(\.colorSchemeContrast) private var contrast |
| 196 | |
| 197 | var body: some View { |
| 198 | VStack(alignment: .leading, spacing: 12) { |
| 199 | if let title { Eyebrow(title, color: accent) } |
| 200 | content |
| 201 | } |
| 202 | .frame(maxWidth: .infinity, alignment: .leading) |
| 203 | .padding(plain ? 0 : padding) |
| 204 | .background { |
| 205 | if !plain { |
| 206 | RoundedRectangle(cornerRadius: 16, style: .continuous).fill(theme.surface) |
| 207 | .overlay { |
| 208 | RoundedRectangle(cornerRadius: 16, style: .continuous) |
| 209 | .strokeBorder(.primary.opacity(contrast == .increased ? 0.30 : 0.08), lineWidth: 1) |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | .padding(.top, plain ? 6 : 0) |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /// Lays children out in rows that wrap, for chips and tags. |
| 218 | struct FlowLayout: Layout { |
| 219 | var spacing: CGFloat = 8 |
| 220 | var centerItemsVertically = false |
| 221 | |
| 222 | func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize { |
| 223 | let rows = arrange(subviews, width: proposal.width ?? .infinity) |
| 224 | let height = rows.map(\.height).reduce(0, +) + spacing * CGFloat(max(0, rows.count - 1)) |
| 225 | return CGSize(width: proposal.width ?? rows.map(\.width).max() ?? 0, height: height) |
| 226 | } |
| 227 | |
| 228 | func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) { |
| 229 | var y = bounds.minY |
| 230 | for row in arrange(subviews, width: bounds.width) { |
| 231 | var x = bounds.minX |
| 232 | for index in row.indices { |
| 233 | let size = subviews[index].sizeThatFits(.unspecified) |
| 234 | let offset = centerItemsVertically ? (row.height - size.height) / 2 : 0 |
| 235 | subviews[index].place(at: CGPoint(x: x, y: y + offset), proposal: ProposedViewSize(size)) |
| 236 | x += size.width + spacing |
| 237 | } |
| 238 | y += row.height + spacing |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | private func arrange(_ subviews: Subviews, width: CGFloat) -> [(indices: [Int], width: CGFloat, height: CGFloat)] { |
| 243 | var rows: [(indices: [Int], width: CGFloat, height: CGFloat)] = [] |
| 244 | var current: (indices: [Int], width: CGFloat, height: CGFloat) = ([], 0, 0) |
| 245 | for index in subviews.indices { |
| 246 | let size = subviews[index].sizeThatFits(.unspecified) |
| 247 | let needed = current.indices.isEmpty ? size.width : current.width + spacing + size.width |
| 248 | if needed > width, !current.indices.isEmpty { |
| 249 | rows.append(current) |
| 250 | current = ([index], size.width, size.height) |
| 251 | } else { |
| 252 | current = (current.indices + [index], needed, max(current.height, size.height)) |
| 253 | } |
| 254 | } |
| 255 | if !current.indices.isEmpty { rows.append(current) } |
| 256 | return rows |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | // MARK: - Liquid Glass |
| 261 | |
| 262 | enum GlimmerButtonRole { case prominent, standard, selected } |
| 263 | |
| 264 | extension View { |
| 265 | func glimmerGlass<S: Shape>(_ shape: S, tint: Color? = nil, interactive: Bool = false) -> some View { |
| 266 | self.modifier(GlassFallback(shape: shape, tint: tint)) |
| 267 | } |
| 268 | |
| 269 | func glassGroup(spacing: CGFloat = 12) -> some View { self } |
| 270 | |
| 271 | func glimmerButton(_ role: GlimmerButtonRole = .standard, tint: Color? = nil, circle: Bool = false) -> some View { |
| 272 | buttonStyle(ReadingButtonStyle(role: role, tint: tint, circle: circle)) |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | struct ReadingButtonStyle: ButtonStyle { |
| 277 | var role: GlimmerButtonRole = .standard |
| 278 | var tint: Color? |
| 279 | var circle = false |
| 280 | @Environment(\.readingTheme) private var theme |
| 281 | @Environment(\.isEnabled) private var enabled |
| 282 | @Environment(\.controlSize) private var size |
| 283 | @Environment(\.accessibilityReduceMotion) private var reducedMotion |
| 284 | @Environment(\.colorSchemeContrast) private var contrast |
| 285 | |
| 286 | func makeBody(configuration: Configuration) -> some View { |
| 287 | let dark = theme == .glimmer |
| 288 | let primary = role == .prominent |
| 289 | let selected = role == .selected |
| 290 | let accent = tint ?? Color(hex: dark ? "8FB8E8" : "2F4A63") |
| 291 | // The main action is ink: near-black on light pages, warm white on dark ones. Blue stays for selection. |
| 292 | let primaryFill = tint ?? Color(hex: dark ? "EBE7E0" : theme == .paper ? "2B2A26" : "1F2328") |
| 293 | let ink = Color(hex: dark ? "F2F2F5" : theme == .paper ? "2B2A26" : "1F2328") |
| 294 | let surface = Color(hex: dark ? "2A2A2E" : theme == .paper ? "FBF8F1" : "FFFFFF") |
| 295 | let outline = Color(hex: dark ? "7C7C85" : theme == .paper ? "8A8272" : "8A8F98") |
| 296 | let shape = circle ? AnyShape(Circle()) : AnyShape(Capsule()) |
| 297 | #if os(macOS) |
| 298 | let v: CGFloat = size == .extraLarge ? 12 : size == .large ? 9 : 6 |
| 299 | #else |
| 300 | let v: CGFloat = size == .large || size == .extraLarge ? 13 : 9 |
| 301 | #endif |
| 302 | return configuration.label |
| 303 | .font(.callout.weight(.semibold)) |
| 304 | .foregroundStyle(!enabled ? Color.secondary : configuration.role == .destructive ? Color.red : primary ? Color(hex: dark ? "1C1C1B" : "FBFAF7") : ink) |
| 305 | .padding(.vertical, v).padding(.horizontal, circle ? v : v + 7) |
| 306 | .background { |
| 307 | shape.fill(!enabled ? theme.inset : primary ? primaryFill : surface) |
| 308 | if enabled && selected { shape.fill(accent.opacity(0.17)) } |
| 309 | if enabled { shape.stroke(primary ? primaryFill : selected ? accent : outline, lineWidth: contrast == .increased || selected ? 2 : 1) } |
| 310 | if configuration.isPressed && enabled { shape.fill(primary ? Color(hex: dark ? "1C1C1B" : "FBFAF7").opacity(0.16) : ink.opacity(0.12)) } |
| 311 | } |
| 312 | .contentShape(shape) |
| 313 | .scaleEffect(configuration.isPressed && !reducedMotion ? 0.97 : 1) |
| 314 | .animation(reducedMotion ? nil : .smooth(duration: 0.12), value: configuration.isPressed) |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | private struct GlassFallback<S: Shape>: ViewModifier { |
| 319 | @Environment(\.readingTheme) private var theme |
| 320 | let shape: S |
| 321 | let tint: Color? |
| 322 | func body(content: Content) -> some View { |
| 323 | content.background { shape.fill(theme.inset) } |
| 324 | .background { shape.fill(theme.surface) } |
| 325 | .overlay { shape.stroke(tint ?? Color.primary.opacity(0.12), lineWidth: tint == nil ? 1 : 2) } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | // MARK: - Controls |
| 330 | |
| 331 | /// The shared content of a glass row: an icon, a title, an optional detail and a trailing hint. |
| 332 | struct RowLabel: View { |
| 333 | let title: String |
| 334 | var detail: String? = nil |
| 335 | var icon: String? = nil |
| 336 | var emoji: String? = nil |
| 337 | var tint: Color = GlimmerColor.accent |
| 338 | var trailing: String? = "chevron.right" |
| 339 | var selected = false |
| 340 | /// When set, the row ends in an animated tick instead of a symbol. |
| 341 | var tick: Bool? = nil |
| 342 | var onTint: Color = Color(light: "ffffff", dark: "1c1c1b") |
| 343 | |
| 344 | static var minHeight: CGFloat { |
| 345 | #if os(macOS) |
| 346 | 30 |
| 347 | #else |
| 348 | 44 |
| 349 | #endif |
| 350 | } |
| 351 | |
| 352 | var body: some View { |
| 353 | HStack(spacing: 10) { |
| 354 | // Rows whose title starts with an emoji (older catalogs, lesson choices) show its glyph instead. |
| 355 | if let symbol = icon ?? emoji ?? Glyph.leadingEmoji(title) { |
| 356 | GlyphTile(Glyph.symbol(icon: symbol, name: title), tint: tint) |
| 357 | } |
| 358 | VStack(alignment: .leading, spacing: 1) { |
| 359 | Text(Glyph.plainName(title)).font(.body.weight(.medium)).foregroundStyle(Color.primary) |
| 360 | .multilineTextAlignment(.leading) |
| 361 | if let detail { |
| 362 | Text(detail).font(.subheadline).foregroundStyle(Color.secondary) |
| 363 | .multilineTextAlignment(.leading) |
| 364 | } |
| 365 | } |
| 366 | Spacer(minLength: 4) |
| 367 | if let tick { |
| 368 | Tick(done: tick, size: 24, tint: tint, onTint: onTint) |
| 369 | } else if let trailing { |
| 370 | Image(systemName: trailing).font(.footnote.weight(.semibold)) |
| 371 | .foregroundStyle(selected ? AnyShapeStyle(tint) : AnyShapeStyle(.tertiary)) |
| 372 | .accessibilityHidden(true) |
| 373 | } |
| 374 | } |
| 375 | .padding(.vertical, detail == nil ? 0 : 5).padding(.horizontal, 6) |
| 376 | .frame(maxWidth: .infinity, minHeight: RowLabel.minHeight, alignment: .leading) |
| 377 | .contentShape(.rect) |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | extension View { |
| 382 | /// The glass treatment shared by every full-width row button and navigation link. |
| 383 | /// A row that ends in a tick says "selected" with the tick alone, so pass `fill: false` for it. |
| 384 | func glassRow(tint: Color = GlimmerColor.accent, selected: Bool = false, fill: Bool = true) -> some View { |
| 385 | self.buttonStyle(RowPressStyle()) |
| 386 | .background(selected && fill ? tint.opacity(0.10) : Color.clear, in: .rect(cornerRadius: 10, style: .continuous)) |
| 387 | .overlay(alignment: .bottom) { Divider().opacity(0.6).accessibilityHidden(true) } |
| 388 | .accessibilityElement(children: .combine) |
| 389 | .accessibilityAddTraits(selected ? .isSelected : []) |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | /// A full-width glass row button. |
| 394 | struct ActionRow: View { |
| 395 | let title: String |
| 396 | var detail: String? = nil |
| 397 | var icon: String? = nil |
| 398 | var emoji: String? = nil |
| 399 | var tint: Color = GlimmerColor.accent |
| 400 | var trailing: String = "chevron.right" |
| 401 | var selected = false |
| 402 | /// When set, the row ends in an animated tick and gives a tap you can feel. |
| 403 | var tick: Bool? = nil |
| 404 | var onTint: Color = Color(light: "ffffff", dark: "1c1c1b") |
| 405 | var action: () -> Void |
| 406 | |
| 407 | var body: some View { |
| 408 | Button(action: action) { |
| 409 | RowLabel(title: title, detail: detail, icon: icon, emoji: emoji, tint: tint, |
| 410 | trailing: trailing, selected: selected, tick: tick, onTint: onTint) |
| 411 | } |
| 412 | .glassRow(tint: tint, selected: selected, fill: tick == nil) |
| 413 | .sensoryFeedback(.selection, trigger: tick) { _, _ in tick != nil } |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | /// A full-width glass row that pushes a destination. |
| 418 | struct LinkRow<Destination: View>: View { |
| 419 | let title: String |
| 420 | var detail: String? = nil |
| 421 | var icon: String? = nil |
| 422 | var emoji: String? = nil |
| 423 | var tint: Color = GlimmerColor.accent |
| 424 | @ViewBuilder let destination: Destination |
| 425 | |
| 426 | var body: some View { |
| 427 | NavigationLink(destination: destination) { |
| 428 | RowLabel(title: title, detail: detail, icon: icon, emoji: emoji, tint: tint) |
| 429 | } |
| 430 | .glassRow(tint: tint) |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /// A single or multiple choice, shown as a full-width glass row. |
| 435 | struct SelectRow: View { |
| 436 | @Environment(\.readingTheme) private var theme |
| 437 | let label: String |
| 438 | let selected: Bool |
| 439 | var tint: Color? = nil |
| 440 | var action: () -> Void |
| 441 | |
| 442 | var body: some View { |
| 443 | ActionRow(title: label, tint: tint ?? theme.selectionInk, selected: selected, tick: selected, |
| 444 | onTint: tint == nil ? theme.selectionOnInk : Color(light: "ffffff", dark: "1c1c1b"), action: action) |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | /// A compact glass capsule for tags, moods and short choices. |
| 449 | struct Chip: View { |
| 450 | let label: String |
| 451 | let selected: Bool |
| 452 | var tint: Color = GlimmerColor.accent |
| 453 | var action: () -> Void |
| 454 | |
| 455 | private static var verticalPadding: CGFloat { |
| 456 | #if os(macOS) |
| 457 | 5 |
| 458 | #else |
| 459 | 9 |
| 460 | #endif |
| 461 | } |
| 462 | |
| 463 | var body: some View { |
| 464 | Button(action: action) { |
| 465 | HStack(spacing: 6) { |
| 466 | if selected { Image(systemName: "checkmark").font(.caption.weight(.bold)).accessibilityHidden(true) } |
| 467 | Text(label).font(.callout) |
| 468 | } |
| 469 | .foregroundStyle(Color.primary) |
| 470 | .frame(minWidth: 24, minHeight: 20) |
| 471 | } |
| 472 | .accessibilityLabel(label) |
| 473 | .buttonStyle(.plain) |
| 474 | .padding(.horizontal, 12).padding(.vertical, Chip.verticalPadding) |
| 475 | .contentShape(.capsule) |
| 476 | .glimmerGlass(.capsule, tint: selected ? tint.opacity(0.20) : nil, interactive: true) |
| 477 | .accessibilityAddTraits(selected ? .isSelected : []) |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | struct PrimaryAction: View { |
| 482 | let title: String |
| 483 | var systemImage: String? = nil |
| 484 | var action: () -> Void |
| 485 | |
| 486 | // A thumb-width button on iPhone and iPad; a normal trailing button on Mac. |
| 487 | private static var minWidth: CGFloat? { |
| 488 | #if os(macOS) |
| 489 | 96 |
| 490 | #else |
| 491 | nil |
| 492 | #endif |
| 493 | } |
| 494 | var body: some View { |
| 495 | Button(action: action) { |
| 496 | HStack(spacing: 6) { |
| 497 | if let systemImage { Image(systemName: systemImage) } |
| 498 | Text(title).fontWeight(.semibold) |
| 499 | } |
| 500 | .foregroundStyle(GlimmerColor.onInk) |
| 501 | .frame(minWidth: PrimaryAction.minWidth, minHeight: 20) |
| 502 | } |
| 503 | .glimmerButton(.prominent) |
| 504 | .buttonBorderShape(.capsule) |
| 505 | .controlSize(.large) |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | /// A secondary glass button that sizes to its label. |
| 510 | struct SecondaryAction: View { |
| 511 | let title: String |
| 512 | var systemImage: String? = nil |
| 513 | var role: ButtonRole? = nil |
| 514 | var fill = false |
| 515 | var action: () -> Void |
| 516 | |
| 517 | var body: some View { |
| 518 | Button(role: role, action: action) { |
| 519 | HStack(spacing: 8) { |
| 520 | if let systemImage { Image(systemName: systemImage) } |
| 521 | Text(title).fontWeight(.medium) |
| 522 | } |
| 523 | .foregroundStyle(role == .destructive ? Color.red : Color.primary) |
| 524 | .frame(maxWidth: fill ? .infinity : nil, minHeight: 24) |
| 525 | } |
| 526 | .glimmerButton(.standard) |
| 527 | .buttonBorderShape(.capsule) |
| 528 | .controlSize(.large) |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | /// A floating glass action bar pinned to the bottom of a flow, within easy reach of a thumb. |
| 533 | struct ActionBar<Content: View>: View { |
| 534 | @Environment(\.readingTheme) private var theme |
| 535 | var width: CGFloat = 560 |
| 536 | @ViewBuilder let content: Content |
| 537 | |
| 538 | var body: some View { |
| 539 | HStack(spacing: 10) { |
| 540 | #if os(macOS) |
| 541 | Spacer(minLength: 0) |
| 542 | #endif |
| 543 | content |
| 544 | } |
| 545 | .glassGroup(spacing: 10) |
| 546 | .frame(maxWidth: width) |
| 547 | .padding(.horizontal, 20).padding(.top, 8).padding(.bottom, 12) |
| 548 | .frame(maxWidth: .infinity) |
| 549 | .background(theme.canvas) |
| 550 | .overlay(alignment: .top) { Divider() } |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | extension View { |
| 555 | /// Pins glass actions to the bottom edge. On 26+ the bar gets the system scroll edge effect. |
| 556 | @ViewBuilder |
| 557 | func actionBar<Content: View>(width: CGFloat = 560, @ViewBuilder _ content: () -> Content) -> some View { |
| 558 | safeAreaInset(edge: .bottom, spacing: 0) { ActionBar(width: width, content: content) } |
| 559 | } |
| 560 | |
| 561 | /// Adds Cancel where each platform expects it: the navigation bar on iPhone and iPad. |
| 562 | /// On Mac, sheets place a Cancel button in their action bar instead (see `SheetCancel`). |
| 563 | func sheetCancel(_ action: @escaping () -> Void) -> some View { |
| 564 | #if os(iOS) |
| 565 | toolbar { |
| 566 | ToolbarItem(placement: .cancellationAction) { |
| 567 | Button(role: .cancel, action: action) { |
| 568 | Label("Cancel", systemImage: "xmark").labelStyle(.iconOnly) |
| 569 | } |
| 570 | .tint(Color.primary) |
| 571 | } |
| 572 | } |
| 573 | #else |
| 574 | self |
| 575 | #endif |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | /// The Mac sheet's Cancel button, shown at the leading edge of the action bar. |
| 580 | struct SheetCancel: View { |
| 581 | var title = "Cancel" |
| 582 | var action: () -> Void |
| 583 | |
| 584 | var body: some View { |
| 585 | #if os(macOS) |
| 586 | Button(title, role: .cancel, action: action) |
| 587 | .glimmerButton(.standard).buttonBorderShape(.capsule).controlSize(.large) |
| 588 | .keyboardShortcut(.cancelAction) |
| 589 | #else |
| 590 | EmptyView() |
| 591 | #endif |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | /// A large, inviting text field: generous padding, a soft sunk surface and a clear focus ring. |
| 596 | private struct GlimmerField: ViewModifier { |
| 597 | /// Fields and the buttons beside them share this height. |
| 598 | static var height: CGFloat { |
| 599 | #if os(macOS) |
| 600 | 40 |
| 601 | #else |
| 602 | 50 |
| 603 | #endif |
| 604 | } |
| 605 | |
| 606 | var symbol: String? = nil |
| 607 | @FocusState private var focused: Bool |
| 608 | @Environment(\.colorSchemeContrast) private var contrast |
| 609 | |
| 610 | func body(content: Content) -> some View { |
| 611 | HStack(spacing: 10) { |
| 612 | if let symbol { |
| 613 | Image(systemName: symbol).foregroundStyle(.secondary).accessibilityHidden(true) |
| 614 | } |
| 615 | content |
| 616 | .textFieldStyle(.plain) |
| 617 | .font(.title3) |
| 618 | .focused($focused) |
| 619 | } |
| 620 | .padding(.horizontal, 20) |
| 621 | .frame(height: GlimmerField.height) |
| 622 | .background(Color.primary.opacity(0.06), in: .capsule) |
| 623 | .overlay { |
| 624 | Capsule() |
| 625 | .strokeBorder(focused ? GlimmerColor.accent : Color.primary.opacity(contrast == .increased ? 0.35 : 0.10), |
| 626 | lineWidth: focused ? 2 : 1) |
| 627 | } |
| 628 | .contentShape(.capsule) |
| 629 | .onTapGesture { focused = true } |
| 630 | .animation(.easeOut(duration: 0.15), value: focused) |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | extension View { |
| 635 | /// Styles a `TextField` as Glimmer's large input. |
| 636 | func glimmerField(symbol: String? = nil) -> some View { modifier(GlimmerField(symbol: symbol)) } |
| 637 | |
| 638 | /// Sizes a glass button to sit beside a `glimmerField`, at the same height. |
| 639 | func besideField() -> some View { controlSize(.extraLarge) } |
| 640 | } |
| 641 | |
| 642 | /// A searchable list for picking one item from many, with Favorites and Recent at the top. |
| 643 | /// Use it wherever a list is too long to scan: rows can be starred, and recent picks float up. |
| 644 | struct ChoiceList: View { |
| 645 | @Environment(\.readingTheme) private var theme |
| 646 | let items: [String] |
| 647 | let selection: String? |
| 648 | var visible = true |
| 649 | var favorites: [String] = [] |
| 650 | var recents: [String] = [] |
| 651 | /// What the search field asks for, such as "Search apps and sites". |
| 652 | var prompt = "Search" |
| 653 | /// Sorts the full list alphabetically. Leave off for lists with a deliberate order. |
| 654 | var alphabetical = false |
| 655 | /// The kind of thing listed, such as "app", "activity" or "person". It picks each row's glyph and tint. |
| 656 | var kind: String? = nil |
| 657 | var onSelect: (String) -> Void |
| 658 | var onToggleFavorite: (String) -> Void |
| 659 | /// When set, a search with no exact match offers to add what was typed. |
| 660 | var onAdd: ((String) -> Void)? = nil |
| 661 | @State private var query = "" |
| 662 | |
| 663 | private var term: String { query.trimmingCharacters(in: .whitespacesAndNewlines) } |
| 664 | private var everything: [String] { |
| 665 | alphabetical ? items.sorted { $0.localizedCaseInsensitiveCompare($1) == .orderedAscending } : items |
| 666 | } |
| 667 | private var matches: [String] { everything.filter { $0.localizedCaseInsensitiveContains(term) } } |
| 668 | private var starred: [String] { favorites.filter(items.contains) } |
| 669 | private var recent: [String] { Array(recents.filter { items.contains($0) && !favorites.contains($0) }.prefix(4)) } |
| 670 | private var canAdd: Bool { |
| 671 | onAdd != nil && !term.isEmpty && !items.contains { $0.caseInsensitiveCompare(term) == .orderedSame } |
| 672 | } |
| 673 | |
| 674 | var body: some View { |
| 675 | VStack(alignment: .leading, spacing: 12) { |
| 676 | if items.count > 8 || onAdd != nil { |
| 677 | TextField(prompt, text: $query).glimmerField(symbol: "magnifyingglass") |
| 678 | .autocorrectionDisabled() |
| 679 | .onSubmit { if canAdd, matches.isEmpty { add() } else if let first = matches.first, !term.isEmpty { onSelect(first) } } |
| 680 | } |
| 681 | if term.isEmpty { |
| 682 | section("Favorites", starred) |
| 683 | section("Recent", recent) |
| 684 | section(starred.isEmpty && recent.isEmpty ? nil : "All", everything) |
| 685 | } else { |
| 686 | section(nil, matches) |
| 687 | if canAdd, matches.isEmpty { |
| 688 | Button(action: add) { |
| 689 | Label("Add “\(term)”", systemImage: "plus.circle").font(.body.weight(.medium)) |
| 690 | .foregroundStyle(Color.primary) |
| 691 | .frame(maxWidth: .infinity, minHeight: RowLabel.minHeight, alignment: .leading) |
| 692 | .contentShape(.rect) |
| 693 | } |
| 694 | .glassRow() |
| 695 | } else if matches.isEmpty { |
| 696 | Text("Nothing matches “\(term)”.").font(.subheadline).foregroundStyle(.secondary) |
| 697 | } |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | private func add() { |
| 703 | onAdd?(term) |
| 704 | query = "" |
| 705 | } |
| 706 | |
| 707 | @ViewBuilder |
| 708 | private func section(_ title: String?, _ names: [String]) -> some View { |
| 709 | if !names.isEmpty { |
| 710 | VStack(alignment: .leading, spacing: 6) { |
| 711 | if let title { |
| 712 | Text(title).font(.footnote.weight(.semibold)).foregroundStyle(.secondary).padding(.leading, 4) |
| 713 | .accessibilityAddTraits(.isHeader) |
| 714 | } |
| 715 | VStack(spacing: 0) { |
| 716 | ForEach(Array(names.enumerated()), id: \.element) { index, name in |
| 717 | row(name) |
| 718 | .modifier(DisclosureRowMotion(index: index, count: names.count, visible: visible)) |
| 719 | if index < names.count - 1 { Divider().padding(.leading, 46) } |
| 720 | } |
| 721 | } |
| 722 | .background(theme.surface, in: .rect(cornerRadius: 14, style: .continuous)) |
| 723 | .overlay { |
| 724 | RoundedRectangle(cornerRadius: 14, style: .continuous).strokeBorder(.primary.opacity(0.08), lineWidth: 1) |
| 725 | } |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | private func row(_ name: String) -> some View { |
| 731 | let isFavorite = favorites.contains(name) |
| 732 | return HStack(spacing: 0) { |
| 733 | Button { onSelect(name) } label: { |
| 734 | HStack(spacing: 12) { |
| 735 | GlyphTile(Glyph.symbol(name: name, category: kind), tint: Glyph.tint(for: kind)) |
| 736 | Text(Glyph.plainName(name)).font(.body).foregroundStyle(Color.primary) |
| 737 | Spacer(minLength: 4) |
| 738 | if selection == name { |
| 739 | Image(systemName: "checkmark").font(.body.weight(.semibold)).foregroundStyle(theme.selectionInk) |
| 740 | } |
| 741 | } |
| 742 | .padding(.leading, 14) |
| 743 | .frame(maxWidth: .infinity, minHeight: RowLabel.minHeight + 16, alignment: .leading) |
| 744 | .contentShape(.rect) |
| 745 | } |
| 746 | .buttonStyle(.plain) |
| 747 | .accessibilityLabel(name) |
| 748 | .accessibilityAddTraits(selection == name ? .isSelected : []) |
| 749 | Button { onToggleFavorite(name) } label: { |
| 750 | Image(systemName: isFavorite ? "star.fill" : "star") |
| 751 | .foregroundStyle(isFavorite ? AnyShapeStyle(GlimmerColor.honey) : AnyShapeStyle(.tertiary)) |
| 752 | .frame(width: 44, height: RowLabel.minHeight + 16) |
| 753 | .contentShape(.rect) |
| 754 | } |
| 755 | .buttonStyle(.plain) |
| 756 | .accessibilityLabel(isFavorite ? "Remove \(name) from favorites" : "Add \(name) to favorites") |
| 757 | } |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | /// One option in a `GlassSegments` control. |
| 762 | struct GlassSegment<Value: Hashable>: Identifiable { |
| 763 | let value: Value |
| 764 | let title: String |
| 765 | var symbol: String? = nil |
| 766 | var id: Value { value } |
| 767 | } |
| 768 | |
| 769 | /// A segmented choice drawn in Liquid Glass: a quiet track with a glass thumb that slides to the selection. |
| 770 | struct GlassSegments<Value: Hashable>: View { |
| 771 | let label: String |
| 772 | let options: [GlassSegment<Value>] |
| 773 | @Binding var selection: Value |
| 774 | @Namespace private var thumb |
| 775 | @Environment(\.accessibilityReduceMotion) private var reduceMotion |
| 776 | |
| 777 | private var height: CGFloat { |
| 778 | #if os(macOS) |
| 779 | 30 |
| 780 | #else |
| 781 | 42 |
| 782 | #endif |
| 783 | } |
| 784 | |
| 785 | var body: some View { |
| 786 | HStack(spacing: 2) { |
| 787 | ForEach(options) { option in |
| 788 | let selected = option.value == selection |
| 789 | Button { |
| 790 | withAnimation(reduceMotion ? nil : .spring(duration: 0.35, bounce: 0.12)) { selection = option.value } |
| 791 | } label: { |
| 792 | HStack(spacing: 6) { |
| 793 | if let symbol = option.symbol { Image(systemName: symbol).font(.footnote.weight(.semibold)) } |
| 794 | Text(option.title).font(.callout.weight(selected ? .semibold : .regular)).lineLimit(1) |
| 795 | } |
| 796 | .foregroundStyle(selected ? Color.primary : Color.secondary) |
| 797 | .padding(.horizontal, 10) |
| 798 | .frame(maxWidth: .infinity, minHeight: height) |
| 799 | .contentShape(.capsule) |
| 800 | .modifier(SegmentThumb(selected: selected, namespace: thumb)) |
| 801 | } |
| 802 | .buttonStyle(.plain) |
| 803 | .accessibilityAddTraits(selected ? .isSelected : []) |
| 804 | } |
| 805 | } |
| 806 | .padding(4) |
| 807 | .background(Color.primary.opacity(0.07), in: .capsule) |
| 808 | .glassGroup(spacing: 0) |
| 809 | .sensoryFeedback(.selection, trigger: selection) |
| 810 | .accessibilityElement(children: .contain) |
| 811 | .accessibilityLabel(label) |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | /// The glass thumb behind the selected segment. On 26+ it morphs between segments as one piece of glass. |
| 816 | private struct SegmentThumb: ViewModifier { |
| 817 | let selected: Bool |
| 818 | let namespace: Namespace.ID |
| 819 | |
| 820 | func body(content: Content) -> some View { |
| 821 | if !selected { |
| 822 | content |
| 823 | } else { |
| 824 | content.background { |
| 825 | if #available(macOS 26, iOS 26, *) { |
| 826 | Capsule().fill(.clear) |
| 827 | .glassEffect(.regular.tint(GlimmerColor.accent.opacity(0.22)), in: .capsule) |
| 828 | .matchedGeometryEffect(id: "thumb", in: namespace) |
| 829 | } else { |
| 830 | Capsule().fill(GlimmerColor.accent.opacity(0.10)) |
| 831 | .overlay { Capsule().fill(GlimmerColor.accent.opacity(0.25)) } |
| 832 | .matchedGeometryEffect(id: "thumb", in: namespace) |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | /// A round glass icon button for back, close and stepping. |
| 840 | struct GlassIconButton: View { |
| 841 | let label: String |
| 842 | let systemImage: String |
| 843 | var action: () -> Void |
| 844 | |
| 845 | var body: some View { |
| 846 | Button(action: action) { |
| 847 | Image(systemName: systemImage).font(.body.weight(.medium)).frame(width: 20, height: 24) |
| 848 | } |
| 849 | .glimmerButton(.standard, circle: true) |
| 850 | .accessibilityLabel(label) |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | /// Step progress as a row of capsules. |
| 855 | struct StepDots: View { |
| 856 | let current: Int |
| 857 | let total: Int |
| 858 | /// Progress colour; plain ink by default. |
| 859 | var tint: Color? = nil |
| 860 | |
| 861 | var body: some View { |
| 862 | HStack(spacing: 4) { |
| 863 | ForEach(0..<total, id: \.self) { index in |
| 864 | Capsule().fill(index <= current ? (tint ?? Color.primary.opacity(0.55)) : Color.primary.opacity(0.12)) |
| 865 | .frame(height: 3) |
| 866 | } |
| 867 | } |
| 868 | .accessibilityElement() |
| 869 | .accessibilityLabel("Step \(max(1, current + 1)) of \(total)") |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | /// A slim progress bar drawn on content surfaces. |
| 874 | struct Meter: View { |
| 875 | @Environment(\.accessibilityReduceMotion) private var reduceMotion |
| 876 | let value: Double |
| 877 | var tint: Color = GlimmerColor.accent |
| 878 | |
| 879 | var body: some View { |
| 880 | let clamped = min(1, max(0, value.isFinite ? value : 0)) |
| 881 | GeometryReader { proxy in |
| 882 | ZStack(alignment: .leading) { |
| 883 | Capsule().fill(Color.primary.opacity(0.08)) |
| 884 | if clamped > 0 { Capsule().fill(tint).frame(width: max(8, proxy.size.width * clamped)) } |
| 885 | } |
| 886 | } |
| 887 | .animation(reduceMotion ? nil : .smooth(duration: 0.58), value: clamped) |
| 888 | .frame(height: 6) |
| 889 | .accessibilityElement() |
| 890 | .accessibilityValue("\(Int((clamped * 100).rounded())) percent") |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | /// The Glimmer logo: the AppMark asset, which is brand/glimmer-icon-1024.png masked to a circle. |
| 895 | /// Never draw the mark in code; regenerate the asset with brand/render-icons.cjs. |
| 896 | struct GlimmerLogo: View { |
| 897 | var size: CGFloat = 64 |
| 898 | |
| 899 | var body: some View { |
| 900 | Image("AppMark") |
| 901 | .resizable() |
| 902 | .interpolation(.high) |
| 903 | .frame(width: size, height: size) |
| 904 | .accessibilityHidden(true) |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | struct CoinMark: View { |
| 909 | var size: CGFloat = 14 |
| 910 | |
| 911 | var body: some View { |
| 912 | ZStack { |
| 913 | Circle().strokeBorder(lineWidth: size * 0.09) |
| 914 | Circle().inset(by: size * 0.24).strokeBorder(lineWidth: size * 0.06) |
| 915 | } |
| 916 | .foregroundStyle(GlimmerColor.honey) |
| 917 | .frame(width: size, height: size) |
| 918 | .accessibilityHidden(true) |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | struct CoinCount: View { |
| 923 | let count: Int |
| 924 | @Environment(\.accessibilityReduceMotion) private var reduceMotion |
| 925 | |
| 926 | var body: some View { |
| 927 | HStack(spacing: 5) { |
| 928 | CoinMark() |
| 929 | Text(count.formatted()) |
| 930 | .font(.subheadline.monospacedDigit().weight(.medium)) |
| 931 | .foregroundStyle(.secondary) |
| 932 | .contentTransition(reduceMotion ? .opacity : .numericText(value: Double(count))) |
| 933 | .animation(.easeOut(duration: reduceMotion ? 0.18 : 0.32), value: count) |
| 934 | } |
| 935 | .accessibilityElement(children: .ignore) |
| 936 | .accessibilityLabel("\(count) coins") |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | /// A small labelled number for summaries. |
| 941 | struct Stat: View { |
| 942 | let value: String |
| 943 | let label: String |
| 944 | var tint: Color = GlimmerColor.accent |
| 945 | |
| 946 | var body: some View { |
| 947 | VStack(alignment: .leading, spacing: 2) { |
| 948 | Text(value).font(.title3.weight(.semibold)) |
| 949 | .monospacedDigit().lineLimit(1).minimumScaleFactor(0.6) |
| 950 | Text(label).font(.caption).foregroundStyle(.secondary) |
| 951 | } |
| 952 | .frame(maxWidth: .infinity, alignment: .leading) |
| 953 | .accessibilityElement(children: .combine) |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | // MARK: - Artwork |
| 958 | |
| 959 | enum AppResources { |
| 960 | static func url(_ name: String, extension ext: String, folder: String = "LibraryContent") -> URL? { |
| 961 | Bundle.main.url(forResource: name, withExtension: ext, subdirectory: folder) |
| 962 | ?? Bundle.main.url(forResource: name, withExtension: ext) |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | // MARK: - Reflection dial |
| 967 | |
| 968 | private struct DialArc: Shape { |
| 969 | var inset: CGFloat |
| 970 | |
| 971 | func path(in rect: CGRect) -> Path { |
| 972 | let radius = min(rect.width, rect.height) / 2 - inset |
| 973 | var path = Path() |
| 974 | path.addArc(center: CGPoint(x: rect.midX, y: rect.midY), radius: radius, |
| 975 | startAngle: .degrees(135), endAngle: .degrees(405), clockwise: false) |
| 976 | return path |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | /// The aftertaste and energy dial: one control, dragged around a 270° arc. |
| 981 | /// The arc fills and glows in the colour of the current step, the word in the middle changes with it, |
| 982 | /// and the knob snaps to the nearest of five stops when released. |
| 983 | struct ReflectionDial: View { |
| 984 | let title: String |
| 985 | let words: [String] |
| 986 | let colors: [Color] |
| 987 | @Binding var value: Double |
| 988 | @Environment(\.accessibilityReduceMotion) private var reduceMotion |
| 989 | @State private var dragging = false |
| 990 | |
| 991 | private let inset: CGFloat = 30 |
| 992 | private let lineWidth: CGFloat = 16 |
| 993 | private var step: Int { Moment.step(value) } |
| 994 | private var color: Color { colors[min(colors.count - 1, step)] } |
| 995 | private var gradient: AngularGradient { |
| 996 | AngularGradient(colors: colors, center: .center, startAngle: .degrees(135), endAngle: .degrees(405)) |
| 997 | } |
| 998 | |
| 999 | var body: some View { |
| 1000 | VStack(spacing: 4) { |
| 1001 | GeometryReader { proxy in |
| 1002 | let diameter = min(proxy.size.width, proxy.size.height) |
| 1003 | let radius = diameter / 2 - inset |
| 1004 | let center = CGPoint(x: proxy.size.width / 2, y: proxy.size.height / 2) |
| 1005 | ZStack { |
| 1006 | glow(radius: radius) |
| 1007 | DialArc(inset: inset) |
| 1008 | .stroke(Color.primary.opacity(0.08), style: StrokeStyle(lineWidth: lineWidth, lineCap: .round)) |
| 1009 | // The lit part of the arc, with a soft halo of the same light behind it. |
| 1010 | DialArc(inset: inset).trim(from: 0, to: max(0.002, value)) |
| 1011 | .stroke(gradient, style: StrokeStyle(lineWidth: lineWidth + 6, lineCap: .round)) |
| 1012 | .blur(radius: 12).opacity(0.55) |
| 1013 | DialArc(inset: inset).trim(from: 0, to: max(0.002, value)) |
| 1014 | .stroke(gradient, style: StrokeStyle(lineWidth: lineWidth, lineCap: .round)) |
| 1015 | ForEach(0..<5) { tick in |
| 1016 | Circle().fill(Double(tick) / 4 <= value + 0.01 ? Color.white.opacity(0.9) : Color.primary.opacity(0.25)) |
| 1017 | .frame(width: 4, height: 4) |
| 1018 | .position(point(center, radius, fraction: Double(tick) / 4)) |
| 1019 | } |
| 1020 | reading.frame(width: radius * 1.4).allowsHitTesting(false) |
| 1021 | knob.position(point(center, radius, fraction: value)) |
| 1022 | } |
| 1023 | .frame(width: proxy.size.width, height: proxy.size.height) |
| 1024 | .contentShape(.rect) |
| 1025 | .gesture(DragGesture(minimumDistance: 0).onChanged { event in |
| 1026 | let dx = event.location.x - center.x |
| 1027 | let dy = event.location.y - center.y |
| 1028 | guard hypot(dx, dy) > 24 else { return } |
| 1029 | let angle = atan2(dx, -dy) * 180 / .pi |
| 1030 | let sweep = (angle - 225 + 720).truncatingRemainder(dividingBy: 360) |
| 1031 | let target = sweep <= 270 ? sweep / 270 : (sweep < 315 ? 1 : 0) |
| 1032 | if dragging { |
| 1033 | value = target |
| 1034 | } else { |
| 1035 | // A tap or the start of a drag glides to the finger instead of jumping. |
| 1036 | withAnimation(reduceMotion ? nil : .snappy(duration: 0.25)) { dragging = true; value = target } |
| 1037 | } |
| 1038 | }.onEnded { _ in settle() }) |
| 1039 | } |
| 1040 | .frame(width: 280, height: 262) |
| 1041 | .animation(reduceMotion ? nil : .smooth(duration: 0.3), value: step) |
| 1042 | .sensoryFeedback(.selection, trigger: step) |
| 1043 | .sensoryFeedback(.impact(weight: .light), trigger: dragging) { _, isDragging in !isDragging } |
| 1044 | .accessibilityElement(children: .ignore) |
| 1045 | .accessibilityLabel(title) |
| 1046 | .accessibilityValue(words[step]) |
| 1047 | .accessibilityAdjustableAction { direction in |
| 1048 | switch direction { |
| 1049 | case .increment: move(by: 1) |
| 1050 | case .decrement: move(by: -1) |
| 1051 | @unknown default: break |
| 1052 | } |
| 1053 | } |
| 1054 | #if os(macOS) |
| 1055 | .focusable() |
| 1056 | .focusEffectDisabled() |
| 1057 | .onMoveCommand { direction in |
| 1058 | switch direction { |
| 1059 | case .right, .up: move(by: 1) |
| 1060 | case .left, .down: move(by: -1) |
| 1061 | @unknown default: break |
| 1062 | } |
| 1063 | } |
| 1064 | #endif |
| 1065 | HStack { |
| 1066 | Text(words.first ?? "") |
| 1067 | Spacer() |
| 1068 | Text(words.last ?? "") |
| 1069 | } |
| 1070 | .font(.caption).foregroundStyle(.secondary) |
| 1071 | .frame(width: 250) |
| 1072 | .padding(.top, -30) |
| 1073 | .accessibilityHidden(true) |
| 1074 | } |
| 1075 | .frame(maxWidth: .infinity) |
| 1076 | #if DEBUG |
| 1077 | .task { await runDemoIfRequested() } |
| 1078 | #endif |
| 1079 | } |
| 1080 | |
| 1081 | /// A pool of light behind the word that grows and brightens as the dial rises. |
| 1082 | private func glow(radius: CGFloat) -> some View { |
| 1083 | Circle() |
| 1084 | .fill(RadialGradient(colors: [color.opacity(0.16 + 0.26 * value), color.opacity(0)], |
| 1085 | center: .center, startRadius: 0, endRadius: radius * 0.95)) |
| 1086 | .frame(width: radius * 2, height: radius * 2) |
| 1087 | .scaleEffect(0.78 + 0.22 * value) |
| 1088 | .allowsHitTesting(false) |
| 1089 | } |
| 1090 | |
| 1091 | private var reading: some View { |
| 1092 | VStack(spacing: 2) { |
| 1093 | Text(title).font(.caption).foregroundStyle(.secondary) |
| 1094 | Text(words[step]).font(GlimmerFont.reading(.largeTitle, weight: .semibold)) |
| 1095 | .lineLimit(1).minimumScaleFactor(0.5) |
| 1096 | .id(step) |
| 1097 | .transition(reduceMotion ? AnyTransition.opacity : AnyTransition(.blurReplace)) |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | private var knob: some View { |
| 1102 | ZStack { |
| 1103 | // A ring of light that leaves the knob each time it lands on a new stop. |
| 1104 | Circle().stroke(color, lineWidth: 2).frame(width: 44, height: 44) |
| 1105 | .phaseAnimator([0, 1, 2], trigger: step) { ring, phase in |
| 1106 | // Rest (0) and the end of the ripple (2) are invisible; 1 is the ring sitting on the knob. |
| 1107 | ring.scaleEffect(phase == 1 ? 1 : 2.1).opacity(phase == 1 && !reduceMotion ? 0.6 : 0) |
| 1108 | } animation: { phase in phase == 2 ? .easeOut(duration: 0.55) : nil } |
| 1109 | Circle().fill(.clear).frame(width: 44, height: 44) |
| 1110 | .glimmerGlass(.circle, tint: color.opacity(0.45), interactive: true) |
| 1111 | Circle().fill(color).frame(width: 14, height: 14) |
| 1112 | .shadow(color: color.opacity(0.9), radius: dragging ? 9 : 4) |
| 1113 | } |
| 1114 | .scaleEffect(dragging ? 1.16 : 1) |
| 1115 | .animation(reduceMotion ? nil : .spring(duration: 0.3, bounce: 0.4), value: dragging) |
| 1116 | } |
| 1117 | |
| 1118 | private func settle() { |
| 1119 | withAnimation(reduceMotion ? nil : .spring(duration: 0.45, bounce: 0.35)) { |
| 1120 | dragging = false |
| 1121 | value = Double(step) / 4 |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | private func move(by amount: Int) { |
| 1126 | withAnimation(reduceMotion ? nil : .spring(duration: 0.4, bounce: 0.3)) { |
| 1127 | value = Double(min(4, max(0, step + amount))) / 4 |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | private func point(_ center: CGPoint, _ radius: CGFloat, fraction: Double) -> CGPoint { |
| 1132 | let radians = (225 + fraction * 270) * .pi / 180 |
| 1133 | return CGPoint(x: center.x + radius * sin(radians), y: center.y - radius * cos(radians)) |
| 1134 | } |
| 1135 | |
| 1136 | #if DEBUG |
| 1137 | /// Moves the dial by itself for design review recordings (GLIMMER_DIAL_DEMO with a test journal). |
| 1138 | private func runDemoIfRequested() async { |
| 1139 | let environment = ProcessInfo.processInfo.environment |
| 1140 | guard environment["GLIMMER_TEST_JOURNAL"] != nil, environment["GLIMMER_DIAL_DEMO"] != nil else { return } |
| 1141 | try? await Task.sleep(for: .seconds(5)) |
| 1142 | for target in [1.0, 0.0, 0.75] { |
| 1143 | let start = value |
| 1144 | dragging = true |
| 1145 | for frame in 1...90 { |
| 1146 | let progress = Double(frame) / 90 |
| 1147 | let eased = progress * progress * (3 - 2 * progress) |
| 1148 | value = start + (target - start) * eased |
| 1149 | try? await Task.sleep(for: .milliseconds(16)) |
| 1150 | } |
| 1151 | settle() |
| 1152 | try? await Task.sleep(for: .seconds(1.1)) |
| 1153 | } |
| 1154 | } |
| 1155 | #endif |
| 1156 | } |
| 1157 | |
| 1158 | extension View { |
| 1159 | /// Uses the tall page-sized sheet on iPad, so the dial is never hidden behind the pinned action bar. |
| 1160 | func tallSheet() -> some View { |
| 1161 | #if os(iOS) |
| 1162 | presentationSizing(.page) |
| 1163 | #else |
| 1164 | self |
| 1165 | #endif |
| 1166 | } |
| 1167 | |
| 1168 | /// Gives a focused, step-by-step flow the whole screen on iPhone. |
| 1169 | func hidesTabBar() -> some View { |
| 1170 | #if os(iOS) |
| 1171 | toolbar(.hidden, for: .tabBar) |
| 1172 | #else |
| 1173 | self |
| 1174 | #endif |
| 1175 | } |
| 1176 | } |