Skip to content

Instantly share code, notes, and snippets.

@n3tr
Created November 5, 2016 12:19
Show Gist options
  • Save n3tr/005396df75b566d6b9d2b8ce36615708 to your computer and use it in GitHub Desktop.
Save n3tr/005396df75b566d6b9d2b8ce36615708 to your computer and use it in GitHub Desktop.
touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItemIdentifier) -> NSTouchBarItem?
// ViewController.swift
extension ViewController: NSTouchBarDelegate {
func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItemIdentifier) -> NSTouchBarItem? {
switch identifier {
// Increase Item
case NSTouchBarItemIdentifier.increase:
let button = NSButton(title: "+", target: self, action: #selector(ViewController.increaseCounter))
let increaseItem = NSCustomTouchBarItem(identifier: identifier)
increaseItem.customizationLabel = "Counter +"
increaseItem.view = button
return increaseItem
// Decrease Item
case NSTouchBarItemIdentifier.decrease:
let button = NSButton(title: "-", target: self, action: #selector(ViewController.decreaseCounter))
let decreaseItem = NSCustomTouchBarItem(identifier: identifier)
decreaseItem.customizationLabel = "Counter -"
decreaseItem.view = button
return decreaseItem
// Counter Label
case NSTouchBarItemIdentifier.counter:
let label = NSTextField(string: "0")
label.textColor = NSColor.green
label.alignment = .center
label.bind("stringValue", to: self, withKeyPath: #keyPath(currentCounter), options: nil)
let counter = NSCustomTouchBarItem(identifier: identifier)
counter.customizationLabel = "Counter"
counter.view = label
let labelLayout = NSLayoutConstraint.constraints(withVisualFormat: "H:[label(60)]",
options: [],
metrics: nil,
views: ["label": label])
NSLayoutConstraint.activate(labelLayout)
return counter
default:
return nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment