Skip to content

MBP Touchbar Support #568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions runtime/menu.vim
Original file line number Diff line number Diff line change
Expand Up @@ -1266,4 +1266,21 @@ if has("gui_macvim")
macm Help.MacVim\ Website action=openWebsite:
endif

if has("touchbar")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a better place to do this setup? I'm specifically thinking that customizing the Touch Bar items will likely be something people will want to do quite a lot, and that having to first go through and delete these menu items is somewhat tedious. It might be better if the initial setup of the Touch Bar menu could be deferred somewhere so users could set a global variable in their vimrc to suppress default menu generation and instead generate the items themselves.

an TouchBar.Open :browse confirm e<CR>
an <silent> TouchBar.Save :if expand("%") == ""<Bar>browse confirm w<Bar>else<Bar>confirm w<Bar>endif<CR>
an TouchBar.SaveAll :browse confirm wa<CR>

an TouchBar.-sep1- <Nop>
an TouchBar.Undo u
an TouchBar.Redo <C-R>

an TouchBar.-sep2- <Nop>
vnoremenu TouchBar.Cut "+x
vnoremenu TouchBar.Copy "+y
cnoremenu TouchBar.Copy <C-Y>
nnoremenu TouchBar.Paste "+gP
cnoremenu TouchBar.Paste <C-R>+
endif

" vim: set sw=2 :
10 changes: 9 additions & 1 deletion src/MacVim/MMVimController.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


@interface MMVimController : NSObject<NSToolbarDelegate,
NSOpenSavePanelDelegate>
NSOpenSavePanelDelegate, NSTouchBarDelegate>
{
unsigned identifier;
BOOL isInitialized;
Expand All @@ -28,6 +28,11 @@
// TODO: Move all toolbar code to window controller?
NSToolbar *toolbar;
NSMutableDictionary *toolbarItemDict;
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indenting on this preprocessor line (and the corresponding #endif) is off.

NSTouchBar *touchbar;
NSMutableDictionary *touchbarItemDict;
NSMutableArray *touchbarItemOrder;
#endif

int pid;
NSString *serverName;
Expand Down Expand Up @@ -65,4 +70,7 @@
- (id)evaluateVimExpressionCocoa:(NSString *)expr
errorString:(NSString **)errstr;
- (void)processInputQueue:(NSArray *)queue;
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
- (NSTouchBar *)makeTouchBar;
#endif
@end
105 changes: 100 additions & 5 deletions src/MacVim/MMVimController.m
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ - (id)initWithBackend:(id)backend pid:(int)processIdentifier
[[MMWindowController alloc] initWithVimController:self];
backendProxy = [backend retain];
popupMenuItems = [[NSMutableArray alloc] init];
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indent on this #if/#endif pair is off.

toolbarItemDict = [[NSMutableDictionary alloc] init];
touchbarItemDict = [[NSMutableDictionary alloc] init];
touchbarItemOrder = [[NSMutableArray alloc] init];
#endif
pid = processIdentifier;
creationDate = [[NSDate alloc] init];

Expand Down Expand Up @@ -178,6 +182,11 @@ - (void)dealloc

[toolbarItemDict release]; toolbarItemDict = nil;
[toolbar release]; toolbar = nil;
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indent on this #if/#endif pair is off.

[touchbarItemDict release]; touchbarItemDict = nil;
[touchbarItemOrder release]; touchbarItemOrder = nil;
[touchbar release]; touchbar = nil;
#endif
[popupMenuItems release]; popupMenuItems = nil;
[windowController release]; windowController = nil;

Expand Down Expand Up @@ -488,7 +497,25 @@ - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)theToolbar
{
return nil;
}
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
- (NSTouchBar *)makeTouchBar
{
touchbar = [[NSTouchBar alloc] init];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 503 - 506 have mixed tab/space indenting and are too far in.

touchbar.delegate = self;
touchbar.defaultItemIdentifiers = [NSArray arrayWithArray: touchbarItemOrder];
return touchbar;
}

- (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)itemId
{
NSTouchBarItem *item = [touchbarItemDict objectForKey:itemId];
if (!item) {
ASLogWarn(@"No touchbar item with id '%@'", itemId);
}

return item;
}
#endif
@end // MMVimController


Expand Down Expand Up @@ -1081,7 +1108,10 @@ - (void)addMenuWithDescriptor:(NSArray *)desc atIndex:(int)idx

return;
}

#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indent of this #if/#endif block is off.

if ([rootName isEqual:@"TouchBar"])
return;
#endif
// This is either a main menu item or a popup menu item.
NSString *title = [desc lastObject];
NSMenuItem *item = [[NSMenuItem alloc] init];
Expand Down Expand Up @@ -1132,7 +1162,13 @@ - (void)addMenuItemWithDescriptor:(NSArray *)desc
[self addToolbarItemWithLabel:title tip:tip icon:icon atIndex:idx];
return;
}

#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
if ([rootName isEqual:@"TouchBar"]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point it might be worth writing isTouchBarMenuName or similar, or at least making the name a constant.

if (toolbar && [desc count] == 2)
[self addTouchbarItemWithLabel:title icon:icon atIndex:idx];
return;
}
#endif
NSMenu *parent = [self parentMenuForDescriptor:desc];
if (!parent) {
ASLogWarn(@"Menu item '%@' has no parent",
Expand Down Expand Up @@ -1195,7 +1231,16 @@ - (void)removeMenuItemWithDescriptor:(NSArray *)desc
}
return;
}

