Adding Button to NSTouchBar Control Strip

3k Views Asked by At

I'm working on a pet project which displays current cryptocurrency prices on the touch bar. I'm a web developer, but not a Swift developer, so it's slow going.

https://github.com/sharkattackhq/crypt

Right now, the basic functionality is in place, but of course the data is only displayed when the app window is active. I'd like to be able to view the data no matter which application is active, and the only way I can see of doing this would be to add a button to the Control Strip section of the Touch Bar.

How can I add a button to the control strip? There's no public API for this, but I can see that other, no-Apple Apps do it.

1

There are 1 best solutions below

1
On

Only via private API, you need to connect /System/Library/PrivateFrameworks/DFRFoundation.framework

Bridge:

// TouchBarPrivateApi-Bridging.h
#import "TouchBarPrivateApi.h"`

Header:

// TouchBarPrivateApi.
#import <AppKit/AppKit.h>`
extern void DFRElementSetControlStripPresenceForIdentifier(NSTouchBarItemIdentifier, BOOL);
    extern void DFRSystemModalShowsCloseBoxWhenFrontMost(BOOL);

    @interface NSTouchBarItem (PrivateMethods)
    + (void)addSystemTrayItem:(NSTouchBarItem *)item;
    + (void)removeSystemTrayItem:(NSTouchBarItem *)item;
    @end


    @interface NSTouchBar (PrivateMethods)
    + (void)presentSystemModalFunctionBar:(NSTouchBar *)touchBar placement:(long long)placement systemTrayItemIdentifier:(NSTouchBarItemIdentifier)identifier;
    + (void)presentSystemModalFunctionBar:(NSTouchBar *)touchBar systemTrayItemIdentifier:(NSTouchBarItemIdentifier)identifier;
    + (void)dismissSystemModalFunctionBar:(NSTouchBar *)touchBar;
    + (void)minimizeSystemModalFunctionBar:(NSTouchBar *)touchBar;
    @end

And use it:

// AppDelegate applicationDidFinishLaunching
func applicationDidFinishLaunching(_ aNotification: Notification) {
    TouchBarController.shared.setupControlStripPresence()
}

// TouchBarController class TouchBarController: NSObject, NSTouchBarDelegate {

    static let shared = TouchBarController()

    let touchBar = NSTouchBar() func setupControlStripPresence() {
        DFRSystemModalShowsCloseBoxWhenFrontMost(false)
        let item = NSCustomTouchBarItem(identifier: .controlStripItem)
        item.view = NSButton(image: #imageLiteral(resourceName: "Strip"), target: self, action: #selector(presentTouchBar))
        NSTouchBarItem.addSystemTrayItem(item)
        DFRElementSetControlStripPresenceForIdentifier(.controlStripItem, true)
    }

UPDATE 10.14

+ (void)presentSystemModalTouchBar:(NSTouchBar *)touchBar placement:(long long)placement systemTrayItemIdentifier:(NSTouchBarItemIdentifier)identifier;
+ (void)presentSystemModalTouchBar:(NSTouchBar *)touchBar systemTrayItemIdentifier:(NSTouchBarItemIdentifier)identifier;
+ (void)dismissSystemModalTouchBar:(NSTouchBar *)touchBar;
+ (void)minimizeSystemModalTouchBar:(NSTouchBar *)touchBar;

And real project as example - https://github.com/Toxblh/MTMR