Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/RoyalIcing/BurntCocoaUI
Use Swift enums and structs with NSMenu, NSPopUpButton, NSSegmentedControl
https://github.com/RoyalIcing/BurntCocoaUI
mac macos nsmenu swift swift-enum
Last synced: 3 months ago
JSON representation
Use Swift enums and structs with NSMenu, NSPopUpButton, NSSegmentedControl
- Host: GitHub
- URL: https://github.com/RoyalIcing/BurntCocoaUI
- Owner: RoyalIcing
- License: mit
- Created: 2015-05-14T03:54:11.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2019-11-15T02:51:02.000Z (almost 5 years ago)
- Last Synced: 2024-07-29T01:35:38.281Z (3 months ago)
- Topics: mac, macos, nsmenu, swift, swift-enum
- Language: Swift
- Homepage:
- Size: 57.6 KB
- Stars: 31
- Watchers: 4
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-macOS - BurntCocoaUI - Use Swift enums and structs with NSMenu, NSPopUpButton, NSSegmentedControl. 🔶 (Uncategorized / Uncategorized)
README
# BurntCocoaUI
Declarative assistants to make NSMenu, NSPopUpButton, NSSegmentedControl easier. Use Swift enums instead of creating and removing NSMenuItems and fiddling with indexes and tags.## Example
### NSPopUpMenu with PopUpButtonAssistant
#### The choices you want to show in the menu, as an enum.
```swift
enum BaseContentTypeChoice: Int {
case LocalHTMLPages = 1
case Images
case Feeds
}
```#### Add an extension to the enum to make it conform to the UIChoiceRepresentative protocol.
```swift
extension BaseContentTypeChoice: UIChoiceRepresentative {
var title: String {
switch self {
case .LocalHTMLPages:
return "Local Pages"
case .Images:
return "Images"
case .Feeds:
return "Feeds"
}
}
typealias UniqueIdentifier = BaseContentTypeChoice
var uniqueIdentifier: UniqueIdentifier { return self }
}
```#### Use PopUpButtonAssistant in View Controller, with customized menu item titles.
```swift
class ExampleViewController: NSViewController {
@IBOutlet var baseContentTypeChoicePopUpButton: NSPopUpButton! // Hooked up in storyboard/nib
var baseContentTypeChoicePopUpButtonAssistant: PopUpButtonAssistant!var chosenBaseContentChoice: BaseContentTypeChoice = .LocalHTMLPages
var baseContentTypeChoiceMenuItemRepresentatives: [BaseContentTypeChoice?] {
return [
.LocalHTMLPages,
.Images,
.Feeds
]
}
func updateBaseContentTypeChoiceUI() {
let popUpButtonAssistant = baseContentTypeChoicePopUpButtonAssistant ?? {
let popUpButtonAssistant = PopUpButtonAssistant(popUpButton: baseContentTypeChoicePopUpButton)
let menuAssistant = popUpButtonAssistant.menuAssistant
// Customize the title by appending a total count
menuAssistant.customization.title = { choice in
let baseContentType = choice.baseContentType
let loadedURLCount = self.pageMapper?.numberOfLoadedURLsWithBaseContentType(baseContentType) ?? 0
return "\(choice.title) (\(loadedURLCount))"
}
self.baseContentTypeChoicePopUpButtonAssistant = popUpButtonAssistant
return popUpButtonAssistant
}()
popUpButtonAssistant.menuItemRepresentatives = baseContentTypeChoiceMenuItemRepresentatives
popUpButtonAssistant.update()
}
@IBAction func changeBaseContentTypeFilter(sender: NSPopUpButton) {
if let contentChoice = baseContentTypeChoicePopUpButtonAssistant.selectedItemRepresentative {
chosenBaseContentChoice = contentChoice
reloadData()
}
}
func reloadData() {
// Example ...
}
}
```## Installation
To integrate into your Xcode project using Carthage, specify it in your Cartfile:
```
github "BurntCaramel/BurntCocoaUI" >= 0.3
```## Origin
My Mac app Lantern: http://www.burntcaramel.com/lantern/