Use signal for presenting menus
This commit is contained in:
parent
badc0c12f6
commit
7f53c267d2
@ -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<Bool>, @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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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")
|
||||
}
|
||||
|
||||
@ -35,8 +35,8 @@ extension AnyView {
|
||||
/// - isPresented: Whether the menu is currently visible.
|
||||
/// - menu: The menu.
|
||||
/// - Returns: The view.
|
||||
public func menu(isPresented: Binding<Bool>, @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.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user