diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index 26430ce625f6..81aa690cabfb 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -395,6 +395,8 @@ bool ScopedDisableResize::disable_resize_ = false; [idents addObject:[NSString stringWithFormat:@"%@%@", SliderIdentifier, [NSString stringWithUTF8String:item_id.c_str()]]]; } else if (type == "popover") { [idents addObject:[NSString stringWithFormat:@"%@%@", PopOverIdentifier, [NSString stringWithUTF8String:item_id.c_str()]]]; + } else if (type == "group") { + [idents addObject:[NSString stringWithFormat:@"%@%@", GroupIdentifier, [NSString stringWithUTF8String:item_id.c_str()]]]; } } } @@ -627,14 +629,43 @@ bool ScopedDisableResize::disable_resize_ = false; return popOverItem; } +- (nullable NSTouchBarItem*) makeGroupForID:(NSString*)id withIdentifier:(NSString*)identifier { + std::string s_id = std::string([id UTF8String]); + if (![self hasTBDict:s_id]) return nil; + mate::PersistentDictionary item = item_id_map[s_id]; + + std::vector items; + if (!item.Get("items", &items)) { + return nil; + } + + NSMutableArray* generatedItems = [[NSMutableArray alloc] init]; + NSMutableArray* identList = [self identifierArrayFromDicts:items]; + for (NSUInteger i = 0; i < [identList count]; i++) { + NSTouchBarItem* generatedItem = [self makeItemForIdentifier:[identList objectAtIndex:i]]; + if (generatedItem) { + [generatedItems addObject:generatedItem]; + } + } + NSGroupTouchBarItem *groupItem = [NSGroupTouchBarItem groupItemWithIdentifier:identifier items:generatedItems]; + + std::string customizationLabel; + if (item.Get("customizationLabel", &customizationLabel)) { + groupItem.customizationLabel = [NSString stringWithUTF8String:customizationLabel.c_str()]; + } + + return groupItem; +} + static NSTouchBarItemIdentifier ButtonIdentifier = @"com.electron.tb.button."; static NSTouchBarItemIdentifier ColorPickerIdentifier = @"com.electron.tb.colorpicker."; // static NSTouchBarItemIdentifier ListIdentifier = @"com.electron.tb.list."; +static NSTouchBarItemIdentifier GroupIdentifier = @"com.electron.tb.group."; static NSTouchBarItemIdentifier LabelIdentifier = @"com.electron.tb.label."; static NSTouchBarItemIdentifier PopOverIdentifier = @"com.electron.tb.popover."; static NSTouchBarItemIdentifier SliderIdentifier = @"com.electron.tb.slider."; -- (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { +- (nullable NSTouchBarItem *)makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { if ([identifier hasPrefix:ButtonIdentifier]) { NSString* id = [self idFromIdentifier:identifier withPrefix:ButtonIdentifier]; return [self makeButtonForID:id withIdentifier:identifier]; @@ -650,11 +681,18 @@ static NSTouchBarItemIdentifier SliderIdentifier = @"com.electron.tb.slider."; } else if ([identifier hasPrefix:PopOverIdentifier]) { NSString* id = [self idFromIdentifier:identifier withPrefix:PopOverIdentifier]; return [self makePopOverForID:id withIdentifier:identifier]; + } else if ([identifier hasPrefix:GroupIdentifier]) { + NSString* id = [self idFromIdentifier:identifier withPrefix:GroupIdentifier]; + return [self makeGroupForID:id withIdentifier:identifier]; } return nil; } +- (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { + return [self makeItemForIdentifier:identifier]; +} + // NSWindow overrides. diff --git a/default_app/default_app.js b/default_app/default_app.js index 401703a9fee9..9ebc6b0ae948 100644 --- a/default_app/default_app.js +++ b/default_app/default_app.js @@ -48,11 +48,15 @@ exports.load = (appUrl) => { label: 'foo', showCloseButton: true, touchBar: new TouchBar([ - new (TouchBar.Button)({ - label: 'Sub Button', - click: () => { - console.log('Sub Button Clicked') - } + new (TouchBar.Group)({ + items: new TouchBar( + [1, 2, 3].map((i) => new (TouchBar.Button)({ + label: `Button ${i}`, + click: () => { + console.log(`Button ${i} (group) Clicked`) + } + })) + ) }) ]) }), diff --git a/lib/browser/api/touch-bar.js b/lib/browser/api/touch-bar.js index 1a397fc573d1..0479e19252ca 100644 --- a/lib/browser/api/touch-bar.js +++ b/lib/browser/api/touch-bar.js @@ -74,6 +74,20 @@ TouchBar.ColorPicker = class TouchBarColorPicker extends TouchBarItem { } } +TouchBar.Group = class TouchBarGroup extends TouchBarItem { + constructor (config) { + super(config) + this.config.type = 'group' + } + + toJSON () { + const config = this.config; + return Object.assign({}, config, { + items: config.items && config.items.toJSON ? config.items.toJSON() : [] + }) + } +} + TouchBar.Label = class TouchBarLabel extends TouchBarItem { constructor (config) { super(config) @@ -81,13 +95,6 @@ TouchBar.Label = class TouchBarLabel extends TouchBarItem { } } -TouchBar.List = class TouchBarList extends TouchBarItem { - constructor (config) { - super(config) - this.config.type = 'list' - } -} - TouchBar.PopOver = class TouchBarPopOver extends TouchBarItem { constructor (config) { super(config)