1
1
import SwiftUI
2
+ import React
2
3
3
4
/**
4
5
This SwiftUI struct returns main React Native scene. It should be used only once as it conains setup code.
@@ -7,11 +8,11 @@ import SwiftUI
7
8
```swift
8
9
@main
9
10
struct YourApp: App {
10
- @UIApplicationDelegateAdaptor var delegate: AppDelegate
11
-
12
- var body: some Scene {
13
- RCTMainWindow(moduleName: "YourApp")
14
- }
11
+ @UIApplicationDelegateAdaptor var delegate: AppDelegate
12
+
13
+ var body: some Scene {
14
+ RCTMainWindow(moduleName: "YourApp")
15
+ }
15
16
}
16
17
```
17
18
@@ -21,25 +22,47 @@ public struct RCTMainWindow: Scene {
21
22
var moduleName : String
22
23
var initialProps : RCTRootViewRepresentable . InitialPropsType
23
24
var onOpenURLCallback : ( ( URL ) -> ( ) ) ?
25
+ var contentView : AnyView ?
24
26
27
+ var rootView : RCTRootViewRepresentable {
28
+ RCTRootViewRepresentable ( moduleName: moduleName, initialProps: initialProps)
29
+ }
30
+
25
31
public init ( moduleName: String , initialProps: RCTRootViewRepresentable . InitialPropsType = nil ) {
26
32
self . moduleName = moduleName
27
33
self . initialProps = initialProps
34
+ self . contentView = AnyView ( rootView)
35
+ }
36
+
37
+ public init < Content: View > (
38
+ moduleName: String ,
39
+ initialProps: RCTRootViewRepresentable . InitialPropsType = nil ,
40
+ devMenuPlacement: ToolbarPlacement = . bottomBar,
41
+ @ViewBuilder contentView: @escaping ( _ view: RCTRootViewRepresentable ) -> Content
42
+ ) {
43
+ self . moduleName = moduleName
44
+ self . initialProps = initialProps
45
+ self . contentView = AnyView ( contentView ( rootView) )
28
46
}
29
47
30
48
public var body : some Scene {
31
49
WindowGroup {
32
- RCTRootViewRepresentable ( moduleName : moduleName , initialProps : initialProps )
50
+ contentView
33
51
. modifier ( WindowHandlingModifier ( ) )
34
52
. onOpenURL ( perform: { url in
35
53
onOpenURLCallback ? ( url)
36
54
} )
55
+ #if DEBUG
56
+ . toolbar {
57
+ DevMenuView ( placement: . bottomOrnament)
58
+ }
59
+ #endif
37
60
}
38
61
}
39
62
}
40
63
41
64
extension RCTMainWindow {
42
- public func onOpenURL( perform action: @escaping ( URL ) -> ( ) ) -> some Scene {
65
+ public func onOpenURL( perform action: @escaping ( URL ) -> ( ) ) -> Self {
43
66
var scene = self
44
67
scene. onOpenURLCallback = action
45
68
return scene
@@ -95,3 +118,27 @@ public struct WindowHandlingModifier: ViewModifier {
95
118
}
96
119
}
97
120
}
121
+
122
+ struct DevMenuView : ToolbarContent {
123
+ let placement : ToolbarItemPlacement
124
+
125
+ var body : some ToolbarContent {
126
+ ToolbarItem ( placement: placement) {
127
+ Button ( action: {
128
+ RCTTriggerReloadCommandListeners ( " User Reload " )
129
+ } , label: {
130
+ Image ( systemName: " arrow.clockwise " )
131
+ } )
132
+ }
133
+ ToolbarItem ( placement: placement) {
134
+ Button ( action: {
135
+ NotificationCenter . default. post (
136
+ Notification ( name: Notification . Name ( " RCTShowDevMenuNotification " ) , object: nil )
137
+ )
138
+ } ,
139
+ label: {
140
+ Image ( systemName: " filemenu.and.selection " )
141
+ } )
142
+ }
143
+ }
144
+ }
0 commit comments