https://github.com/igormuzyka/menubar
A declarative wrapper for your macOS app to easily define MenuBar
https://github.com/igormuzyka/menubar
dsl macos menubar swift
Last synced: about 1 year ago
JSON representation
A declarative wrapper for your macOS app to easily define MenuBar
- Host: GitHub
- URL: https://github.com/igormuzyka/menubar
- Owner: IgorMuzyka
- License: mit
- Created: 2018-10-16T13:57:30.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-12-14T21:42:17.000Z (over 5 years ago)
- Last Synced: 2025-03-22T02:01:31.537Z (over 1 year ago)
- Topics: dsl, macos, menubar, swift
- Language: Swift
- Homepage:
- Size: 46.9 KB
- Stars: 15
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Menu Bar
A declarative wrapper to easily define your **MenuBar**.
## Installation
To install it, simply add the following line to your Podfile:
```ruby
pod 'MenuBar', :git => 'https://github.com/IgorMuzyka/MenuBar.git'
```
## Usage
In order to construct your **MenuBar** you'll need to use **Descriptor** and **Parameter** values. **Descriptors** define ***items*** of the ***menu*** while **Parameters** allow to _configure_ the ***item***.
```swift
public enum Descriptor {
case menu([Parameter], [Descriptor])
case item([Parameter])
case separator
}
```
```swift
public enum Parameter {
case action(() -> Void)
case title(String)
case enabled(Bool)
case state(NSControl.StateValue)
}
```
To construct a **MenuBar** simply create an instance like this.
```swift
let menuBar = MenuBar(descriptors: [
.item([.title("This is a title of an item of a menu bar")]),
.item([.enabled(false), .title("You can set the enabled status of the item")]),
.item([.state(.on), .title("Also you can mark a item with a on or off state")]),
.item([.action({ print("click") }), .title("This is how you set action to be executed when item is clicked")]),
.separator, // and this is just a separator that will be visible between the items
.menu([.title("And this is a menu or a submenu in this case")], [
.item([.title("Items in which can be configured in the same exact way")])
])
])
```
In order to interact with a **MenuBar** you'll need to also give it an **image** or a **title** so that it can appear in the **MenuBar**.
```swift
menuBar.title = "Your app name"
```
or
```swift
menuBar.image = NSImage(named: "You app MenuBar icon")
```

> This is how the **MenuBar** we just created looks like.
Also **keep in mind** that you should **hold** the instance of **MenuBar** in order for it to **exist** and work properly. (App Delegate is an okay place to hold it).
Whenever you want to update the **MenuBar** just overwrite it's **descriptors** property.
```swift
menuBar.descriptors = [
.item([.title("Now this is the only item in menu bar")])
]
```
## Author
Igor Muzyka, igormuzyka42@gmail.com
## License
MenuBar is available under the MIT license. See the LICENSE file for more info.