2016-12-29 17 views
2

我試圖增加對通過插件暴露NSTouchBar按鈕的支持,但我無法修改其他應用程序。該插件是一個共享庫,在創建主窗口後在運行時加載。我創建了一個AppDelegate如下:在創建主窗口後添加NSTouchBar支持

@interface AppDelegate : NSResponder <NSTouchBarDelegate> 
@end 

使用實現makeTouchBartouchBar功能的@implmentation

- (NSTouchBar *) makeTouchBar 
- (nullable NSTouchBarItem *) touchBar:(NSTouchBar *)touchBar 
         makeItemForIdentifier: (NSTouchBarItemIdentifier)identifier 

我終於嘗試將其注入到應用程序中,我有下面的onload函數在加載到應用程序中時在動態庫中調用。

NSWindow *w = [NSApp mainWindow]; 

AppDelegate *a = [[AppDelegate alloc] init]; 
a.nextResponder = w.nextResponder; 
w.nextResponder = a; 

// Re-setting FirstResponder will trigger AppDelegate::makeTouchBar 
// that was attached above. 
NSResponder *n = w.firstResponder; 
[w makeFirstResponder: nil]; 
[w makeFirstResponder: n]; 

但是... AppDelegate::touchBar將永遠不會被調用,因此觸摸欄決不會與我的測試按鈕來填充。

如果我在Xcode中創建一個新的項目,並使用完全相同的AppDelegate實現,我得到的功能按鈕,它們都顯示,並響應按事件(同一@interface@implementation的複製粘貼),所以它的注射似乎被破壞的部分。 (在Xcode的一切都是通過MainMenu.xib我想勾搭上了)

更新: 的主要問題是,申請的方式,防止觸摸欄工作編譯。也許建立在較舊版本的Mac OS上。

回答

1

如果這個對象真的應該作爲應用程序委託(顧名思義) - 而不是將它插入到主窗口的響應鏈中,最好將其實際設置爲NSApp的delegate。設置代理的touchBar也用於構建觸摸欄(請參閱NSTouchBar Discovery and the Responder Chain)。

如果這不適合您的插件(也許應用程序已經有它需要的特定應用程序委託),您可以將它插入NSApp的響應者鏈而不是mainWindow的。

這兩種方法都具有使觸摸欄在應用程序級別而不是窗口關聯的好處。所以如果主/窗口改變,將始終使用提供的觸摸條。

+0

設置NSApp.delegate時,沒有什麼破壞,應用程序就像以前一樣工作。儘管我的AppDelegate的touchBar仍然沒有被調用。我嘗試設置NSApp.delegate = myappdelegate,myappdelegate.touchBar = nil,NSApp.touchBar = nil,myappdelegate.nextResponder = NSApp.nextResponder,NSApp.nextResponder = myappdelegate。有問題的應用程序是Emacs,並且我正在嘗試通過在評估(模塊加載「/ path/to/module」)時加載的本機elisp模塊添加觸摸條。 – dsvensson

+0

myappdelegate.touchBar仍然由makeTouchBar創建。我試着將myappdelegate.touchBar分配給NSApp.touchBar,但仍然沒有觸摸條。 – dsvensson

+0

難道這是我試圖注入觸摸板的應用程序,在這種情況下,Emacs編譯了一些選項,禁用觸摸板支持?我使用最新的https://emacsformacosx.com/ – dsvensson