forked from ole/swiftui-layout-inspector
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathContentView.swift
107 lines (95 loc) · 3.27 KB
/
ContentView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import SwiftUI
import DebugLayout
struct ContentView: View {
var body: some View {
Inspector {
/// The view tree whose layout you want to inspect. Add `.debugLayout()` calls at
/// each point where you want to inspect the layout algorithm, i.e. what sizes are
/// being proposed and returned. We call these **inspection points**.
hStackExample
}
}
var paddingExample: some View {
Text("Hello world")
.debugLayout("Text")
.padding(10)
.debugLayout("padding")
.border(Color.green)
.debugLayout("border")
}
var hStackExample: some View {
HStack(spacing: 10) {
Rectangle().fill(.green)
.debugLayout("green")
Text("Hello world")
.debugLayout("Text")
Rectangle().fill(.yellow)
.debugLayout("yellow")
}
.debugLayout("HStack")
}
var fixedSizeExample: some View {
Text("Lorem ipsum dolor sit amet")
.debugLayout("Text")
.fixedSize()
.debugLayout("fixedSize")
.frame(width: 100)
.debugLayout("frame")
.border(Color.green)
}
}
struct Inspector<Subject: View>: View {
@ViewBuilder var subject: Subject
@State private var width: CGFloat = 300
@State private var height: CGFloat = 100
@State private var selectedView: String? = nil
@State private var generation: Int = 0
var body: some View {
VStack {
VStack {
subject
.startDebugLayout(selection: selectedView)
.id(generation)
.frame(width: width, height: height)
.overlay {
Rectangle()
.strokeBorder(style: StrokeStyle(dash: [5]))
}
.padding(.bottom, 16)
VStack {
LabeledContent {
HStack {
Slider(value: $width, in: 50...500, step: 1)
Stepper("Width", value: $width)
}
.labelsHidden()
} label: {
Text("W \(width, format: .number.precision(.fractionLength(0)))")
.monospacedDigit()
}
LabeledContent {
HStack {
Slider(value: $height, in: 50...500, step: 1)
Stepper("Height", value: $height)
}
.labelsHidden()
} label: {
Text("H \(height, format: .number.precision(.fractionLength(0)))")
.monospacedDigit()
}
Button("Reset layout cache") {
generation += 1
}
}
.buttonStyle(.bordered)
}
.padding()
DebugLayoutLogView(selection: $selectedView)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}