Skip to content

Commit

Permalink
fix: allow to manually move dev menu to avoid conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
okwasniewski committed Jun 10, 2024
1 parent 6901261 commit 7bbc160
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions packages/react-native/Libraries/SwiftExtensions/RCTMainWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public struct RCTMainWindow: Scene {
var moduleName: String
var initialProps: RCTRootViewRepresentable.InitialPropsType
var onOpenURLCallback: ((URL) -> ())?
var devMenuPlacement: ToolbarPlacement = .bottomOrnament
var devMenuPlacement: OrnamentAttachmentAnchor? = .scene(.bottom)
var contentView: AnyView?

var rootView: RCTRootViewRepresentable {
Expand All @@ -38,7 +38,7 @@ public struct RCTMainWindow: Scene {
public init(
moduleName: String,
initialProps: RCTRootViewRepresentable.InitialPropsType = nil,
devMenuPlacement: ToolbarPlacement = .bottomOrnament
devMenuPlacement: OrnamentAttachmentAnchor? = .scene(.bottom)
) {
self.moduleName = moduleName
self.initialProps = initialProps
Expand All @@ -56,7 +56,7 @@ public struct RCTMainWindow: Scene {
public init<Content: View>(
moduleName: String,
initialProps: RCTRootViewRepresentable.InitialPropsType = nil,
devMenuPlacement: ToolbarPlacement = .bottomOrnament,
devMenuPlacement: OrnamentAttachmentAnchor? = .scene(.bottom),
@ViewBuilder contentView: @escaping (_ view: RCTRootViewRepresentable) -> Content
) {
self.moduleName = moduleName
Expand All @@ -73,8 +73,10 @@ public struct RCTMainWindow: Scene {
onOpenURLCallback?(url)
})
#if DEBUG
.toolbar {
DevMenuView(placement: .bottomOrnament)
.if(devMenuPlacement != nil) { content in
content.ornament(attachmentAnchor: devMenuPlacement!) {
DevMenuView()
}
}
#endif
}
Expand Down Expand Up @@ -142,18 +144,14 @@ public struct WindowHandlingModifier: ViewModifier {
/**
Toolbar which displays additional controls to easily open dev menu and trigger reload command.
*/
struct DevMenuView: ToolbarContent {
let placement: ToolbarItemPlacement

var body: some ToolbarContent {
ToolbarItem(placement: placement) {
struct DevMenuView: View {
var body: some View {
VStack {
Button(action: {
RCTTriggerReloadCommandListeners("User Reload")
}, label: {
Image(systemName: "arrow.clockwise")
})
}
ToolbarItem(placement: placement) {
Button(action: {
NotificationCenter.default.post(
Notification(name: Notification.Name("RCTShowDevMenuNotification"), object: nil)
Expand All @@ -162,6 +160,22 @@ struct DevMenuView: ToolbarContent {
label: {
Image(systemName: "filemenu.and.selection")
})

}
}
}

extension View {
/// Applies the given transform if the given condition evaluates to `true`.
/// - Parameters:
/// - condition: The condition to evaluate.
/// - transform: The transform to apply to the source `View`.
/// - Returns: Either the original `View` or the modified `View` if the condition is `true`.
@ViewBuilder func `if`<Content: View>(_ condition: @autoclosure () -> Bool, transform: (Self) -> Content) -> some View {
if condition() {
transform(self)
} else {
self
}
}
}

0 comments on commit 7bbc160

Please sign in to comment.