Skip to content

Commit 3702b87

Browse files
committed
Add height slider, UI improvements
1 parent 9c664b9 commit 3702b87

File tree

2 files changed

+65
-57
lines changed

2 files changed

+65
-57
lines changed

LayoutInspector/ContentView.swift

+20-12
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,45 @@ import SwiftUI
22

33
struct ContentView: View {
44
@State private var width: CGFloat = 300
5+
@State private var height: CGFloat = 100
56
@State private var selectedView: String? = nil
67
@State private var generation: Int = 0
78

89
var subject: some View {
9-
HStack {
10-
Rectangle().fill(.green)
11-
.debugLayout("green")
12-
Text("Hello world")
13-
.debugLayout("Text")
14-
Rectangle().fill(.yellow)
15-
.debugLayout("yellow")
16-
}
17-
.debugLayout("HStack")
10+
Text("Hello")
11+
.debugLayout("Text")
12+
.aspectRatio(1, contentMode: .fit)
13+
.debugLayout("aspectRatio")
1814
}
1915

2016
var body: some View {
2117
VStack {
22-
VStack(spacing: 24) {
18+
VStack {
2319
subject
2420
.clearConsole()
2521
.environment(\.debugLayoutSelection, selectedView)
26-
.frame(width: width, height: 80)
22+
.frame(width: width, height: height)
23+
.overlay {
24+
Rectangle()
25+
.strokeBorder(style: StrokeStyle(dash: [5]))
26+
}
2727
.id(generation)
28+
.padding(.bottom, 16)
2829

2930
LabeledContent {
30-
Slider(value: $width, in: 100...500, step: 1)
31+
Slider(value: $width, in: 50...500, step: 1)
3132
} label: {
3233
Text("Width: \(width, format: .number.precision(.fractionLength(0)))")
3334
.monospacedDigit()
3435
}
3536

37+
LabeledContent {
38+
Slider(value: $height, in: 50...500, step: 1)
39+
} label: {
40+
Text("Height: \(height, format: .number.precision(.fractionLength(0)))")
41+
.monospacedDigit()
42+
}
43+
3644
Button("Reset layout cache") {
3745
generation += 1
3846
}

LayoutInspector/DebugLayout.swift

+45-45
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@ struct DebugLayoutWrapper: ViewModifier {
1717
@Environment(\.debugLayoutSelection) private var selection: String?
1818

1919
func body(content: Content) -> some View {
20-
let isSelected = label == selection
2120
content
22-
.border(isSelected ? Color.pink : .clear, width: 2)
21+
.overlay {
22+
let isSelected = label == selection
23+
if isSelected {
24+
Rectangle()
25+
.strokeBorder(style: StrokeStyle(lineWidth: 1, dash: [5]))
26+
.foregroundColor(.pink)
27+
}
28+
}
2329
}
2430
}
2531

@@ -156,61 +162,55 @@ struct Selection<Value: Equatable>: PreferenceKey {
156162

157163
struct ConsoleView: View {
158164
@ObservedObject var console = Console.shared
159-
@State private var selection: String? = "Text"
165+
@State private var selection: String? = nil
160166

161167
var body: some View {
162168
ScrollView(.vertical) {
163-
VStack(alignment: .leading, spacing: 16) {
164-
Text("Layout Log")
165-
.font(.headline)
166-
.padding(.top, 16)
169+
Grid(alignment: .leadingFirstTextBaseline, horizontalSpacing: 0, verticalSpacing: 0) {
170+
GridRow {
171+
Text("View")
172+
Text("Proposal")
173+
Text("Response")
174+
}
175+
.font(.headline)
176+
.padding(.vertical, 4)
177+
.padding(.horizontal, 8)
178+
179+
Rectangle().fill(.secondary)
180+
.frame(height: 1)
181+
.gridCellUnsizedAxes(.horizontal)
182+
.padding(.vertical, 4)
167183
.padding(.horizontal, 8)
168184

169-
Grid(alignment: .leadingFirstTextBaseline, horizontalSpacing: 0, verticalSpacing: 0) {
185+
ForEach(console.log) { item in
186+
let isSelected = selection == item.label
170187
GridRow {
171-
Text("View")
172-
Text("Proposal")
173-
Text("Response")
188+
Text(item.label)
189+
.font(.body)
190+
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
191+
192+
Text(item.proposal?.pretty ?? "")
193+
.monospacedDigit()
194+
.fixedSize()
195+
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
196+
197+
Text(item.response?.pretty ?? "")
198+
.monospacedDigit()
199+
.fixedSize()
200+
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
174201
}
175-
.font(.headline)
202+
.font(.callout)
176203
.padding(.vertical, 4)
177204
.padding(.horizontal, 8)
178-
179-
Rectangle().fill(.secondary)
180-
.frame(height: 1)
181-
.gridCellUnsizedAxes(.horizontal)
182-
.padding(.vertical, 4)
183-
.padding(.horizontal, 8)
184-
185-
ForEach(console.log) { item in
186-
let isSelected = selection == item.label
187-
GridRow {
188-
Text(item.label)
189-
.font(.body)
190-
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
191-
192-
Text(item.proposal?.pretty ?? "")
193-
.monospacedDigit()
194-
.fixedSize()
195-
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
196-
197-
Text(item.response?.pretty ?? "")
198-
.monospacedDigit()
199-
.fixedSize()
200-
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
201-
}
202-
.font(.callout)
203-
.padding(.vertical, 4)
204-
.padding(.horizontal, 8)
205-
.foregroundColor(isSelected ? .white : nil)
206-
.background(isSelected ? Color.accentColor : .clear)
207-
.contentShape(Rectangle())
208-
.onTapGesture {
209-
selection = isSelected ? nil : item.label
210-
}
205+
.foregroundColor(isSelected ? .white : nil)
206+
.background(isSelected ? Color.accentColor : .clear)
207+
.contentShape(Rectangle())
208+
.onTapGesture {
209+
selection = isSelected ? nil : item.label
211210
}
212211
}
213212
}
213+
.padding(.vertical, 8)
214214
}
215215
.background(Color(uiColor: .secondarySystemBackground))
216216
.preference(key: Selection.self, value: selection)

0 commit comments

Comments
 (0)