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
|
|
|
|
|
2018-04-20 18:47:04 +00:00
|
|
|
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.";
|
|
|
|
static NSTouchBarItemIdentifier SegmentedControlIdentifier =
|
|
|
|
@"com.electron.touchbar.segmentedcontrol.";
|
|
|
|
static NSTouchBarItemIdentifier ScrubberIdentifier =
|
|
|
|
@"com.electron.touchbar.scrubber.";
|
2017-03-12 23:51:12 +00:00
|
|
|
|
|
|
|
static NSString* const TextScrubberItemIdentifier = @"scrubber.text.item";
|
|
|
|
static NSString* const ImageScrubberItemIdentifier = @"scrubber.image.item";
|
2017-02-28 20:29:23 +00:00
|
|
|
|
|
|
|
- (id)initWithDelegate:(id<NSTouchBarDelegate>)delegate
|
2017-03-01 23:26:10 +00:00
|
|
|
window:(atom::NativeWindow*)window
|
2018-04-20 18:47:04 +00:00
|
|
|
settings:
|
|
|
|
(const std::vector<mate::PersistentDictionary>&)settings {
|
2017-02-28 20:29:23 +00:00
|
|
|
if ((self = [super init])) {
|
|
|
|
delegate_ = delegate;
|
|
|
|
window_ = window;
|
2017-03-01 23:26:10 +00:00
|
|
|
ordered_settings_ = settings;
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-03-01 23:26:10 +00:00
|
|
|
- (NSTouchBar*)makeTouchBar {
|
2018-04-20 18:47:04 +00:00
|
|
|
NSMutableArray* identifiers =
|
|
|
|
[self identifiersFromSettings:ordered_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
|
|
|
}
|
|
|
|
|
2018-04-20 18:47:04 +00:00
|
|
|
- (NSMutableArray*)identifiersFromSettings:
|
|
|
|
(const std::vector<mate::PersistentDictionary>&)dicts {
|
2017-03-02 17:16:29 +00:00
|
|
|
NSMutableArray* identifiers = [NSMutableArray array];
|
2017-02-28 20:29:23 +00:00
|
|
|
|
2018-04-17 23:45:26 +00:00
|
|
|
if (@available(macOS 10.12.2, *)) {
|
|
|
|
for (const auto& item : dicts) {
|
|
|
|
std::string type;
|
|
|
|
std::string item_id;
|
|
|
|
if (item.Get("type", &type) && item.Get("id", &item_id)) {
|
|
|
|
NSTouchBarItemIdentifier identifier = nil;
|
|
|
|
if (type == "spacer") {
|
|
|
|
std::string size;
|
|
|
|
item.Get("size", &size);
|
|
|
|
if (size == "large") {
|
|
|
|
identifier = NSTouchBarItemIdentifierFixedSpaceLarge;
|
|
|
|
} else if (size == "flexible") {
|
|
|
|
identifier = NSTouchBarItemIdentifierFlexibleSpace;
|
|
|
|
} else {
|
|
|
|
identifier = NSTouchBarItemIdentifierFixedSpaceSmall;
|
|
|
|
}
|
2017-03-01 22:10:52 +00:00
|
|
|
} else {
|
2018-04-17 23:45:26 +00:00
|
|
|
identifier = [self identifierFromID:item_id type:type];
|
2017-03-01 22:10:52 +00:00
|
|
|
}
|
2017-03-02 18:23:24 +00:00
|
|
|
|
2018-04-17 23:45:26 +00:00
|
|
|
if (identifier) {
|
|
|
|
settings_[item_id] = item;
|
|
|
|
[identifiers addObject:identifier];
|
|
|
|
}
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-17 23:45:26 +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
|
|
|
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];
|
2017-03-02 18:23:24 +00:00
|
|
|
return [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];
|
2017-03-02 18:23:24 +00:00
|
|
|
return [self makeLabelForID:item_id withIdentifier:identifier];
|
2017-02-28 20:29:23 +00:00
|
|
|
} else if ([identifier hasPrefix:ColorPickerIdentifier]) {
|
2018-04-20 18:47:04 +00:00
|
|
|
item_id =
|
|
|
|
[self idFromIdentifier:identifier withPrefix:ColorPickerIdentifier];
|
2017-03-02 18:23:24 +00:00
|
|
|
return [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];
|
2017-03-02 18:23:24 +00:00
|
|
|
return [self makeSliderForID:item_id withIdentifier:identifier];
|
2017-03-02 18:28:07 +00:00
|
|
|
} else if ([identifier hasPrefix:PopoverIdentifier]) {
|
|
|
|
item_id = [self idFromIdentifier:identifier withPrefix:PopoverIdentifier];
|
2017-03-02 18:23:24 +00:00
|
|
|
return [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];
|
2017-03-02 18:23:24 +00:00
|
|
|
return [self makeGroupForID:item_id withIdentifier:identifier];
|
2017-03-10 06:40:39 +00:00
|
|
|
} else if ([identifier hasPrefix:SegmentedControlIdentifier]) {
|
2018-04-20 18:47:04 +00:00
|
|
|
item_id = [self idFromIdentifier:identifier
|
|
|
|
withPrefix:SegmentedControlIdentifier];
|
2017-03-10 06:40:39 +00:00
|
|
|
return [self makeSegmentedControlForID:item_id withIdentifier:identifier];
|
2017-03-12 23:51:12 +00:00
|
|
|
} else if ([identifier hasPrefix:ScrubberIdentifier]) {
|
|
|
|
item_id = [self idFromIdentifier:identifier withPrefix:ScrubberIdentifier];
|
|
|
|
return [self makeScrubberForID:item_id withIdentifier:identifier];
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-02 18:23:24 +00:00
|
|
|
return nil;
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-02 18:23:24 +00:00
|
|
|
- (void)refreshTouchBarItem:(NSTouchBar*)touchBar
|
2017-03-27 10:10:14 +00:00
|
|
|
id:(NSTouchBarItemIdentifier)identifier
|
2017-04-03 17:57:57 +00:00
|
|
|
withType:(const std::string&)item_type
|
2018-04-17 23:45:26 +00:00
|
|
|
withSettings:(const mate::PersistentDictionary&)settings
|
2018-04-20 18:47:04 +00:00
|
|
|
API_AVAILABLE(macosx(10.12.2)) {
|
2017-03-27 10:10:14 +00:00
|
|
|
NSTouchBarItem* item = [touchBar itemForIdentifier:identifier];
|
2018-04-20 18:47:04 +00:00
|
|
|
if (!item)
|
|
|
|
return;
|
2017-03-02 18:23:24 +00:00
|
|
|
|
2017-03-01 00:08:12 +00:00
|
|
|
if (item_type == "button") {
|
2017-03-02 18:23:24 +00:00
|
|
|
[self updateButton:(NSCustomTouchBarItem*)item withSettings:settings];
|
2017-03-01 00:08:12 +00:00
|
|
|
} else if (item_type == "label") {
|
2017-03-02 18:23:24 +00:00
|
|
|
[self updateLabel:(NSCustomTouchBarItem*)item withSettings:settings];
|
2017-03-01 00:08:12 +00:00
|
|
|
} else if (item_type == "colorpicker") {
|
2017-03-02 18:23:24 +00:00
|
|
|
[self updateColorPicker:(NSColorPickerTouchBarItem*)item
|
2017-03-29 04:01:14 +00:00
|
|
|
withSettings:settings];
|
2017-03-01 00:08:12 +00:00
|
|
|
} else if (item_type == "slider") {
|
2017-03-02 18:23:24 +00:00
|
|
|
[self updateSlider:(NSSliderTouchBarItem*)item withSettings:settings];
|
2017-03-01 00:08:12 +00:00
|
|
|
} else if (item_type == "popover") {
|
2017-03-02 18:23:24 +00:00
|
|
|
[self updatePopover:(NSPopoverTouchBarItem*)item withSettings:settings];
|
2017-03-12 23:51:12 +00:00
|
|
|
} else if (item_type == "segmented_control") {
|
2018-04-20 18:47:04 +00:00
|
|
|
[self updateSegmentedControl:(NSCustomTouchBarItem*)item
|
|
|
|
withSettings:settings];
|
2017-03-12 23:51:12 +00:00
|
|
|
} else if (item_type == "scrubber") {
|
|
|
|
[self updateScrubber:(NSCustomTouchBarItem*)item withSettings:settings];
|
2018-02-12 17:53:04 +00:00
|
|
|
} else if (item_type == "group") {
|
|
|
|
[self updateGroup:(NSGroupTouchBarItem*)item withSettings:settings];
|
2017-03-12 23:51:12 +00:00
|
|
|
}
|
2017-03-27 00:22:52 +00:00
|
|
|
}
|
|
|
|
|
2018-04-20 18:47:04 +00:00
|
|
|
- (void)addNonDefaultTouchBarItems:
|
|
|
|
(const std::vector<mate::PersistentDictionary>&)items {
|
2017-03-27 00:22:52 +00:00
|
|
|
[self identifiersFromSettings:items];
|
|
|
|
}
|
2017-03-13 17:07:48 +00:00
|
|
|
|
2018-04-20 18:47:04 +00:00
|
|
|
- (void)setEscapeTouchBarItem:(const mate::PersistentDictionary&)item
|
|
|
|
forTouchBar:(NSTouchBar*)touchBar {
|
|
|
|
if (![touchBar
|
|
|
|
respondsToSelector:@selector(escapeKeyReplacementItemIdentifier)])
|
|
|
|
return;
|
2017-03-27 00:22:52 +00:00
|
|
|
std::string type;
|
|
|
|
std::string item_id;
|
|
|
|
NSTouchBarItemIdentifier identifier = nil;
|
|
|
|
if (item.Get("type", &type) && item.Get("id", &item_id)) {
|
|
|
|
identifier = [self identifierFromID:item_id type:type];
|
|
|
|
}
|
|
|
|
if (identifier) {
|
2018-04-20 18:47:04 +00:00
|
|
|
[self addNonDefaultTouchBarItems:{item}];
|
2017-03-27 00:22:52 +00:00
|
|
|
touchBar.escapeKeyReplacementItemIdentifier = identifier;
|
|
|
|
} else {
|
|
|
|
touchBar.escapeKeyReplacementItemIdentifier = nil;
|
|
|
|
}
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-27 10:10:14 +00:00
|
|
|
- (void)refreshTouchBarItem:(NSTouchBar*)touchBar
|
|
|
|
id:(const std::string&)item_id {
|
2018-04-20 18:47:04 +00:00
|
|
|
if (![self hasItemWithID:item_id])
|
|
|
|
return;
|
2017-03-27 10:10:14 +00:00
|
|
|
|
|
|
|
mate::PersistentDictionary settings = settings_[item_id];
|
|
|
|
std::string item_type;
|
|
|
|
settings.Get("type", &item_type);
|
|
|
|
|
2017-04-03 18:05:16 +00:00
|
|
|
auto identifier = [self identifierFromID:item_id type:item_type];
|
2018-04-20 18:47:04 +00:00
|
|
|
if (!identifier)
|
|
|
|
return;
|
2017-03-27 10:10:14 +00:00
|
|
|
|
2018-02-12 17:53:04 +00:00
|
|
|
std::vector<mate::Dictionary> parents;
|
|
|
|
settings.Get("_parents", &parents);
|
|
|
|
for (auto& parent : parents) {
|
|
|
|
std::string parent_type;
|
|
|
|
std::string parent_id;
|
|
|
|
if (!parent.Get("type", &parent_type) || !parent.Get("id", &parent_id))
|
|
|
|
continue;
|
|
|
|
auto parentIdentifier = [self identifierFromID:parent_id type:parent_type];
|
2018-04-20 18:47:04 +00:00
|
|
|
if (!parentIdentifier)
|
|
|
|
continue;
|
2018-02-12 17:53:04 +00:00
|
|
|
|
|
|
|
if (parent_type == "popover") {
|
|
|
|
NSPopoverTouchBarItem* popoverItem =
|
2018-04-20 18:47:04 +00:00
|
|
|
[touchBar itemForIdentifier:parentIdentifier];
|
2018-02-12 17:53:04 +00:00
|
|
|
[self refreshTouchBarItem:popoverItem.popoverTouchBar
|
2018-04-20 18:47:04 +00:00
|
|
|
id:identifier
|
|
|
|
withType:item_type
|
|
|
|
withSettings:settings];
|
2018-02-12 17:53:04 +00:00
|
|
|
} else if (parent_type == "group") {
|
|
|
|
NSGroupTouchBarItem* groupItem =
|
2018-04-20 18:47:04 +00:00
|
|
|
[touchBar itemForIdentifier:parentIdentifier];
|
2018-02-12 17:53:04 +00:00
|
|
|
[self refreshTouchBarItem:groupItem.groupTouchBar
|
|
|
|
id:identifier
|
|
|
|
withType:item_type
|
|
|
|
withSettings:settings];
|
|
|
|
}
|
2017-03-27 10:10:14 +00:00
|
|
|
}
|
2017-04-03 18:05:16 +00:00
|
|
|
|
|
|
|
[self refreshTouchBarItem:touchBar
|
|
|
|
id:identifier
|
|
|
|
withType:item_type
|
|
|
|
withSettings:settings];
|
2017-03-27 10:10:14 +00:00
|
|
|
}
|
|
|
|
|
2017-02-28 20:29:23 +00:00
|
|
|
- (void)buttonAction:(id)sender {
|
2018-04-20 18:47:04 +00:00
|
|
|
NSString* item_id =
|
|
|
|
[NSString stringWithFormat:@"%ld", ((NSButton*)sender).tag];
|
2017-02-28 23:37:15 +00:00
|
|
|
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;
|
2018-04-20 18:47:04 +00:00
|
|
|
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;
|
2018-04-20 18:47:04 +00:00
|
|
|
NSString* item_id =
|
|
|
|
[self idFromIdentifier:identifier withPrefix:SliderIdentifier];
|
2017-02-28 23:37:15 +00:00
|
|
|
base::DictionaryValue details;
|
|
|
|
details.SetInteger("value",
|
|
|
|
[((NSSliderTouchBarItem*)sender).slider intValue]);
|
|
|
|
window_->NotifyTouchBarItemInteraction([item_id UTF8String], details);
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-02 18:32:55 +00:00
|
|
|
- (NSString*)idFromIdentifier:(NSString*)identifier
|
2018-04-20 18:47:04 +00:00
|
|
|
withPrefix:(NSString*)prefix {
|
2017-02-28 20:29:23 +00:00
|
|
|
return [identifier substringFromIndex:[prefix length]];
|
|
|
|
}
|
|
|
|
|
2017-03-10 06:40:39 +00:00
|
|
|
- (void)segmentedControlAction:(id)sender {
|
2018-04-20 18:47:04 +00:00
|
|
|
NSString* item_id =
|
|
|
|
[NSString stringWithFormat:@"%ld", ((NSSegmentedControl*)sender).tag];
|
2017-03-10 06:40:39 +00:00
|
|
|
base::DictionaryValue details;
|
2018-04-20 18:47:04 +00:00
|
|
|
details.SetInteger("selectedIndex",
|
|
|
|
((NSSegmentedControl*)sender).selectedSegment);
|
|
|
|
details.SetBoolean(
|
|
|
|
"isSelected",
|
|
|
|
[((NSSegmentedControl*)sender)
|
|
|
|
isSelectedForSegment:((NSSegmentedControl*)sender).selectedSegment]);
|
|
|
|
window_->NotifyTouchBarItemInteraction([item_id UTF8String], details);
|
2017-03-10 06:40:39 +00:00
|
|
|
}
|
|
|
|
|
2018-04-20 18:47:04 +00:00
|
|
|
- (void)scrubber:(NSScrubber*)scrubber
|
|
|
|
didSelectItemAtIndex:(NSInteger)selectedIndex
|
|
|
|
API_AVAILABLE(macosx(10.12.2)) {
|
2017-03-12 23:51:12 +00:00
|
|
|
base::DictionaryValue details;
|
2017-03-13 17:28:30 +00:00
|
|
|
details.SetInteger("selectedIndex", selectedIndex);
|
2017-03-12 23:51:12 +00:00
|
|
|
details.SetString("type", "select");
|
2018-04-20 18:47:04 +00:00
|
|
|
window_->NotifyTouchBarItemInteraction([scrubber.identifier UTF8String],
|
|
|
|
details);
|
2017-03-12 23:51:12 +00:00
|
|
|
}
|
|
|
|
|
2018-04-20 18:47:04 +00:00
|
|
|
- (void)scrubber:(NSScrubber*)scrubber
|
|
|
|
didHighlightItemAtIndex:(NSInteger)highlightedIndex
|
|
|
|
API_AVAILABLE(macosx(10.12.2)) {
|
2017-03-12 23:51:12 +00:00
|
|
|
base::DictionaryValue details;
|
2017-03-13 17:28:30 +00:00
|
|
|
details.SetInteger("highlightedIndex", highlightedIndex);
|
2017-03-12 23:51:12 +00:00
|
|
|
details.SetString("type", "highlight");
|
2018-04-20 18:47:04 +00:00
|
|
|
window_->NotifyTouchBarItemInteraction([scrubber.identifier UTF8String],
|
|
|
|
details);
|
2017-03-12 23:51:12 +00:00
|
|
|
}
|
|
|
|
|
2017-03-02 18:23:24 +00:00
|
|
|
- (NSTouchBarItemIdentifier)identifierFromID:(const std::string&)item_id
|
|
|
|
type:(const std::string&)type {
|
|
|
|
NSTouchBarItemIdentifier base_identifier = nil;
|
|
|
|
if (type == "button")
|
|
|
|
base_identifier = ButtonIdentifier;
|
|
|
|
else if (type == "label")
|
|
|
|
base_identifier = LabelIdentifier;
|
|
|
|
else if (type == "colorpicker")
|
|
|
|
base_identifier = ColorPickerIdentifier;
|
|
|
|
else if (type == "slider")
|
|
|
|
base_identifier = SliderIdentifier;
|
|
|
|
else if (type == "popover")
|
2017-03-02 18:28:07 +00:00
|
|
|
base_identifier = PopoverIdentifier;
|
2017-03-02 18:23:24 +00:00
|
|
|
else if (type == "group")
|
|
|
|
base_identifier = GroupIdentifier;
|
2017-03-10 06:40:39 +00:00
|
|
|
else if (type == "segmented_control")
|
|
|
|
base_identifier = SegmentedControlIdentifier;
|
2017-03-12 23:51:12 +00:00
|
|
|
else if (type == "scrubber")
|
|
|
|
base_identifier = ScrubberIdentifier;
|
2017-03-02 18:23:24 +00:00
|
|
|
|
|
|
|
if (base_identifier)
|
|
|
|
return [NSString stringWithFormat:@"%@%s", base_identifier, item_id.data()];
|
|
|
|
else
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
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);
|
2017-03-02 17:22:57 +00:00
|
|
|
return skia::SkColorToDeviceNSColor(color);
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (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]);
|
2018-04-20 18:47:04 +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-03-02 18:23:24 +00:00
|
|
|
base::scoped_nsobject<NSCustomTouchBarItem> item([[NSClassFromString(
|
|
|
|
@"NSCustomTouchBarItem") alloc] initWithIdentifier:identifier]);
|
2017-02-28 20:29:23 +00:00
|
|
|
NSButton* button = [NSButton buttonWithTitle:@""
|
|
|
|
target:self
|
|
|
|
action:@selector(buttonAction:)];
|
|
|
|
button.tag = [id floatValue];
|
2017-03-02 18:23:24 +00:00
|
|
|
[item setView:button];
|
2017-03-01 01:12:35 +00:00
|
|
|
[self updateButton:item withSettings:settings];
|
2017-03-02 18:23:24 +00:00
|
|
|
return item.autorelease();
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (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 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-03 23:24:32 +00:00
|
|
|
settings.Get("label", &label);
|
|
|
|
button.title = base::SysUTF8ToNSString(label);
|
2017-02-28 20:29:23 +00:00
|
|
|
|
|
|
|
gfx::Image image;
|
2017-03-02 20:56:23 +00:00
|
|
|
if (settings.Get("icon", &image)) {
|
2017-02-28 20:29:23 +00:00
|
|
|
button.image = image.AsNSImage();
|
2017-04-18 15:58:54 +00:00
|
|
|
|
|
|
|
std::string iconPosition;
|
|
|
|
settings.Get("iconPosition", &iconPosition);
|
2017-04-17 00:30:55 +00:00
|
|
|
if (iconPosition == "left") {
|
2017-04-18 15:58:54 +00:00
|
|
|
button.imagePosition = NSImageLeft;
|
2017-04-17 00:26:33 +00:00
|
|
|
} else if (iconPosition == "right") {
|
2017-04-18 15:58:54 +00:00
|
|
|
button.imagePosition = NSImageRight;
|
2017-04-17 00:26:33 +00:00
|
|
|
} else {
|
2017-04-18 15:58:54 +00:00
|
|
|
button.imagePosition = NSImageOverlaps;
|
2017-04-17 00:26:33 +00:00
|
|
|
}
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (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]);
|
2018-04-20 18:47:04 +00:00
|
|
|
if (![self hasItemWithID:s_id])
|
|
|
|
return nil;
|
2017-02-28 20:29:23 +00:00
|
|
|
|
2017-03-02 18:23:24 +00:00
|
|
|
mate::PersistentDictionary settings = settings_[s_id];
|
|
|
|
base::scoped_nsobject<NSCustomTouchBarItem> item([[NSClassFromString(
|
|
|
|
@"NSCustomTouchBarItem") alloc] initWithIdentifier:identifier]);
|
|
|
|
[item setView:[NSTextField labelWithString:@""]];
|
|
|
|
[self updateLabel:item withSettings:settings];
|
|
|
|
return item.autorelease();
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updateLabel:(NSCustomTouchBarItem*)item
|
2017-03-01 01:12:35 +00:00
|
|
|
withSettings:(const mate::PersistentDictionary&)settings {
|
2017-03-03 23:14:51 +00:00
|
|
|
NSTextField* text_field = (NSTextField*)item.view;
|
|
|
|
|
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
|
|
|
text_field.stringValue = base::SysUTF8ToNSString(label);
|
2017-03-03 23:14:51 +00:00
|
|
|
|
|
|
|
std::string textColor;
|
|
|
|
if (settings.Get("textColor", &textColor) && !textColor.empty()) {
|
|
|
|
text_field.textColor = [self colorFromHexColorString:textColor];
|
|
|
|
} else {
|
|
|
|
text_field.textColor = nil;
|
|
|
|
}
|
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]);
|
2018-04-20 18:47:04 +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-03-02 18:23:24 +00:00
|
|
|
base::scoped_nsobject<NSColorPickerTouchBarItem> item([[NSClassFromString(
|
2018-04-20 18:47:04 +00:00
|
|
|
@"NSColorPickerTouchBarItem") alloc] initWithIdentifier:identifier]);
|
2017-03-02 18:23:24 +00:00
|
|
|
[item setTarget:self];
|
|
|
|
[item setAction:@selector(colorPickerAction:)];
|
2017-03-01 22:54:43 +00:00
|
|
|
[self updateColorPicker:item withSettings:settings];
|
2017-03-02 18:23:24 +00:00
|
|
|
return item.autorelease();
|
2017-03-01 22:54:43 +00:00
|
|
|
}
|
2017-03-01 20:50:36 +00:00
|
|
|
|
2017-03-01 22:54:43 +00:00
|
|
|
- (void)updateColorPicker:(NSColorPickerTouchBarItem*)item
|
|
|
|
withSettings:(const mate::PersistentDictionary&)settings {
|
2017-03-01 20:50:36 +00:00
|
|
|
std::vector<std::string> colors;
|
2017-03-30 19:59:18 +00:00
|
|
|
if (settings.Get("availableColors", &colors) && !colors.empty()) {
|
2018-04-20 18:47:04 +00:00
|
|
|
NSColorList* color_list =
|
|
|
|
[[[NSColorList alloc] initWithName:@""] autorelease];
|
2017-03-01 20:50:36 +00:00
|
|
|
for (size_t i = 0; i < colors.size(); ++i) {
|
|
|
|
[color_list insertColor:[self colorFromHexColorString:colors[i]]
|
|
|
|
key:base::SysUTF8ToNSString(colors[i])
|
|
|
|
atIndex:i];
|
|
|
|
}
|
2018-04-20 18:47:04 +00:00
|
|
|
item.colorList = color_list;
|
2017-03-01 20:50:36 +00:00
|
|
|
}
|
|
|
|
|
2017-03-01 22:54:43 +00:00
|
|
|
std::string selectedColor;
|
|
|
|
if (settings.Get("selectedColor", &selectedColor)) {
|
|
|
|
item.color = [self colorFromHexColorString:selectedColor];
|
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]);
|
2018-04-20 18:47:04 +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-03-02 18:23:24 +00:00
|
|
|
base::scoped_nsobject<NSSliderTouchBarItem> item([[NSClassFromString(
|
|
|
|
@"NSSliderTouchBarItem") alloc] initWithIdentifier:identifier]);
|
|
|
|
[item setTarget:self];
|
|
|
|
[item setAction:@selector(sliderAction:)];
|
2017-03-01 01:12:35 +00:00
|
|
|
[self updateSlider:item withSettings:settings];
|
2017-03-02 18:23:24 +00:00
|
|
|
return item.autorelease();
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (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 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;
|
2017-03-01 22:54:43 +00:00
|
|
|
int value = 50;
|
2017-03-01 01:12:35 +00:00
|
|
|
settings.Get("minValue", &minValue);
|
|
|
|
settings.Get("maxValue", &maxValue);
|
2017-03-01 22:54:43 +00:00
|
|
|
settings.Get("value", &value);
|
2017-02-28 20:29:23 +00:00
|
|
|
|
|
|
|
item.slider.minValue = minValue;
|
|
|
|
item.slider.maxValue = maxValue;
|
2017-03-01 22:54:43 +00:00
|
|
|
item.slider.doubleValue = value;
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (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]);
|
2018-04-20 18:47:04 +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-03-02 18:23:24 +00:00
|
|
|
base::scoped_nsobject<NSPopoverTouchBarItem> item([[NSClassFromString(
|
|
|
|
@"NSPopoverTouchBarItem") alloc] initWithIdentifier:identifier]);
|
2017-03-01 01:12:35 +00:00
|
|
|
[self updatePopover:item withSettings:settings];
|
2017-03-02 18:23:24 +00:00
|
|
|
return item.autorelease();
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (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 label;
|
2017-03-03 23:24:32 +00:00
|
|
|
settings.Get("label", &label);
|
|
|
|
item.collapsedRepresentationLabel = base::SysUTF8ToNSString(label);
|
|
|
|
|
2017-03-02 21:37:34 +00:00
|
|
|
gfx::Image image;
|
|
|
|
if (settings.Get("icon", &image)) {
|
2017-02-28 20:29:23 +00:00
|
|
|
item.collapsedRepresentationImage = image.AsNSImage();
|
|
|
|
}
|
|
|
|
|
2017-03-03 17:54:46 +00:00
|
|
|
bool showCloseButton = true;
|
2017-03-03 23:24:32 +00:00
|
|
|
settings.Get("showCloseButton", &showCloseButton);
|
|
|
|
item.showsCloseButton = showCloseButton;
|
2017-02-28 20:29:23 +00:00
|
|
|
|
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)) {
|
2018-04-20 18:47:04 +00:00
|
|
|
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]);
|
2018-04-20 18:47:04 +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;
|
2018-04-20 18:47:04 +00:00
|
|
|
if (!settings.Get("child", &child))
|
|
|
|
return nil;
|
2017-02-28 20:29:23 +00:00
|
|
|
std::vector<mate::PersistentDictionary> items;
|
2018-04-20 18:47:04 +00:00
|
|
|
if (!child.Get("ordereredItems", &items))
|
|
|
|
return nil;
|
2017-02-28 20:29:23 +00:00
|
|
|
|
2017-03-02 17:16:29 +00:00
|
|
|
NSMutableArray* generatedItems = [NSMutableArray array];
|
2017-03-03 23:28:32 +00:00
|
|
|
NSMutableArray* identifiers = [self identifiersFromSettings:items];
|
2017-03-30 20:56:21 +00:00
|
|
|
for (NSUInteger i = 0; i < [identifiers count]; ++i) {
|
2018-04-20 18:47:04 +00:00
|
|
|
if ([identifiers objectAtIndex:i] !=
|
|
|
|
NSTouchBarItemIdentifierOtherItemsProxy) {
|
|
|
|
NSTouchBarItem* generatedItem =
|
|
|
|
[self makeItemForIdentifier:[identifiers objectAtIndex:i]];
|
2017-02-28 20:29:23 +00:00
|
|
|
if (generatedItem) {
|
|
|
|
[generatedItems addObject:generatedItem];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-20 18:47:04 +00:00
|
|
|
return [NSClassFromString(@"NSGroupTouchBarItem")
|
|
|
|
groupItemWithIdentifier:identifier
|
|
|
|
items:generatedItems];
|
2017-02-28 20:29:23 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 17:53:04 +00:00
|
|
|
- (void)updateGroup:(NSGroupTouchBarItem*)item
|
2018-04-20 18:47:04 +00:00
|
|
|
withSettings:(const mate::PersistentDictionary&)settings
|
|
|
|
API_AVAILABLE(macosx(10.12.2)) {
|
2018-02-12 17:53:04 +00:00
|
|
|
mate::PersistentDictionary child;
|
2018-04-20 18:47:04 +00:00
|
|
|
if (!settings.Get("child", &child))
|
|
|
|
return;
|
2018-02-12 17:53:04 +00:00
|
|
|
std::vector<mate::PersistentDictionary> items;
|
2018-04-20 18:47:04 +00:00
|
|
|
if (!child.Get("ordereredItems", &items))
|
|
|
|
return;
|
2018-02-12 17:53:04 +00:00
|
|
|
|
2018-04-20 18:47:04 +00:00
|
|
|
item.groupTouchBar =
|
|
|
|
[self touchBarFromItemIdentifiers:[self identifiersFromSettings:items]];
|
2018-02-12 17:53:04 +00:00
|
|
|
}
|
|
|
|
|
2017-03-10 06:40:39 +00:00
|
|
|
- (NSTouchBarItem*)makeSegmentedControlForID:(NSString*)id
|
2018-04-20 18:47:04 +00:00
|
|
|
withIdentifier:(NSString*)identifier
|
|
|
|
API_AVAILABLE(macosx(10.12.2)) {
|
2017-03-10 06:40:39 +00:00
|
|
|
std::string s_id([id UTF8String]);
|
2018-04-20 18:47:04 +00:00
|
|
|
if (![self hasItemWithID:s_id])
|
|
|
|
return nil;
|
2017-03-10 06:40:39 +00:00
|
|
|
|
|
|
|
mate::PersistentDictionary settings = settings_[s_id];
|
|
|
|
base::scoped_nsobject<NSCustomTouchBarItem> item([[NSClassFromString(
|
|
|
|
@"NSCustomTouchBarItem") alloc] initWithIdentifier:identifier]);
|
|
|
|
|
2018-04-20 18:47:04 +00:00
|
|
|
NSSegmentedControl* control = [NSSegmentedControl
|
|
|
|
segmentedControlWithLabels:[NSMutableArray array]
|
|
|
|
trackingMode:NSSegmentSwitchTrackingSelectOne
|
|
|
|
target:self
|
|
|
|
action:@selector(segmentedControlAction:)];
|
2017-03-10 06:40:39 +00:00
|
|
|
control.tag = [id floatValue];
|
|
|
|
[item setView:control];
|
|
|
|
|
|
|
|
[self updateSegmentedControl:item withSettings:settings];
|
|
|
|
return item.autorelease();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updateSegmentedControl:(NSCustomTouchBarItem*)item
|
2018-04-20 18:47:04 +00:00
|
|
|
withSettings:(const mate::PersistentDictionary&)settings
|
|
|
|
API_AVAILABLE(macosx(10.12.2)) {
|
2017-03-10 06:40:39 +00:00
|
|
|
NSSegmentedControl* control = item.view;
|
|
|
|
|
|
|
|
std::string segmentStyle;
|
|
|
|
settings.Get("segmentStyle", &segmentStyle);
|
2017-03-10 18:11:10 +00:00
|
|
|
if (segmentStyle == "rounded")
|
2017-03-10 06:40:39 +00:00
|
|
|
control.segmentStyle = NSSegmentStyleRounded;
|
|
|
|
else if (segmentStyle == "textured-rounded")
|
|
|
|
control.segmentStyle = NSSegmentStyleTexturedRounded;
|
|
|
|
else if (segmentStyle == "round-rect")
|
|
|
|
control.segmentStyle = NSSegmentStyleRoundRect;
|
|
|
|
else if (segmentStyle == "textured-square")
|
|
|
|
control.segmentStyle = NSSegmentStyleTexturedSquare;
|
|
|
|
else if (segmentStyle == "capsule")
|
|
|
|
control.segmentStyle = NSSegmentStyleCapsule;
|
|
|
|
else if (segmentStyle == "small-square")
|
|
|
|
control.segmentStyle = NSSegmentStyleSmallSquare;
|
|
|
|
else if (segmentStyle == "separated")
|
|
|
|
control.segmentStyle = NSSegmentStyleSeparated;
|
|
|
|
else
|
|
|
|
control.segmentStyle = NSSegmentStyleAutomatic;
|
|
|
|
|
2017-04-28 04:50:58 +00:00
|
|
|
std::string segmentMode;
|
|
|
|
settings.Get("mode", &segmentMode);
|
|
|
|
if (segmentMode == "multiple")
|
|
|
|
control.trackingMode = NSSegmentSwitchTrackingSelectAny;
|
|
|
|
else if (segmentMode == "buttons")
|
|
|
|
control.trackingMode = NSSegmentSwitchTrackingMomentary;
|
|
|
|
else
|
|
|
|
control.trackingMode = NSSegmentSwitchTrackingSelectOne;
|
|
|
|
|
2017-03-10 06:40:39 +00:00
|
|
|
std::vector<mate::Dictionary> segments;
|
|
|
|
settings.Get("segments", &segments);
|
|
|
|
|
|
|
|
control.segmentCount = segments.size();
|
2017-03-30 20:56:21 +00:00
|
|
|
for (size_t i = 0; i < segments.size(); ++i) {
|
2017-03-10 06:40:39 +00:00
|
|
|
std::string label;
|
|
|
|
gfx::Image image;
|
|
|
|
bool enabled = true;
|
|
|
|
segments[i].Get("enabled", &enabled);
|
|
|
|
if (segments[i].Get("label", &label)) {
|
|
|
|
[control setLabel:base::SysUTF8ToNSString(label) forSegment:i];
|
|
|
|
} else if (segments[i].Get("icon", &image)) {
|
|
|
|
[control setImage:image.AsNSImage() forSegment:i];
|
|
|
|
[control setImageScaling:NSImageScaleProportionallyUpOrDown forSegment:i];
|
|
|
|
}
|
|
|
|
[control setEnabled:enabled forSegment:i];
|
|
|
|
}
|
|
|
|
|
|
|
|
int selectedIndex = 0;
|
|
|
|
settings.Get("selectedIndex", &selectedIndex);
|
2017-03-10 18:09:14 +00:00
|
|
|
if (selectedIndex >= 0 && selectedIndex < control.segmentCount)
|
|
|
|
control.selectedSegment = selectedIndex;
|
2017-03-10 06:40:39 +00:00
|
|
|
}
|
|
|
|
|
2017-03-12 23:51:12 +00:00
|
|
|
- (NSTouchBarItem*)makeScrubberForID:(NSString*)id
|
2018-04-20 18:47:04 +00:00
|
|
|
withIdentifier:(NSString*)identifier
|
|
|
|
API_AVAILABLE(macosx(10.12.2)) {
|
2017-03-12 23:51:12 +00:00
|
|
|
std::string s_id([id UTF8String]);
|
2018-04-20 18:47:04 +00:00
|
|
|
if (![self hasItemWithID:s_id])
|
|
|
|
return nil;
|
2017-03-12 23:51:12 +00:00
|
|
|
|
|
|
|
mate::PersistentDictionary settings = settings_[s_id];
|
|
|
|
base::scoped_nsobject<NSCustomTouchBarItem> item([[NSClassFromString(
|
|
|
|
@"NSCustomTouchBarItem") alloc] initWithIdentifier:identifier]);
|
|
|
|
|
2018-04-20 18:47:04 +00:00
|
|
|
NSScrubber* scrubber = [[[NSClassFromString(@"NSScrubber") alloc]
|
|
|
|
initWithFrame:NSZeroRect] autorelease];
|
2017-03-12 23:51:12 +00:00
|
|
|
|
2018-04-20 18:47:04 +00:00
|
|
|
[scrubber registerClass:NSClassFromString(@"NSScrubberTextItemView")
|
|
|
|
forItemIdentifier:TextScrubberItemIdentifier];
|
|
|
|
[scrubber registerClass:NSClassFromString(@"NSScrubberImageItemView")
|
|
|
|
forItemIdentifier:ImageScrubberItemIdentifier];
|
2017-03-12 23:51:12 +00:00
|
|
|
|
|
|
|
scrubber.delegate = self;
|
2017-03-13 18:06:41 +00:00
|
|
|
scrubber.dataSource = self;
|
2017-03-12 23:51:12 +00:00
|
|
|
scrubber.identifier = id;
|
2017-03-13 17:07:48 +00:00
|
|
|
|
2017-03-12 23:51:12 +00:00
|
|
|
[item setView:scrubber];
|
|
|
|
[self updateScrubber:item withSettings:settings];
|
2017-03-13 18:06:41 +00:00
|
|
|
|
2017-03-12 23:51:12 +00:00
|
|
|
return item.autorelease();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updateScrubber:(NSCustomTouchBarItem*)item
|
2018-04-20 18:47:04 +00:00
|
|
|
withSettings:(const mate::PersistentDictionary&)settings
|
|
|
|
API_AVAILABLE(macosx(10.12.2)) {
|
2017-03-12 23:51:12 +00:00
|
|
|
NSScrubber* scrubber = item.view;
|
2017-03-14 07:57:57 +00:00
|
|
|
|
|
|
|
bool showsArrowButtons = false;
|
|
|
|
settings.Get("showArrowButtons", &showsArrowButtons);
|
|
|
|
scrubber.showsArrowButtons = showsArrowButtons;
|
|
|
|
|
|
|
|
std::string selectedStyle;
|
|
|
|
std::string overlayStyle;
|
|
|
|
settings.Get("selectedStyle", &selectedStyle);
|
|
|
|
settings.Get("overlayStyle", &overlayStyle);
|
|
|
|
|
|
|
|
if (selectedStyle == "outline") {
|
2018-04-20 18:47:04 +00:00
|
|
|
scrubber.selectionBackgroundStyle =
|
|
|
|
[NSClassFromString(@"NSScrubberSelectionStyle") outlineOverlayStyle];
|
2017-03-14 07:57:57 +00:00
|
|
|
} else if (selectedStyle == "background") {
|
2018-04-20 18:47:04 +00:00
|
|
|
scrubber.selectionBackgroundStyle =
|
|
|
|
[NSClassFromString(@"NSScrubberSelectionStyle") roundedBackgroundStyle];
|
2017-03-14 07:57:57 +00:00
|
|
|
} else {
|
|
|
|
scrubber.selectionBackgroundStyle = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (overlayStyle == "outline") {
|
2018-04-20 18:47:04 +00:00
|
|
|
scrubber.selectionOverlayStyle =
|
|
|
|
[NSClassFromString(@"NSScrubberSelectionStyle") outlineOverlayStyle];
|
2017-03-14 07:57:57 +00:00
|
|
|
} else if (overlayStyle == "background") {
|
2018-04-20 18:47:04 +00:00
|
|
|
scrubber.selectionOverlayStyle =
|
|
|
|
[NSClassFromString(@"NSScrubberSelectionStyle") roundedBackgroundStyle];
|
2017-03-14 07:57:57 +00:00
|
|
|
} else {
|
|
|
|
scrubber.selectionOverlayStyle = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string mode;
|
|
|
|
settings.Get("mode", &mode);
|
|
|
|
if (mode == "fixed") {
|
|
|
|
scrubber.mode = NSScrubberModeFixed;
|
|
|
|
} else {
|
|
|
|
scrubber.mode = NSScrubberModeFree;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool continuous = true;
|
|
|
|
settings.Get("continuous", &continuous);
|
|
|
|
scrubber.continuous = continuous;
|
|
|
|
|
2017-03-13 18:06:41 +00:00
|
|
|
[scrubber reloadData];
|
|
|
|
}
|
|
|
|
|
2018-04-20 18:47:04 +00:00
|
|
|
- (NSInteger)numberOfItemsForScrubber:(NSScrubber*)scrubber
|
|
|
|
API_AVAILABLE(macosx(10.12.2)) {
|
2017-03-13 18:06:41 +00:00
|
|
|
std::string s_id([[scrubber identifier] UTF8String]);
|
2018-04-20 18:47:04 +00:00
|
|
|
if (![self hasItemWithID:s_id])
|
|
|
|
return 0;
|
2017-03-13 18:06:41 +00:00
|
|
|
|
|
|
|
mate::PersistentDictionary settings = settings_[s_id];
|
|
|
|
std::vector<mate::PersistentDictionary> items;
|
|
|
|
settings.Get("items", &items);
|
|
|
|
return items.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSScrubberItemView*)scrubber:(NSScrubber*)scrubber
|
2018-04-20 18:47:04 +00:00
|
|
|
viewForItemAtIndex:(NSInteger)index
|
|
|
|
API_AVAILABLE(macosx(10.12.2)) {
|
2017-03-13 18:06:41 +00:00
|
|
|
std::string s_id([[scrubber identifier] UTF8String]);
|
2018-04-20 18:47:04 +00:00
|
|
|
if (![self hasItemWithID:s_id])
|
|
|
|
return nil;
|
2017-03-12 23:51:12 +00:00
|
|
|
|
2017-03-13 18:06:41 +00:00
|
|
|
mate::PersistentDictionary settings = settings_[s_id];
|
2017-03-12 23:51:12 +00:00
|
|
|
std::vector<mate::PersistentDictionary> items;
|
2018-04-20 18:47:04 +00:00
|
|
|
if (!settings.Get("items", &items))
|
|
|
|
return nil;
|
2017-03-14 07:57:57 +00:00
|
|
|
|
2018-04-20 18:47:04 +00:00
|
|
|
if (index >= static_cast<NSInteger>(items.size()))
|
|
|
|
return nil;
|
2017-03-14 07:57:57 +00:00
|
|
|
|
2017-03-13 18:06:41 +00:00
|
|
|
mate::PersistentDictionary item = items[index];
|
2017-03-12 23:51:12 +00:00
|
|
|
|
2017-03-13 18:06:41 +00:00
|
|
|
NSScrubberItemView* itemView;
|
|
|
|
std::string title;
|
2017-03-12 23:51:12 +00:00
|
|
|
|
2017-03-13 18:06:41 +00:00
|
|
|
if (item.Get("label", &title)) {
|
2018-04-20 18:47:04 +00:00
|
|
|
NSScrubberTextItemView* view =
|
|
|
|
[scrubber makeItemWithIdentifier:TextScrubberItemIdentifier owner:self];
|
2017-03-13 18:06:41 +00:00
|
|
|
view.title = base::SysUTF8ToNSString(title);
|
|
|
|
itemView = view;
|
|
|
|
} else {
|
2018-04-20 18:47:04 +00:00
|
|
|
NSScrubberImageItemView* view =
|
|
|
|
[scrubber makeItemWithIdentifier:ImageScrubberItemIdentifier
|
|
|
|
owner:self];
|
2017-03-13 18:06:41 +00:00
|
|
|
gfx::Image image;
|
2017-03-15 16:33:11 +00:00
|
|
|
if (item.Get("icon", &image)) {
|
2017-03-13 18:06:41 +00:00
|
|
|
view.image = image.AsNSImage();
|
|
|
|
}
|
|
|
|
itemView = view;
|
|
|
|
}
|
|
|
|
|
|
|
|
return itemView;
|
2017-03-12 23:51:12 +00:00
|
|
|
}
|
|
|
|
|
2018-04-20 18:47:04 +00:00
|
|
|
- (NSSize)scrubber:(NSScrubber*)scrubber
|
|
|
|
layout:(NSScrubberFlowLayout*)layout
|
|
|
|
sizeForItemAtIndex:(NSInteger)itemIndex API_AVAILABLE(macosx(10.12.2)) {
|
2017-10-21 23:00:27 +00:00
|
|
|
NSInteger width = 50;
|
|
|
|
NSInteger height = 30;
|
|
|
|
NSInteger margin = 15;
|
|
|
|
NSSize defaultSize = NSMakeSize(width, height);
|
|
|
|
|
|
|
|
std::string s_id([[scrubber identifier] UTF8String]);
|
2018-04-20 18:47:04 +00:00
|
|
|
if (![self hasItemWithID:s_id])
|
|
|
|
return defaultSize;
|
2017-10-21 23:00:27 +00:00
|
|
|
|
|
|
|
mate::PersistentDictionary settings = settings_[s_id];
|
|
|
|
std::vector<mate::PersistentDictionary> items;
|
2018-04-20 18:47:04 +00:00
|
|
|
if (!settings.Get("items", &items))
|
|
|
|
return defaultSize;
|
2017-10-21 23:00:27 +00:00
|
|
|
|
2018-04-20 18:47:04 +00:00
|
|
|
if (itemIndex >= static_cast<NSInteger>(items.size()))
|
|
|
|
return defaultSize;
|
2017-10-21 23:00:27 +00:00
|
|
|
|
|
|
|
mate::PersistentDictionary item = items[itemIndex];
|
|
|
|
std::string title;
|
|
|
|
|
|
|
|
if (item.Get("label", &title)) {
|
|
|
|
NSSize size = NSMakeSize(CGFLOAT_MAX, CGFLOAT_MAX);
|
2018-04-20 18:47:04 +00:00
|
|
|
NSRect textRect = [base::SysUTF8ToNSString(title)
|
|
|
|
boundingRectWithSize:size
|
|
|
|
options:NSStringDrawingUsesLineFragmentOrigin |
|
|
|
|
NSStringDrawingUsesFontLeading
|
|
|
|
attributes:@{
|
|
|
|
NSFontAttributeName : [NSFont systemFontOfSize:0]
|
|
|
|
}];
|
2017-10-21 23:00:27 +00:00
|
|
|
|
|
|
|
width = textRect.size.width + margin;
|
|
|
|
} else {
|
|
|
|
gfx::Image image;
|
|
|
|
if (item.Get("icon", &image)) {
|
|
|
|
width = image.AsNSImage().size.width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NSMakeSize(width, height);
|
|
|
|
}
|
|
|
|
|
2017-02-28 20:29:23 +00:00
|
|
|
@end
|