PoweredTouchBar
PoweredTouchBar is a library which lets you easily and more powerfully use Touch Bar on macOS with SwiftUI.
How to use it?
There is a simple example of how to use PoweredTouchBar
library:
import SwiftUI
struct ContentView: View {
@State var text: String = ""
var body: some View {
TextEditor(text: $text)
.poweredTouchBar {
PoweredTouchBarButton(
identifier: "HelloWorld.text",
title: "Hello World!",
action: {
print("You've pressed the button with text")
}
)
PoweredTouchBarButton(
identifier: "HelloWorld.image",
image: NSImage(systemSymbolName: "star.fill", accessibilityDescription: nil)!,
action: {
print("You've pressed the button with image")
}
)
}
}
}
PoweredTouchBarItem
?
How to create your own PoweredTouchBarItem
is a protocol which basically allows you to create items for Touch Bar.
You can use it to wrap any NSTouchBarItem
to do so you just need to make a method to get
an identifier (getIdentifier
) and to get item itself (touchBarItem
). There is an example below
which shows how you can create a simple wrapper for NSTouchBarItem
to use them with
PoweredTouchBar
(It's already available by importing PoweredTouchBar
):
import AppKit
public class PoweredNSTouchBarItem: PoweredTouchBarItem {
var item: NSTouchBarItem
public init(_ item: () -> NSTouchBarItem) {
self.item = item()
}
public func touchBarItem() -> NSTouchBarItem {
return item
}
public func getIdentifier() -> NSTouchBarItem.Identifier {
return item.identifier
}
}
You can also check out how NSButtonTouchBarItem
is wrapped in PoweredTouchBarButton
to make it work in PoweredTouchBar
.