Skip to content

Commit

Permalink
Add Shape interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Ye committed Jun 27, 2024
1 parent ab5c434 commit eaa599c
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Sources/OpenSwiftUI/View/Shape/ForegroundStyle.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
@frozen
public struct ForegroundStyle: ShapeStyle {
@inlinable
public init() {}

public static func _makeView<S>(view: _GraphValue<_ShapeView<S, ForegroundStyle>>, inputs: _ViewInputs) -> _ViewOutputs where S: Shape {
fatalError("TODO")
}
}

#if OPENSWIFTUI_SUPPORT_2021_API

extension ForegroundStyle {
public func _apply(to shape: inout _ShapeStyle_Shape) {
fatalError("TODO")
}

public static func _apply(to type: inout _ShapeStyle_ShapeType) {
fatalError("TODO")
}
}

extension ShapeStyle where Self == ForegroundStyle {
@_alwaysEmitIntoClient
public static var foreground: ForegroundStyle {
.init()
}
}
#endif
25 changes: 25 additions & 0 deletions Sources/OpenSwiftUI/View/Shape/Rectangle.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Foundation

/// A rectangular shape aligned inside the frame of the view containing it.
@frozen
public struct Rectangle: Shape {
public func path(in rect: CGRect) -> Path {
fatalError("TODO")
}

/// Creates a new rectangle shape.
@inlinable
public init() {}

public typealias AnimatableData = EmptyAnimatableData

public typealias Body = _ShapeView<Rectangle, ForegroundStyle>
}


extension Shape where Self == Rectangle {
/// A rectangular shape aligned inside the frame of the view containing it.
public static var rect: Rectangle {
Rectangle()
}
}
51 changes: 51 additions & 0 deletions Sources/OpenSwiftUI/View/Shape/Shape.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// Shape.swift
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: WIP

import Foundation

/// A 2D shape that you can use when drawing a view.
///
/// Shapes without an explicit fill or stroke get a default fill based on the
/// foreground color.
///
/// You can define shapes in relation to an implicit frame of reference, such as
/// the natural size of the view that contains it. Alternatively, you can define
/// shapes in terms of absolute coordinates.
public protocol Shape: Animatable, View {
/// Describes this shape as a path within a rectangular frame of reference.
///
/// - Parameter rect: The frame of reference for describing this shape.
///
/// - Returns: A path that describes this shape.
func path(in rect: CGRect) -> Path

#if OPENSWIFTUI_SUPPORT_2021_API
/// An indication of how to style a shape.
///
/// OpenSwiftUI looks at a shape's role when deciding how to apply a
/// ``ShapeStyle`` at render time. The ``Shape`` protocol provides a
/// default implementation with a value of ``ShapeRole/fill``. If you
/// create a composite shape, you can provide an override of this property
/// to return another value, if appropriate.
static var role: ShapeRole { get }
#endif
}

extension Shape {
public var body: _ShapeView<Self, ForegroundStyle> {
_ShapeView(shape: self, style: ForegroundStyle())
}
}

#if OPENSWIFTUI_SUPPORT_2021_API

extension Shape {
public static var role: ShapeRole {
.fill
}
}
#endif
27 changes: 27 additions & 0 deletions Sources/OpenSwiftUI/View/Shape/ShapeRole.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// ShapeRole.swift
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: Complete

#if OPENSWIFTUI_SUPPORT_2021_API

/// Ways of styling a shape.
public enum ShapeRole: Sendable {
/// Indicates to the shape's style that SwiftUI fills the shape.
case fill

/// Indicates to the shape's style that SwiftUI applies a stroke to
/// the shape's path.
case stroke

/// Indicates to the shape's style that SwiftUI uses the shape as a
/// separator.
case separator
}

extension ShapeRole: Equatable {}
extension ShapeRole: Hashable {}

#endif
38 changes: 38 additions & 0 deletions Sources/OpenSwiftUI/View/Shape/ShapeStyle.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
public protocol ShapeStyle {
@available(*, deprecated, message: "obsolete")
static func _makeView<S>(view: _GraphValue<_ShapeView<S, Self>>, inputs: _ViewInputs) -> _ViewOutputs where S : Shape

#if OPENSWIFTUI_SUPPORT_2021_API
func _apply(to shape: inout _ShapeStyle_Shape)

static func _apply(to type: inout _ShapeStyle_ShapeType)
#endif
}


#if OPENSWIFTUI_SUPPORT_2021_API

public struct _ShapeStyle_Shape {
// TODO
}

public struct _ShapeStyle_ShapeType {
// TODO
}

extension ShapeStyle {
public static func _makeView<S>(view: _GraphValue<_ShapeView<S, Self>>, inputs: _ViewInputs) -> _ViewOutputs where S: Shape {
fatalError("TODO")
}

public func _apply(to shape: inout _ShapeStyle_Shape) {
fatalError("TODO")
}

public static func _apply(to type: inout _ShapeStyle_ShapeType) {
fatalError("TODO")
}
}


#endif
20 changes: 20 additions & 0 deletions Sources/OpenSwiftUI/View/Shape/ShapeView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@frozen
public struct _ShapeView<Content, Style>: PrimitiveView where Content : Shape, Style: ShapeStyle {
public var shape: Content
public var style: Style
public var fillStyle: FillStyle

@inlinable
public init(shape: Content, style: Style, fillStyle: FillStyle = FillStyle()) {
self.shape = shape
self.style = style
self.fillStyle = fillStyle
}


public static func _makeView(view: _GraphValue<_ShapeView<Content, Style>>, inputs: _ViewInputs) -> _ViewOutputs {
fatalError("TODO")
}

public typealias Body = Never
}

0 comments on commit eaa599c

Please sign in to comment.