Skip to content

Commit 8fc7276

Browse files
wiomocychin
authored andcommitted
basic touchbarsupport
1 parent 1868c4c commit 8fc7276

7 files changed

Lines changed: 155 additions & 7 deletions

File tree

runtime/menu.vim

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,4 +1287,21 @@ if has("gui_macvim")
12871287
macm Help.MacVim\ Website action=openWebsite:
12881288
endif
12891289

1290+
if has("touchbar")
1291+
an TouchBar.Open :browse confirm e<CR>
1292+
an <silent> TouchBar.Save :if expand("%") == ""<Bar>browse confirm w<Bar>else<Bar>confirm w<Bar>endif<CR>
1293+
an TouchBar.SaveAll :browse confirm wa<CR>
1294+
1295+
an TouchBar.-sep1- <Nop>
1296+
an TouchBar.Undo u
1297+
an TouchBar.Redo <C-R>
1298+
1299+
an TouchBar.-sep2- <Nop>
1300+
vnoremenu TouchBar.Cut "+x
1301+
vnoremenu TouchBar.Copy "+y
1302+
cnoremenu TouchBar.Copy <C-Y>
1303+
nnoremenu TouchBar.Paste "+gP
1304+
cnoremenu TouchBar.Paste <C-R>+
1305+
endif
1306+
12901307
" vim: set sw=2 :

src/MacVim/MMVimController.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717

1818
@interface MMVimController : NSObject<NSToolbarDelegate,
19-
NSOpenSavePanelDelegate>
19+
NSOpenSavePanelDelegate, NSTouchBarDelegate>
2020
{
2121
unsigned identifier;
2222
BOOL isInitialized;
@@ -28,6 +28,11 @@
2828
// TODO: Move all toolbar code to window controller?
2929
NSToolbar *toolbar;
3030
NSMutableDictionary *toolbarItemDict;
31+
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
32+
NSTouchBar *touchbar;
33+
NSMutableDictionary *touchbarItemDict;
34+
NSMutableArray *touchbarItemOrder;
35+
#endif
3136

3237
int pid;
3338
NSString *serverName;
@@ -65,4 +70,7 @@
6570
- (id)evaluateVimExpressionCocoa:(NSString *)expr
6671
errorString:(NSString **)errstr;
6772
- (void)processInputQueue:(NSArray *)queue;
73+
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
74+
- (NSTouchBar *)makeTouchBar;
75+
#endif
6876
@end

src/MacVim/MMVimController.m

Lines changed: 100 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/MacVim/MMWindowController.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +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
913+
- (IBAction)vimTouchbarItemAction:(id)sender
914+
{
915+
NSArray *desc = [NSArray arrayWithObjects:@"TouchBar", [sender title], nil];
916+
NSDictionary *attrs = [NSDictionary dictionaryWithObject:desc
917+
forKey:@"descriptor"];
918+
[vimController sendMessage:ExecuteMenuMsgID data:[attrs dictionaryAsData]];
919+
}
920+
921+
#endif
912922
- (IBAction)fontSizeUp:(id)sender
913923
{
914924
[[NSFontManager sharedFontManager] modifyFont:
@@ -1348,6 +1358,13 @@ - (void)runAfterWindowPresentedUsingBlock:(void (^)(void))block
13481358
[afterWindowPresentedQueue addObject:[block copy]];
13491359
}
13501360

1361+
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
1362+
- (NSTouchBar *)makeTouchBar
1363+
{
1364+
return [vimController makeTouchBar];
1365+
}
1366+
1367+
#endif
13511368
@end // MMWindowController
13521369

13531370

src/evalfunc.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6442,6 +6442,9 @@ f_has(typval_T *argvars, typval_T *rettv)
64426442
#ifdef FEAT_TOOLBAR
64436443
"toolbar",
64446444
#endif
6445+
#ifdef FEAT_TOUCHBAR
6446+
"touchbar",
6447+
#endif
64456448
#ifdef FEAT_TRANSPARENCY
64466449
"transparency",
64476450
#endif

src/feature.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,10 @@
731731
#if defined(FEAT_TOOLBAR) && !defined(FEAT_MENU)
732732
# define FEAT_MENU
733733
#endif
734+
735+
#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MACVIM)
736+
# define FEAT_TOUCHBAR
737+
#endif
734738

735739
/*
736740
* GUI tabline

src/menu.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1835,7 +1835,11 @@ menu_is_child_of_popup(vimmenu_T *menu)
18351835
int
18361836
menu_is_toolbar(char_u *name)
18371837
{
1838-
return (STRNCMP(name, "ToolBar", 7) == 0);
1838+
return (STRNCMP(name, "ToolBar", 7) == 0)
1839+
#if defined(FEAT_TOUCHBAR)
1840+
|| (STRNCMP(name, "TouchBar", 8) == 0)
1841+
#endif
1842+
;
18391843
}
18401844

18411845
/*

0 commit comments

Comments
 (0)