@@ -20,48 +20,52 @@ enum ViewInspection {
2020 return ( 0 ..< childrenCount) . map { Self . attribute ( label: " . \( $0) " , value: tupleViews) as! any View }
2121 }
2222
23- public static func children( of view: some View ) -> [ ( any View , [ any ViewModifier ] ) ] {
23+ public static func childrenIfMultiple( of view: some View ) -> [ ( any View , [ any ViewModifier ] ) ] {
24+ let children = children ( of: view)
25+ if children. count == 1 {
26+ return [ ( view, [ ] ) ]
27+ }
28+ return children
29+ }
30+
31+ private static func children( of view: some View ) -> [ ( any View , [ any ViewModifier ] ) ] {
2432 let viewType = type ( of: view)
2533 let typeName = String ( reflecting: viewType)
2634 if typeName. starts ( with: " SwiftUI._ConditionalContent " ) {
2735 let storage = Self . attribute ( label: " storage " , value: view) !
2836 if let trueContent = Self . attribute ( label: " trueContent " , value: storage) as? any View {
29- return children ( of: trueContent)
37+ return childrenIfMultiple ( of: trueContent)
3038 } else {
3139 let content = Self . attribute ( label: " falseContent " , value: storage) as! any View
32- return children ( of: content)
40+ return childrenIfMultiple ( of: content)
3341 }
3442 } else if typeName. starts ( with: " SwiftUI.EmptyView " ) {
3543 return [ ]
3644 } else if typeName. starts ( with: " SwiftUI.Tuple " ) {
37- return Self . tupleChildren ( view) . flatMap { children ( of: $0) }
45+ return tupleChildren ( view) . flatMap { childrenIfMultiple ( of: $0) }
3846 } else if typeName. starts ( with: " SwiftUI.Group " ) {
39- let content = Self . attribute ( label: " content " , value: view) as! any View
40- return children ( of: content)
47+ let content = Self . attribute ( label: " content " , value: view) as! any View
48+ return childrenIfMultiple ( of: content)
4149 } else if typeName. starts ( with: " SwiftUI.ForEach " ) , let provider = view as? ViewsProvider {
42- return provider. views ( ) . flatMap { children ( of: $0) }
50+ return provider. views ( ) . flatMap { childrenIfMultiple ( of: $0) }
4351 } else if typeName. starts ( with: " SwiftUI.AnyView " ) {
4452 let storage = Self . attribute ( label: " storage " , value: view) !
4553 let storageView = Self . attribute ( label: " view " , value: storage) as! any View
46- return children ( of: storageView)
54+ return childrenIfMultiple ( of: storageView)
4755 } else if typeName. starts ( with: " SwiftUI.ModifiedContent " ) {
4856 let content = Self . attribute ( label: " content " , value: view) as! any View
4957 let modifier = Self . attribute ( label: " modifier " , value: view) as! any ViewModifier
50- return children ( of: content) . map { ( view, modifiers) in
58+ return childrenIfMultiple ( of: content) . map { ( view, modifiers) in
5159 var modifiers = modifiers
5260 modifiers. append ( modifier)
5361 return ( view, modifiers)
5462 }
5563 }
5664 if viewType. Body != Never . self && !typeName. starts ( with: " SwiftUI. " ) {
57- let children = children ( of: view. body)
58- if children. count == 1 {
59- return [ ( view, [ ] ) ]
60- }
61- return children
65+ return childrenIfMultiple ( of: view. body)
6266 }
6367 return [ ( view, [ ] ) ]
64- }
68+ }
6569
6670 public static func preferredColorScheme( of view: some View ) -> ColorScheme ? {
6771 let typeName = String ( reflecting: type ( of: view) )
0 commit comments