fix(visionos): multi-scene improvements (#10653)

This commit is contained in:
Nathan Walker
2024-11-18 19:42:34 -08:00
committed by GitHub
parent e5caa2cc53
commit d6922b9896

View File

@ -2,6 +2,14 @@ import SwiftUI
import NativeScriptEmbedder
import UIKit
// Ensure runtime phases are called only once per app lifecycle
// In multi-scene apps, we need to ensure runtime doesn't reinit alongside main window
// For example, if @State is used to drive @Environment at the root level, we need to ensure
// a rerender of the main body doesn't cause runtime to cycle again
var hasMainInit = false
var hasMainBoot = false
var hasMainSetMainScene = false
@available(iOS 14.0, *)
struct NativeScriptMainWindow: Scene {
@ -18,12 +26,18 @@ struct NativeScriptMainWindow: Scene {
// windowStyle is only supported on visionOS
WindowGroup {
NativeScriptAppView(found: { windowScene in
if (!hasMainSetMainScene) {
hasMainSetMainScene = true
NativeScriptEmbedder.sharedInstance().setWindowScene(windowScene)
}
}).onAppear {
// print("NativeScriptAppView onAppear")
if (!hasMainBoot) {
hasMainBoot = true
DispatchQueue.main.async {
NativeScriptEmbedder.boot()
}
}
}.onReceive(NotificationCenter.default
.publisher(for: NSNotification.Name("NativeScriptWindowOpen")), perform: { obj in
let info = parseWindowInfo(obj: obj)
@ -68,10 +82,13 @@ struct NativeScriptMainWindow: Scene {
}
init() {
if (!hasMainInit) {
hasMainInit = true
NativeScriptViewFactory.initShared()
NativeScriptEmbedder.sharedInstance().setDelegate(NativeScriptViewFactory.shared)
NativeScriptEmbedder.setup()
}
}
#if os(visionOS)
func parseWindowInfo(obj: Notification, close: Bool = false) -> [String:Bool] {