Skip to content

Commit 515ef9f

Browse files
committed
Make Touch Bar support robust against older macOS versions
Make sure to check MAC_OS_X_VERSION_10_12_2 so older Xcode / SDK versions will still be able to build MacVim, albeit without Touch Bar. Also make sure to use `NSClassFromString(@"NSTouchBar")` to check during runtime to avoid MacVim crashing for users using older versions of macOS when they by mistake bind a menu item with TouchBar as main menu (nothing will show up instead).
1 parent e1bc03b commit 515ef9f

File tree

3 files changed

+51
-37
lines changed

3 files changed

+51
-37
lines changed

src/MacVim/MMVimController.h

+9-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@
1515

1616

1717

18-
@interface MMVimController : NSObject<NSToolbarDelegate,
19-
NSOpenSavePanelDelegate, NSTouchBarDelegate>
18+
@interface MMVimController : NSObject<
19+
NSToolbarDelegate
20+
, NSOpenSavePanelDelegate
21+
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_12_2
22+
, NSTouchBarDelegate
23+
#endif
24+
>
2025
{
2126
unsigned identifier;
2227
BOOL isInitialized;
@@ -28,7 +33,7 @@
2833
// TODO: Move all toolbar code to window controller?
2934
NSToolbar *toolbar;
3035
NSMutableDictionary *toolbarItemDict;
31-
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
36+
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_12_2
3237
NSTouchBar *touchbar;
3338
NSMutableDictionary *touchbarItemDict;
3439
NSMutableArray *touchbarItemOrder;
@@ -71,7 +76,7 @@
7176
- (id)evaluateVimExpressionCocoa:(NSString *)expr
7277
errorString:(NSString **)errstr;
7378
- (void)processInputQueue:(NSArray *)queue;
74-
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
79+
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_12_2
7580
- (NSTouchBar *)makeTouchBar;
7681
#endif
7782
@end

src/MacVim/MMVimController.m

+38-29
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,13 @@ - (id)initWithBackend:(id)backend pid:(int)processIdentifier
132132
[[MMWindowController alloc] initWithVimController:self];
133133
backendProxy = [backend retain];
134134
popupMenuItems = [[NSMutableArray alloc] init];
135-
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
136135
toolbarItemDict = [[NSMutableDictionary alloc] init];
137-
touchbarItemDict = [[NSMutableDictionary alloc] init];
138-
touchbarItemOrder = [[NSMutableArray alloc] init];
139-
touchbarDisabledItems = [[NSMutableSet alloc] init];
136+
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_12_2
137+
if (NSClassFromString(@"NSTouchBar")) {
138+
touchbarItemDict = [[NSMutableDictionary alloc] init];
139+
touchbarItemOrder = [[NSMutableArray alloc] init];
140+
touchbarDisabledItems = [[NSMutableSet alloc] init];
141+
}
140142
#endif
141143
pid = processIdentifier;
142144
creationDate = [[NSDate alloc] init];
@@ -186,7 +188,7 @@ - (void)dealloc
186188

187189
[toolbarItemDict release]; toolbarItemDict = nil;
188190
[toolbar release]; toolbar = nil;
189-
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
191+
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_12_2
190192
[touchbarItemDict release]; touchbarItemDict = nil;
191193
[touchbarItemOrder release]; touchbarItemOrder = nil;
192194
[touchbarDisabledItems release]; touchbarDisabledItems = nil;
@@ -502,7 +504,7 @@ - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)theToolbar
502504
{
503505
return nil;
504506
}
505-
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
507+
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_12_2
506508
- (NSTouchBar *)makeTouchBar
507509
{
508510
touchbar = [[NSTouchBar alloc] init];
@@ -1140,10 +1142,10 @@ - (void)addMenuWithDescriptor:(NSArray *)desc atIndex:(int)idx
11401142

11411143
return;
11421144
}
1143-
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
1145+
11441146
if ([rootName isEqual:MMTouchbarMenuName])
11451147
return;
1146-
#endif
1148+
11471149
// This is either a main menu item or a popup menu item.
11481150
NSString *title = [desc lastObject];
11491151
NSMenuItem *item = [[NSMenuItem alloc] init];
@@ -1194,13 +1196,15 @@ - (void)addMenuItemWithDescriptor:(NSArray *)desc
11941196
[self addToolbarItemWithLabel:title tip:tip icon:icon atIndex:idx];
11951197
return;
11961198
}
1197-
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
11981199
if ([rootName isEqual:MMTouchbarMenuName]) {
1199-
if ([desc count] == 2)
1200-
[self addTouchbarItemWithLabel:title icon:icon atIndex:idx];
1200+
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_12_2
1201+
if (NSClassFromString(@"NSTouchBar")) {
1202+
if ([desc count] == 2)
1203+
[self addTouchbarItemWithLabel:title icon:icon atIndex:idx];
1204+
}
1205+
#endif
12011206
return;
12021207
}
1203-
#endif
12041208
NSMenu *parent = [self parentMenuForDescriptor:desc];
12051209
if (!parent) {
12061210
ASLogWarn(@"Menu item '%@' has no parent",
@@ -1263,17 +1267,19 @@ - (void)removeMenuItemWithDescriptor:(NSArray *)desc
12631267
}
12641268
return;
12651269
}
1266-
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
12671270
if ([rootName isEqual:MMTouchbarMenuName]){
1268-
if ([desc count] == 2) {
1269-
[touchbarItemOrder removeObject:title];
1270-
[touchbarItemDict removeObjectForKey:title];
1271-
[touchbarDisabledItems removeObject:title];
1272-
[windowController setTouchBar:nil];
1271+
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_12_2
1272+
if (NSClassFromString(@"NSTouchBar")) {
1273+
if ([desc count] == 2) {
1274+
[touchbarItemOrder removeObject:title];
1275+
[touchbarItemDict removeObjectForKey:title];
1276+
[touchbarDisabledItems removeObject:title];
1277+
[windowController setTouchBar:nil];
1278+
}
12731279
}
1280+
#endif
12741281
return;
12751282
}
1276-
#endif
12771283
NSMenuItem *item = [self menuItemForDescriptor:desc];
12781284
if (!item) {
12791285
ASLogWarn(@"Failed to remove menu item, descriptor not found: %@",
@@ -1308,19 +1314,22 @@ - (void)enableMenuItemWithDescriptor:(NSArray *)desc state:(BOOL)on
13081314
}
13091315
return;
13101316
}
1311-
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
1317+
13121318
if ([rootName isEqual:MMTouchbarMenuName]) {
1313-
if ([desc count] == 2) {
1314-
NSString *title = [desc lastObject];
1315-
if (on)
1316-
[touchbarDisabledItems removeObject:title];
1317-
else
1318-
[touchbarDisabledItems addObject:title];
1319-
[windowController setTouchBar:nil];
1319+
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_12_2
1320+
if (NSClassFromString(@"NSTouchBar")) {
1321+
if ([desc count] == 2) {
1322+
NSString *title = [desc lastObject];
1323+
if (on)
1324+
[touchbarDisabledItems removeObject:title];
1325+
else
1326+
[touchbarDisabledItems addObject:title];
1327+
[windowController setTouchBar:nil];
1328+
}
13201329
}
1330+
#endif
13211331
return;
13221332
}
1323-
#endif
13241333

13251334
// Use tag to set whether item is enabled or disabled instead of
13261335
// calling setEnabled:. This way the menus can autoenable themselves
@@ -1398,7 +1407,7 @@ - (void)addToolbarItemWithLabel:(NSString *)label
13981407

13991408
[toolbar insertItemWithItemIdentifier:label atIndex:idx];
14001409
}
1401-
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
1410+
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_12_2
14021411
- (void)addTouchbarItemWithLabel:(NSString *)label
14031412
icon:(NSString *)icon
14041413
atIndex:(int)idx

src/MacVim/MMWindowController.m

+4-4
Original file line numberDiff line numberDiff line change
@@ -909,16 +909,16 @@ - (IBAction)vimToolbarItemAction:(id)sender
909909
[vimController sendMessage:ExecuteMenuMsgID data:[attrs dictionaryAsData]];
910910
}
911911

912-
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
912+
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_12_2
913913
- (IBAction)vimTouchbarItemAction:(id)sender
914914
{
915915
NSArray *desc = [NSArray arrayWithObjects:@"TouchBar", [sender title], nil];
916916
NSDictionary *attrs = [NSDictionary dictionaryWithObject:desc
917917
forKey:@"descriptor"];
918918
[vimController sendMessage:ExecuteMenuMsgID data:[attrs dictionaryAsData]];
919919
}
920-
921920
#endif
921+
922922
- (IBAction)fontSizeUp:(id)sender
923923
{
924924
[[NSFontManager sharedFontManager] modifyFont:
@@ -1358,13 +1358,13 @@ - (void)runAfterWindowPresentedUsingBlock:(void (^)(void))block
13581358
[afterWindowPresentedQueue addObject:[block copy]];
13591359
}
13601360

1361-
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
1361+
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_12_2
13621362
- (NSTouchBar *)makeTouchBar
13631363
{
13641364
return [vimController makeTouchBar];
13651365
}
1366-
13671366
#endif
1367+
13681368
@end // MMWindowController
13691369

13701370

0 commit comments

Comments
 (0)