A function builder for NSMenu
s, similar in spirit to SwiftUI’s ViewBuilder
.
Usage example (see demo or read the documentation for more details):
let menu = NSMenu {
MenuItem("Click me")
.onSelect { print("clicked!") }
MenuItem("Item with a view")
.view {
MyMenuItemView() // any SwiftUI view
}
SeparatorItem()
MenuItem("About") {
// rendered as disabled items in a submenu
MenuItem("Version 1.2.3")
MenuItem("Copyright 2021")
}
MenuItem("Quit")
.shortcut("q")
.onSelect { NSApp.terminate(nil) }
}
// later, to replace the menu items with different/updated ones:
menu.replaceItems {
MenuItem("Replaced item").onSelect { print("Hello!") }
}
Note that there is no way to preserve the existing menu items, although it should be possible to implement that — feel free to open an issue or PR adding update support if you want it!
@resultBuilder
(Xcode 12.5+ is now required)MenuItem.set(WriteableKeyPath, to: Value)
method to make it easier to customize the menu itemMenuItem.apply { menuItem in }
method to allow arbitrary customization of the menu itemIndentGroup
to make it easier to indent several adjacent menu itemsCustomMenuItem
which allows you to include custom subclasses of NSMenuItem
in a MenuBuilder
Fixes & cleanup
Add loop support
Add conditional support
Add license, clean up code
Initial version!
Open the MenuBuilder.xcworkspace
to view the package and demo at the same time. PRs and issues are appreciated!
link |
Stars: 21 |
Last commit: 1 week ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics