-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Adds support for MeshGradient theming #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,94 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import SwiftUI | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nonisolated extension MeshGradient { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public init( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| width: Int, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| height: Int, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| colors: [Color] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.init( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| width: width, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| height: height, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| points: MeshGradient.pointsFrom(width: width, height: height), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| colors: colors, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // MARK: - Codable | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nonisolated extension MeshGradient: @retroactive Codable { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| enum CodingKeys: String, CodingKey { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case width, height, colors, points | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public init(from decoder: Decoder) throws { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let container = try decoder.container(keyedBy: CodingKeys.self) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let width = try container.decode(Int.self, forKey: .width) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let height = try container.decode(Int.self, forKey: .height) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let colors = try container.decode([Color].self, forKey: .colors) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let points = try container.decodeIfPresent([SIMD2<Float>].self, forKey: .points) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.init( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| width: width, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| height: height, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| points: points ?? MeshGradient.pointsFrom(width: width, height: height), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+32
to
+36
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let points = try container.decodeIfPresent([SIMD2<Float>].self, forKey: .points) | |
| self.init( | |
| width: width, | |
| height: height, | |
| points: points ?? MeshGradient.pointsFrom(width: width, height: height), | |
| let decodedPoints = try container.decodeIfPresent([SIMD2<Float>].self, forKey: .points) | |
| let resolvedPoints: [SIMD2<Float>] | |
| if let decodedPoints { | |
| let expectedCount = width * height | |
| guard decodedPoints.count == expectedCount else { | |
| throw DecodingError.dataCorruptedError( | |
| forKey: .points, | |
| in: container, | |
| debugDescription: "Expected \(expectedCount) points for mesh of size \(width)x\(height), but found \(decodedPoints.count)." | |
| ) | |
| } | |
| resolvedPoints = decodedPoints | |
| } else { | |
| resolvedPoints = MeshGradient.pointsFrom(width: width, height: height) | |
| } | |
| self.init( | |
| width: width, | |
| height: height, | |
| points: resolvedPoints, |
Copilot
AI
Feb 24, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no validation to ensure the number of colors matches the expected count (width × height). The MeshGradient API requires exactly width × height colors for a valid mesh, but the decoding logic doesn't verify this constraint. If a mismatch occurs, the SwiftUI MeshGradient initializer may fail or produce unexpected behavior. Consider adding validation to throw a DecodingError if colors.count != width × height.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No validation exists for negative or zero width/height values. The decoding logic should validate that both width and height are positive integers (at least 2 for proper mesh gradient generation given the current pointsFrom implementation). Consider adding validation that throws a DecodingError for invalid dimensions.