diff --git a/Sources/Core/View/MenuWrapper.swift b/Sources/Core/View/MenuWrapper.swift index ea98ede..591afee 100644 --- a/Sources/Core/View/MenuWrapper.swift +++ b/Sources/Core/View/MenuWrapper.swift @@ -11,7 +11,7 @@ import AppKit public struct MenuWrapper: MacWidget { /// Whether the menu is visible. - @Binding var isPresented: Bool + var present: Signal /// The content. var content: Body /// The menu. @@ -20,10 +20,10 @@ public struct MenuWrapper: MacWidget { /// Initialize a menu wrapper. /// - Parameters: /// - content: The view content. - /// - isPresented: Whether the menu is visible. + /// - present: The signal for presenting the menu. /// - menu: The menu. - public init(@ViewBuilder content: () -> Body, isPresented: Binding, @ViewBuilder menu: () -> Body) { - self._isPresented = isPresented + public init(@ViewBuilder content: () -> Body, present: Signal, @ViewBuilder menu: () -> Body) { + self.present = present self.content = content() self.menu = menu() } @@ -65,12 +65,11 @@ public struct MenuWrapper: MacWidget { MenuCollection { self.menu } .updateStorage(menu, data: data, updateProperties: updateProperties, type: MenuContext.self) } - if isPresented, + if present.update, let menu = storage.fields["menu"] as? NSMenu, let content = storage.content[.mainContent]?.first?.pointer as? NSView, let currentEvent = NSApp.currentEvent { NSMenu.popUpContextMenu(menu, with: currentEvent, for: content) - isPresented = false } } diff --git a/Sources/Demo/Demo.swift b/Sources/Demo/Demo.swift index 87b068a..371a89c 100644 --- a/Sources/Demo/Demo.swift +++ b/Sources/Demo/Demo.swift @@ -24,7 +24,7 @@ struct Demo: App { @State private var elements: [Element] = [.init()] @State private var selectedElement: String? @State private var alert = false - @State private var menuPresented = false + @State private var presentMenu: Signal = .init() @State private var selection = "Hello" var scene: Scene { @@ -49,9 +49,9 @@ struct Demo: App { selectedElement = element.id } Button(alert.description) { - menuPresented = true + presentMenu.signal() } - .menu(isPresented: $menuPresented) { + .menu(present: presentMenu) { MenuButton(alert.description) { print("Hi") } diff --git a/Sources/MacBackend/AnyView.swift b/Sources/MacBackend/AnyView.swift index 6d7df23..85e1dfd 100644 --- a/Sources/MacBackend/AnyView.swift +++ b/Sources/MacBackend/AnyView.swift @@ -35,8 +35,8 @@ extension AnyView { /// - isPresented: Whether the menu is currently visible. /// - menu: The menu. /// - Returns: The view. - public func menu(isPresented: Binding, @ViewBuilder menu: @escaping () -> Body) -> AnyView { - MenuWrapper(content: { self }, isPresented: isPresented, menu: menu) + public func menu(present: Signal, @ViewBuilder menu: @escaping () -> Body) -> AnyView { + MenuWrapper(content: { self }, present: present, menu: menu) } /// Add toolbar items to the end.