Skip to content

Instantly share code, notes, and snippets.

@krzyzanowskim
Created July 16, 2020 16:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krzyzanowskim/3a0031754b2cf705297697bd87abbdac to your computer and use it in GitHub Desktop.
Save krzyzanowskim/3a0031754b2cf705297697bd87abbdac to your computer and use it in GitHub Desktop.
private struct PopUpButton: NSViewRepresentable {
@Binding var selection: String
var content: [String]
func makeNSView(context: Context) -> NSPopUpButton {
let button = NSPopUpButton()
button.imagePosition = .imageLeading
button.usesSingleLineMode = true
button.autoenablesItems = false
let menu = NSMenu()
menu.items = content.map { name -> NSMenuItem in
let item = NSMenuItem(title: name, action: nil, keyEquivalent: "")
item.image = NSImage(systemSymbolName: name, accessibilityDescription: nil)
item.identifier = NSUserInterfaceItemIdentifier(rawValue: name)
item.toolTip = name
return item
}
button.menu = menu
button.select(menu.items.first(where: { $0.title == selection }))
return button
}
func updateNSView(_ button: NSPopUpButton, context: Context) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment