Skip to content

Commit

Permalink
Pass Primarymenu view
Browse files Browse the repository at this point in the history
  • Loading branch information
lkzhao committed Jan 17, 2025
1 parent 38235a0 commit 503f23e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
12 changes: 7 additions & 5 deletions Sources/UIComponent/Components/View/PrimaryMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@ public class PrimaryMenu: UIControl {
public var isShowingMenu = false

/// The menu to be displayed when the control is interacted with.
public var menuBuilder: (() -> UIMenu)? {
public var menuBuilder: ((PrimaryMenu) -> UIMenu)? {
didSet {
guard isShowingMenu else { return }
if let menuBuilder {
contextMenuInteraction?.updateVisibleMenu({ _ in
return menuBuilder()
contextMenuInteraction?.updateVisibleMenu({ [weak self] _ in
guard let self else { return UIMenu() }
return menuBuilder(self)
})
} else {
contextMenuInteraction?.dismissMenu()
Expand Down Expand Up @@ -130,8 +131,9 @@ public class PrimaryMenu: UIControl {

public override func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
(config ?? .default).didTap?(self)
let menuConfiguration = UIContextMenuConfiguration(actionProvider: { [menuBuilder] suggested in
return menuBuilder?()
let menuConfiguration = UIContextMenuConfiguration(actionProvider: { [menuBuilder, weak self] suggested in
guard let self else { return UIMenu() }
return menuBuilder?(self)
})
if #available(iOS 16.0, *) {
menuConfiguration.preferredMenuElementOrder = self.preferredMenuElementOrder
Expand Down
15 changes: 11 additions & 4 deletions Sources/UIComponent/Components/View/PrimaryMenuComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,22 @@ public struct PrimaryMenuComponent: Component {
public let component: any Component

/// A builder that constructs the menu to be displayed as part of the primary menu component.
public let menuBuilder: () -> UIMenu
public let menuBuilder: (PrimaryMenu) -> UIMenu

public init(component: any Component, menuBuilder: @escaping () -> UIMenu) {
public init(component: any Component, menuBuilder: @escaping (PrimaryMenu) -> UIMenu) {
self.component = component
self.menuBuilder = menuBuilder
}

public init(component: any Component, menuBuilder: @escaping () -> UIMenu) {
self.component = component
self.menuBuilder = { _ in
menuBuilder()
}
}

public init(component: any Component, menu: UIMenu) {
self.init(component: component, menuBuilder: { menu })
self.init(component: component, menuBuilder: { _ in menu })
}

/// Lays out the component within the given constraints and returns a `PrimaryMenuRenderNode`.
Expand All @@ -51,7 +58,7 @@ public struct PrimaryMenuRenderNode: RenderNode {
public let content: any RenderNode

/// A builder that constructs the menu to be displayed as part of the primary menu component.
public let menuBuilder: (() -> UIMenu)?
public let menuBuilder: ((PrimaryMenu) -> UIMenu)?

/// The configuration for the tappable view.
public let config: PrimaryMenuConfig?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,13 @@ public extension Component {
func primaryMenu(_ menuBuilder: @escaping () -> UIMenu) -> PrimaryMenuComponent {
PrimaryMenuComponent(component: self, menuBuilder: menuBuilder)
}

/// Wrap the content in a component that displays the provided menu when tapped.
/// - Parameter menuBuilder: A closure that returns a `UIMenu` to be displayed when tapped.
/// - Returns: A `PrimaryMenuComponent` containing the component and displays the menu when tapped.
func primaryMenu(_ menuBuilder: @escaping (PrimaryMenu) -> UIMenu) -> PrimaryMenuComponent {
PrimaryMenuComponent(component: self, menuBuilder: menuBuilder)
}
}

// MARK: - Environment modifiers
Expand Down

0 comments on commit 503f23e

Please sign in to comment.