Fix menu not working properly
All checks were successful
Deploy Docs / publish (push) Successful in 3m33s
SwiftLint / SwiftLint (push) Successful in 3s

This commit is contained in:
david-swift 2024-12-15 22:20:59 +01:00
parent 8391997848
commit 3c2cc8cb48
2 changed files with 7 additions and 4 deletions

View File

@ -11,7 +11,7 @@ import AppKit
public struct MenuWrapper: MacWidget { public struct MenuWrapper: MacWidget {
/// Whether the menu is visible. /// Whether the menu is visible.
var present: Signal @Binding var present: Signal
/// The content. /// The content.
var content: Body var content: Body
/// The menu. /// The menu.
@ -22,8 +22,8 @@ public struct MenuWrapper: MacWidget {
/// - content: The view content. /// - content: The view content.
/// - present: The signal for presenting the menu. /// - present: The signal for presenting the menu.
/// - menu: The menu. /// - menu: The menu.
public init(@ViewBuilder content: () -> Body, present: Signal, @ViewBuilder menu: () -> Body) { public init(@ViewBuilder content: () -> Body, present: Binding<Signal>, @ViewBuilder menu: () -> Body) {
self.present = present self._present = present
self.content = content() self.content = content()
self.menu = menu() self.menu = menu()
} }
@ -69,6 +69,9 @@ public struct MenuWrapper: MacWidget {
let menu = storage.fields["menu"] as? NSMenu, let menu = storage.fields["menu"] as? NSMenu,
let content = storage.content[.mainContent]?.first?.pointer as? NSView, let content = storage.content[.mainContent]?.first?.pointer as? NSView,
let currentEvent = NSApp.currentEvent { let currentEvent = NSApp.currentEvent {
StateManager.blockUpdates = true
present.signal()
StateManager.blockUpdates = false
NSMenu.popUpContextMenu(menu, with: currentEvent, for: content) NSMenu.popUpContextMenu(menu, with: currentEvent, for: content)
} }
} }

View File

@ -35,7 +35,7 @@ extension AnyView {
/// - isPresented: Whether the menu is currently visible. /// - isPresented: Whether the menu is currently visible.
/// - menu: The menu. /// - menu: The menu.
/// - Returns: The view. /// - Returns: The view.
public func menu(present: Signal, @ViewBuilder menu: @escaping () -> Body) -> AnyView { public func menu(present: Binding<Signal>, @ViewBuilder menu: @escaping () -> Body) -> AnyView {
MenuWrapper(content: { self }, present: present, menu: menu) MenuWrapper(content: { self }, present: present, menu: menu)
} }