Skip to content

Commit d80ad69

Browse files
committed
Coalesce proposal and response if there are no other views in between
1 parent e7f4ca8 commit d80ad69

File tree

1 file changed

+64
-15
lines changed

1 file changed

+64
-15
lines changed

LayoutInspector/DebugLayout.swift

+64-15
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ struct DebugLayout: Layout {
2828

2929
func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {
3030
assert(subviews.count == 1)
31-
log(label, action: "P", value: proposal.pretty)
32-
let result = subviews[0].sizeThatFits(proposal)
33-
log(label, action: "", value: result.pretty)
34-
return result
31+
log(label, action: .proposal(proposal))
32+
let response = subviews[0].sizeThatFits(proposal)
33+
log(label, action: .response(response))
34+
return response
3535
}
3636

3737
func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
@@ -91,16 +91,47 @@ final class Console: ObservableObject {
9191
@Published var log: [LogItem] = []
9292

9393
struct LogItem: Identifiable {
94+
enum Action {
95+
case proposal(ProposedViewSize)
96+
case response(CGSize)
97+
case proposalAndResponse(proposal: ProposedViewSize, response: CGSize)
98+
}
99+
94100
var id: UUID = .init()
95101
var label: String
96-
var action: String
97-
var value: String
102+
var action: Action
103+
104+
var proposal: ProposedViewSize? {
105+
switch action {
106+
case .proposal(let p): return p
107+
case .response(_): return nil
108+
case .proposalAndResponse(proposal: let p, response: _): return p
109+
}
110+
}
111+
112+
var response: CGSize? {
113+
switch action {
114+
case .proposal(_): return nil
115+
case .response(let r): return r
116+
case .proposalAndResponse(proposal: _, response: let r): return r
117+
}
118+
}
98119
}
99120
}
100121

101-
func log(_ label: String, action: String, value: String) {
122+
func log(_ label: String, action: Console.LogItem.Action) {
102123
DispatchQueue.main.async {
103-
Console.shared.log.append(.init(label: label, action: action, value: value))
124+
if var lastLogItem = Console.shared.log.last,
125+
lastLogItem.label == label,
126+
case .proposal(let proposal) = lastLogItem.action,
127+
case .response(let response) = action
128+
{
129+
Console.shared.log.removeLast()
130+
lastLogItem.action = .proposalAndResponse(proposal: proposal, response: response)
131+
Console.shared.log.append(lastLogItem)
132+
} else {
133+
Console.shared.log.append(.init(label: label, action: action))
134+
}
104135
}
105136
}
106137

@@ -143,13 +174,31 @@ struct ConsoleView: View {
143174
.frame(maxWidth: .infinity, alignment: .leading)
144175
.padding(.horizontal, 8)
145176

146-
Text(item.action)
147-
.font(.headline)
148-
149-
Text(item.value)
150-
.monospacedDigit()
151-
.gridColumnAlignment(.trailing)
152-
.padding(.horizontal, 8)
177+
if let proposal = item.proposal {
178+
Text("P")
179+
.font(.headline)
180+
181+
Text(proposal.pretty)
182+
.monospacedDigit()
183+
.gridColumnAlignment(.trailing)
184+
.padding(.horizontal, 8)
185+
} else {
186+
Text("")
187+
Text("")
188+
}
189+
190+
if let response = item.response {
191+
Text("")
192+
.font(.headline)
193+
194+
Text(response.pretty)
195+
.monospacedDigit()
196+
.gridColumnAlignment(.trailing)
197+
.padding(.horizontal, 8)
198+
} else {
199+
Text("")
200+
Text("")
201+
}
153202
}
154203
.padding(.vertical, 8)
155204
.foregroundColor(isSelected ? .white : nil)

0 commit comments

Comments
 (0)