@@ -130,6 +130,10 @@ struct ThemedBackground: View {
130130 @Environment ( \. accessibilityReduceMotion) private var reduceMotion
131131 @Environment ( \. colorScheme) private var colorScheme
132132
133+ private var identity : String {
134+ style. identityKey
135+ }
136+
133137 var body : some View {
134138 Group {
135139 switch style {
@@ -166,6 +170,7 @@ struct ThemedBackground: View {
166170 . ignoresSafeArea ( )
167171 }
168172 }
173+ . id ( identity)
169174 }
170175}
171176
@@ -329,8 +334,15 @@ private struct ParticleFieldBackground: View {
329334 }
330335 }
331336 . task {
332- while true {
333- try ? await Task . sleep ( nanoseconds: 16_000_000 )
337+ while !Task. isCancelled {
338+ do {
339+ try await Task . sleep ( nanoseconds: 16_000_000 )
340+ } catch is CancellationError {
341+ break
342+ } catch {
343+ break
344+ }
345+ guard !Task. isCancelled else { break }
334346 var next = particles
335347 for i in next. indices {
336348 var p = next [ i]
@@ -359,4 +371,37 @@ private extension Array where Element == Color {
359371 if count == 1 { return [ self [ 0 ] , self [ 0 ] . opacity ( 0.6 ) ] }
360372 return self
361373 }
374+
375+ var identityKey : String {
376+ map { $0. identityKey } . joined ( separator: " , " )
377+ }
378+ }
379+
380+ private extension BackgroundStyle {
381+ var identityKey : String {
382+ switch self {
383+ case . staticGradient( let colors) :
384+ return " static: \( colors. identityKey) "
385+ case . animatedGradient( let colors, let speed) :
386+ return " animated: \( String ( format: " %.4f " , speed) ) : \( colors. identityKey) "
387+ case . blobs( let colors, let background) :
388+ return " blobs: \( colors. identityKey) |bg: \( background. identityKey) "
389+ case . particles( let particle, let background) :
390+ return " particles: \( particle. identityKey) |bg: \( background. identityKey) "
391+ case . customGradient( let colors) :
392+ return " custom: \( colors. identityKey) "
393+ case . adaptiveGradient( let light, let dark) :
394+ return " adaptive:l= \( light. identityKey) |d= \( dark. identityKey) "
395+ }
396+ }
397+ }
398+
399+ private extension Color {
400+ var identityKey : String {
401+ if let hex = toHex ( ) {
402+ return hex
403+ }
404+ // Fallback to descriptive string when hex can't be produced (e.g. dynamic colors)
405+ return String ( describing: self )
406+ }
362407}
0 commit comments