Glimmer / app · glimmertoday.ai / native / Glimmer / ReadingMotion.swift
| 1 | import SwiftUI |
| 2 | |
| 3 | /// A single stroke laps a completed item, then its timeline ends. |
| 4 | private struct CompletionLight<Outline: InsettableShape>: ViewModifier { |
| 5 | var complete: Bool |
| 6 | let outline: Outline |
| 7 | @Environment(\.accessibilityReduceMotion) private var reduceMotion |
| 8 | @State private var pulse = 0 |
| 9 | |
| 10 | func body(content: Content) -> some View { |
| 11 | content.overlay { |
| 12 | if !reduceMotion { |
| 13 | Color.clear.keyframeAnimator(initialValue: 1.0, trigger: pulse) { _, phase in |
| 14 | let tail = max(0, phase * 1.18 - 0.18) |
| 15 | let head = min(1, phase * 1.18) |
| 16 | outline.inset(by: 0.8).trim(from: tail, to: head) |
| 17 | .stroke(GlimmerColor.accent.opacity(0.82), style: .init(lineWidth: 1.3, lineCap: .round)) |
| 18 | } keyframes: { _ in |
| 19 | MoveKeyframe(0.0) |
| 20 | LinearKeyframe(1.0, duration: 0.72) |
| 21 | } |
| 22 | .opacity(complete ? 1 : 0) |
| 23 | .accessibilityHidden(true).allowsHitTesting(false) |
| 24 | } |
| 25 | } |
| 26 | .onChange(of: complete) { _, done in if done && !reduceMotion { pulse += 1 } } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | /// Finite depth change when an editorial surface takes its place. |
| 31 | private struct ReadingArrival: ViewModifier { |
| 32 | var delay: Double |
| 33 | @Environment(\.accessibilityReduceMotion) private var reduceMotion |
| 34 | @State private var settled = false |
| 35 | |
| 36 | func body(content: Content) -> some View { |
| 37 | content |
| 38 | .rotation3DEffect(.degrees(settled || reduceMotion ? 0 : -3), axis: (1, 0, 0), anchor: .bottom) |
| 39 | .offset(y: settled || reduceMotion ? 0 : 12) |
| 40 | .onAppear { |
| 41 | withAnimation(reduceMotion ? nil : .smooth(duration: 0.65).delay(delay)) { settled = true } |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | extension View { |
| 47 | func completionLight(_ completed: Bool, radius: CGFloat = 14) -> some View { |
| 48 | completionLight(completed, outline: RoundedRectangle(cornerRadius: radius, style: .continuous)) |
| 49 | } |
| 50 | |
| 51 | func completionLight(_ completed: Bool, outline: some InsettableShape) -> some View { |
| 52 | modifier(CompletionLight(complete: completed, outline: outline)) |
| 53 | } |
| 54 | |
| 55 | func readingArrival(delay: Double = 0) -> some View { modifier(ReadingArrival(delay: delay)) } |
| 56 | } |
| 57 | |
| 58 | /// The depth of the orbit changes the radius and brightness of each point. |
| 59 | struct ReflectionOrbit: View { |
| 60 | @Environment(\.accessibilityReduceMotion) private var reduceMotion |
| 61 | @Environment(\.scenePhase) private var scene |
| 62 | var body: some View { |
| 63 | TimelineView(.animation(minimumInterval: 1 / 24, paused: reduceMotion || scene != .active)) { clock in |
| 64 | let angle = reduceMotion ? 0 : clock.date.timeIntervalSinceReferenceDate.truncatingRemainder(dividingBy: 3.6) * 2 * .pi / 3.6 |
| 65 | Canvas { context, size in |
| 66 | let radius = min(size.width, size.height) * 0.40 |
| 67 | for index in 0..<16 { |
| 68 | let t = angle + Double(index) * .pi / 8 |
| 69 | let depth = (sin(t) + 1) / 2 |
| 70 | let diameter = 1.1 + depth * 1.7 |
| 71 | let x = size.width / 2 + radius * cos(t) |
| 72 | let y = size.height / 2 + radius * (0.36 * sin(t) - 0.4 * cos(t)) |
| 73 | let rect = CGRect(x: x - diameter/2, y: y - diameter/2, width: diameter, height: diameter) |
| 74 | context.fill(Path(ellipseIn: rect), with: .color(GlimmerColor.sage.opacity(0.35 + depth * 0.65))) |
| 75 | } |
| 76 | } |
| 77 | .overlay { Image(systemName: "sparkle").font(.system(size: 9)).foregroundStyle(GlimmerColor.honey) } |
| 78 | }.frame(width: 26, height: 26).accessibilityHidden(true) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | struct ChartReveal: ViewModifier { |
| 83 | @Environment(\.accessibilityReduceMotion) private var reduceMotion |
| 84 | @State private var revealed = false |
| 85 | |
| 86 | func body(content: Content) -> some View { |
| 87 | content.mask(alignment: .leading) { |
| 88 | Rectangle().scaleEffect(x: revealed || reduceMotion ? 1 : 0.04, anchor: .leading) |
| 89 | } |
| 90 | .onAppear { |
| 91 | withAnimation(reduceMotion ? nil : .easeOut(duration: 0.65)) { revealed = true } |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /// Move the incoming sheet a few points above its final position. The underlying |
| 97 | /// paper and controls stay opaque throughout keyboard, pointer and touch navigation. |
| 98 | struct ReadingPageTurn: ViewModifier { |
| 99 | let forward: Bool |
| 100 | @Environment(\.accessibilityReduceMotion) private var reduceMotion |
| 101 | @State private var arrived = false |
| 102 | |
| 103 | func body(content: Content) -> some View { |
| 104 | content |
| 105 | .offset(x: arrived || reduceMotion ? 0 : forward ? 14 : -14) |
| 106 | .onAppear { |
| 107 | withAnimation(reduceMotion ? nil : .smooth(duration: 0.32)) { arrived = true } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /// Mount once for layout, then reveal the contents. Closing reverses the reveal before collapsing. |
| 113 | struct ReadingDisclosure<Content: View>: View { |
| 114 | let expanded: Bool |
| 115 | @ViewBuilder let content: (Bool) -> Content |
| 116 | @State private var mounted = false |
| 117 | @State private var visible = false |
| 118 | @Environment(\.accessibilityReduceMotion) private var reduceMotion |
| 119 | |
| 120 | var body: some View { |
| 121 | VStack(alignment: .leading, spacing: 0) { |
| 122 | if mounted { |
| 123 | content(visible) |
| 124 | .accessibilityHidden(!expanded || !visible) |
| 125 | .allowsHitTesting(expanded && visible) |
| 126 | .modifier(DisclosureRowMotion(index: 0, count: 1, visible: visible)) |
| 127 | .transition(.opacity) |
| 128 | } |
| 129 | } |
| 130 | .task(id: expanded) { |
| 131 | if reduceMotion { |
| 132 | mounted = expanded |
| 133 | visible = expanded |
| 134 | } else if expanded { |
| 135 | withAnimation(.smooth(duration: 0.28)) { mounted = true } |
| 136 | try? await Task.sleep(for: .milliseconds(30)) |
| 137 | if !Task.isCancelled { visible = true } |
| 138 | } else if mounted { |
| 139 | visible = false |
| 140 | try? await Task.sleep(for: .milliseconds(190)) |
| 141 | if !Task.isCancelled { |
| 142 | withAnimation(.smooth(duration: 0.19)) { mounted = false } |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | struct DisclosureRowMotion: ViewModifier { |
| 150 | let index: Int |
| 151 | let count: Int |
| 152 | let visible: Bool |
| 153 | @Environment(\.accessibilityReduceMotion) private var reduceMotion |
| 154 | |
| 155 | private var timing: Animation { |
| 156 | if visible { return .easeOut(duration: 0.23).delay(Double(min(index, 5)) * 0.044) } |
| 157 | return .easeOut(duration: 0.10).delay(Double(min(max(count - index - 1, 0), 5)) * 0.016) |
| 158 | } |
| 159 | |
| 160 | func body(content: Content) -> some View { |
| 161 | content.opacity(visible ? 1 : 0) |
| 162 | .offset(y: visible || reduceMotion ? 0 : -5) |
| 163 | .animation(reduceMotion ? nil : timing, value: visible) |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | struct GlimmerDisclosureStyle: DisclosureGroupStyle { |
| 168 | @Environment(\.accessibilityReduceMotion) private var reduceMotion |
| 169 | |
| 170 | func makeBody(configuration: Configuration) -> some View { |
| 171 | VStack(alignment: .leading, spacing: 8) { |
| 172 | Button { |
| 173 | configuration.isExpanded.toggle() |
| 174 | } label: { |
| 175 | HStack(spacing: 8) { |
| 176 | configuration.label |
| 177 | Spacer(minLength: 6) |
| 178 | Image(systemName: "chevron.right") |
| 179 | .font(.footnote.weight(.semibold)) |
| 180 | .foregroundStyle(.secondary) |
| 181 | .rotationEffect(.degrees(configuration.isExpanded ? 90 : 0)) |
| 182 | .animation(reduceMotion ? nil : .smooth(duration: 0.22), value: configuration.isExpanded) |
| 183 | .accessibilityHidden(true) |
| 184 | } |
| 185 | .padding(.vertical, 8) |
| 186 | .contentShape(.rect) |
| 187 | } |
| 188 | .buttonStyle(.plain) |
| 189 | .accessibilityValue(configuration.isExpanded ? "Expanded" : "Collapsed") |
| 190 | ReadingDisclosure(expanded: configuration.isExpanded) { _ in configuration.content } |
| 191 | } |
| 192 | } |
| 193 | } |