Branchwell

Glimmer / app · glimmertoday.ai / native / Glimmer / Tactile.swift

1import SwiftUI
2#if os(macOS)
3import AppKit
4#endif
5
6// Things you touch answer like objects: they give under a finger, settle with a little spring,
7// and confirm with a tap you can feel. Every check-off in the app uses the pieces in this file.
8
9// MARK: - Tick
10
11private struct CheckStroke: Shape {
12 func path(in rect: CGRect) -> Path {
13 var path = Path()
14 path.move(to: CGPoint(x: rect.minX + rect.width * 0.08, y: rect.minY + rect.height * 0.55))
15 path.addLine(to: CGPoint(x: rect.minX + rect.width * 0.38, y: rect.minY + rect.height * 0.84))
16 path.addLine(to: CGPoint(x: rect.minX + rect.width * 0.94, y: rect.minY + rect.height * 0.18))
17 return path
18 }
19}
20
21/// The one check mark used across Glimmer. Checking it fills the ring with a soft spring, draws the
22/// check as a stroke and lets a ring of light leave it. Unchecking is quick and plain.
23struct Tick: View {
24 let done: Bool
25 var size: CGFloat = 26
26 var tint: Color = GlimmerColor.sage
27 /// The colour of the check drawn on `tint`.
28 var onTint: Color = Color(light: "ffffff", dark: "1c1c1b")
29 var ripples = true
30 @Environment(\.accessibilityReduceMotion) private var reduceMotion
31 @Environment(\.colorSchemeContrast) private var contrast
32 @State private var ripple = 0
33
34 var body: some View {
35 ZStack {
36 if ripples && !reduceMotion {
37 Circle().stroke(tint, lineWidth: 1.5)
38 .phaseAnimator([0, 1, 2], trigger: ripple) { ring, phase in
39 ring.scaleEffect(phase == 1 ? 1 : 2).opacity(phase == 1 ? 0.55 : 0)
40 } animation: { phase in phase == 2 ? .easeOut(duration: 0.5) : nil }
41 }
42 Circle().strokeBorder(Color.primary.opacity(contrast == .increased ? 0.8 : 0.42), lineWidth: 1.5)
43 .opacity(done ? 0 : 1)
44 .animation(.easeOut(duration: 0.12), value: done)
45 Circle().fill(tint)
46 .scaleEffect(done ? 1 : 0.3).opacity(done ? 1 : 0)
47 .animation(fill, value: done)
48 CheckStroke().trim(from: 0, to: done ? 1 : 0)
49 .stroke(onTint, style: StrokeStyle(lineWidth: max(1.8, size * 0.085), lineCap: .round, lineJoin: .round))
50 .padding(size * 0.29)
51 .animation(stroke, value: done)
52 }
53 .frame(width: size, height: size)
54 .onChange(of: done) { _, now in if now { ripple += 1 } }
55 .accessibilityHidden(true)
56 }
57
58 private var fill: Animation {
59 guard done else { return .easeOut(duration: 0.12) }
60 return reduceMotion ? .easeOut(duration: 0.15) : .spring(duration: 0.4, bounce: 0.42)
61 }
62
63 private var stroke: Animation {
64 guard done, !reduceMotion else { return .easeOut(duration: 0.12) }
65 return .easeOut(duration: 0.26).delay(0.1)
66 }
67}
68
69/// A control gives a little under the finger and springs back.
70struct PressStyle: ButtonStyle {
71 var scale: CGFloat = 0.94
72 @Environment(\.accessibilityReduceMotion) private var reduceMotion
73
74 func makeBody(configuration: Configuration) -> some View {
75 configuration.label
76 .scaleEffect(configuration.isPressed && !reduceMotion ? scale : 1)
77 .opacity(configuration.isPressed && reduceMotion ? 0.6 : 1)
78 .animation(configuration.isPressed ? .easeOut(duration: 0.1) : .spring(duration: 0.4, bounce: 0.5),
79 value: configuration.isPressed)
80 }
81}
82
83/// A row lights softly under the finger, so the whole line reads as touchable.
84struct RowPressStyle: ButtonStyle {
85 var radius: CGFloat = 10
86 func makeBody(configuration: Configuration) -> some View {
87 configuration.label
88 .background(Color.primary.opacity(configuration.isPressed ? 0.07 : 0), in: .rect(cornerRadius: radius, style: .continuous))
89 .animation(.easeOut(duration: configuration.isPressed ? 0.05 : 0.25), value: configuration.isPressed)
90 }
91}
92
93private struct TickFeedback: ViewModifier {
94 let done: Bool
95
96 func body(content: Content) -> some View {
97 #if os(iOS)
98 content.sensoryFeedback(trigger: done) { _, now in
99 now ? .success : .impact(flexibility: .soft, intensity: 0.6)
100 }
101 #else
102 content.onChange(of: done) { _, now in
103 if now { NSHapticFeedbackManager.defaultPerformer.perform(.levelChange, performanceTime: .now) }
104 }
105 #endif
106 }
107}
108
109extension View {
110 /// The feel of a check-off: a success tap on iPhone and iPad, a trackpad click on Mac.
111 func tickFeedback(_ done: Bool) -> some View { modifier(TickFeedback(done: done)) }
112}
113
114/// The tick as a button, with a full 44 point target on the leading edge of a row.
115struct TickButton: View {
116 let done: Bool
117 /// What VoiceOver says, such as "Complete Drink some water".
118 let label: String
119 var size: CGFloat = 26
120 var tint: Color = GlimmerColor.sage
121 var onTint: Color = Color(light: "ffffff", dark: "1c1c1b")
122 /// False when the tick only includes or selects something, so it gets a lighter tap.
123 var completes = true
124 var action: () -> Void
125
126 var body: some View {
127 Button(action: action) {
128 Tick(done: done, size: size, tint: tint, onTint: onTint, ripples: completes)
129 .frame(width: 44, height: 44)
130 .contentShape(.rect)
131 }
132 .buttonStyle(PressStyle(scale: 0.8))
133 .accessibilityLabel(label)
134 .tickFeedback(completes && done)
135 .sensoryFeedback(.selection, trigger: done) { _, _ in !completes }
136 }
137}
138
139/// An empty, dashed tick that opens a list for a new line.
140struct AddMark: View {
141 var size: CGFloat = 26
142 var body: some View {
143 Image(systemName: "plus").font(.system(size: size * 0.46, weight: .semibold)).foregroundStyle(.secondary)
144 .frame(width: size, height: size)
145 .background { Circle().strokeBorder(Color.primary.opacity(0.3), style: StrokeStyle(lineWidth: 1.5, dash: [3, 3.5])) }
146 .frame(width: 44, height: 44)
147 .accessibilityHidden(true)
148 }
149}
150
151// MARK: - Habit stones
152
153private struct RingArc: Shape {
154 let start: Double
155 let end: Double
156
157 func path(in rect: CGRect) -> Path {
158 var path = Path()
159 path.addArc(center: CGPoint(x: rect.midX, y: rect.midY), radius: min(rect.width, rect.height) / 2,
160 startAngle: .degrees(start), endAngle: .degrees(end), clockwise: false)
161 return path
162 }
163}
164
165/// Seven arcs around a stone, one for each of the last seven days. The last arc is today.
166private struct WeekRing: View {
167 let week: [Bool]
168 let tint: Color
169 @Environment(\.accessibilityReduceMotion) private var reduceMotion
170 private let width: CGFloat = 4
171 private let gap = 9.0
172
173 var body: some View {
174 let span = 360.0 / Double(week.count)
175 ZStack {
176 ForEach(week.indices, id: \.self) { index in
177 let arc = RingArc(start: -90 + span * Double(index) + gap / 2, end: -90 + span * Double(index + 1) - gap / 2)
178 arc.stroke(Color.primary.opacity(0.11), style: StrokeStyle(lineWidth: width, lineCap: .round))
179 arc.trim(from: 0, to: week[index] ? 1 : 0)
180 .stroke(tint, style: StrokeStyle(lineWidth: width, lineCap: .round))
181 .opacity(week[index] ? 1 : 0)
182 .animation(reduceMotion ? .easeOut(duration: 0.15) : .easeOut(duration: 0.34), value: week[index])
183 }
184 }
185 .padding(width / 2)
186 }
187}
188
189/// A habit you press like a smooth stone. Its rim is the week: pressing it lights today's arc,
190/// the icon hops, and a ring of light leaves the stone.
191struct HabitStone: View {
192 @Environment(\.readingTheme) private var theme
193 @Environment(\.accessibilityReduceMotion) private var reduceMotion
194 let icon: String
195 let name: String
196 /// The last seven days, oldest first. The last entry is today.
197 let week: [Bool]
198 var size: CGFloat = 78
199 var action: () -> Void
200 @State private var pulse = 0
201
202 private var done: Bool { week.last == true }
203 private var days: Int { week.filter { $0 }.count }
204 private var tint: Color { days == week.count ? GlimmerColor.honey : GlimmerColor.sage }
205
206 var body: some View {
207 Button(action: action) {
208 VStack(spacing: 9) {
209 stone
210 Text(name).font(.footnote.weight(.medium))
211 .foregroundStyle(done ? Color.secondary : Color.primary)
212 .multilineTextAlignment(.center).lineLimit(3)
213 .fixedSize(horizontal: false, vertical: true)
214 }
215 .frame(maxWidth: .infinity, alignment: .top)
216 .contentShape(.rect)
217 }
218 .buttonStyle(PressStyle(scale: 0.9))
219 .tickFeedback(done)
220 .onChange(of: done) { _, now in if now { pulse += 1 } }
221 .accessibilityElement(children: .ignore)
222 .accessibilityLabel(name)
223 .accessibilityValue("\(done ? "Done today" : "Not done today"), \(days) of the last 7 days")
224 .accessibilityAddTraits([.isButton, .isToggle])
225 }
226
227 private var stone: some View {
228 ZStack {
229 if !reduceMotion {
230 Circle().stroke(tint, lineWidth: 2)
231 .phaseAnimator([0, 1, 2], trigger: pulse) { ring, phase in
232 ring.scaleEffect(phase == 1 ? 1 : 1.5).opacity(phase == 1 ? 0.5 : 0)
233 } animation: { phase in phase == 2 ? .easeOut(duration: 0.6) : nil }
234 }
235 Circle().fill(theme.surface)
236 .shadow(color: .black.opacity(theme == .glimmer ? 0.35 : 0.10), radius: 5, y: 2)
237 Circle().fill(tint.opacity(done ? 0.17 : 0))
238 .animation(.easeOut(duration: 0.3), value: done)
239 WeekRing(week: week, tint: tint)
240 Image(systemName: Glyph.symbol(icon: icon, name: name, category: "habit"))
241 .font(.system(size: size * 0.3, weight: .semibold)).symbolRenderingMode(.monochrome)
242 .foregroundStyle(tint)
243 .keyframeAnimator(initialValue: 1.0, trigger: pulse) { glyph, scale in
244 glyph.scaleEffect(reduceMotion ? 1 : scale)
245 } keyframes: { _ in
246 SpringKeyframe(1.28, duration: 0.16, spring: .snappy)
247 SpringKeyframe(1.0, duration: 0.5, spring: .bouncy(extraBounce: 0.2))
248 }
249 Tick(done: done, size: 22, tint: tint, ripples: false)
250 .background { Circle().fill(theme.canvas).padding(-2).opacity(done ? 1 : 0) }
251 .opacity(done ? 1 : 0)
252 .offset(x: size * 0.34, y: size * 0.34)
253 }
254 .frame(width: size, height: size)
255 }
256}