Implement group item
This commit is contained in:
parent
269d899a99
commit
43cc5079d8
3 changed files with 62 additions and 13 deletions
|
@ -395,6 +395,8 @@ bool ScopedDisableResize::disable_resize_ = false;
|
||||||
[idents addObject:[NSString stringWithFormat:@"%@%@", SliderIdentifier, [NSString stringWithUTF8String:item_id.c_str()]]];
|
[idents addObject:[NSString stringWithFormat:@"%@%@", SliderIdentifier, [NSString stringWithUTF8String:item_id.c_str()]]];
|
||||||
} else if (type == "popover") {
|
} else if (type == "popover") {
|
||||||
[idents addObject:[NSString stringWithFormat:@"%@%@", PopOverIdentifier, [NSString stringWithUTF8String:item_id.c_str()]]];
|
[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;
|
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<mate::PersistentDictionary> items;
|
||||||
|
if (!item.Get("items", &items)) {
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
|
||||||
|
NSMutableArray<NSTouchBarItem*>* generatedItems = [[NSMutableArray alloc] init];
|
||||||
|
NSMutableArray<NSString*>* 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 ButtonIdentifier = @"com.electron.tb.button.";
|
||||||
static NSTouchBarItemIdentifier ColorPickerIdentifier = @"com.electron.tb.colorpicker.";
|
static NSTouchBarItemIdentifier ColorPickerIdentifier = @"com.electron.tb.colorpicker.";
|
||||||
// static NSTouchBarItemIdentifier ListIdentifier = @"com.electron.tb.list.";
|
// static NSTouchBarItemIdentifier ListIdentifier = @"com.electron.tb.list.";
|
||||||
|
static NSTouchBarItemIdentifier GroupIdentifier = @"com.electron.tb.group.";
|
||||||
static NSTouchBarItemIdentifier LabelIdentifier = @"com.electron.tb.label.";
|
static NSTouchBarItemIdentifier LabelIdentifier = @"com.electron.tb.label.";
|
||||||
static NSTouchBarItemIdentifier PopOverIdentifier = @"com.electron.tb.popover.";
|
static NSTouchBarItemIdentifier PopOverIdentifier = @"com.electron.tb.popover.";
|
||||||
static NSTouchBarItemIdentifier SliderIdentifier = @"com.electron.tb.slider.";
|
static NSTouchBarItemIdentifier SliderIdentifier = @"com.electron.tb.slider.";
|
||||||
|
|
||||||
- (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier {
|
- (nullable NSTouchBarItem *)makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier {
|
||||||
if ([identifier hasPrefix:ButtonIdentifier]) {
|
if ([identifier hasPrefix:ButtonIdentifier]) {
|
||||||
NSString* id = [self idFromIdentifier:identifier withPrefix:ButtonIdentifier];
|
NSString* id = [self idFromIdentifier:identifier withPrefix:ButtonIdentifier];
|
||||||
return [self makeButtonForID:id withIdentifier:identifier];
|
return [self makeButtonForID:id withIdentifier:identifier];
|
||||||
|
@ -650,11 +681,18 @@ static NSTouchBarItemIdentifier SliderIdentifier = @"com.electron.tb.slider.";
|
||||||
} else if ([identifier hasPrefix:PopOverIdentifier]) {
|
} else if ([identifier hasPrefix:PopOverIdentifier]) {
|
||||||
NSString* id = [self idFromIdentifier:identifier withPrefix:PopOverIdentifier];
|
NSString* id = [self idFromIdentifier:identifier withPrefix:PopOverIdentifier];
|
||||||
return [self makePopOverForID:id withIdentifier:identifier];
|
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;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier {
|
||||||
|
return [self makeItemForIdentifier:identifier];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// NSWindow overrides.
|
// NSWindow overrides.
|
||||||
|
|
||||||
|
|
|
@ -48,11 +48,15 @@ exports.load = (appUrl) => {
|
||||||
label: 'foo',
|
label: 'foo',
|
||||||
showCloseButton: true,
|
showCloseButton: true,
|
||||||
touchBar: new TouchBar([
|
touchBar: new TouchBar([
|
||||||
new (TouchBar.Button)({
|
new (TouchBar.Group)({
|
||||||
label: 'Sub Button',
|
items: new TouchBar(
|
||||||
|
[1, 2, 3].map((i) => new (TouchBar.Button)({
|
||||||
|
label: `Button ${i}`,
|
||||||
click: () => {
|
click: () => {
|
||||||
console.log('Sub Button Clicked')
|
console.log(`Button ${i} (group) Clicked`)
|
||||||
}
|
}
|
||||||
|
}))
|
||||||
|
)
|
||||||
})
|
})
|
||||||
])
|
])
|
||||||
}),
|
}),
|
||||||
|
|
|
@ -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 {
|
TouchBar.Label = class TouchBarLabel extends TouchBarItem {
|
||||||
constructor (config) {
|
constructor (config) {
|
||||||
super(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 {
|
TouchBar.PopOver = class TouchBarPopOver extends TouchBarItem {
|
||||||
constructor (config) {
|
constructor (config) {
|
||||||
super(config)
|
super(config)
|
||||||
|
|
Loading…
Reference in a new issue