forked from ole/swiftui-layout-inspector
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLogEntriesTable.swift
71 lines (66 loc) · 2.37 KB
/
LogEntriesTable.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
import SwiftUI
@available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *)
public struct LogEntriesTable: View {
var logEntries: [LogEntry]
@Binding var highlight: String?
@State private var selectedRow: LogEntry.ID? = nil
public init(logEntries: [LogEntry], highlight: Binding<String?>? = nil) {
self.logEntries = logEntries
if let binding = highlight {
self._highlight = binding
} else {
var nirvana: String? = nil
self._highlight = Binding(get: { nirvana }, set: { nirvana = $0 })
}
}
public var body: some View {
Table(logEntries, selection: $selectedRow) {
TableColumn("View") { item in
let shouldHighlight = highlight == item.label
HStack {
indentation(level: item.indent)
Text(item.label)
Image(systemName: "circle.fill")
.font(Font.caption2)
.foregroundStyle(.tint)
.opacity(shouldHighlight ? 1 : 0)
}
}
TableColumn("Proposal") { item in
Text(item.proposal?.pretty ?? "…")
.monospacedDigit()
.fixedSize()
.foregroundStyle(.primary)
}
TableColumn("Response") { item in
Text(item.response?.pretty ?? "…")
.monospacedDigit()
.fixedSize()
.foregroundStyle(.primary)
}
}
.onChange(of: highlight) { viewLabel in
let selectedLogEntry = logEntries.first { $0.id == selectedRow }
if viewLabel != selectedLogEntry?.label {
selectedRow = nil
}
}
.onChange(of: selectedRow) { rowID in
let selectedLogEntry = logEntries.first { $0.id == rowID }
highlight = selectedLogEntry?.label
}
.font(.callout)
}
private func indentation(level: Int) -> some View {
ForEach(0 ..< level, id: \.self) { _ in
Color.clear
.frame(width: 12)
}
}
}
@available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *)
struct LogEntriesTable_Previews: PreviewProvider {
static var previews: some View {
LogEntriesTable(logEntries: sampleLogEntries)
}
}