Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Example/Shared/PreviewShims.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// PreviewShims.swift
// Shared

import OpenSwiftUI
import SwiftUI

// Helper method before we add fully preview thunk and preview macro support for OpenSwiftUI

#if canImport(AppKit) && !targetEnvironment(macCatalyst)
import AppKit
extension OpenSwiftUI.View {
func _previewVC() -> some NSViewController {
OpenSwiftUI.NSHostingController(rootView: self)
}
}
extension SwiftUI.View {
func _previewVC() -> some NSViewController {
SwiftUI.NSHostingController(rootView: self)
}
}
#else
import UIKit
extension OpenSwiftUI.View {
func _previewVC() -> some UIViewController {
OpenSwiftUI.UIHostingController(rootView: self)
}
}
extension SwiftUI.View {
func _previewVC() -> some UIViewController {
SwiftUI.UIHostingController(rootView: self)
}
}
#endif

#Preview {
// FIXME: Not working on macOS yet
ContentView()
._previewVC()
}
21 changes: 18 additions & 3 deletions Sources/OpenSwiftUICore/Graph/GraphHost.swift
Original file line number Diff line number Diff line change
Expand Up @@ -690,11 +690,26 @@ extension Graph {
}
}

// MARK: - Preview
// MARK: - Preview [6.5.4]

private var blockedGraphHosts: [Unmanaged<GraphHost>] = []
private let waitingForPreviewThunks = EnvironmentHelper.bool(for: "XCODE_RUNNING_FOR_PREVIEWS")
// NOTE: In SwiftUI, PreviewsInjection.framework's DYLDDynamicProductLoader calls
// SwiftUI.__previewThunksHaveFinishedLoading() via a library-specific GOT binding after
// all preview dylibs are dlopen'd. This unblocks graph instantiation for waiting hosts.
// Since the binding targets SwiftUI specifically (two-level namespace), OpenSwiftUI's
// version is never called. We disable the blocking to allow graph instantiation in Preview.
// TODO: Re-enable when OpenSwiftUI implements its own preview thunk registration system.
private var waitingForPreviewThunks = false // EnvironmentHelper.bool(for: "XCODE_RUNNING_FOR_PREVIEWS")

public func __previewThunksHaveFinishedLoading() {
_openSwiftUIUnimplementedFailure()
guard waitingForPreviewThunks else { return }
waitingForPreviewThunks = false
let hosts = blockedGraphHosts
blockedGraphHosts = []
for host in hosts {
let graphHost = host.takeUnretainedValue()
if let graphDelegate = graphHost.graphDelegate {
graphDelegate.graphDidChange()
}
}
}
Loading