Skip to content

Commit f46200a

Browse files
authored
Fix NSHostingView async rendering cycle on macOS (#898)
1 parent fb8f421 commit f46200a

1 file changed

Lines changed: 26 additions & 25 deletions

File tree

‎Sources/OpenSwiftUI/Integration/Hosting/AppKit/View/NSHostingView.swift‎

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -421,28 +421,22 @@ open class NSHostingView<Content>: NSView, XcodeViewDebugDataProvider where Cont
421421
// TODO
422422
NSAnimationContext.runAnimationGroup { context in
423423
context.allowsImplicitAnimation = false
424-
isUpdating = true
425-
// TODO
426-
render(targetTimestamp: nil)
427-
// TODO
428-
isUpdating = false
429-
// TODO
430-
if needsDeferredUpdate {
431-
if !viewGraph.updateRequiredMainThread {
432-
if isLinkedOnOrAfter(.v3) {
433-
if startAsyncRendering() {
434-
needsDeferredUpdate = false
435-
}
424+
var iteration = 0
425+
repeat {
426+
needsDeferredUpdate = false
427+
isUpdating = true
428+
render(targetTimestamp: nil)
429+
isUpdating = false
430+
if needsDeferredUpdate,
431+
!viewGraph.updateRequiredMainThread,
432+
isLinkedOnOrAfter(.v3),
433+
!viewGraph.mayDeferUpdate {
434+
if startAsyncRendering() {
435+
needsDeferredUpdate = false
436436
}
437437
}
438-
}
439-
if needsDeferredUpdate {
440-
onNextMainRunLoop { [weak self] in
441-
guard let self else { return }
442-
setNeedsUpdate()
443-
}
444-
needsDeferredUpdate = false
445-
}
438+
iteration += 1
439+
} while needsDeferredUpdate && iteration < 8
446440
}
447441
}
448442

@@ -782,8 +776,12 @@ open class NSHostingView<Content>: NSView, XcodeViewDebugDataProvider where Cont
782776
static func defaultViewGraphOutputs() -> ViewGraph.Outputs { .defaults }
783777

784778
func setNeedsUpdate() {
785-
needsUpdateConstraints = true
786-
needsLayout = true
779+
if isUpdating {
780+
needsDeferredUpdate = true
781+
} else {
782+
needsUpdateConstraints = true
783+
needsLayout = true
784+
}
787785
}
788786

789787
// MARK: - Window Notification Handlers
@@ -954,10 +952,13 @@ extension NSHostingView: ViewRendererHost {
954952

955953
if adjustedDelay >= 0.25 {
956954
setUpdateTimer(delay: adjustedDelay)
957-
} else if isUpdating {
958-
needsDeferredUpdate = true
959-
} else {
955+
} else if adjustedDelay == 0 || !isUpdating {
960956
setNeedsUpdate()
957+
} else {
958+
onNextMainRunLoop { [weak self] in
959+
guard let self else { return }
960+
setNeedsUpdate()
961+
}
961962
}
962963

963964
// TODO: Notify delegate

0 commit comments

Comments
 (0)