Skip to content

Commit e7f4ca8

Browse files
committed
Support view selection in DebugLayout console
1 parent f2426a1 commit e7f4ca8

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

LayoutInspector/ContentView.swift

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import SwiftUI
22

33
struct ContentView: View {
44
@State var width: CGFloat = 300
5+
@State var selectedView: String? = nil
56

67
var body: some View {
78
VStack {
@@ -16,6 +17,7 @@ struct ContentView: View {
1617
}
1718
.debugLayout("HStack")
1819
.clearConsole()
20+
.environment(\.debugLayoutSelection, selectedView)
1921
.frame(width: width, height: 80)
2022

2123
LabeledContent {
@@ -28,6 +30,9 @@ struct ContentView: View {
2830
.padding()
2931

3032
ConsoleView()
33+
.onPreferenceChange(Selection.self) { selection in
34+
selectedView = selection
35+
}
3136
}
3237
}
3338
}

LayoutInspector/DebugLayout.swift

+49-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,21 @@ import SwiftUI
55

66
extension View {
77
func debugLayout(_ label: String) -> some View {
8-
DebugLayout(label: label) { self }
8+
DebugLayout(label: label) {
9+
self
10+
}
11+
.modifier(DebugLayoutWrapper(label: label))
12+
}
13+
}
14+
15+
struct DebugLayoutWrapper: ViewModifier {
16+
var label: String
17+
@Environment(\.debugLayoutSelection) private var selection: String?
18+
19+
func body(content: Content) -> some View {
20+
let isSelected = label == selection
21+
content
22+
.border(isSelected ? Color.blue : .clear, width: 2)
923
}
1024
}
1125

@@ -90,36 +104,68 @@ func log(_ label: String, action: String, value: String) {
90104
}
91105
}
92106

107+
struct DebugLayoutSelection: EnvironmentKey {
108+
static var defaultValue: String? { nil }
109+
}
110+
111+
extension EnvironmentValues {
112+
var debugLayoutSelection: String? {
113+
get { self[DebugLayoutSelection.self] }
114+
set { self[DebugLayoutSelection.self] = newValue }
115+
}
116+
}
117+
118+
struct Selection<Value: Equatable>: PreferenceKey {
119+
static var defaultValue: Value? { nil }
120+
121+
static func reduce(value: inout Value?, nextValue: () -> Value?) {
122+
value = value ?? nextValue()
123+
}
124+
}
125+
93126
struct ConsoleView: View {
94127
@ObservedObject var console = Console.shared
128+
@State private var selection: String? = nil
95129

96130
var body: some View {
97131
ScrollView(.vertical) {
98132
VStack(alignment: .leading, spacing: 16) {
99133
Text("Layout Log")
100134
.font(.headline)
135+
.padding(.top, 16)
136+
.padding(.horizontal, 8)
101137

102-
Grid(alignment: .leadingFirstTextBaseline, horizontalSpacing: 8, verticalSpacing: 8) {
138+
Grid(alignment: .leadingFirstTextBaseline, horizontalSpacing: 0, verticalSpacing: 0) {
103139
ForEach(console.log) { item in
140+
let isSelected = selection == item.label
104141
GridRow {
105142
Text(item.label)
106143
.frame(maxWidth: .infinity, alignment: .leading)
144+
.padding(.horizontal, 8)
107145

108146
Text(item.action)
109147
.font(.headline)
110148

111149
Text(item.value)
112150
.monospacedDigit()
113151
.gridColumnAlignment(.trailing)
152+
.padding(.horizontal, 8)
153+
}
154+
.padding(.vertical, 8)
155+
.foregroundColor(isSelected ? .white : nil)
156+
.background(isSelected ? Color.accentColor : .clear)
157+
.contentShape(Rectangle())
158+
.onTapGesture {
159+
selection = isSelected ? nil : item.label
114160
}
115161

116162
Divider()
117163
.gridCellUnsizedAxes(.horizontal)
118164
}
119165
}
120166
}
121-
.padding()
122167
}
123168
.background(Color(uiColor: .secondarySystemBackground))
169+
.preference(key: Selection.self, value: selection)
124170
}
125171
}

0 commit comments

Comments
 (0)