From 06b7f78ba8217596bfafcd2f5769bcaf31108109 Mon Sep 17 00:00:00 2001 From: Kyle Date: Sun, 5 May 2024 16:48:48 +0800 Subject: [PATCH] Update ModifiedContent implementation --- Package.resolved | 4 +- .../Core/Modifier/ModifiedContent.swift | 66 +++++++++++++++++-- .../ViewModifier/CustomViewModifier.swift | 2 +- .../View/Debug/TODO/ViewDebug.swift | 30 +++++++-- 4 files changed, 90 insertions(+), 12 deletions(-) diff --git a/Package.resolved b/Package.resolved index e08910b2..30e6d488 100644 --- a/Package.resolved +++ b/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "c92107a159713b44e293a2fe944a02f419f98dc401c81653bfee56d07c8a2b70", + "originHash" : "807d4a48456544b2827b8d4f69dc5e39b01103e9105f11c138529cc3159d18d4", "pins" : [ { "identity" : "opengraph", @@ -7,7 +7,7 @@ "location" : "https://github.com/OpenSwiftUIProject/OpenGraph", "state" : { "branch" : "main", - "revision" : "acbee3c7c30cac49d64a8f619f3e3856a4e943f8" + "revision" : "47a81fde4bfa4092577abd29122206c19ad0cf98" } }, { diff --git a/Sources/OpenSwiftUI/Core/Modifier/ModifiedContent.swift b/Sources/OpenSwiftUI/Core/Modifier/ModifiedContent.swift index 4f215604..2a76d855 100644 --- a/Sources/OpenSwiftUI/Core/Modifier/ModifiedContent.swift +++ b/Sources/OpenSwiftUI/Core/Modifier/ModifiedContent.swift @@ -7,20 +7,34 @@ extension View { @inlinable - @inline(__always) public func modifier(_ modifier: T) -> ModifiedContent { .init(content: self, modifier: modifier) } } +/// A value with a modifier applied to it. @frozen public struct ModifiedContent { 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 - @inline(__always) public init(content: Content, modifier: Modifier) { self.content = content self.modifier = modifier @@ -34,9 +48,51 @@ extension ModifiedContent: Equatable where Content: Equatable, Modifier: Equatab } extension ModifiedContent: View where Content: View, Modifier: ViewModifier { - // public static func _makeView(view: _GraphValue>, inputs: _ViewInputs) -> _ViewOutputs - // public static func _makeViewList(view: _GraphValue>, inputs: _ViewListInputs) -> _ViewListOutputs - // public static func _viewListCount(inputs: _ViewListCountInputs) -> Int? + public static func _makeView( + view: _GraphValue, + 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, + 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.Body { bodyError() } diff --git a/Sources/OpenSwiftUI/Core/Modifier/ViewModifier/CustomViewModifier.swift b/Sources/OpenSwiftUI/Core/Modifier/ViewModifier/CustomViewModifier.swift index c28e90ad..529ecf57 100644 --- a/Sources/OpenSwiftUI/Core/Modifier/ViewModifier/CustomViewModifier.swift +++ b/Sources/OpenSwiftUI/Core/Modifier/ViewModifier/CustomViewModifier.swift @@ -95,7 +95,7 @@ public struct _ViewModifier_Content: PrimitiveView { case let .view(makeViewBody): return makeViewBody(_Graph(), inputs) case let .list(makeViewListBody): - fatalError("TODO") + fatalError("TODO: \(String(describing: makeViewListBody))") } } diff --git a/Sources/OpenSwiftUI/View/Debug/TODO/ViewDebug.swift b/Sources/OpenSwiftUI/View/Debug/TODO/ViewDebug.swift index f1ef8251..06d2b7b4 100644 --- a/Sources/OpenSwiftUI/View/Debug/TODO/ViewDebug.swift +++ b/Sources/OpenSwiftUI/View/Debug/TODO/ViewDebug.swift @@ -10,6 +10,31 @@ import Foundation internal import COpenSwiftUI internal import OpenGraphShims +// MARK: View and ViewModifier + +extension View { + static func makeDebuggableViewList( + view: _GraphValue, + inputs: _ViewListInputs + ) -> _ViewListOutputs { + OGSubgraph.beginTreeElement(value: view.value, flags: 1) + defer { OGSubgraph.endTreeElement(value: view.value) } + return _makeViewList(view: view, inputs: inputs) + } +} + +extension ViewModifier { + static func makeDebuggableViewList( + modifier: _GraphValue, + inputs: _ViewListInputs, + body: @escaping (_Graph, _ViewListInputs) -> _ViewListOutputs + ) -> _ViewListOutputs { + OGSubgraph.beginTreeElement(value: modifier.value, flags: 1) + defer { OGSubgraph.endTreeElement(value: modifier.value) } + return _makeViewList(modifier: modifier, inputs: inputs, body: body) + } +} + // MARK: _ViewDebug public enum _ViewDebug { @@ -42,7 +67,6 @@ extension _ViewDebug { } } - @_transparent @inline(__always) static func makeView( view: _GraphValue, @@ -50,9 +74,7 @@ extension _ViewDebug { body: (_ view: _GraphValue, _ inputs: _ViewInputs) -> _ViewOutputs ) -> _ViewOutputs { var inputs = inputs - if OGSubgraph.shouldRecordTree { - OGSubgraph.beginTreeElement(value: view.value, flags: 0) - } + OGSubgraph.beginTreeElement(value: view.value, flags: 0) var outputs = inputs.withEmptyChangedDebugPropertiesInputs { inputs in body(view, inputs) }