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 ViewModifier implementation #91

Merged
merged 6 commits into from
May 5, 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
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"originHash" : "c92107a159713b44e293a2fe944a02f419f98dc401c81653bfee56d07c8a2b70",
"originHash" : "807d4a48456544b2827b8d4f69dc5e39b01103e9105f11c138529cc3159d18d4",
"pins" : [
{
"identity" : "opengraph",
"kind" : "remoteSourceControl",
"location" : "https://github.com/OpenSwiftUIProject/OpenGraph",
"state" : {
"branch" : "main",
"revision" : "acbee3c7c30cac49d64a8f619f3e3856a4e943f8"
"revision" : "47a81fde4bfa4092577abd29122206c19ad0cf98"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenSwiftUI/Core/BodyAccessor/BodyAccessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal import OpenGraphShims
protocol BodyAccessor<Container, Body> {
associatedtype Container
associatedtype Body
func updateBody(of: Container, changed: Bool)
func updateBody(of container: Container, changed: Bool)
}

extension BodyAccessor {
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenSwiftUI/Core/Graph/GraphInputsModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
// Audited for RELEASE_2021
// Status: Complete

protocol _GraphInputsModifier {
public protocol _GraphInputsModifier {
static func _makeInputs(modifier: _GraphValue<Self>, inputs: inout _GraphInputs)
}
69 changes: 69 additions & 0 deletions Sources/OpenSwiftUI/Core/Modifier/EmptyModifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// EmptyModifier.swift
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: Blocked by PrimitiveSceneModifier

/// An empty, or identity, modifier, used during development to switch
/// modifiers at compile time.
///
/// Use the empty modifier to switch modifiers at compile time during
/// development. In the example below, in a debug build the ``Text``
/// view inside `ContentView` has a yellow background and a red border.
/// A non-debug build reflects the default system, or container supplied
/// appearance.
///
/// struct EmphasizedLayout: ViewModifier {
/// func body(content: Content) -> some View {
/// content
/// .background(Color.yellow)
/// .border(Color.red)
/// }
/// }
///
/// struct ContentView: View {
/// var body: some View {
/// Text("Hello, World!")
/// .modifier(modifier)
/// }
///
/// var modifier: some ViewModifier {
/// #if DEBUG
/// return EmphasizedLayout()
/// #else
/// return EmptyModifier()
/// #endif
/// }
/// }
///
@frozen
public struct EmptyModifier: PrimitiveViewModifier/*, PrimitiveSceneModifier*/ {
public static let identity = EmptyModifier()

@inlinable
public init() {}

public static func _makeView(
modifier: _GraphValue<Self>,
inputs: _ViewInputs,
body: @escaping (_Graph, _ViewInputs) -> _ViewOutputs
) -> _ViewOutputs {
body(_Graph(), inputs)
}

public static func _makeViewList(
modifier: _GraphValue<Self>,
inputs: _ViewListInputs,
body: @escaping (_Graph, _ViewListInputs) -> _ViewListOutputs
) -> _ViewListOutputs {
body(_Graph(), inputs)
}

public static func _viewListCount(
inputs: _ViewListCountInputs,
body: (_ViewListCountInputs) -> Int?
) -> Int? {
body(inputs)
}
}
119 changes: 119 additions & 0 deletions Sources/OpenSwiftUI/Core/Modifier/ModifiedContent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
//
// ModifiedContent.swift
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: WIP

extension View {
@inlinable
public func modifier<T>(_ modifier: T) -> ModifiedContent<Self, T> {
.init(content: self, modifier: modifier)
}
}

/// A value with a modifier applied to it.
@frozen
public struct ModifiedContent<Content, Modifier> {
public typealias Body = Never

/// The content that the modifier transforms into a new view or new
/// view modifier.
public var content: Content

/// The view modifier.
public var modifier: Modifier

/// A structure that the defines the content and modifier needed to produce
/// a new view or view modifier.
///
/// If `content` is a ``View`` and `modifier` is a ``ViewModifier``, the
/// result is a ``View``. If `content` and `modifier` are both view
/// modifiers, then the result is a new ``ViewModifier`` combining them.
///
/// - Parameters:
/// - content: The content that the modifier changes.
/// - modifier: The modifier to apply to the content.
@inlinable
public init(content: Content, modifier: Modifier) {
self.content = content
self.modifier = modifier
}
}

extension ModifiedContent: Equatable where Content: Equatable, Modifier: Equatable {
public static func == (a: ModifiedContent<Content, Modifier>, b: ModifiedContent<Content, Modifier>) -> Bool {
a.content == b.content && a.modifier == b.modifier
}
}

extension ModifiedContent: View where Content: View, Modifier: ViewModifier {
public static func _makeView(
view: _GraphValue<Self>,
inputs: _ViewInputs
) -> _ViewOutputs {
_ViewDebug.makeView(
view: view[offset: { .of(&$0.modifier) }],
inputs: inputs
) { modifier, inputs in
Modifier._makeView(
modifier: modifier,
inputs: inputs
) { _, inputs in
_ViewDebug.makeView(
view: view[offset: { .of(&$0.content) }],
inputs: inputs
) { view, inputs in
Content._makeView(view: view, inputs: inputs)
}
}
}
}

public static func _makeViewList(
view: _GraphValue<Self>,
inputs: _ViewListInputs
) -> _ViewListOutputs {
Modifier.makeDebuggableViewList(
modifier: view[offset: { .of(&$0.modifier) }],
inputs: inputs
) { _, inputs in
Content.makeDebuggableViewList(
view: view[offset: { .of(&$0.content) }],
inputs: inputs
)
}
}

public static func _viewListCount(
inputs: _ViewListCountInputs
) -> Int? {
Modifier._viewListCount(inputs: inputs) { inputs in
Content._viewListCount(inputs: inputs)
}
}

public var body: ModifiedContent<Content, Modifier>.Body {
bodyError()
}
}

extension ModifiedContent: ViewModifier where Content: ViewModifier, Modifier: ViewModifier {
// public static func _makeView(modifier: _GraphValue<ModifiedContent<Content, Modifier>>, inputs: _ViewInputs, body: @escaping (_Graph, _ViewInputs) -> _ViewOutputs) -> _ViewOutputs {
//
// }
// public static func _makeViewList(modifier: _GraphValue<ModifiedContent<Content, Modifier>>, inputs: _ViewListInputs, body: @escaping (_Graph, _ViewListInputs) -> _ViewListOutputs) -> _ViewListOutputs {
//
// }
// public static func _viewListCount(inputs: _ViewListCountInputs, body: (_ViewListCountInputs) -> Int?) -> Int? {
//
// }
}

extension ViewModifier {
@inlinable
@inline(__always)
public func concat<T>(_ modifier: T) -> ModifiedContent<Self, T> {
.init(content: self, modifier: modifier)
}
}
18 changes: 0 additions & 18 deletions Sources/OpenSwiftUI/Core/Modifier/TODO/EmptyModifier.swift

This file was deleted.

63 changes: 0 additions & 63 deletions Sources/OpenSwiftUI/Core/Modifier/TODO/ModifiedContent.swift

This file was deleted.

Loading
Loading