#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
if ([rootName isEqual:@"TouchBar"]){
if ([desc count] == 2) {
[touchbarItemOrder removeObject:title];
[touchbarItemDict removeObjectForKey:title];
[windowController setTouchBar:nil];
}
return;
}
#endif
NSMenuItem *item = [self menuItemForDescriptor:desc];
if (!item) {
ASLogWarn(@"Failed to remove menu item, descriptor not found: %@",
Expand Down Expand Up @@ -1228,7 +1273,11 @@ - (void)enableMenuItemWithDescriptor:(NSArray *)desc state:(BOOL)on
NSString *title = [desc lastObject];
[[toolbar itemWithItemIdentifier:title] setEnabled:on];
}
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For clarity, this should be written in a way that doesn't inject the preprocessor check between the else and the if; this kind of construct is error-prone and can be difficult to follow (I realize it might be elsewhere in MacVim's codebase, but I'd rather we don't propagate it). I'd rather see something more like:

} else {
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
  if ([rootName isEqual:@"TouchBar")) { return; }
#endif

// Use tag to set...

} else
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
if (![rootName isEqual:@"TouchBar"])
#endif
{
// Use tag to set whether item is enabled or disabled instead of
// calling setEnabled:. This way the menus can autoenable themselves
// but at the same time Vim can set if a menu is enabled whenever it
Expand Down Expand Up @@ -1306,7 +1355,53 @@ - (void)addToolbarItemWithLabel:(NSString *)label

[toolbar insertItemWithItemIdentifier:label atIndex:idx];
}

#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
- (void)addTouchbarItemWithLabel:(NSString *)label
icon:(NSString *)icon
atIndex:(int)idx
{
// Check for separator items.
if (!label) {
label = NSTouchBarItemIdentifierFixedSpaceLarge;
} else if ([label length] >= 2 && [label hasPrefix:@"-"]
&& [label hasSuffix:@"-"]) {
// The label begins and ends with '-'; decided which kind of separator
// item it is by looking at the prefix.
if ([label hasPrefix:@"-space"]) {
label = NSTouchBarItemIdentifierFixedSpaceSmall;
} else if ([label hasPrefix:@"-flexspace"]) {
label = NSTouchBarItemIdentifierFlexibleSpace;
} else {
label = NSTouchBarItemIdentifierFixedSpaceLarge;
}
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole else block has mixed tab/space indenting and seemingly overly deep indents.

NSButton* button = [NSButton buttonWithTitle:label target:windowController action:@selector(vimTouchbarItemAction:)];
NSCustomTouchBarItem *item =
[[NSCustomTouchBarItem alloc] initWithIdentifier:label];
NSImage *img = [NSImage imageNamed:icon];

if (!img) {
img = [[[NSImage alloc] initByReferencingFile:icon] autorelease];
if (!(img && [img isValid]))
img = nil;
}
if (img) {
[button setImage: img];
//[button setImagePosition:NSImageLeft];
[button setImagePosition:NSImageOnly];
}

[item setView:button];
[touchbarItemDict setObject:item forKey:label];
}

int maxIdx = [touchbarItemOrder count];
if (maxIdx < idx) idx = maxIdx;
[touchbarItemOrder insertObject:label atIndex:idx];

[windowController setTouchBar:nil];
}
#endif
- (void)popupMenuWithDescriptor:(NSArray *)desc
atRow:(NSNumber *)row
column:(NSNumber *)col
Expand Down
17 changes: 17 additions & 0 deletions src/MacVim/MMWindowController.m
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,16 @@ - (IBAction)vimToolbarItemAction:(id)sender
[vimController sendMessage:ExecuteMenuMsgID data:[attrs dictionaryAsData]];
}

#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
- (IBAction)vimTouchbarItemAction:(id)sender
{
NSArray *desc = [NSArray arrayWithObjects:@"TouchBar", [sender title], nil];
NSDictionary *attrs = [NSDictionary dictionaryWithObject:desc
forKey:@"descriptor"];
[vimController sendMessage:ExecuteMenuMsgID data:[attrs dictionaryAsData]];
}

#endif
- (IBAction)fontSizeUp:(id)sender
{
[[NSFontManager sharedFontManager] modifyFont:
Expand Down Expand Up @@ -1332,6 +1342,13 @@ - (void)runAfterWindowPresentedUsingBlock:(void (^)(void))block
[afterWindowPresentedQueue addObject:[block copy]];
}

#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_12
- (NSTouchBar *)makeTouchBar
{
return [vimController makeTouchBar];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A tab is used to indent here instead of spaces.

}

#endif
@end // MMWindowController


Expand Down
3 changes: 3 additions & 0 deletions src/evalfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -5966,6 +5966,9 @@ f_has(typval_T *argvars, typval_T *rettv)
#ifdef FEAT_TOOLBAR
"toolbar",
#endif
#ifdef FEAT_TOUCHBAR
"touchbar",
#endif
#ifdef FEAT_TRANSPARENCY
"transparency",
#endif
Expand Down
4 changes: 4 additions & 0 deletions src/feature.h
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,10 @@
#if defined(FEAT_TOOLBAR) && !defined(FEAT_MENU)
# define FEAT_MENU
#endif

#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MACVIM)
# define FEAT_TOUCHBAR
#endif

/*
* GUI tabline
Expand Down
6 changes: 5 additions & 1 deletion src/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -1840,7 +1840,11 @@ menu_is_child_of_popup(vimmenu_T *menu)
int
menu_is_toolbar(char_u *name)
{
return (STRNCMP(name, "ToolBar", 7) == 0);
return (STRNCMP(name, "ToolBar", 7) == 0)
#if defined(FEAT_TOUCHBAR)
|| (STRNCMP(name, "TouchBar", 8) == 0)
#endif
;
}

/*
Expand Down