Skip to content

Commit 25e716d

Browse files
authored
Add ViewModifier implementation (#91)
* Add ViewInputFlag implementation * Update ViewModifier implementation * Update ViewModifier+_GraphInputsModifier extension * Update _ViewModifier_Content implementation * Update EmptyModifier implementation * Update ModifiedContent implementation
1 parent e16ee45 commit 25e716d

21 files changed

+584
-162
lines changed

Package.resolved

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
2-
"originHash" : "c92107a159713b44e293a2fe944a02f419f98dc401c81653bfee56d07c8a2b70",
2+
"originHash" : "807d4a48456544b2827b8d4f69dc5e39b01103e9105f11c138529cc3159d18d4",
33
"pins" : [
44
{
55
"identity" : "opengraph",
66
"kind" : "remoteSourceControl",
77
"location" : "https://github.com/OpenSwiftUIProject/OpenGraph",
88
"state" : {
99
"branch" : "main",
10-
"revision" : "acbee3c7c30cac49d64a8f619f3e3856a4e943f8"
10+
"revision" : "47a81fde4bfa4092577abd29122206c19ad0cf98"
1111
}
1212
},
1313
{

Sources/OpenSwiftUI/Core/BodyAccessor/BodyAccessor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ internal import OpenGraphShims
1010
protocol BodyAccessor<Container, Body> {
1111
associatedtype Container
1212
associatedtype Body
13-
func updateBody(of: Container, changed: Bool)
13+
func updateBody(of container: Container, changed: Bool)
1414
}
1515

1616
extension BodyAccessor {

Sources/OpenSwiftUI/Core/Graph/GraphInputsModifier.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
// Audited for RELEASE_2021
66
// Status: Complete
77

8-
protocol _GraphInputsModifier {
8+
public protocol _GraphInputsModifier {
99
static func _makeInputs(modifier: _GraphValue<Self>, inputs: inout _GraphInputs)
1010
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//
2+
// EmptyModifier.swift
3+
// OpenSwiftUI
4+
//
5+
// Audited for RELEASE_2021
6+
// Status: Blocked by PrimitiveSceneModifier
7+
8+
/// An empty, or identity, modifier, used during development to switch
9+
/// modifiers at compile time.
10+
///
11+
/// Use the empty modifier to switch modifiers at compile time during
12+
/// development. In the example below, in a debug build the ``Text``
13+
/// view inside `ContentView` has a yellow background and a red border.
14+
/// A non-debug build reflects the default system, or container supplied
15+
/// appearance.
16+
///
17+
/// struct EmphasizedLayout: ViewModifier {
18+
/// func body(content: Content) -> some View {
19+
/// content
20+
/// .background(Color.yellow)
21+
/// .border(Color.red)
22+
/// }
23+
/// }
24+
///
25+
/// struct ContentView: View {
26+
/// var body: some View {
27+
/// Text("Hello, World!")
28+
/// .modifier(modifier)
29+
/// }
30+
///
31+
/// var modifier: some ViewModifier {
32+
/// #if DEBUG
33+
/// return EmphasizedLayout()
34+
/// #else
35+
/// return EmptyModifier()
36+
/// #endif
37+
/// }
38+
/// }
39+
///
40+
@frozen
41+
public struct EmptyModifier: PrimitiveViewModifier/*, PrimitiveSceneModifier*/ {
42+
public static let identity = EmptyModifier()
43+
44+
@inlinable
45+
public init() {}
46+
47+
public static func _makeView(
48+
modifier: _GraphValue<Self>,
49+
inputs: _ViewInputs,
50+
body: @escaping (_Graph, _ViewInputs) -> _ViewOutputs
51+
) -> _ViewOutputs {
52+
body(_Graph(), inputs)
53+
}
54+
55+
public static func _makeViewList(
56+
modifier: _GraphValue<Self>,
57+
inputs: _ViewListInputs,
58+
body: @escaping (_Graph, _ViewListInputs) -> _ViewListOutputs
59+
) -> _ViewListOutputs {
60+
body(_Graph(), inputs)
61+
}
62+
63+
public static func _viewListCount(
64+
inputs: _ViewListCountInputs,
65+
body: (_ViewListCountInputs) -> Int?
66+
) -> Int? {
67+
body(inputs)
68+
}
69+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
//
2+
// ModifiedContent.swift
3+
// OpenSwiftUI
4+
//
5+
// Audited for RELEASE_2021
6+
// Status: WIP
7+
8+
extension View {
9+
@inlinable
10+
public func modifier<T>(_ modifier: T) -> ModifiedContent<Self, T> {
11+
.init(content: self, modifier: modifier)
12+
}
13+
}
14+
15+
/// A value with a modifier applied to it.
16+
@frozen
17+
public struct ModifiedContent<Content, Modifier> {
18+
public typealias Body = Never
19+
20+
/// The content that the modifier transforms into a new view or new
21+
/// view modifier.
22+
public var content: Content
23+
24+
/// The view modifier.
25+
public var modifier: Modifier
26+
27+
/// A structure that the defines the content and modifier needed to produce
28+
/// a new view or view modifier.
29+
///
30+
/// If `content` is a ``View`` and `modifier` is a ``ViewModifier``, the
31+
/// result is a ``View``. If `content` and `modifier` are both view
32+
/// modifiers, then the result is a new ``ViewModifier`` combining them.
33+
///
34+
/// - Parameters:
35+
/// - content: The content that the modifier changes.
36+
/// - modifier: The modifier to apply to the content.
37+
@inlinable
38+
public init(content: Content, modifier: Modifier) {
39+
self.content = content
40+
self.modifier = modifier
41+
}
42+
}
43+
44+
extension ModifiedContent: Equatable where Content: Equatable, Modifier: Equatable {
45+
public static func == (a: ModifiedContent<Content, Modifier>, b: ModifiedContent<Content, Modifier>) -> Bool {
46+
a.content == b.content && a.modifier == b.modifier
47+
}
48+
}
49+
50+
extension ModifiedContent: View where Content: View, Modifier: ViewModifier {
51+
public static func _makeView(
52+
view: _GraphValue<Self>,
53+
inputs: _ViewInputs
54+
) -> _ViewOutputs {
55+
_ViewDebug.makeView(
56+
view: view[offset: { .of(&$0.modifier) }],
57+
inputs: inputs
58+
) { modifier, inputs in
59+
Modifier._makeView(
60+
modifier: modifier,
61+
inputs: inputs
62+
) { _, inputs in
63+
_ViewDebug.makeView(
64+
view: view[offset: { .of(&$0.content) }],
65+
inputs: inputs
66+
) { view, inputs in
67+
Content._makeView(view: view, inputs: inputs)
68+
}
69+
}
70+
}
71+
}
72+
73+
public static func _makeViewList(
74+
view: _GraphValue<Self>,
75+
inputs: _ViewListInputs
76+
) -> _ViewListOutputs {
77+
Modifier.makeDebuggableViewList(
78+
modifier: view[offset: { .of(&$0.modifier) }],
79+
inputs: inputs
80+
) { _, inputs in
81+
Content.makeDebuggableViewList(
82+
view: view[offset: { .of(&$0.content) }],
83+
inputs: inputs
84+
)
85+
}
86+
}
87+
88+
public static func _viewListCount(
89+
inputs: _ViewListCountInputs
90+
) -> Int? {
91+
Modifier._viewListCount(inputs: inputs) { inputs in
92+
Content._viewListCount(inputs: inputs)
93+
}
94+
}
95+
96+
public var body: ModifiedContent<Content, Modifier>.Body {
97+
bodyError()
98+
}
99+
}
100+
101+
extension ModifiedContent: ViewModifier where Content: ViewModifier, Modifier: ViewModifier {
102+
// public static func _makeView(modifier: _GraphValue<ModifiedContent<Content, Modifier>>, inputs: _ViewInputs, body: @escaping (_Graph, _ViewInputs) -> _ViewOutputs) -> _ViewOutputs {
103+
//
104+
// }
105+
// public static func _makeViewList(modifier: _GraphValue<ModifiedContent<Content, Modifier>>, inputs: _ViewListInputs, body: @escaping (_Graph, _ViewListInputs) -> _ViewListOutputs) -> _ViewListOutputs {
106+
//
107+
// }
108+
// public static func _viewListCount(inputs: _ViewListCountInputs, body: (_ViewListCountInputs) -> Int?) -> Int? {
109+
//
110+
// }
111+
}
112+
113+
extension ViewModifier {
114+
@inlinable
115+
@inline(__always)
116+
public func concat<T>(_ modifier: T) -> ModifiedContent<Self, T> {
117+
.init(content: self, modifier: modifier)
118+
}
119+
}

Sources/OpenSwiftUI/Core/Modifier/TODO/EmptyModifier.swift

Lines changed: 0 additions & 18 deletions
This file was deleted.

Sources/OpenSwiftUI/Core/Modifier/TODO/ModifiedContent.swift

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)