@@ -129,7 +129,11 @@ - (id)initWithBackend:(id)backend pid:(int)processIdentifier
129129 [[MMWindowController alloc ] initWithVimController: self ];
130130 backendProxy = [backend retain ];
131131 popupMenuItems = [[NSMutableArray alloc ] init ];
132+ #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
132133 toolbarItemDict = [[NSMutableDictionary alloc ] init ];
134+ touchbarItemDict = [[NSMutableDictionary alloc ] init ];
135+ touchbarItemOrder = [[NSMutableArray alloc ] init ];
136+ #endif
133137 pid = processIdentifier;
134138 creationDate = [[NSDate alloc ] init ];
135139
@@ -178,6 +182,11 @@ - (void)dealloc
178182
179183 [toolbarItemDict release ]; toolbarItemDict = nil ;
180184 [toolbar release ]; toolbar = nil ;
185+ #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
186+ [touchbarItemDict release ]; touchbarItemDict = nil ;
187+ [touchbarItemOrder release ]; touchbarItemOrder = nil ;
188+ [touchbar release ]; touchbar = nil ;
189+ #endif
181190 [popupMenuItems release ]; popupMenuItems = nil ;
182191 [windowController release ]; windowController = nil ;
183192
@@ -488,7 +497,25 @@ - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)theToolbar
488497{
489498 return nil ;
490499}
500+ #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
501+ - (NSTouchBar *)makeTouchBar
502+ {
503+ touchbar = [[NSTouchBar alloc ] init ];
504+ touchbar.delegate = self;
505+ touchbar.defaultItemIdentifiers = [NSArray arrayWithArray: touchbarItemOrder];
506+ return touchbar;
507+ }
508+
509+ - (nullable NSTouchBarItem *)touchBar : (NSTouchBar *)touchBar makeItemForIdentifier : (NSTouchBarItemIdentifier )itemId
510+ {
511+ NSTouchBarItem *item = [touchbarItemDict objectForKey: itemId];
512+ if (!item) {
513+ ASLogWarn (@" No touchbar item with id '%@ '" , itemId);
514+ }
491515
516+ return item;
517+ }
518+ #endif
492519@end // MMVimController
493520
494521
@@ -1087,7 +1114,10 @@ - (void)addMenuWithDescriptor:(NSArray *)desc atIndex:(int)idx
10871114
10881115 return ;
10891116 }
1090-
1117+ #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
1118+ if ([rootName isEqual: @" TouchBar" ])
1119+ return ;
1120+ #endif
10911121 // This is either a main menu item or a popup menu item.
10921122 NSString *title = [desc lastObject ];
10931123 NSMenuItem *item = [[NSMenuItem alloc ] init ];
@@ -1138,7 +1168,13 @@ - (void)addMenuItemWithDescriptor:(NSArray *)desc
11381168 [self addToolbarItemWithLabel: title tip: tip icon: icon atIndex: idx];
11391169 return ;
11401170 }
1141-
1171+ #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
1172+ if ([rootName isEqual: @" TouchBar" ]) {
1173+ if (toolbar && [desc count ] == 2 )
1174+ [self addTouchbarItemWithLabel: title icon: icon atIndex: idx];
1175+ return ;
1176+ }
1177+ #endif
11421178 NSMenu *parent = [self parentMenuForDescriptor: desc];
11431179 if (!parent) {
11441180 ASLogWarn (@" Menu item '%@ ' has no parent" ,
@@ -1201,7 +1237,16 @@ - (void)removeMenuItemWithDescriptor:(NSArray *)desc
12011237 }
12021238 return ;
12031239 }
1204-
1240+ #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
1241+ if ([rootName isEqual: @" TouchBar" ]){
1242+ if ([desc count ] == 2 ) {
1243+ [touchbarItemOrder removeObject: title];
1244+ [touchbarItemDict removeObjectForKey: title];
1245+ [windowController setTouchBar: nil ];
1246+ }
1247+ return ;
1248+ }
1249+ #endif
12051250 NSMenuItem *item = [self menuItemForDescriptor: desc];
12061251 if (!item) {
12071252 ASLogWarn (@" Failed to remove menu item, descriptor not found: %@ " ,
@@ -1234,7 +1279,11 @@ - (void)enableMenuItemWithDescriptor:(NSArray *)desc state:(BOOL)on
12341279 NSString *title = [desc lastObject ];
12351280 [[toolbar itemWithItemIdentifier: title] setEnabled: on];
12361281 }
1237- } else {
1282+ } else
1283+ #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
1284+ if (![rootName isEqual: @" TouchBar" ])
1285+ #endif
1286+ {
12381287 // Use tag to set whether item is enabled or disabled instead of
12391288 // calling setEnabled:. This way the menus can autoenable themselves
12401289 // but at the same time Vim can set if a menu is enabled whenever it
@@ -1312,7 +1361,53 @@ - (void)addToolbarItemWithLabel:(NSString *)label
13121361
13131362 [toolbar insertItemWithItemIdentifier: label atIndex: idx];
13141363}
1315-
1364+ #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
1365+ - (void )addTouchbarItemWithLabel:(NSString *)label
1366+ icon:(NSString *)icon
1367+ atIndex:(int )idx
1368+ {
1369+ // Check for separator items.
1370+ if (!label) {
1371+ label = NSTouchBarItemIdentifierFixedSpaceLarge ;
1372+ } else if ([label length ] >= 2 && [label hasPrefix: @" -" ]
1373+ && [label hasSuffix: @" -" ]) {
1374+ // The label begins and ends with '-'; decided which kind of separator
1375+ // item it is by looking at the prefix.
1376+ if ([label hasPrefix: @" -space" ]) {
1377+ label = NSTouchBarItemIdentifierFixedSpaceSmall ;
1378+ } else if ([label hasPrefix: @" -flexspace" ]) {
1379+ label = NSTouchBarItemIdentifierFlexibleSpace ;
1380+ } else {
1381+ label = NSTouchBarItemIdentifierFixedSpaceLarge ;
1382+ }
1383+ } else {
1384+ NSButton * button = [NSButton buttonWithTitle: label target: windowController action: @selector (vimTouchbarItemAction: )];
1385+ NSCustomTouchBarItem *item =
1386+ [[NSCustomTouchBarItem alloc ] initWithIdentifier: label];
1387+ NSImage *img = [NSImage imageNamed: icon];
1388+
1389+ if (!img) {
1390+ img = [[[NSImage alloc ] initByReferencingFile: icon] autorelease ];
1391+ if (!(img && [img isValid ]))
1392+ img = nil ;
1393+ }
1394+ if (img) {
1395+ [button setImage: img];
1396+ // [button setImagePosition:NSImageLeft];
1397+ [button setImagePosition: NSImageOnly];
1398+ }
1399+
1400+ [item setView: button];
1401+ [touchbarItemDict setObject: item forKey: label];
1402+ }
1403+
1404+ int maxIdx = [touchbarItemOrder count ];
1405+ if (maxIdx < idx) idx = maxIdx;
1406+ [touchbarItemOrder insertObject: label atIndex: idx];
1407+
1408+ [windowController setTouchBar: nil ];
1409+ }
1410+ #endif
13161411- (void )popupMenuWithDescriptor:(NSArray *)desc
13171412 atRow:(NSNumber *)row
13181413 column:(NSNumber *)col
0 commit comments