From 1cfd28d73ac7609583a4f5ff91522dc737d7f29e Mon Sep 17 00:00:00 2001 From: Christoph Walcher Date: Wed, 9 Aug 2017 16:33:18 +0200 Subject: [PATCH] basic touchbarsupport --- runtime/menu.vim | 17 ++++++ src/MacVim/MMVimController.h | 10 ++- src/MacVim/MMVimController.m | 105 ++++++++++++++++++++++++++++++-- src/MacVim/MMWindowController.m | 17 ++++++ src/evalfunc.c | 3 + src/feature.h | 4 ++ src/menu.c | 6 +- 7 files changed, 155 insertions(+), 7 deletions(-) diff --git a/runtime/menu.vim b/runtime/menu.vim index 35923e5dc6..475e1cfd61 100644 --- a/runtime/menu.vim +++ b/runtime/menu.vim @@ -1266,4 +1266,21 @@ if has("gui_macvim") macm Help.MacVim\ Website action=openWebsite: endif +if has("touchbar") + an TouchBar.Open :browse confirm e + an TouchBar.Save :if expand("%") == ""browse confirm welseconfirm wendif + an TouchBar.SaveAll :browse confirm wa + + an TouchBar.-sep1- + an TouchBar.Undo u + an TouchBar.Redo + + an TouchBar.-sep2- + vnoremenu TouchBar.Cut "+x + vnoremenu TouchBar.Copy "+y + cnoremenu TouchBar.Copy + nnoremenu TouchBar.Paste "+gP + cnoremenu TouchBar.Paste + +endif + " vim: set sw=2 : diff --git a/src/MacVim/MMVimController.h b/src/MacVim/MMVimController.h index cb5665ccf7..a7c14417cd 100644 --- a/src/MacVim/MMVimController.h +++ b/src/MacVim/MMVimController.h @@ -16,7 +16,7 @@ @interface MMVimController : NSObject + NSOpenSavePanelDelegate, NSTouchBarDelegate> { unsigned identifier; BOOL isInitialized; @@ -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 + NSTouchBar *touchbar; + NSMutableDictionary *touchbarItemDict; + NSMutableArray *touchbarItemOrder; + #endif int pid; NSString *serverName; @@ -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 diff --git a/src/MacVim/MMVimController.m b/src/MacVim/MMVimController.m index 66d4b4b556..6a73be96c8 100644 --- a/src/MacVim/MMVimController.m +++ b/src/MacVim/MMVimController.m @@ -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 toolbarItemDict = [[NSMutableDictionary alloc] init]; + touchbarItemDict = [[NSMutableDictionary alloc] init]; + touchbarItemOrder = [[NSMutableArray alloc] init]; + #endif pid = processIdentifier; creationDate = [[NSDate alloc] init]; @@ -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 + [touchbarItemDict release]; touchbarItemDict = nil; + [touchbarItemOrder release]; touchbarItemOrder = nil; + [touchbar release]; touchbar = nil; + #endif [popupMenuItems release]; popupMenuItems = nil; [windowController release]; windowController = nil; @@ -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]; + 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 @@ -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 + 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]; @@ -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"]) { + 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", @@ -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: %@", @@ -1228,7 +1273,11 @@ - (void)enableMenuItemWithDescriptor:(NSArray *)desc state:(BOOL)on NSString *title = [desc lastObject]; [[toolbar itemWithItemIdentifier:title] setEnabled:on]; } - } else { + } 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 @@ -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 { + 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 diff --git a/src/MacVim/MMWindowController.m b/src/MacVim/MMWindowController.m index 5f7ddbf30a..0fb8a70fdf 100644 --- a/src/MacVim/MMWindowController.m +++ b/src/MacVim/MMWindowController.m @@ -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: @@ -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]; +} + +#endif @end // MMWindowController diff --git a/src/evalfunc.c b/src/evalfunc.c index 3c19beb96e..f94fcc2d49 100644 --- a/src/evalfunc.c +++ b/src/evalfunc.c @@ -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 diff --git a/src/feature.h b/src/feature.h index caf0703ddb..7ad772b929 100644 --- a/src/feature.h +++ b/src/feature.h @@ -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 diff --git a/src/menu.c b/src/menu.c index 85aa814478..f009ba1203 100644 --- a/src/menu.c +++ b/src/menu.c @@ -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 + ; } /*