Skip to content

Commit

Permalink
Update ViewTransform (#181)
Browse files Browse the repository at this point in the history
* Update ProjectionTransform

* Update protobuf implementation and test case

* Update folder structure

* Update EdgeInsets

* Add ScrollGeometry implementation

* Update ViewTransform implementation

* Update ViewTransform.UnsafeBuffer

* Update ViewTransform.append method

* Update ViewTransfrom.apply method

* Add ViewTransform convert API

* Fix linux platform build issue
  • Loading branch information
Kyle-Ye authored Jan 13, 2025
1 parent 2f27747 commit f1ba63a
Show file tree
Hide file tree
Showing 34 changed files with 2,555 additions and 368 deletions.
42 changes: 42 additions & 0 deletions Sources/OpenSwiftUI/Layout/Edge/EdgeInsets.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// EdgeInsets.swift
// OpenSwiftUI
//
// Audited for iOS 18.0
// Status: Complete

#if canImport(Darwin)
// MARK: - EdgeInsets + Conversion

#if canImport(UIKit)
public import UIKit
#elseif canImport(AppKit)
public import AppKit
#endif

extension EdgeInsets {
/// Create edge insets from the equivalent NSDirectionalEdgeInsets.
@available(watchOS, unavailable)
public init(_ nsEdgeInsets: NSDirectionalEdgeInsets) {
self.init(
top: nsEdgeInsets.top,
leading: nsEdgeInsets.leading,
bottom: nsEdgeInsets.bottom,
trailing: nsEdgeInsets.trailing
)
}
}

extension NSDirectionalEdgeInsets {
/// Create edge insets from the equivalent EdgeInsets.
@available(watchOS, unavailable)
public init(_ edgeInsets: EdgeInsets) {
self.init(
top: edgeInsets.top,
leading: edgeInsets.leading,
bottom: edgeInsets.bottom,
trailing: edgeInsets.trailing
)
}
}
#endif
80 changes: 61 additions & 19 deletions Sources/OpenSwiftUICore/Extension/CGAffineTransform+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,48 @@
// Audited for iOS 18.0
// Status: Complete

#if canImport(Darwin)

#if canImport(CoreGraphics)
package import CoreGraphics
#else
package import Foundation
// FIXME: Use Silica or other implementation
public struct CGAffineTransform: Equatable {
public init() {
a = .zero
b = .zero
c = .zero
d = .zero
tx = .zero
ty = .zero
}

public init(a: Double, b: Double, c: Double, d: Double, tx: Double, ty: Double) {
self.a = a
self.b = b
self.c = c
self.d = d
self.tx = tx
self.ty = ty
}

public var a: Double
public var b: Double
public var c: Double
public var d: Double
public var tx: Double
public var ty: Double

public static let identity = CGAffineTransform(a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0)

public func concatenating(_ transform: CGAffineTransform) -> CGAffineTransform {
preconditionFailure("Unimplemented")
}

public func inverted() -> CGAffineTransform {
preconditionFailure("Unimplemented")
}
}
#endif

extension CGAffineTransform {
package init(rotation: Angle) {
Expand Down Expand Up @@ -49,29 +88,32 @@ extension CGAffineTransform {

extension CGAffineTransform: ProtobufMessage {
package func encode(to encoder: inout ProtobufEncoder) throws {
encoder.cgFloatField(1, a, defaultValue: 1)
encoder.cgFloatField(2, b, defaultValue: 0)
encoder.cgFloatField(3, c, defaultValue: 0)
encoder.cgFloatField(4, d, defaultValue: 1)
encoder.cgFloatField(5, tx, defaultValue: 0)
encoder.cgFloatField(6, ty, defaultValue: 0)
withUnsafePointer(to: self) { pointer in
let pointer = UnsafeRawPointer(pointer).assumingMemoryBound(to: CGFloat.self)
let bufferPointer = UnsafeBufferPointer(start: pointer, count: 6)
for index: UInt in 1 ... 6 {
encoder.cgFloatField(
index,
bufferPointer[Int(index &- 1)],
defaultValue: (index == 1 || index == 4) ? 1 : 0
)
}
}
}

package init(from decoder: inout ProtobufDecoder) throws {
var transform = CGAffineTransform.identity
while let field = try decoder.nextField() {
switch field.tag {
case 1: transform.a = try decoder.cgFloatField(field)
case 2: transform.b = try decoder.cgFloatField(field)
case 3: transform.c = try decoder.cgFloatField(field)
case 4: transform.d = try decoder.cgFloatField(field)
case 5: transform.tx = try decoder.cgFloatField(field)
case 6: transform.ty = try decoder.cgFloatField(field)
default: try decoder.skipField(field)
try withUnsafeMutablePointer(to: &transform) { pointer in
let pointer = UnsafeMutableRawPointer(pointer).assumingMemoryBound(to: CGFloat.self)
let bufferPointer = UnsafeMutableBufferPointer(start: pointer, count: 6)
while let field = try decoder.nextField() {
let tag = field.tag
switch tag {
case 1...6: bufferPointer[Int(tag &- 1)] = try decoder.cgFloatField(field)
default: try decoder.skipField(field)
}
}
}
self = transform
}
}

#endif
30 changes: 19 additions & 11 deletions Sources/OpenSwiftUICore/Extension/CGRect+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -245,21 +245,29 @@ extension CGRect: Animatable {

extension CGRect: ProtobufMessage {
package func encode(to encoder: inout ProtobufEncoder) {
encoder.cgFloatField(1, x)
encoder.cgFloatField(2, y)
encoder.cgFloatField(3, width)
encoder.cgFloatField(4, height)
withUnsafePointer(to: self) { pointer in
let pointer = UnsafeRawPointer(pointer).assumingMemoryBound(to: CGFloat.self)
let bufferPointer = UnsafeBufferPointer(start: pointer, count: 4)
for index: UInt in 1 ... 4 {
encoder.cgFloatField(
index,
bufferPointer[Int(index &- 1)]
)
}
}
}

package init(from decoder: inout ProtobufDecoder) throws {
var rect = CGRect.zero
while let field = try decoder.nextField() {
switch field.tag {
case 1: rect.x = try decoder.cgFloatField(field)
case 2: rect.y = try decoder.cgFloatField(field)
case 3: rect.size.width = try decoder.cgFloatField(field)
case 4: rect.size.height = try decoder.cgFloatField(field)
default: try decoder.skipField(field)
try withUnsafeMutablePointer(to: &rect) { pointer in
let pointer = UnsafeMutableRawPointer(pointer).assumingMemoryBound(to: CGFloat.self)
let bufferPointer = UnsafeMutableBufferPointer(start: pointer, count: 4)
while let field = try decoder.nextField() {
let tag = field.tag
switch tag {
case 1...6: bufferPointer[Int(tag &- 1)] = try decoder.cgFloatField(field)
default: try decoder.skipField(field)
}
}
}
self = rect
Expand Down
68 changes: 23 additions & 45 deletions Sources/OpenSwiftUICore/Graphic/Color/ColorMatrix.swift
Original file line number Diff line number Diff line change
Expand Up @@ -295,56 +295,34 @@ extension _ColorMatrix: ShapeStyle {

extension _ColorMatrix: ProtobufMessage {
package func encode(to encoder: inout ProtobufEncoder) {
encoder.floatField(1, m11, defaultValue: 1.0)
encoder.floatField(2, m12, defaultValue: 0.0)
encoder.floatField(3, m13, defaultValue: 0.0)
encoder.floatField(4, m14, defaultValue: 0.0)
encoder.floatField(5, m15, defaultValue: 0.0)
encoder.floatField(6, m21, defaultValue: 0.0)
encoder.floatField(7, m22, defaultValue: 1.0)
encoder.floatField(8, m23, defaultValue: 0.0)
encoder.floatField(9, m24, defaultValue: 0.0)
encoder.floatField(10, m25, defaultValue: 0.0)
encoder.floatField(11, m31, defaultValue: 0.0)
encoder.floatField(12, m32, defaultValue: 0.0)
encoder.floatField(13, m33, defaultValue: 1.0)
encoder.floatField(14, m34, defaultValue: 0.0)
encoder.floatField(15, m35, defaultValue: 0.0)
encoder.floatField(16, m41, defaultValue: 0.0)
encoder.floatField(17, m42, defaultValue: 0.0)
encoder.floatField(18, m43, defaultValue: 0.0)
encoder.floatField(19, m44, defaultValue: 1.0)
encoder.floatField(20, m45, defaultValue: 0.0)
withUnsafePointer(to: self) { pointer in
let pointer = UnsafeRawPointer(pointer).assumingMemoryBound(to: Float.self)
let bufferPointer = UnsafeBufferPointer(start: pointer, count: 20)
for index: UInt in 1 ... 6 {
encoder.floatField(
index,
bufferPointer[Int(index &- 1)],
defaultValue: (index == 1 || index == 7 || index == 13 || index == 19) ? 1 : 0
)
}
}
}

package init(from decoder: inout ProtobufDecoder) throws {
self = _ColorMatrix()
while let field = try decoder.nextField() {
switch field.tag {
case 1: m11 = try decoder.floatField(field)
case 2: m12 = try decoder.floatField(field)
case 3: m13 = try decoder.floatField(field)
case 4: m14 = try decoder.floatField(field)
case 5: m15 = try decoder.floatField(field)
case 6: m21 = try decoder.floatField(field)
case 7: m22 = try decoder.floatField(field)
case 8: m23 = try decoder.floatField(field)
case 9: m24 = try decoder.floatField(field)
case 10: m25 = try decoder.floatField(field)
case 11: m31 = try decoder.floatField(field)
case 12: m32 = try decoder.floatField(field)
case 13: m33 = try decoder.floatField(field)
case 14: m34 = try decoder.floatField(field)
case 15: m35 = try decoder.floatField(field)
case 16: m41 = try decoder.floatField(field)
case 17: m42 = try decoder.floatField(field)
case 18: m43 = try decoder.floatField(field)
case 19: m44 = try decoder.floatField(field)
case 20: m45 = try decoder.floatField(field)
default:
try decoder.skipField(field)
var matrix = _ColorMatrix()
try withUnsafeMutablePointer(to: &matrix) { pointer in
let pointer = UnsafeMutableRawPointer(pointer).assumingMemoryBound(to: Float.self)
let bufferPointer = UnsafeMutableBufferPointer(start: pointer, count: 20)
while let field = try decoder.nextField() {
let tag = field.tag
switch tag {
case 1...6: bufferPointer[Int(tag &- 1)] = try decoder.floatField(field)
default: try decoder.skipField(field)
}
}
}
self = matrix

}
}

Expand Down
Loading

0 comments on commit f1ba63a

Please sign in to comment.