Swiftpack.co -  j-f1/MenuBuilder as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
j-f1/MenuBuilder
Swift Function Builder for creating NSMenuItems
.package(url: "https://github.com/j-f1/MenuBuilder.git", from: "v2.0.0")

MenuBuilder

A function builder for NSMenus, similar in spirit to SwiftUI’s ViewBuilder.

Usage example (see demo 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!

Changelog

v2.0.0

  • (BREAKING) Migrate to @resultBuilder (Xcode 12.5+ is now required)
  • Apply modifiers to shortcuts
  • Add a MenuItem.set(WriteableKeyPath, to: Value) method to make it easier to customize the menu item
  • Add a MenuItem.apply { menuItem in } method to allow arbitrary customization of the menu item
  • Add IndentGroup to make it easier to indent several adjacent menu items
  • Add CustomMenuItem which allows you to include custom subclasses of NSMenuItem in a MenuBuilder

v1.3.0

Fixes & cleanup

v1.2.0

Add loop support

v1.1.0

Add conditional support

v1.0.1

Add license, clean up code

v1.0.0

Initial version!

Contributing

Open the MenuBuilder.xcworkspace to view the package and demo at the same time. PRs and issues are appreciated!

GitHub

link
Stars: 12
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API