mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-14 18:12:09 +08:00
fix(visionos): multi-scene improvements (#10653)
This commit is contained in:
@ -2,6 +2,14 @@ import SwiftUI
|
|||||||
import NativeScriptEmbedder
|
import NativeScriptEmbedder
|
||||||
import UIKit
|
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, *)
|
@available(iOS 14.0, *)
|
||||||
struct NativeScriptMainWindow: Scene {
|
struct NativeScriptMainWindow: Scene {
|
||||||
|
|
||||||
@ -18,12 +26,18 @@ struct NativeScriptMainWindow: Scene {
|
|||||||
// windowStyle is only supported on visionOS
|
// windowStyle is only supported on visionOS
|
||||||
WindowGroup {
|
WindowGroup {
|
||||||
NativeScriptAppView(found: { windowScene in
|
NativeScriptAppView(found: { windowScene in
|
||||||
NativeScriptEmbedder.sharedInstance().setWindowScene(windowScene)
|
if (!hasMainSetMainScene) {
|
||||||
|
hasMainSetMainScene = true
|
||||||
|
NativeScriptEmbedder.sharedInstance().setWindowScene(windowScene)
|
||||||
|
}
|
||||||
}).onAppear {
|
}).onAppear {
|
||||||
// print("NativeScriptAppView onAppear")
|
// print("NativeScriptAppView onAppear")
|
||||||
DispatchQueue.main.async {
|
if (!hasMainBoot) {
|
||||||
NativeScriptEmbedder.boot()
|
hasMainBoot = true
|
||||||
}
|
DispatchQueue.main.async {
|
||||||
|
NativeScriptEmbedder.boot()
|
||||||
|
}
|
||||||
|
}
|
||||||
}.onReceive(NotificationCenter.default
|
}.onReceive(NotificationCenter.default
|
||||||
.publisher(for: NSNotification.Name("NativeScriptWindowOpen")), perform: { obj in
|
.publisher(for: NSNotification.Name("NativeScriptWindowOpen")), perform: { obj in
|
||||||
let info = parseWindowInfo(obj: obj)
|
let info = parseWindowInfo(obj: obj)
|
||||||
@ -68,9 +82,12 @@ struct NativeScriptMainWindow: Scene {
|
|||||||
}
|
}
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
NativeScriptViewFactory.initShared()
|
if (!hasMainInit) {
|
||||||
NativeScriptEmbedder.sharedInstance().setDelegate(NativeScriptViewFactory.shared)
|
hasMainInit = true
|
||||||
NativeScriptEmbedder.setup()
|
NativeScriptViewFactory.initShared()
|
||||||
|
NativeScriptEmbedder.sharedInstance().setDelegate(NativeScriptViewFactory.shared)
|
||||||
|
NativeScriptEmbedder.setup()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#if os(visionOS)
|
#if os(visionOS)
|
||||||
|
Reference in New Issue
Block a user