问题
I am making a macOS menu bar application which displays a fullscreen overlay. The user is able to control the opacity of this overlay via a slider in the menu bar. I wish to move these controls to the touch bar, because the application is used for screen recordings, and distractions on the main screen would be recorded. The slider in my application should act just like the brightness and volume sliders that are on the touch bar by default.
I have added my NSTouchBar
by subclassing NSApplication
and implementing makeTouchBar
. The bar appears when the application is first launched. However, the touch bar disappears when focussing any other application, and there seems to be no way to bring my application's touch bar again. The problem, I think, is that my application is a pure menu bar application with no windows.
Here are the ways I could make my application's touch bar visible, from most preferable to least preferable:
Add an icon for my application to the control strip, next to the brightness and volume icons. This would let the user hit my icon to adjust the opacity, even when other applications are focussed. However, strangely, I am told that there is no public API to manipulate the control strip.
Somehow give my user a way to manually add my application to the control strip. The control strip can be customized via
System Preferences > Keyboard > Customize Control Strip ...
, which shows a variety of things to drag-and-drop into the control strip. I would like to register my application as an item to include here. However, I cannot find any way to do this.Register my application as a music application. Spotify on my machine somehow uses the "music" icon in the Control Strip (the same icon that iTunes uses). I don't know how Spotify does this - does anyone else know? Obviously, this would be a hack, as my application is not playing audio, but it's better than nothing.
Somehow override the App Region so that my application's touch bar is always visible, clobbering all other applications' use of the touch bar. This is obviously not ideal, since it doesn't play well with other applications.
Are any of these solutions possible? Are there other possible solutions? How do I make my application's touch bar always accessible via the touch bar?
回答1:
Maybe there's some hacky solution, but according to official documentation you can't do it: https://developer.apple.com/reference/appkit/nstouchbar
On supported MacBook Pro models, the Touch Bar, above the keyboard, shows instances of the NSTouchBar class from the front-most app.
回答2:
If you could get your application into Control Strip area (where the volume-control and friends reside), then user can touch it there to bring up your touchbar. See hazeover's TouchSwitcher to get the idea.
来源:https://stackoverflow.com/questions/42878676/how-can-i-make-my-applications-touch-bar-always-visible-on-macos