Skip to content

Commit 8a17e87

Browse files
authored
Make ShapeReference public (#995)
1 parent 1cb2df7 commit 8a17e87

File tree

7 files changed

+33
-17
lines changed

7 files changed

+33
-17
lines changed

Sources/LiveViewNative/ContentBuilder.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,18 @@ public protocol ContentModifier<Builder>: Decodable {
512512
) -> Builder.Content
513513
}
514514

515+
public struct EmptyContentModifier<Builder: ContentBuilder>: ContentModifier {
516+
public init() {}
517+
518+
public func apply<R>(
519+
to content: Builder.Content,
520+
on element: ElementNode,
521+
in context: Builder.Context<R>
522+
) -> Builder.Content where R : RootRegistry {
523+
return content
524+
}
525+
}
526+
515527
/// A type-erased ``ContentModifier`` in a stack.
516528
private struct AnyContentBuilderModifier<R: RootRegistry, Builder: ContentBuilder>: Decodable {
517529
let modifier: any ContentModifier<Builder>

Sources/LiveViewNative/Modifiers/Drawing and Graphics Modifiers/ClipShapeModifier.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ struct ClipShapeModifier<R: RootRegistry>: ViewModifier, Decodable {
6464

6565
func body(content: Content) -> some View {
6666
content.clipShape(
67-
shape.resolve(on: element, in: context),
67+
shape.resolve(on: element),
6868
style: style
6969
)
7070
}

Sources/LiveViewNative/Modifiers/Layout Fundamentals Modifiers/BackgroundModifier.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ struct BackgroundModifier<R: RootRegistry>: ViewModifier, Decodable {
124124
context.buildChildren(of: element, forTemplate: reference)
125125
}
126126
} else if let shape {
127-
content.background(style ?? AnyShapeStyle(.background), in: shape.resolve(on: element, in: context), fillStyle: fillStyle)
127+
content.background(style ?? AnyShapeStyle(.background), in: shape.resolve(on: element), fillStyle: fillStyle)
128128
} else if let style {
129129
content.background(style, ignoresSafeAreaEdges: ignoresSafeAreaEdges)
130130
} else {

Sources/LiveViewNative/Modifiers/Layout Fundamentals Modifiers/OverlayModifier.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ struct OverlayModifier<R: RootRegistry>: ViewModifier, Decodable {
122122
} else if let shape,
123123
let style
124124
{
125-
content.overlay(style, in: shape.resolve(on: element, in: context), fillStyle: fillStyle)
125+
content.overlay(style, in: shape.resolve(on: element), fillStyle: fillStyle)
126126
} else if let style {
127127
content.overlay(style, ignoresSafeAreaEdges: ignoresSafeAreaEdges)
128128
} else {

Sources/LiveViewNative/Modifiers/Shapes Modifiers/ContainerShapeModifier.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ struct ContainerShapeModifier<R: RootRegistry>: ViewModifier, Decodable {
4949
}
5050

5151
func body(content: Content) -> some View {
52-
content.containerShape(shape.resolveInsettableShape(on: element, in: context))
52+
content.containerShape(shape.resolveInsettableShape(on: element))
5353
}
5454

5555
enum CodingKeys: CodingKey {

Sources/LiveViewNative/Modifiers/View Styles Modifiers/ContentShapeModifier.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ struct ContentShapeModifier<R: RootRegistry>: ViewModifier, Decodable {
5858
func body(content: Content) -> some View {
5959
content.contentShape(
6060
kind,
61-
shape.resolve(on: element, in: context),
61+
shape.resolve(on: element),
6262
eoFill: eoFill
6363
)
6464
}

Sources/LiveViewNative/Utils/ShapeReference.swift

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ import SwiftUI
6161
#if swift(>=5.8)
6262
@_documentation(visibility: public)
6363
#endif
64-
enum ShapeReference: Decodable {
64+
public enum ShapeReference: Decodable {
6565
case `static`(any InsettableShape)
6666
case key(String)
6767

68-
init(from decoder: Decoder) throws {
68+
public init(from decoder: Decoder) throws {
6969
let container = try decoder.container(keyedBy: CodingKeys.self)
7070
if let key = try container.decodeIfPresent(String.self, forKey: .key) {
7171
self = .key(key)
@@ -123,12 +123,14 @@ enum ShapeReference: Decodable {
123123
}
124124
}
125125

126-
func resolve<R: RootRegistry>(on element: ElementNode, in context: LiveContext<R>) -> AnyShape {
126+
public func resolve(on element: ElementNode) -> AnyShape {
127127
switch self {
128128
case let .static(anyShape):
129129
return AnyShape(anyShape)
130130
case let .key(keyName):
131-
return context.children(of: element, forTemplate: keyName)
131+
return element.children()
132+
.lazy
133+
.filter({ $0.attributes.contains(where: { $0.name == "template" && $0.value == keyName }) })
132134
.compactMap { $0.asElement() }
133135
.compactMap {
134136
let shape: any InsettableShape
@@ -159,12 +161,14 @@ enum ShapeReference: Decodable {
159161
}
160162
}
161163

162-
func resolveInsettableShape<R: RootRegistry>(on element: ElementNode, in context: LiveContext<R>) -> AnyInsettableShape {
164+
public func resolveInsettableShape(on element: ElementNode) -> AnyInsettableShape {
163165
switch self {
164166
case let .static(anyShape):
165167
return AnyInsettableShape(anyShape)
166168
case let .key(keyName):
167-
return context.children(of: element, forTemplate: keyName)
169+
return element.children()
170+
.lazy
171+
.filter({ $0.attributes.contains(where: { $0.name == "template" && $0.value == keyName }) })
168172
.compactMap { $0.asElement() }
169173
.compactMap {
170174
let shape: any InsettableShape
@@ -201,7 +205,7 @@ enum ShapeReference: Decodable {
201205
}
202206
}
203207

204-
struct AnyInsettableShape: InsettableShape {
208+
public struct AnyInsettableShape: InsettableShape {
205209
let _path: @Sendable (CGRect) -> Path
206210
let _inset: @Sendable (CGFloat) -> any InsettableShape
207211
let _sizeThatFits: @Sendable (ProposedViewSize) -> CGSize
@@ -212,19 +216,19 @@ enum ShapeReference: Decodable {
212216
self._sizeThatFits = { shape.sizeThatFits($0) }
213217
}
214218

215-
typealias InsetShape = Self
219+
public typealias InsetShape = Self
216220

217-
static let role: ShapeRole = .fill
221+
public static let role: ShapeRole = .fill
218222

219-
func path(in rect: CGRect) -> Path {
223+
public func path(in rect: CGRect) -> Path {
220224
_path(rect)
221225
}
222226

223-
func sizeThatFits(_ proposal: ProposedViewSize) -> CGSize {
227+
public func sizeThatFits(_ proposal: ProposedViewSize) -> CGSize {
224228
_sizeThatFits(proposal)
225229
}
226230

227-
func inset(by amount: CGFloat) -> Self.InsetShape {
231+
public func inset(by amount: CGFloat) -> Self.InsetShape {
228232
.init(_inset(amount))
229233
}
230234
}

0 commit comments

Comments
 (0)