Skip to content

Commit 64ce4b1

Browse files
committed
Add @mainactor annotations
1 parent 8e74e7a commit 64ce4b1

File tree

3 files changed

+42
-37
lines changed

3 files changed

+42
-37
lines changed

Sources/LayoutInspector/DebugLayout.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import SwiftUI
33
@available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *)
44
extension View {
55
/// Inspect the layout for this subtree.
6-
public func inspectLayout() -> some View {
6+
@MainActor public func inspectLayout() -> some View {
77
modifier(InspectLayout(logStore: LogStore()))
88
}
99

1010
/// Monitor the layout proposals and responses for this view and add them
1111
/// to the log.
12-
public func layoutStep(
12+
@MainActor public func layoutStep(
1313
_ label: String,
1414
file: StaticString = #fileID,
1515
line: UInt = #line

Sources/LayoutInspector/DebugLayoutImpl.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import SwiftUI
22

33
@available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *)
4+
@MainActor
45
struct InspectLayout: ViewModifier {
56
// Don't observe LogStore. Avoids an infinite update loop.
67
var logStore: LogStore
@@ -92,6 +93,7 @@ struct InspectLayout: ViewModifier {
9293
}
9394

9495
@available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *)
96+
@MainActor
9597
struct DebugLayoutModifier: ViewModifier {
9698
var label: String
9799
var file: StaticString
@@ -127,9 +129,13 @@ struct DebugLayout: Layout {
127129
cache: inout ()
128130
) -> CGSize {
129131
assert(subviews.count == 1)
130-
actions.logLayoutStep(label, .proposal(proposal))
132+
DispatchQueue.main.async {
133+
actions.logLayoutStep(label, .proposal(proposal))
134+
}
131135
let response = subviews[0].sizeThatFits(proposal)
132-
actions.logLayoutStep(label, .response(response))
136+
DispatchQueue.main.async {
137+
actions.logLayoutStep(label, .response(response))
138+
}
133139
return response
134140
}
135141

Sources/LayoutInspector/LogStore.swift

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import SwiftUI
22

33
@available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *)
4+
@MainActor
45
final class LogStore: ObservableObject {
56
@Published var log: [LogEntry]
67
var viewLabels: Set<String> = []
@@ -34,49 +35,47 @@ final class LogStore: ObservableObject {
3435
}
3536

3637
func logLayoutStep(_ label: String, step: LogEntry.Step) {
37-
DispatchQueue.main.async { [self] in
38-
guard let prevEntry = log.last else {
39-
// First log entry → start at indent 0.
40-
log.append(LogEntry(label: label, step: step, indent: 0))
41-
return
42-
}
38+
guard let prevEntry = log.last else {
39+
// First log entry → start at indent 0.
40+
log.append(LogEntry(label: label, step: step, indent: 0))
41+
return
42+
}
4343

44-
var newEntry = LogEntry(label: label, step: step, indent: prevEntry.indent)
45-
let isSameView = prevEntry.label == label
46-
switch (isSameView, prevEntry.step, step) {
47-
case (true, .proposal(let prop), .response(let resp)):
48-
// Response follows immediately after proposal for the same view.
49-
// → We want to display them in a single row.
50-
// → Coalesce both layout steps.
51-
log.removeLast()
52-
newEntry = prevEntry
53-
newEntry.step = .proposalAndResponse(proposal: prop, response: resp)
54-
log.append(newEntry)
44+
var newEntry = LogEntry(label: label, step: step, indent: prevEntry.indent)
45+
let isSameView = prevEntry.label == label
46+
switch (isSameView, prevEntry.step, step) {
47+
case (true, .proposal(let prop), .response(let resp)):
48+
// Response follows immediately after proposal for the same view.
49+
// → We want to display them in a single row.
50+
// → Coalesce both layout steps.
51+
log.removeLast()
52+
newEntry = prevEntry
53+
newEntry.step = .proposalAndResponse(proposal: prop, response: resp)
54+
log.append(newEntry)
5555

56-
case (_, .proposal, .proposal):
57-
// A proposal follows a proposal → nested view → increment indent.
58-
newEntry.indent += 1
59-
log.append(newEntry)
56+
case (_, .proposal, .proposal):
57+
// A proposal follows a proposal → nested view → increment indent.
58+
newEntry.indent += 1
59+
log.append(newEntry)
6060

61-
case (_, .response, .response),
62-
(_, .proposalAndResponse, .response):
63-
// A response follows a response → last child returns to parent → decrement indent.
64-
newEntry.indent -= 1
65-
log.append(newEntry)
61+
case (_, .response, .response),
62+
(_, .proposalAndResponse, .response):
63+
// A response follows a response → last child returns to parent → decrement indent.
64+
newEntry.indent -= 1
65+
log.append(newEntry)
6666

67-
default:
68-
// Keep current indentation.
69-
log.append(newEntry)
70-
}
67+
default:
68+
// Keep current indentation.
69+
log.append(newEntry)
7170
}
7271
}
7372
}
7473

7574
@available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *)
7675
struct DebugLayoutActions {
77-
var clearLog: () -> Void
78-
var registerViewLabelAndWarnIfNotUnique: (_ label: String, _ file: StaticString, _ line: UInt) -> Void
79-
var logLayoutStep: (_ label: String, _ step: LogEntry.Step) -> Void
76+
var clearLog: @MainActor () -> Void
77+
var registerViewLabelAndWarnIfNotUnique: @MainActor (_ label: String, _ file: StaticString, _ line: UInt) -> Void
78+
var logLayoutStep: @MainActor (_ label: String, _ step: LogEntry.Step) -> Void
8079
}
8180

8281
@available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *)

0 commit comments

Comments
 (0)