dagronf/DSFMenuBuilder
A SwiftUI-style DSL for generating `NSMenu` instances for AppKit.
Why?
I'd done this for DSFAppKitBuilder and thought I'd pull it out into its own micro-framework.
tl;dr Show me something!
Creates a menu with 'cut, copy, paste, separator, clear selection' with enablers and actions
let menu = NSMenu {
MenuItem("Cut")
.enabled { [weak self] in self?.hasSelection ?? false }
.onAction { [weak self] in /* perform cut action */ }
MenuItem("Copy")
.enabled { [weak self] in self?.hasSelection ?? false }
.onAction { [weak self] in /* perform copy action */ }
MenuItem("Paste")
.enabled { [weak self] in self?.clipboardHasText ?? false }
.onAction { [weak self] in /* perform paste action */ }
Separator()
MenuItem("Clear selection")
.onAction { [weak self] in /* clear the current selection */ }
}
menu.popUp(
positioning: nil,
at: .init(x: sender.bounds.minX, y: sender.bounds.maxY),
in: sender
)<details> <summary>Bigger Example</summary>
// A fictional NSViewController that displays an interactive position matrix
let positionMatrixViewController = PositionMatrixViewController()
// A menu to be displayed as a submenu of the main menu
private lazy var presets = Menu {
MenuItem("Github")
.onAction { [weak self] in
// Change the style to github
}
}
MenuItem("BitBucket")
.onAction { [weak self] in
// Change the style to bitbucket
}
}
}
let menu = Menu {
MenuItem("Convert tabs to spaces")
.onAction { [weak self] in /* perform tabs to spaces */ }
MenuItem("Convert spaces to tabs")
.onAction { [weak self] in /* perform spaces to tabs */ }
Separator()
ViewItem("Position Matrix", positionMatrixViewController)
Separator()
MenuItem("Preset Styles", subMenu: presets)
}</details>
Package Metadata
Repository: dagronf/DSFMenuBuilder
Stars: 16
Forks: 0
Open issues: 0
Default branch: main
Primary language: swift
License: MIT
Topics: dsl, nsmenu, swift
README: README.md