1
1
import SwiftUI
2
2
3
- func logLayoutStep( _ label: String , step: LogItem . Step ) {
3
+ func logLayoutStep( _ label: String , step: LogItem . Step , indent : Int ) {
4
4
DispatchQueue . main. async {
5
- // Coalesce layout steps if the response follow immediately after the proposal
5
+ // Coalesce layout steps if the response follows immediately after the proposal
6
6
// for the same view.
7
7
//
8
8
// In this case, proposal and response can be shown in a single row in the log.
@@ -15,7 +15,7 @@ func logLayoutStep(_ label: String, step: LogItem.Step) {
15
15
lastLogItem. step = . proposalAndResponse( proposal: proposal, response: response)
16
16
LogStore . shared. log. append ( lastLogItem)
17
17
} else {
18
- LogStore . shared. log. append ( . init( label: label, step: step) )
18
+ LogStore . shared. log. append ( . init( label: label, step: step, indent : indent ) )
19
19
}
20
20
}
21
21
}
@@ -53,6 +53,7 @@ struct LogItem: Identifiable {
53
53
var id : UUID = . init( )
54
54
var label : String
55
55
var step : Step
56
+ var indent : Int
56
57
57
58
var proposal : ProposedViewSize ? {
58
59
switch step {
@@ -86,6 +87,9 @@ struct DebugLayoutLogView: View {
86
87
@Binding var selection : String ?
87
88
@ObservedObject var logStore : LogStore
88
89
90
+ private static let tableRowHorizontalPadding : CGFloat = 8
91
+ private static let tableRowVerticalPadding : CGFloat = 4
92
+
89
93
init ( selection: Binding < String ? > ? = nil , logStore: LogStore = LogStore . shared) {
90
94
if let binding = selection {
91
95
self . _selection = binding
@@ -99,27 +103,33 @@ struct DebugLayoutLogView: View {
99
103
var body : some View {
100
104
ScrollView ( . vertical) {
101
105
Grid ( alignment: . leadingFirstTextBaseline, horizontalSpacing: 0 , verticalSpacing: 0 ) {
106
+ // Table header row
102
107
GridRow {
103
108
Text ( " View " )
104
109
Text ( " Proposal " )
105
110
Text ( " Response " )
106
111
}
107
112
. font ( . headline)
108
- . padding ( . vertical, 4 )
109
- . padding ( . horizontal, 8 )
113
+ . padding ( . vertical, Self . tableRowVerticalPadding )
114
+ . padding ( . horizontal, Self . tableRowHorizontalPadding )
110
115
116
+ // Table header separator line
111
117
Rectangle ( ) . fill ( . secondary)
112
118
. frame ( height: 1 )
113
119
. gridCellUnsizedAxes ( . horizontal)
114
- . padding ( . vertical, 4 )
115
- . padding ( . horizontal, 8 )
120
+ . padding ( . vertical, Self . tableRowVerticalPadding )
121
+ . padding ( . horizontal, Self . tableRowHorizontalPadding )
116
122
123
+ // Table rows
117
124
ForEach ( logStore. log) { item in
118
125
let isSelected = selection == item. label
119
126
GridRow {
120
- Text ( item. label)
121
- . font ( . body)
122
- . frame ( maxWidth: . infinity, maxHeight: . infinity, alignment: . leading)
127
+ HStack ( spacing: 0 ) {
128
+ indentation ( level: item. indent)
129
+ Text ( item. label)
130
+ . font ( . body)
131
+ }
132
+ . frame ( maxWidth: . infinity, maxHeight: . infinity, alignment: . leading)
123
133
124
134
Text ( item. proposal? . pretty ?? " … " )
125
135
. monospacedDigit ( )
@@ -132,8 +142,8 @@ struct DebugLayoutLogView: View {
132
142
. frame ( maxWidth: . infinity, maxHeight: . infinity, alignment: . leading)
133
143
}
134
144
. font ( . callout)
135
- . padding ( . vertical, 4 )
136
- . padding ( . horizontal, 8 )
145
+ . padding ( . vertical, Self . tableRowVerticalPadding )
146
+ . padding ( . horizontal, Self . tableRowHorizontalPadding )
137
147
. foregroundColor ( isSelected ? . white : nil )
138
148
. background ( isSelected ? Color . accentColor : . clear)
139
149
. contentShape ( Rectangle ( ) )
@@ -146,4 +156,18 @@ struct DebugLayoutLogView: View {
146
156
}
147
157
. background ( Color ( uiColor: . secondarySystemBackground) )
148
158
}
159
+
160
+ private func indentation( level: Int ) -> some View {
161
+ ForEach ( 0 ..< level, id: \. self) { _ in
162
+ Color . clear
163
+ . frame ( width: 16 )
164
+ . overlay ( alignment: . leading) {
165
+ Rectangle ( )
166
+ . frame ( width: 1 )
167
+ . padding ( . leading, 4 )
168
+ // Compensate for cell padding, we want continuous vertical lines.
169
+ . padding ( . vertical, - Self. tableRowVerticalPadding)
170
+ }
171
+ }
172
+ }
149
173
}
0 commit comments