2017-02-28 20:29:23 +00:00
|
|
|
// Copyright (c) 2017 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#import "atom/browser/ui/cocoa/atom_touch_bar.h"
|
|
|
|
|
|
|
|
#include "atom/common/color_util.h"
|
|
|
|
#include "atom/common/native_mate_converters/image_converter.h"
|
2017-02-28 20:43:57 +00:00
|
|
|
#include "base/strings/sys_string_conversions.h"
|
2017-02-28 20:29:23 +00:00
|
|
|
#include "skia/ext/skia_utils_mac.h"
|
|
|
|
#include "ui/gfx/image/image.h"
|
|
|
|
|
|
|
|
@implementation AtomTouchBar
|
|
|
|
|
|
|
|
static NSTouchBarItemIdentifier ButtonIdentifier = @"com.electron.touchbar.button.";
|
|
|
|
static NSTouchBarItemIdentifier ColorPickerIdentifier = @"com.electron.touchbar.colorpicker.";
|
|
|
|
static NSTouchBarItemIdentifier GroupIdentifier = @"com.electron.touchbar.group.";
|
|
|
|
static NSTouchBarItemIdentifier LabelIdentifier = @"com.electron.touchbar.label.";
|
|
|
|
static NSTouchBarItemIdentifier PopOverIdentifier = @"com.electron.touchbar.popover.";
|
|
|
|
static NSTouchBarItemIdentifier SliderIdentifier = @"com.electron.touchbar.slider.";
|
|
|
|
|
|
|
|
- (id)initWithDelegate:(id<NSTouchBarDelegate>)delegate
|
|
|
|
window:(atom::NativeWindow*)window {
|
|
|
|
if ((self = [super init])) {
|
|
|
|
delegate_ = delegate;
|
|
|
|
window_ = window;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-03-01 01:12:35 +00:00
|
|
|
- (NSTouchBar*)makeTouchBarFromSettings:(const std::vector<mate::PersistentDictionary>&)settings {
|
|
|
|
NSMutableArray* identifiers = [self identifiersFromSettings:settings];
|
2017-02-28 20:50:11 +00:00
|
|
|
return [self touchBarFromItemIdentifiers:identifiers];
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
|
2017-02-28 20:50:11 +00:00
|
|
|
- (NSTouchBar*)touchBarFromItemIdentifiers:(NSMutableArray*)items {
|
2017-03-01 00:44:03 +00:00
|
|
|
base::scoped_nsobject<NSTouchBar> bar(
|
|
|
|
[[NSClassFromString(@"NSTouchBar") alloc] init]);
|
|
|
|
[bar setDelegate:delegate_];
|
|
|
|
[bar setDefaultItemIdentifiers:items];
|
|
|
|
return bar.autorelease();
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-01 01:12:35 +00:00
|
|
|
- (NSMutableArray*)identifiersFromSettings:(const std::vector<mate::PersistentDictionary>&)dicts {
|
|
|
|
NSMutableArray* identifiers = [[NSMutableArray alloc] init];
|
2017-02-28 20:29:23 +00:00
|
|
|
|
|
|
|
for (const auto& item : dicts) {
|
|
|
|
std::string type;
|
|
|
|
std::string item_id;
|
|
|
|
if (item.Get("type", &type) && item.Get("id", &item_id)) {
|
2017-03-01 01:12:35 +00:00
|
|
|
settings_[item_id] = item;
|
2017-02-28 20:29:23 +00:00
|
|
|
if (type == "button") {
|
2017-03-01 01:12:35 +00:00
|
|
|
[identifiers addObject:[NSString stringWithFormat:@"%@%@", ButtonIdentifier, base::SysUTF8ToNSString(item_id)]];
|
2017-02-28 20:29:23 +00:00
|
|
|
} else if (type == "label") {
|
2017-03-01 01:12:35 +00:00
|
|
|
[identifiers addObject:[NSString stringWithFormat:@"%@%@", LabelIdentifier, base::SysUTF8ToNSString(item_id)]];
|
2017-02-28 20:29:23 +00:00
|
|
|
} else if (type == "colorpicker") {
|
2017-03-01 01:12:35 +00:00
|
|
|
[identifiers addObject:[NSString stringWithFormat:@"%@%@", ColorPickerIdentifier, base::SysUTF8ToNSString(item_id)]];
|
2017-02-28 20:29:23 +00:00
|
|
|
} else if (type == "slider") {
|
2017-03-01 01:12:35 +00:00
|
|
|
[identifiers addObject:[NSString stringWithFormat:@"%@%@", SliderIdentifier, base::SysUTF8ToNSString(item_id)]];
|
2017-02-28 20:29:23 +00:00
|
|
|
} else if (type == "popover") {
|
2017-03-01 01:12:35 +00:00
|
|
|
[identifiers addObject:[NSString stringWithFormat:@"%@%@", PopOverIdentifier, base::SysUTF8ToNSString(item_id)]];
|
2017-02-28 20:29:23 +00:00
|
|
|
} else if (type == "group") {
|
2017-03-01 01:12:35 +00:00
|
|
|
[identifiers addObject:[NSString stringWithFormat:@"%@%@", GroupIdentifier, base::SysUTF8ToNSString(item_id)]];
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-01 01:12:35 +00:00
|
|
|
[identifiers addObject:NSTouchBarItemIdentifierOtherItemsProxy];
|
2017-02-28 20:29:23 +00:00
|
|
|
|
2017-03-01 01:12:35 +00:00
|
|
|
return identifiers;
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSTouchBarItem*)makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier {
|
2017-03-01 00:44:03 +00:00
|
|
|
base::scoped_nsobject<NSTouchBarItem> item;
|
|
|
|
NSString* item_id = nil;
|
|
|
|
|
2017-02-28 20:29:23 +00:00
|
|
|
if ([identifier hasPrefix:ButtonIdentifier]) {
|
2017-03-01 00:44:03 +00:00
|
|
|
item_id = [self idFromIdentifier:identifier withPrefix:ButtonIdentifier];
|
|
|
|
item.reset([self makeButtonForID:item_id withIdentifier:identifier]);
|
2017-02-28 20:29:23 +00:00
|
|
|
} else if ([identifier hasPrefix:LabelIdentifier]) {
|
2017-03-01 00:44:03 +00:00
|
|
|
item_id = [self idFromIdentifier:identifier withPrefix:LabelIdentifier];
|
|
|
|
item.reset([self makeLabelForID:item_id withIdentifier:identifier]);
|
2017-02-28 20:29:23 +00:00
|
|
|
} else if ([identifier hasPrefix:ColorPickerIdentifier]) {
|
2017-03-01 00:44:03 +00:00
|
|
|
item_id = [self idFromIdentifier:identifier withPrefix:ColorPickerIdentifier];
|
|
|
|
item.reset([self makeColorPickerForID:item_id withIdentifier:identifier]);
|
2017-02-28 20:29:23 +00:00
|
|
|
} else if ([identifier hasPrefix:SliderIdentifier]) {
|
2017-03-01 00:44:03 +00:00
|
|
|
item_id = [self idFromIdentifier:identifier withPrefix:SliderIdentifier];
|
|
|
|
item.reset([self makeSliderForID:item_id withIdentifier:identifier]);
|
2017-02-28 20:29:23 +00:00
|
|
|
} else if ([identifier hasPrefix:PopOverIdentifier]) {
|
2017-03-01 00:44:03 +00:00
|
|
|
item_id = [self idFromIdentifier:identifier withPrefix:PopOverIdentifier];
|
|
|
|
item.reset([self makePopoverForID:item_id withIdentifier:identifier]);
|
2017-02-28 20:29:23 +00:00
|
|
|
} else if ([identifier hasPrefix:GroupIdentifier]) {
|
2017-03-01 00:44:03 +00:00
|
|
|
item_id = [self idFromIdentifier:identifier withPrefix:GroupIdentifier];
|
|
|
|
item.reset([self makeGroupForID:item_id withIdentifier:identifier]);
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-01 00:44:03 +00:00
|
|
|
if (item_id)
|
2017-03-01 01:12:35 +00:00
|
|
|
items_[[item_id UTF8String]] = item;
|
2017-02-28 20:29:23 +00:00
|
|
|
|
2017-03-01 00:44:03 +00:00
|
|
|
return item.autorelease();
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-01 00:08:12 +00:00
|
|
|
- (void)refreshTouchBarItem:(const std::string&)item_id {
|
2017-03-01 01:12:35 +00:00
|
|
|
if (items_.find(item_id) == items_.end()) return;
|
2017-03-01 00:08:12 +00:00
|
|
|
if (![self hasItemWithID:item_id]) return;
|
2017-02-28 20:29:23 +00:00
|
|
|
|
2017-03-01 01:12:35 +00:00
|
|
|
mate::PersistentDictionary settings = settings_[item_id];
|
2017-03-01 00:08:12 +00:00
|
|
|
std::string item_type;
|
2017-03-01 01:12:35 +00:00
|
|
|
settings.Get("type", &item_type);
|
2017-03-01 00:08:12 +00:00
|
|
|
|
|
|
|
if (item_type == "button") {
|
2017-03-01 01:12:35 +00:00
|
|
|
[self updateButton:(NSCustomTouchBarItem*)items_[item_id]
|
|
|
|
withSettings:settings];
|
2017-03-01 00:08:12 +00:00
|
|
|
} else if (item_type == "label") {
|
2017-03-01 01:12:35 +00:00
|
|
|
[self updateLabel:(NSCustomTouchBarItem*)items_[item_id]
|
|
|
|
withSettings:settings];
|
2017-03-01 00:08:12 +00:00
|
|
|
} else if (item_type == "colorpicker") {
|
2017-03-01 01:12:35 +00:00
|
|
|
[self updateColorPicker:(NSColorPickerTouchBarItem*)items_[item_id]
|
|
|
|
withSettings:settings];
|
2017-03-01 00:08:12 +00:00
|
|
|
} else if (item_type == "slider") {
|
2017-03-01 01:12:35 +00:00
|
|
|
[self updateSlider:(NSSliderTouchBarItem*)items_[item_id]
|
|
|
|
withSettings:settings];
|
2017-03-01 00:08:12 +00:00
|
|
|
} else if (item_type == "popover") {
|
2017-03-01 01:12:35 +00:00
|
|
|
[self updatePopover:(NSPopoverTouchBarItem*)items_[item_id]
|
|
|
|
withSettings:settings];
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)buttonAction:(id)sender {
|
2017-02-28 23:37:15 +00:00
|
|
|
NSString* item_id = [NSString stringWithFormat:@"%ld", ((NSButton*)sender).tag];
|
|
|
|
window_->NotifyTouchBarItemInteraction([item_id UTF8String],
|
|
|
|
base::DictionaryValue());
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)colorPickerAction:(id)sender {
|
2017-02-28 23:37:15 +00:00
|
|
|
NSString* identifier = ((NSColorPickerTouchBarItem*)sender).identifier;
|
|
|
|
NSString* item_id = [self idFromIdentifier:identifier
|
|
|
|
withPrefix:ColorPickerIdentifier];
|
2017-02-28 20:29:23 +00:00
|
|
|
NSColor* color = ((NSColorPickerTouchBarItem*)sender).color;
|
|
|
|
std::string hex_color = atom::ToRGBHex(skia::NSDeviceColorToSkColor(color));
|
2017-02-28 23:37:15 +00:00
|
|
|
base::DictionaryValue details;
|
|
|
|
details.SetString("color", hex_color);
|
|
|
|
window_->NotifyTouchBarItemInteraction([item_id UTF8String], details);
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)sliderAction:(id)sender {
|
2017-02-28 23:37:15 +00:00
|
|
|
NSString* identifier = ((NSSliderTouchBarItem*)sender).identifier;
|
|
|
|
NSString* item_id = [self idFromIdentifier:identifier
|
|
|
|
withPrefix:SliderIdentifier];
|
|
|
|
base::DictionaryValue details;
|
|
|
|
details.SetInteger("value",
|
|
|
|
[((NSSliderTouchBarItem*)sender).slider intValue]);
|
|
|
|
window_->NotifyTouchBarItemInteraction([item_id UTF8String], details);
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*)idFromIdentifier:(NSString*)identifier withPrefix:(NSString*)prefix {
|
|
|
|
return [identifier substringFromIndex:[prefix length]];
|
|
|
|
}
|
|
|
|
|
2017-03-01 01:12:35 +00:00
|
|
|
- (bool)hasItemWithID:(const std::string&)item_id {
|
|
|
|
return settings_.find(item_id) != settings_.end();
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSColor*)colorFromHexColorString:(const std::string&)colorString {
|
|
|
|
SkColor color = atom::ParseHexColor(colorString);
|
|
|
|
return skia::SkColorToCalibratedNSColor(color);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSTouchBarItem*)makeButtonForID:(NSString*)id
|
2017-02-28 21:50:16 +00:00
|
|
|
withIdentifier:(NSString*)identifier {
|
2017-02-28 20:29:23 +00:00
|
|
|
std::string s_id([id UTF8String]);
|
2017-02-28 20:39:07 +00:00
|
|
|
if (![self hasItemWithID:s_id]) return nil;
|
2017-02-28 20:29:23 +00:00
|
|
|
|
2017-03-01 01:12:35 +00:00
|
|
|
mate::PersistentDictionary settings = settings_[s_id];
|
2017-02-28 20:29:23 +00:00
|
|
|
NSCustomTouchBarItem* item = [[NSClassFromString(@"NSCustomTouchBarItem") alloc] initWithIdentifier:identifier];
|
|
|
|
NSButton* button = [NSButton buttonWithTitle:@""
|
|
|
|
target:self
|
|
|
|
action:@selector(buttonAction:)];
|
|
|
|
button.tag = [id floatValue];
|
|
|
|
item.view = button;
|
2017-03-01 01:12:35 +00:00
|
|
|
[self updateButton:item withSettings:settings];
|
2017-02-28 20:29:23 +00:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updateButton:(NSCustomTouchBarItem*)item
|
2017-03-01 01:12:35 +00:00
|
|
|
withSettings:(const mate::PersistentDictionary&)settings {
|
2017-02-28 20:29:23 +00:00
|
|
|
NSButton* button = (NSButton*)item.view;
|
|
|
|
|
|
|
|
std::string customizationLabel;
|
2017-03-01 01:12:35 +00:00
|
|
|
if (settings.Get("customizationLabel", &customizationLabel)) {
|
2017-02-28 20:43:57 +00:00
|
|
|
item.customizationLabel = base::SysUTF8ToNSString(customizationLabel);
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string backgroundColor;
|
2017-03-01 01:12:35 +00:00
|
|
|
if (settings.Get("backgroundColor", &backgroundColor)) {
|
2017-02-28 20:29:23 +00:00
|
|
|
button.bezelColor = [self colorFromHexColorString:backgroundColor];
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string label;
|
2017-03-01 01:12:35 +00:00
|
|
|
if (settings.Get("label", &label)) {
|
2017-02-28 20:43:57 +00:00
|
|
|
button.title = base::SysUTF8ToNSString(label);
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string labelColor;
|
2017-03-01 01:12:35 +00:00
|
|
|
if (!label.empty() && settings.Get("labelColor", &labelColor)) {
|
2017-02-28 20:43:57 +00:00
|
|
|
NSMutableAttributedString* attrTitle = [[[NSMutableAttributedString alloc] initWithString:base::SysUTF8ToNSString(label)] autorelease];
|
2017-02-28 20:29:23 +00:00
|
|
|
NSRange range = NSMakeRange(0, [attrTitle length]);
|
|
|
|
[attrTitle addAttribute:NSForegroundColorAttributeName
|
|
|
|
value:[self colorFromHexColorString:labelColor]
|
|
|
|
range:range];
|
|
|
|
[attrTitle fixAttributesInRange:range];
|
|
|
|
button.attributedTitle = attrTitle;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx::Image image;
|
2017-03-01 01:12:35 +00:00
|
|
|
if (settings.Get("image", &image)) {
|
2017-02-28 20:29:23 +00:00
|
|
|
button.image = image.AsNSImage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSTouchBarItem*)makeLabelForID:(NSString*)id
|
2017-02-28 21:50:16 +00:00
|
|
|
withIdentifier:(NSString*)identifier {
|
2017-02-28 20:29:23 +00:00
|
|
|
std::string s_id([id UTF8String]);
|
2017-02-28 20:39:07 +00:00
|
|
|
if (![self hasItemWithID:s_id]) return nil;
|
2017-02-28 20:29:23 +00:00
|
|
|
|
2017-03-01 01:12:35 +00:00
|
|
|
mate::PersistentDictionary item = settings_[s_id];
|
2017-02-28 20:29:23 +00:00
|
|
|
NSCustomTouchBarItem* customItem = [[NSClassFromString(@"NSCustomTouchBarItem") alloc] initWithIdentifier:identifier];
|
|
|
|
customItem.view = [NSTextField labelWithString:@""];
|
2017-03-01 01:12:35 +00:00
|
|
|
[self updateLabel:customItem withSettings:item];
|
2017-02-28 20:29:23 +00:00
|
|
|
|
|
|
|
return customItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updateLabel:(NSCustomTouchBarItem*)item
|
2017-03-01 01:12:35 +00:00
|
|
|
withSettings:(const mate::PersistentDictionary&)settings {
|
2017-02-28 20:29:23 +00:00
|
|
|
std::string label;
|
2017-03-01 01:12:35 +00:00
|
|
|
settings.Get("label", &label);
|
2017-02-28 20:29:23 +00:00
|
|
|
NSTextField* text_field = (NSTextField*)item.view;
|
2017-02-28 20:43:57 +00:00
|
|
|
text_field.stringValue = base::SysUTF8ToNSString(label);
|
2017-02-28 20:29:23 +00:00
|
|
|
|
|
|
|
std::string customizationLabel;
|
2017-03-01 01:12:35 +00:00
|
|
|
if (settings.Get("customizationLabel", &customizationLabel)) {
|
2017-02-28 20:43:57 +00:00
|
|
|
item.customizationLabel = base::SysUTF8ToNSString(customizationLabel);
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSTouchBarItem*)makeColorPickerForID:(NSString*)id
|
2017-03-01 20:50:36 +00:00
|
|
|
withIdentifier:(NSString*)identifier {
|
2017-02-28 20:29:23 +00:00
|
|
|
std::string s_id([id UTF8String]);
|
2017-02-28 20:39:07 +00:00
|
|
|
if (![self hasItemWithID:s_id]) return nil;
|
2017-02-28 20:29:23 +00:00
|
|
|
|
2017-03-01 01:12:35 +00:00
|
|
|
mate::PersistentDictionary settings = settings_[s_id];
|
2017-02-28 20:29:23 +00:00
|
|
|
NSColorPickerTouchBarItem* item = [[NSClassFromString(@"NSColorPickerTouchBarItem") alloc] initWithIdentifier:identifier];
|
|
|
|
item.target = self;
|
|
|
|
item.action = @selector(colorPickerAction:);
|
2017-03-01 20:50:36 +00:00
|
|
|
|
|
|
|
std::string selectedColor;
|
|
|
|
if (settings.Get("selectedColor", &selectedColor)) {
|
|
|
|
item.color = [self colorFromHexColorString:selectedColor];
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> colors;
|
|
|
|
if (settings.Get("availableColors", &colors) && colors.size() > 0) {
|
|
|
|
NSColorList* color_list = [[[NSColorList alloc] initWithName:identifier] autorelease];
|
|
|
|
for (size_t i = 0; i < colors.size(); ++i) {
|
|
|
|
[color_list insertColor:[self colorFromHexColorString:colors[i]]
|
|
|
|
key:base::SysUTF8ToNSString(colors[i])
|
|
|
|
atIndex:i];
|
|
|
|
}
|
|
|
|
item.colorList = color_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
item.showsAlpha = NO;
|
|
|
|
|
2017-03-01 01:12:35 +00:00
|
|
|
[self updateColorPicker:item withSettings:settings];
|
2017-02-28 20:29:23 +00:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updateColorPicker:(NSColorPickerTouchBarItem*)item
|
2017-03-01 01:12:35 +00:00
|
|
|
withSettings:(const mate::PersistentDictionary&)settings {
|
2017-02-28 20:29:23 +00:00
|
|
|
std::string customizationLabel;
|
2017-03-01 01:12:35 +00:00
|
|
|
if (settings.Get("customizationLabel", &customizationLabel)) {
|
2017-02-28 20:43:57 +00:00
|
|
|
item.customizationLabel = base::SysUTF8ToNSString(customizationLabel);
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSTouchBarItem*)makeSliderForID:(NSString*)id
|
2017-02-28 21:50:16 +00:00
|
|
|
withIdentifier:(NSString*)identifier {
|
2017-02-28 20:29:23 +00:00
|
|
|
std::string s_id([id UTF8String]);
|
2017-02-28 20:39:07 +00:00
|
|
|
if (![self hasItemWithID:s_id]) return nil;
|
2017-02-28 20:29:23 +00:00
|
|
|
|
2017-03-01 01:12:35 +00:00
|
|
|
mate::PersistentDictionary settings = settings_[s_id];
|
2017-02-28 20:29:23 +00:00
|
|
|
NSSliderTouchBarItem* item = [[NSClassFromString(@"NSSliderTouchBarItem") alloc] initWithIdentifier:identifier];
|
|
|
|
item.target = self;
|
|
|
|
item.action = @selector(sliderAction:);
|
2017-03-01 01:12:35 +00:00
|
|
|
[self updateSlider:item withSettings:settings];
|
2017-02-28 20:29:23 +00:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updateSlider:(NSSliderTouchBarItem*)item
|
2017-03-01 01:12:35 +00:00
|
|
|
withSettings:(const mate::PersistentDictionary&)settings {
|
2017-02-28 20:29:23 +00:00
|
|
|
std::string customizationLabel;
|
2017-03-01 01:12:35 +00:00
|
|
|
if (settings.Get("customizationLabel", &customizationLabel)) {
|
2017-02-28 20:43:57 +00:00
|
|
|
item.customizationLabel = base::SysUTF8ToNSString(customizationLabel);
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string label;
|
2017-03-01 01:12:35 +00:00
|
|
|
settings.Get("label", &label);
|
2017-02-28 20:43:57 +00:00
|
|
|
item.label = base::SysUTF8ToNSString(label);
|
2017-02-28 20:29:23 +00:00
|
|
|
|
|
|
|
int maxValue = 100;
|
|
|
|
int minValue = 0;
|
|
|
|
int initialValue = 50;
|
2017-03-01 01:12:35 +00:00
|
|
|
settings.Get("minValue", &minValue);
|
|
|
|
settings.Get("maxValue", &maxValue);
|
|
|
|
settings.Get("initialValue", &initialValue);
|
2017-02-28 20:29:23 +00:00
|
|
|
|
|
|
|
item.slider.minValue = minValue;
|
|
|
|
item.slider.maxValue = maxValue;
|
|
|
|
item.slider.doubleValue = initialValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSTouchBarItem*)makePopoverForID:(NSString*)id
|
2017-02-28 21:50:16 +00:00
|
|
|
withIdentifier:(NSString*)identifier {
|
2017-02-28 20:45:21 +00:00
|
|
|
std::string s_id([id UTF8String]);
|
2017-02-28 20:39:07 +00:00
|
|
|
if (![self hasItemWithID:s_id]) return nil;
|
2017-02-28 20:29:23 +00:00
|
|
|
|
2017-03-01 01:12:35 +00:00
|
|
|
mate::PersistentDictionary settings = settings_[s_id];
|
2017-02-28 20:29:23 +00:00
|
|
|
NSPopoverTouchBarItem* item = [[NSClassFromString(@"NSPopoverTouchBarItem") alloc] initWithIdentifier:identifier];
|
2017-03-01 01:12:35 +00:00
|
|
|
[self updatePopover:item withSettings:settings];
|
2017-02-28 20:29:23 +00:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updatePopover:(NSPopoverTouchBarItem*)item
|
2017-03-01 01:12:35 +00:00
|
|
|
withSettings:(const mate::PersistentDictionary&)settings {
|
2017-02-28 20:29:23 +00:00
|
|
|
std::string customizationLabel;
|
2017-03-01 01:12:35 +00:00
|
|
|
if (settings.Get("customizationLabel", &customizationLabel)) {
|
2017-02-28 20:43:57 +00:00
|
|
|
item.customizationLabel = base::SysUTF8ToNSString(customizationLabel);
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string label;
|
|
|
|
gfx::Image image;
|
2017-03-01 01:12:35 +00:00
|
|
|
if (settings.Get("label", &label)) {
|
2017-02-28 20:43:57 +00:00
|
|
|
item.collapsedRepresentationLabel = base::SysUTF8ToNSString(label);
|
2017-03-01 01:12:35 +00:00
|
|
|
} else if (settings.Get("image", &image)) {
|
2017-02-28 20:29:23 +00:00
|
|
|
item.collapsedRepresentationImage = image.AsNSImage();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool showCloseButton;
|
2017-03-01 01:12:35 +00:00
|
|
|
if (settings.Get("showCloseButton", &showCloseButton)) {
|
2017-02-28 20:29:23 +00:00
|
|
|
item.showsCloseButton = showCloseButton;
|
|
|
|
}
|
|
|
|
|
2017-02-28 23:37:15 +00:00
|
|
|
mate::PersistentDictionary child;
|
|
|
|
std::vector<mate::PersistentDictionary> items;
|
2017-03-01 01:12:35 +00:00
|
|
|
if (settings.Get("child", &child) && child.Get("ordereredItems", &items)) {
|
|
|
|
item.popoverTouchBar = [self touchBarFromItemIdentifiers:[self identifiersFromSettings:items]];
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSTouchBarItem*)makeGroupForID:(NSString*)id
|
|
|
|
withIdentifier:(NSString*)identifier {
|
|
|
|
std::string s_id([id UTF8String]);
|
2017-02-28 20:39:07 +00:00
|
|
|
if (![self hasItemWithID:s_id]) return nil;
|
2017-03-01 01:12:35 +00:00
|
|
|
mate::PersistentDictionary settings = settings_[s_id];
|
2017-02-28 20:29:23 +00:00
|
|
|
|
2017-02-28 23:37:15 +00:00
|
|
|
mate::PersistentDictionary child;
|
2017-03-01 01:12:35 +00:00
|
|
|
if (!settings.Get("child", &child)) return nil;
|
2017-02-28 20:29:23 +00:00
|
|
|
std::vector<mate::PersistentDictionary> items;
|
2017-02-28 23:37:15 +00:00
|
|
|
if (!child.Get("ordereredItems", &items)) return nil;
|
2017-02-28 20:29:23 +00:00
|
|
|
|
|
|
|
NSMutableArray* generatedItems = [[NSMutableArray alloc] init];
|
2017-03-01 01:12:35 +00:00
|
|
|
NSMutableArray* identList = [self identifiersFromSettings:items];
|
2017-02-28 20:29:23 +00:00
|
|
|
for (NSUInteger i = 0; i < [identList count]; i++) {
|
|
|
|
if ([identList objectAtIndex:i] != NSTouchBarItemIdentifierOtherItemsProxy) {
|
|
|
|
NSTouchBarItem* generatedItem = [self makeItemForIdentifier:[identList objectAtIndex:i]];
|
|
|
|
if (generatedItem) {
|
|
|
|
[generatedItems addObject:generatedItem];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NSGroupTouchBarItem* item = [NSClassFromString(@"NSGroupTouchBarItem") groupItemWithIdentifier:identifier items:generatedItems];
|
|
|
|
std::string customizationLabel;
|
2017-03-01 01:12:35 +00:00
|
|
|
if (settings.Get("customizationLabel", &customizationLabel)) {
|
2017-02-28 20:43:57 +00:00
|
|
|
item.customizationLabel = base::SysUTF8ToNSString(customizationLabel);;
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|