Use signal for presenting menus
This commit is contained in:
parent
badc0c12f6
commit
7f53c267d2
@ -11,7 +11,7 @@ import AppKit
|
|||||||
public struct MenuWrapper: MacWidget {
|
public struct MenuWrapper: MacWidget {
|
||||||
|
|
||||||
/// Whether the menu is visible.
|
/// Whether the menu is visible.
|
||||||
@Binding var isPresented: Bool
|
var present: Signal
|
||||||
/// The content.
|
/// The content.
|
||||||
var content: Body
|
var content: Body
|
||||||
/// The menu.
|
/// The menu.
|
||||||
@ -20,10 +20,10 @@ public struct MenuWrapper: MacWidget {
|
|||||||
/// Initialize a menu wrapper.
|
/// Initialize a menu wrapper.
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - content: The view content.
|
/// - content: The view content.
|
||||||
/// - isPresented: Whether the menu is visible.
|
/// - present: The signal for presenting the menu.
|
||||||
/// - menu: The menu.
|
/// - menu: The menu.
|
||||||
public init(@ViewBuilder content: () -> Body, isPresented: Binding<Bool>, @ViewBuilder menu: () -> Body) {
|
public init(@ViewBuilder content: () -> Body, present: Signal, @ViewBuilder menu: () -> Body) {
|
||||||
self._isPresented = isPresented
|
self.present = present
|
||||||
self.content = content()
|
self.content = content()
|
||||||
self.menu = menu()
|
self.menu = menu()
|
||||||
}
|
}
|
||||||
@ -65,12 +65,11 @@ public struct MenuWrapper: MacWidget {
|
|||||||
MenuCollection { self.menu }
|
MenuCollection { self.menu }
|
||||||
.updateStorage(menu, data: data, updateProperties: updateProperties, type: MenuContext.self)
|
.updateStorage(menu, data: data, updateProperties: updateProperties, type: MenuContext.self)
|
||||||
}
|
}
|
||||||
if isPresented,
|
if present.update,
|
||||||
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 {
|
||||||
NSMenu.popUpContextMenu(menu, with: currentEvent, for: content)
|
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 elements: [Element] = [.init()]
|
||||||
@State private var selectedElement: String?
|
@State private var selectedElement: String?
|
||||||
@State private var alert = false
|
@State private var alert = false
|
||||||
@State private var menuPresented = false
|
@State private var presentMenu: Signal = .init()
|
||||||
@State private var selection = "Hello"
|
@State private var selection = "Hello"
|
||||||
|
|
||||||
var scene: Scene {
|
var scene: Scene {
|
||||||
@ -49,9 +49,9 @@ struct Demo: App {
|
|||||||
selectedElement = element.id
|
selectedElement = element.id
|
||||||
}
|
}
|
||||||
Button(alert.description) {
|
Button(alert.description) {
|
||||||
menuPresented = true
|
presentMenu.signal()
|
||||||
}
|
}
|
||||||
.menu(isPresented: $menuPresented) {
|
.menu(present: presentMenu) {
|
||||||
MenuButton(alert.description) {
|
MenuButton(alert.description) {
|
||||||
print("Hi")
|
print("Hi")
|
||||||
}
|
}
|
||||||
|
|||||||
@ -35,8 +35,8 @@ 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(isPresented: Binding<Bool>, @ViewBuilder menu: @escaping () -> Body) -> AnyView {
|
public func menu(present: Signal, @ViewBuilder menu: @escaping () -> Body) -> AnyView {
|
||||||
MenuWrapper(content: { self }, isPresented: isPresented, menu: menu)
|
MenuWrapper(content: { self }, present: present, menu: menu)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add toolbar items to the end.
|
/// Add toolbar items to the end.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user