diff --git a/Package.resolved b/Package.resolved
index 861a0fc4f..a21107159 100644
--- a/Package.resolved
+++ b/Package.resolved
@@ -7,7 +7,7 @@
"location" : "https://github.com/OpenSwiftUIProject/DarwinPrivateFrameworks.git",
"state" : {
"branch" : "main",
- "revision" : "37ca0a00e96e6056c22f81ff2fcdfb3ec30da14d"
+ "revision" : "a0ac60f4d56a9b50fb56c79113497d93fb166e1c"
}
},
{
@@ -43,7 +43,7 @@
"location" : "https://github.com/OpenSwiftUIProject/OpenRenderBox",
"state" : {
"branch" : "main",
- "revision" : "f267bd792e387bff9e88940f55de0dab7c6e7ac0"
+ "revision" : "5f2ba682c0390124aa8a80f3ce5ea7d305ccce48"
}
},
{
diff --git a/Sources/OpenSwiftUICore/Graphic/Color/Paint.swift b/Sources/OpenSwiftUICore/Graphic/Color/Paint.swift
index 9731c0aeb..2b4e89770 100644
--- a/Sources/OpenSwiftUICore/Graphic/Color/Paint.swift
+++ b/Sources/OpenSwiftUICore/Graphic/Color/Paint.swift
@@ -3,7 +3,7 @@
// OpenSwiftUICore
//
// Audited for 6.5.4
-// Status: Complete (Blocked by ImagePaint, Shader, and MeshGradient)
+// Status: Complete (Blocked by ImagePaint and Shader)
// ID: 1A0B37DF690543DA63A8C3A51910F39C (SwiftUICore)
package import Foundation
@@ -190,7 +190,7 @@ package protocol ResolvedPaintVisitor {
mutating func visitPaint
(_ paint: P) where P: ResolvedPaint
}
-// MARK: - CodableResolvedPaint [WIP: ImagePaint, Shader, MeshGradient]
+// MARK: - CodableResolvedPaint [WIP: ImagePaint, Shader]
package struct CodableResolvedPaint: ProtobufMessage {
package struct Tag: Equatable, ProtobufTag {
@@ -250,8 +250,8 @@ package struct CodableResolvedPaint: ProtobufMessage {
// TODO: Decode Shader.ResolvedShader.
_openSwiftUIUnimplementedFailure()
case Tag.meshGradient.rawValue:
- // TODO: Decode MeshGradient._Paint.
- _openSwiftUIUnimplementedFailure()
+ let paint: MeshGradient._Paint = try decoder.messageField(field)
+ base = _AnyResolvedPaint(paint)
default:
try decoder.skipField(field)
}
diff --git a/Sources/OpenSwiftUICore/Graphic/Gradient/MeshGradient.swift b/Sources/OpenSwiftUICore/Graphic/Gradient/MeshGradient.swift
new file mode 100644
index 000000000..1e1d8c55a
--- /dev/null
+++ b/Sources/OpenSwiftUICore/Graphic/Gradient/MeshGradient.swift
@@ -0,0 +1,569 @@
+//
+// MeshGradient.swift
+// OpenSwiftUICore
+//
+// Audited for 6.5.4
+// Status: Complete
+
+package import Foundation
+package import OpenRenderBoxShims
+
+// MARK: - MeshGradient
+
+/// A two-dimensional gradient defined by a grid of colored vertices.
+///
+/// Each vertex has a position and four Bézier control points. The control
+/// points define the tangents toward its leading, top, trailing, and bottom
+/// neighbors. Vertices at the boundary use only the control points for their
+/// existing neighbors. You can supply the control points or let the mesh infer
+/// them from neighboring positions.
+///
+/// The renderer forms Bézier patches between vertices and interpolates the
+/// colors across each patch. Use ``smoothsColors`` to control whether color
+/// interpolation is cubic or linear.
+///
+/// MeshGradient(width: 3, height: 3, points: [
+/// .init(0, 0), .init(0.5, 0), .init(1, 0),
+/// .init(0, 0.5), .init(0.5, 0.5), .init(1, 0.5),
+/// .init(0, 1), .init(0.5, 1), .init(1, 1)
+/// ], colors: [
+/// .red, .purple, .indigo,
+/// .orange, .white, .blue,
+/// .yellow, .green, .mint
+/// ])
+@available(OpenSwiftUI_v6_0, *)
+nonisolated public struct MeshGradient: UnaryView, Paint, Equatable, Sendable {
+ /// The geometry of the vertices in a mesh gradient.
+ public enum Locations: Equatable, Sendable {
+ /// Vertex positions with control points inferred from their neighbors.
+ case points([SIMD2])
+
+ /// Vertex positions with explicit Bézier control points.
+ case bezierPoints([BezierPoint])
+ }
+
+ /// The colors of the vertices in a mesh gradient.
+ public enum Colors: Equatable, Sendable {
+ /// Vertex colors that resolve in the environment.
+ case colors([Color])
+
+ /// Vertex colors that have already been resolved to sRGB.
+ case resolvedColors([Color.Resolved])
+ }
+
+ /// A vertex position and its four surrounding Bézier control points.
+ @frozen
+ public struct BezierPoint: Equatable, Sendable {
+ /// The position of the vertex.
+ public var position: SIMD2
+
+ /// The Bézier control point for the vertex's leading edge.
+ public var leadingControlPoint: SIMD2
+
+ /// The Bézier control point for the vertex's top edge.
+ public var topControlPoint: SIMD2
+
+ /// The Bézier control point for the vertex's trailing edge.
+ public var trailingControlPoint: SIMD2
+
+ /// The Bézier control point for the vertex's bottom edge.
+ public var bottomControlPoint: SIMD2
+
+ /// Creates a vertex with explicit Bézier control points.
+ ///
+ /// - Parameters:
+ /// - position: The position of the vertex in the gradient's
+ /// coordinate space.
+ /// - leadingControlPoint: The control point for the leading edge.
+ /// - topControlPoint: The control point for the top edge.
+ /// - trailingControlPoint: The control point for the trailing edge.
+ /// - bottomControlPoint: The control point for the bottom edge.
+ @_alwaysEmitIntoClient
+ public init(position: SIMD2, leadingControlPoint: SIMD2, topControlPoint: SIMD2,
+ trailingControlPoint: SIMD2, bottomControlPoint: SIMD2) {
+ self.position = position
+ self.leadingControlPoint = leadingControlPoint
+ self.topControlPoint = topControlPoint
+ self.trailingControlPoint = trailingControlPoint
+ self.bottomControlPoint = bottomControlPoint
+ }
+ }
+
+ /// The number of vertices per row.
+ public var width: Int
+
+ /// The number of vertices per column.
+ public var height: Int
+
+ /// The vertex locations. The array must contain `width * height` elements.
+ public var locations: Locations
+
+ /// The vertex colors. The array must contain `width * height` elements.
+ public var colors: Colors
+
+ /// The color that fills the area outside the mesh.
+ public var background: Color
+
+ /// Whether to use cubic interpolation for the vertex colors.
+ public var smoothsColors: Bool
+
+ /// The color space in which to interpolate vertex colors.
+ public var colorSpace: Gradient.ColorSpace
+
+ /// Creates a mesh gradient from a grid of colored vertices.
+ ///
+ /// - Parameters:
+ /// - width: The number of vertices per row.
+ /// - height: The number of vertices per column.
+ /// - locations: The `width * height` vertex locations.
+ /// - colors: The `width * height` vertex colors.
+ /// - background: The color that fills the area outside the mesh.
+ /// - smoothsColors: Whether to use cubic interpolation for vertex colors.
+ /// - colorSpace: The color space in which to interpolate vertex colors.
+ public init(
+ width: Int,
+ height: Int,
+ locations: Locations,
+ colors: Colors,
+ background: Color = .clear,
+ smoothsColors: Bool = true,
+ colorSpace: Gradient.ColorSpace = .device
+ ) {
+ self.width = width
+ self.height = height
+ self.locations = locations
+ self.colors = colors
+ self.background = background
+ self.smoothsColors = smoothsColors
+ self.colorSpace = colorSpace
+ }
+
+ /// Creates a mesh gradient with control points inferred from vertex positions.
+ ///
+ /// - Parameters:
+ /// - width: The number of vertices per row.
+ /// - height: The number of vertices per column.
+ /// - points: The `width * height` vertex positions.
+ /// - colors: The `width * height` vertex colors.
+ /// - background: The color that fills the area outside the mesh.
+ /// - smoothsColors: Whether to use cubic interpolation for vertex colors.
+ /// - colorSpace: The color space in which to interpolate vertex colors.
+ @_alwaysEmitIntoClient
+ public init(
+ width: Int,
+ height: Int,
+ points: [SIMD2],
+ colors: [Color],
+ background: Color = .clear,
+ smoothsColors: Bool = true,
+ colorSpace: Gradient.ColorSpace = .device
+ ) {
+ self.init(
+ width: width,
+ height: height,
+ locations: .points(points),
+ colors: .colors(colors),
+ background: background,
+ smoothsColors: smoothsColors,
+ colorSpace: colorSpace
+ )
+ }
+
+ /// Creates a mesh gradient with inferred control points and resolved sRGB
+ /// colors.
+ ///
+ /// - Parameters:
+ /// - width: The number of vertices per row.
+ /// - height: The number of vertices per column.
+ /// - points: The `width * height` vertex positions.
+ /// - resolvedColors: The `width * height` resolved vertex colors.
+ /// - background: The color that fills the area outside the mesh.
+ /// - smoothsColors: Whether to use cubic interpolation for vertex colors.
+ /// - colorSpace: The color space in which to interpolate vertex colors.
+ @_alwaysEmitIntoClient
+ public init(
+ width: Int,
+ height: Int,
+ points: [SIMD2],
+ resolvedColors: [Color.Resolved],
+ background: Color = .clear,
+ smoothsColors: Bool = true,
+ colorSpace: Gradient.ColorSpace = .device
+ ) {
+ self.init(
+ width: width,
+ height: height,
+ locations: .points(points),
+ colors: .resolvedColors(resolvedColors),
+ background: background,
+ smoothsColors: smoothsColors,
+ colorSpace: colorSpace
+ )
+ }
+
+ /// Creates a mesh gradient with explicit Bézier control points.
+ ///
+ /// - Parameters:
+ /// - width: The number of vertices per row.
+ /// - height: The number of vertices per column.
+ /// - bezierPoints: The `width * height` vertices with their control points.
+ /// - colors: The `width * height` vertex colors.
+ /// - background: The color that fills the area outside the mesh.
+ /// - smoothsColors: Whether to use cubic interpolation for vertex colors.
+ /// - colorSpace: The color space in which to interpolate vertex colors.
+ @_alwaysEmitIntoClient
+ public init(
+ width: Int,
+ height: Int,
+ bezierPoints: [BezierPoint],
+ colors: [Color],
+ background: Color = .clear,
+ smoothsColors: Bool = true,
+ colorSpace: Gradient.ColorSpace = .device
+ ) {
+ self.init(
+ width: width,
+ height: height,
+ locations: .bezierPoints(bezierPoints),
+ colors: .colors(colors),
+ background: background,
+ smoothsColors: smoothsColors,
+ colorSpace: colorSpace
+ )
+ }
+
+ /// Creates a mesh gradient with explicit Bézier control points and resolved
+ /// sRGB colors.
+ ///
+ /// - Parameters:
+ /// - width: The number of vertices per row.
+ /// - height: The number of vertices per column.
+ /// - bezierPoints: The `width * height` vertices with their control points.
+ /// - resolvedColors: The `width * height` resolved vertex colors.
+ /// - background: The color that fills the area outside the mesh.
+ /// - smoothsColors: Whether to use cubic interpolation for vertex colors.
+ /// - colorSpace: The color space in which to interpolate vertex colors.
+ @_alwaysEmitIntoClient
+ public init(
+ width: Int,
+ height: Int,
+ bezierPoints: [BezierPoint],
+ resolvedColors: [Color.Resolved],
+ background: Color = .clear,
+ smoothsColors: Bool = true,
+ colorSpace: Gradient.ColorSpace = .device
+ ) {
+ self.init(
+ width: width,
+ height: height,
+ locations: .bezierPoints(bezierPoints),
+ colors: .resolvedColors(resolvedColors),
+ background: background,
+ smoothsColors: smoothsColors,
+ colorSpace: colorSpace
+ )
+ }
+
+ package struct _Paint {
+ package var locations: Locations
+ package var colors: [Color.Resolved]
+ package var background: Color.Resolved
+ package var width: Int
+ package var height: Int
+ package var flags: ORBMeshGradientFlags
+
+ package init(
+ locations: Locations,
+ colors: [Color.Resolved],
+ background: Color.Resolved,
+ width: Int,
+ height: Int,
+ flags: ORBMeshGradientFlags
+ ) {
+ self.locations = locations
+ self.colors = colors
+ self.background = background
+ self.width = width
+ self.height = height
+ self.flags = flags
+ }
+ }
+
+ package func resolvePaint(in environment: EnvironmentValues) -> _Paint {
+ let count = width * height
+ let resolvedColors: [Color.Resolved]
+ switch colors {
+ case let .colors(colors):
+ var resolved = Array(repeating: Color.Resolved.clear, count: count)
+ resolved.withUnsafeMutableBufferPointer { buffer in
+ for (index, color) in colors.prefix(count).enumerated() {
+ buffer[index] = color.resolve(in: environment)
+ }
+ }
+ resolvedColors = resolved
+ case let .resolvedColors(colors):
+ resolvedColors = colors
+ }
+ let resolvedBackground = background.resolve(in: environment)
+ var flags: ORBMeshGradientFlags
+ switch colorSpace.base {
+ case .device: flags = []
+ case .linear: flags = .linearColor
+ case .perceptual: flags = .perceptualColor
+ }
+ if smoothsColors {
+ flags.insert(.smoothsColors)
+ }
+ return _Paint(
+ locations: locations,
+ colors: resolvedColors,
+ background: resolvedBackground,
+ width: width,
+ height: height,
+ flags: flags
+ )
+ }
+
+ package func withResolvedColors(
+ in environment: EnvironmentValues,
+ _ body: (UnsafeBufferPointer) -> Void
+ ) {
+ switch colors {
+ case let .colors(colors):
+ let count = width * height
+ withUnsafeTemporaryAllocation(of: Color.Resolved.self, capacity: count) { buffer in
+ for (index, color) in colors.prefix(count).enumerated() {
+ buffer[index] = color.resolve(in: environment)
+ }
+ body(UnsafeBufferPointer(buffer))
+ }
+ case let .resolvedColors(colors):
+ colors.withUnsafeBufferPointer(body)
+ }
+ }
+
+ package func fallbackColor(in environment: EnvironmentValues) -> Color? {
+ background
+ }
+}
+
+// MARK: - MeshGradient._Paint
+
+extension MeshGradient._Paint: ResolvedPaint {
+ package func draw(path: Path, style: PathDrawingStyle, in context: GraphicsContext, bounds: CGRect?) {
+ context.draw(path, with: .meshGradient(self, bounds ?? path.boundingRect), style: style)
+ }
+
+ package var isClear: Bool {
+ background.opacity == 0 && colors.allSatisfy { $0.opacity == 0 }
+ }
+
+ package var isOpaque: Bool {
+ background.opacity == 1 && colors.allSatisfy { $0.opacity == 1 }
+ }
+
+ package var isCALayerCompatible: Bool { false }
+
+ package typealias AnimatableData = AnimatablePair<
+ AnimatableArray,
+ AnimatablePair, Color.Resolved.AnimatableData>
+ >
+
+ package var animatableData: AnimatableData {
+ get { AnimatablePair(locations.animatableData, AnimatablePair(colors.animatableData, background.animatableData)) }
+ set {
+ colors.animatableData = newValue.second.first
+ locations.animatableData = newValue.first
+ background.animatableData = newValue.second.second
+ }
+ }
+
+ package static var leafProtobufTag: CodableResolvedPaint.Tag? { .meshGradient }
+}
+
+// MARK: - MeshGradient._Paint + ProtobufMessage
+
+extension MeshGradient._Paint: ProtobufMessage {
+ package func encodePaint(to encoder: inout ProtobufEncoder) throws {
+ if encoder.archiveOptions.deploymentVersion < .v6 {
+ try encoder.messageField(CodableResolvedPaint.Tag.color, background)
+ } else {
+ try encoder.messageField(CodableResolvedPaint.Tag.meshGradient, self)
+ }
+ }
+
+ package func encode(to encoder: inout ProtobufEncoder) throws {
+ try encoder.messageField(1, locations)
+ for color in colors {
+ try encoder.messageField(2, color)
+ }
+ try encoder.messageField(3, background, defaultValue: .clear)
+ if width > 0 {
+ encoder.uintField(4, UInt(width))
+ }
+ if height > 0 {
+ encoder.uintField(5, UInt(height))
+ }
+ encoder.uintField(6, UInt(flags.rawValue))
+ }
+
+ package init(from decoder: inout ProtobufDecoder) throws {
+ self.init(locations: .points([]), colors: [], background: .clear, width: 0, height: 0, flags: [])
+ while let field = try decoder.nextField() {
+ switch field.tag {
+ case 1:
+ locations = try decoder.messageField(field)
+ case 2:
+ colors.append(try decoder.messageField(field))
+ case 3:
+ background = try decoder.messageField(field)
+ case 4:
+ if let value = Int(exactly: try decoder.uintField(field)) {
+ width = value
+ }
+ case 5:
+ if let value = Int(exactly: try decoder.uintField(field)) {
+ height = value
+ }
+ case 6:
+ flags = .init(rawValue: try decoder.uint32Field(field))
+ default:
+ try decoder.skipField(field)
+ }
+ }
+ }
+}
+
+// MARK: - MeshGradient.Locations
+
+extension MeshGradient.Locations {
+ fileprivate var animatableData: AnimatableArray {
+ get {
+ var values: [Float] = []
+ switch self {
+ case let .points(points):
+ values.reserveCapacity(points.count * 2)
+ for point in points {
+ values.append(point.x)
+ values.append(point.y)
+ }
+ case let .bezierPoints(points):
+ values.reserveCapacity(points.count * 10)
+ for point in points {
+ values.append(point.position.x)
+ values.append(point.position.y)
+ values.append(point.leadingControlPoint.x)
+ values.append(point.leadingControlPoint.y)
+ values.append(point.topControlPoint.x)
+ values.append(point.topControlPoint.y)
+ values.append(point.trailingControlPoint.x)
+ values.append(point.trailingControlPoint.y)
+ values.append(point.bottomControlPoint.x)
+ values.append(point.bottomControlPoint.y)
+ }
+ }
+ return AnimatableArray(values)
+ }
+ set {
+ let values = newValue.elements
+ switch self {
+ case var .points(points):
+ guard values.count == points.count * 2 else { return }
+ for index in points.indices {
+ points[index].x = values[index * 2]
+ points[index].y = values[index * 2 + 1]
+ }
+ self = .points(points)
+ case var .bezierPoints(points):
+ guard values.count == points.count * 10 else { return }
+ for index in points.indices {
+ let offset = index * 10
+ points[index].position.x = values[offset]
+ points[index].position.y = values[offset + 1]
+ points[index].leadingControlPoint.x = values[offset + 2]
+ points[index].leadingControlPoint.y = values[offset + 3]
+ points[index].topControlPoint.x = values[offset + 4]
+ points[index].topControlPoint.y = values[offset + 5]
+ points[index].trailingControlPoint.x = values[offset + 6]
+ points[index].trailingControlPoint.y = values[offset + 7]
+ points[index].bottomControlPoint.x = values[offset + 8]
+ points[index].bottomControlPoint.y = values[offset + 9]
+ }
+ self = .bezierPoints(points)
+ }
+ }
+ }
+}
+
+// MARK: - MeshGradient.Locations + ProtobufMessage
+
+extension MeshGradient.Locations: ProtobufMessage {
+ package func encode(to encoder: inout ProtobufEncoder) throws {
+ switch self {
+ case let .points(points):
+ for point in points {
+ encoder.messageField(1) { encoder in
+ encoder.floatField(1, point.x)
+ encoder.floatField(2, point.y)
+ }
+ }
+ case let .bezierPoints(points):
+ for point in points {
+ encoder.messageField(2) { encoder in
+ withUnsafePointer(to: point) { pointer in
+ let components = UnsafeRawPointer(pointer).assumingMemoryBound(to: Float.self)
+ for index in 0 ..< 10 {
+ encoder.floatField(UInt(index + 1), components[index])
+ }
+ }
+ }
+ }
+ }
+ }
+
+ package init(from decoder: inout ProtobufDecoder) throws {
+ var points: [SIMD2] = []
+ var bezierPoints: [MeshGradient.BezierPoint] = []
+ while let field = try decoder.nextField() {
+ switch field.tag {
+ case 1:
+ let point: SIMD2 = try decoder.messageField(field) { decoder in
+ var point = SIMD2.zero
+ while let field = try decoder.nextField() {
+ switch field.tag {
+ case 1: point.x = try decoder.floatField(field)
+ case 2: point.y = try decoder.floatField(field)
+ default: try decoder.skipField(field)
+ }
+ }
+ return point
+ }
+ points.append(point)
+ case 2:
+ let point: MeshGradient.BezierPoint = try decoder.messageField(field) { decoder in
+ var point = MeshGradient.BezierPoint(
+ position: .zero,
+ leadingControlPoint: .zero,
+ topControlPoint: .zero,
+ trailingControlPoint: .zero,
+ bottomControlPoint: .zero
+ )
+ try withUnsafeMutablePointer(to: &point) { pointer in
+ let components = UnsafeMutableRawPointer(pointer).assumingMemoryBound(to: Float.self)
+ while let field = try decoder.nextField() {
+ switch field.tag {
+ case 1 ... 10: components[Int(field.tag) - 1] = try decoder.floatField(field)
+ default: try decoder.skipField(field)
+ }
+ }
+ }
+ return point
+ }
+ bezierPoints.append(point)
+ default:
+ try decoder.skipField(field)
+ }
+ }
+ self = bezierPoints.isEmpty ? .points(points) : .bezierPoints(bezierPoints)
+ }
+}
diff --git a/Sources/OpenSwiftUICore/Graphic/GraphicsContext.swift b/Sources/OpenSwiftUICore/Graphic/GraphicsContext.swift
index 3e648e44b..3a78c9e26 100644
--- a/Sources/OpenSwiftUICore/Graphic/GraphicsContext.swift
+++ b/Sources/OpenSwiftUICore/Graphic/GraphicsContext.swift
@@ -514,7 +514,7 @@ public struct GraphicsContext {
case foreground
case style(AnyShapeStyle)
case gradient(EitherGradient, geometry: GradientGeometry, options: GradientOptions)
- // case meshGradient(MeshGradient)
+ case meshGradient(MeshGradient)
case tiledImage(Image, origin: CGPoint = .zero, sourceRect: CGRect = CGRect(x: 0, y: 0, width: 1, height: 1), scale: CGFloat = 1)
case levels([Shading])
indirect case resolved(ResolvedShading)
@@ -528,6 +528,14 @@ public struct GraphicsContext {
// TODO
+ /// Creates a shading that fills with a mesh gradient.
+ ///
+ /// - Parameter mesh: The mesh gradient that supplies the fill colors.
+ @available(OpenSwiftUI_v6_0, *)
+ public static func meshGradient(_ mesh: MeshGradient) -> Shading {
+ Shading(storage: .meshGradient(mesh))
+ }
+
public static func linearGradient(
_ gradient: Gradient,
startPoint: CGPoint,
@@ -694,7 +702,7 @@ public struct GraphicsContext {
case shader(Shader.ResolvedShader, CGRect)
case style(_ShapeStyle_Pack.Style)
case gradient(ResolvedGradient, GradientGeometry, _: GradientOptions = .init())
- // case meshGradient(MeshGradient._Paint, CGRect)
+ case meshGradient(MeshGradient._Paint, CGRect)
case tiledImage(GraphicsImage, origin: CGPoint = .zero, sourceRect: CGRect = CGRect(x: 0, y: 0, width: 1, height: 1), scale: CGFloat = 1)
case levels([GraphicsContext.ResolvedShading])
}
diff --git a/Sources/OpenSwiftUICore/Shape/ShapeStyle/ShapeStylePack.swift b/Sources/OpenSwiftUICore/Shape/ShapeStyle/ShapeStylePack.swift
index e369a07e0..2d3bff3f5 100644
--- a/Sources/OpenSwiftUICore/Shape/ShapeStyle/ShapeStylePack.swift
+++ b/Sources/OpenSwiftUICore/Shape/ShapeStyle/ShapeStylePack.swift
@@ -3,7 +3,7 @@
// OpenSwiftUICore
//
// Audited for 6.5.4
-// Status: Complete - Blocked by MeshGradient and Shader
+// Status: Complete - Blocked by Shader
// ID: 4DBF651155A4B32ED86C55EAB1B96C61 (SwiftUICore)
package import Foundation
@@ -497,8 +497,8 @@ extension _ShapeStyle_Pack.Fill: Animatable {
case radialGradient(RadialGradient._Paint.AnimatableData)
case ellipticalGradient(EllipticalGradient._Paint.AnimatableData)
case angularGradient(AngularGradient._Paint.AnimatableData)
- // TODO: MeshGradient + Shader
- // case meshGradient(MeshGradient._Paint.AnimatableData)
+ case meshGradient(MeshGradient._Paint.AnimatableData)
+ // TODO: Shader
// case shader(Shader.ResolvedShader.AnimatableData)
case colorMatrix(_ColorMatrix)
@@ -555,7 +555,11 @@ extension _ShapeStyle_Pack.Fill: Animatable {
var visitor = PaintSetVisitor(data: data, result: .color(.clear))
anyPaint.visit(&visitor)
fill = visitor.result
- // TODO: MeshGradient + Shader
+ case let .meshGradient(data):
+ var visitor = PaintSetVisitor(data: data, result: .color(.clear))
+ anyPaint.visit(&visitor)
+ fill = visitor.result
+ // TODO: Shader
default:
break
}
@@ -602,6 +606,8 @@ extension _ShapeStyle_Pack.Fill: Animatable {
lhs = .ellipticalGradient(lhsData + rhsData)
case let (.angularGradient(lhsData), .angularGradient(rhsData)):
lhs = .angularGradient(lhsData + rhsData)
+ case let (.meshGradient(lhsData), .meshGradient(rhsData)):
+ lhs = .meshGradient(lhsData + rhsData)
case (.colorMatrix(var lhsData), .colorMatrix(let rhsData)):
lhsData.add(rhsData)
lhs = .colorMatrix(lhsData)
@@ -629,6 +635,8 @@ extension _ShapeStyle_Pack.Fill: Animatable {
lhs = .ellipticalGradient(lhsData - rhsData)
case let (.angularGradient(lhsData), .angularGradient(rhsData)):
lhs = .angularGradient(lhsData - rhsData)
+ case let (.meshGradient(lhsData), .meshGradient(rhsData)):
+ lhs = .meshGradient(lhsData - rhsData)
case (.colorMatrix(var lhsData), .colorMatrix(let rhsData)):
lhsData.subtract(rhsData)
lhs = .colorMatrix(lhsData)
@@ -647,6 +655,8 @@ extension _ShapeStyle_Pack.Fill: Animatable {
self = .ellipticalGradient(.zero - data)
case let .angularGradient(data):
self = .angularGradient(.zero - data)
+ case let .meshGradient(data):
+ self = .meshGradient(.zero - data)
case var .colorMatrix(data):
data.negate()
self = .colorMatrix(data)
@@ -692,6 +702,9 @@ extension _ShapeStyle_Pack.Fill: Animatable {
case var .angularGradient(data):
data.scale(by: rhs)
self = .angularGradient(data)
+ case var .meshGradient(data):
+ data.scale(by: rhs)
+ self = .meshGradient(data)
case var .colorMatrix(data):
data.scale(by: rhs)
self = .colorMatrix(data)
@@ -707,6 +720,7 @@ extension _ShapeStyle_Pack.Fill: Animatable {
case let .radialGradient(data): return data.magnitudeSquared
case let .ellipticalGradient(data): return data.magnitudeSquared
case let .angularGradient(data): return data.magnitudeSquared
+ case let .meshGradient(data): return data.magnitudeSquared
case let .colorMatrix(data): return data.magnitudeSquared
}
}
@@ -720,6 +734,7 @@ extension _ShapeStyle_Pack.Fill: Animatable {
case let (.radialGradient(lhs), .radialGradient(rhs)): return lhs == rhs
case let (.ellipticalGradient(lhs), .ellipticalGradient(rhs)): return lhs == rhs
case let (.angularGradient(lhs), .angularGradient(rhs)): return lhs == rhs
+ case let (.meshGradient(lhs), .meshGradient(rhs)): return lhs == rhs
case let (.colorMatrix(lhs), .colorMatrix(rhs)): return lhs == rhs
default: return false
}
@@ -751,8 +766,10 @@ extension _ShapeStyle_Pack.Fill: Animatable {
result.pointee = .ellipticalGradient(gradient.animatableData)
} else if let gradient = paint as? AngularGradient._Paint {
result.pointee = .angularGradient(gradient.animatableData)
+ } else if let gradient = paint as? MeshGradient._Paint {
+ result.pointee = .meshGradient(gradient.animatableData)
}
- // TODO: MeshGradient + Shader
+ // TODO: Shader
}
}
}
diff --git a/Tests/OpenSwiftUICoreTests/Graphics/MeshGradientTests.swift b/Tests/OpenSwiftUICoreTests/Graphics/MeshGradientTests.swift
new file mode 100644
index 000000000..b8678a5d2
--- /dev/null
+++ b/Tests/OpenSwiftUICoreTests/Graphics/MeshGradientTests.swift
@@ -0,0 +1,139 @@
+//
+// MeshGradientTests.swift
+// OpenSwiftUICoreTests
+
+@testable import OpenSwiftUICore
+import Testing
+
+struct MeshGradientTests {
+ @Test
+ @MainActor
+ func shapeStyleResolvesMeshAndFallsBackToBackground() throws {
+ let mesh = MeshGradient(
+ width: 2,
+ height: 2,
+ points: [[0, 0], [1, 0], [0, 1], [1, 1]],
+ colors: [.red, .red, .blue, .blue],
+ background: .green,
+ colorSpace: .perceptual
+ )
+ let style: any ShapeStyle = mesh
+ let environment = EnvironmentValues()
+ var shape = _ShapeStyle_Shape(operation: .resolveStyle(name: .foreground, levels: 0..<1))
+ style._apply(to: &shape)
+
+ guard case let .paint(base) = shape.stylePack[.foreground, 0].fill else {
+ Issue.record("Expected a mesh paint")
+ return
+ }
+ let paint = try #require(base.as(type: MeshGradient._Paint.self))
+ #expect(paint.flags.rawValue == 0x13)
+ #expect(paint.isOpaque)
+ #expect(!paint.isCALayerCompatible)
+ mesh.withResolvedColors(in: environment) { colors in
+ #expect(Array(colors) == paint.colors)
+ }
+
+ shape = _ShapeStyle_Shape(operation: .fallbackColor(level: 0))
+ style._apply(to: &shape)
+ guard case let .color(color) = shape.result else {
+ Issue.record("Expected the background color")
+ return
+ }
+ #expect(color == .green)
+ }
+
+ @Test
+ func fillAnimationInterpolatesPositionsAndOpacity() throws {
+ let start = MeshGradient._Paint(
+ locations: .points([[0, 0], [1, 0]]),
+ colors: [.clear, .clear],
+ background: .clear,
+ width: 2,
+ height: 1,
+ flags: .smoothsColors
+ )
+ #expect(start.isClear)
+ var end = start
+ end.locations = .points([[0, 0], [1, 1]])
+ end.colors[1].opacity = 1
+ end.background.opacity = 1
+
+ var fill = _ShapeStyle_Pack.Fill.paint(_AnyResolvedPaint(start))
+ let endFill = _ShapeStyle_Pack.Fill.paint(_AnyResolvedPaint(end))
+ var delta = endFill.animatableData - fill.animatableData
+ delta.scale(by: 0.5)
+ fill.animatableData += delta
+
+ guard case let .paint(base) = fill else {
+ Issue.record("Expected the interpolated mesh paint")
+ return
+ }
+ let paint = try #require(base.as(type: MeshGradient._Paint.self))
+ #expect(paint.locations == .points([[0, 0], [1, 0.5]]))
+ #expect(paint.colors[1].opacity == 0.5)
+ #expect(paint.background.opacity == 0.5)
+ #expect(paint.flags == .smoothsColors)
+ #expect(!paint.isOpaque)
+ #expect(!paint.isClear)
+ }
+
+ @Test
+ func bezierAnimationPreservesLocationShape() {
+ let point = MeshGradient.BezierPoint(
+ position: [0, 1],
+ leadingControlPoint: [2, 3],
+ topControlPoint: [4, 5],
+ trailingControlPoint: [6, 7],
+ bottomControlPoint: [8, 9]
+ )
+ var paint = MeshGradient._Paint(
+ locations: .bezierPoints([point]), colors: [], background: .clear, width: 1, height: 1, flags: []
+ )
+ var data = paint.animatableData
+ #expect(data.first.elements == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
+
+ data.first.elements.removeLast()
+ paint.animatableData = data
+ #expect(paint.locations == .bezierPoints([point]))
+
+ data.first.elements.append(10)
+ paint.animatableData = data
+ var expected = point
+ expected.bottomControlPoint.y = 10
+ #expect(paint.locations == .bezierPoints([expected]))
+ }
+
+ @Test(arguments: [ArchivedViewInput.DeploymentVersion.v5, .v6])
+ func archiveUsesPaintSupportedByDeploymentVersion(version: ArchivedViewInput.DeploymentVersion) throws {
+ let background = Color.Resolved(linearRed: 1, linearGreen: 0, linearBlue: 0, opacity: 1)
+ let point = MeshGradient.BezierPoint(
+ position: [0, 1],
+ leadingControlPoint: [2, 3],
+ topControlPoint: [4, 5],
+ trailingControlPoint: [6, 7],
+ bottomControlPoint: [8, 9]
+ )
+ let paint = MeshGradient._Paint(
+ locations: .bezierPoints([point]),
+ colors: [.clear],
+ background: background,
+ width: 1,
+ height: 1,
+ flags: .init(rawValue: 0x8000_0003)
+ )
+ let data = try ProtobufEncoder.encoding { encoder in
+ encoder.userInfo[ArchivedViewCore.archiveOptionsKey] = ArchivedViewInput.Value(deploymentVersion: version)
+ try CodableResolvedPaint(_AnyResolvedPaint(paint)).encode(to: &encoder)
+ }
+ var decoder = ProtobufDecoder(data)
+ let decoded = try CodableResolvedPaint(from: &decoder)
+ if version == .v5 {
+ #expect(data.first == 0x0a)
+ #expect(decoded.base.as(type: Color.Resolved.self) == background)
+ } else {
+ #expect(data.first == 0x4a)
+ #expect(decoded.base.as(type: MeshGradient._Paint.self) == paint)
+ }
+ }
+}