Skip to content

Commit d312387

Browse files
committed
fix: allow to manually move dev menu to avoid conflicts
1 parent fa26a3b commit d312387

File tree

3 files changed

+45
-21
lines changed

3 files changed

+45
-21
lines changed

packages/react-native/Libraries/SwiftExtensions/RCTMainWindow.swift

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ public struct RCTMainWindow: Scene {
2222
var moduleName: String
2323
var initialProps: RCTRootViewRepresentable.InitialPropsType
2424
var onOpenURLCallback: ((URL) -> ())?
25-
var devMenuPlacement: ToolbarPlacement = .bottomOrnament
25+
var devMenuSceneAnchor: UnitPoint?
2626
var contentView: AnyView?
2727

2828
var rootView: RCTRootViewRepresentable {
29-
RCTRootViewRepresentable(moduleName: moduleName, initialProps: initialProps)
29+
RCTRootViewRepresentable(moduleName: moduleName, initialProps: initialProps, devMenuSceneAnchor: devMenuSceneAnchor)
3030
}
3131

3232
/// Creates new RCTMainWindowWindow.
@@ -38,11 +38,11 @@ public struct RCTMainWindow: Scene {
3838
public init(
3939
moduleName: String,
4040
initialProps: RCTRootViewRepresentable.InitialPropsType = nil,
41-
devMenuPlacement: ToolbarPlacement = .bottomOrnament
41+
devMenuSceneAnchor: UnitPoint? = .bottom
4242
) {
4343
self.moduleName = moduleName
4444
self.initialProps = initialProps
45-
self.devMenuPlacement = devMenuPlacement
45+
self.devMenuSceneAnchor = devMenuSceneAnchor
4646
self.contentView = AnyView(rootView)
4747
}
4848

@@ -56,12 +56,12 @@ public struct RCTMainWindow: Scene {
5656
public init<Content: View>(
5757
moduleName: String,
5858
initialProps: RCTRootViewRepresentable.InitialPropsType = nil,
59-
devMenuPlacement: ToolbarPlacement = .bottomOrnament,
59+
devMenuSceneAnchor: UnitPoint? = .bottom,
6060
@ViewBuilder contentView: @escaping (_ view: RCTRootViewRepresentable) -> Content
6161
) {
6262
self.moduleName = moduleName
6363
self.initialProps = initialProps
64-
self.devMenuPlacement = devMenuPlacement
64+
self.devMenuSceneAnchor = devMenuSceneAnchor
6565
self.contentView = AnyView(contentView(rootView))
6666
}
6767

@@ -72,11 +72,6 @@ public struct RCTMainWindow: Scene {
7272
.onOpenURL(perform: { url in
7373
onOpenURLCallback?(url)
7474
})
75-
#if DEBUG
76-
.toolbar {
77-
DevMenuView(placement: .bottomOrnament)
78-
}
79-
#endif
8075
}
8176
}
8277
}
@@ -142,18 +137,14 @@ public struct WindowHandlingModifier: ViewModifier {
142137
/**
143138
Toolbar which displays additional controls to easily open dev menu and trigger reload command.
144139
*/
145-
struct DevMenuView: ToolbarContent {
146-
let placement: ToolbarItemPlacement
147-
148-
var body: some ToolbarContent {
149-
ToolbarItem(placement: placement) {
140+
struct DevMenuView: View {
141+
var body: some View {
142+
HStack {
150143
Button(action: {
151144
RCTTriggerReloadCommandListeners("User Reload")
152145
}, label: {
153146
Image(systemName: "arrow.clockwise")
154147
})
155-
}
156-
ToolbarItem(placement: placement) {
157148
Button(action: {
158149
NotificationCenter.default.post(
159150
Notification(name: Notification.Name("RCTShowDevMenuNotification"), object: nil)
@@ -163,5 +154,22 @@ struct DevMenuView: ToolbarContent {
163154
Image(systemName: "filemenu.and.selection")
164155
})
165156
}
157+
.padding()
158+
.glassBackgroundEffect()
166159
}
167160
}
161+
162+
extension View {
163+
/// Applies the given transform if the given condition evaluates to `true`.
164+
/// - Parameters:
165+
/// - condition: The condition to evaluate.
166+
/// - transform: The transform to apply to the source `View`.
167+
/// - Returns: Either the original `View` or the modified `View` if the condition is `true`.
168+
@ViewBuilder func `if`<Content: View>(_ condition: @autoclosure () -> Bool, transform: (Self) -> Content) -> some View {
169+
if condition() {
170+
transform(self)
171+
} else {
172+
self
173+
}
174+
}
175+
}

packages/react-native/Libraries/SwiftExtensions/RCTRootViewRepresentable.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,30 @@ public struct RCTRootViewRepresentable: UIViewControllerRepresentable {
1616

1717
var moduleName: String
1818
var initialProps: InitialPropsType
19+
var devMenuSceneAnchor: UnitPoint?
1920

20-
public init(moduleName: String, initialProps: InitialPropsType = nil) {
21+
public init(
22+
moduleName: String,
23+
initialProps: InitialPropsType = nil,
24+
devMenuSceneAnchor: UnitPoint? = .bottom
25+
) {
2126
self.moduleName = moduleName
2227
self.initialProps = initialProps
28+
self.devMenuSceneAnchor = devMenuSceneAnchor
2329
}
2430

2531
public func makeUIViewController(context: Context) -> RCTReactViewController {
26-
RCTReactViewController(moduleName: moduleName, initProps: initialProps)
32+
let viewController = RCTReactViewController(moduleName: moduleName, initProps: initialProps)
33+
#if DEBUG
34+
if let devMenuSceneAnchor {
35+
let ornament = UIHostingOrnament(sceneAnchor: devMenuSceneAnchor) {
36+
DevMenuView()
37+
}
38+
// check if user has an ornament at this position, if so thr
39+
viewController.ornaments.append(ornament)
40+
}
41+
#endif
42+
return viewController
2743
}
2844

2945
public func updateUIViewController(_ uiViewController: RCTReactViewController, context: Context) {

packages/react-native/Libraries/SwiftExtensions/RCTWindow.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public struct RCTWindow : Scene {
1616
var contentView: AnyView?
1717

1818
func getRootView(sceneData: RCTSceneData?) -> RCTRootViewRepresentable {
19-
return RCTRootViewRepresentable(moduleName: moduleName, initialProps: sceneData?.props ?? [:])
19+
return RCTRootViewRepresentable(moduleName: moduleName, initialProps: sceneData?.props ?? [:], devMenuSceneAnchor: nil)
2020
}
2121

2222
public var body: some Scene {

0 commit comments

Comments
 (0)