Glimmer / app · glimmertoday.ai / native / Glimmer / MoodSky.swift
| 1 | import SwiftUI |
| 2 | |
| 3 | /// One colour of the sky, kept as components so the weather can blend between moods. |
| 4 | private struct Tone { |
| 5 | let r: Double, g: Double, b: Double |
| 6 | |
| 7 | init(_ hex: String) { |
| 8 | let value = UInt32(hex, radix: 16) ?? 0 |
| 9 | r = Double((value >> 16) & 255) / 255 |
| 10 | g = Double((value >> 8) & 255) / 255 |
| 11 | b = Double(value & 255) / 255 |
| 12 | } |
| 13 | |
| 14 | /// The colour at `position` (0...1) along evenly spaced stops. |
| 15 | static func blend(_ stops: [Tone], at position: Double) -> Color { |
| 16 | let scaled = min(1, max(0, position)) * Double(stops.count - 1) |
| 17 | let lower = min(stops.count - 2, Int(scaled)) |
| 18 | let mix = scaled - Double(lower) |
| 19 | let a = stops[lower], b = stops[lower + 1] |
| 20 | return Color(red: a.r + (b.r - a.r) * mix, green: a.g + (b.g - a.g) * mix, blue: a.b + (b.b - a.b) * mix) |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | /// The colours of the five moods, from rain to a clear golden sky. |
| 25 | private struct SkyPalette { |
| 26 | let top: [Tone], bottom: [Tone], cloud: [Tone], sun: [Tone] |
| 27 | |
| 28 | static let day = SkyPalette( |
| 29 | top: ["6F7C8C", "93A0AD", "8FB3D3", "74AEDF", "5FA6E4"].map(Tone.init), |
| 30 | bottom: ["A5AEB8", "C6CCD2", "D5E3EC", "F1E3C6", "FADD9F"].map(Tone.init), |
| 31 | cloud: ["5D6875", "A9B2BC", "F2F5F7", "FFFFFF", "FFFFFF"].map(Tone.init), |
| 32 | sun: ["E6E3DC", "EFEADB", "F7DC9A", "F8CB62", "FBC23F"].map(Tone.init)) |
| 33 | |
| 34 | static let night = SkyPalette( |
| 35 | top: ["20252C", "2A313A", "27384C", "264566", "264E7A"].map(Tone.init), |
| 36 | bottom: ["353B44", "444C56", "4B5C72", "7F6E52", "A8873F"].map(Tone.init), |
| 37 | cloud: ["171B21", "39414B", "8795A6", "B9C4D1", "C8D2DE"].map(Tone.init), |
| 38 | sun: ["8D9299", "A9A9A2", "DCC68D", "ECC566", "F5C453"].map(Tone.init)) |
| 39 | } |
| 40 | |
| 41 | private struct CloudShape: Shape { |
| 42 | func path(in rect: CGRect) -> Path { |
| 43 | var path = Path() |
| 44 | let puffs: [(x: Double, y: Double, w: Double, h: Double)] = [ |
| 45 | (0.00, 0.46, 0.46, 0.54), (0.20, 0.14, 0.42, 0.74), (0.46, 0.00, 0.36, 0.80), (0.62, 0.34, 0.38, 0.66), |
| 46 | ] |
| 47 | for puff in puffs { |
| 48 | path.addEllipse(in: CGRect(x: rect.minX + rect.width * puff.x, y: rect.minY + rect.height * puff.y, |
| 49 | width: rect.width * puff.w, height: rect.height * puff.h)) |
| 50 | } |
| 51 | path.addRoundedRect(in: CGRect(x: rect.minX + rect.width * 0.14, y: rect.minY + rect.height * 0.62, |
| 52 | width: rect.width * 0.72, height: rect.height * 0.38), |
| 53 | cornerSize: CGSize(width: rect.height * 0.19, height: rect.height * 0.19)) |
| 54 | return path |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /// Today's mood as a small sky. Drag the sun: rain thins to cloud, cloud parts, and the light warms. |
| 59 | /// It lands on one of five moods, with a tick you can feel at each one. |
| 60 | struct MoodSky: View { |
| 61 | static let words = ["Low", "Off", "Okay", "Good", "Great"] |
| 62 | |
| 63 | /// 1...5, or nil when today has no mood yet. |
| 64 | let mood: Int? |
| 65 | var onChange: (Int?) -> Void |
| 66 | |
| 67 | @Environment(\.readingTheme) private var theme |
| 68 | @Environment(\.accessibilityReduceMotion) private var reduceMotion |
| 69 | @Environment(\.colorSchemeContrast) private var contrast |
| 70 | @State private var position = 0.5 |
| 71 | @State private var moved = false |
| 72 | @GestureState private var dragging = false |
| 73 | |
| 74 | private var step: Int { Int((position * 4).rounded()) } |
| 75 | private var isSet: Bool { mood != nil || moved } |
| 76 | private var palette: SkyPalette { theme == .glimmer ? .night : .day } |
| 77 | |
| 78 | var body: some View { |
| 79 | VStack(alignment: .leading, spacing: 0) { |
| 80 | GeometryReader { proxy in sky(proxy.size) } |
| 81 | .frame(height: 168) |
| 82 | .clipShape(.rect(cornerRadius: 12, style: .continuous)) |
| 83 | .padding(4) |
| 84 | footer.padding(.horizontal, 16).padding(.top, 8).padding(.bottom, 14) |
| 85 | } |
| 86 | .background(theme.surface, in: .rect(cornerRadius: 16, style: .continuous)) |
| 87 | .overlay { |
| 88 | RoundedRectangle(cornerRadius: 16, style: .continuous) |
| 89 | .strokeBorder(.primary.opacity(contrast == .increased ? 0.30 : 0.08), lineWidth: 1) |
| 90 | } |
| 91 | .onAppear { if let mood { position = Double(mood - 1) / 4 } } |
| 92 | .onChange(of: mood) { _, new in |
| 93 | guard !dragging else { return } |
| 94 | withAnimation(reduceMotion ? nil : .smooth(duration: 0.4)) { |
| 95 | if let new { position = Double(new - 1) / 4 } else { position = 0.5; moved = false } |
| 96 | } |
| 97 | } |
| 98 | .onChange(of: dragging) { _, active in if !active && moved { settle() } } |
| 99 | .sensoryFeedback(.selection, trigger: step) { _, _ in moved } |
| 100 | .sensoryFeedback(.impact(weight: .light), trigger: dragging) { _, active in !active } |
| 101 | } |
| 102 | |
| 103 | // MARK: Sky |
| 104 | |
| 105 | private func sky(_ size: CGSize) -> some View { |
| 106 | let light = isSet ? position : 0.5 |
| 107 | let sun = sunPoint(size, at: position) |
| 108 | return ZStack { |
| 109 | LinearGradient(colors: [Tone.blend(palette.top, at: light), Tone.blend(palette.bottom, at: light)], |
| 110 | startPoint: .top, endPoint: .bottom) |
| 111 | track(size) |
| 112 | // The light the sun throws grows as the sky clears. |
| 113 | Circle() |
| 114 | .fill(RadialGradient(colors: [Tone.blend(palette.sun, at: light).opacity(0.25 + 0.45 * light), .clear], |
| 115 | center: .center, startRadius: 0, endRadius: 60 + 80 * light)) |
| 116 | .frame(width: 320, height: 320) |
| 117 | .position(sun) |
| 118 | glints(size, sun: sun).opacity(isSet ? fade(light, from: 0.82, to: 1) : 0) |
| 119 | sunDisc(light).position(sun) |
| 120 | clouds(size, light: light) |
| 121 | rain(size).opacity(isSet ? 1 - fade(light, from: 0.04, to: 0.26) : 0) |
| 122 | } |
| 123 | .saturation(isSet ? 1 : 0.35) |
| 124 | .contentShape(.rect) |
| 125 | .onTapGesture(coordinateSpace: .local) { point in |
| 126 | moved = true |
| 127 | withAnimation(reduceMotion ? nil : .spring(duration: 0.5, bounce: 0.25)) { position = fraction(point.x, size.width) } |
| 128 | settle() |
| 129 | } |
| 130 | // Runs beside the page's scrolling, so a vertical swipe that starts on the sky still scrolls. |
| 131 | .simultaneousGesture( |
| 132 | DragGesture(minimumDistance: 8) |
| 133 | .updating($dragging) { value, active, _ in |
| 134 | if abs(value.translation.width) >= abs(value.translation.height) || active { active = true } |
| 135 | } |
| 136 | .onChanged { value in |
| 137 | guard dragging || abs(value.translation.width) >= abs(value.translation.height) else { return } |
| 138 | moved = true |
| 139 | position = fraction(value.location.x, size.width) |
| 140 | } |
| 141 | ) |
| 142 | .accessibilityElement(children: .ignore) |
| 143 | .accessibilityLabel("Today’s mood") |
| 144 | .accessibilityValue(isSet ? Self.words[step] : "Not set") |
| 145 | .accessibilityHint("Swipe up or down to change") |
| 146 | .accessibilityAdjustableAction { direction in |
| 147 | switch direction { |
| 148 | case .increment: move(by: 1) |
| 149 | case .decrement: move(by: -1) |
| 150 | @unknown default: break |
| 151 | } |
| 152 | } |
| 153 | .accessibilityIdentifier("habits.mood") |
| 154 | #if os(macOS) |
| 155 | .focusable() |
| 156 | .onMoveCommand { direction in |
| 157 | switch direction { |
| 158 | case .right, .up: move(by: 1) |
| 159 | case .left, .down: move(by: -1) |
| 160 | @unknown default: break |
| 161 | } |
| 162 | } |
| 163 | #endif |
| 164 | } |
| 165 | |
| 166 | /// The path the sun travels, with a stop for each mood. |
| 167 | private func track(_ size: CGSize) -> some View { |
| 168 | ZStack { |
| 169 | Path { path in |
| 170 | path.move(to: sunPoint(size, at: 0)) |
| 171 | for index in 1...40 { path.addLine(to: sunPoint(size, at: Double(index) / 40)) } |
| 172 | } |
| 173 | .stroke(Color.white.opacity(0.38), style: StrokeStyle(lineWidth: 1.5, lineCap: .round, dash: [1, 6])) |
| 174 | ForEach(0..<5) { stop in |
| 175 | Circle().fill(Color.white.opacity(isSet && stop == step ? 0 : 0.7)) |
| 176 | .frame(width: 5, height: 5) |
| 177 | .position(sunPoint(size, at: Double(stop) / 4)) |
| 178 | } |
| 179 | } |
| 180 | .accessibilityHidden(true) |
| 181 | } |
| 182 | |
| 183 | private func sunDisc(_ light: Double) -> some View { |
| 184 | let color = Tone.blend(palette.sun, at: light) |
| 185 | return ZStack { |
| 186 | Circle().fill(color) |
| 187 | .shadow(color: color.opacity(0.9), radius: 6 + 12 * light) |
| 188 | Circle().strokeBorder(Color.white.opacity(0.75), lineWidth: 1.5) |
| 189 | } |
| 190 | .frame(width: 38, height: 38) |
| 191 | .scaleEffect(dragging ? 1.18 : 1) |
| 192 | .animation(reduceMotion ? nil : .spring(duration: 0.3, bounce: 0.45), value: dragging) |
| 193 | } |
| 194 | |
| 195 | private func clouds(_ size: CGSize, light: Double) -> some View { |
| 196 | let color = Tone.blend(palette.cloud, at: light) |
| 197 | let heavy = 1 - fade(light, from: 0.05, to: 0.45) |
| 198 | return ZStack { |
| 199 | // A grey lid over the whole sky on the lowest days. |
| 200 | Rectangle().fill(color).opacity(heavy * 0.5) |
| 201 | CloudShape().fill(color) |
| 202 | .frame(width: size.width * 0.62, height: 74) |
| 203 | .position(x: size.width * (0.36 - 0.22 * light), y: size.height * 0.34) |
| 204 | .opacity(0.94 * (1 - fade(light, from: 0.55, to: 0.95))) |
| 205 | CloudShape().fill(color) |
| 206 | .frame(width: size.width * 0.52, height: 62) |
| 207 | .position(x: size.width * (0.72 + 0.30 * light), y: size.height * 0.50) |
| 208 | .opacity(0.9 * (1 - fade(light, from: 0.35, to: 0.72))) |
| 209 | CloudShape().fill(color) |
| 210 | .frame(width: size.width * 0.30, height: 34) |
| 211 | .position(x: size.width * (0.20 + 0.10 * light), y: size.height * 0.74) |
| 212 | .opacity(0.7 * (1 - fade(light, from: 0.62, to: 0.98))) |
| 213 | } |
| 214 | .blur(radius: 5) |
| 215 | .allowsHitTesting(false) |
| 216 | } |
| 217 | |
| 218 | private func rain(_ size: CGSize) -> some View { |
| 219 | TimelineView(.animation(minimumInterval: 1 / 30, paused: reduceMotion || !isSet || position > 0.27)) { timeline in |
| 220 | Canvas { context, canvas in |
| 221 | let time = reduceMotion ? 0 : timeline.date.timeIntervalSinceReferenceDate |
| 222 | for drop in 0..<26 { |
| 223 | let seed = Double(drop) |
| 224 | let column = (seed * 0.6180339887).truncatingRemainder(dividingBy: 1) |
| 225 | let speed = 0.55 + (seed * 0.37).truncatingRemainder(dividingBy: 1) * 0.4 |
| 226 | let fall = (time * speed + seed * 0.173).truncatingRemainder(dividingBy: 1) |
| 227 | let x = canvas.width * (column * 1.1 - 0.05) - fall * 14 |
| 228 | let y = canvas.height * (0.42 + fall * 0.62) |
| 229 | var path = Path() |
| 230 | path.move(to: CGPoint(x: x, y: y)) |
| 231 | path.addLine(to: CGPoint(x: x - 3, y: y + 11)) |
| 232 | context.stroke(path, with: .color(.white.opacity(0.5 * sin(fall * .pi))), |
| 233 | style: StrokeStyle(lineWidth: 1.2, lineCap: .round)) |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | .allowsHitTesting(false) |
| 238 | } |
| 239 | |
| 240 | /// A few small glints around the sun on the brightest days. |
| 241 | private func glints(_ size: CGSize, sun: CGPoint) -> some View { |
| 242 | Canvas { context, _ in |
| 243 | let spots: [(x: Double, y: Double, r: Double)] = [(-74, 18, 5), (-44, -34, 3.5), (30, 52, 4), (-104, -18, 3)] |
| 244 | for spot in spots { |
| 245 | var path = Path() |
| 246 | for point in 0..<8 { |
| 247 | let angle = Double(point) * .pi / 4 - .pi / 2 |
| 248 | let distance = point.isMultiple(of: 2) ? spot.r : spot.r * 0.24 |
| 249 | let corner = CGPoint(x: sun.x + spot.x + cos(angle) * distance, y: sun.y + spot.y + sin(angle) * distance) |
| 250 | if point == 0 { path.move(to: corner) } else { path.addLine(to: corner) } |
| 251 | } |
| 252 | path.closeSubpath() |
| 253 | context.fill(path, with: .color(.white.opacity(0.9))) |
| 254 | } |
| 255 | } |
| 256 | .allowsHitTesting(false) |
| 257 | } |
| 258 | |
| 259 | // MARK: Footer |
| 260 | |
| 261 | private var footer: some View { |
| 262 | HStack(alignment: .firstTextBaseline) { |
| 263 | VStack(alignment: .leading, spacing: 1) { |
| 264 | Text("Today’s mood").font(.caption).foregroundStyle(.secondary) |
| 265 | Text(isSet ? Self.words[step] : "How does today feel?") |
| 266 | .font(GlimmerFont.reading(.title2, weight: .semibold)) |
| 267 | .lineLimit(1).minimumScaleFactor(0.7) |
| 268 | .id(isSet ? step : -1) |
| 269 | .transition(reduceMotion ? AnyTransition.opacity : AnyTransition(.blurReplace)) |
| 270 | } |
| 271 | .animation(reduceMotion ? nil : .smooth(duration: 0.3), value: step) |
| 272 | .animation(reduceMotion ? nil : .smooth(duration: 0.3), value: isSet) |
| 273 | .accessibilityHidden(true) |
| 274 | Spacer(minLength: 8) |
| 275 | if mood != nil { |
| 276 | Button("Clear") { |
| 277 | moved = false |
| 278 | onChange(nil) |
| 279 | } |
| 280 | .buttonStyle(.plain).font(.footnote).foregroundStyle(.secondary) |
| 281 | .frame(minHeight: 30).contentShape(.rect) |
| 282 | .accessibilityLabel("Clear today’s mood") |
| 283 | } else { |
| 284 | Text("Drag the sun").font(.footnote).foregroundStyle(.secondary).accessibilityHidden(true) |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | // MARK: Geometry |
| 290 | |
| 291 | private func sunPoint(_ size: CGSize, at position: Double) -> CGPoint { |
| 292 | let rise = 1 - pow(1 - position, 2) |
| 293 | return CGPoint(x: size.width * (0.13 + 0.74 * position), y: size.height * (0.74 - 0.44 * rise)) |
| 294 | } |
| 295 | |
| 296 | private func fraction(_ x: CGFloat, _ width: CGFloat) -> Double { |
| 297 | min(1, max(0, (x / max(1, width) - 0.13) / 0.74)) |
| 298 | } |
| 299 | |
| 300 | /// 0 below `from`, 1 above `to`, and a straight ramp between. |
| 301 | private func fade(_ value: Double, from: Double, to: Double) -> Double { |
| 302 | min(1, max(0, (value - from) / (to - from))) |
| 303 | } |
| 304 | |
| 305 | private func settle() { |
| 306 | let landed = step |
| 307 | withAnimation(reduceMotion ? nil : .spring(duration: 0.45, bounce: 0.35)) { position = Double(landed) / 4 } |
| 308 | if mood != landed + 1 { onChange(landed + 1) } |
| 309 | } |
| 310 | |
| 311 | private func move(by amount: Int) { |
| 312 | let next = isSet ? min(4, max(0, step + amount)) : 2 |
| 313 | moved = true |
| 314 | withAnimation(reduceMotion ? nil : .spring(duration: 0.4, bounce: 0.3)) { position = Double(next) / 4 } |
| 315 | if mood != next + 1 { onChange(next + 1) } |
| 316 | } |
| 317 | } |