Skip to content
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

Add StrokeStyle and FillStyle #96

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Sources/OpenSwiftUI/View/Animation/TODO/Animatable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@

// MARK: - Animatable

/// A type that describes how to animate a property of a view.
public protocol Animatable {
/// The type defining the data to animate.
associatedtype AnimatableData: VectorArithmetic

/// The data to animate.
var animatableData: AnimatableData { get set }

/// Replaces `value` with an animated version of the value, using
/// `inputs`.
static func _makeAnimatable(value: inout _GraphValue<Self>, inputs: _GraphInputs)
}

Expand Down
38 changes: 38 additions & 0 deletions Sources/OpenSwiftUI/View/Shape/FillStyle.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// FillStyle.swift
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: Complete

/// A style for rasterizing vector shapes.
@frozen
public struct FillStyle: Equatable {

/// A Boolean value that indicates whether to use the even-odd rule when
/// rendering a shape.
///
/// When `isOEFilled` is `false`, the style uses the non-zero winding number
/// rule.
public var isEOFilled: Bool

/// A Boolean value that indicates whether to apply antialiasing to the
/// edges of a shape.
public var isAntialiased: Bool

/// Creates a new fill style with the specified settings.
///
/// - Parameters:
/// - eoFill: A Boolean value that indicates whether to use the even-odd
/// rule for rendering a shape. Pass `false` to use the non-zero winding
/// number rule instead.
/// - antialiased: A Boolean value that indicates whether to use
/// antialiasing when rendering the edges of a shape.
@inlinable
public init(eoFill: Bool = false, antialiased: Bool = true) {
self.isEOFilled = eoFill
self.isAntialiased = antialiased
}
}

extension FillStyle: Sendable {}
82 changes: 82 additions & 0 deletions Sources/OpenSwiftUI/View/Shape/StrokeStyle.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//
// StrokeStyle.swift
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: Complete

#if canImport(CoreGraphics)
import CoreGraphics
#else
/// Line join styles
public enum CGLineJoin: Int32, @unchecked Sendable {
case miter = 0
case round = 1
case bevel = 2
}

/// Line cap styles
public enum CGLineCap : Int32, @unchecked Sendable {
case butt = 0
case round = 1
case square = 2
}
#endif

/// The characteristics of a stroke that traces a path.
@frozen
public struct StrokeStyle: Equatable {
/// The width of the stroked path.
public var lineWidth: CGFloat

/// The endpoint style of a line.
public var lineCap: CGLineCap

/// The join type of a line.
public var lineJoin: CGLineJoin

/// A threshold used to determine whether to use a bevel instead of a
/// miter at a join.
public var miterLimit: CGFloat

/// The lengths of painted and unpainted segments used to make a dashed line.
public var dash: [CGFloat]

/// How far into the dash pattern the line starts.
public var dashPhase: CGFloat

/// Creates a new stroke style from the given components.
///
/// - Parameters:
/// - lineWidth: The width of the segment.
/// - lineCap: The endpoint style of a segment.
/// - lineJoin: The join type of a segment.
/// - miterLimit: The threshold used to determine whether to use a bevel
/// instead of a miter at a join.
/// - dash: The lengths of painted and unpainted segments used to make a
/// dashed line.
/// - dashPhase: How far into the dash pattern the line starts.
public init(lineWidth: CGFloat = 1, lineCap: CGLineCap = .butt, lineJoin: CGLineJoin = .miter, miterLimit: CGFloat = 10, dash: [CGFloat] = [CGFloat](), dashPhase: CGFloat = 0) {
self.lineWidth = lineWidth
self.lineCap = lineCap
self.lineJoin = lineJoin
self.miterLimit = miterLimit
self.dash = dash
self.dashPhase = dashPhase
}
}

extension StrokeStyle: Animatable {
public var animatableData: AnimatablePair<CGFloat, AnimatablePair<CGFloat, CGFloat>> {
get {
AnimatablePair(lineWidth, AnimatablePair(miterLimit, dashPhase))
}
set {
lineWidth = newValue.first
miterLimit = newValue.second.first
dashPhase = newValue.second.second
}
}
}

extension StrokeStyle: Sendable {}
Loading