diff --git a/Sources/Introspect.swift b/Sources/Introspect.swift index cd699d7..245280a 100644 --- a/Sources/Introspect.swift +++ b/Sources/Introspect.swift @@ -12,33 +12,50 @@ public struct IntrospectionScope: OptionSet { } extension View { - @ViewBuilder public func introspect( _ viewType: SwiftUIViewType, on platforms: (PlatformViewVersions)..., scope: IntrospectionScope? = nil, customize: @escaping (PlatformSpecificEntity) -> Void ) -> some View { + self.modifier(IntrospectModifier(viewType, platforms: platforms, scope: scope, customize: customize)) + } +} + +struct IntrospectModifier: ViewModifier { + let id = IntrospectionViewID() + let scope: IntrospectionScope + let selector: IntrospectionSelector? + let customize: (PlatformSpecificEntity) -> Void + + init( + _ viewType: SwiftUIViewType, + platforms: [PlatformViewVersions], + scope: IntrospectionScope?, + customize: @escaping (PlatformSpecificEntity) -> Void + ) { + self.scope = scope ?? viewType.scope if let platform = platforms.first(where: \.isCurrent) { - let introspectionViewID = IntrospectionViewID() - self.background( - IntrospectionAnchorView( - id: introspectionViewID - ) - .frame(width: 0, height: 0) - ) - .overlay( - IntrospectionView( - id: introspectionViewID, - selector: { controller in - (platform.selector ?? .default)(controller, scope ?? viewType.scope) - }, - customize: customize - ) - .frame(width: 0, height: 0) - ) + self.selector = platform.selector ?? .default } else { - self + self.selector = nil + } + self.customize = customize + } + + func body(content: Content) -> some View { + if let selector { + content + .background( + IntrospectionAnchorView(id: id) + .frame(width: 0, height: 0) + ) + .overlay( + IntrospectionView(id: id, selector: { selector($0, scope) }, customize: customize) + .frame(width: 0, height: 0) + ) + } else { + content } } }